objfile_test.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * DoRayMe - a quick and dirty Raytracer
  3. * OBJ File unit tests
  4. *
  5. * Created by Manoël Trapier
  6. * Copyright (c) 2020 986-Studio.
  7. *
  8. */
  9. #include <objfile.h>
  10. #include <math.h>
  11. #include <gtest/gtest.h>
  12. #include <triangle.h>
  13. #include <smoothtriangle.h>
  14. TEST(OBJFileTest, Ignoring_unrecognised_lines)
  15. {
  16. const char gibberish[] = "There was a young lady named Bright\n"
  17. "who traveled much faster than light\n"
  18. "She set out one day\n"
  19. "in a relative way\n"
  20. "and came back the previous night.\n";
  21. OBJFile parser = OBJFile();
  22. parser.parseOBJFile(gibberish);
  23. ASSERT_EQ(parser.ignoredLines, 5);
  24. }
  25. TEST(OBJFileTest, Vertex_record)
  26. {
  27. const char file[] = "v -1 1 0\n"
  28. "v -1.0000 0.5000 0.0000\n"
  29. "v 1 0 0\n"
  30. "v 1 1 0\n";
  31. OBJFile parser = OBJFile();
  32. parser.parseOBJFile(file);
  33. ASSERT_EQ(parser.vertices(1), Point(-1, 1, 0));
  34. ASSERT_EQ(parser.vertices(2), Point(-1, 0.5, 0));
  35. ASSERT_EQ(parser.vertices(3), Point(1, 0, 0));
  36. ASSERT_EQ(parser.vertices(4), Point(1, 1, 0));
  37. }
  38. TEST(OBJFileTest, Parsing_triangle_faces)
  39. {
  40. const char file[] = "v -1 1 0\n"
  41. "v -1 0 0\n"
  42. "v 1 0 0\n"
  43. "v 1 1 0\n"
  44. "\n"
  45. "f 1 2 3\n"
  46. "f 1 3 4\n";
  47. OBJFile parser = OBJFile();
  48. parser.parseOBJFile(file);
  49. Group *g0 = parser.groups(0);
  50. Triangle *t1 = (Triangle *)(*g0)[0];
  51. Triangle *t2 = (Triangle *)(*g0)[1];
  52. ASSERT_EQ(t1->p1, parser.vertices(1));
  53. ASSERT_EQ(t1->p2, parser.vertices(2));
  54. ASSERT_EQ(t1->p3, parser.vertices(3));
  55. ASSERT_EQ(t2->p1, parser.vertices(1));
  56. ASSERT_EQ(t2->p2, parser.vertices(3));
  57. ASSERT_EQ(t2->p3, parser.vertices(4));
  58. }
  59. TEST(OBJFileTest, Triangulating_polygons)
  60. {
  61. const char file[] = "v -1 1 0\n"
  62. "v -1 0 0\n"
  63. "v 1 0 0\n"
  64. "v 1 1 0\n"
  65. "v 0 2 0\n"
  66. "\n"
  67. "f 1 2 3 4 5\n";
  68. OBJFile parser = OBJFile();
  69. parser.parseOBJFile(file);
  70. Group *g0 = parser.groups(0);
  71. Triangle *t1 = (Triangle *)(*g0)[0];
  72. Triangle *t2 = (Triangle *)(*g0)[1];
  73. Triangle *t3 = (Triangle *)(*g0)[2];
  74. ASSERT_EQ(t1->p1, parser.vertices(1));
  75. ASSERT_EQ(t1->p2, parser.vertices(2));
  76. ASSERT_EQ(t1->p3, parser.vertices(3));
  77. ASSERT_EQ(t2->p1, parser.vertices(1));
  78. ASSERT_EQ(t2->p2, parser.vertices(3));
  79. ASSERT_EQ(t2->p3, parser.vertices(4));
  80. ASSERT_EQ(t3->p1, parser.vertices(1));
  81. ASSERT_EQ(t3->p2, parser.vertices(4));
  82. ASSERT_EQ(t3->p3, parser.vertices(5));
  83. }
  84. TEST(OBJFileTest, Triangle_in_groups)
  85. {
  86. OBJFile parser = OBJFile("triangles.obj");
  87. /* TODO: Add group name search/test */
  88. Group *g1 = parser.groups(1);
  89. Group *g2 = parser.groups(2);
  90. Triangle *t1 = (Triangle *)(*g1)[0];
  91. Triangle *t2 = (Triangle *)(*g2)[0];
  92. ASSERT_EQ(t1->p1, parser.vertices(1));
  93. ASSERT_EQ(t1->p2, parser.vertices(2));
  94. ASSERT_EQ(t1->p3, parser.vertices(3));
  95. ASSERT_EQ(t2->p1, parser.vertices(1));
  96. ASSERT_EQ(t2->p2, parser.vertices(3));
  97. ASSERT_EQ(t2->p3, parser.vertices(4));
  98. }
  99. TEST(OBJFileTest, Vertex_normal_record)
  100. {
  101. const char file[] = "vn 0 0 1\n"
  102. "vn 0.707 0 -0.707\n"
  103. "vn 1 2 3\n";
  104. OBJFile parser = OBJFile();
  105. parser.parseOBJFile(file);
  106. ASSERT_EQ(parser.verticesNormal(1), Vector(0, 0, 1));
  107. ASSERT_EQ(parser.verticesNormal(2), Vector(0.707, 0, -0.707));
  108. ASSERT_EQ(parser.verticesNormal(3), Vector(1, 2, 3));
  109. }
  110. TEST(OBJFileTest, Faces_with_normal)
  111. {
  112. const char file[] = "v 0 1 0\n"
  113. "v -1 0 0\n"
  114. "v 1 0 0\n"
  115. "\n"
  116. "vn -1 0 0\n"
  117. "vn 1 0 0\n"
  118. "vn 0 1 0\n"
  119. "\n"
  120. "f 1//3 2//1 3//2\n"
  121. "f 1/0/3 2/102/1 3/14/2\n";
  122. OBJFile parser = OBJFile();
  123. parser.parseOBJFile(file);
  124. Group *g0 = parser.groups(0);
  125. SmoothTriangle *t1 = (SmoothTriangle *)(*g0)[0];
  126. SmoothTriangle *t2 = (SmoothTriangle *)(*g0)[1];
  127. ASSERT_EQ(t1->p1, parser.vertices(1));
  128. ASSERT_EQ(t1->p2, parser.vertices(2));
  129. ASSERT_EQ(t1->p3, parser.vertices(3));
  130. ASSERT_EQ(t1->n1, parser.verticesNormal(3));
  131. ASSERT_EQ(t1->n2, parser.verticesNormal(1));
  132. ASSERT_EQ(t1->n3, parser.verticesNormal(2));
  133. ASSERT_EQ(t2->p1, parser.vertices(1));
  134. ASSERT_EQ(t2->p2, parser.vertices(2));
  135. ASSERT_EQ(t2->p3, parser.vertices(3));
  136. ASSERT_EQ(t2->n1, parser.verticesNormal(3));
  137. ASSERT_EQ(t2->n2, parser.verticesNormal(1));
  138. ASSERT_EQ(t2->n3, parser.verticesNormal(2));
  139. }