-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMeshGrowthSystem.cs
207 lines (183 loc) · 8.21 KB
/
MeshGrowthSystem.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
206
207
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Plankton;
using PlanktonGh;
using Rhino.Geometry;
namespace MeshGrowth
{
public class MeshGrowthSystem
{
private PlanktonMesh _ptMesh;
private List<Vector3d> _totalWeightedMoves;
private List<double> _totalWeights;
//private int _vertexCount;
public MeshGrowthSystem(Mesh startingMesh)
{
_ptMesh = startingMesh.ToPlanktonMesh();
//_vertexCount = _ptMesh.Vertices.Count;
}
public Mesh GetRhinoMesh()
{
return _ptMesh.ToRhinoMesh();
}
public void Update()
{
if(Grow)
{
SplitAllLongEdges();
}
int vertexCount = _ptMesh.Vertices.Count;
_totalWeightedMoves = new List<Vector3d>(Enumerable.Repeat(new Vector3d(), vertexCount));
_totalWeights = new List<double>(Enumerable.Repeat(0.0, vertexCount));
if (UseRTree)
{
ProcessCollisionUsingRTree();
}
else
{
ProcessCollision();
}
ProcessBendingResistance();
UpdateVertexPositions();
ProcessEdgeLengthConstraint();
}
private void ProcessCollisionUsingRTree()
{
RTree rTree = new RTree();
for (int i = 0; i < _ptMesh.Vertices.Count; i++)
{
rTree.Insert(_ptMesh.Vertices[i].ToPoint3d(), i);
}
for (int i = 0; i < _ptMesh.Vertices.Count; i++)
{
Point3d vI = _ptMesh.Vertices[i].ToPoint3d();
Sphere searchSphere = new Sphere(vI, CollisionDistance);
List<int> collisionIndices = new List<int>();
rTree.Search(
searchSphere,
(sender, args) => { if (i < args.Id) collisionIndices.Add(args.Id); });
foreach (int j in collisionIndices)
{
Vector3d move = _ptMesh.Vertices[j].ToPoint3d() - _ptMesh.Vertices[i].ToPoint3d();
double currentDistance = move.Length;
move *= 0.5 * (currentDistance - CollisionDistance) / currentDistance;
_totalWeightedMoves[i] += CollisionWeight * move;
_totalWeightedMoves[j] -= CollisionWeight * move;
_totalWeights[i] += CollisionWeight;
_totalWeights[j] += CollisionWeight;
}
}
}
private void ProcessEdgeLengthConstraint()
{
for (int k = 0; k < _ptMesh.Halfedges.Count; k += 2)
{
int i = _ptMesh.Halfedges[k].StartVertex;
int j = _ptMesh.Halfedges[k + 1].StartVertex;
Point3d ptI = _ptMesh.Vertices[i].ToPoint3d();
Point3d ptJ = _ptMesh.Vertices[j].ToPoint3d();
if (ptI.DistanceTo(ptJ) < CollisionDistance) continue;
Vector3d move = ptJ - ptI;
move *= (move.Length - CollisionDistance) * 0.5 / move.Length;
_totalWeightedMoves[i] += move * EdgeLengthConstraintWeight;
_totalWeightedMoves[j] -= move * EdgeLengthConstraintWeight;
_totalWeights[i] += EdgeLengthConstraintWeight;
_totalWeights[j] += EdgeLengthConstraintWeight;
}
}
private void ProcessBendingResistance()
{
int halfEdgeCount = _ptMesh.Halfedges.Count;
for (int k = 0; k < halfEdgeCount; k += 2)
{
int i = _ptMesh.Halfedges[k].StartVertex;
int j = _ptMesh.Halfedges[k + 1].StartVertex;
int p = _ptMesh.Halfedges[_ptMesh.Halfedges[k].PrevHalfedge].StartVertex;
int q = _ptMesh.Halfedges[_ptMesh.Halfedges[k + 1].PrevHalfedge].StartVertex;
Point3d vI = _ptMesh.Vertices[i].ToPoint3d();
Point3d vJ = _ptMesh.Vertices[j].ToPoint3d();
Point3d vP = _ptMesh.Vertices[p].ToPoint3d();
Point3d vQ = _ptMesh.Vertices[q].ToPoint3d();
Vector3d nP = Vector3d.CrossProduct(vJ - vI, vP - vI);
Vector3d nQ = Vector3d.CrossProduct(vQ - vI, vJ - vI);
Vector3d planeNormal = (nP + nQ);
Point3d planeOrigin = 0.25 * (vI + vJ + vP + vQ);
Plane plane = new Plane(planeOrigin, planeNormal);
_totalWeightedMoves[i] += BendingResistanceWeight * (plane.ClosestPoint(vI) - vI);
_totalWeightedMoves[j] += BendingResistanceWeight * (plane.ClosestPoint(vJ) - vJ);
_totalWeightedMoves[p] += BendingResistanceWeight * (plane.ClosestPoint(vP) - vP);
_totalWeightedMoves[q] += BendingResistanceWeight * (plane.ClosestPoint(vQ) - vQ);
_totalWeights[i] += BendingResistanceWeight;
_totalWeights[j] += BendingResistanceWeight;
_totalWeights[p] += BendingResistanceWeight;
_totalWeights[q] += BendingResistanceWeight;
}
}
private void ProcessCollision()
{
int vertexCount = _ptMesh.Vertices.Count;
for (int i = 0; i < vertexCount; i++)
{
for (int j = 0; j < vertexCount; j++)
{
Vector3d move = _ptMesh.Vertices[j].ToPoint3d() -
_ptMesh.Vertices[i].ToPoint3d();
double currentDistance = move.Length;
if (currentDistance > CollisionDistance)
continue;
move *= 0.5 * (currentDistance - CollisionDistance) / currentDistance;
_totalWeightedMoves[i] += move;
_totalWeightedMoves[j] -= move;
_totalWeights[i] += 1;
_totalWeights[j] += 1;
}
}
}
private void SplitAllLongEdges()
{
int halfedgeCount = _ptMesh.Halfedges.Count;
for (int i = 0; i < halfedgeCount; i+=2)
{
if (_ptMesh.Vertices.Count< MaxVertexCount &&
_ptMesh.Halfedges.GetLength(i) > 0.99 * CollisionDistance)
{
SplitEdge(i);
}
}
}
private void SplitEdge(int index)
{
int newHalfEdgeIndex = _ptMesh.Halfedges.SplitEdge(index);
_ptMesh.Vertices.SetVertex(
_ptMesh.Vertices.Count - 1,
0.5 * (_ptMesh.Vertices[_ptMesh.Halfedges[index].StartVertex].ToPoint3d() +
_ptMesh.Vertices[_ptMesh.Halfedges[index + 1].StartVertex].ToPoint3d()));
if (_ptMesh.Halfedges[index].AdjacentFace >= 0)
_ptMesh.Faces.SplitFace(newHalfEdgeIndex, _ptMesh.Halfedges[index].PrevHalfedge);
if (_ptMesh.Halfedges[index + 1].AdjacentFace >= 0)
_ptMesh.Faces.SplitFace(index + 1, _ptMesh.Halfedges[_ptMesh.Halfedges[index + 1].NextHalfedge].NextHalfedge);
}
private void UpdateVertexPositions()
{
int vertexCount = _ptMesh.Vertices.Count;
for (int i = 0; i < vertexCount; i++)
{
if (_totalWeights[i] == 0.0)
continue;
Vector3d move = _totalWeightedMoves[i]/ _totalWeights[i];
Point3d updatedPosistion = _ptMesh.Vertices[i].ToPoint3d() + move;
_ptMesh.Vertices.SetVertex(i, updatedPosistion.X, updatedPosistion.Y, updatedPosistion.Z);
}
}
public bool Grow { get; set; }
public int MaxVertexCount { get; set; }
public double EdgeLengthConstraintWeight { get; set; }
public double CollisionDistance { get; set; }
public double CollisionWeight { get; set; }
public double BendingResistanceWeight { get; set; }
public bool UseRTree { get; set; }
}
}