/
Lattice.cs
495 lines (455 loc) · 21.9 KB
/
Lattice.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
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
using Grasshopper;
using Grasshopper.Kernel.Data;
using Rhino;
using Rhino.DocObjects;
using Rhino.Geometry;
using Rhino.Geometry.Intersect;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
// Summary: This set of classes is used to generate a lattice wireframe.
// Refer to the developer documentation for more information.
// =====================================================================================================
// Author(s): Aidan Kurtz (http://aidankurtz.com)
namespace IntraLattice.CORE.Data
{
/// <summary>
/// Represents the lattice as a set of nodes in a UVW tree.
/// Once the nodes are set, the various mapping methods can be used to
/// map the unit cell topology to the node tree.
/// </summary>
public class Lattice
{
#region Fields
private DataTree<LatticeNode> m_nodes;
private List<Curve> m_struts;
#endregion
#region Constructors
/// <summary>
/// Default constructor.
/// </summary>
public Lattice()
{
m_nodes = new DataTree<LatticeNode>();
m_struts = new List<Curve>();
}
/// <summary>
/// Copy constructor.
/// </summary>
public Lattice Duplicate()
{
using (MemoryStream stream = new MemoryStream())
{
if (this.GetType().IsSerializable)
{
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, this);
stream.Position = 0;
return (Lattice)formatter.Deserialize(stream);
}
return null;
}
}
#endregion
#region Properties
/// <summary>
/// Nodes as a UVWi tree.
/// </summary>
public DataTree<LatticeNode> Nodes
{
get { return m_nodes; }
set { m_nodes = value; }
}
/// <summary>
/// Struts as a list of curves, to be output.
/// </summary>
public List<Curve> Struts
{
get { return m_struts; }
set { m_struts = value; }
}
#endregion
#region Methods
/// <summary>
/// Maps cell topology to UVWi node map (linear struts).
/// </summary>
public void ConformMapping(UnitCell cell, float[] N)
{
for (int u = 0; u <= N[0]; u++)
{
for (int v = 0; v <= N[1]; v++)
{
for (int w = 0; w <= N[2]; w++)
{
// We're inside a unit cell
// Loop through all pairs of nodes that make up struts
foreach (IndexPair nodePair in cell.NodePairs)
{
// Prepare the path of the nodes (path in tree)
// First, get relative paths of nodes (with respect to current unit cell)
int[] IRel = cell.NodePaths[nodePair.I];
int[] JRel = cell.NodePaths[nodePair.J];
// Next, compute absolute paths
GH_Path IPath = new GH_Path(u + IRel[0], v + IRel[1], w + IRel[2]);
GH_Path JPath = new GH_Path(u + JRel[0], v + JRel[1], w + JRel[2]);
// Make sure the cell exists
// No cells exist beyond the boundary + 1
if (Nodes.PathExists(IPath) && Nodes.PathExists(JPath))
{
LatticeNode node1 = Nodes[IPath, IRel[3]];
LatticeNode node2 = Nodes[JPath, JRel[3]];
// Make sure both nodes exist:
// Null nodes either belong to other cells, or are beyond the upper uvw boundary
if (node1 != null && node2 != null)
{
LineCurve curve = new LineCurve(node1.Point3d, node2.Point3d);
if (curve != null && curve.IsValid)
{
Struts.Add(curve);
}
}
}
}
}
}
}
}
/// <summary>
/// Morphs cell topology to UVWI node map (morphed struts).
/// </summary>
public void MorphMapping(UnitCell cell, DataTree<GeometryBase> spaceTree, float[] N)
{
for (int u = 0; u <= N[0]; u++)
{
for (int v = 0; v <= N[1]; v++)
{
for (int w = 0; w <= N[2]; w++)
{
// We're inside a unit cell
// Loop through all pairs of nodes that make up struts
foreach (IndexPair nodePair in cell.NodePairs)
{
// Prepare the path of the nodes (path in tree)
// First, get relative paths of nodes (with respect to current unit cell)
int[] IRel = cell.NodePaths[nodePair.I];
int[] JRel = cell.NodePaths[nodePair.J];
// Next, compute absolute paths
GH_Path IPath = new GH_Path(u + IRel[0], v + IRel[1], w + IRel[2]);
GH_Path JPath = new GH_Path(u + JRel[0], v + JRel[1], w + JRel[2]);
// Make sure the cell exists
// No cells exist beyond the boundary + 1
if (Nodes.PathExists(IPath) && Nodes.PathExists(JPath))
{
LatticeNode node1 = Nodes[IPath, IRel[3]];
LatticeNode node2 = Nodes[JPath, JRel[3]];
// Make sure both nodes exist:
// Null nodes either belong to other cells, or are beyond the upper uvw boundary.
if (node1 != null && node2 != null)
{
GH_Path spacePath;
// If strut is along boundary, we must use the previous morph space
// Since one does not exist beyond the boundary)
if (u == N[0] && v == N[1])
{
spacePath = new GH_Path(u - 1, v - 1);
}
else if (u == N[0])
{
spacePath = new GH_Path(u - 1, v);
}
else if (v == N[1])
{
spacePath = new GH_Path(u, v - 1);
}
else
{
spacePath = new GH_Path(u, v);
}
// Retrieve uv cell space (will be casted in the tempPt loop)
GeometryBase ss1 = spaceTree[spacePath, 0];
GeometryBase ss2 = spaceTree[spacePath, 1];
// Discretize the unit cell line for morph mapping
int ptCount = 16;
// Template points are unitized cell points (x,y of these points are u,v of sub-surface)
var templatePts = new List<Point3d>();
Line templateLine = new Line(cell.Nodes[nodePair.I], cell.Nodes[nodePair.J]);
for (int ptIndex = 0; ptIndex <= ptCount; ptIndex++)
{
templatePts.Add(templateLine.PointAt(ptIndex / (double)ptCount));
}
// We will map the lines' points to its uvw cell-space
// Control points are the interpolation points in space
var controlPoints = new List<Point3d>();
foreach (Point3d tempPt in templatePts)
{
Point3d surfPt;
Vector3d[] surfDerivs;
// UV params on unitized sub-surface are simply the xy coordinate of the template point
double uParam = tempPt.X;
double vParam = tempPt.Y;
// If at boundary, we're using a previous morph space, so reverse the parameter(s)
if (u == N[0])
{
uParam = 1 - uParam;
}
if (v == N[1])
{
vParam = 1 - vParam;
}
// Now, we will map the template point to the uvw-space
((Surface)ss1).Evaluate(uParam, vParam, 0, out surfPt, out surfDerivs);
Vector3d wVect = Vector3d.Unset;
switch (ss2.ObjectType)
{
case ObjectType.Point:
wVect = ((Point)ss2).Location - surfPt; ;
break;
case ObjectType.Curve:
wVect = ((Curve)ss2).PointAt(uParam) - surfPt;
break;
case ObjectType.Surface:
Point3d surfPt2;
Vector3d[] surfDerivs2;
((Surface)ss2).Evaluate(uParam, vParam, 0, out surfPt2, out surfDerivs2);
wVect = surfPt2 - surfPt;
break;
}
// The mapped point
Point3d uvwPt = surfPt + wVect * (w + tempPt.Z) / N[2];
controlPoints.Add(uvwPt);
}
// Now create interpolated curve based on control points
Curve curve = Curve.CreateInterpolatedCurve(controlPoints, 3);
if (curve != null && curve.IsValid)
{
Struts.Add(curve);
}
}
}
}
}
}
}
}
/// <summary>
/// Maps cell topology to the node grid and trims to the design space.
/// </summary>
public void UniformMapping(UnitCell cell, GeometryBase designSpace, int spaceType, float[] N, double minStrutLength, double maxStrutLength)
{
double tol = RhinoDoc.ActiveDoc.ModelAbsoluteTolerance;
for (int u = 0; u <= N[0]; u++)
{
for (int v = 0; v <= N[1]; v++)
{
for (int w = 0; w <= N[2]; w++)
{
// We're inside a unit cell
// Loop through all pairs of nodes that make up struts
foreach (IndexPair nodePair in cell.NodePairs)
{
// Prepare the path of the nodes (path in tree)
// First, get relative paths of nodes (with respect to current unit cell)
int[] IRel = cell.NodePaths[nodePair.I];
int[] JRel = cell.NodePaths[nodePair.J];
// Next, compute absolute paths
GH_Path IPath = new GH_Path(u + IRel[0], v + IRel[1], w + IRel[2]);
GH_Path JPath = new GH_Path(u + JRel[0], v + JRel[1], w + JRel[2]);
// Make sure the cell exists
if (Nodes.PathExists(IPath) && Nodes.PathExists(JPath))
{
LatticeNode node1 = Nodes[IPath, IRel[3]];
LatticeNode node2 = Nodes[JPath, JRel[3]];
// Make sure both nodes exist:
// Null nodes either belong to other cells, or are beyond the upper uvw boundary
if (node1 != null && node2 != null)
{
Curve fullCurve = new LineCurve(node1.Point3d, node2.Point3d);
// If both nodes are inside, add full strut
if (node1.IsInside && node2.IsInside)
{
Struts.Add(fullCurve);
}
// If neither node is inside, skip to next loop
else if (!node1.IsInside && !node2.IsInside)
{
continue;
}
// Else, strut requires trimming
else
{
// We are going to find the intersection point with the design space
Point3d[] intersectionPts = null;
Curve[] overlapCurves = null;
LineCurve strutToTrim = null;
switch (spaceType)
{
// Brep design space
case 1:
strutToTrim = new LineCurve(node1.Point3d, node2.Point3d);
// Find intersection point
Intersection.CurveBrep(strutToTrim, (Brep)designSpace, tol, out overlapCurves, out intersectionPts);
break;
// Mesh design space
case 2:
// Dummy faceIds variable for MeshLine call
int[] faceIds;
strutToTrim = new LineCurve(node1.Point3d, node2.Point3d);
// Find intersection point
intersectionPts = Intersection.MeshLine((Mesh)designSpace, strutToTrim.Line, out faceIds);
break;
// Solid surface design space
case 3:
// Dummy overlapCurves variable for CurveBrep call
overlapCurves = null;
strutToTrim = new LineCurve(node1.Point3d, node2.Point3d);
// Find intersection point
Intersection.CurveBrep(strutToTrim, ((Surface)designSpace).ToBrep(), tol, out overlapCurves, out intersectionPts);
break;
}
LineCurve testLine = null;
// Now, if an intersection point was found, trim the strut
if (intersectionPts.Length > 0)
{
testLine = AddTrimmedStrut(node1, node2, intersectionPts[0], minStrutLength, maxStrutLength);
// If the strut was succesfully trimmed, add it to the list
if (testLine != null)
{
Struts.Add(testLine);
}
}
else if (overlapCurves != null && overlapCurves.Length > 0)
{
Struts.Add(overlapCurves[0]);
}
}
}
}
}
}
}
}
}
/// <summary>
/// Trims strut with known intersection point, returning the trimmed LineCurve which is inside the space.
/// </summary>
public LineCurve AddTrimmedStrut(LatticeNode node1, LatticeNode node2, Point3d intersectionPt, double minLength, double maxLength)
{
LineCurve testStrut = new LineCurve(new Line(node1.Point3d, node2.Point3d), 0, 1); // set line, with curve parameter domain [0,1]
if (node1.IsInside)
{
double trimmedLength = intersectionPt.DistanceTo(node1.Point3d);
if (trimmedLength > minLength && trimmedLength < maxLength)
{
Nodes.Add(new LatticeNode(intersectionPt, LatticeNodeState.Boundary));
return new LineCurve(node1.Point3d, intersectionPt);
}
else
{
node1.State = LatticeNodeState.Boundary;
}
}
if (node2.IsInside)
{
double trimmedLength = intersectionPt.DistanceTo(node2.Point3d);
if (trimmedLength > minLength && trimmedLength < maxLength)
{
Nodes.Add(new LatticeNode(intersectionPt, LatticeNodeState.Boundary));
return new LineCurve(node2.Point3d, intersectionPt);
}
else
{
node2.State = LatticeNodeState.Boundary;
}
}
return null;
}
#endregion
}
/// <summary>
/// Represents a lattice node. Could be extended to include more information. This will do for now.
/// </summary>
[Serializable]
public class LatticeNode
{
#region Fields
private Point3d m_point3d;
private LatticeNodeState m_state;
#endregion
#region Constructors
/// <summary>
/// Default constructor.
/// </summary>
public LatticeNode()
{
m_point3d = Point3d.Unset;
m_state = LatticeNodeState.Inside;
}
/// <summary>
/// Instance constructor based on a Point3d location.
/// </summary>
public LatticeNode(Point3d point3d)
{
m_point3d = point3d;
m_state = LatticeNodeState.Inside;
}
/// <summary>
/// Instance constructor based on a Point3d location and an inside/outside state (wrt design space).
/// </summary>
public LatticeNode(Point3d point3d, LatticeNodeState state)
{
m_point3d = point3d;
m_state = state;
}
#endregion
#region Properties
/// <summary>
/// Coordinates of node.
/// </summary>
public Point3d Point3d
{
get { return m_point3d; }
set { m_point3d = value; }
}
/// <summary>
/// State of the node wrt design space (either inside, outside or on bounary).
/// </summary>
public LatticeNodeState State
{
get { return m_state; }
set { m_state = value; }
}
/// <summary>
/// Simplified state, as boolean.
/// </summary>
public bool IsInside
{
get
{
if (m_state == LatticeNodeState.Outside)
{
return false;
}
else
{
return true;
}
}
}
#endregion
#region Methods
// none yet
#endregion
}
/// <summary>
/// Represents the state of the node, with respect to the design space.
/// </summary>
public enum LatticeNodeState
{
Outside = 0,
Inside = 1,
Boundary = 2,
}
}