-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathpolygon.js
520 lines (479 loc) · 12 KB
/
polygon.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
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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
/**
* Class: Polygon
* Description: Represents a 3D polygon.
*/
JSM.Polygon = function ()
{
this.vertices = null;
this.cache = null;
this.Clear ();
};
/**
* Function: Polygon.AddVertex
* Description: Adds a vertex to the polygon.
* Parameters:
* x {number} the x coordinate of the vertex
* y {number} the y coordinate of the vertex
* z {number} the z coordinate of the vertex
*/
JSM.Polygon.prototype.AddVertex = function (x, y, z)
{
this.AddVertexCoord (new JSM.Coord (x, y, z));
};
/**
* Function: Polygon.AddVertexCoord
* Description: Adds a vertex coordinate to the polygon.
* Parameters:
* coord {Coord} the coordinate
*/
JSM.Polygon.prototype.AddVertexCoord = function (coord)
{
this.vertices.push (coord);
this.ClearCache ();
};
/**
* Function: Polygon.GetVertex
* Description: Returns the vertex with the given index.
* Parameters:
* index {integer} the index of the vertex
* Returns:
* {Coord} the vertex
*/
JSM.Polygon.prototype.GetVertex = function (index)
{
return this.vertices[index];
};
/**
* Function: Polygon.VertexCount
* Description: Returns the vertex count of the polygon.
* Returns:
* {integer} vertex count
*/
JSM.Polygon.prototype.VertexCount = function ()
{
return this.vertices.length;
};
/**
* Function: Polygon.GetNextVertex
* Description: Returns the vertex index after the given one.
* Parameters:
* index {integer} the vertex index
* Returns:
* {integer} the result
*/
JSM.Polygon.prototype.GetNextVertex = function (index)
{
return JSM.NextIndex (index, this.vertices.length);
};
/**
* Function: Polygon.ReverseVertices
* Description: Reverses the orientation of the vertices.
*/
JSM.Polygon.prototype.ReverseVertices = function ()
{
this.vertices.reverse ();
this.ClearCache ();
};
/**
* Function: Polygon.GetPrevVertex
* Description: Returns the vertex index before the given one.
* Parameters:
* index {integer} the vertex index
* Returns:
* {integer} the result
*/
JSM.Polygon.prototype.GetPrevVertex = function (index)
{
return JSM.PrevIndex (index, this.vertices.length);
};
/**
* Function: Polygon.GetVertexAngle
* Description: Returns the angle of the given vertex.
* Parameters:
* index {integer} the vertex index
* Returns:
* {number} the result
*/
JSM.Polygon.prototype.GetVertexAngle = function (index)
{
var prev = this.vertices[this.GetPrevVertex (index)];
var curr = this.vertices[index];
var next = this.vertices[this.GetNextVertex (index)];
var prevDir = JSM.CoordSub (prev, curr);
var nextDir = JSM.CoordSub (next, curr);
return prevDir.AngleTo (nextDir);
};
/**
* Function: Polygon.GetNormal
* Description: Calculates the normal vector of the polygon.
* Returns:
* {Vector} the result
*/
JSM.Polygon.prototype.GetNormal = function ()
{
if (this.cache.normal !== null) {
return this.cache.normal;
}
var result = JSM.CalculateNormal (this.vertices);
this.cache.normal = result;
return result;
};
/**
* Function: Polygon.ToPolygon2D
* Description: Converts the polygon to a 2D polygon.
* Returns:
* {Polygon2D} the result
*/
JSM.Polygon.prototype.ToPolygon2D = function ()
{
var normal = this.GetNormal ();
var result = new JSM.Polygon2D ();
var i, vertex;
for (i = 0; i < this.vertices.length; i++) {
vertex = this.vertices[i].ToCoord2D (normal);
result.AddVertex (vertex.x, vertex.y);
}
return result;
};
/**
* Function: Polygon.ToArray
* Description: Creates an array of vertices from polygon.
* Returns:
* {Coord[*]} the result
*/
JSM.Polygon.prototype.ToArray = function ()
{
var vertices = [];
var i, vertex;
for (i = 0; i < this.vertices.length; i++) {
vertex = this.vertices[i];
vertices.push (vertex.Clone ());
}
return vertices;
};
/**
* Function: Polygon.FromArray
* Description: Creates the polygon from an array of vertices.
* Parameters:
* vertices {Coord[*]} the array of vertices
*/
JSM.Polygon.prototype.FromArray = function (vertices)
{
this.Clear ();
var i, vertex;
for (i = 0; i < vertices.length; i++) {
vertex = vertices[i];
this.AddVertex (vertex.x, vertex.y, vertex.z);
}
};
/**
* Function: Polygon.Clear
* Description: Makes the polygon empty.
*/
JSM.Polygon.prototype.Clear = function ()
{
this.vertices = [];
this.ClearCache ();
};
/**
* Function: Polygon.ClearCache
* Description: Clears stored values from the polygon.
*/
JSM.Polygon.prototype.ClearCache = function ()
{
this.cache = {
normal : null
};
};
/**
* Function: Polygon.Clone
* Description: Clones the polygon.
* Returns:
* {Polygon} a cloned instance
*/
JSM.Polygon.prototype.Clone = function ()
{
var result = new JSM.Polygon ();
var i, vertex;
for (i = 0; i < this.vertices.length; i++) {
vertex = this.vertices[i];
result.AddVertexCoord (vertex.Clone ());
}
return result;
};
/**
* Class: ContourPolygon
* Description: Represents a 3D polygon with more contours.
*/
JSM.ContourPolygon = function ()
{
this.contours = null;
this.Clear ();
};
/**
* Function: ContourPolygon.AddVertex
* Description: Adds a vertex to the last contour of the polygon.
* Parameters:
* x {number} the x coordinate of the vertex
* y {number} the y coordinate of the vertex
* z {number} the z coordinate of the vertex
*/
JSM.ContourPolygon.prototype.AddVertex = function (x, y, z)
{
this.lastContour.AddVertex (x, y, z);
};
/**
* Function: ContourPolygon.AddVertexCoord
* Description: Adds a vertex coordinate to the last contour of the polygon.
* Parameters:
* coord {Coord} the coordinate
*/
JSM.ContourPolygon.prototype.AddVertexCoord = function (coord)
{
this.lastContour.AddVertexCoord (coord);
};
/**
* Function: ContourPolygon.AddContourVertex
* Description: Adds a vertex to the given contour of the polygon.
* Parameters:
* contourIndex {integer} the index of the contour
* x {number} the x coordinate of the vertex
* y {number} the y coordinate of the vertex
* z {number} the z coordinate of the vertex
*/
JSM.ContourPolygon.prototype.AddContourVertex = function (contourIndex, x, y, z)
{
return this.contours[contourIndex].AddVertex (x, y, z);
};
/**
* Function: ContourPolygon.AddContourVertexCoord
* Description: Adds a vertex coordinate to the given contour of the polygon.
* Parameters:
* contourIndex {integer} the index of the contour
* coord {Coord} the coordinate
*/
JSM.ContourPolygon.prototype.AddContourVertexCoord = function (contourIndex, coord)
{
return this.contours[contourIndex].AddVertexCoord (coord);
};
/**
* Function: ContourPolygon.VertexCount
* Description: Returns the vertex count of the polygon.
* Returns:
* {integer} vertex count
*/
JSM.ContourPolygon.prototype.VertexCount = function ()
{
var vertexCount = 0;
var i;
for (i = 0; i < this.contours.length; i++) {
vertexCount += this.contours[i].VertexCount ();
}
return vertexCount;
};
/**
* Function: ContourPolygon.ContourVertexCount
* Description: Returns the vertex count of the given contour of the polygon.
* Parameters:
* contourIndex {integer} the index of the contour
* Returns:
* {integer} vertex count
*/
JSM.ContourPolygon.prototype.ContourVertexCount = function (contourIndex)
{
return this.contours[contourIndex].VertexCount ();
};
/**
* Function: ContourPolygon.AddContour
* Description:
* Adds a contour to the polygon. If the given contour is null,
* an empty contour is added to the polygon.
* Parameters:
* contour {Polygon} the new contour
*/
JSM.ContourPolygon.prototype.AddContour = function (contour)
{
if (contour === undefined || contour === null) {
this.lastContour = new JSM.Polygon ();
} else {
this.lastContour = contour;
}
this.contours.push (this.lastContour);
};
/**
* Function: ContourPolygon.GetLastContour
* Description: Returns the last contour of the polygon.
* Returns:
* {Polygon} the result
*/
JSM.ContourPolygon.prototype.GetLastContour = function ()
{
return this.lastContour;
};
/**
* Function: ContourPolygon.GetContourVertex
* Description: Returns the vertex of the given contour with the given index.
* Parameters:
* contourIndex {integer} the index of the contour
* vertexIndex {integer} the index of the vertex
* Returns:
* {Coord} the vertex
*/
JSM.ContourPolygon.prototype.GetContourVertex = function (contourIndex, vertexIndex)
{
return this.contours[contourIndex].GetVertex (vertexIndex);
};
/**
* Function: ContourPolygon.GetContour
* Description: Returns the contour with the given index.
* Parameters:
* contourIndex {integer} the index of the contour
* Returns:
* {Polygon} the contour
*/
JSM.ContourPolygon.prototype.GetContour = function (contourIndex)
{
return this.contours[contourIndex];
};
/**
* Function: ContourPolygon.ContourCount
* Description: Returns the contour count of the polygon.
* Returns:
* {integer} contour count
*/
JSM.ContourPolygon.prototype.ContourCount = function ()
{
return this.contours.length;
};
/**
* Function: ContourPolygon.ToContourPolygon2D
* Description: Converts the polygon to a 2D polygon.
* Returns:
* {ContourPolygon2D} the result
*/
JSM.ContourPolygon.prototype.ToContourPolygon2D = function ()
{
var normal = this.contours[0].GetNormal ();
var result = new JSM.ContourPolygon2D ();
var i, j, contour, vertex;
for (i = 0; i < this.contours.length; i++) {
result.AddContour ();
contour = this.contours[i];
for (j = 0; j < contour.VertexCount (); j++) {
vertex = contour.GetVertex (j);
result.AddVertexCoord (vertex.ToCoord2D (normal));
}
}
return result;
};
/**
* Function: ContourPolygon.ToArray
* Description:
* Creates an array of vertices from polygon. The result contains
* null values between contours.
* Returns:
* {Coord[*]} the result
*/
JSM.ContourPolygon.prototype.ToArray = function ()
{
var vertices = [];
var i, j, contour, vertex;
for (i = 0; i < this.contours.length; i++) {
contour = this.contours[i];
for (j = 0; j < contour.VertexCount (); j++) {
vertex = contour.GetVertex (j);
vertices.push (vertex.Clone ());
}
if (i < this.contours.length - 1) {
vertices.push (null);
}
}
return vertices;
};
/**
* Function: ContourPolygon.FromArray
* Description:
* Creates the polygon from an array of vertices. The input should contain
* null values between contours.
* Parameters:
* vertices {Coord[*]} the array of vertices
*/
JSM.ContourPolygon.prototype.FromArray = function (vertices)
{
this.Clear ();
this.AddContour ();
var i, vertex;
for (i = 0; i < vertices.length; i++) {
vertex = vertices[i];
if (vertex === null) {
this.AddContour ();
} else {
this.AddVertex (vertex.x, vertex.y, vertex.z);
}
}
};
/**
* Function: ContourPolygon.Clear
* Description: Makes the polygon empty.
*/
JSM.ContourPolygon.prototype.Clear = function ()
{
this.contours = [];
this.lastContour = null;
};
/**
* Function: ContourPolygon.Clone
* Description: Clones the polygon.
* Returns:
* {ContourPolygon} a cloned instance
*/
JSM.ContourPolygon.prototype.Clone = function ()
{
var result = new JSM.ContourPolygon ();
var i, contour;
for (i = 0; i < this.contours.length; i++) {
contour = this.contours[i];
result.AddContour (contour.Clone ());
}
return result;
};
/**
* Function: OffsetPolygonContour
* Description: Offsets all vertices of a polygon.
* Parameters:
* polygon {Polygon} the polygon
* width {number} the width of the offset
* Returns:
* {Polygon} the result
*/
JSM.OffsetPolygonContour = function (polygon, width)
{
var count = polygon.VertexCount ();
var normal = polygon.GetNormal ();
var prev, curr, next;
var prevVertex, currVertex, nextVertex;
var prevDir, nextDir;
var distance, offsetedCoord;
var result = new JSM.Polygon ();
var i, angle;
for (i = 0; i < count; i++) {
prev = polygon.GetPrevVertex (i);
curr = i;
next = polygon.GetNextVertex (i);
prevVertex = polygon.GetVertex (prev);
currVertex = polygon.GetVertex (curr);
nextVertex = polygon.GetVertex (next);
prevDir = JSM.CoordSub (prevVertex, currVertex);
nextDir = JSM.CoordSub (nextVertex, currVertex);
angle = prevDir.AngleTo (nextDir) / 2.0;
if (JSM.CoordOrientation (prevVertex, currVertex, nextVertex, normal) == JSM.Orientation.Clockwise) {
angle = Math.PI - angle;
}
distance = width / Math.sin (angle);
offsetedCoord = currVertex.Clone ();
offsetedCoord.Offset (nextDir, distance);
offsetedCoord.Rotate (normal, angle, currVertex);
result.AddVertex (offsetedCoord.x, offsetedCoord.y, offsetedCoord.z);
}
return result;
};