-
Notifications
You must be signed in to change notification settings - Fork 1
/
triangle_mesh.cpp
68 lines (56 loc) · 2.05 KB
/
triangle_mesh.cpp
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
#include <tess/triangle_mesh.h>
namespace tess
{
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
// vertex
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
vertex::vertex()
: vertex(vec3::ZERO, vec3::ZERO)
{
}
vertex::vertex(const vec3& _position, const vec3& _normal)
: position(_position), normal(_normal)
{
}
bool vertex::operator==(const vertex& other) const
{
return position == other.position && normal == other.normal;
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
// triangle_mesh
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
bool triangle_mesh::is_valid() const
{
// Check if there are at least 3 vertices and 3 elements to define a triangle
if(vertices.size() < 3 || elements.size() < 3)
{
return false;
}
// Check if mesh has at least one valid triangle
for(unsigned int i = 0; i < elements.size() - 3; i += 3)
{
if(elements[i] != elements[i+1] && elements[i+1] != elements[i+2])
{
if(elements[i] >= vertices.size() || elements[i+1] >= vertices.size() || elements[i+2] >= vertices.size())
{
continue;
}
const auto& v1 = vertices[elements[i]].position;
const auto& v2 = vertices[elements[i+1]].position;
const auto& v3 = vertices[elements[i+2]].position;
if(!math::is_valid(v1.x) || !math::is_valid(v1.y) || !math::is_valid(v1.z) ||
!math::is_valid(v2.x) || !math::is_valid(v2.y) || !math::is_valid(v2.z) ||
!math::is_valid(v3.x) || !math::is_valid(v3.y) || !math::is_valid(v3.z))
{
continue;
}
if(v1 == v2 || v2 == v3 || v1 == v3)
{
continue;
}
return true;
}
}
return false;
}
} // namespace tess