objfile_test.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. TEST(OBJFileTest, Ignoring_unrecognised_lines)
  14. {
  15. const char gibberish[] = "There was a young lady named Bright\n"
  16. "who traveled much faster than light\n"
  17. "She set out one day\n"
  18. "in a relative way\n"
  19. "and came back the previous night.\n";
  20. OBJFile parser = OBJFile();
  21. parser.parseOBJFile(gibberish);
  22. ASSERT_EQ(parser.ignoredLines, 5);
  23. }
  24. TEST(OBJFileTest, Vertex_record)
  25. {
  26. const char file[] = "v -1 1 0\n"
  27. "v -1.0000 0.5000 0.0000\n"
  28. "v 1 0 0\n"
  29. "v 1 1 0\n";
  30. OBJFile parser = OBJFile();
  31. parser.parseOBJFile(file);
  32. ASSERT_EQ(parser.vertices(1), Point(-1, 1, 0));
  33. ASSERT_EQ(parser.vertices(2), Point(-1, 0.5, 0));
  34. ASSERT_EQ(parser.vertices(3), Point(1, 0, 0));
  35. ASSERT_EQ(parser.vertices(4), Point(1, 1, 0));
  36. }
  37. TEST(OBJFileTest, Parsing_triangle_faces)
  38. {
  39. const char file[] = "v -1 1 0\n"
  40. "v -1 0 0\n"
  41. "v 1 0 0\n"
  42. "v 1 1 0\n"
  43. "\n"
  44. "f 1 2 3\n"
  45. "f 1 3 4\n";
  46. OBJFile parser = OBJFile();
  47. parser.parseOBJFile(file);
  48. Group *g0 = parser.groups(0);
  49. Triangle *t1 = (Triangle *)(*g0)[0];
  50. Triangle *t2 = (Triangle *)(*g0)[1];
  51. ASSERT_EQ(t1->p1, parser.vertices(1));
  52. ASSERT_EQ(t1->p2, parser.vertices(2));
  53. ASSERT_EQ(t1->p3, parser.vertices(3));
  54. ASSERT_EQ(t2->p1, parser.vertices(1));
  55. ASSERT_EQ(t2->p2, parser.vertices(3));
  56. ASSERT_EQ(t2->p3, parser.vertices(4));
  57. }
  58. TEST(OBJFileTest, Triangulating_polygons)
  59. {
  60. const char file[] = "v -1 1 0\n"
  61. "v -1 0 0\n"
  62. "v 1 0 0\n"
  63. "v 1 1 0\n"
  64. "v 0 2 0\n"
  65. "\n"
  66. "f 1 2 3 4 5\n";
  67. OBJFile parser = OBJFile();
  68. parser.parseOBJFile(file);
  69. Group *g0 = parser.groups(0);
  70. Triangle *t1 = (Triangle *)(*g0)[0];
  71. Triangle *t2 = (Triangle *)(*g0)[1];
  72. Triangle *t3 = (Triangle *)(*g0)[2];
  73. ASSERT_EQ(t1->p1, parser.vertices(1));
  74. ASSERT_EQ(t1->p2, parser.vertices(2));
  75. ASSERT_EQ(t1->p3, parser.vertices(3));
  76. ASSERT_EQ(t2->p1, parser.vertices(1));
  77. ASSERT_EQ(t2->p2, parser.vertices(3));
  78. ASSERT_EQ(t2->p3, parser.vertices(4));
  79. ASSERT_EQ(t3->p1, parser.vertices(1));
  80. ASSERT_EQ(t3->p2, parser.vertices(4));
  81. ASSERT_EQ(t3->p3, parser.vertices(5));
  82. }
  83. TEST(OBJFileTest, Triangle_in_groups)
  84. {
  85. OBJFile parser = OBJFile("triangles.obj");
  86. /* TODO: Add group name search/test */
  87. Group *g1 = parser.groups(1);
  88. Group *g2 = parser.groups(2);
  89. Triangle *t1 = (Triangle *)(*g1)[0];
  90. Triangle *t2 = (Triangle *)(*g2)[0];
  91. ASSERT_EQ(t1->p1, parser.vertices(1));
  92. ASSERT_EQ(t1->p2, parser.vertices(2));
  93. ASSERT_EQ(t1->p3, parser.vertices(3));
  94. ASSERT_EQ(t2->p1, parser.vertices(1));
  95. ASSERT_EQ(t2->p2, parser.vertices(3));
  96. ASSERT_EQ(t2->p3, parser.vertices(4));
  97. }