@@ -70,24 +70,55 @@ public partial class MainWindow : Window
70
70
/// </summary>
71
71
private const ColorImageFormat cFormat = ColorImageFormat . InfraredResolution640x480Fps30 ;
72
72
73
+ // stores furthest depth in the scene
73
74
public ushort greatestDepth = 0 ;
75
+
76
+ // array for all of the depth data
74
77
private int [ ] Depth = new int [ 320 * 240 ] ;
78
+
79
+ // stores all of the 3D trianlges with normals and points
75
80
Model3DGroup modelGroup = new Model3DGroup ( ) ;
81
+
82
+ // material placed over the mesh for viewing
76
83
public GeometryModel3D msheet = new GeometryModel3D ( ) ;
84
+
85
+ // collection of corners for the triangles
77
86
public Point3DCollection corners = new Point3DCollection ( ) ;
87
+
88
+ // collection of all the triangles
78
89
public Int32Collection Triangles = new Int32Collection ( ) ;
90
+
91
+
79
92
public MeshGeometry3D tmesh = new MeshGeometry3D ( ) ;
93
+
94
+ // collection of all the cross product normals
80
95
public Vector3DCollection Normals = new Vector3DCollection ( ) ;
96
+
97
+ // add texture to the mesh
81
98
public PointCollection myTextureCoordinatesCollection = new PointCollection ( ) ;
99
+
100
+ // storage for camera, scene, etc...
82
101
public ModelVisual3D modelsVisual = new ModelVisual3D ( ) ;
102
+
103
+
83
104
public Viewport3D myViewport = new Viewport3D ( ) ;
105
+
106
+ // test variable
84
107
public int samplespot ;
108
+
109
+ // variable for changing the quality 1 is the best 16 contains almost no data
85
110
public int s = 1 ;
86
111
112
+ // depth point collection
87
113
public int [ ] depths_array = new int [ 4 ] ;
114
+
115
+ // collection of points
88
116
Point3D [ ] points_array = new Point3D [ 4 ] ;
117
+
118
+ // collection of vectors
89
119
Vector3D [ ] vectors_array = new Vector3D [ 5 ] ;
90
120
121
+ //used for displaying RGB camera
91
122
public byte [ ] colorPixels ;
92
123
public WriteableBitmap colorBitmap ;
93
124
@@ -141,10 +172,16 @@ private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs
141
172
this . sensor . DepthFrameReady -= this . SensorDepthFrameReady ;
142
173
this . sensor . ColorFrameReady -= this . SensorColorFrameReady ;
143
174
}
144
-
175
+
176
+ // Empty the canvas
145
177
this . ClearMesh ( ) ;
146
178
}
147
179
180
+ /// <summary>
181
+ /// Handles adding a new kinect
182
+ /// </summary>
183
+ /// <param name="sender">object sending the event</param>
184
+ /// <param name="e">event arguments for the newly connected Kinect</param>
148
185
private void OnKinectSensorChanged ( object sender , KinectChangedEventArgs e )
149
186
{
150
187
// Check new sensor's status
@@ -169,7 +206,8 @@ private void OnKinectSensorChanged(object sender, KinectChangedEventArgs e)
169
206
}
170
207
171
208
if ( null == this . sensor )
172
- {
209
+ {
210
+ // if no kinect clear the text on screen
173
211
this . statusBarText . Content = Properties . Resources . NoKinectReady ;
174
212
this . IR_Title . Content = "" ;
175
213
this . Model_Title . Content = "" ;
@@ -284,7 +322,8 @@ void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
284
322
this . colorBitmap . PixelWidth * colorFrame . BytesPerPixel ,
285
323
0 ) ;
286
324
}
287
-
325
+
326
+ // set the RGB image to the RGB camera
288
327
this . KinectRGBView . Source = this . colorBitmap ;
289
328
290
329
}
@@ -310,11 +349,10 @@ void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
310
349
{
311
350
for ( int x = 0 ; x < 320 ; x ++ )
312
351
{
313
- // this.Depth[x + (y * 320)] = ((ushort)((pixelData[x + y * 320]) >> DepthImageFrame.PlayerIndexBitmaskWidth) / 100);
314
- //this.Depth[x + (y * 320)] = (this.Depth[x + (y * 320)] < minDepth) || (this.Depth[x + (y * 320)] > maxDepth) ? maxDepth : this.Depth[x + (y * 320)];
352
+ // scale depth down
315
353
this . Depth [ x + ( y * 320 ) ] = ( ( ushort ) pixelData [ x + y * 320 ] ) / 100 ;
316
- //this.Depth[x + (y * 320)] = this.Depth[x + (y * 320)] / 10;
317
354
355
+ // finds the furthest depth from all the depth pixels
318
356
if ( ( this . Depth [ x + y * 320 ] > this . greatestDepth ) && ( this . Depth [ x + y * 320 ] < maxDepth ) )
319
357
{
320
358
this . greatestDepth = ( ushort ) this . Depth [ x + y * 320 ] ;
@@ -323,7 +361,7 @@ void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
323
361
324
362
}
325
363
}
326
- // Blur Filter
364
+ // Blur Filter -- Guassian
327
365
if ( Filter_Blur . IsChecked == true )
328
366
{
329
367
for ( int i = 641 ; i < this . Depth . Length - 641 ; ++ i )
@@ -341,16 +379,17 @@ void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
341
379
}
342
380
}
343
381
382
+ // Set the depth image to the Depth sensor view
344
383
this . KinectDepthView . Source = DepthToBitmapSource ( imageFrame ) ;
345
384
}
346
385
}
347
386
348
387
349
388
/// <summary>
350
- /// Event handler for building the mesh
389
+ /// Flag check for a point within the bounding box
351
390
/// </summary>
352
- /// <param name="sender">object sending the event </param>
353
- /// <param name="e">event arguments </param>
391
+ /// <param name="x">location on the x plane </param>
392
+ /// <param name="y">location on the y plane </param>
354
393
private bool PointinRange ( int x , int y )
355
394
{
356
395
double minDepth = Near_Filter_Slider . Value ;
@@ -362,6 +401,9 @@ private bool PointinRange(int x, int y)
362
401
363
402
}
364
403
404
+ /// <summary>
405
+ /// Create the mesh
406
+ /// </summary>
365
407
void BuildMesh ( )
366
408
{
367
409
double maxDepth = Far_Filter_Slider . Value ;
@@ -409,31 +451,36 @@ void BuildMesh()
409
451
depths_array [ 3 ] = - this . Depth [ ( x + s ) + ( ( y + s ) * 320 ) ] ;
410
452
}
411
453
454
+ // triangle point locations
412
455
points_array [ 0 ] = new Point3D ( x , ( y + s ) , depths_array [ 0 ] ) ;
413
456
points_array [ 1 ] = new Point3D ( x , y , depths_array [ 1 ] ) ;
414
457
points_array [ 2 ] = new Point3D ( ( x + s ) , y , depths_array [ 2 ] ) ;
415
458
points_array [ 3 ] = new Point3D ( ( x + s ) , ( y + s ) , depths_array [ 3 ] ) ;
416
459
460
+ // create vectors of size difference between points
417
461
vectors_array [ 0 ] = new Vector3D ( points_array [ 1 ] . X - points_array [ 0 ] . X , points_array [ 1 ] . Y - points_array [ 0 ] . Y , points_array [ 1 ] . Z - points_array [ 0 ] . Z ) ;
418
462
vectors_array [ 1 ] = new Vector3D ( points_array [ 1 ] . X - points_array [ 2 ] . X , points_array [ 1 ] . Y - points_array [ 2 ] . Y , points_array [ 1 ] . Z - points_array [ 2 ] . Z ) ;
419
463
vectors_array [ 2 ] = new Vector3D ( points_array [ 2 ] . X - points_array [ 0 ] . X , points_array [ 2 ] . Y - points_array [ 0 ] . Y , points_array [ 2 ] . Z - points_array [ 0 ] . Z ) ;
420
464
vectors_array [ 3 ] = new Vector3D ( points_array [ 3 ] . X - points_array [ 0 ] . X , points_array [ 3 ] . Y - points_array [ 0 ] . Y , points_array [ 3 ] . Z - points_array [ 0 ] . Z ) ;
421
465
vectors_array [ 4 ] = new Vector3D ( points_array [ 2 ] . X - points_array [ 3 ] . X , points_array [ 2 ] . Y - points_array [ 3 ] . Y , points_array [ 2 ] . Z - points_array [ 3 ] . Z ) ;
422
466
467
+ // add the corners to the 2 triangles to form a square
423
468
corners . Add ( points_array [ 0 ] ) ;
424
469
corners . Add ( points_array [ 1 ] ) ;
425
470
corners . Add ( points_array [ 2 ] ) ;
426
471
corners . Add ( points_array [ 2 ] ) ;
427
472
corners . Add ( points_array [ 3 ] ) ;
428
473
corners . Add ( points_array [ 0 ] ) ;
429
474
475
+ // add triangles to the collection
430
476
Triangles . Add ( i ) ;
431
477
Triangles . Add ( i + 1 ) ;
432
478
Triangles . Add ( i + 2 ) ;
433
479
Triangles . Add ( i + 3 ) ;
434
480
Triangles . Add ( i + 4 ) ;
435
481
Triangles . Add ( i + 5 ) ;
436
482
483
+ // find the normals of the triangles by taking the cross product
437
484
Normals . Add ( Vector3D . CrossProduct ( vectors_array [ 0 ] , vectors_array [ 2 ] ) ) ;
438
485
Normals . Add ( Vector3D . CrossProduct ( vectors_array [ 0 ] , vectors_array [ 1 ] ) ) ;
439
486
Normals . Add ( Vector3D . CrossProduct ( vectors_array [ 1 ] , vectors_array [ 2 ] ) ) ;
@@ -447,6 +494,7 @@ void BuildMesh()
447
494
}
448
495
}
449
496
497
+ // add the flat back wall
450
498
int numcorners = corners . Count ;
451
499
for ( int p = 0 ; p < numcorners ; p ++ )
452
500
{
@@ -460,6 +508,10 @@ void BuildMesh()
460
508
461
509
}
462
510
511
+ /// <summary>
512
+ /// Create depth image from depth frame
513
+ /// </summary>
514
+ /// <param name="imageFrame">collection of depth data</param>
463
515
BitmapSource DepthToBitmapSource ( DepthImageFrame imageFrame )
464
516
{
465
517
short [ ] pixelData = new short [ imageFrame . PixelDataLength ] ;
@@ -475,28 +527,41 @@ BitmapSource DepthToBitmapSource(DepthImageFrame imageFrame)
475
527
return bmap ;
476
528
}
477
529
530
+ /// <summary>
531
+ /// take a photo when button is clicked
532
+ /// </summary>
533
+ /// <param name="sender">object sending the event</param>
534
+ /// <param name="e">event arguments</param>
478
535
private void Begin_Scan_Click ( object sender , RoutedEventArgs e )
479
536
{
537
+ //clear the canvas
480
538
this . ClearMesh ( ) ;
481
539
540
+ // add light to the scene
482
541
DirectionalLight DirLight1 = new DirectionalLight ( ) ;
483
542
DirLight1 . Color = Colors . White ;
484
543
DirLight1 . Direction = new Vector3D ( 0 , 0 , - 1 ) ;
544
+
545
+ // add a camera to the scene
485
546
PerspectiveCamera Camera1 = new PerspectiveCamera ( ) ;
486
547
548
+ // set the location of the camera
487
549
Camera1 . Position = new Point3D ( 160 , 120 , 480 ) ;
488
550
Camera1 . LookDirection = new Vector3D ( 0 , 0 , - 1 ) ;
489
551
Camera1 . UpDirection = new Vector3D ( 0 , - 1 , 0 ) ;
490
552
553
+ // create the mesh from depth data
491
554
this . BuildMesh ( ) ;
492
555
556
+ // add texture to all the points
493
557
tmesh . Positions = corners ;
494
558
tmesh . TriangleIndices = Triangles ;
495
559
tmesh . Normals = Normals ;
496
560
tmesh . TextureCoordinates = myTextureCoordinatesCollection ;
497
561
msheet . Geometry = tmesh ;
498
562
msheet . Material = new DiffuseMaterial ( ( SolidColorBrush ) ( new BrushConverter ( ) . ConvertFrom ( "#52318F" ) ) ) ;
499
563
564
+ // build the scene and display it
500
565
this . modelGroup . Children . Add ( msheet ) ;
501
566
this . modelGroup . Children . Add ( DirLight1 ) ;
502
567
this . modelsVisual . Content = this . modelGroup ;
@@ -511,15 +576,22 @@ private void Begin_Scan_Click(object sender, RoutedEventArgs e)
511
576
512
577
}
513
578
579
+ /// <summary>
580
+ /// Export the completed mesh to a .obj file
581
+ /// </summary>
582
+ /// <param name="sender">object sending the event</param>
583
+ /// <param name="e">event arguments</param>
514
584
private void Export_Model_Click ( object sender , RoutedEventArgs e )
515
585
{
586
+ //function from Helix Toolkit
516
587
string fileName = Model_Name . Text + ".obj" ;
517
588
518
589
using ( var exporter = new ObjExporter ( fileName ) )
519
590
{
520
591
exporter . Export ( this . modelGroup ) ;
521
592
}
522
593
594
+ // test code for seeing depth frame values
523
595
Process . Start ( "explorer.exe" , "/select,\" " + fileName + "\" " ) ;
524
596
525
597
string fileName2 = "depth.txt" ;
@@ -545,6 +617,9 @@ private void ShowStatusMessage(string message)
545
617
} ) ) ;
546
618
}
547
619
620
+ /// <summary>
621
+ /// clear everything from the scene and canvas
622
+ /// </summary>
548
623
public void ClearMesh ( )
549
624
{
550
625
KinectNormalView . Children . Clear ( ) ;
@@ -558,6 +633,11 @@ public void ClearMesh()
558
633
559
634
}
560
635
636
+ /// <summary>
637
+ /// Clear canvas button click
638
+ /// </summary>
639
+ /// <param name="sender">object sending the event</param>
640
+ /// <param name="e">event arguments</param>
561
641
private void End_Scan_Click ( object sender , RoutedEventArgs e )
562
642
{
563
643
this . ClearMesh ( ) ;
0 commit comments