-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel.java
483 lines (436 loc) · 13.6 KB
/
Model.java
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import java.util.ArrayList;
/**
* Created by Nick on 11/18/2015.
*/
public class Model
{
// Contains the points of the polygon
public ArrayList Xs; // X points
public ArrayList Ys; // Y points
public ArrayList Zs; // Z points
public Triangle[] triangles;
public Point[] vertices;
public int scale;
public String shape;
public Model(String shape, int[] args)
{
this.shape = shape;
clearVertices();
scale = 0;
if(args != null)
{
scale = args[0];
}
if(shape.equalsIgnoreCase("cube"))
{
triangles = new Triangle[12]; // A cube has 6 square faces, triangulated to 12 triangles total
vertices = new Point[8]; // 8 vertices for a cube
int x = scale;
Point XYZ, XYz, XyZ, Xyz, xYZ, xYz, xyZ, xyz;
Point a, b, c, d, e, f, g, h;
XYZ = new Point( x, x, x);
XYz = new Point( x, x, -x);
XyZ = new Point( x, -x, x);
Xyz = new Point( x, -x, -x);
xYZ = new Point(-x, x, x);
xYz = new Point(-x, x, -x);
xyZ = new Point(-x, -x, x);
xyz = new Point(-x, -x, -x);
a = XYZ;
b = XYz;
c = XyZ;
d = Xyz;
e = xYZ;
f = xYz;
g = xyZ;
h = xyz;
vertices[0] = a;
vertices[1] = b;
vertices[2] = c;
vertices[3] = d;
vertices[4] = e;
vertices[5] = f;
vertices[6] = g;
vertices[7] = h;
retriangulate();
}
else if(shape.equalsIgnoreCase("axis"))
{
scale *= 2; // Make it 5 times bigger because it looks better
createAxis(scale);
}
else if(shape.equalsIgnoreCase("pyramid"))
{
vertices = new Point[5];
triangles = new Triangle[6];
int x = scale;
vertices[0] = new Point(-x, 0, x);
vertices[1] = new Point(-x, 0, -x);
vertices[2] = new Point(x, 0, x);
vertices[3] = new Point(x, 0, -x);
vertices[4] = new Point(0, 2*x, 0);
retriangulate();
}
else if(shape.equalsIgnoreCase("arrow"))
{
addPoint(-1, 0, -1);
addPoint(-1, 0, 1);
addPoint(2, 0, 1);
addPoint(4, 0, 0);
addPoint(2, 0, -2);
addPoint(2, 0, -1);
}
else
{
addPoint(0, 0, 0);
}
}
/**
* Gets the current amount of vertices in the model.
*
* @return The number of vertices in the model.
*/
public int ptCount()
{
return Xs.size();
}
/**
* Provides the X-value of the vertex at the specified index.
*
* @param i The index of the point.
* @return The X value of the point.
*/
public double getX(int i)
{
return Double.parseDouble(Xs.get(i).toString());
}
/**
* Provides the Y-value of the vertex at the specified index.
*
* @param i The index of the point.
* @return The Y value of the point.
*/
public double getY(int i)
{
return Double.parseDouble(Ys.get(i).toString());
}
/**
* Provides the Z-value of the vertex at the specified index.
*
* @param i The index of the point.
* @return The Z value of the point.
*/
public double getZ(int i)
{
return Double.parseDouble(Zs.get(i).toString());
}
/**
* Provides a way of resetting the model's vertices
*/
public void clearVertices()
{
// Reset points
Xs = new ArrayList();
Ys = new ArrayList();
Zs = new ArrayList();
triangles = new Triangle[0];
}
public void retriangulate()
{
if(shape.equals("cube"))
{
Point a, b, c, d, e, f, g, h;
Triangle bac, bcd, aeg, agc, fea, fab, efh, ehg, fbd, fdh, gdc, ghd;
a = vertices[0];
b = vertices[1];
c = vertices[2];
d = vertices[3];
e = vertices[4];
f = vertices[5];
g = vertices[6];
h = vertices[7];
bac = new Triangle(b, a, c);
bcd = new Triangle(b, c, d);
aeg = new Triangle(a, e, g);
agc = new Triangle(a, g, c);
fea = new Triangle(f, e, a);
fab = new Triangle(f, a, b);
efh = new Triangle(e, f, h);
ehg = new Triangle(e, h, g);
fbd = new Triangle(f, b, d);
fdh = new Triangle(f, d, h);
gdc = new Triangle(g, d, c);
ghd = new Triangle(g, h, d);
triangles[0] = bac;
triangles[1] = bcd;
triangles[2] = aeg;
triangles[3] = agc;
triangles[4] = fea;
triangles[5] = fab;
triangles[6] = efh;
triangles[7] = ehg;
triangles[8] = fbd;
triangles[9] = fdh;
triangles[10] = gdc;
triangles[11] = ghd;
}
else if(shape.equals("pyramid"))
{
Point a, b, c, d, e;
Triangle bca, bdc, ecd, eac, eba, edb;
a = vertices[0];
b = vertices[1];
c = vertices[2];
d = vertices[3];
e = vertices[4];
bca = new Triangle(b, c, a);
bdc = new Triangle(b, d, c);
ecd = new Triangle(e, c, d);
eac = new Triangle(e, a, c);
eba = new Triangle(e, b, a);
edb = new Triangle(e, d, b);
triangles[0] = bca;
triangles[1] = bdc;
triangles[2] = ecd;
triangles[3] = eac;
triangles[4] = eba;
triangles[5] = edb;
}
}
public void addTriangle(int i, Point a, Point b, Point c)
{
triangles[i] = new Triangle(a, b, c);
}
/**
* Adds a vertex at the specified location. This method should be used in groups of three's to specify triangles.
*
* @param x The X value of the point.
* @param y The Y Value of the point.
* @param z The Z Value of the point.
*/
public void addPoint(double x, double y, double z)
{
Xs.add(x);
Ys.add(y);
Zs.add(z);
}
/**
* Provides the center point of the polygon based on the X and Y coordinates in Xs and Ys ArrayLists.
*
* @return The Centroid of the polygon
*/
public void createAxis(int scale)
{
addPoint( scale, 0, 0);
addPoint(-scale, 0, 0);
addPoint(0, scale, 0);
addPoint(0, -scale, 0);
addPoint(0, 0, scale);
addPoint(0, 0, -scale);
}
/**
* Provides the center of the model in XYZ space.
*
* @return An array containing the [0]X, [1]Y, [2]Z Values of the centroid of the model.
*/
public double[] getCentroid()
{
double xSum = 0;
double ySum = 0;
double zSum = 0;
int pointCount = getVertexCount();
// Sums all the X points, and sums all the Y points
Point p;
for(int i = 0; i < pointCount; i++)
{
p = vertices[i];
xSum += p.getX();
ySum += p.getY();
zSum += p.getZ();
}
// returns the average of the X points and the average of the Y points.
return new double[] { xSum/pointCount, ySum/pointCount, zSum/pointCount };
}
/**
* Scales the model with the specified scaling factors.
*
* @param Sx The scaling factor in the X direction.
* @param Sy The scaling factor in the Y direction.
* @param Sz The scaling factor in the Z direction.
*/
public void scale(double Sx, double Sy, double Sz)
{
double[] centroid = getCentroid();
double Cx = centroid[0];
double Cy = centroid[1];
double Cz = centroid[2];
// Translate so centroid is at the origin
translate(-Cx, -Cy, -Cz);
// Scale all points
for(int i = 0; i < Xs.size(); i++)
{
double x = Double.parseDouble(Xs.get(i).toString());
double y = Double.parseDouble(Ys.get(i).toString());
double z = Double.parseDouble(Zs.get(i).toString());
// Scale Transformation
x *= Sx;
y *= Sy;
z *= Sz;
Xs.set(i, x);
Ys.set(i, y);
Zs.set(i, z);
}
// Inverse translation so centroid is in original position
translate(Cx, Cy, Cz);
}
/**
* Moves the vertices of the model to make the object rotate around its centroid.
*
* @param theta The angle of rotation, counter-clockwise, around the X-axis.
* @param phi The angle of rotation, counter-clockwise, around the Y-axis.
* @param omega The angle of rotation, counter-clockwise, around the Z-axis.
*/
public void rotate(double theta, double phi, double omega)
{
double[] centroid = getCentroid();
double Cx = centroid[0];
double Cy = centroid[1];
double Cz = centroid[2];
rotateAroundPivotPoint(theta, phi, omega, Cx, Cy, Cz);
}
/**
* Rotates the model around a specified point in space.
*
* @param theta The angle of rotation, counter-clockwise, around the X-axis.
* @param phi The angle of rotation, counter-clockwise, around the Y-axis.
* @param omega The angle of rotation, counter-clockwise, around the Z-axis.
* @param Px The pivot point's world-space X-coordinate.
* @param Py The pivot point's world-space Y-coordinate.
* @param Pz The pivot point's world-space Z-coordinate.
*/
public void rotateAroundPivotPoint(double theta, double phi, double omega, double Px, double Py, double Pz)
{
theta = Math.toRadians(theta);
phi = Math.toRadians(phi);
omega = Math.toRadians(omega);
// Translate so centroid is at pivot point
translate( -Px, -Py, -Pz);
for(int i = 0; i < getVertexCount(); i++)
{
Point p = vertices[i];
double x = p.getX();
double y = p.getY();
double z = p.getZ();
double newX = 0;
double newY = 0;
double newZ = 0;
boolean newPoints = false;
if(theta != 0)
{
newY = y*Math.cos(theta) - z*Math.sin(theta);
newZ = y*Math.sin(theta) + z*Math.cos(theta);
newX = x;
y = newY;
z = newZ;
newPoints = true;
}
if(phi != 0)
{
// Rotate Transformation
newZ = z*Math.cos(phi) - x*Math.sin(phi);
newX = z*Math.sin(phi) + x*Math.cos(phi);
newY = y;
z = newZ;
x = newX;
newPoints = true;
}
if(omega != 0)
{
newX = x*Math.cos(omega) - y*Math.sin(omega);
newY = x*Math.sin(omega) + y*Math.cos(omega);
newZ = z;
newPoints = true;
}
if(newPoints)
{
p.change(newX, newY, newZ);
}
}
// Inverse translation so centroid is in original position
translate(Px, Py, Pz);
}
/**
* Moves all the vertices uniformly a specified amount to move the model.
*
* @param Tx The distance to move all points on the X-axis.
* @param Ty The distance to move all points on the Y-axis.
* @param Tz The distance to move all points on the Z-axis.
*/
public void translate(double Tx, double Ty, double Tz)
{
Point p;
for(int i = 0; i < getVertexCount(); i++)
{
p = vertices[i];
double x = p.getX();
double y = p.getY();
double z = p.getZ();
// Translate Transformation
x += Tx;
y += Ty;
z += Tz;
p.change(x, y, z);
}
}
/**
* Moves the model so its centroid is exactly at the origin in world-space.
*
* @return An array containing the X, Y, and Z values of the distance the model was moved. (to inverse translate)
*/
public double[] translateToOrigin()
{
double[] centroid = getCentroid();
double Cx = centroid[0];
double Cy = centroid[1];
double Cz = centroid[2];
// Translate so centroid is at the origin
translate( -Cx, -Cy, -Cz);
return new double[] {Cx, Cy, Cz};
}
/**
* Moves all the vertices of the model so the model's centroid is exactly at the specified XYZ location.
*
* @param x The X position of the centroid of the model.
* @param y The Y position of the centroid of the model.
* @param z The Z position of the centroid of the model.
*/
public void translateToPosition(double x, double y, double z)
{
translateToOrigin();
translate(x, y, z);
}
public int getTriangleCount()
{
return triangles.length;
}
public Triangle getTriangle(int i)
{
return triangles[i];
}
public int getVertexCount()
{
return vertices.length;
}
public Point getVertex(int i)
{
return vertices[i];
}
public void setVertex(int i, Point p)
{
vertices[i] = new Point(p.getX(), p.getY(), p.getZ());
}
public Model copy()
{
return new Model(shape, new int[]{scale});
}
}