-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath3d.js
More file actions
893 lines (761 loc) · 21.8 KB
/
math3d.js
File metadata and controls
893 lines (761 loc) · 21.8 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
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
import Triangle from "./triangle.js"
import Colors from './colors.js'
import Configs from './configs.js'
export default null
export const DEG2RAD = 3.14159265 / 180
/*
n is a float number
return should be bitween 0 and 1
*/
export function float_saturate(n)
{
return n < 0 ? 0 : (n > 1 ? 1 : n);
}
export function bbox(pol, width, height) // pol is Traingle here
{
let Xmin = width // set to the max value
let Ymin = height // set to the max value
let Xmax = 0
let Ymax = 0
let v = [pol.v1, pol.v2, pol.v3]
// convert from 0 to 1 to screen scall (in pixels)
/* v.map(v => {
// v means vertex
v.x = Math.round(v.x * width)
v.y = Math.round(-v.y * height)
})*/
for (let i = 0; i < 3; ++i)
{
if (v[i].x < Xmin)
Xmin = v[i].x
if (v[i].y < Ymin)
Ymin = v[i].y
if (v[i].x > Xmax)
Xmax = v[i].x
if (v[i].y > Ymax)
Ymax = v[i].y
}
return [
[Xmin, Ymin],
[Xmax, Ymax]
]
}
export function pixelContainedIn2DTraingle(v1, v2, v3, x, y, z)
{
if (Math.random() > 0.5)
return true
else
return false
}
/*
v1 & v2 are tow vertices
x & y is P is a point coordinat
*/
export function edgeFunction(a, b, x, y)
{
return (x - a.x) * (b.y - a.y) - (y - a.y) * (b.x - a.x) >= 0
}
/*
v1 & v2 are tow vertices
p is a point
*/
export function Area(a, b, p)
{
return (p.x - a.x) * (b.y - a.y) - (p.y - a.y) * (b.x - a.x)
}
export function Area2(a, b, x, y)
{
return (x - a.x) * (b.y - a.y) - (y - a.y) * (b.x - a.x)
}
export function isPointInsideTriangle(v0, v1, v2, x, y)
{
return edgeFunction(v0, v1, x, y) && edgeFunction(v1, v2, x, y) && edgeFunction(v2, v0, x, y)
}
function flip_vect(v1, v2)
{
let tmp = v1
v1 = v2
v2 = tmp
}
function flip_triangle(tr)
{
let tmp = tr.v3
tr.v3 = tr.v2
tr.v2 = tmp
}
// drow just line from v1 to v2
export function drawLine(v1, v2, v3, width, height, FrameBuffer, Z_Buffer)
{
let [R, G, B, A] = Configs.render.strokeStyle
let col_distance = Math.abs(v2.x - v1.x)
let row_distance = Math.abs(v2.y - v1.y)
let area = 0
let a, b, c = 0
let z = 0
let flipped = false
if (col_distance == 0 && row_distance == 0)
{
// draw a line.
area = Area(v1, v2, v3) // the total area of the triangle (gon)
a = Area2(v2, v3, v1.x, v1.y) // aria of triangle (v2, v3, p)
b = Area2(v3, v1, v1.x, v1.y) // aria of triangle (v3, v1, p)
c = Area2(v1, v2, v1.x, v1.y) // aria of triangle (v1, v2, p)
a /= area
b /= area
c /= area
z = a * v1.z + b * v2.z + c * v3.z
drawPoint(v1.x, v1.y, z, width, height, FrameBuffer, Z_Buffer)
}
else if (row_distance > col_distance)
{
if (v1.y > v2.y)
{
// flep a points
flip_vect(v1, v2)
flipped = true
}
let col = null
let t = null
for (let row = v1.y; row <= v2.y; ++row)
{
t = (row - v1.y) / row_distance
if (v1.x > v2.x)
col = Math.round((-col_distance * t + v1.x))
else
col = Math.round((col_distance * t + v1.x))
area = Area(v1, v2, v3) // the total area of the triangle (gon)
if (flipped)
{
a = Area2(v1, v3, col, row) // aria of triangle (v2, v3, p)
b = Area2(v3, v2, col, row) // aria of triangle (v3, v1, p)
c = Area2(v2, v1, col, row) // aria of triangle (v1, v2, p)
}
else
{
a = Area2(v2, v3, col, row) // aria of triangle (v2, v3, p)
b = Area2(v3, v1, col, row) // aria of triangle (v3, v1, p)
c = Area2(v1, v2, col, row) // aria of triangle (v1, v2, p)
}
a /= area
b /= area
c /= area
if (flipped)
z = a * v2.z + b * v1.z + c * v3.z
else
z = a * v1.z + b * v2.z + c * v3.z
drawPoint(col, row, z, width, height, FrameBuffer, Z_Buffer)
}
}
else
{
if (v1.x > v2.x)
{
// flep a points
flip_vect(v1, v2)
flipped = true
}
let row = 0
let t = 0
for (let col = v1.x; col <= v2.x; ++col)
{
t = (col - v1.x) / col_distance
if (v1.y > v2.y)
row = Math.round((-row_distance * t + v1.y))
else
row = Math.round((row_distance * t + v1.y))
area = Area(v1, v2, v3) // the total area of the triangle (gon)
if (flipped)
{
a = Area2(v1, v3, col, row) // aria of triangle (v2, v3, p)
b = Area2(v3, v2, col, row) // aria of triangle (v3, v1, p)
c = Area2(v2, v1, col, row) // aria of triangle (v1, v2, p)
}
else
{
a = Area2(v2, v3, col, row) // aria of triangle (v2, v3, p)
b = Area2(v3, v1, col, row) // aria of triangle (v3, v1, p)
c = Area2(v1, v2, col, row) // aria of triangle (v1, v2, p)
}
a /= area
b /= area
c /= area
if (flipped)
z = a * v2.z + b * v1.z + c * v3.z
else
z = a * v1.z + b * v2.z + c * v3.z
drawPoint(col, row, z, width, height, FrameBuffer, Z_Buffer)
}
}
}
export function drawPoint(x, y, z, width, height, FrameBuffer, Z_Buffer)
{
// don't write out of (FrameBuffer/Z_Buffer) range
if (x > width || x < 0 || y > height || y < 0)
return;
let index = y * width + x
if (z <= Z_Buffer[index])
{
Z_Buffer[index] = z // distance from camera to the traingle
let [R, G, B, A] = Configs.render.strokeStyle
FrameBuffer[index * 4 + 0] = R
FrameBuffer[index * 4 + 1] = G
FrameBuffer[index * 4 + 2] = B
FrameBuffer[index * 4 + 3] = A
}
}
export function perpendicularOnTr(tr)
{
let AB = tr.v1.multi(-1).add(tr.v2)
let AC = tr.v1.multi(-1).add(tr.v3)
return normalize(AB.cross(AC))
//return normalize(tr.v1.cross(tr.v2))
}
export class Vector4 {
constructor (x = 0 , y = 0 , z = 0 , w = 1) {
this.x = x;
this.y = y;
this.z = z;
this.w = w;
}
dot (v) {
return this.x * v.x + this.y * v.y + this.z * v.z
}
cross (v) {
return new Vector4(
this.y * v.z - this.z * v.y,
this.z * v.x - this.x * v.z,
this.x * v.y - this.y * v.x,
this.w
)
}
add (v) {
return new Vector4(this.x + v.x, this.y + v.y, this.z + v.z, this.w)
}
sub (v) {
return new Vector4(this.x - v.x, this.y - v.y, this.z - v.z, this.w)
}
multi(num) {
return new Vector4(
this.x * num,
this.y * num,
this.z * num,
this.w
);
}
multiMatrix(m)
{
let v = new Vector4(
this.x * m.m[0][0] + this.y * m.m[1][0] + this.z * m.m[2][0] + this.w * m.m[3][0],
this.x * m.m[0][1] + this.y * m.m[1][1] + this.z * m.m[2][1] + this.w * m.m[3][1],
this.x * m.m[0][2] + this.y * m.m[1][2] + this.z * m.m[2][2] + this.w * m.m[3][2],
this.x * m.m[0][3] + this.y * m.m[1][3] + this.z * m.m[2][3] + this.w * m.m[3][3]
);
return new Vector4(v.x * v.w, v.y * v.w, v.z * v.w, 1.0 / v.w)
}
}
export class Vector3 {
constructor (x = 0 , y = 0 , z = 0) {
//console.log(x, y, z);
this.x = x;
this.y = y;
this.z = z;
}
dot (v) {
return this.x * v.x + this.y * v.y + this.z * v.z;
}
cross (v) {
return new Vector3(
this.y * v.z - this.z * v.y,
this.z * v.x - this.x * v.z,
this.x * v.y - this.y * v.x
)
}
add (v) {
return new Vector3(this.x + v.x, this.y + v.y, this.z + v.z)
}
sub (v) {
return new Vector3(this.x - v.x, this.y - v.y, this.z - v.z)
}
multi (num) {
return new Vector3(
this.x * num,
this.y * num,
this.z * num
);
}
}
export class Vector2 {
constructor (x = 0 , y = 0) {
this.x = x
this.y = y
}
dot (v) {
return this.x * v.x + this.y * v.y
}
add (v) {
return new Vector2(this.x + v.x, this.y + v.y)
}
sub (v) {
return new Vector2(this.x - v.x, this.y - v.y)
}
multi(num) {
return new Vector2(
this.x * num,
this.y * num
);
}
}
export function scale(matrix, scaleNumber) {
//return new Vector4(scaleVector.x * v.x, scaleVector.y * v.y, scaleVector.z * v.z, 1)
return new Matrix4([
[scaleNumber, 0, 0, 0],
[0, scaleNumber, 0, 0],
[0, 0, scaleNumber, 0],
[0, 0, 0, 1]
]).multi(matrix);
}
export function translate(matrix, transVector) {
//return new Vector4(transVector + v.x, transVector.y + v.y, transVector.z + v.z, 1)
return new Matrix4([
[1, 0, 0, transVector.x],
[0, 1, 0, transVector.y],
[0, 0, 1, transVector.z],
[0, 0, 0, 1]
]).multi(matrix);
}
export function rotate(matrix, angle, axe)
{
// to radians
//angle = angle * Math.PI / 180;
// rotation on X, Y & Z
return new Matrix4([
[Math.cos(angle) + axe.x * axe.x * (1 - Math.cos(angle)), axe.x * axe.y * (1 - Math.cos(angle)) - axe.z * Math.sin(angle), axe.x * axe.z * (1 - Math.cos(angle)) + axe.y * Math.sin(angle), 0],
[axe.x * axe.y * (1 - Math.cos(angle)) + axe.z * Math.sin(angle), Math.cos(angle) + axe.y * axe.y * (1 - Math.cos(angle)), axe.y * axe.z * (1 - Math.cos(angle)) - axe.x * Math.sin(angle), 0],
[axe.z * axe.x * (1 - Math.cos(angle)) - axe.y * Math.sin(angle), axe.y * axe.z * (1 - Math.cos(angle)) + axe.x * Math.sin(angle), Math.cos(angle) + axe.z * axe.z * (1 - Math.cos(angle)), 0],
[0, 0, 0, 1],
]).multi(matrix);
}
export function rotateX(v, angle) {
let tmp = Math.cos(angle) * v.y - Math.sin(angle) * v.z;
v.z = Math.sin(angle) * v.y + Math.cos(angle) * v.z;
v.y = tmp;
/* return new Vector3(
v.x,
Math.cos(angle) * v.y - Math.sin(angle) * v.z,
Math.sin(angle) * v.y + Math.cos(angle) * v.z
); */
}
export function rotateY(v, angle) {
//console.log(v);
let tmp = Math.cos(angle) * v.x + Math.sin(angle) * v.z;
v.z = -Math.sin(angle) * v.x + Math.cos(angle) * v.z;
v.x = tmp;
/*return new Vector3(
Math.cos(angle) * v.x + Math.sin(angle) * v.z,
v.y,
-Math.sin(angle) * v.x + Math.cos(angle) * v.z
);
*/
}
export function rotateZ(v, angle) {
let tmp = Math.cos(angle) * v.x - Math.sin(angle) * v.y;
v.y = Math.sin(angle) * v.x + Math.cos(angle) * v.y;
v.x = tmp;
/* return new Vector3(
Math.cos(angle) * v.x - Math.sin(angle) * v.y,
Math.sin(angle) * v.x + Math.cos(angle) * v.y
); */
}
/*
FOV, width, height, far, near, vector
hw float: means width / height
*/
// fovX, hw=Height/Width
export function makeFrustum(fov, hw, near, far)
{
const DEG2RAD = 3.14159265 / 180
let tangent = Math.tan(fov * 0.5 * DEG2RAD)
let half_width = near * tangent
let half_height = half_width * hw
return myFrastum(-half_width, half_width, -half_height, half_height, near, far)
}
// perspective frustum
// params: left, right, bottom, top, near, far
export function myFrastum(l, r, b, t, n, f)
{
return new Matrix4([
[2*n/(r-l), 0, (r+l)/(r-l), 0],
[0, 2*n/(t-b), (t+b)/(t-b), 0],
[0, 0, (f+n)/(n-f), 2*f*n/(n-f)],
[0, 0, -1, 0]
])
}
// orthographic frustum
// params: left, right, bottom, top, near, far
export function orthoFrustum(l, r, b, t, n, f)
{
return new Matrix4([
[2/(r-l), 0, 0, (r+l)/(l-r)],
[0, 2/(t-b), 0, (t+b)/(b-t)],
[0, 0, 2/(n-f), (f+n)/(n-f)],
[0, 0, 0, 1]
])
}
// LookAt Matrix is ModelView matrix
export function createLookAt(eye, target, up) {
let F = normalize(eye.sub(target));
let R = normalize(up.cross(F));
let U = F.cross(R);
let T = new Vector3();
T.x = -R.dot(eye);
T.y = -U.dot(eye);
T.z = -F.dot(eye);
return new Matrix4([
[R.x, R.y, R.z, T.x],
[U.x, U.y, U.z, T.y],
[F.x, F.y, F.z, T.z],
[0, 0, 0, 1]
]);
}
// fov in radians
// export function perspective(fov, wh, near, far, v) {
// /* return new Vector3(
// v.x / (Math.tan(radians(fov) / 2) * wh),
// v.y / Math.tan(radians(fov) / 2),
// far / (far - near) * v.z + 1
// ); */
// return new Matrix4([
// [1 / (Math.tan(fov * 0.5) * wh), 0, 0, 0],
// [0, 1 / Math.tan(fov * 0.5), 0, 0],
// [0, 0, far / (far - near), 1],
// [0, 0, near * far / (near - far), 0]
// ]);
// }
export function normalize (v) {
let normal = Math.sqrt(v.x * v.x + v.y * v.y + v.z * v.z);
return new Vector4(v.x / normal, v.y / normal, v.z / normal, 0);
}
export function mat4(num)
{
return new Matrix4([
[num, 0, 0, 0],
[0, num, 0, 0],
[0, 0, num, 0],
[0, 0, 0, num]
]);
}
export class Matrix4 {
constructor (m) {
this.m = m
}
add (m) {
return new Matrix4([
[this.m[0][0] + m.m[0][0], this.m[0][1] + m.m[0][1], this.m[0][2] + m.m[0][2], this.m[0][3] + m.m[0][3]],
[this.m[1][0] + m.m[1][0], this.m[1][1] + m.m[1][1], this.m[1][2] + m.m[1][2], this.m[1][3] + m.m[1][3]],
[this.m[2][0] + m.m[2][0], this.m[2][1] + m.m[2][1], this.m[2][2] + m.m[2][2], this.m[2][3] + m.m[2][3]],
[this.m[3][0] + m.m[3][0], this.m[3][1] + m.m[3][1], this.m[3][2] + m.m[3][2], this.m[3][3] + m.m[3][3]]
])
}
sub (m) {
return new Matrix4([
[this.m[0][0] - m.m[0][0], this.m[0][1] - m.m[0][1], this.m[0][2] - m.m[0][2], this.m[0][3] - m.m[0][3]],
[this.m[1][0] - m.m[1][0], this.m[1][1] - m.m[1][1], this.m[1][2] - m.m[1][2], this.m[1][3] - m.m[1][3]],
[this.m[2][0] - m.m[2][0], this.m[2][1] - m.m[2][1], this.m[2][2] - m.m[2][2], this.m[2][3] - m.m[2][3]],
[this.m[3][0] - m.m[3][0], this.m[3][1] - m.m[3][1], this.m[3][2] - m.m[3][2], this.m[3][3] - m.m[3][3]]
])
}
multiVector(v) {
return new Vector4(
this.m[0][0] * v.x + this.m[0][1] * v.y + this.m[0][2] * v.z + this.m[0][3] * v.w,
this.m[1][0] * v.x + this.m[1][1] * v.y + this.m[1][2] * v.z + this.m[1][3] * v.w,
this.m[2][0] * v.x + this.m[2][1] * v.y + this.m[2][2] * v.z + this.m[2][3] * v.w,
this.m[3][0] * v.x + this.m[3][1] * v.y + this.m[3][2] * v.z + this.m[3][3] * v.w
);
}
multi(m)
{
let arr = [
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]
];
for (let r = 0; r < 4; ++r)
{
for (let c = 0; c < 4; ++c)
{
//arr[r][c] = 0;
for (let i = 0; i < 4; ++i)
{
arr[r][c] += this.m[r][i] * m.m[i][c];
}
}
}
return new Matrix4(arr);
}
reverse()
{
return new Matrix4([
[this.m[0][0], this.m[1][0], this.m[2][0], this.m[3][0]],
[this.m[0][1], this.m[1][1], this.m[2][1], this.m[3][1]],
[this.m[0][2], this.m[1][2], this.m[2][2], this.m[3][2]],
[this.m[0][3], this.m[1][3], this.m[2][3], this.m[3][3]]
]);
}
transpose()
{
return new Matrix4([
[this.m[0][0], this.m[1][0], this.m[2][0], this.m[3][0]],
[this.m[0][1], this.m[1][1], this.m[2][1], this.m[3][1]],
[this.m[0][2], this.m[1][2], this.m[2][2], this.m[3][2]],
[this.m[0][3], this.m[1][3], this.m[2][3], this.m[3][3]]
]);
}
inverse()
{
let _A = this.transpose().m
let temp,
N = _A.length,
E = [];
for (let i = 0; i < N; i++)
E[i] = [];
for (let i = 0; i < N; i++)
for (let j = 0; j < N; j++) {
E[i][j] = 0;
if (i == j)
E[i][j] = 1;
}
for (let k = 0; k < N; k++) {
temp = _A[k][k];
for (let j = 0; j < N; j++) {
_A[k][j] /= temp;
E[k][j] /= temp;
}
for (let i = k + 1; i < N; i++) {
temp = _A[i][k];
for (let j = 0; j < N; j++) {
_A[i][j] -= _A[k][j] * temp;
E[i][j] -= E[k][j] * temp;
}
}
}
for (let k = N - 1; k > 0; k--) {
for (let i = k - 1; i >= 0; i--) {
temp = _A[i][k];
for (let j = 0; j < N; j++) {
_A[i][j] -= _A[k][j] * temp;
E[i][j] -= E[k][j] * temp;
}
}
}
for (let i = 0; i < N; i++)
for (let j = 0; j < N; j++)
_A[i][j] = E[i][j];
return (new Matrix4(_A)).transpose();
}
}
export function radians (angle) {
return angle * Math.PI / 180
}
export function degrees (radians) {
return radians * 180 / Math.PI
}
export function worldSpace (center, listOfVectors4) {
let newListOfVectors4 = [];
listOfVectors4.forEach(v => {
newListOfVectors4.push(center.add(v));
})
return newListOfVectors4;
}
export function viewSpace (camera, list) {
let newList = [];
list.forEach(v => {
newList.push(camera.m.multi(v));
});
return newList;
}
/* export function simpleProjection(list)
{
let newList = [];
list.forEach(v => {
newList.push(new Vector3(
v.x / v.z,
v.y / v.z
));
});
return newList;
} */
export class Quadrant
{
constructor(v1, v2, v3, v4, face = [], color = Colors.WHITE)
{
this.v1 = v1
this.v2 = v2
this.v3 = v3
this.v4 = v4
this.face = face
this.color = color
}
}
export function intersectPlane(planePos, planeNormal, startP, endP)
{
planeNormal = normalize(planeNormal);
let plane_d = -planeNormal.dot(planePos);
let ad = startP.dot(planeNormal);
let bd = endP.dot(planeNormal);
let t = (-plane_d - ad) / (bd - ad);
let lineStartToEnd = endP.sub(startP);
let lineToIntersect = lineStartToEnd.multi(t);
return startP.add(lineToIntersect);
}
export function dist(planePos, planeNormal, point)
{
//let N = normalize(point);
return (planeNormal.x * point.x + planeNormal.y * point.y + planeNormal.z * point.z) - planeNormal.dot(planePos);
//return planeNormal.dot(point.sub(planePos))
}
export function clipAgainstPlane(planePos, planeNormal, pol, out)
{
// check if polygon is a Triangle or Quadrant
/*if (pol.v4 !== undefined)
return clipQuadAgainstPlane(planePos, planeNormal, pol, out);*/
return clipTrAgainstPlane(planePos, planeNormal, pol, out);
}
/*export function clipQuadAgainstPlane(planePos, planeNormal, quad, out)
{
planeNormal = normalize(planeNormal);
let insidePoints = [];
let outsidePoints = [];
let d0 = dist(planePos, planeNormal, quad.v1);
let d1 = dist(planePos, planeNormal, quad.v2);
let d2 = dist(planePos, planeNormal, quad.v3);
let d3 = dist(planePos, planeNormal, quad.v4);
if (d0 >= 0)
insidePoints.push(quad.v1);
else
outsidePoints.push(quad.v1);
if (d1 >= 0)
insidePoints.push(quad.v2);
else
outsidePoints.push(quad.v2);
if (d2 >= 0)
insidePoints.push(quad.v3);
else
outsidePoints.push(quad.v3);
if (d3 >= 0)
insidePoints.push(quad.v4);
else
outsidePoints.push(quad.v4);
if (insidePoints.length == 4)
{
out.push(quad);
return 1;
}
else
{
let list = convertQuadrantToTriangles(quad);
clipTrAgainstPlane(planePos, planeNormal, list[0], out);
clipTrAgainstPlane(planePos, planeNormal, list[1], out);
}
}*/
export function clipTrAgainstPlane(planePos, planeNormal, tr, out)
{
planeNormal = normalize(planeNormal);
let N = perpendicularOnTr(tr) // Normal of a triangle.
// normalsList[pol.face.vn[0]-1]
let insidePoints = [];
let outsidePoints = [];
let d0 = dist(planePos, planeNormal, tr.v1);
let d1 = dist(planePos, planeNormal, tr.v2);
let d2 = dist(planePos, planeNormal, tr.v3);
if (d0 >= 0)
insidePoints.push(tr.v1);
else
outsidePoints.push(tr.v1);
if (d1 >= 0)
insidePoints.push(tr.v2);
else
outsidePoints.push(tr.v2);
if (d2 >= 0)
insidePoints.push(tr.v3);
else
outsidePoints.push(tr.v3);
if (insidePoints.length == 0)
{
// do nothing
return 0;
}
if (insidePoints.length == 3)
{
out.push(tr);
return 1;
}
if (insidePoints.length == 1 && outsidePoints.length == 2)
{
//tr.color = Colors.RED
let tr1 = new Triangle(
insidePoints[0],
intersectPlane(planePos, planeNormal, insidePoints[0], outsidePoints[0]),
intersectPlane(planePos, planeNormal, insidePoints[0], outsidePoints[1]),
tr.face,
tr.color
)
// check if N vector is in the same direction of our triangle normal
if (N.dot(perpendicularOnTr(tr1)) < 0)
{
//console.log('Flipping', N.dot(perpendicularOnTr(tr1)))
// flip triangle if N = -N(tr)
flip_triangle(tr1)
}
out.push(tr1)
return 1;
}
if (insidePoints.length == 2 && outsidePoints.length == 1)
{
//tr.color = Colors.GREEN
let tr1 = new Triangle(insidePoints[0], insidePoints[1], intersectPlane(planePos, planeNormal, insidePoints[1], outsidePoints[0]), tr.face, tr.color);
let tr2 = new Triangle(insidePoints[0], tr1.v3, intersectPlane(planePos, planeNormal, insidePoints[0], outsidePoints[0]), tr.face, tr.color)
// check if N vector is in the same direction of our triangle normal
if (N.dot(perpendicularOnTr(tr1)) < 0) {
// flip triangle if N = -N(tr)
flip_triangle(tr1)
}
// check if N vector is in the same direction of our triangle normal
if (N.dot(perpendicularOnTr(tr2)) < 0) {
// flip triangle if N = -N(tr)
flip_triangle(tr2)
}
out.push(tr1);
out.push(tr2);
return 2;
}
}
export function isInScene(tr, topPlan, buttomPlan, leftPlan, rightPlan)
{
//let planeNormal = perpendicularOnTr(tr);
if ( isOutsidePlan(new Vector3(), topPlan, tr)
|| isOutsidePlan(new Vector3(), buttomPlan, tr)
|| isOutsidePlan(new Vector3(), leftPlan, tr)
|| isOutsidePlan(new Vector3(), rightPlan, tr))
{
return false
}
return true
}
export function isOutsidePlan(planePos, planeNormal, tr)
{
planeNormal = normalize(planeNormal);
let d0 = dist(planePos, planeNormal, tr.v1);
let d1 = dist(planePos, planeNormal, tr.v2);
let d2 = dist(planePos, planeNormal, tr.v3);
if (!(d0 >= 0 || d1 >= 0 || d2 >= 0)) {
return true
}
return false
}
export function convertQuadrantToTriangles(quad)
{
//console.log('convertQuadrantToTriangles')
//quad.color = Colors.PURPLE
return [new Triangle(quad.v1, quad.v2, quad.v3, quad.face, quad.color), new Triangle(quad.v3, quad.v4, quad.v1, quad.face, quad.color)];
}