objfile_test.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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(OBJ_DEFAULT_GROUP);
  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(OBJ_DEFAULT_GROUP);
  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. Group *g1 = parser.groups("FirstGroup");
  88. Group *g2 = parser.groups("SecondGroup");
  89. /* The groups must exists */
  90. ASSERT_NE(g1, nullptr);
  91. ASSERT_NE(g2, nullptr);
  92. Triangle *t1 = (Triangle *)(*g1)[0];
  93. Triangle *t2 = (Triangle *)(*g2)[0];
  94. ASSERT_EQ(t1->p1, parser.vertices(1));
  95. ASSERT_EQ(t1->p2, parser.vertices(2));
  96. ASSERT_EQ(t1->p3, parser.vertices(3));
  97. ASSERT_EQ(t2->p1, parser.vertices(1));
  98. ASSERT_EQ(t2->p2, parser.vertices(3));
  99. ASSERT_EQ(t2->p3, parser.vertices(4));
  100. }
  101. TEST(OBJFileTest, Vertex_normal_record)
  102. {
  103. const char file[] = "vn 0 0 1\n"
  104. "vn 0.707 0 -0.707\n"
  105. "vn 1 2 3\n";
  106. OBJFile parser = OBJFile();
  107. parser.parseOBJFile(file);
  108. ASSERT_EQ(parser.verticesNormal(1), Vector(0, 0, 1));
  109. ASSERT_EQ(parser.verticesNormal(2), Vector(0.707, 0, -0.707));
  110. ASSERT_EQ(parser.verticesNormal(3), Vector(1, 2, 3));
  111. }
  112. TEST(OBJFileTest, Faces_with_normal)
  113. {
  114. const char file[] = "v 0 1 0\n"
  115. "v -1 0 0\n"
  116. "v 1 0 0\n"
  117. "\n"
  118. "vn -1 0 0\n"
  119. "vn 1 0 0\n"
  120. "vn 0 1 0\n"
  121. "\n"
  122. "f 1//3 2//1 3//2\n"
  123. "f 1/0/3 2/102/1 3/14/2\n";
  124. OBJFile parser = OBJFile();
  125. parser.parseOBJFile(file);
  126. Group *g0 = parser.groups(OBJ_DEFAULT_GROUP);
  127. SmoothTriangle *t1 = (SmoothTriangle *)g0->getObject(0);
  128. SmoothTriangle *t2 = (SmoothTriangle *)g0->getObject(1);
  129. ASSERT_EQ(t1->p1, parser.vertices(1));
  130. ASSERT_EQ(t1->p2, parser.vertices(2));
  131. ASSERT_EQ(t1->p3, parser.vertices(3));
  132. ASSERT_EQ(t1->n1, parser.verticesNormal(3));
  133. ASSERT_EQ(t1->n2, parser.verticesNormal(1));
  134. ASSERT_EQ(t1->n3, parser.verticesNormal(2));
  135. ASSERT_EQ(t2->p1, parser.vertices(1));
  136. ASSERT_EQ(t2->p2, parser.vertices(2));
  137. ASSERT_EQ(t2->p3, parser.vertices(3));
  138. ASSERT_EQ(t2->n1, parser.verticesNormal(3));
  139. ASSERT_EQ(t2->n2, parser.verticesNormal(1));
  140. ASSERT_EQ(t2->n3, parser.verticesNormal(2));
  141. }