Debian Codegraph @

unittests/unit/math/test_TriMesh.cpp

raw
1 /*
2 * Copyright (c) 2011-2022, The DART development contributors
3 * All rights reserved.
4 *
5 * The list of contributors can be found at:
6 * https://github.com/dartsim/dart/blob/master/LICENSE
7 *
8 * This file is provided under the following "BSD-style" License:
9 * Redistribution and use in source and binary forms, with or
10 * without modification, are permitted provided that the following
11 * conditions are met:
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
27 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <gtest/gtest.h>
34
35 #include "dart/math/TriMesh.hpp"
36
37 #include "TestHelpers.hpp"
38
39 using namespace dart;
40 using namespace math;
41
42 //==============================================================================
43 TEST(TriMeshTests, DefaultConstructor)
44 {
45 auto mesh = TriMeshd();
46 EXPECT_FALSE(mesh.hasTriangles());
47 EXPECT_FALSE(mesh.hasTriangleNormals());
48 EXPECT_TRUE(mesh.isEmpty());
49 }
50
51 //==============================================================================
52 TEST(TriMeshTests, SetTriangles)
53 {
54 auto mesh = TriMeshd();
55 EXPECT_TRUE(mesh.isEmpty());
56
57 auto vertices = TriMeshd::Vertices();
58 vertices.emplace_back(0, 0, 0);
59 vertices.emplace_back(1, 0, 0);
60 vertices.emplace_back(0, 1, 0);
61 auto triangles = TriMeshd::Triangles();
62 triangles.emplace_back(0, 1, 2);
63
64 mesh.setTriangles(vertices, triangles);
65 EXPECT_TRUE(mesh.hasTriangles());
66 EXPECT_FALSE(mesh.hasTriangleNormals());
67 EXPECT_FALSE(mesh.isEmpty());
68 EXPECT_EQ(mesh.getVertices(), vertices);
69 EXPECT_EQ(mesh.getTriangles(), triangles);
70
71 mesh.computeVertexNormals();
72 EXPECT_TRUE(mesh.hasTriangles());
73 EXPECT_TRUE(mesh.hasTriangleNormals());
74 EXPECT_FALSE(mesh.isEmpty());
75
76 mesh.clear();
77 EXPECT_FALSE(mesh.hasTriangles());
78 EXPECT_FALSE(mesh.hasTriangleNormals());
79 EXPECT_TRUE(mesh.isEmpty());
80 }
81
82 //==============================================================================
83 TEST(TriMeshTests, Operators)
84 {
85 auto mesh1 = TriMeshd();
86 auto mesh2 = TriMeshd();
87
88 auto vertices = TriMeshd::Vertices();
89 vertices.emplace_back(0, 0, 0);
90 vertices.emplace_back(1, 0, 0);
91 vertices.emplace_back(0, 1, 0);
92 auto triangles = TriMeshd::Triangles();
93 triangles.emplace_back(0, 1, 2);
94
95 mesh1.setTriangles(vertices, triangles);
96 EXPECT_EQ(mesh1.getVertices().size(), 3);
97 EXPECT_EQ(mesh1.getTriangles().size(), 1);
98 mesh2.setTriangles(vertices, triangles);
99 EXPECT_EQ(mesh2.getVertices().size(), 3);
100 EXPECT_EQ(mesh2.getTriangles().size(), 1);
101
102 auto mesh3 = mesh1 + mesh2;
103 EXPECT_EQ(mesh3.getVertices().size(), 6);
104 EXPECT_EQ(mesh3.getTriangles().size(), 2);
105 EXPECT_FALSE(mesh3.hasTriangleNormals());
106 EXPECT_FALSE(mesh3.hasVertexNormals());
107
108 mesh1.computeVertexNormals();
109 EXPECT_TRUE(mesh1.hasTriangleNormals());
110 EXPECT_TRUE(mesh1.hasVertexNormals());
111 EXPECT_FALSE(mesh2.hasTriangleNormals());
112 EXPECT_FALSE(mesh2.hasVertexNormals());
113 EXPECT_FALSE((mesh1 + mesh2).hasTriangleNormals());
114 EXPECT_FALSE((mesh1 + mesh2).hasVertexNormals());
115
116 mesh2.computeVertexNormals();
117 EXPECT_TRUE(mesh1.hasTriangleNormals());
118 EXPECT_TRUE(mesh1.hasVertexNormals());
119 EXPECT_TRUE(mesh2.hasTriangleNormals());
120 EXPECT_TRUE(mesh2.hasVertexNormals());
121 EXPECT_TRUE((mesh1 + mesh2).hasTriangleNormals());
122 EXPECT_TRUE((mesh1 + mesh2).hasVertexNormals());
123
124 mesh1 += mesh2;
125 EXPECT_EQ(mesh1.getVertices().size(), 6);
126 EXPECT_EQ(mesh1.getTriangles().size(), 2);
127 }
128
129 //==============================================================================
130 TEST(TriMeshTests, GenerateConvexHull)
131 {
132 auto mesh = TriMeshd();
133 EXPECT_TRUE(mesh.isEmpty());
134
135 auto emptyConvexHull = mesh.generateConvexHull();
136 ASSERT_NE(emptyConvexHull, nullptr);
137 EXPECT_TRUE(emptyConvexHull->isEmpty());
138
139 auto vertices = TriMeshd::Vertices();
140 vertices.emplace_back(0, 0, 0);
141 vertices.emplace_back(1, 0, 0);
142 vertices.emplace_back(0, 1, 0);
143 vertices.emplace_back(0, 0, 1);
144 mesh.setTriangles(vertices, {});
145
146 auto convexHull = mesh.generateConvexHull();
147 ASSERT_NE(convexHull, nullptr);
148 EXPECT_EQ(convexHull->getVertices().size(), vertices.size());
149 EXPECT_EQ(convexHull->getTriangles().size(), 4);
150 }