-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtests.cpp
More file actions
3213 lines (2556 loc) · 90 KB
/
Copy pathtests.cpp
File metadata and controls
3213 lines (2556 loc) · 90 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
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 "tests.h"
#include "camera.h"
#include "color.h"
#include "computations.h"
#include "cone.h"
#include "cube.h"
#include "cylinder.h"
#include "groups.h"
#include "intersection.h"
#include "light_source.h"
#include "matrix.h"
#include "obj_parser.h"
#include "pattern.h"
#include "plane.h"
#include "ray.h"
#include "shapes.h"
#include "sphere.h"
#include "triangle.h"
#include "tuple.h"
#include "utilities.h"
#include "world.h"
#include <cassert>
#include <vector>
void test_tuple_to_point() {
RayPoint p = RayPoint(4, -4, 3);
assert(equal(p.w, 1.0) && "Point is not equal.");
std::cout << "[PASS 1.1] Points equal." << std::endl;
}
void test_tuple_to_vector() {
RayVector v = RayVector(4, -4, 3);
assert(equal(v.w, 0.0) && "Vector is not equal.");
std::cout << "[PASS 1.2] Vectors are equal." << std::endl;
}
void test_tuple_addition() {
RayTuple t1 = RayTuple(3, -2, 5, 1);
RayTuple t2 = RayTuple(-2, 3, 1, 0);
RayTuple t3 = t1 + t2;
assert(t3 == RayTuple(1, 1, 6, 1));
std::cout << "[PASS 1.3] Tuple addition works perfectly." << std::endl;
}
void test_point_minus_point() {
RayPoint s1(3, 2, 1);
RayPoint s2(5, 6, 7);
RayVector s3 = s1 - s2;
assert((s3 == RayVector(-2, -4, -6)) && "Point - Point failed.");
std::cout << "[PASS 1.4] Point - Point subtraction works perfectly."
<< std::endl;
}
void test_point_minus_vector() {
RayPoint p1(3, 2, 1);
RayVector v2(5, 6, 7);
RayPoint x3 = p1 - v2;
assert((x3 == RayPoint(-2, -4, -6)) && "Point - Vector failed.");
std::cout << "[PASS 1.5] Point - Vector subtraction works perfectly."
<< std::endl;
}
void test_subtract_vector_from_zero_vector() {
RayVector zero = RayVector(0, 0, 0);
RayVector v = RayVector(1, -2, 3);
assert((zero - v == RayVector(-1, 2, -3)) && "Zero - Vecotr failed.");
std::cout << "[PASS 1.6] Zero Vector - Vector works." << std::endl;
}
void test_negate_tuple() {
RayTuple t = RayTuple(1, -2, 3, -4);
assert((-t == RayTuple(-1, 2, -3, 4)) && "Negate failed.");
std::cout << "[PASS 1.7] Negate works." << std::endl;
}
void test_multiply_tuple_by_scalar() {
RayTuple t = RayTuple(1, -2, 3, -4);
assert((t * 3.5 == RayTuple(3.5, -7, 10.5, -14) &&
"Multiply with scalar failed."));
std::cout << "[PASS 1.8] Multiply Tuple with Scalar works." << std::endl;
}
void test_divide_tuple_by_scalar() {
RayTuple t = RayTuple(1, -2, 3, -4);
assert(
(t / 2.0 == RayTuple(0.5, -1, 1.5, -2) && "Divide with scalar failed."));
std::cout << "[PASS 1.9] Divide Tuple with Scalar works." << std::endl;
}
void test_magnitude_of_vector() {
RayVector v = RayVector(1, 0, 0);
assert(equal(v.magnitude(), 1.0) && "Magnitude of vector failed.");
RayVector v1 = RayVector(0, 1, 0);
assert(equal(v1.magnitude(), 1.0) && "Magnitude of vector failed.");
RayVector v2 = RayVector(0, 0, 1);
assert(equal(v2.magnitude(), 1.0) && "Magnitude of vector failed.");
RayVector v3 = RayVector(1, 2, 3);
assert(equal(v3.magnitude(), std::sqrt(14)) && "Magnitude of vector failed.");
RayVector v4 = RayVector(-1, -2, -3);
assert(equal(v4.magnitude(), std::sqrt(14)) && "Magnitude of vector failed.");
std::cout << "[PASS 1.10] Magnitude works." << std::endl;
}
void test_normalize_vector() {
RayVector v(4, 0, 0);
assert(v.normalize() == RayVector(1, 0, 0) && "normalize failed.");
RayVector v1(1, 2, 3);
assert(v1.normalize() == RayVector(0.26726, 0.53452, 0.80178) &&
"normalize failed.");
RayVector v2(1, 2, 3);
RayTuple v2_norm = v2.normalize();
assert(equal(v2_norm.magnitude(), 1.0) &&
"magnitude of norm vector is one, failed.");
std::cout << "[PASS 1.11] Normalize vector works." << std::endl;
}
void test_dot_product_vector() {
RayVector a = RayVector(1, 2, 3);
RayVector b = RayVector(2, 3, 4);
assert(equal(a.dot(b), 20.0) && "vector dot product failed.");
std::cout << "[PASS 1.12] Dot product of Vectors works." << std::endl;
}
void test_cross_product_vector() {
RayVector a = RayVector(1, 2, 3);
RayVector b = RayVector(2, 3, 4);
assert(a.cross(b) == RayVector(-1, 2, -1) &&
b.cross(a) == RayVector(1, -2, 1) && "Vector cross failed.");
std::cout << "[PASS 1.13] Cross product of vectors works." << std::endl;
}
void test_color_implementation() {
Color c = Color(-0.5, 0.4, 1.7);
assert(c.r == -0.5 && c.g == 0.4 && c.b == 1.7 &&
"Failed color implementation.");
std::cout << "[PASS 2.1] Colors implementation works." << std::endl;
}
void test_color_addition() {
Color c1 = Color(0.9, 0.6, 0.75);
Color c2 = Color(0.7, 0.1, 0.25);
assert((c1 + c2 == Color(1.6, 0.7, 1.0)) && "Color addition failed.");
std::cout << "[PASS 2.2] Colors addition works." << std::endl;
}
void test_color_subtraction() {
Color c1 = Color(0.9, 0.6, 0.75);
Color c2 = Color(0.7, 0.1, 0.25);
assert((c1 - c2 == Color(0.2, 0.5, 0.5)) && "Color subtraction failed.");
std::cout << "[PASS 2.3] Colors subtraction works." << std::endl;
}
void test_color_multiplication_by_scalar() {
Color c = Color(0.2, 0.3, 0.4);
assert((c * 2 == Color(0.4, 0.6, 0.8)) && "Color multiply by scalar failed.");
std::cout << "[PASS 2.4] Color multiply by scalar works." << std::endl;
}
void test_color_multiply_by_color() {
Color c1 = Color(1, 0.2, 0.4);
Color c2 = Color(0.9, 1, 0.1);
assert((c1 * c2 == Color(0.9, 0.2, 0.04)) &&
"Color multiply by color failed.");
std::cout << "[PASS 2.5] Color multiply by color works." << std::endl;
}
void test_create_canvas() {
Canvas c = Canvas(10, 20);
assert(
(c.width == 10 && c.height == 20 && c.pixel_at(0, 0) == Color(0, 0, 0)) &&
"create canvas failed.");
std::cout << "[PASS 2.6] Created Canvas works." << std::endl;
}
void test_writing_canvas() {
Canvas c = Canvas(10, 20);
Color red = Color(1, 0, 0);
c.write_pixel(2, 3, red);
assert((c.pixel_at(2, 3) == red) && "Color at pixel in canvas failed.");
std::cout << "[PASS 2.7] Writing pixels to canvas works." << std::endl;
}
void test_canvas_to_ppm_header() {
Canvas c = Canvas(5, 3);
std::string wh = "";
wh += std::to_string(c.width);
wh += " ";
wh += std::to_string(c.height);
std::string ppm = c.canvas_to_ppm();
std::stringstream ss(ppm);
std::string line1, line2, line3;
std::getline(ss, line1);
std::getline(ss, line2);
std::getline(ss, line3);
assert(line1 == "P3");
assert(line2 == wh);
assert(line3 == "255");
std::cout << "[PASS 2.6] Canvas header works." << std::endl;
}
void test_matrix_construction() {
Matrix m = Matrix(4, 4);
assert(m.rows == 4);
assert(m.columns == 4);
for (unsigned int i = 0; i < m.rows; i++) {
for (unsigned int j = 0; j < m.columns; j++) {
assert(m[i][j] == 0.0);
}
}
std::cout
<< "[PASS 3.1] Matrix construction and default initialization works."
<< std::endl;
}
void test_matrix_comparision_equal() {
Matrix m1(4, 4);
Matrix m2(4, 4);
m1[0][0] = 1.0;
m1[0][1] = 2.0;
m1[0][2] = 3.0;
m1[0][3] = 4.0;
m2[0][0] = 1.0;
m2[0][1] = 2.0;
m2[0][2] = 3.0;
m2[0][3] = 4.0;
m1[1][0] = 5.5;
m1[1][1] = 6.5;
m1[1][2] = 7.5;
m1[1][3] = 8.5;
m2[1][0] = 5.5;
m2[1][1] = 6.5;
m2[1][2] = 7.5;
m2[1][3] = 8.5;
m1[2][0] = 9.0;
m1[2][1] = 10.0;
m1[2][2] = 11.0;
m1[2][3] = 12.0;
m2[2][0] = 9.0;
m2[2][1] = 10.0;
m2[2][2] = 11.0;
m2[2][3] = 12.0;
m1[3][0] = 13.5;
m1[3][1] = 14.5;
m1[3][2] = 15.5;
m1[3][3] = 16.5;
m2[3][0] = 13.5;
m2[3][1] = 14.5;
m2[3][2] = 15.5;
m2[3][3] = 16.5;
assert((m1 == m2) && "comparision equal failed.");
std::cout << "[PASS 3.2] Matrix comparision works." << std::endl;
}
void test_matrix_comparision_not_equal() {
Matrix m1(4, 4);
Matrix m2(4, 4);
m1[0][0] = 1.0;
m1[0][1] = 2.0;
m1[0][2] = 3.0;
m1[0][3] = 4.0;
m2[0][0] = 1.0;
m2[0][1] = 6.0;
m2[0][2] = 3.0;
m2[0][3] = 4.0;
m1[1][0] = 5.5;
m1[1][1] = 6.5;
m1[1][2] = 7.5;
m1[1][3] = 5.5;
m2[1][0] = 5.5;
m2[1][1] = 6.5;
m2[1][2] = 7.5;
m2[1][3] = 8.5;
m1[2][0] = 9.0;
m1[2][1] = 10.0;
m1[2][2] = 11.0;
m1[2][3] = 2.0;
m2[2][0] = 9.0;
m2[2][1] = 10.0;
m2[2][2] = 11.0;
m2[2][3] = 2.0;
m1[3][0] = 13.5;
m1[3][1] = 14.5;
m1[3][2] = 5.5;
m1[3][3] = 16.5;
m2[3][0] = 13.5;
m2[3][1] = 14.5;
m2[3][2] = 15.5;
m2[3][3] = 16.5;
assert((m1 != m2) && "comparision not equal failed.");
std::cout << "[PASS 3.3] Matrix comparision not equal works." << std::endl;
}
void test_matrix_multiply() {
Matrix m1(4, 4);
m1[0][0] = 1.0;
m1[0][1] = 2.0;
m1[0][2] = 3.0;
m1[0][3] = 4.0;
m1[1][0] = 5.0;
m1[1][1] = 6.0;
m1[1][2] = 7.0;
m1[1][3] = 8.0;
m1[2][0] = 9.0;
m1[2][1] = 8.0;
m1[2][2] = 7.0;
m1[2][3] = 6.0;
m1[3][0] = 5.0;
m1[3][1] = 4.0;
m1[3][2] = 3.0;
m1[3][3] = 2.0;
Matrix m2(4, 4);
m2[0][0] = -2.0;
m2[0][1] = 1.0;
m2[0][2] = 2.0;
m2[0][3] = 3.0;
m2[1][0] = 3.0;
m2[1][1] = 2.0;
m2[1][2] = 1.0;
m2[1][3] = -1.0;
m2[2][0] = 4.0;
m2[2][1] = 3.0;
m2[2][2] = 6.0;
m2[2][3] = 5.0;
m2[3][0] = 1.0;
m2[3][1] = 2.0;
m2[3][2] = 7.0;
m2[3][3] = 8.0;
Matrix result = m1.matrix_multiply(m2);
assert(result.rows == 4);
assert(result.columns == 4);
assert(equal(result[0][0], 20.0));
assert(equal(result[0][1], 22.0));
assert(equal(result[0][2], 50.0));
assert(equal(result[0][3], 48.0));
assert(equal(result[1][0], 44.0));
assert(equal(result[1][1], 54.0));
assert(equal(result[1][2], 114.0));
assert(equal(result[1][3], 108.0));
assert(equal(result[2][0], 40.0));
assert(equal(result[2][1], 58.0));
assert(equal(result[2][2], 110.0));
assert(equal(result[2][3], 102.0));
assert(equal(result[3][0], 16.0));
assert(equal(result[3][1], 26.0));
assert(equal(result[3][2], 46.0));
assert(equal(result[3][3], 42.0));
std::cout << "[PASS 3.4] Matrix multiplication works." << std::endl;
}
void test_matrix_tuple_multiply() {
Matrix m(4, 4);
m[0][0] = 1.0;
m[0][1] = 2.0;
m[0][2] = 3.0;
m[0][3] = 4.0;
m[1][0] = 2.0;
m[1][1] = 4.0;
m[1][2] = 4.0;
m[1][3] = 2.0;
m[2][0] = 8.0;
m[2][1] = 6.0;
m[2][2] = 4.0;
m[2][3] = 1.0;
m[3][0] = 0.0;
m[3][1] = 0.0;
m[3][2] = 0.0;
m[3][3] = 1.0;
RayTuple t(1.0, 2.0, 3.0, 1.0);
RayTuple result = m.tuple_multiply(t);
assert(equal(result.x, 18.0));
assert(equal(result.y, 24.0));
assert(equal(result.z, 33.0));
assert(equal(result.w, 1.0));
std::cout << "[PASS 3.5] Matrix multiplied by RayTuple works." << std::endl;
}
void test_identity_matrix_multiplication() {
Matrix m(4, 4);
m[0][0] = 0.0;
m[0][1] = 1.0;
m[0][2] = 2.0;
m[0][3] = 4.0;
m[1][0] = 1.0;
m[1][1] = 2.0;
m[1][2] = 4.0;
m[1][3] = 8.0;
m[2][0] = 2.0;
m[2][1] = 4.0;
m[2][2] = 8.0;
m[2][3] = 16.0;
m[3][0] = 4.0;
m[3][1] = 8.0;
m[3][2] = 16.0;
m[3][3] = 32.0;
Matrix id = Matrix::identity(4);
Matrix result_matrix = m.matrix_multiply(id);
assert(result_matrix == m);
RayTuple t(1.0, 2.0, 3.0, 4.0);
RayTuple result_tuple = id.tuple_multiply(t);
assert(equal(result_tuple.x, 1.0));
assert(equal(result_tuple.y, 2.0));
assert(equal(result_tuple.z, 3.0));
assert(equal(result_tuple.w, 4.0));
std::cout << "[PASS 3.6] Identity matrix multiplications works." << std::endl;
}
void test_matrix_transpose() {
Matrix m(4, 4);
m[0][0] = 0.0;
m[0][1] = 9.0;
m[0][2] = 3.0;
m[0][3] = 0.0;
m[1][0] = 9.0;
m[1][1] = 8.0;
m[1][2] = 0.0;
m[1][3] = 8.0;
m[2][0] = 1.0;
m[2][1] = 8.0;
m[2][2] = 5.0;
m[2][3] = 3.0;
m[3][0] = 0.0;
m[3][1] = 0.0;
m[3][2] = 5.0;
m[3][3] = 8.0;
Matrix current = m.transpose();
assert(current[0][0] == 0.0);
assert(current[0][1] == 9.0);
assert(current[0][2] == 1.0);
assert(current[0][3] == 0.0);
assert(current[1][0] == 9.0);
assert(current[1][1] == 8.0);
assert(current[1][2] == 8.0);
assert(current[1][3] == 0.0);
assert(current[2][0] == 3.0);
assert(current[2][1] == 0.0);
assert(current[2][2] == 5.0);
assert(current[2][3] == 5.0);
assert(current[3][0] == 0.0);
assert(current[3][1] == 8.0);
assert(current[3][2] == 3.0);
assert(current[3][3] == 8.0);
Matrix id = Matrix::identity(4);
assert(id.transpose() == id);
std::cout << "[PASS 3.7] Matrix transpositions works." << std::endl;
}
void test_matrix_submatrix() {
Matrix m1(3, 3);
m1[0][0] = 1.0;
m1[0][1] = 5.0;
m1[0][2] = 0.0;
m1[1][0] = -3.0;
m1[1][1] = 2.0;
m1[1][2] = 7.0;
m1[2][0] = 0.0;
m1[2][1] = 6.0;
m1[2][2] = -3.0;
Matrix sub1 = m1.submatrix(0, 2);
assert(sub1.rows == 2);
assert(sub1.columns == 2);
assert(equal(sub1[0][0], -3.0));
assert(equal(sub1[0][1], 2.0));
assert(equal(sub1[1][0], 0.0));
assert(equal(sub1[1][1], 6.0));
Matrix m2(4, 4);
m2[0][0] = -6.0;
m2[0][1] = 1.0;
m2[0][2] = 1.0;
m2[0][3] = 6.0;
m2[1][0] = -8.0;
m2[1][1] = 5.0;
m2[1][2] = 8.0;
m2[1][3] = 6.0;
m2[2][0] = -1.0;
m2[2][1] = 0.0;
m2[2][2] = 8.0;
m2[2][3] = 2.0;
m2[3][0] = -7.0;
m2[3][1] = 1.0;
m2[3][2] = -1.0;
m2[3][3] = 1.0;
Matrix sub2 = m2.submatrix(2, 1);
assert(sub2.rows == 3);
assert(sub2.columns == 3);
assert(equal(sub2[0][0], -6.0));
assert(equal(sub2[0][1], 1.0));
assert(equal(sub2[0][2], 6.0));
assert(equal(sub2[1][0], -8.0));
assert(equal(sub2[1][1], 8.0));
assert(equal(sub2[1][2], 6.0));
assert(equal(sub2[2][0], -7.0));
assert(equal(sub2[2][1], -1.0));
assert(equal(sub2[2][2], 1.0));
std::cout << "[PASS 3.8] Matrix submatrix extraction works." << std::endl;
}
void test_matrix_determinant_2x2() {
Matrix m(2, 2);
m[0][0] = 1.0;
m[0][1] = 5.0;
m[1][0] = -3.0;
m[1][1] = 2.0;
double det = m.determinant();
assert(equal(det, 17.0));
std::cout << "[PASS 3.9] 2x2 Matrix determinant calculation works."
<< std::endl;
}
void test_matrix_minor_3x3() {
Matrix m(3, 3);
m[0][0] = 3.0;
m[0][1] = 5.0;
m[0][2] = 0.0;
m[1][0] = 2.0;
m[1][1] = -1.0;
m[1][2] = -7.0;
m[2][0] = 6.0;
m[2][1] = -1.5;
m[2][2] = 5.0;
Matrix sub = m.submatrix(1, 0);
assert(equal(sub.determinant(), 25.0));
assert(equal(m.minor(1, 0), 25.0));
std::cout << "[PASS 3.10] 3x3 Matrix minor calculation works." << std::endl;
}
void test_matrix_cofactor() {
Matrix m(3, 3);
m[0][0] = 3.0;
m[0][1] = 5.0;
m[0][2] = 0.0;
m[1][0] = 2.0;
m[1][1] = -1.0;
m[1][2] = -7.0;
m[2][0] = 6.0;
m[2][1] = -1.5;
m[2][2] = 5.0;
assert(equal(m.minor(0, 0), -15.5));
assert(equal(m.cofactor(0, 0), -15.5));
assert(equal(m.minor(1, 0), 25.0));
assert(equal(m.cofactor(1, 0), -25.0));
std::cout << "[PASS 3.11] Matrix cofactor calculations works." << std::endl;
}
void test_matrix_determinant_large() {
Matrix m1(3, 3);
m1[0][0] = 1.0;
m1[0][1] = 2.0;
m1[0][2] = 6.0;
m1[1][0] = -5.0;
m1[1][1] = 8.0;
m1[1][2] = -4.0;
m1[2][0] = 2.0;
m1[2][1] = 6.0;
m1[2][2] = 4.0;
assert(equal(m1.cofactor(0, 0), 56.0));
assert(equal(m1.cofactor(0, 1), 12.0));
assert(equal(m1.cofactor(0, 2), -46.0));
assert(equal(m1.determinant(), -196.0));
Matrix m2(4, 4);
m2[0][0] = -2.0;
m2[0][1] = -8.0;
m2[0][2] = 3.0;
m2[0][3] = 5.0;
m2[1][0] = -3.0;
m2[1][1] = 1.0;
m2[1][2] = 7.0;
m2[1][3] = 3.0;
m2[2][0] = 1.0;
m2[2][1] = 2.0;
m2[2][2] = -9.0;
m2[2][3] = 6.0;
m2[3][0] = -6.0;
m2[3][1] = 7.0;
m2[3][2] = 7.0;
m2[3][3] = -9.0;
assert(m2.cofactor(0, 0) == 690.0);
assert(m2.cofactor(0, 1) == 447.0);
assert(m2.cofactor(0, 2) == 210.0);
assert(m2.cofactor(0, 3) == 51.0);
assert(equal(m2.determinant(), -4071.0));
std::cout << "[PASS 3.12] Large matrix determinant calculations works."
<< std::endl;
}
void test_matrix_inversion() {
Matrix invertible_m(4, 4);
invertible_m[0][0] = 6.0;
invertible_m[0][1] = 4.0;
invertible_m[0][2] = 4.0;
invertible_m[0][3] = 4.0;
invertible_m[1][0] = 5.0;
invertible_m[1][1] = 5.0;
invertible_m[1][2] = 7.0;
invertible_m[1][3] = 6.0;
invertible_m[2][0] = 4.0;
invertible_m[2][1] = -9.0;
invertible_m[2][2] = 3.0;
invertible_m[2][3] = -7.0;
invertible_m[3][0] = 9.0;
invertible_m[3][1] = 1.0;
invertible_m[3][2] = 7.0;
invertible_m[3][3] = -6.0;
assert(equal(invertible_m.determinant(), -2120.0));
assert(invertible_m.is_invertible() == true);
Matrix non_invertible_m(4, 4);
non_invertible_m[0][0] = -4.0;
non_invertible_m[0][1] = 2.0;
non_invertible_m[0][2] = -2.0;
non_invertible_m[0][3] = -3.0;
non_invertible_m[1][0] = 9.0;
non_invertible_m[1][1] = 6.0;
non_invertible_m[1][2] = 2.0;
non_invertible_m[1][3] = 6.0;
non_invertible_m[2][0] = 0.0;
non_invertible_m[2][1] = -5.0;
non_invertible_m[2][2] = 1.0;
non_invertible_m[2][3] = -5.0;
non_invertible_m[3][0] = 0.0;
non_invertible_m[3][1] = 0.0;
non_invertible_m[3][2] = 0.0;
non_invertible_m[3][3] = 0.0;
assert(equal(non_invertible_m.determinant(), 0.0));
assert(non_invertible_m.is_invertible() == false);
Matrix A(4, 4);
A[0][0] = -5.0;
A[0][1] = 2.0;
A[0][2] = 6.0;
A[0][3] = -8.0;
A[1][0] = 1.0;
A[1][1] = -5.0;
A[1][2] = 1.0;
A[1][3] = 8.0;
A[2][0] = 7.0;
A[2][1] = 7.0;
A[2][2] = -6.0;
A[2][3] = -7.0;
A[3][0] = 1.0;
A[3][1] = -3.0;
A[3][2] = 7.0;
A[3][3] = 4.0;
Matrix B = A.inverse();
assert(equal(A.determinant(), 532.0));
assert(equal(B[0][0], 0.21805));
assert(equal(B[0][1], 0.45113));
assert(equal(B[2][3], 0.197368));
assert(equal(B[3][3], 0.306391));
Matrix C(4, 4);
C[0][0] = 3.0;
C[0][1] = -9.0;
C[0][2] = 7.0;
C[0][3] = 3.0;
C[1][0] = 3.0;
C[1][1] = -8.0;
C[1][2] = 2.0;
C[1][3] = -9.0;
C[2][0] = -4.0;
C[2][1] = 4.0;
C[2][2] = 4.0;
C[2][3] = 1.0;
C[3][0] = -6.0;
C[3][1] = 5.0;
C[3][2] = -1.0;
C[3][3] = 1.0;
Matrix D(4, 4);
D[0][0] = 8.0;
D[0][1] = 2.0;
D[0][2] = 2.0;
D[0][3] = 2.0;
D[1][0] = 3.0;
D[1][1] = -1.0;
D[1][2] = 7.0;
D[1][3] = 0.0;
D[2][0] = 7.0;
D[2][1] = 0.0;
D[2][2] = 5.0;
D[2][3] = 4.0;
D[3][0] = 6.0;
D[3][1] = -2.0;
D[3][2] = 0.5;
D[3][3] = 1.0;
Matrix Product = C.matrix_multiply(D);
Matrix Original_C = Product.matrix_multiply(D.inverse());
assert(Original_C == C);
}
void test_matrix_translation() {
Matrix transform = Matrix::translation(5, -3, 2);
RayPoint p(-3, 4, 5);
RayPoint result = transform.transform_point(p);
assert(equal(result.x, 2.0));
assert(equal(result.y, 1.0));
assert(equal(result.z, 7.0));
assert(equal(result.w, 1.0));
Matrix inv = transform.inverse();
RayPoint back_result = inv.transform_point(p);
assert(equal(back_result.x, -8.0));
assert(equal(back_result.y, 7.0));
assert(equal(back_result.z, 3.0));
assert(equal(back_result.w, 1.0));
RayVector v(-3, 4, 5);
RayVector vec_result = transform.transform_vector(v);
assert(equal(vec_result.x, -3.0));
assert(equal(vec_result.y, 4.0));
assert(equal(vec_result.z, 5.0));
assert(equal(vec_result.w, 0.0));
std::cout << "[PASS 4.1] Matrix translation works." << std::endl;
}
void test_matrix_scaling() {
Matrix transform = Matrix::scaling(2, 3, 4);
RayPoint p(-4, 6, 8);
RayPoint point_result = transform.transform_point(p);
assert(equal(point_result.x, -8.0));
assert(equal(point_result.y, 18.0));
assert(equal(point_result.z, 32.0));
assert(equal(point_result.w, 1.0));
RayVector v(-4, 6, 8);
RayVector vec_result = transform.transform_vector(v);
assert(equal(vec_result.x, -8.0));
assert(equal(vec_result.y, 18.0));
assert(equal(vec_result.z, 32.0));
assert(equal(vec_result.w, 0.0));
Matrix inv = transform.inverse();
RayVector inv_result = inv.transform_vector(v);
assert(equal(inv_result.x, -2.0));
assert(equal(inv_result.y, 2.0));
assert(equal(inv_result.z, 2.0));
assert(equal(inv_result.w, 0.0));
Matrix reflect = Matrix::scaling(-1, 1, 1);
RayPoint p2(2, 3, 4);
RayPoint reflect_result = reflect.transform_point(p2);
assert(equal(reflect_result.x, -2.0));
assert(equal(reflect_result.y, 3.0));
assert(equal(reflect_result.z, 4.0));
assert(equal(reflect_result.w, 1.0));
std::cout << "[PASS 4.2] Matrix scaling and reflection works." << std::endl;
}
void test_matrix_rotation_x() {
Matrix half_quater = Matrix::rotation_x(PI / 4);
Matrix full_quarter = Matrix::rotation_x(PI / 2);
RayPoint p(0, 1, 0);
RayPoint point_result = half_quater.transform_point(p);
RayPoint point_result_2 = full_quarter.transform_point(p);
assert(equal(point_result.x, 0));
assert(equal(point_result.y, std::sqrt(2) / 2));
assert(equal(point_result.z, std::sqrt(2) / 2));
assert(equal(point_result_2.x, 0));
assert(equal(point_result_2.y, 0));
assert(equal(point_result_2.z, 1));
std::cout << "[PASS 4.3] Matrix rotation_x works." << std::endl;
}
void test_matrix_rotation_x_inverse() {
Matrix half_quarter = Matrix::rotation_x(PI / 4.0);
Matrix inv = half_quarter.inverse();
RayPoint p(0, 1, 0);
RayPoint point_result = inv.transform_point(p);
assert(equal(point_result.x, 0.0));
assert(equal(point_result.y, std::sqrt(2.0) / 2.0));
assert(equal(point_result.z, -std::sqrt(2.0) / 2.0));
assert(equal(point_result.w, 1.0));
std::cout << "[PASS 4.4] Matrix rotation_x inverse works." << std::endl;
}
void test_matrix_rotation_y() {
Matrix half_quarter = Matrix::rotation_y(PI / 4.0);
Matrix full_quarter = Matrix::rotation_y(PI / 2.0);
RayPoint p(0, 0, 1);
RayPoint point_result = half_quarter.transform_point(p);
RayPoint point_result_2 = full_quarter.transform_point(p);
assert(equal(point_result.x, std::sqrt(2.0) / 2.0));
assert(equal(point_result.y, 0.0));
assert(equal(point_result.z, std::sqrt(2.0) / 2.0));
assert(equal(point_result.w, 1.0));
assert(equal(point_result_2.x, 1.0));
assert(equal(point_result_2.y, 0.0));
assert(equal(point_result_2.z, 0.0));
assert(equal(point_result_2.w, 1.0));
std::cout << "[PASS 4.5] Matrix rotation_y works." << std::endl;
}
void test_matrix_rotation_z() {
Matrix half_quarter = Matrix::rotation_z(PI / 4.0);
Matrix full_quarter = Matrix::rotation_z(PI / 2.0);
RayPoint p(0, 1, 0);
RayPoint point_result = half_quarter.transform_point(p);
RayPoint point_result_2 = full_quarter.transform_point(p);
assert(equal(point_result.x, -std::sqrt(2.0) / 2.0));
assert(equal(point_result.y, std::sqrt(2.0) / 2.0));
assert(equal(point_result.z, 0.0));
assert(equal(point_result.w, 1.0));
assert(equal(point_result_2.x, -1.0));
assert(equal(point_result_2.y, 0.0));
assert(equal(point_result_2.z, 0.0));
assert(equal(point_result_2.w, 1.0));
std::cout << "[PASS 4.6] Matrix rotation_z works." << std::endl;
}
void test_matrix_shearing() {
RayPoint p(2, 3, 4);
Matrix s_xy = Matrix::shearing(1.0, 0.0, 0.0, 0.0, 0.0, 0.0);
RayPoint r_xy = s_xy.transform_point(p);
assert(equal(r_xy.x, 5.0));
assert(equal(r_xy.y, 3.0));
assert(equal(r_xy.z, 4.0));
Matrix s_xz = Matrix::shearing(0.0, 1.0, 0.0, 0.0, 0.0, 0.0);
RayPoint r_xz = s_xz.transform_point(p);
assert(equal(r_xz.x, 6.0));
assert(equal(r_xz.y, 3.0));
assert(equal(r_xz.z, 4.0));
Matrix s_yx = Matrix::shearing(0.0, 0.0, 1.0, 0.0, 0.0, 0.0);
RayPoint r_yx = s_yx.transform_point(p);
assert(equal(r_yx.x, 2.0));
assert(equal(r_yx.y, 5.0));
assert(equal(r_yx.z, 4.0));
Matrix s_yz = Matrix::shearing(0.0, 0.0, 0.0, 1.0, 0.0, 0.0);
RayPoint r_yz = s_yz.transform_point(p);
assert(equal(r_yz.x, 2.0));
assert(equal(r_yz.y, 7.0));
assert(equal(r_yz.z, 4.0));
Matrix s_zx = Matrix::shearing(0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
RayPoint r_zx = s_zx.transform_point(p);
assert(equal(r_zx.x, 2.0));
assert(equal(r_zx.y, 3.0));
assert(equal(r_zx.z, 6.0));
Matrix s_zy = Matrix::shearing(0.0, 0.0, 0.0, 0.0, 0.0, 1.0);
RayPoint r_zy = s_zy.transform_point(p);
assert(equal(r_zy.x, 2.0));
assert(equal(r_zy.y, 3.0));
assert(equal(r_zy.z, 7.0));
std::cout << "[PASS 4.7] Matrix shearing transformations works." << std::endl;
}
void test_individual_and_chained_transformations() {
RayPoint p(1.0, 0.0, 1.0);
Matrix A = Matrix::rotation_x(PI / 2.0);
Matrix B = Matrix::scaling(5.0, 5.0, 5.0);
Matrix C = Matrix::translation(10.0, 5.0, 7.0);
RayPoint p2 = A.transform_point(p);
assert(equal(p2.x, 1.0));
assert(equal(p2.y, -1.0));
assert(equal(p2.z, 0.0));
RayPoint p3 = B.transform_point(p2);
assert(equal(p3.x, 5.0));
assert(equal(p3.y, -5.0));
assert(equal(p3.z, 0.0));
RayPoint p4 = C.transform_point(p3);
assert(equal(p4.x, 15.0));
assert(equal(p4.y, 0.0));
assert(equal(p4.z, 7.0));
Matrix T = C.matrix_multiply(B.matrix_multiply(A));
RayPoint chained_result = T.transform_point(p);
assert(equal(chained_result.x, 15.0));
assert(equal(chained_result.y, 0.0));
assert(equal(chained_result.z, 7.0));
assert(equal(chained_result.w, 1.0));
std::cout << "[PASS 4.8] Individual and chained transformations works."
<< std::endl;
}
void test_create_and_query_ray() {
RayPoint origin(0, 0, 0);
RayVector direction(4, 5, 6);
Ray r(origin, direction);
assert(r.origin == origin);
assert(r.direction == direction);
std::cout << "[PASS 5.1] Ray create and query works." << std::endl;
}
void test_ray_point_from_dist() {
Ray r(RayPoint(2, 3, 4), RayVector(1, 0, 0));
assert(r.position(0) == RayPoint(2, 3, 4));
assert(r.position(1) == RayPoint(3, 3, 4));
assert(r.position(-1) == RayPoint(1, 3, 4));
assert(r.position(2.5) == RayPoint(4.5, 3, 4));
std::cout << "[PASS 5.2] Ray point fron dist works." << std::endl;
}
void test_sphere_ray_intersect_at_two_points() {
Ray r(RayPoint(0, 0, -5), RayVector(0, 0, 1));
Sphere s(1);
std::vector<Intersection> xs = s.intersects(r);
assert(xs.size() == 2);
assert(equal(xs[0].t, 4.0));
assert(equal(xs[1].t, 6.0));
std::cout << "[PASS 5.3] Ray intersects sphere at two points works."
<< std::endl;