private static void TestPointCloud() { System.IO.Directory.CreateDirectory(@"..\..\testData\Output"); // TEST DotNet3d.ply from DotNet3d.Pointcloud { var ply = new DotNet3d.Ply(@"..\..\testData\Frame_00600.ply"); // ~200ms var pc = ply.ToPointCloud(); // ~1ms var moveToOrigin = DotNet3d.Matrix.Translation(-722.58, 123.87, -2805.85); // ~1ms var rotateX90 = DotNet3d.Matrix.RotationX(90); // ~1ms var rotateY180 = DotNet3d.Matrix.RotationY(180); // ~1ms var rotateZ30 = DotNet3d.Matrix.RotationZ(30); // ~1ms var superMatrix = rotateZ30 * rotateY180 * rotateX90 * moveToOrigin; // ~1ms pc.Multiply(superMatrix); // ~1400ms var ply2 = new DotNet3d.Ply(pc); // ~1ms ply2.SaveBinary(@"..\..\testData\Output\Frame_00600-ONFLOOR-FACINGZ.ply"); // ~1100ms } // Test writing matrix as ply { var mat = new DotNet3d.Matrix(); var ply = new DotNet3d.Ply(mat); ply.SaveAscii(@"..\..\testData\Output\IdentityMatrix.ply"); } // Test writing array of matrices as ply { var mat1 = DotNet3d.Matrix.Identity(); var mat2 = DotNet3d.Matrix.Identity(); mat2.Translate(1000, 1000, 1000); var matArr = new DotNet3d.Matrix[2]; matArr[0] = mat1; matArr[1] = mat2; var ply = new DotNet3d.Ply(matArr); ply.SaveBinary(@"..\..\testData\Output\2Matices.ply"); } // Test seting, getting { float x = 1; float y = 2; float z = 3; var mat = DotNet3d.Matrix.Translation(x, y, z); var ply = new DotNet3d.Ply(mat); var pc = ply.ToPointCloud(); Trace.Assert((float)pc.Get("x", 0) == x); Trace.Assert((float)pc.Get("y", 0) == y); Trace.Assert((float)pc.Get("z", 0) == z); Trace.Assert((float)pc.Get("x", 1) == (x + 500)); Trace.Assert((float)pc.Get("y", 1) == y); Trace.Assert((float)pc.Get("z", 1) == z); Trace.Assert((float)pc.Get("x", 2) == x); Trace.Assert((float)pc.Get("y", 2) == (y + 500)); Trace.Assert((float)pc.Get("z", 2) == z); Trace.Assert((float)pc.Get("x", 3) == x); Trace.Assert((float)pc.Get("y", 3) == y); Trace.Assert((float)pc.Get("z", 3) == (z + 1000)); float dummy = 9; pc.Set("x", 0, dummy); Trace.Assert((float)pc.Get("x", 0) == dummy); byte dummy2 = 32; pc.Set("red", 1, dummy2); Trace.Assert((byte)pc.Get("red", 1) == dummy2); } // Test dividing by z { float x = 2; float y = 4; float z = 8; var mat = DotNet3d.Matrix.Translation(x, y, z); var ply = new DotNet3d.Ply(mat); var pc = ply.ToPointCloud(); Trace.Assert((float)pc.Get("x", 0) == x); Trace.Assert((float)pc.Get("y", 0) == y); Trace.Assert((float)pc.Get("z", 0) == z); pc.DividePositionByZ(); Trace.Assert((float)pc.Get("x", 0) == x / z); Trace.Assert((float)pc.Get("y", 0) == y / z); Trace.Assert((float)pc.Get("z", 0) == z / z); } // Test drawing on image { int size = 256; var pointArr = new DotNet3d.Point[size]; for (int i = 0; i < size; i++) { pointArr[i] = new DotNet3d.Point(); pointArr[i].X = i; pointArr[i].Y = i; pointArr[i].Z = i; } var pc = new DotNet3d.PointCloud(pointArr); var newImg = new System.Drawing.Bitmap(size, size); var src = System.IO.Path.GetFullPath(@"..\..\testData\Output\TestDrawOnImage-EMPTY.png"); var dst = System.IO.Path.GetFullPath(@"..\..\testData\Output\TestDrawOnImage-LINE.png"); newImg.Save(src); // src: black 256x256 image pc.DrawOnImage(src, dst); // dst: black 256x256 with red line from upper left to lower right } // Test drawing on image, 8-bit index { int size = 3840; var pointArr = new DotNet3d.Point[size]; for (int i = 0; i < size; i++) { pointArr[i] = new DotNet3d.Point(); pointArr[i].X = i; pointArr[i].Y = i; pointArr[i].Z = i; } var pc = new DotNet3d.PointCloud(pointArr); var src = System.IO.Path.GetFullPath(@"..\..\testData\Mask-Cam0000-F00001-8bitIndexed.png"); var dst = System.IO.Path.GetFullPath(@"..\..\testData\Output\Mask-Cam0000-F00001-8bitIndexed-LINE.png"); pc.DrawOnImage(src, dst); // dst: 5120x3840 mask with white image of person, with red line that starts at upper left and ends at bottom right (if bottom right were square) } // Test adding point clouds { float x1 = 2; float y1 = 4; float z1 = 8; var mat1 = DotNet3d.Matrix.Translation(x1, y1, z1); var ply1 = new DotNet3d.Ply(mat1); var one = ply1.ToPointCloud(); float x2 = 3; float y2 = 6; float z2 = 9; var mat2 = DotNet3d.Matrix.Translation(x2, y2, z2); var ply2 = new DotNet3d.Ply(mat2); var two = ply2.ToPointCloud(); DotNet3d.PointCloud three = one + two; Trace.Assert(three.ToString() == (one.ToString() + two.ToString())); Trace.Assert(three.Data.Length == (one.Data.Length + two.Data.Length)); for (int i = 0; i < one.Data.Length; i++) { Trace.Assert(three.Data[i] == one.Data[i]); int j = i + one.Data.Length; // second half of data Trace.Assert(three.Data[j] == two.Data[i]); } var ply3 = new DotNet3d.Ply(three); ply3.SaveAscii(@"..\..\testData\Output\AddingPointClouds-Ascii.ply"); } // Test multiply point cloud by a matrix { DotNet3d.Ply ply = new DotNet3d.Ply(DotNet3d.Matrix.Identity()); DotNet3d.PointCloud pc = ply.ToPointCloud(); Trace.Assert(pc.ToString() == "0 0 0 0 0 0 255\n500 0 0 255 0 0 255\n0 500 0 0 255 0 255\n0 0 1000 0 0 255 255\n"); pc.Multiply(DotNet3d.Matrix.Scaling(2, 2, 2)); Trace.Assert(pc.ToString() == "0 0 0 0 0 0 255\n1000 0 0 255 0 0 255\n0 1000 0 0 255 0 255\n0 0 2000 0 0 255 255\n"); // test point cloud with normals int numPoints = 1; var dotNetTypes = new List <string> { "System.Single", "System.Single", "System.Single", "System.Single", "System.Single", "System.Single" }; var names = new List <string> { "x", "y", "z", "nx", "ny", "nz" }; int numBytes = DotNet3d.Utilities.TotalBytes(dotNetTypes); var data = new byte[numBytes]; int index = 0; index += DotNet3d.Utilities.NumToBytes((float)1, data, index); index += DotNet3d.Utilities.NumToBytes((float)2, data, index); index += DotNet3d.Utilities.NumToBytes((float)3, data, index); index += DotNet3d.Utilities.NumToBytes((float)4, data, index); index += DotNet3d.Utilities.NumToBytes((float)5, data, index); index += DotNet3d.Utilities.NumToBytes((float)6, data, index); var pcWithNormals = new DotNet3d.PointCloud(numPoints, dotNetTypes, names, data); var trans = DotNet3d.Matrix.Translation(10, 10, 10); var scale = DotNet3d.Matrix.Scaling(2, 2, 2); var super = scale * trans; pcWithNormals.Multiply(super); Trace.Assert(pcWithNormals.ToString() == "22 24 26 8 10 12\n"); } }
private static void TestPly() { System.IO.Directory.CreateDirectory(@"..\..\testData\Output"); var plyAscii = new DotNet3d.Ply(@"..\..\testData\CalibrationXml-Ascii-UnixLineEndings.ply"); var PointCloudAscii = plyAscii.ToPointCloud(); var asciiTxt = PointCloudAscii.ToString(); var plyBinary = new DotNet3d.Ply(@"..\..\testData\CalibrationXml-Binary.ply"); var PointCloudBinary = plyBinary.ToPointCloud(); var binaryTxt = PointCloudBinary.ToString(); Trace.Assert(asciiTxt == binaryTxt); var plyAsciiWinLineEndings = new DotNet3d.Ply(@"..\..\testData\CalibrationXml-Ascii-WinLineEndings.ply"); var PointCloudAsciiWinLineEndings = plyAsciiWinLineEndings.ToPointCloud(); var asciiWinLineEndingsTxt = PointCloudAsciiWinLineEndings.ToString(); Trace.Assert(asciiTxt == asciiWinLineEndingsTxt); plyAscii.SaveAscii(@"..\..\testData\Output\CalibrationXml-Ascii-ToAscii.ply"); plyAscii.SaveBinary(@"..\..\testData\Output\CalibrationXml-Ascii-ToBinary.ply"); plyBinary.SaveAscii(@"..\..\testData\Output\CalibrationXml-Binary-ToAscii.ply"); plyBinary.SaveBinary(@"..\..\testData\Output\CalibrationXml-Binary-ToBinary.ply"); // array of points { var ptArr = new DotNet3d.Point[3]; ptArr[0] = new DotNet3d.Point(0, 0, 0); ptArr[1] = new DotNet3d.Point(1, 1, 1); ptArr[2] = new DotNet3d.Point(-1, -1, -1); var ply = new DotNet3d.Ply(ptArr); var txt = "ply\n" + "format ascii 1.0\n" + "comment Created by DotNet3d\n" + "element vertex 3\n" + "property float x\n" + "property float y\n" + "property float z\n" + "end_header\n" + "0 0 0\n" + "1 1 1\n" + "-1 -1 -1\n"; Trace.Assert(ply.ToString() == txt); } // point from strings { var pt = new DotNet3d.Point("1 2 3"); Trace.Assert(pt.ToString() == "(1 2 3)\n"); pt = new DotNet3d.Point("2, 4, 6"); Trace.Assert(pt.ToString() == "(2 4 6)\n"); pt = new DotNet3d.Point("3\t6\t9"); Trace.Assert(pt.ToString() == "(3 6 9)\n"); pt = new DotNet3d.Point("(4, 8, 12)"); Trace.Assert(pt.ToString() == "(4 8 12)\n"); } // Test loading binary (if this takes longer than a second to load, this consider this test a failure) { Stopwatch sw = new Stopwatch(); sw.Start(); var ply = new DotNet3d.Ply(@"..\..\testData\BIN-color_to_depth_Frame1.ply"); sw.Stop(); long oneSec = 1000; // NOTE: This takes about 2 ms on my home pc Trace.Assert(sw.ElapsedMilliseconds < oneSec); } // Test loading ascii (if this takes longer than a second to load, this consider this test a failure) { Stopwatch sw = new Stopwatch(); sw.Start(); var ply = new DotNet3d.Ply(@"..\..\testData\color_to_depth_Frame1.ply"); sw.Stop(); long tenSeconds = 10 * 1000; // NOTE: This once took 853414 ms (14 min), now it takes about 5 seconds on my home pc in debug mode Trace.Assert(sw.ElapsedMilliseconds < tenSeconds); } }