-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.cpp
2849 lines (2703 loc) · 67.8 KB
/
geometry.cpp
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
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "geometry.h"
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <iostream>
#include <float.h>
#include <random>
#include <unordered_map>
#include <time.h>
#include <memory>
#include <algorithm>
#include <functional>
// =========== GEOMETRY HELPERS =================
// Ensure norm always points in opposite direction of ray
void fixNorm(const VEC3 ray, VEC3& norm)
{
// Multiply by 1e4 to prevent dumb floating point stuff
if ((ray*1e4).dot(norm) >= 0)
{
norm *= -1;
}
}
// Change of basis (from standard cartesian)
MATRIX4 buildCOB(VEC3 z)
{
VEC3 w = z.normalized();
VEC3 u = VEC3(1,0,0).cross(w).normalized();
if (u.isApprox(VEC3(0,0,0)))
{
u = VEC3(0,1,0).cross(w).normalized();
}
VEC3 v = w.cross(u).normalized();
MATRIX4 cob; cob << u.transpose(), 0, v.transpose(), 0, w.transpose(), 0,
0, 0, 0, 1;
return cob;
}
// Coefficients for shortest line between two lines (P4-P3) and (P2-P1)
void shortestLine(VEC3 P1, VEC3 P2, VEC3 P3, VEC3 P4, float& u1, float& u2)
{
u1 = ((P1-P3).dot(P4-P3) * (P4-P3).dot(P2-P1) - (P1-P3).dot(P2-P1)*(P4-P3).dot(P4-P3))/
(pow((P2-P1).norm(), 2) * pow((P4-P3).norm(), 2) - pow((P4-P3).dot(P2-P1), 2));
u2 = ((P1-P3).dot(P4-P3) + u1 * (P4-P3).dot(P2-P1))/pow((P4-P3).norm(), 2);
}
bool segmentIntersect(VEC3 A, VEC3 B, VEC3 ray, VEC3 origin, float& t)
{
// Use shortest line method to compute
float u1, u2;
shortestLine(A, B, ray+origin, origin, u1, u2);
// BA is a segment: check that first coefficient between 0 and 1
if (u1 < 0 || u1 > 1)
{
return false;
}
// Check that second segment is non-negative
if (u2 < 0)
{
return false;
}
// Intersection points should be within epsilon
VEC3 p1 = A + u1 * (B-A);
VEC3 p2 = origin + u2 * ray;
if ((p2-p1).norm() < 1e-4)
{
t = u2;
return true;
}
return false;
}
bool lineIntersect(VEC3 l1, VEC3 o1, VEC3 l2, VEC3 o2, float& t)
{
return false;
}
// =========== SHAPES =================
Sphere::Sphere()
{
center = VEC3(0,0,0);
radius = 1;
color = VEC3(1,1,1);
dims = 3;
reflect_params.material = "";
model = "lambert";
name ="sphere";
}
Sphere::Sphere(VEC3 c, float r, VEC3 col, std::string material, bool in_motion, std::string shader)
{
center = c;
radius = r;
color = col;
reflect_params.material = material;
dims = 3;
motion = in_motion;
model = shader;
name="sphere";
}
bool Sphere::intersect(const VEC3 ray, const VEC3 start, float& t, bool& inside)
{
float A = ray.dot(ray);
float B = 2 * ray.dot(start - center);
float C = (start - center).dot(start - center) - pow(radius, 2);
float discriminant = pow(B, 2) - 4 * A * C;
if (discriminant < 0) // No real roots = no intersection
{
return false;
}
else
{
float t0 = (-B + sqrt(discriminant))/(2*A);
float t1 = (-B - sqrt(discriminant))/(2*A);
if (t0 <= 0.001 && t1 <= 0.001) // If sphere is behind camera then no intersection
{
inside = false;
return false;
}
else if (t0 <= 0.001 || t1 <= 0.001) // If inside sphere then return the intersection in front
{
float t_ret = std::max(t0, t1);
t = t_ret;
inside = true;
return true;
}
else // Standard double intersection
{
float t_ret = std::min(t0, t1);
t = t_ret;
inside = false;
return true;
}
}
}
bool Sphere::intersectMax(const VEC3 ray, const VEC3 start, float& t)
{
float A = ray.dot(ray);
float B = 2 * ray.dot(start - center);
float C = (start - center).dot(start - center) - pow(radius, 2);
float discriminant = pow(B, 2) - 4 * A * C;
if (discriminant < 0) // No real roots = no intersection
{
return false;
}
else
{
float t0 = (-B + sqrt(discriminant))/(2*A);
float t1 = (-B - sqrt(discriminant))/(2*A);
if (t0 <= 0.001 && t1 <= 0.001) // If sphere is behind camera then no intersection
{
return false;
}
else if (t0 <= 0.001 || t1 <= 0.001) // Can't be inside sphere
{
return false;
}
else // Standard double intersection
{
float t_ret = std::max(t0, t1);
t = t_ret;
return true;
}
}
}
bool Sphere::intersectShadow(const VEC3 ray, const VEC3 start, const float t_max)
{
float eps = 1e-3;
float A = ray.dot(ray);
float B = 2 * ray.dot(start - center);
float C = (start - center).dot(start - center) - pow(radius, 2);
float discriminant = pow(B, 2) - 4 * A * C;
if (discriminant < 0) // No real roots = no intersection
{
return false;
}
else
{
float t0 = (-B + sqrt(discriminant))/(2*A);
float t1 = (-B - sqrt(discriminant))/(2*A);
if ((t0 <= eps || t0 >= t_max) && (t1 <= eps || t1 >= t_max)) // Need small constant to avoid intersection with surface
{
return false;
}
else
{
return true;
}
}
}
VEC3 Sphere::getNorm(const VEC3 point)
{
VEC3 norm = point - center;
norm = norm/norm.norm();
return norm;
}
void Sphere::getBounds(VEC3& lbound, VEC3& ubound)
{
lbound = VEC3(center[0] - radius, center[1] - radius, center[2] - radius);
ubound = VEC3(center[0] + radius, center[1] + radius, center[2] + radius);
}
Cylinder::Cylinder()
{
c1 = VEC3(0,-1,0);
c2 = VEC3(0,1,0);
axis = (c2 - c1).normalized();
radius = 1;
color = VEC3(1,1,1);
reflect_params.material = "";
dims = 3;
motion = false;
model = "lambert";
center = VEC3(0,0,0);
name = "cylinder";
}
Cylinder::Cylinder(VEC3 v1, VEC3 v2, float r, VEC3 col, std::string material, bool in_motion, std::string shader)
{
c1 = v1;
c2 = v2;
axis = (v2 - v1).normalized();
radius = r;
color = col;
reflect_params.material = material;
dims = 3;
motion = in_motion;
model = shader;
center = (v1 + v2)/2;
name = "cylinder";
}
bool Cylinder::intersect(const VEC3 ray, const VEC3 start, float& t, bool& inside)
{
float eps = 1e-3;
// Check body intersection
VEC3 ray_a_proj = ray - ray.dot(axis) * axis;
VEC3 constant = start - c1 - ((start-c1).dot(axis)) * axis;
float A = ray_a_proj.dot(ray_a_proj);
float B = 2 * ray_a_proj.dot(constant);
float C = constant.dot(constant) - pow(radius,2);
float discriminant = pow(B, 2) - 4 * A * C;
float t1_body = FLT_MIN, t2_body = FLT_MIN;
if (discriminant >= 0)
{
t1_body = (-B + sqrt(discriminant))/(2*A);
t2_body = (-B - sqrt(discriminant))/(2*A);
if (t1_body <= eps && t2_body <= eps) // If behind camera then no intersection
{
inside = false;
return false;
}
else if (t1_body <= eps || t2_body <= eps) // If inside then return the intersection in front (max)
{
// Check within caps
VEC3 p = start + t1_body * ray;
if (axis.dot(p - c1) > 0 && axis.dot(p - c2) < 0)
{
t = t1_body;
inside = true;
return true;
}
else
{
return false;
}
}
else // Standard double intersection (min)
{
// Check within caps
VEC3 p = start + t2_body * ray;
if (axis.dot(p - c1) > 0 && axis.dot(p - c2) < 0)
{
t = t2_body;
inside = false;
return true;
}
else
{
return false;
}
}
}
return false;
}
bool Cylinder::intersectCap(const VEC3 ray, const VEC3 start, float& t, bool& inside)
{
float eps = 1e-3;
inside = false;
// Basic circle intersection test
float rdota = ray.dot(axis);
if (rdota == 0)
{
return false;
}
float t1 = (c1.dot(axis) - start.dot(axis))/rdota;
float t2 = (c2.dot(axis) - start.dot(axis))/rdota;
if (t1 < eps && t2 < eps)
{
return false;
}
else if (t1 < eps || t2 < eps)
{
inside = true;
t = (float) std::max(t1,t2);
return true;
}
else
{
t = (float) std::min(t1, t2);
return true;
}
}
bool Cylinder::intersectMax(const VEC3 ray, const VEC3 start, float& t)
{
float eps = 1e-3;
// Check body intersection
VEC3 ray_a_proj = ray - ray.dot(axis) * axis;
VEC3 constant = start - c1 - ((start-c1).dot(axis)) * axis;
float A = ray_a_proj.dot(ray_a_proj);
float B = 2 * ray_a_proj.dot(constant);
float C = constant.dot(constant) - pow(radius,2);
float discriminant = pow(B, 2) - 4 * A * C;
float t1_body = FLT_MIN, t2_body = FLT_MIN;
if (discriminant >= 0)
{
t1_body = (-B + sqrt(discriminant))/(2*A);
t2_body = (-B - sqrt(discriminant))/(2*A);
if (t1_body <= eps && t2_body <= eps) // If behind camera then no intersection
{
return false;
}
else if (t1_body <= eps || t2_body <= eps) // Can't be inside
{
return false;
}
else // Standard double intersection (min)
{
// Check within caps
VEC3 p = start + t2_body * ray;
if (axis.dot(p - c1) > 0 && axis.dot(p - c2) < 0)
{
t = t1_body;
return true;
}
else
{
return false;
}
}
}
return false;
}
bool Cylinder::intersectShadow(const VEC3 ray, const VEC3 start, const float t_max)
{
float eps = 1e-3;
// Check body intersection
VEC3 ray_a_proj = ray - ray.dot(axis) * axis;
VEC3 constant = start - c1 - ((start-c1).dot(axis)) * axis;
float A = ray_a_proj.dot(ray_a_proj);
float B = 2 * ray_a_proj.dot(constant);
float C = constant.dot(constant) - pow(radius,2);
float discriminant = pow(B, 2) - 4 * A * C;
float t1_body, t2_body;
if (discriminant >= 0)
{
t1_body = (-B + sqrt(discriminant))/(2*A);
t2_body = (-B - sqrt(discriminant))/(2*A);
// If behind camera OR behind light then no intersection
if ((t1_body <= eps || t1_body >= t_max) && (t2_body <= eps || t2_body >= t_max))
{
return false;
}
else if (t1_body <= eps || t2_body <= eps) // If inside then return the intersection in front
{
// Check within caps
VEC3 p = start + t1_body * ray;
if (axis.dot(p - c1) > 0 && axis.dot(p - c2) < 0 && t1_body < t_max)
{
return true;
}
else
{
return false;
}
}
else // Standard double intersection
{
// Check within caps
VEC3 p = start + t2_body * ray;
if (axis.dot(p - c1) > 0 && axis.dot(p - c2) < 0 && t2_body < t_max)
{
return true;
}
else
{
return false;
}
}
}
return false;
}
VEC3 Cylinder::getNorm(const VEC3 point)
{
// Project p-c to axis, then subtract
VEC3 pc = point - c1;
VEC3 norm = pc - pc.dot(axis) * axis;
return(norm.normalized());
}
void Cylinder::getBounds(VEC3& lbound, VEC3& ubound)
{
lbound = (c1.array() - radius).min(c2.array() - radius);
ubound = (c1.array() + radius).max(c2.array() + radius);
}
Triangle::Triangle(VEC3 a_vert, VEC3 b_vert, VEC3 c_vert, VEC3 col, std::string material, bool in_motion, std::string shader)
{
A = a_vert;
B = b_vert;
C = c_vert;
color = col;
reflect_params.material = material;
dims = 2;
motion = in_motion;
model = shader;
center = (A + B + C)/3;
name = "triangle";
}
VEC2 Triangle::getUV(const VEC3 p, int& valid)
{
VEC3 bcent = getBarycentricCoords3D(p, A, B, C);
if (bcent[0] < 0 || bcent[0] > 1 || bcent[1] < 0 || bcent[1] > 1 || bcent[2] < 0 || bcent[2] > 1)
{
valid = 0;
return VEC2(-1,-1);
}
// This only makes sense if have UV-mapped vertices
if (uv_verts == false)
{
printf("Triangle getUV: need UV-mapped vertices\n");
throw "Triangle getUV: need UV-mapped vertices";
}
valid = 1;
return(bcent[0] * uvA + bcent[1] * uvB + bcent[2] * uvC);
}
VEC3 getBarycentricCoords2D(VEC2 p, VEC2 A, VEC2 B, VEC2 C)
{
MATRIX2 left; left << B[0] - A[0], C[0] - A[0], B[1]-A[1], C[1]-A[1];
VEC2 right; right << p[0]-A[0], p[1]-A[1];
VEC2 bg = left.inverse()*right;
VEC3 ret(1-bg[0]-bg[1], bg[0], bg[1]);
return ret;
}
VEC3 getBarycentricCoords3D(VEC3 p, VEC3 A, VEC3 B, VEC3 C)
{
VEC3 n = (B - A).cross(C - A);
VEC3 n_a = (C - B).cross(p - B);
VEC3 n_b = (A - C).cross(p - C);
VEC3 n_c = (B - A).cross(p - A);
float n_sqnorm = n.squaredNorm();
float alpha = n.dot(n_a)/n_sqnorm;
float beta = n.dot(n_b)/n_sqnorm;
float gamma = 1 - alpha - beta;
VEC3 ret(alpha, beta, gamma);
return ret;
}
bool Triangle::intersect(const VEC3 ray, const VEC3 start, float& t, bool& inside)
{
inside = false;
float eps = 1e-5;
// Check for EDGE INTERSECTIONS: whenever viewing ray is perpendicular to normal
// if (getNorm(start).dot(ray) == 0)
// {
// // Compute t for every line: should take care of parallel, and behind eye edge cases
// float tmin = FLT_MAX;
// float t_tmp;
// bool hit = false;
// hit = segmentIntersect(A, B, ray, start, t_tmp);
// if (hit == true) {tmin = std::min(t_tmp, tmin);}
//
// hit = segmentIntersect(B, C, ray, start, t_tmp);
// if (hit == true) {tmin = std::min(t_tmp, tmin);}
//
// hit = segmentIntersect(C, A, ray, start, t_tmp);
// if (hit == true) {tmin = std::min(t_tmp, tmin);}
//
// if (tmin < FLT_MAX)
// {
// t = tmin;
// return true;
// }
// else return false;
// }
// Implement Moller and Trumbore 97
VEC3 r1 = B-A;
VEC3 r2 = C-A;
VEC3 norm = r1.cross(r2);
VEC3 h = ray.cross(r2);
float det = r1.dot(h);
float invdet = 1.0/det;
if (det >= -0.0001 && det <= 0.0001)
{
return false;
}
VEC3 A0 = start - A;
float u = invdet * A0.dot(h);
if (u < 0 || u > 1)
{
return false;
}
VEC3 DA0 = A0.cross(r1);
float v = ray.dot(DA0) * invdet;
if (v < 0 || u+v > 1)
{
return false;
}
float t_final = r2.dot(DA0) * invdet;
if (t_final > 0.0001)
{
// Check ray against mesh normal if necessary
if (mesh == true)
{
if (ray.dot(mesh_normal) > 0)
{
inside = true;
}
}
t = t_final;
return true;
}
return false;
}
bool Triangle::intersectShadow(const VEC3 ray, const VEC3 start, const float t_max)
{
// Trumbore algorithm
VEC3 r1 = B-A;
VEC3 r2 = C-A;
VEC3 norm = r1.cross(r2);
VEC3 h = ray.cross(r2);
float det = r1.dot(h);
float invdet = 1.0/det;
if (det >= -0.0001 && det <= 0.0001)
{
return false;
}
VEC3 A0 = start - A;
float u = invdet * A0.dot(h);
if (u < 0 || u > 1)
{
return false;
}
VEC3 DA0 = A0.cross(r1);
float v = ray.dot(DA0) * invdet;
if (v < 0 || u+v > 1)
{
return false;
}
float t_final = r2.dot(DA0) * invdet;
if (t_final > 0.001 && t_final < t_max)
{
return true;
}
return false;
}
VEC3 Triangle::getNorm(const VEC3 point)
{
VEC3 r1 = B - A;
VEC3 r2 = C - A;
VEC3 norm = r1.cross(r2).normalized();
return norm;
}
void Triangle::getBounds(VEC3& lbound, VEC3& ubound)
{
VEC3 min_tmp = A.array().min(B.array());
lbound = min_tmp.array().min(C.array());
VEC3 max_tmp = A.array().max(B.array());
ubound = max_tmp.array().max(C.array());
}
Rectangle::Rectangle()
{
motion = false;
color = VEC3(1,0,0);
length = 1;
width = 1;
A = VEC3(0,0,0);
B = VEC3(1,0,0);
C = VEC3(1,1,0);
D = VEC3(0,1,0);
dims = 2;
model = "lambert";
reflect_params.material = "";
center = (A + B + C + D)/4;
name = "rectangle";
}
Rectangle::Rectangle(VEC3 a, VEC3 b, VEC3 c, VEC3 d, VEC3 col, std::string material, bool in_motion,
int texframe, std::string shader)
{
A = a;
B = b;
C = c;
D = d;
length = (b-a).norm();
width = (d-a).norm();
color = col;
reflect_params.material = material;
dims = 2;
motion = in_motion;
model = shader;
center = (A + B + C + D)/4;
name = "rectangle";
if (texframe >= 0) tex_frame=texframe;
}
bool Rectangle::intersect(const VEC3 ray, const VEC3 start, float& t, bool& inside)
{
float eps = 1e-4;
inside = false;
// Check for EDGE INTERSECTIONS: whenever viewing ray is perpendicular to normal
// if (getNorm(start).dot(ray) == 0)
// {
// // Compute t for every line: should take care of parallel, and behind eye edge cases
// float tmin = FLT_MAX;
// float t_tmp;
// bool hit = false;
// hit = segmentIntersect(A, B, ray, start, t_tmp);
// if (hit == true) {tmin = std::min(t_tmp, tmin);}
//
// hit = segmentIntersect(A, D, ray, start, t_tmp);
// if (hit == true) {tmin = std::min(t_tmp, tmin);}
//
// hit = segmentIntersect(B, C, ray, start, t_tmp);
// if (hit == true) {tmin = std::min(t_tmp, tmin);}
//
// hit = segmentIntersect(C, D, ray, start, t_tmp);
// if (hit == true) {tmin = std::min(t_tmp, tmin);}
//
// if (tmin < FLT_MAX)
// {
// t = tmin;
// return true;
// }
// else return false;
// }
// Hardcode solution
VEC3 norm = getNorm(start).normalized();
float dn = ray.dot(norm); // Parallel edge case: should be taken care of with above but just in case
if (dn == 0)
{
return false;
}
float t_final = (A - start).dot(norm)/dn;
if (t_final <= eps)
{
return false;
}
VEC3 point = start + t_final * ray;
VEC3 V_hit = point - A;
VEC3 V1 = B-A;
VEC3 V2 = D-A;
float check1 = V1.normalized().dot(V_hit);
float check2 = V2.normalized().dot(V_hit);
if (0 <= check1 && check1 <= V1.norm() && 0 <= check2 && check2 <= V2.norm())
{
t = t_final;
return true;
}
return false;
}
bool Rectangle::intersectShadow(const VEC3 ray, const VEC3 start, const float t_max)
{
// Check for EDGE INTERSECTIONS: whenever viewing ray is perpendicular to normal
float eps = 1e-4;
// if (getNorm(start).dot(ray) == 0)
// {
// float t_tmp;
// bool hit = false;
// hit = segmentIntersect(A, B, ray, start, t_tmp);
// if (hit == true) {return true;}
//
// hit = segmentIntersect(A, D, ray, start, t_tmp);
// if (hit == true) {return true;}
//
// hit = segmentIntersect(B, C, ray, start, t_tmp);
// if (hit == true) {return true;}
//
// hit = segmentIntersect(C, D, ray, start, t_tmp);
// if (hit == true) {return true;}
//
// return false;
// }
// Hardcode solution
VEC3 norm = getNorm(start).normalized();
float dn = ray.dot(norm); // Parallel edge case: should be taken care of with above but just in case
if (dn == 0)
{
return false;
}
float t_final = (A - start).dot(norm)/dn;
if (t_final <= eps)
{
return false;
}
VEC3 point = start + t_final * ray;
VEC3 V_hit = point - A;
VEC3 V1 = B-A;
VEC3 V2 = D-A;
float check1 = V1.normalized().dot(V_hit);
float check2 = V2.normalized().dot(V_hit);
if (0 <= check1 && check1 <= V1.norm() && 0 <= check2 && check2 <= V2.norm() && t_final < t_max)
{
return true;
}
return false;
}
VEC3 Rectangle::getNorm(const VEC3 point)
{
VEC3 r1 = B - A;
VEC3 r2 = C - A;
VEC3 norm = r1.cross(r2).normalized();
return norm;
}
VEC2 Rectangle::getUV(const VEC3 p, int& valid)
{
VEC3 ad = D-A;
VEC3 dc = C-D;
float u = (p-A).cross(ad).norm()/(ad.norm() * dc.norm()); // "X" distance
float v = (p-D).cross(dc).norm()/(dc.norm() * ad.norm()); // "Y" distance
valid = 1;
return VEC2(u,v);
}
void Rectangle::getBounds(VEC3& lbound, VEC3& ubound)
{
VEC3 min_tmp = A.array().min(B.array());
min_tmp = min_tmp.array().min(C.array());
lbound = min_tmp.array().min(D.array());
VEC3 max_tmp = A.array().max(B.array());
max_tmp = max_tmp.array().max(C.array());
ubound = max_tmp.array().max(D.array());
}
VEC3 Rectangle::samplePoint()
{
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_real_distribution<double> uniform(0.0, 1.0);
float x = uniform(generator);
float y = uniform(generator);
VEC3 sample = A + x * (B-A) + y * (D-A);
return sample;
}
RectPrismV2::RectPrismV2(VEC3 a, VEC3 b, VEC3 c, VEC3 d, VEC3 e, VEC3 f, VEC3 g, VEC3 h,
VEC3 col, std::string material, bool in_motion,
int texframe, std::string shader)
{
A = a;
B = b;
C = c;
D = d;
E = e;
F = f;
G = g;
H = h;
faces.push_back(std::make_shared<Rectangle>(a,b,c,d,col));
faces.push_back(std::make_shared<Rectangle>(e,f,g,h,col));
faces.push_back(std::make_shared<Rectangle>(a,b,f,e,col));
faces.push_back(std::make_shared<Rectangle>(d,a,e,h,col));
faces.push_back(std::make_shared<Rectangle>(b,c,g,f,col));
faces.push_back(std::make_shared<Rectangle>(c,d,h,g,col));
length = (b-a).norm();
width = (d-a).norm();
height = (e-a).norm();
color = col;
reflect_params.material = material;
dims = 3;
motion = in_motion;
model = shader;
center = (A + B + C + D + E + F + G + H)/8;
name = "rectprism";
if (texframe >= 0) tex_frame=texframe;
}
bool RectPrismV2::intersect(const VEC3 ray, const VEC3 start, float& t, bool& inside)
{
float tmin = FLT_MAX;
float t_tmp;
bool inside_tmp;
for (std::shared_ptr<Rectangle> face : faces)
{
bool hit = face->intersect(ray, start, t_tmp, inside_tmp);
if (hit == true)
{
if (t_tmp < tmin)
{
tmin = t_tmp;
inside = inside_tmp;
}
}
}
if (tmin < FLT_MAX)
{
t = tmin;
return true;
}
return false;
}
bool RectPrismV2::intersectShadow(const VEC3 ray, const VEC3 start, const float t_max)
{
for (std::shared_ptr<Rectangle> face : faces)
{
bool hit = face->intersectShadow(ray, start, t_max);
if (hit == true)
{
// if (start[2] > 21)
// {
// std::cout << "Point: " << start << std::endl;
// std::cout << "Ray: " << ray << std::endl;
// std::cout << "Occluding shape center: " << face->center << std::endl;
// std::cout << "A: " << face->A << std::endl;
// std::cout << "B: " << face->B << std::endl;
// std::cout << "C: " << face->C << std::endl;
// std::cout << "D: " << face->D << std::endl;
// }
return true;
}
}
return false;
}
VEC3 RectPrismV2::getNorm(const VEC3 point)
{
float eps = 1e-3;
VEC3 normbot = -(F-E).cross(H-E).normalized();
VEC3 normright = (E-A).cross(D-A).normalized();
VEC3 normfront = (B-A).cross(E-A).normalized();
float pa_bot = abs((point - A).normalized().dot(normbot));
float pg_bot = abs((point - G).normalized().dot(normbot));
if (pa_bot <= eps || pg_bot <= eps)
{
return normbot;
}
float pa_right= abs((point - A).normalized().dot(normright));
float pg_right = abs((point - G).normalized().dot(normright));
if (pa_right <= eps || pg_right <= eps)
{
return normright;
}
float pa_front = abs((point - A).normalized().dot(normfront));
float pg_front = abs((point - G).normalized().dot(normfront));
if (pa_front <= eps || pg_front <= eps)
{
return normfront;
}
// If made it this far, then point is not on surface of prism
printf("RectPrism getNorm: point is not on prism!\n");
std::cout << "Point: " << point << std::endl;
std::cout << "A: " << A << std::endl;
std::cout << "B: " << B << std::endl;
std::cout << "C: " << C << std::endl;
std::cout << "D: " << D << std::endl;
std::cout << "E: " << E << std::endl;
std::cout << "F: " << F << std::endl;
std::cout << "G: " << G << std::endl;
std::cout << "H: " << H << std::endl;
std::cout << "P-A botnorm check: " << abs((point - A).normalized().dot(normbot)) << std::endl;
std::cout << "P-A normright check: " << abs((point - A).normalized().dot(normright)) << std::endl;
std::cout << "P-A normfront check: " << abs((point - A).normalized().dot(normfront)) << std::endl;
std::cout << "P-G botnorm check: " << abs((point - G).normalized().dot(normbot)) << std::endl;
std::cout << "P-G normright check: " << abs((point - G).normalized().dot(normright)) << std::endl;
std::cout << "P-G normfront check: " << abs((point - G).normalized().dot(normfront)) << std::endl;
// Just return the closest one
float min_side = std::min({pa_bot, pg_bot, pa_right, pg_right, pa_front, pg_front});
if (pa_bot == min_side || pg_bot == min_side)
{
return normbot;
}
if (pa_right == min_side || pg_right == min_side)
{
return normright;
}
return normfront;
}
void RectPrismV2::getBounds(VEC3& lbound, VEC3& ubound)
{
VEC3 min_tmp = A.array().min(B.array());
min_tmp = min_tmp.array().min(C.array());
min_tmp = min_tmp.array().min(D.array());
min_tmp = min_tmp.array().min(E.array());
min_tmp = min_tmp.array().min(F.array());
min_tmp = min_tmp.array().min(G.array());
min_tmp = min_tmp.array().min(H.array());
lbound = min_tmp;
VEC3 max_tmp = A.array().max(B.array());
max_tmp = max_tmp.array().max(C.array());
max_tmp = max_tmp.array().max(D.array());
max_tmp = max_tmp.array().max(E.array());
max_tmp = max_tmp.array().max(F.array());
max_tmp = max_tmp.array().max(G.array());
max_tmp = max_tmp.array().max(H.array());
ubound = max_tmp;
}
VEC2 RectPrismV2::getUV(const VEC3 p, int& valid)
{
// Get UV of TOP face!
VEC2 uv = faces[0]->getUV(p, valid);
return uv;
}
RectPrism::RectPrism(VEC3 a, VEC3 b, VEC3 c, VEC3 d, VEC3 e, VEC3 f, VEC3 g, VEC3 h,
VEC3 col, std::string material, bool in_motion,
int texframe, std::string shader)
{
A = a;
B = b;
C = c;
D = d;
E = e;
F = f;
G = g;
H = h;
length = (b-a).norm();
width = (d-a).norm();
height = (e-a).norm();
color = col;
reflect_params.material = material;
dims = 3;
motion = in_motion;
model = shader;
center = (A + B + C + D + E + F + G + H)/8;
name = "rectprism";
if (texframe >= 0) tex_frame=texframe;
// Pre-compute change of basis
VEC3 w = (e-a).normalized();
VEC3 u = (b-a).normalized();
VEC3 v = (d-a).normalized();
MATRIX4 cob; cob << u.transpose(), 0, v.transpose(), 0, w.transpose(), 0,
0, 0, 0, 1;
MATRIX4 origin = MatrixXd::Identity(4,4);
origin(0,3) = -center[0];
origin(1,3) = -center[1];
origin(2,3) = -center[2];
objM = cob * origin;
// Set bounds
getObjBounds(lbound, ubound);
getBounds(lbound, ubound);
}
bool RectPrism::intersect(const VEC3 ray, const VEC3 start, float& t, bool& inside)
{
// Same as bounding volume
float eps = 1e-6;
VEC3 inv_ray = ray.cwiseInverse();
float tmin;
float tmax;
inside = false;
if (isinf(inv_ray[0]))
{