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
| Imports System.Windows.Media.Media3D
Public Class FactoryMesh
Private defaultTickness As Double = 0.01
Public Sub New()
Thickness = defaultTickness
End Sub
Public ReadOnly Property CubeMesh() As MeshGeometry3D
Get
Return CreateCube()
End Get
End Property
Public Property Center() As Point3D
Public ReadOnly Property CubeWire As MeshGeometry3D
Get
Return ToWireframe()
End Get
End Property
Private thicknessValue As Double
Public Property Thickness As Double
Get
Return thicknessValue
End Get
Set(ByVal value As Double)
thicknessValue = value
ToWireframe()
End Set
End Property
'partie faces
Private Function CreateCube() As MeshGeometry3D
Dim cube As New MeshGeometry3D()
Dim p0 As Point3D = New Point3D(0, 0, 0)
Dim p1 As Point3D = New Point3D(1, 0, 0)
Dim p2 As Point3D = New Point3D(1, 0, 1)
Dim p3 As Point3D = New Point3D(0, 0, 1)
Dim p4 As Point3D = New Point3D(0, 1, 0)
Dim p5 As Point3D = New Point3D(1, 1, 0)
Dim p6 As Point3D = New Point3D(1, 1, 1)
Dim p7 As Point3D = New Point3D(0, 1, 1)
'front side triangles
CreeTriangle(cube, p3, p2, p6)
CreeTriangle(cube, p3, p6, p7)
'back side triangles
CreeTriangle(cube, p1, p0, p4)
CreeTriangle(cube, p1, p4, p5)
'right side triangles
CreeTriangle(cube, p2, p1, p5)
CreeTriangle(cube, p2, p5, p6)
'left side triangles
CreeTriangle(cube, p0, p3, p7)
CreeTriangle(cube, p0, p7, p4)
'top side triangles
CreeTriangle(cube, p7, p6, p5)
CreeTriangle(cube, p7, p5, p4)
'bottom side triangles
CreeTriangle(cube, p2, p3, p0)
CreeTriangle(cube, p2, p0, p1)
For Each p As Point3D In cube.Positions
Center += p
Next
Return cube
End Function
Private Sub CreeTriangle(ByVal m As MeshGeometry3D, ByVal p0 As Point3D, ByVal p1 As Point3D, ByVal p2 As Point3D)
Dim index As Integer = m.Positions.Count
m.Positions.Add(p0)
m.Positions.Add(p1)
m.Positions.Add(p2)
m.TriangleIndices.Add(index)
m.TriangleIndices.Add(index + 1)
m.TriangleIndices.Add(index + 2)
Dim normal As Vector3D = CalculateNormal(p0, p1, p2)
m.Normals.Add(normal)
m.Normals.Add(normal)
m.Normals.Add(normal)
m.TextureCoordinates.Add(New Point(0, 0))
m.TextureCoordinates.Add(New Point(0, 1))
m.TextureCoordinates.Add(New Point(1, 1))
m.TextureCoordinates.Add(New Point(1, 1))
End Sub
Private Function CalculateNormal(ByVal p0 As Point3D, ByVal p1 As Point3D, ByVal p2 As Point3D) As Vector3D
Dim v0 As Vector3D = New Vector3D(
p1.X - p0.X, p1.Y - p0.Y, p1.Z - p0.Z)
Dim v1 As Vector3D = New Vector3D(
p2.X - p1.X, p2.Y - p1.Y, p2.Z - p1.Z)
Return Vector3D.CrossProduct(v0, v1)
End Function
'partie maillage
Public Function ToWireframe() As MeshGeometry3D
If CubeMesh Is Nothing Then Return Nothing
'Make a mesh to hold the wireframe.
Dim wireframe As New MeshGeometry3D()
'Loop through the mesh's triangles.
For triangle As Integer = 0 To CubeMesh.TriangleIndices.Count - 1 Step 3
'Get the triangle's corner indices.
Dim index1 As Integer = CubeMesh.TriangleIndices(triangle)
Dim index2 As Integer = CubeMesh.TriangleIndices(triangle + 1)
Dim index3 As Integer = CubeMesh.TriangleIndices(triangle + 2)
'Make the triangle's three segments.
AddTriangleSegment(CubeMesh, wireframe, index1, index2, Thickness)
AddTriangleSegment(CubeMesh, wireframe, index2, index3, Thickness)
AddTriangleSegment(CubeMesh, wireframe, index3, index1, Thickness)
Next
Return wireframe
End Function
'Add the triangle's three segments.
Private Sub AddTriangleSegment(ByVal OMesh As MeshGeometry3D,
ByVal OWireframe As MeshGeometry3D,
ByVal index1 As Integer, ByVal index2 As Double, ByVal thickness As Double)
Dim up As Vector3D = New Vector3D(0, 1, 0)
Dim segment As Vector3D = OMesh.Positions(index2) - OMesh.Positions(index1)
segment.Normalize()
If Math.Abs(Vector3D.DotProduct(up, segment)) > 0.9 Then
up = New Vector3D(1, 0, 0)
End If
'Create the segment.
AddSegment(OWireframe, OMesh.Positions(index1), OMesh.Positions(index2), up, thickness)
End Sub
Private Sub AddSegment(ByVal OWireframe As MeshGeometry3D,
ByVal point1 As Point3D, ByVal point2 As Point3D,
ByVal up As Vector3D, ByVal th As Double)
'Get the segment's vector.
Dim v As Vector3D = point2 - point1
'Get the scaled up vector.
Dim n1 As Vector3D = ScaleVector(up, th / 2.0)
'Get another scaled perpendicular vector.
Dim n2 As Vector3D = Vector3D.CrossProduct(v, n1)
n2 = ScaleVector(n2, th / 2.0)
'Make a skinny box.
'p1pm means point1 PLUS n1 MINUS n2.
Dim p1pp As Point3D = point1 + n1 + n2
Dim p1mp As Point3D = point1 - n1 + n2
Dim p1pm As Point3D = point1 + n1 - n2
Dim p1mm As Point3D = point1 - n1 - n2
Dim p2pp As Point3D = point2 + n1 + n2
Dim p2mp As Point3D = point2 - n1 + n2
Dim p2pm As Point3D = point2 + n1 - n2
Dim p2mm As Point3D = point2 - n1 - n2
'Sides.
AddTriangle(OWireframe, p1pp, p1mp, p2mp)
AddTriangle(OWireframe, p1pp, p2mp, p2pp)
AddTriangle(OWireframe, p1pp, p2pp, p2pm)
AddTriangle(OWireframe, p1pp, p2pm, p1pm)
AddTriangle(OWireframe, p1pm, p2pm, p2mm)
AddTriangle(OWireframe, p1pm, p2mm, p1mm)
AddTriangle(OWireframe, p1mm, p2mm, p2mp)
AddTriangle(OWireframe, p1mm, p2mp, p1mp)
'Ends.
AddTriangle(OWireframe, p1pp, p1pm, p1mm)
AddTriangle(OWireframe, p1pp, p1mm, p1mp)
AddTriangle(OWireframe, p2pp, p2mp, p2mm)
AddTriangle(OWireframe, p2pp, p2mm, p2pm)
End Sub
Private Sub AddTriangle(ByVal OWireframe As MeshGeometry3D, ByVal point1 As Point3D, ByVal point2 As Point3D, ByVal point3 As Point3D)
'Create the points.
Dim index1 As Integer = OWireframe.Positions.Count
OWireframe.Positions.Add(point1)
OWireframe.Positions.Add(point2)
OWireframe.Positions.Add(point3)
'Create the triangle.
OWireframe.TriangleIndices.Add(index1)
index1 += 1
OWireframe.TriangleIndices.Add(index1)
index1 += 1
OWireframe.TriangleIndices.Add(index1)
End Sub
'Set the vector's length.
Private Function ScaleVector(ByVal vector As Vector3D, ByVal length As Double) As Vector3D
Dim scale As Double = length / vector.Length
Return New Vector3D(
vector.X * scale,
vector.Y * scale,
vector.Z * scale)
End Function
End Class |
Partager