-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathCutTool.Geometry.cs
More file actions
459 lines (391 loc) · 19.4 KB
/
Copy pathCutTool.Geometry.cs
File metadata and controls
459 lines (391 loc) · 19.4 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEditor.EditorTools;
using UnityEngine;
using UnityEngine.ProBuilder;
using UnityEngine.ProBuilder.MeshOperations;
using Vertex = UnityEngine.ProBuilder.Vertex;
using Edge = UnityEngine.ProBuilder.Edge;
using Math = UnityEngine.ProBuilder.Math;
namespace UnityEditor.ProBuilder
{
partial class CutTool
{
/// <summary>
/// Compute the cut result and display a notification
/// </summary>
void ExecuteCut(bool restorePrevious = true)
{
ActionResult result = DoCut();
EditorUtility.ShowNotification(result.notification);
if(restorePrevious)
ExitTool();
}
/// <summary>
/// Compute the faces resulting from the cut:
/// - First inserts points defining the cut as vertices in the face
/// - Compute the central polygon is the cut is creating a closed polygon in the face
/// - Update the rest of the face accordingly to the cut and the central polygon
/// </summary>
/// <returns>ActionResult success if it was possible to create the cut</returns>
internal ActionResult DoCut()
{
if (m_TargetFace == null || m_CutPath.Count < 2)
{
return new ActionResult(ActionResult.Status.Canceled, L10n.Tr("Not enough elements selected for a cut"));
}
if(!m_IsCutValid)
{
return new ActionResult(ActionResult.Status.Failure, L10n.Tr("The current cut overlaps itself"));
}
UndoUtility.RecordObject(m_Mesh, "Execute Cut");
List<Vertex> meshVertices = new List<Vertex>();
m_Mesh.GetVerticesInList(meshVertices);
Vertex[] formerVertices = new Vertex[m_MeshConnections.Count];
for(int i = 0; i < m_MeshConnections.Count; i++)
{
formerVertices[i] = meshVertices[m_MeshConnections[i].item2];
}
//Insert cut vertices in the mesh
List<Vertex> cutVertices = InsertVertices();
m_Mesh.GetVerticesInList(meshVertices);
//Retrieve indexes of the cut points in the mesh vertices
int[] cutIndexes = cutVertices.Select(vert => meshVertices.IndexOf(vert)).ToArray();
//Update mesh connections with new indexes
for(int i = 0; i<m_MeshConnections.Count; i++)
{
SimpleTuple<int, int> connection = m_MeshConnections[i];
connection.item1 = meshVertices.IndexOf(cutVertices[connection.item1]);
connection.item2 = meshVertices.IndexOf(formerVertices[i]);
m_MeshConnections[i] = connection;
}
List<Face> newFaces = new List<Face>();
// If the cut defines a loop in the face, create the polygon corresponding to that loop
if (isALoop)
{
Face f = m_Mesh.CreatePolygon(cutIndexes, false);
if(f == null)
return new ActionResult(ActionResult.Status.Failure, L10n.Tr("Cut Shape is not valid"));
ApplySourceFaceSettings(f, m_TargetFace);
Vector3 nrm = Math.Normal(m_Mesh, f);
Vector3 targetNrm = Math.Normal(m_Mesh, m_TargetFace);
// If the shape is define in the wrong orientation compared to the former face, reverse it
if(Vector3.Dot(nrm,targetNrm) < 0f)
f.Reverse();
newFaces.Add(f);
}
//Compute the rest of the new faces (faces outside of the loop or division of the original face)
List<Face> faces = ComputeNewFaces(m_TargetFace, cutIndexes);
newFaces.AddRange(faces);
//Remove inserted vertices only if they were inserted for the process
List<int> verticesIndexesToDelete = new List<int>();
for(int i = 0; i < m_CutPath.Count; i++)
{
if(( m_CutPath[i].types & VertexTypes.NewVertex ) != 0
&& ( m_CutPath[i].types & VertexTypes.VertexInShape ) == 0)
verticesIndexesToDelete.Add(cutIndexes[i]);
}
m_Mesh.DeleteVertices(verticesIndexesToDelete);
//Delete former face
m_Mesh.DeleteFace(m_TargetFace);
m_Mesh.ToMesh();
m_Mesh.Refresh();
m_Mesh.Optimize();
//Update mesh selection after the cut has been performed
MeshSelection.ClearElementSelection();
m_Mesh.SetSelectedFaces(newFaces);
ProBuilderEditor.Refresh();
ResetToolState(true);
return new ActionResult(ActionResult.Status.Success, L10n.Tr("Cut executed"));
}
/// <summary>
/// Based on the new vertices inserted in the face, this method computes the different faces
/// created between the cut and the original face (external to the cut if it makes a loop)
///
/// The faces are created by parsing the edges that defines the border of the original face. Is an edge ends on a
/// vertex that is part of the cut, or belongs to a connection between the cut and the face,
/// we close the defined polygon using the cut (though ComputeFaceClosure method) and create a face out of this polygon
/// </summary>
/// <param name="face">Original face to modify</param>
/// <param name="cutVertexIndexes">Indexes of the new vertices inserted in the face</param>
/// <returns>The list of polygons to create (defined by their vertices indexes)</returns>
List<Face> ComputeNewFaces(Face face, IList<int> cutVertexIndexes)
{
List<Face> newFaces = new List<Face>();
//Get Vertices from the mesh
Dictionary<int, int> sharedToUnique = m_Mesh.sharedVertexLookup;
var cutVertexSharedIndexes = cutVertexIndexes.Select(ind => sharedToUnique[ind]).ToList();
//Parse peripheral edges to unique id and find a common point between the peripheral edges and the cut
var peripheralEdges = WingedEdge.SortEdgesByAdjacency(face);
var peripheralEdgesUnique = new List<Edge>();
int startIndex = -1;
for (int i = 0; i < peripheralEdges.Count; i++)
{
Edge eShared = peripheralEdges[i];
Edge eUnique = new Edge(sharedToUnique[eShared.a], sharedToUnique[eShared.b]);
peripheralEdgesUnique.Add(eUnique);
if (startIndex == -1 && ( cutVertexSharedIndexes.Contains(eUnique.a)
|| m_MeshConnections.Exists(tup => sharedToUnique[tup.item2] == eUnique.a)))
startIndex = i;
}
//Create a polygon for each cut reaching the mesh edges
List<Face> facesToDelete = new List<Face>();
List<int> polygon = new List<int>();
for (int i = startIndex; i <= peripheralEdgesUnique.Count + startIndex; i++)
{
polygon.Add(peripheralEdges[i % peripheralEdgesUnique.Count].a);
Edge e = peripheralEdgesUnique[i % peripheralEdgesUnique.Count];
if(polygon.Count > 1)
{
int index = -1;
if(cutVertexSharedIndexes.Contains(e.a)) // get next vertex
{
index = e.a;
}
else if(m_MeshConnections.Exists(tup => sharedToUnique[tup.item2] == e.a))
{
SimpleTuple<int, int> connection = m_MeshConnections.Find(tup => sharedToUnique[tup.item2] == e.a);
polygon.Add(connection.item1);
index = sharedToUnique[connection.item1];
}
if(index >= 0)
{
// In the case of only 2 distinct, a face should not be added.
if (polygon.Count != 2)
{
List<Face> toDelete;
Face newFace = ComputeFaceClosure(polygon, index, cutVertexSharedIndexes, out toDelete);
if (newFace != null && newFace.indexesInternal != null)
{
ApplySourceFaceSettings(newFace, m_TargetFace);
newFaces.Add(newFace);
facesToDelete.AddRange(toDelete);
}
}
//Start a new polygon
polygon = new List<int>();
polygon.Add(peripheralEdges[i % peripheralEdgesUnique.Count].a);
}
}
}
polygon.Clear();
m_Mesh.DeleteFaces(facesToDelete);
return newFaces;
}
/// <summary>
/// The method computes all the possible faces that can be made starting by the vertices in polygonStart and ending with the cut
/// This method creates faces that are not the final one and that must be deleted at the end. These invalid faces are returned in facesToDelete
/// The only valid face is returned from this method. From all defined faces, the valid face is the one with the smaller area
/// (otherwise it means it covers another face of the mesh).
/// </summary>
/// <param name="polygonStart">Indexes of the first vertices of the new Face to define, these vertices are coming from the original face only</param>
/// <param name="currentIndex">Current vertex index in the cut</param>
/// <param name="cutIndexes">Indexes of the vertices defining the cut</param>
/// <param name="cutIndexes">out : extra faces created by this method that will need to be deleted after
/// (these faces cannot be deleted directly as it will break the m_MeshConnections by deleting some indexes before the end of the algorithm)
/// <returns>the valid face that need to be kept in the resulting mesh</returns>
Face ComputeFaceClosure( List<int> polygonStart, int currentIndex, List<int> cutIndexes, out List<Face> facesToDelete)
{
List<Vertex> meshVertices = new List<Vertex>();
IList<SharedVertex> uniqueIdToVertexIndex = m_Mesh.sharedVertices;
Dictionary<int, int> sharedToUnique = m_Mesh.sharedVertexLookup;
facesToDelete = new List<Face>();
if (polygonStart == null || polygonStart.Count == 0 || cutIndexes == null || cutIndexes.Count == 0)
return null;
int polygonFirstVertex = polygonStart[0];
int startIndex = cutIndexes.IndexOf(currentIndex);
if (startIndex < 0 || !sharedToUnique.ContainsKey(polygonFirstVertex))
return null;
int polygonFirstSharedIndex = sharedToUnique[polygonFirstVertex];
bool hasConnection = m_MeshConnections.Exists(tup => sharedToUnique.ContainsKey(tup.item2)
&& sharedToUnique[tup.item2] == polygonFirstSharedIndex);
SimpleTuple<int,int> connection = default;
if (hasConnection)
connection = m_MeshConnections.Find(tup => sharedToUnique.ContainsKey(tup.item2)
&& sharedToUnique[tup.item2] == polygonFirstSharedIndex);
List<List<int>> closureCandidates = new List<List<int>>();
//Go through the cut in reverse direction
int index;
int finalIndex = isALoop ?(startIndex - cutIndexes.Count) : 0;
bool connected = false;
List<int> candidate = new List<int>();
for(index = startIndex - 1; index >= finalIndex; index--)
{
int vertexIndex = uniqueIdToVertexIndex[cutIndexes[(index + cutIndexes.Count) % cutIndexes.Count]][0];
candidate.Add(vertexIndex);
if(sharedToUnique[vertexIndex] == polygonFirstSharedIndex ||
(hasConnection && sharedToUnique.ContainsKey(connection.item1)
&& sharedToUnique[vertexIndex] == sharedToUnique[connection.item1]))
{
connected = true;
break;
}
}
//If we find a valid candidate for the connection, add it to the list
if(connected)
closureCandidates.Add(candidate);
//Go through the cut in forward direction
finalIndex = isALoop ? (startIndex + cutIndexes.Count) : cutIndexes.Count;
connected = false;
candidate = new List<int>();
for(index = startIndex + 1; index < finalIndex; index++)
{
int vertexIndex = uniqueIdToVertexIndex[cutIndexes[index % cutIndexes.Count]][0];
candidate.Add(vertexIndex);
if(sharedToUnique[vertexIndex] == polygonFirstSharedIndex ||
(hasConnection && sharedToUnique.ContainsKey(connection.item1)
&& sharedToUnique[vertexIndex] == sharedToUnique[connection.item1]))
{
connected = true;
break;
}
}
//If we find a valid candidate for the connection, add it to the list
if(connected)
closureCandidates.Add(candidate);
//Go through the different candidate and keep the best one.
Face bestFace = null;
float bestArea = 0f;
foreach(var closure in closureCandidates)
{
closure.AddRange(polygonStart);
Face face = m_Mesh.CreatePolygon(closure, false);
meshVertices.Clear();
m_Mesh.GetVerticesInList(meshVertices);
uniqueIdToVertexIndex = m_Mesh.sharedVertices;
sharedToUnique = m_Mesh.sharedVertexLookup;
float area;
if (!TryGetFaceArea(face, meshVertices, uniqueIdToVertexIndex, sharedToUnique, out area))
{
if (face != null)
facesToDelete.Add(face);
continue;
}
if(bestFace != null)
{
if(area < bestArea)
{
facesToDelete.Add(bestFace);
bestArea = area;
bestFace = face;
}
else
facesToDelete.Add(face);
}
else
{
bestFace = face;
bestArea = area;
}
}
return bestFace;
}
void ApplySourceFaceSettings(Face destination, Face source)
{
if (destination == null || source == null)
return;
destination.submeshIndex = source.submeshIndex;
destination.manualUV = source.manualUV;
destination.uv = new AutoUnwrapSettings(source.uv);
destination.textureGroup = source.textureGroup;
destination.smoothingGroup = source.smoothingGroup;
destination.elementGroup = source.elementGroup;
}
bool TryGetFaceArea(Face face, List<Vertex> meshVertices, IList<SharedVertex> uniqueIdToVertexIndex,
Dictionary<int, int> sharedToUnique, out float area)
{
area = 0f;
if (face == null || face.indexesInternal == null)
return false;
Vector3[] vertices = meshVertices.Select(vertex => vertex.position).ToArray();
int[] indexes = new int[face.indexesInternal.Length];
for (int i = 0; i < face.indexesInternal.Length; i++)
{
int uniqueIndex;
if (!sharedToUnique.TryGetValue(face.indexesInternal[i], out uniqueIndex))
return false;
if (uniqueIndex < 0 || uniqueIndex >= uniqueIdToVertexIndex.Count)
return false;
SharedVertex sharedVertex = uniqueIdToVertexIndex[uniqueIndex];
if (sharedVertex == null || sharedVertex.Count < 1)
return false;
indexes[i] = sharedVertex[0];
}
area = Math.PolygonArea(vertices, indexes);
return true;
}
/// <summary>
/// Insert all position from the cut path to the current faces as new vertices
/// </summary>
/// <returns>The list of Vertex inserted in the face</returns>
List<Vertex> InsertVertices()
{
List<Vertex> newVertices = new List<Vertex>();
foreach (var vertexData in m_CutPath)
{
switch (vertexData.types)
{
case VertexTypes.ExistingVertex:
case VertexTypes.VertexInShape:
newVertices.Add(InsertVertexOnExistingVertex(vertexData.position));
break;
case VertexTypes.AddedOnEdge:
newVertices.Add(InsertVertexOnExistingEdge(vertexData.position));
break;
case VertexTypes.NewVertex:
newVertices.Add(m_Mesh.InsertVertexInMesh(vertexData.position,vertexData.normal));
break;
default:
break;
}
}
return newVertices;
}
/// <summary>
/// Method to retrieve a vertex already existing in the face to avoid duplicated
/// </summary>
/// <param name="vertexPosition">The vertex position</param>
/// <returns>The retrieved vertex</returns>
Vertex InsertVertexOnExistingVertex(Vector3 vertexPosition)
{
Vertex vertex = null;
List<Vertex> vertices = m_Mesh.GetVertices().ToList();
for (int vertIndex = 0; vertIndex < vertices.Count; vertIndex++)
{
if (Math.Approx3(vertices[vertIndex].position, vertexPosition)
&& !float.IsNaN(vertices[vertIndex].normal.x) )
{
vertex = vertices[vertIndex];
break;
}
}
return vertex;
}
/// <summary>
/// Insert the vertex in an exiting edge
/// </summary>
/// <param name="vertexPosition">The position of the vertex to insert</param>
/// <returns>The inew vertex inserted</returns>
Vertex InsertVertexOnExistingEdge(Vector3 vertexPosition)
{
List<Vertex> vertices = m_Mesh.GetVertices().ToList();
List<Edge> peripheralEdges = WingedEdge.SortEdgesByAdjacency(m_TargetFace);
int bestIndex = -1;
float bestDistance = Mathf.Infinity;
for (int i = 0; i < peripheralEdges.Count; i++)
{
float dist = UnityEngine.ProBuilder.Math.DistancePointLineSegment(vertexPosition,
vertices[peripheralEdges[i].a].position,
vertices[peripheralEdges[i].b].position);
if (dist < bestDistance)
{
bestIndex = i;
bestDistance = dist;
}
}
Vertex v = m_Mesh.InsertVertexOnEdge(peripheralEdges[bestIndex], vertexPosition);
return v;
}
}
}