summaryrefslogtreecommitdiff
path: root/thirdparty/xatlas/avoid-failing-on-bad-geometry.patch
blob: a28cd9f82b32f012019e5c1a6f81e65ac048fa15 (plain)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
diff --git a/thirdparty/xatlas/xatlas.cpp b/thirdparty/xatlas/xatlas.cpp
index df5ef94db..eb0824a51 100644
--- a/thirdparty/xatlas/xatlas.cpp
+++ b/thirdparty/xatlas/xatlas.cpp
@@ -1276,6 +1276,9 @@ class Vertex
 {
 public:
 	uint32_t id;
+	// -- GODOT start --
+	uint32_t original_id;
+	// -- GODOT end --
 	Edge *edge;
 	Vertex *next;
 	Vertex *prev;
@@ -1283,7 +1286,10 @@ public:
 	Vector3 nor;
 	Vector2 tex;
 
-	Vertex(uint32_t id) : id(id), edge(NULL), pos(0.0f), nor(0.0f), tex(0.0f)
+	// -- GODOT start --
+	//Vertex(uint32_t id) : id(id), edge(NULL), pos(0.0f), nor(0.0f), tex(0.0f)
+	Vertex(uint32_t id) : id(id), original_id(id), edge(NULL), pos(0.0f), nor(0.0f), tex(0.0f)
+	// -- GODOT end --
 	{
 		next = this;
 		prev = this;
@@ -1934,6 +1940,64 @@ public:
 		return f;
 	}
 
+	// -- GODOT start --
+	Face *addUniqueFace(uint32_t v0, uint32_t v1, uint32_t v2) {
+
+		int base_vertex = m_vertexArray.size();
+
+		uint32_t ids[3] = { v0, v1, v2 };
+
+		Vector3 base[3] = {
+			m_vertexArray[v0]->pos,
+			m_vertexArray[v1]->pos,
+			m_vertexArray[v2]->pos,
+		};
+
+		//make sure its not a degenerate
+		bool degenerate = distanceSquared(base[0], base[1]) < NV_EPSILON || distanceSquared(base[0], base[2]) < NV_EPSILON || distanceSquared(base[1], base[2]) < NV_EPSILON;
+		xaDebugAssert(!degenerate);
+
+		float min_x = 0;
+
+		for (int i = 0; i < 3; i++) {
+			if (i == 0 || m_vertexArray[v0]->pos.x < min_x) {
+				min_x = m_vertexArray[v0]->pos.x;
+			}
+		}
+
+		float max_x = 0;
+
+		for (int j = 0; j < m_vertexArray.size(); j++) {
+			if (j == 0 || m_vertexArray[j]->pos.x > max_x) { //vertex already exists
+				max_x = m_vertexArray[j]->pos.x;
+			}
+		}
+
+		//separate from everything else, in x axis
+		for (int i = 0; i < 3; i++) {
+
+			base[i].x -= min_x;
+			base[i].x += max_x + 10.0;
+		}
+
+		for (int i = 0; i < 3; i++) {
+			Vertex *v = new Vertex(m_vertexArray.size());
+			v->pos = base[i];
+			v->nor = m_vertexArray[ids[i]]->nor,
+			v->tex = m_vertexArray[ids[i]]->tex,
+
+			v->original_id = ids[i];
+			m_vertexArray.push_back(v);
+		}
+
+		uint32_t indexArray[3];
+		indexArray[0] = base_vertex + 0;
+		indexArray[1] = base_vertex + 1;
+		indexArray[2] = base_vertex + 2;
+		return addFace(indexArray, 3, 0, 3);
+	}
+	// -- GODOT end --
+
 	// These functions disconnect the given element from the mesh and delete it.
 
 	// @@ We must always disconnect edge pairs simultaneously.
@@ -2915,6 +2979,14 @@ Mesh *triangulate(const Mesh *inputMesh)
 					Vector2 p0 = polygonPoints[i0];
 					Vector2 p1 = polygonPoints[i1];
 					Vector2 p2 = polygonPoints[i2];
+
+					// -- GODOT start --
+					bool degenerate = distance(p0, p1) < NV_EPSILON || distance(p0, p2) < NV_EPSILON || distance(p1, p2) < NV_EPSILON;
+					if (degenerate) {
+						continue;
+					}
+					// -- GODOT end --
+
 					float d = clamp(dot(p0 - p1, p2 - p1) / (length(p0 - p1) * length(p2 - p1)), -1.0f, 1.0f);
 					float angle = acosf(d);
 					float area = triangleArea(p0, p1, p2);
@@ -2938,6 +3010,11 @@ Mesh *triangulate(const Mesh *inputMesh)
 						}
 					}
 				}
+				// -- GODOT start --
+				if (!bestIsValid)
+					break;
+				// -- GODOT end --
+
 				xaDebugAssert(minAngle <= 2 * PI);
 				// Clip best ear:
 				uint32_t i0 = (bestEar + size - 1) % size;
@@ -5606,7 +5683,10 @@ public:
 				}
 				if (chartMeshIndices[vertex->id] == ~0) {
 					chartMeshIndices[vertex->id] = m_chartMesh->vertexCount();
-					m_chartToOriginalMap.push_back(vertex->id);
+					// -- GODOT start --
+					//m_chartToOriginalMap.push_back(vertex->id);
+					m_chartToOriginalMap.push_back(vertex->original_id);
+					// -- GODOT end --
 					m_chartToUnifiedMap.push_back(unifiedMeshIndices[unifiedVertex->id]);
 					halfedge::Vertex *v = m_chartMesh->addVertex(vertex->pos);
 					v->nor = vertex->nor;
@@ -5699,7 +5779,10 @@ public:
 				const halfedge::Vertex *vertex = it.current()->vertex;
 				if (chartMeshIndices[vertex->id] == ~0) {
 					chartMeshIndices[vertex->id] = m_chartMesh->vertexCount();
-					m_chartToOriginalMap.push_back(vertex->id);
+					// -- GODOT start --
+					//m_chartToOriginalMap.push_back(vertex->id);
+					m_chartToOriginalMap.push_back(vertex->original_id);
+					// -- GODOT end --
 					halfedge::Vertex *v = m_chartMesh->addVertex(vertex->pos);
 					v->nor = vertex->nor;
 					v->tex = vertex->tex; // @@ Not necessary.
@@ -7573,6 +7656,14 @@ AddMeshError AddMesh(Atlas *atlas, const InputMesh &mesh, bool useColocalVertice
 			}
 		}
 		internal::halfedge::Face *face = heMesh->addFace(tri[0], tri[1], tri[2]);
+
+		// -- GODOT start --
+		if (!face && heMesh->errorCode == internal::halfedge::Mesh::ErrorCode::AlreadyAddedEdge) {
+			//there is still hope for this, no reason to not add, at least add as separate
+			face = heMesh->addUniqueFace(tri[0], tri[1], tri[2]);
+		}
+		// -- GODOT end --
+
 		if (!face) {
 			if (heMesh->errorCode == internal::halfedge::Mesh::ErrorCode::AlreadyAddedEdge)
 				error.code = AddMeshErrorCode::AlreadyAddedEdge;