-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCoordinateCreator.js
More file actions
323 lines (278 loc) · 9.17 KB
/
Copy pathCoordinateCreator.js
File metadata and controls
323 lines (278 loc) · 9.17 KB
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import { Vertex } from "./vertex.js";
export class CoordinateCreator {
static createCubeCoords(size) {
const cubeCoords = [];
const baseSize = size * 0.6;
cubeCoords.push([
new Vertex(baseSize, baseSize, baseSize), // window wall
new Vertex(baseSize, baseSize, -baseSize),
new Vertex(baseSize, -baseSize, -baseSize),
new Vertex(baseSize, -baseSize, baseSize),
]);
cubeCoords.push([
new Vertex(-baseSize, baseSize, baseSize), // door wall
new Vertex(-baseSize, baseSize, -baseSize),
new Vertex(-baseSize, -baseSize, -baseSize),
new Vertex(-baseSize, -baseSize, baseSize),
]);
cubeCoords.push([
new Vertex(baseSize, baseSize, baseSize), // bathroom wall
new Vertex(baseSize, -baseSize, baseSize),
new Vertex(-baseSize, -baseSize, baseSize),
new Vertex(-baseSize, baseSize, baseSize),
]);
cubeCoords.push([
new Vertex(baseSize, baseSize, -baseSize), // opposite to bathroom
new Vertex(baseSize, -baseSize, -baseSize),
new Vertex(-baseSize, -baseSize, -baseSize),
new Vertex(-baseSize, baseSize, -baseSize),
]);
cubeCoords.push([
new Vertex(baseSize, baseSize, baseSize), // top
new Vertex(baseSize, baseSize, -baseSize),
new Vertex(-baseSize, baseSize, -baseSize),
new Vertex(-baseSize, baseSize, baseSize),
]);
cubeCoords.push([
new Vertex(baseSize, -baseSize, baseSize), // bottom
new Vertex(baseSize, -baseSize, -baseSize),
new Vertex(-baseSize, -baseSize, -baseSize),
new Vertex(-baseSize, -baseSize, baseSize),
]);
return cubeCoords;
}
static createTetrahedronCoords(size) {
const tetrahedronCoords = [];
const baseSize = size * 0.6;
tetrahedronCoords.push([
new Vertex(baseSize, baseSize, baseSize),
new Vertex(-baseSize, -baseSize, baseSize),
new Vertex(-baseSize, baseSize, -baseSize),
]);
tetrahedronCoords.push([
new Vertex(baseSize, baseSize, baseSize),
new Vertex(-baseSize, -baseSize, baseSize),
new Vertex(baseSize, -baseSize, -baseSize),
]);
tetrahedronCoords.push([
new Vertex(-baseSize, baseSize, -baseSize),
new Vertex(baseSize, -baseSize, -baseSize),
new Vertex(baseSize, baseSize, baseSize),
]);
tetrahedronCoords.push([
new Vertex(-baseSize, baseSize, -baseSize),
new Vertex(baseSize, -baseSize, -baseSize),
new Vertex(-baseSize, -baseSize, baseSize),
]);
return tetrahedronCoords;
}
static createOctahedronCoords(size) {
const baseSize = size * 1;
const top = new Vertex(0, 0, baseSize);
const bottom = new Vertex(0, 0, -baseSize);
const front = new Vertex(0, baseSize, 0);
const back = new Vertex(0, -baseSize, 0);
const left = new Vertex(-baseSize, 0, 0);
const right = new Vertex(baseSize, 0, 0);
const octahedronCoords = [];
// Top pyramid
octahedronCoords.push([top, front, right]);
octahedronCoords.push([top, right, back]);
octahedronCoords.push([top, back, left]);
octahedronCoords.push([top, left, front]);
// Bottom pyramid
octahedronCoords.push([bottom, right, front]);
octahedronCoords.push([bottom, back, right]);
octahedronCoords.push([bottom, left, back]);
octahedronCoords.push([bottom, front, left]);
return octahedronCoords;
}
static createSphereCoords(size = 100) {
const radius = size;
const segments = Math.max(8, Math.floor(size / 10)); // Avoid too few segments
const sphereFaces = [];
for (let i = 0; i < segments; i++) {
for (let j = 0; j < segments; j++) {
const theta1 = (2 * Math.PI * i) / segments;
const theta2 = (2 * Math.PI * (i + 1)) / segments;
const phi1 = (Math.PI * j) / segments;
const phi2 = (Math.PI * (j + 1)) / segments;
const v1 = new Vertex(
radius * Math.cos(theta1) * Math.sin(phi1),
radius * Math.sin(theta1) * Math.sin(phi1),
radius * Math.cos(phi1)
);
const v2 = new Vertex(
radius * Math.cos(theta2) * Math.sin(phi1),
radius * Math.sin(theta2) * Math.sin(phi1),
radius * Math.cos(phi1)
);
const v3 = new Vertex(
radius * Math.cos(theta1) * Math.sin(phi2),
radius * Math.sin(theta1) * Math.sin(phi2),
radius * Math.cos(phi2)
);
const v4 = new Vertex(
radius * Math.cos(theta2) * Math.sin(phi2),
radius * Math.sin(theta2) * Math.sin(phi2),
radius * Math.cos(phi2)
);
// Two triangles per quad
sphereFaces.push([v1, v2, v3]);
sphereFaces.push([v3, v2, v4]);
}
}
return sphereFaces;
}
static createIcosahedronCoords(size) {
const icosahedronCoords = [];
const phi = (1 + Math.sqrt(5)) / 2; // Golden ratio
const baseSize = size * 0.6;
// Define 12 vertices
const vertices = [
new Vertex(-baseSize, phi * baseSize, 0),
new Vertex(baseSize, phi * baseSize, 0),
new Vertex(-baseSize, -phi * baseSize, 0),
new Vertex(baseSize, -phi * baseSize, 0),
new Vertex(0, -baseSize, phi * baseSize),
new Vertex(0, baseSize, phi * baseSize),
new Vertex(0, -baseSize, -phi * baseSize),
new Vertex(0, baseSize, -phi * baseSize),
new Vertex(phi * baseSize, 0, -baseSize),
new Vertex(phi * baseSize, 0, baseSize),
new Vertex(-phi * baseSize, 0, -baseSize),
new Vertex(-phi * baseSize, 0, baseSize),
];
const faces = [
[0, 11, 5],
[0, 5, 1],
[0, 1, 7],
[0, 7, 10],
[0, 10, 11],
[1, 5, 9],
[5, 11, 4],
[11, 10, 2],
[10, 7, 6],
[7, 1, 8],
[3, 9, 4],
[3, 4, 2],
[3, 2, 6],
[3, 6, 8],
[3, 8, 9],
[4, 9, 5],
[2, 4, 11],
[6, 2, 10],
[8, 6, 7],
[9, 8, 1],
];
for (const face of faces) {
icosahedronCoords.push([
vertices[face[0]],
vertices[face[1]],
vertices[face[2]],
]);
}
return icosahedronCoords;
}
static createTesseractCoords(size) {
const tesseractFaces = [];
const halfSize = size / 2;
const vertices = new Array(16);
for (let i = 0; i < 8; i++) {
const x = (i & 1) === 0 ? -halfSize : halfSize;
const y = (i & 2) === 0 ? -halfSize : halfSize;
const z = (i & 4) === 0 ? -halfSize : halfSize;
vertices[i] = new Vertex(x, y, z);
}
for (let i = 0; i < 8; i++) {
vertices[i + 8] = new Vertex(
vertices[i].x * 0.7,
vertices[i].y * 0.7,
vertices[i].z * 0.7
);
}
for (let i = 0; i < 8; i++) {
tesseractFaces.push([vertices[i], vertices[i + 8]]);
}
const edges = [
0, 1, 0, 2, 0, 4, 1, 3, 1, 5, 2, 3, 2, 6, 3, 7, 4, 5, 4, 6, 5, 7, 6, 7,
];
for (let i = 0; i < edges.length; i += 2) {
tesseractFaces.push([vertices[edges[i]], vertices[edges[i + 1]]]);
tesseractFaces.push([vertices[edges[i] + 8], vertices[edges[i + 1] + 8]]);
}
return tesseractFaces;
}
static createTorusCoords(size) {
const polygons = [];
const baseSize = size * 0.6;
const segMajor = 20,
segMinor = 20;
const R = baseSize;
const r = baseSize / 2.0;
const majorStep = (2 * Math.PI) / segMajor;
const minorStep = (2 * Math.PI) / segMinor;
const grid = Array.from({ length: segMajor }, () => new Array(segMinor));
for (let i = 0; i < segMajor; i++) {
const phi = i * majorStep;
for (let j = 0; j < segMinor; j++) {
const theta = j * minorStep;
const x = (R + r * Math.cos(theta)) * Math.cos(phi);
const y = (R + r * Math.cos(theta)) * Math.sin(phi);
const z = r * Math.sin(theta);
grid[i][j] = new Vertex(x, y, z);
}
}
for (let i = 0; i < segMajor; i++) {
const nextI = (i + 1) % segMajor;
for (let j = 0; j < segMinor; j++) {
const nextJ = (j + 1) % segMinor;
polygons.push([
grid[i][j],
grid[nextI][j],
grid[nextI][nextJ],
grid[i][nextJ],
]);
}
}
return polygons;
}
static createMobiusStripCoords(size) {
size = size * 1.5;
const polygons = [];
const segU = 40;
const segV = 10;
const R = size * 0.6;
const w = R / 3.0;
const du = (2 * Math.PI) / segU;
const dv = (2 * w) / segV;
const grid = Array.from({ length: segU }, () => new Array(segV + 1));
for (let i = 0; i < segU; i++) {
const u = i * du;
for (let j = 0; j <= segV; j++) {
const v = -w + j * dv;
const x = (R + v * Math.cos(u / 2)) * Math.cos(u);
const y = (R + v * Math.cos(u / 2)) * Math.sin(u);
const z = v * Math.sin(u / 2);
grid[i][j] = new Vertex(x, y, z);
}
}
for (let i = 0; i < segU; i++) {
const nextI = (i + 1) % segU;
for (let j = 0; j < segV; j++) {
const v00 = grid[i][j];
const v01 = grid[i][j + 1];
let v10, v11;
if (i !== segU - 1) {
v10 = grid[nextI][j];
v11 = grid[nextI][j + 1];
} else {
v10 = grid[nextI][segV - j];
v11 = grid[nextI][segV - (j + 1)];
}
polygons.push([v00, v01, v11, v10]);
}
}
return polygons;
}
}