-
Notifications
You must be signed in to change notification settings - Fork 1
/
Conversion.cs
99 lines (90 loc) · 3.71 KB
/
Conversion.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
using g3;
using Elements.Geometry;
namespace Elements.G3;
/// <summary>
/// Provides static methods for converting between Elements.Geometry and g3 types.
/// </summary>
public static class Conversion
{
/// <summary>
/// Converts a single Mesh to a DMesh3 type.
/// </summary>
/// <param name="mesh">The Mesh to be converted.</param>
/// <param name="transform">An optional transformation to be applied to the Mesh.</param>
/// <returns>A DMesh3 instance corresponding to the input Mesh.</returns>
public static DMesh3 ToDMesh3(this Mesh mesh, Transform? transform = null)
{
return new Mesh[] { mesh }.ToDMesh3(transform);
}
/// <summary>
/// Converts a collection of Meshes to a single DMesh3 type.
/// </summary>
/// <param name="meshes">The collection of Meshes to be converted.</param>
/// <param name="transform">An optional transformation to be applied to each Mesh in the collection.</param>
/// <returns>A DMesh3 instance representing the combined input Meshes.</returns>
public static DMesh3 ToDMesh3(this IEnumerable<Mesh> meshes, Transform? transform = null)
{
var dMesh = new DMesh3();
var indexOffset = 0;
foreach (var m in meshes)
{
foreach (var v in m.Vertices)
{
var pt = transform == null ? v.Position : transform.OfPoint(v.Position);
dMesh.AppendVertex(pt.ToG3Vector3d());
}
foreach (var tri in m.Triangles)
{
var triVerts = tri.Vertices;
dMesh.AppendTriangle(triVerts[0].Index + indexOffset, triVerts[1].Index + indexOffset, triVerts[2].Index + indexOffset);
}
indexOffset += m.Vertices.Count;
}
return dMesh;
}
/// <summary>
/// Converts a single Mesh to a DMeshAABBTree3 type.
/// </summary>
/// <param name="mesh">The Mesh to be converted.</param>
/// <returns>A DMeshAABBTree3 instance corresponding to the input Mesh.</returns>
public static DMeshAABBTree3 ToAABBTree(this Mesh mesh)
{
return new Mesh[] { mesh }.ToAABBTree();
}
/// <summary>
/// Converts a collection of Meshes to a single DMeshAABBTree3 type.
/// </summary>
/// <param name="meshes">The collection of Meshes to be converted.</param>
/// <returns>A DMeshAABBTree3 instance representing the combined input Meshes.</returns>
public static DMeshAABBTree3 ToAABBTree(this IEnumerable<Mesh> meshes)
{
return new DMeshAABBTree3(meshes.ToDMesh3(), true);
}
/// <summary>
/// Converts an Elements.Geometry Vector3 to a g3 Vector3d.
/// </summary>
/// <param name="v">The Vector3 to be converted.</param>
/// <returns>A Vector3d instance corresponding to the input Vector3.</returns>
public static Vector3d ToG3Vector3d(this Vector3 v)
{
return new Vector3d(v.X, v.Y, v.Z);
}
/// <summary>
/// Converts a g3 Vector3d to an Elements.Geometry Vector3.
/// </summary>
/// <param name="v">The Vector3d to be converted.</param>
/// <returns>A Vector3 instance corresponding to the input Vector3d.</returns>
public static Vector3 ToElementsVector3(this Vector3d v)
{
return new Vector3(v.x, v.y, v.z);
}
/// <summary>
/// Converts an Elements.Geometry Ray to a g3 Ray3d.
/// </summary>
/// <param name="ray">The Ray to be converted.</param>
/// <returns>A Ray3d instance corresponding to the input Ray.</returns>
public static Ray3d ToG3Ray3d(this Ray ray)
{
return new Ray3d(ray.Origin.ToG3Vector3d(), ray.Direction.ToG3Vector3d());
}
}