Beispiel #1
0
        public void SetSize(SimpleMeshCollection simpleMeshCollection)
        {
            minXYZ_um = simpleMeshCollection.MinXYZ_um();
            maxXYZ_um = simpleMeshCollection.MaxXYZ_um();

            size_um = maxXYZ_um - minXYZ_um;
        }
Beispiel #2
0
        private static bool loadModelSTL_binary(SimpleMeshCollection simpleModel, string filename, Matrix4X4 matrix)
        {
            SimpleMesh vol = new SimpleMesh();

            using (FileStream stlStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            {
                // load it as a binary stl
                // skip the first 80 bytes
                // read in the number of triangles
                stlStream.Position = 0;
                BinaryReader br              = new BinaryReader(stlStream);
                byte[]       fileContents    = br.ReadBytes((int)stlStream.Length);
                int          currentPosition = 80;
                if (fileContents.Length < currentPosition)
                {
                    return(false);
                }
                uint numTriangles      = System.BitConverter.ToUInt32(fileContents, currentPosition);
                long bytesForNormals   = numTriangles * 3 * 4;
                long bytesForVertices  = numTriangles * 3 * 4;
                long bytesForAttributs = numTriangles * 2;
                currentPosition += 4;
                long numBytesRequiredForVertexData = currentPosition + bytesForNormals + bytesForVertices + bytesForAttributs;
                if (fileContents.Length < numBytesRequiredForVertexData || numTriangles < 0)
                {
                    stlStream.Close();
                    return(false);
                }

                IntPoint[] vector = new IntPoint[3];
                for (int i = 0; i < numTriangles; i++)
                {
                    // skip the normal
                    currentPosition += 3 * 4;
                    for (int j = 0; j < 3; j++)
                    {
                        var vertex = new MatterHackers.VectorMath.Vector3(
                            System.BitConverter.ToSingle(fileContents, currentPosition + 0 * 4),
                            System.BitConverter.ToSingle(fileContents, currentPosition + 1 * 4),
                            System.BitConverter.ToSingle(fileContents, currentPosition + 2 * 4));

                        var new0 = VectorMath.Vector3.Transform(vertex, matrix);
                        vector[j]        = new IntPoint(new0.X * 1000, new0.Y * 1000, new0.Z * 1000);
                        currentPosition += 3 * 4;
                    }
                    currentPosition += 2;                     // skip the attribute

                    vol.addFaceTriangle(vector[2], vector[1], vector[0]);
                }
            }

            if (vol.faceTriangles.Count > 0)
            {
                simpleModel.SimpleMeshes.Add(vol);
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        public static bool LoadModelFromFile(SimpleMeshCollection simpleModel, string filename, Matrix4X4 matrix)
        {
            if (!loadModelSTL_ascii(simpleModel, filename, matrix))
            {
                return(loadModelSTL_binary(simpleModel, filename, matrix));
            }

            return(true);
        }
Beispiel #4
0
        public static bool LoadModelFromStream(SimpleMeshCollection simpleModel, Stream stream, Matrix4X4 matrix)
        {
            if (!loadModelSTLStream_ascii(simpleModel, stream, matrix))
            {
                stream.Position = 0;
                return(loadModelSTLStream_binary(simpleModel, stream, matrix));
            }

            return(true);
        }
 public OptimizedMeshCollection(SimpleMeshCollection simpleMeshCollection)
 {
     for (int simpleMeshIndex = 0; simpleMeshIndex < simpleMeshCollection.SimpleMeshes.Count; simpleMeshIndex++)
     {
         OptimizedMeshes.Add(new OptimizedMesh(simpleMeshCollection.SimpleMeshes[simpleMeshIndex], this));
         if (MatterSlice.Canceled)
         {
             return;
         }
     }
 }
Beispiel #6
0
        public OptimizedMeshCollection(SimpleMeshCollection simpleMeshCollection)
        {
            for (int simpleMeshIndex = 0; simpleMeshIndex < simpleMeshCollection.SimpleMeshes.Count; simpleMeshIndex++)
            {
                OptimizedMeshes.Add(null);
            }

            Agg.Parallel.For(0, simpleMeshCollection.SimpleMeshes.Count, (simpleMeshIndex) =>
                             // for (int simpleMeshIndex = 0; simpleMeshIndex < simpleMeshCollection.SimpleMeshes.Count; simpleMeshIndex++)
            {
                OptimizedMeshes[simpleMeshIndex] = new OptimizedMesh(simpleMeshCollection.SimpleMeshes[simpleMeshIndex], this);
                if (MatterSlice.Canceled)
                {
                    return;
                }
            });
        }
        public void SetPositionAndSize(SimpleMeshCollection simpleMeshCollection, long xCenter_um, long yCenter_um, long zClip_um, bool centerObjectInXy)
        {
            minXYZ_um = simpleMeshCollection.minXYZ_um();
            maxXYZ_um = simpleMeshCollection.maxXYZ_um();

            if (centerObjectInXy)
            {
                IntPoint modelXYCenterZBottom_um = new IntPoint((minXYZ_um.X + maxXYZ_um.X) / 2, (minXYZ_um.Y + maxXYZ_um.Y) / 2, minXYZ_um.Z);
                modelXYCenterZBottom_um -= new IntPoint(xCenter_um, yCenter_um, zClip_um);
                for (int optimizedMeshIndex = 0; optimizedMeshIndex < OptimizedMeshes.Count; optimizedMeshIndex++)
                {
                    for (int n = 0; n < OptimizedMeshes[optimizedMeshIndex].vertices.Count; n++)
                    {
                        OptimizedMeshes[optimizedMeshIndex].vertices[n].position -= modelXYCenterZBottom_um;
                    }
                }

                minXYZ_um -= modelXYCenterZBottom_um;
                maxXYZ_um -= modelXYCenterZBottom_um;
            }
            else             // we still need to put in the bottom clip
            {
                // Offset by bed center and correctly position in z
                IntPoint modelZBottom_um = new IntPoint(0, 0, minXYZ_um.Z - zClip_um);
                for (int optimizedMeshIndex = 0; optimizedMeshIndex < OptimizedMeshes.Count; optimizedMeshIndex++)
                {
                    for (int vertexIndex = 0; vertexIndex < OptimizedMeshes[optimizedMeshIndex].vertices.Count; vertexIndex++)
                    {
                        OptimizedMeshes[optimizedMeshIndex].vertices[vertexIndex].position -= modelZBottom_um;
                    }
                }

                minXYZ_um -= modelZBottom_um;
                maxXYZ_um -= modelZBottom_um;
            }

            size_um = maxXYZ_um - minXYZ_um;
        }
Beispiel #8
0
        public static bool loadModelSTL_ascii(SimpleMeshCollection simpleModel, string filename, Matrix4X4 matrix)
        {
            SimpleMesh vol = new SimpleMesh();

            using (StreamReader f = new StreamReader(filename))
            {
                // check for "SOLID"

                var      vertex           = default(MatterHackers.VectorMath.Vector3);
                int      n                = 0;
                IntPoint v0               = new IntPoint(0, 0, 0);
                IntPoint v1               = new IntPoint(0, 0, 0);
                IntPoint v2               = new IntPoint(0, 0, 0);
                string   line             = f.ReadLine();
                Regex    onlySingleSpaces = new Regex("\\s+", RegexOptions.Compiled);
                int      lineCount        = 0;
                while (line != null)
                {
                    if (lineCount++ > 100 && vol.faceTriangles.Count == 0)
                    {
                        return(false);
                    }

                    line = onlySingleSpaces.Replace(line, " ");
                    var parts = line.Trim().Split(' ');
                    if (parts[0].Trim() == "vertex")
                    {
                        vertex.X = Convert.ToDouble(parts[1]);
                        vertex.Y = Convert.ToDouble(parts[2]);
                        vertex.Z = Convert.ToDouble(parts[3]);

                        // change the scale from mm to micrometers
                        n++;
                        switch (n)
                        {
                        case 1:
                            var new0 = VectorMath.Vector3Ex.Transform(vertex, matrix) * 1000;
                            v0 = new IntPoint(new0.X, new0.Y, new0.Z);
                            break;

                        case 2:
                            var new1 = VectorMath.Vector3Ex.Transform(vertex, matrix) * 1000;
                            v1 = new IntPoint(new1.X, new1.Y, new1.Z);
                            break;

                        case 3:
                            var new2 = VectorMath.Vector3Ex.Transform(vertex, matrix) * 1000;
                            v2 = new IntPoint(new2.X, new2.Y, new2.Z);
                            vol.addFaceTriangle(v0, v1, v2);
                            n = 0;
                            break;
                        }
                    }

                    line = f.ReadLine();
                }
            }

            if (vol.faceTriangles.Count > 3)
            {
                simpleModel.SimpleMeshes.Add(vol);
                return(true);
            }

            return(false);
        }
Beispiel #9
0
        private static bool loadModelSTLStream_binary(SimpleMeshCollection simpleModel, Stream stlStream, Matrix4X4 matrix)
        {
            SimpleMesh vol = new SimpleMesh();

            // load it as a binary stl
            // skip the first 80 bytes
            // read in the number of triangles
            stlStream.Position = 0;
            BinaryReader br = new BinaryReader(stlStream);

            byte[] fileContents    = br.ReadBytes((int)stlStream.Length);
            int    currentPosition = 80;

            if (fileContents.Length < currentPosition)
            {
                return(false);
            }

            uint numTriangles      = System.BitConverter.ToUInt32(fileContents, currentPosition);
            long bytesForNormals   = numTriangles * 3 * 4;
            long bytesForVertices  = numTriangles * 3 * 4;
            long bytesForAttributs = numTriangles * 2;

            currentPosition += 4;
            long numBytesRequiredForVertexData = currentPosition + bytesForNormals + bytesForVertices + bytesForAttributs;

            if (fileContents.Length < numBytesRequiredForVertexData || numTriangles < 0)
            {
                stlStream.Close();
                return(false);
            }

            IntPoint[] vector = new IntPoint[3];
            for (int i = 0; i < numTriangles; i++)
            {
                // skip the normal
                currentPosition += 3 * 4;
                for (int j = 0; j < 3; j++)
                {
                    var vertex = new MatterHackers.VectorMath.Vector3(
                        System.BitConverter.ToSingle(fileContents, currentPosition + 0 * 4),
                        System.BitConverter.ToSingle(fileContents, currentPosition + 1 * 4),
                        System.BitConverter.ToSingle(fileContents, currentPosition + 2 * 4));

                    var new0 = VectorMath.Vector3Ex.Transform(vertex, matrix);
                    vector[j]        = new IntPoint(new0.X * 1000, new0.Y * 1000, new0.Z * 1000);
                    currentPosition += 3 * 4;
                }

                currentPosition += 2;                 // skip the attribute

                vol.addFaceTriangle(vector[2], vector[1], vector[0]);
            }


            // Detect and skip non-visible mesh
            var bounds = vol.maxXYZ_um() - vol.minXYZ_um();

            if (vol.faceTriangles.Count > 0)
            {
                if (bounds.X == 0)
                {
                    vol.faceTriangles = new List <SimpleFace>();
                }

                simpleModel.SimpleMeshes.Add(vol);
                return(true);
            }

            return(false);
        }
 public OptimizedMeshCollection(SimpleMeshCollection simpleMeshCollection)
 {
     for (int simpleMeshIndex = 0; simpleMeshIndex < simpleMeshCollection.SimpleMeshes.Count; simpleMeshIndex++)
     {
         OptimizedMeshes.Add(new OptimizedMesh(simpleMeshCollection.SimpleMeshes[simpleMeshIndex], this));
         if (MatterSlice.Canceled)
         {
             return;
         }
     }
 }
Beispiel #11
0
		public static bool LoadModelFromFile(SimpleMeshCollection simpleModel, string filename, FMatrix3x3 matrix)
		{
			if (!loadModelSTL_ascii(simpleModel, filename, matrix))
			{
				return loadModelSTL_binary(simpleModel, filename, matrix);
			}

			return true;
		}
Beispiel #12
0
		private static bool loadModelSTL_binary(SimpleMeshCollection simpleModel, string filename, FMatrix3x3 matrix)
		{
			SimpleMesh vol = new SimpleMesh();
			using (FileStream stlStream = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
			{
				// load it as a binary stl
				// skip the first 80 bytes
				// read in the number of triangles
				stlStream.Position = 0;
				BinaryReader br = new BinaryReader(stlStream);
				byte[] fileContents = br.ReadBytes((int)stlStream.Length);
				int currentPosition = 80;
				uint numTriangles = System.BitConverter.ToUInt32(fileContents, currentPosition);
				long bytesForNormals = numTriangles * 3 * 4;
				long bytesForVertices = numTriangles * 3 * 4;
				long bytesForAttributs = numTriangles * 2;
				currentPosition += 4;
				long numBytesRequiredForVertexData = currentPosition + bytesForNormals + bytesForVertices + bytesForAttributs;
				if (fileContents.Length < numBytesRequiredForVertexData || numTriangles < 0)
				{
					stlStream.Close();
					return false;
				}

				Point3[] vector = new Point3[3];
				for (int i = 0; i < numTriangles; i++)
				{
					// skip the normal
					currentPosition += 3 * 4;
					for (int j = 0; j < 3; j++)
					{
						vector[j] = new Point3(
							System.BitConverter.ToSingle(fileContents, currentPosition + 0 * 4) * 1000,
							System.BitConverter.ToSingle(fileContents, currentPosition + 1 * 4) * 1000,
							System.BitConverter.ToSingle(fileContents, currentPosition + 2 * 4) * 1000);
						currentPosition += 3 * 4;
					}
					currentPosition += 2; // skip the attribute

					vol.addFaceTriangle(vector[2], vector[1], vector[0]);
				}
			}

			if (vol.faceTriangles.Count > 0)
			{
				simpleModel.SimpleMeshes.Add(vol);
				return true;
			}

			return false;
		}
Beispiel #13
0
		public static bool loadModelSTL_ascii(SimpleMeshCollection simpleModel, string filename, FMatrix3x3 matrix)
		{
			SimpleMesh vol = new SimpleMesh();
			using (StreamReader f = new StreamReader(filename))
			{
				// check for "SOLID"

				Vector3 vertex = new Vector3();
				int n = 0;
				Point3 v0 = new Point3(0, 0, 0);
				Point3 v1 = new Point3(0, 0, 0);
				Point3 v2 = new Point3(0, 0, 0);
				string line = f.ReadLine();
				Regex onlySingleSpaces = new Regex("\\s+", RegexOptions.Compiled);
				int lineCount = 0;
				while (line != null)
				{
					if(lineCount++ > 100 && vol.faceTriangles.Count == 0)
					{
						return false;
					}
					line = onlySingleSpaces.Replace(line, " ");
					var parts = line.Trim().Split(' ');
					if (parts[0].Trim() == "vertex")
					{
						vertex.x = Convert.ToDouble(parts[1]);
						vertex.y = Convert.ToDouble(parts[2]);
						vertex.z = Convert.ToDouble(parts[3]);

						// change the scale from mm to micrometers
						vertex *= 1000.0;

						n++;
						switch (n)
						{
							case 1:
								v0 = matrix.apply(vertex);
								break;

							case 2:
								v1 = matrix.apply(vertex);
								break;

							case 3:
								v2 = matrix.apply(vertex);
								vol.addFaceTriangle(v0, v1, v2);
								n = 0;
								break;
						}
					}
					line = f.ReadLine();
				}
			}

			if (vol.faceTriangles.Count > 3)
			{
				simpleModel.SimpleMeshes.Add(vol);
				return true;
			}

			return false;
		}
Beispiel #14
0
        public static bool loadModelSTL_ascii(SimpleMeshCollection simpleModel, string filename, FMatrix3x3 matrix)
        {
            SimpleMesh vol = new SimpleMesh();

            using (StreamReader f = new StreamReader(filename))
            {
                // check for "SOLID"

                Vector3 vertex           = new Vector3();
                int     n                = 0;
                Point3  v0               = new Point3(0, 0, 0);
                Point3  v1               = new Point3(0, 0, 0);
                Point3  v2               = new Point3(0, 0, 0);
                string  line             = f.ReadLine();
                Regex   onlySingleSpaces = new Regex("\\s+", RegexOptions.Compiled);
                int     lineCount        = 0;
                while (line != null)
                {
                    if (lineCount++ > 100 && vol.faceTriangles.Count == 0)
                    {
                        return(false);
                    }
                    line = onlySingleSpaces.Replace(line, " ");
                    var parts = line.Trim().Split(' ');
                    if (parts[0].Trim() == "vertex")
                    {
                        vertex.x = Convert.ToDouble(parts[1]);
                        vertex.y = Convert.ToDouble(parts[2]);
                        vertex.z = Convert.ToDouble(parts[3]);

                        // change the scale from mm to micrometers
                        vertex *= 1000.0;

                        n++;
                        switch (n)
                        {
                        case 1:
                            v0 = matrix.apply(vertex);
                            break;

                        case 2:
                            v1 = matrix.apply(vertex);
                            break;

                        case 3:
                            v2 = matrix.apply(vertex);
                            vol.addFaceTriangle(v0, v1, v2);
                            n = 0;
                            break;
                        }
                    }
                    line = f.ReadLine();
                }
            }

            if (vol.faceTriangles.Count > 3)
            {
                simpleModel.SimpleMeshes.Add(vol);
                return(true);
            }

            return(false);
        }
Beispiel #15
0
		public void SetPositionAndSize(SimpleMeshCollection simpleMeshCollection, long xCenter_um, long yCenter_um, long zClip_um, bool centerObjectInXy)
		{
			minXYZ_um = simpleMeshCollection.minXYZ_um();
			maxXYZ_um = simpleMeshCollection.maxXYZ_um();

			if (centerObjectInXy)
			{
				Point3 modelXYCenterZBottom_um = new Point3((minXYZ_um.x + maxXYZ_um.x) / 2, (minXYZ_um.y + maxXYZ_um.y) / 2, minXYZ_um.z);
				modelXYCenterZBottom_um -= new Point3(xCenter_um, yCenter_um, zClip_um);
				for (int optimizedMeshIndex = 0; optimizedMeshIndex < OptimizedMeshes.Count; optimizedMeshIndex++)
				{
					for (int n = 0; n < OptimizedMeshes[optimizedMeshIndex].vertices.Count; n++)
					{
						OptimizedMeshes[optimizedMeshIndex].vertices[n].position -= modelXYCenterZBottom_um;
					}
				}

				minXYZ_um -= modelXYCenterZBottom_um;
				maxXYZ_um -= modelXYCenterZBottom_um;
			}
			else // we still need to put in the bottom clip
			{
				// Ofset by bed center and correctly position in z
				Point3 modelZBottom_um = new Point3(0, 0, minXYZ_um.z - zClip_um);
				for (int optimizedMeshIndex = 0; optimizedMeshIndex < OptimizedMeshes.Count; optimizedMeshIndex++)
				{
					for (int vertexIndex = 0; vertexIndex < OptimizedMeshes[optimizedMeshIndex].vertices.Count; vertexIndex++)
					{
						OptimizedMeshes[optimizedMeshIndex].vertices[vertexIndex].position -= modelZBottom_um;
					}
				}

				minXYZ_um -= modelZBottom_um;
				maxXYZ_um -= modelZBottom_um;
			}

			size_um = maxXYZ_um - minXYZ_um;
		}