Skip to content

Commit 4bfd095

Browse files
committed
Final work on KinectScan code cleanup and comments
1 parent 1ecd655 commit 4bfd095

File tree

1 file changed

+90
-10
lines changed

1 file changed

+90
-10
lines changed

MainWindow.xaml.cs

+90-10
Original file line numberDiff line numberDiff line change
@@ -70,24 +70,55 @@ public partial class MainWindow : Window
7070
/// </summary>
7171
private const ColorImageFormat cFormat = ColorImageFormat.InfraredResolution640x480Fps30;
7272

73+
// stores furthest depth in the scene
7374
public ushort greatestDepth = 0;
75+
76+
// array for all of the depth data
7477
private int[] Depth = new int[320 * 240];
78+
79+
// stores all of the 3D trianlges with normals and points
7580
Model3DGroup modelGroup = new Model3DGroup();
81+
82+
// material placed over the mesh for viewing
7683
public GeometryModel3D msheet = new GeometryModel3D();
84+
85+
// collection of corners for the triangles
7786
public Point3DCollection corners = new Point3DCollection();
87+
88+
// collection of all the triangles
7889
public Int32Collection Triangles = new Int32Collection();
90+
91+
7992
public MeshGeometry3D tmesh = new MeshGeometry3D();
93+
94+
// collection of all the cross product normals
8095
public Vector3DCollection Normals = new Vector3DCollection();
96+
97+
// add texture to the mesh
8198
public PointCollection myTextureCoordinatesCollection = new PointCollection();
99+
100+
// storage for camera, scene, etc...
82101
public ModelVisual3D modelsVisual = new ModelVisual3D();
102+
103+
83104
public Viewport3D myViewport = new Viewport3D();
105+
106+
// test variable
84107
public int samplespot;
108+
109+
// variable for changing the quality 1 is the best 16 contains almost no data
85110
public int s = 1;
86111

112+
// depth point collection
87113
public int[] depths_array = new int[4];
114+
115+
// collection of points
88116
Point3D[] points_array = new Point3D[4];
117+
118+
// collection of vectors
89119
Vector3D[] vectors_array = new Vector3D[5];
90120

121+
//used for displaying RGB camera
91122
public byte[] colorPixels;
92123
public WriteableBitmap colorBitmap;
93124

@@ -141,10 +172,16 @@ private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs
141172
this.sensor.DepthFrameReady -= this.SensorDepthFrameReady;
142173
this.sensor.ColorFrameReady -= this.SensorColorFrameReady;
143174
}
144-
175+
176+
// Empty the canvas
145177
this.ClearMesh();
146178
}
147179

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>
148185
private void OnKinectSensorChanged(object sender, KinectChangedEventArgs e)
149186
{
150187
// Check new sensor's status
@@ -169,7 +206,8 @@ private void OnKinectSensorChanged(object sender, KinectChangedEventArgs e)
169206
}
170207

171208
if (null == this.sensor)
172-
{
209+
{
210+
// if no kinect clear the text on screen
173211
this.statusBarText.Content = Properties.Resources.NoKinectReady;
174212
this.IR_Title.Content = "";
175213
this.Model_Title.Content = "";
@@ -284,7 +322,8 @@ void SensorColorFrameReady(object sender, ColorImageFrameReadyEventArgs e)
284322
this.colorBitmap.PixelWidth * colorFrame.BytesPerPixel,
285323
0);
286324
}
287-
325+
326+
// set the RGB image to the RGB camera
288327
this.KinectRGBView.Source = this.colorBitmap;
289328

290329
}
@@ -310,11 +349,10 @@ void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
310349
{
311350
for (int x = 0; x < 320; x++)
312351
{
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
315353
this.Depth[x + (y * 320)] = ((ushort)pixelData[x + y * 320]) / 100;
316-
//this.Depth[x + (y * 320)] = this.Depth[x + (y * 320)] / 10;
317354

355+
// finds the furthest depth from all the depth pixels
318356
if ((this.Depth[x + y * 320] > this.greatestDepth) && (this.Depth[x + y * 320] < maxDepth))
319357
{
320358
this.greatestDepth = (ushort)this.Depth[x + y * 320];
@@ -323,7 +361,7 @@ void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
323361

324362
}
325363
}
326-
// Blur Filter
364+
// Blur Filter -- Guassian
327365
if (Filter_Blur.IsChecked == true)
328366
{
329367
for (int i = 641; i < this.Depth.Length - 641; ++i)
@@ -341,16 +379,17 @@ void SensorDepthFrameReady(object sender, DepthImageFrameReadyEventArgs e)
341379
}
342380
}
343381

382+
// Set the depth image to the Depth sensor view
344383
this.KinectDepthView.Source = DepthToBitmapSource(imageFrame);
345384
}
346385
}
347386

348387

349388
/// <summary>
350-
/// Event handler for building the mesh
389+
/// Flag check for a point within the bounding box
351390
/// </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>
354393
private bool PointinRange(int x, int y)
355394
{
356395
double minDepth = Near_Filter_Slider.Value;
@@ -362,6 +401,9 @@ private bool PointinRange(int x, int y)
362401

363402
}
364403

404+
/// <summary>
405+
/// Create the mesh
406+
/// </summary>
365407
void BuildMesh()
366408
{
367409
double maxDepth = Far_Filter_Slider.Value;
@@ -409,31 +451,36 @@ void BuildMesh()
409451
depths_array[3] = -this.Depth[(x + s) + ((y + s) * 320)];
410452
}
411453

454+
// triangle point locations
412455
points_array[0] = new Point3D(x, (y + s), depths_array[0]);
413456
points_array[1] = new Point3D(x, y, depths_array[1]);
414457
points_array[2] = new Point3D((x + s), y, depths_array[2]);
415458
points_array[3] = new Point3D((x + s), (y + s), depths_array[3]);
416459

460+
// create vectors of size difference between points
417461
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);
418462
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);
419463
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);
420464
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);
421465
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);
422466

467+
// add the corners to the 2 triangles to form a square
423468
corners.Add(points_array[0]);
424469
corners.Add(points_array[1]);
425470
corners.Add(points_array[2]);
426471
corners.Add(points_array[2]);
427472
corners.Add(points_array[3]);
428473
corners.Add(points_array[0]);
429474

475+
// add triangles to the collection
430476
Triangles.Add(i);
431477
Triangles.Add(i + 1);
432478
Triangles.Add(i + 2);
433479
Triangles.Add(i + 3);
434480
Triangles.Add(i + 4);
435481
Triangles.Add(i + 5);
436482

483+
// find the normals of the triangles by taking the cross product
437484
Normals.Add(Vector3D.CrossProduct(vectors_array[0], vectors_array[2]));
438485
Normals.Add(Vector3D.CrossProduct(vectors_array[0], vectors_array[1]));
439486
Normals.Add(Vector3D.CrossProduct(vectors_array[1], vectors_array[2]));
@@ -447,6 +494,7 @@ void BuildMesh()
447494
}
448495
}
449496

497+
// add the flat back wall
450498
int numcorners = corners.Count;
451499
for (int p = 0; p < numcorners; p++)
452500
{
@@ -460,6 +508,10 @@ void BuildMesh()
460508

461509
}
462510

511+
/// <summary>
512+
/// Create depth image from depth frame
513+
/// </summary>
514+
/// <param name="imageFrame">collection of depth data</param>
463515
BitmapSource DepthToBitmapSource(DepthImageFrame imageFrame)
464516
{
465517
short[] pixelData = new short[imageFrame.PixelDataLength];
@@ -475,28 +527,41 @@ BitmapSource DepthToBitmapSource(DepthImageFrame imageFrame)
475527
return bmap;
476528
}
477529

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>
478535
private void Begin_Scan_Click(object sender, RoutedEventArgs e)
479536
{
537+
//clear the canvas
480538
this.ClearMesh();
481539

540+
// add light to the scene
482541
DirectionalLight DirLight1 = new DirectionalLight();
483542
DirLight1.Color = Colors.White;
484543
DirLight1.Direction = new Vector3D(0, 0, -1);
544+
545+
// add a camera to the scene
485546
PerspectiveCamera Camera1 = new PerspectiveCamera();
486547

548+
// set the location of the camera
487549
Camera1.Position = new Point3D(160, 120, 480);
488550
Camera1.LookDirection = new Vector3D(0, 0, -1);
489551
Camera1.UpDirection = new Vector3D(0, -1, 0);
490552

553+
// create the mesh from depth data
491554
this.BuildMesh();
492555

556+
// add texture to all the points
493557
tmesh.Positions = corners;
494558
tmesh.TriangleIndices = Triangles;
495559
tmesh.Normals = Normals;
496560
tmesh.TextureCoordinates = myTextureCoordinatesCollection;
497561
msheet.Geometry = tmesh;
498562
msheet.Material = new DiffuseMaterial((SolidColorBrush)(new BrushConverter().ConvertFrom("#52318F")));
499563

564+
// build the scene and display it
500565
this.modelGroup.Children.Add(msheet);
501566
this.modelGroup.Children.Add(DirLight1);
502567
this.modelsVisual.Content = this.modelGroup;
@@ -511,15 +576,22 @@ private void Begin_Scan_Click(object sender, RoutedEventArgs e)
511576

512577
}
513578

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>
514584
private void Export_Model_Click(object sender, RoutedEventArgs e)
515585
{
586+
//function from Helix Toolkit
516587
string fileName = Model_Name.Text + ".obj";
517588

518589
using (var exporter = new ObjExporter(fileName))
519590
{
520591
exporter.Export(this.modelGroup);
521592
}
522593

594+
// test code for seeing depth frame values
523595
Process.Start("explorer.exe", "/select,\"" + fileName + "\"");
524596

525597
string fileName2 = "depth.txt";
@@ -545,6 +617,9 @@ private void ShowStatusMessage(string message)
545617
}));
546618
}
547619

620+
/// <summary>
621+
/// clear everything from the scene and canvas
622+
/// </summary>
548623
public void ClearMesh()
549624
{
550625
KinectNormalView.Children.Clear();
@@ -558,6 +633,11 @@ public void ClearMesh()
558633

559634
}
560635

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>
561641
private void End_Scan_Click(object sender, RoutedEventArgs e)
562642
{
563643
this.ClearMesh();

0 commit comments

Comments
 (0)