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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
'use strict';
export class Mat3 {
constructor(arr = null) {
if (arr == null) {
this.identity();
}
else {
this.arr = arr;
}
}
i(row, col) {
const r = row-1
const c = col-1
const index = (3*r)+c
return this.arr[index]
}
identity() {
this.arr = [
1, 0, 0,
0, 1, 0,
0, 0, 1,
];
return this;
}
mul(other) {
const out = []
out[0] = (this.i(1,1)*other.i(1,1)) + (this.i(1,2)*other.i(2,1)) + (this.i(1,3)*other.i(3,1));
out[1] = (this.i(1,1)*other.i(1,2)) + (this.i(1,2)*other.i(2,2)) + (this.i(1,3)*other.i(3,2));
out[2] = (this.i(1,1)*other.i(1,3)) + (this.i(1,2)*other.i(2,3)) + (this.i(1,3)*other.i(3,3));
out[3] = (this.i(2,1)*other.i(1,1)) + (this.i(2,2)*other.i(2,1)) + (this.i(2,3)*other.i(3,1));
out[4] = (this.i(2,1)*other.i(1,2)) + (this.i(2,2)*other.i(2,2)) + (this.i(2,3)*other.i(3,2));
out[5] = (this.i(2,1)*other.i(1,3)) + (this.i(2,2)*other.i(2,3)) + (this.i(2,3)*other.i(3,3));
out[6] = (this.i(3,1)*other.i(1,1)) + (this.i(3,2)*other.i(2,1)) + (this.i(3,3)*other.i(3,1));
out[7] = (this.i(3,1)*other.i(1,2)) + (this.i(3,2)*other.i(2,2)) + (this.i(3,3)*other.i(3,2));
out[8] = (this.i(3,1)*other.i(1,3)) + (this.i(3,2)*other.i(2,3)) + (this.i(3,3)*other.i(3,3));
this.arr = out
return this
}
mulv(vec) {
const x = (this.i(1,1)*vec.x) + (this.i(1,2)*vec.y) + (this.i(1,3)*vec.z);
const y = (this.i(2,1)*vec.x) + (this.i(2,2)*vec.y) + (this.i(2,3)*vec.z);
const z = (this.i(3,1)*vec.x) + (this.i(3,2)*vec.y) + (this.i(3,3)*vec.z);
return new Vec3(x, y, z);
}
rotation(axis, angle) {
const cos = Math.cos(angle);
const sin = Math.sin(angle);
const mcos = 1-cos;
const msin = 1-sin;
this.arr[0] = cos + (axis.x * axis.x * mcos);
this.arr[1] = (axis.x * axis.y * mcos) - (axis.z * sin);
this.arr[2] = (axis.x * axis.z * mcos) + (axis.y * sin);
this.arr[3] = (axis.y * axis.x * mcos) + (axis.z * sin);
this.arr[4] = cos + (axis.y * axis.y * mcos);
this.arr[5] = (axis.y * axis.z * mcos) - (axis.x * sin);
this.arr[6] = (axis.z * axis.x * mcos) - (axis.y * sin);
this.arr[7] = (axis.z * axis.y * mcos) + (axis.x * sin);
this.arr[8] = cos + (axis.z * axis.z * mcos);
return this
}
}
export class Point {
constructor(latitude, longitude) {
this.lat = latitude;
this.long = longitude;
}
normal() {
const x = Math.cos(this.lat) * Math.cos(this.long)
const y = Math.cos(this.lat) * Math.sin(this.long)
const z = Math.sin(this.lat)
return new Vec3(x, y, z)
}
}
export class Vec3 {
constructor(x, y, z) {
this.x = x;
this.y = y;
this.z = z;
}
normalize() {
const len2 = (this.x*this.x) + (this.y*this.y) + (this.z*this.z);
const len = Math.sqrt(len2);
this.x = this.x/len;
this.y = this.y/len;
this.z = this.z/len;
return this;
}
point() {
const latitude = Math.asin(this.z);
const longitute =
Math.sign(this.this.y) * Math.acos(
this.x / Math.sqrt( (this.x*this.x) + (this.y*this.y) )
)
return new Point(latitude, longitude)
}
copy() {
return new Vec3(this.x, this.y, this.z);
}
add(vec) {
this.x = this.x + vec.x;
this.y = this.y + vec.y;
this.z = this.z + vec.z;
return this
}
sub(vec) {
this.x = this.x - vec.x;
this.y = this.y - vec.y;
this.z = this.z - vec.z;
return this;
}
scale(a) {
this.x = this.x * a;
this.y = this.y * a;
this.z = this.z * a;
return this;
}
dot(vec) {
return (this.x * vec.x) + (this.y * vec.y) + (this.z * vec.z);
}
cross(vec) {
const x = (this.y*vec.z) - (this.z*vec.y);
const y = (this.z*vec.x) - (this.x*vec.z);
const z = (this.x*vec.y) - (this.y*vec.x);
return new Vec3(x, y, z);
}
transform(matrix) {
return matrix.mulv(this);
}
render(ct, view) {
const viewNorm = view.mulv(this);
if (viewNorm.z >= -0.01) {
ct.beginPath()
ct.arc(viewNorm.x, viewNorm.y, 0.01, 0, 2*Math.PI);
ct.fill();
}
}
}
export function RandomNormal() {
const theta = 2*Math.PI*Math.random();
const z = 2*Math.random() - 1;
const s = Math.sqrt(1-(z*z));
return new Vec3(s*Math.cos(theta), s*Math.sin(theta), z).normalize();
}
export class Shape {
constructor(normals) {
this.normals = normals;
}
translate(circle, angle) {
const transform = new Mat3().rotation(circle, angle);
for (let i=0; i<this.normals.length; i++) {
this.normals[i] = transform.mulv(this.normals[i]);
}
}
getNextRenderPoint(ct, start, view) {
for (let i=start; i<this.normals.length; i++) {
const v = view.mulv(this.normals[i]);
if (v.z >= -0.01) {
if (i != start) {
ct.stroke();
ct.beginPath();
ct.moveTo(v.x, v.y);
}
return [ i+1, v ];
}
}
return [null, null];
}
render(ct, view) {
ct.beginPath();
let i = 0;
let v;
[ i, v ] = this.getNextRenderPoint(ct, i, view);
if (v == null) {
// no renderable points
return;
}
ct.moveTo(v.x, v.y);
let count = 0;
while(v != null) {
count += 1;
ct.lineTo(v.x, v.y);
[ i, v ] = this.getNextRenderPoint(ct, i, view);
}
if (count == this.normals.length) {
ct.closePath();
}
ct.stroke();
}
}
export function ConvexHull(points) {
const hull = [];
console.log("compute center");
// compute the average of the group of points
let center = new Vec3(0, 0, 0);
for (let point of points) {
center.add(point);
}
center.scale(1/points.length).normalize;
console.log("find furthest");
// find the furthest point from the center
let furthest = null;
let mindot = 1;
for (let point of points) {
const d = center.dot(point);
if (d < mindot) {
mindot = d;
furthest = point;
}
}
const t0 = furthest.copy().sub(center).cross(furthest).normalize();
function getNextHullPoint(p, tangent) {
let next = null;
let max = -1;
for (let point of points) {
const v = point.copy().sub(p).normalize();
const d = v.dot(tangent);
if (d > max) {
max = d;
next = point;
}
}
return next;
}
function tangent(p, prev) {
return p.copy().sub(prev).normalize();
}
hull.push[furthest];
let p = getNextHullPoint(furthest, t0);
let tan = tangent(p, furthest);
// begin loop
console.log("loop");
let count = 0;
while (p != null) {
count += 1;
hull.push(p);
p = getNextHullPoint(p, tan);
tan = tangent(p, hull[hull.length-1]);
if (p == furthest || count > 50) {
console.log(count);
p = null;
}
}
return new Shape(hull);
}
|