This repository was archived by the owner on Dec 16, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.fs
524 lines (479 loc) · 25 KB
/
models.fs
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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
// This module is not part of the Graphing Calculator implementation.
// I'm using this code to develop the algorithms for the implementation.
// But, I'm going to leave this module in the project for it's value
// as code examples and prototyping area.
namespace GraphingCalculator
module Models =
open System
open System.Windows
open System.Windows.Controls
open System.Windows.Media
open System.Windows.Shapes
open System.IO
open System.Windows.Markup
open System.Windows.Controls
open System.Reflection
open System.Windows.Media.Imaging
open System.Windows.Media.Media3D
open Utilities
let testModel =
// from Model3DCollection Class example
// Define the lights cast in the scene. Without light, the 3D object cannot
// be seen. Note: to illuminate an object from additional directions, create
// additional lights.
let model = GeometryModel3D()
// The geometry specifes the shape of the 3D plane. In this sample, a flat sheet
// is created.
let geometry =
let meshGeometry = MeshGeometry3D()
// Create a collection of normal vectors for the MeshGeometry3D.
let normals =
let normalCollection = Vector3DCollection()
do normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection
// Create a collection of vertex positions for the MeshGeometry3D.
let positions =
let positionCollection = Point3DCollection()
do positionCollection.Add(new Point3D(-0.5,-0.5, 0.5))
positionCollection.Add(new Point3D( 0.5,-0.5, 0.5))
positionCollection.Add(new Point3D( 0.5, 0.5, 0.5))
positionCollection.Add(new Point3D( 0.5, 0.5, 0.5))
positionCollection.Add(new Point3D(-0.5, 0.5, 0.5))
positionCollection.Add(new Point3D(-0.5,-0.5, 0.5))
positionCollection
// Create a collection of texture coordinates for the MeshGeometry3D.
let textureCoordinates =
let textureCoordinatesCollection = PointCollection()
do textureCoordinatesCollection.Add(System.Windows.Point(0., 1.))
textureCoordinatesCollection.Add(System.Windows.Point(0., 1.))
textureCoordinatesCollection.Add(System.Windows.Point(0., 1.))
textureCoordinatesCollection.Add(System.Windows.Point(0., 1.))
textureCoordinatesCollection.Add(System.Windows.Point(0., 1.))
textureCoordinatesCollection.Add(System.Windows.Point(0., 1.))
textureCoordinatesCollection
// Create a collection of triangle indices for the MeshGeometry3D.
let triangleIndices =
let triangleIndicesCollection = Int32Collection()
do triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(5)
triangleIndicesCollection
do meshGeometry.Normals <- normals
meshGeometry.Positions <- positions
meshGeometry.TextureCoordinates <- textureCoordinates
meshGeometry.TriangleIndices <- triangleIndices
meshGeometry
// The material property of GeometryModel3D specifies the material applied to the 3D object.
// In this sample the material applied to the 3D object is made up of two materials layered
// on top of each other - a DiffuseMaterial (gradient brush) with an EmissiveMaterial
// layered on top (blue SolidColorBrush). The EmmisiveMaterial alters the appearance of
// the gradient toward blue
let material =
let linearGradiantBrush = LinearGradientBrush()
do linearGradiantBrush.StartPoint <- System.Windows.Point(0., 0.5)
linearGradiantBrush.EndPoint <- System.Windows.Point(1., 0.5)
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.Yellow, 0.0))
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.Red, 0.25))
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.Blue, 0.75))
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.LimeGreen, 1.0))
// Define material that will use the gradient.
let diffuseMaterial = DiffuseMaterial(linearGradiantBrush)
// Add this gradient to a MaterialGroup.
let materialGroup = MaterialGroup()
do materialGroup.Children.Add(diffuseMaterial)
// Define an Emissive Material with a blue brush.
let emissiveMaterial =
let c = Color.FromScRgb(1.f,255.f,0.f,0.f)
EmissiveMaterial(new SolidColorBrush(c))
do materialGroup.Children.Add(emissiveMaterial)
materialGroup
do model.Geometry <- geometry
model.Material <- material
model
let testModel3 =
let geometry =
let meshGeometry = MeshGeometry3D()
// Create a collection of normal vectors for the MeshGeometry3D.
let normals =
let normalCollection = Vector3DCollection()
do normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection
// Create a collection of vertex positions for the MeshGeometry3D.
let positions =
let positionCollection = Point3DCollection()
//Front
do positionCollection.Add(new Point3D(-0.5,-0.5, 1.0))
positionCollection.Add(new Point3D( 0.5,-0.5, 1.0))
positionCollection.Add(new Point3D( 0.5, 0.5, 1.0))
positionCollection.Add(new Point3D(-0.5, 0.5, 1.0))
//Back
positionCollection.Add(new Point3D(-1.0,-1.0,-1.0))
positionCollection.Add(new Point3D( 1.0,-1.0,-1.0))
positionCollection.Add(new Point3D( 1.0, 1.0,-1.0))
positionCollection.Add(new Point3D(-1.0, 1.0,-1.0))
positionCollection
// Create a collection of triangle indices for the MeshGeometry3D.
let triangleIndices =
let triangleIndicesCollection = Int32Collection()
//front
do triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(0)
//
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(7)
triangleIndicesCollection.Add(6)
//
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(2)
//
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(7)
//
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(5)
//
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(7)
triangleIndicesCollection.Add(4)
//
triangleIndicesCollection
do meshGeometry.Normals <- normals
meshGeometry.Positions <- positions
meshGeometry.TriangleIndices <- triangleIndices
meshGeometry
let material =
let linearGradiantBrush = LinearGradientBrush()
do linearGradiantBrush.StartPoint <- System.Windows.Point(0., 0.5)
linearGradiantBrush.EndPoint <- System.Windows.Point(1., 0.5)
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.YellowGreen, 0.0))
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.Red, 0.25))
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.Blue, 0.75))
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.Chocolate, 1.0))
// Define material that will use the gradient.
let diffuseMaterial = DiffuseMaterial(linearGradiantBrush)
// Add this gradient to a MaterialGroup.
let materialGroup = MaterialGroup()
do materialGroup.Children.Add(diffuseMaterial)
// Define an Emissive Material with a blue brush.
let emissiveMaterial =
let c = Colors.Chocolate
EmissiveMaterial(new SolidColorBrush(c))
do materialGroup.Children.Add(emissiveMaterial)
materialGroup
// Apply a transform to the object. In this sample, a rotation transform is applied,
// rendering the 3D object rotated.
let model = GeometryModel3D(geometry,material)
model
let material =
let linearGradiantBrush = LinearGradientBrush()
do linearGradiantBrush.StartPoint <- System.Windows.Point(0., 0.5)
linearGradiantBrush.EndPoint <- System.Windows.Point(1., 0.5)
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.Yellow, 0.0))
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.Red, 0.25))
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.Blue, 0.75))
linearGradiantBrush.GradientStops.Add(GradientStop(Colors.LimeGreen, 1.0))
// Define material that will use the gradient.
let diffuseMaterial = DiffuseMaterial(linearGradiantBrush)
// Add this gradient to a MaterialGroup.
let materialGroup = MaterialGroup()
do materialGroup.Children.Add(diffuseMaterial)
// Define an Emissive Material with a blue brush.
let emissiveMaterial =
let c = Color.FromScRgb(1.f,255.f,0.f,0.f)
EmissiveMaterial(new SolidColorBrush(c))
do materialGroup.Children.Add(emissiveMaterial)
materialGroup
let makeBar () =
let geometry =
let meshGeometry = MeshGeometry3D()
// Create a collection of normal vectors for the MeshGeometry3D.
let normals =
let normalCollection = Vector3DCollection()
do normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection
// Create a collection of vertex positions for the MeshGeometry3D.
let positions =
let positionCollection = Point3DCollection()
//Front
do positionCollection.Add(new Point3D(-0.05,-0.05, 1.0))
positionCollection.Add(new Point3D( 0.05,-0.05, 1.0))
positionCollection.Add(new Point3D( 0.05, 0.05, 1.0))
positionCollection.Add(new Point3D(-0.05, 0.05, 1.0))
//Back
positionCollection.Add(new Point3D(-0.05,-0.05, -1.0))
positionCollection.Add(new Point3D( 0.05,-0.05, -1.0))
positionCollection.Add(new Point3D( 0.05, 0.05, -1.0))
positionCollection.Add(new Point3D(-0.05, 0.05, -1.0))
positionCollection
// Create a collection of triangle indices for the MeshGeometry3D.
let triangleIndices =
let triangleIndicesCollection = Int32Collection()
//front
do triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(0)
//
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(7)
triangleIndicesCollection.Add(6)
//
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(2)
//
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(7)
//
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(5)
//
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(7)
triangleIndicesCollection.Add(4)
triangleIndicesCollection
do meshGeometry.Normals <- normals
meshGeometry.Positions <- positions
meshGeometry.TriangleIndices <- triangleIndices
meshGeometry
let model = GeometryModel3D(geometry,material)
model
let makeBox () =
let geometry =
let meshGeometry = MeshGeometry3D()
// Create a collection of normal vectors for the MeshGeometry3D.
let normals =
let normalCollection = Vector3DCollection()
do normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., 1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection.Add(Vector3D(0., 0., -1.))
normalCollection
// Create a collection of vertex positions for the MeshGeometry3D.
let positions =
let positionCollection = Point3DCollection()
//Front
do positionCollection.Add(new Point3D(-0.05,-0.05, 0.1))
positionCollection.Add(new Point3D( 0.05,-0.05, 0.1))
positionCollection.Add(new Point3D( 0.05, 0.05, 0.1))
positionCollection.Add(new Point3D(-0.05, 0.05, 0.1))
//Back
positionCollection.Add(new Point3D(-0.05,-0.05, -0.00))
positionCollection.Add(new Point3D( 0.05,-0.05, -0.00))
positionCollection.Add(new Point3D( 0.05, 0.05, -0.00))
positionCollection.Add(new Point3D(-0.05, 0.05, -0.00))
positionCollection
// Create a collection of triangle indices for the MeshGeometry3D.
let triangleIndices =
let triangleIndicesCollection = Int32Collection()
//front
do triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(0)
//
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(7)
triangleIndicesCollection.Add(6)
//
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(2)
//
triangleIndicesCollection.Add(2)
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(6)
triangleIndicesCollection.Add(7)
//
triangleIndicesCollection.Add(5)
triangleIndicesCollection.Add(1)
triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(5)
//
triangleIndicesCollection.Add(4)
triangleIndicesCollection.Add(0)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(3)
triangleIndicesCollection.Add(7)
triangleIndicesCollection.Add(4)
//
triangleIndicesCollection
do meshGeometry.Normals <- normals
meshGeometry.Positions <- positions
meshGeometry.TriangleIndices <- triangleIndices
meshGeometry
let model = GeometryModel3D(geometry,material)
model
let transformModel (model :GeometryModel3D) (v1 :Vector3D) (point :Point3D) =
let v2 =
let v = Vector3D(0.,0.,1.)
do v.Normalize()
v
let transforms = Transform3DGroup()
let translation = TranslateTransform3D(Vector3D(point.X,point.Y,point.Z))
let axis = Vector3D.CrossProduct (v1, v2)
let angle = axis.Length * (180./System.Math.PI)
let quaternion =
let q = (Quaternion(axis, angle))
do q.Normalize()
q
let rotation = QuaternionRotation3D(quaternion) |> RotateTransform3D
do transforms.Children.Add(rotation)
transforms.Children.Add(translation)
model.Transform <- transforms
model
let helix =
let x(t) = sin(t)
let y(t) = cos(t)
let z(t) = -t
let points0 = seq{for t in 0.0..0.01..25.0 -> Point3D(x(t), y(t), z(t))}
let points1 = seq{for t in 0.01..0.01..25.01 -> Point3D(x(t), y(t), z(t))}
let points = Seq.zip points0 points1
let normals =
seq{for p0, p1 in points
-> let vector = Vector3D( p1.X, p1.Y, p1.Z) - Vector3D( p0.X, p0.Y, p0.Z)
do vector.Normalize()
vector}
let pointsAndNormals = Seq.zip points0 normals
let models = seq{for p,n in pointsAndNormals ->
let model = makeBox ()
transformModel model n p}
let model3DGroup = Model3DGroup()
do Seq.iter (fun m -> model3DGroup.Children.Add(m)) models
model3DGroup
let surface =
// Surface example
(*let x(u,v) = u
let y(u,v) = v
let z(u,v) = sin(u**2. + v**2.)*)
// Torus example
(*let x(u,v) = sin(v)
let y(u,v) = (2.+cos(v))*sin(u)
let z(u,v) = (2.+cos(v))*cos(u)*)
// Sphere example
let x(u,v) = cos(u)*sin(v)
let y(_u,v) = -cos(v)
let z(u,v) = sin(-u)*sin(v)
let resolution = 0.1 * System.Math.PI
let uLowerLimit = 0.0
let uUpperLimit = 2.*System.Math.PI
let vLowerLimit = 0.0
let vUpperLimit = 2.*System.Math.PI
let points0 =
seq{for u in uLowerLimit..resolution..uUpperLimit do
for v in vLowerLimit..resolution..vUpperLimit -> Point3D(x(u, v), y(u, v), z(u, v))}
let points1 =
seq{for u in uLowerLimit..resolution..uUpperLimit do
for v in vLowerLimit..resolution..vUpperLimit -> Point3D(x(u, v + resolution), y(u, v + resolution), z(u, v + resolution))}
let points2 =
seq{for u in uLowerLimit..resolution..uUpperLimit do
for v in vLowerLimit..resolution..vUpperLimit -> Point3D(x(u + resolution, v + resolution), y(u + resolution, v + resolution), z(u + resolution, v + resolution))}
let points3 =
seq{for u in uLowerLimit..resolution..uUpperLimit do
for v in vLowerLimit..resolution..vUpperLimit -> Point3D(x(u + resolution, v), y(u + resolution, v), z(u + resolution, v))}
let points1 = Seq.zip3 points0 points1 points2
let points2 = Seq.zip3 points2 points3 points0
let meshGeometry = MeshGeometry3D()
do Seq.iter (fun n ->
let p1,p2,p3 = n
// Front
meshGeometry.Positions.Add(p3)
meshGeometry.Positions.Add(p2)
meshGeometry.Positions.Add(p1)
// back
meshGeometry.Positions.Add(p1)
meshGeometry.Positions.Add(p2)
meshGeometry.Positions.Add(p3)
) points1
do Seq.iter (fun n ->
let p1,p2,p3 = n
// Front
meshGeometry.Positions.Add(p3)
meshGeometry.Positions.Add(p2)
meshGeometry.Positions.Add(p1)
// Back
meshGeometry.Positions.Add(p1)
meshGeometry.Positions.Add(p2)
meshGeometry.Positions.Add(p3)
) points2
let model3D = GeometryModel3D(meshGeometry, material)
model3D