-
Notifications
You must be signed in to change notification settings - Fork 0
/
sphere.js
127 lines (109 loc) · 3.09 KB
/
sphere.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import * as THREE from "three";
import {
getAllImages,
clearSelection,
getSelectedImages,
paintRangeImages,
clearRangeImages,
} from "./interaction";
import { create, all } from "mathjs";
import { saveSphere } from "./inspect";
const math = create(all, {});
var radius = 0.5;
var C;
var scene;
var sphereObject;
function setScene(sce) {
scene = sce;
}
function openSphericalImages() {
let images = getAllImages();
let json = [];
images.forEach((object) => {
let P_inter = object.userData.intersection;
let P_real = object.position;
if (P_inter == null) return;
if (C.distanceTo(P_real) < radius) {
const real_pos = get2DCoords(P_real);
const inter_pos = get2DCoords(P_inter);
json.push({
name: object.name,
x_real: real_pos.x,
y_real: real_pos.y,
x_inter: inter_pos.x,
y_inter: inter_pos.y,
isLandscape: object.userData.isLandscape,
heightToWidthRatio: object.userData.heightToWidthRatio,
zoom: object.userData.zoom,
});
}
});
let jsonContent = JSON.stringify(json);
localStorage.setItem("images", jsonContent);
const url = "openseadragon.html?mode=spherical";
window.open(url, "_blank");
clearSelection();
}
function get2DCoords(P) {
const V = new THREE.Vector3().subVectors(P, C).normalize();
const phi = math.acos(V.y);
const theta = math.atan2(V.x, V.z);
return { x: -theta, y: phi }; // TODO: Check if it is correct
}
function applySphericalRadius(r) {
clearRangeImages();
sphereObject.scale.set(1 / radius, 1 / radius, 1 / radius);
sphereObject.scale.set(r, r, r);
radius = r;
paintRange();
}
function createSphere() {
var imagesSelected = getSelectedImages();
if (imagesSelected.size != 1) {
console.log("You need to select exactly 1 image");
return;
}
const images = Array.from(imagesSelected);
C = images[0].position;
const geometry = new THREE.SphereGeometry(1, 10, 10);
const material = new THREE.MeshBasicMaterial({
color: 0x000000,
wireframe: true,
wireframeLinewidth: 0.5,
});
sphereObject = new THREE.Mesh(geometry, material);
scene.add(sphereObject);
sphereObject.scale.set(radius, radius, radius);
sphereObject.position.set(C.x, C.y, C.z);
clearSelection();
paintRange();
}
function cancelSphere() {
C = null;
scene.remove(sphereObject);
clearSelection();
sphereObject = null;
clearRangeImages();
}
function paintRange() {
let images = getAllImages();
let rangeImages = new Set();
images.forEach((object) => {
const P = object.position;
if (C.distanceTo(P) < radius) {
rangeImages.add(object);
}
});
paintRangeImages(rangeImages);
}
function saveSphereToInspectMode() {
saveSphere(C, radius);
}
export {
openSphericalImages,
createSphere,
cancelSphere,
applySphericalRadius,
setScene,
saveSphereToInspectMode,
};