-
Notifications
You must be signed in to change notification settings - Fork 0
/
MeshTools.cs
205 lines (198 loc) · 7.8 KB
/
MeshTools.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Collections.ObjectModel;
namespace Mola
{
/// <summary>
/// Tools to edit MolaMesh
/// </summary>
public class MeshTools
{
/// <summary>
/// Creates an offset of a mesh.
/// If `doclose` is `true`, it will create quad faces
/// along the naked edges of an open input mesh.
/// </summary>
/// <param name="mesh">A MolaMesh</param>
/// <param name="offset">Offset distance</param>
/// <param name="closeborders">Wether to close the borders or not</param>
/// <param name="constrainZ"></param>
/// <returns>The result MolaMesh</returns>
/// ![](offsetface.png)
public static MolaMesh Offset(MolaMesh mesh, float offset, bool closeborders = true, bool constrainZ = false)
{
// calculate normals per vertex
// create new vertices and duplicate faces
// close borders
mesh = mesh.Copy(); // do not change original mesh
if (closeborders) mesh.WeldVertices();
int nFaces = mesh.Faces.Count;
int nVertices = mesh.Vertices.Count;
Vec3[] normals = UtilsVertex.getVertexNormals(mesh);
if (constrainZ)
{
for (int i = 0; i < normals.Length; i++)
{
Vec3 normal = normals[i];
normal.Set(normal.x, normal.y, 0);
normal.Normalize();
normals[i] = normal;
}
}
for (int i = 0; i < normals.Length; i++)
{
Vec3 n = normals[i];
n *= offset;
n += mesh.Vertices[i];
mesh.AddVertex(n.x, n.y, n.z);
}
for (int i = 0; i < nFaces; i++)
{
int[] face = mesh.Faces[i];
int[] newFace = new int[face.Length];
for (int j = 0; j < face.Length; j++)
{
newFace[j] = face[face.Length - j - 1] + nVertices;
}
mesh.AddFace(newFace);
}
if (closeborders)
{
mesh.UpdateTopology();
ReadOnlyCollection<int[]> edges = mesh.GetTopoEdges();
foreach (int[] edge in edges)
{
int f1 = edge[2];
int f2 = edge[3];
if (f1 < 0 || f2 < 0)
{
if (edge[0] < nVertices && edge[1] < nVertices)
{
int[] face = new int[4];
face[3] = edge[0];
face[2] = edge[1];
face[1] = edge[1] + nVertices;
face[0] = edge[0] + nVertices;
mesh.AddFace(face);
}
}
}
}
for (int i = 0; i < mesh.FacesCount(); i++)
{
mesh.Faces[i] = Enumerable.Reverse(mesh.Faces[i]).ToArray();
}
return mesh;
}
/// <summary>
/// Divide a MolaMesh into 2 based on the boolean mask
/// </summary>
/// <param name="molaMesh">A MolaMesh</param>
/// <param name="mask">A boolean array</param>
/// <returns>A list of two MolaMesh</returns>
public static List<MolaMesh> Split(MolaMesh molaMesh, bool[] mask)
{
if(mask.Length != molaMesh.FacesCount())
{
throw new ArgumentException("mask array count doesn't match face count!");
}
MolaMesh m1 = molaMesh.CopySubMesh(mask);
mask = mask.Select(b => !b).ToArray();
MolaMesh m2 = molaMesh.CopySubMesh(mask);
return new List<MolaMesh> { m1, m2 };
}
/// <summary>
/// Divide a MolaMesh into 2 based on the boolean mask
/// </summary>
/// <param name="molaMesh">A MolaMesh</param>
/// <param name="mask">A boolean array</param>
/// <returns>A list of two molaMesh</returns>
public static List<MolaMesh> Split(MolaMesh molaMesh, List<bool> mask)
{
bool[] maskArray = mask.ToArray();
return Split(molaMesh, maskArray);
}
/// <summary>
/// Merge a list of MolaMesh into one
/// </summary>
/// <param name="molaMeshes">A list of MolaMesh</param>
/// <returns>The result MolaMesh</returns>
public static MolaMesh Merge(List<MolaMesh> molaMeshes)
{
MolaMesh molaMesh = new MolaMesh();
for (int i = 0; i < molaMeshes.Count; i++)
{
molaMesh.AddMesh(molaMeshes[i]);
}
return molaMesh;
}
/// <summary>
/// Color each face of a MolaMesh with a list of float values.
/// The list length must match the face count.
/// </summary>
/// <param name="molaMesh">A MolaMesh</param>
/// <param name="values">A list of float values</param>
/// <returns>The result MolaMesh</returns>
public static MolaMesh Color(MolaMesh molaMesh, List<float> values)
{
if (values.Count != molaMesh.FacesCount())
{
throw new ArgumentException("value list count doesn't match face count!");
}
UtilsFace.ColorFaceByValue(molaMesh, values);
return molaMesh;
}
/// <summary>
/// Color all faces of a MolaMesh
/// </summary>
/// <param name="molaMesh">A MolaMesh</param>
/// <param name="color">System.Drawing.Color</param>
/// <returns>The result MolaMesh</returns>
public static MolaMesh Color(MolaMesh molaMesh, System.Drawing.Color color)
{
Color mColor = new Color((float)color.R / 255, (float)color.G / 255, (float)color.B / 255, (float)color.A / 255);
molaMesh.Colors = Enumerable.Repeat(mColor, molaMesh.VertexCount()).ToList();
return molaMesh;
}
/// <summary>
/// Get a boolean array from a float value list
/// based on the input filter condition
/// </summary>
/// <param name="values">A float list</param>
/// <param name="filter">A Predicate</param>
/// <returns>A boolean Array</returns>
/// ### Example
/// ~~~~~~~~~~~~.cs
/// MolaMesh molaMesh = MeshFactory.CreateSphere();
/// List<float> faceArea = MeshAnalysis.FaceArea(molaMesh);
/// Predicate<float> filter = a => a > 1;
/// bool[] mask = MeshUtils.FaceMask(faceArea, filter);
/// ~~~~~~~~~~~~
public static bool[] FaceMask(List<float> values, Predicate<float> filter)
{
return values.Select(a => filter(a)).ToArray();
}
/// <summary>
/// Update the topology of a MolaMesh
/// </summary>
/// <param name="molaMesh">A MolaMesh</param>
/// <returns>A boolean Array</returns>
public static MolaMesh UpdateTopology(MolaMesh molaMesh)
{
molaMesh.UpdateTopology();
return molaMesh;
}
/// <summary>
/// Weld overlapping vertices of a MolaMesh
/// </summary>
/// <param name="molaMesh">A MolaMesh</param>
/// <returns>A boolean Array</returns>
public static MolaMesh WeldVertices(MolaMesh molaMesh)
{
molaMesh.WeldVertices();
return molaMesh;
}
}
}