Пример #1
0
        public static Solid SolidFromMesh(Mesh model)
        {
            var            solid    = new Solid();
            List <Vector3> vertices = new List <Vector3>();
            List <int>     indices  = new List <int>();

            int nextIndex = 0;

            foreach (Face face in model.Faces)
            {
                List <IVertex>     triangle = new List <IVertex>();
                VectorMath.Vector3 first    = VectorMath.Vector3.Zero;
                VectorMath.Vector3 last     = VectorMath.Vector3.Zero;
                bool isFirst = true;
                int  count   = 0;
                foreach (FaceEdge faceEdge in face.FaceEdges())
                {
                    VectorMath.Vector3 position = faceEdge.FirstVertex.Position;
                    if (isFirst)
                    {
                        first   = position;
                        isFirst = false;
                    }
                    if (count < 3)
                    {
                        vertices.Add(new Vector3(position.X, position.Y, position.Z));
                        indices.Add(nextIndex++);
                    }
                    else                     // add an entire new polygon
                    {
                        vertices.Add(new Vector3(first.X, first.Y, first.Z));
                        indices.Add(nextIndex++);
                        vertices.Add(new Vector3(last.X, last.Y, last.Z));
                        indices.Add(nextIndex++);
                        vertices.Add(new Vector3(position.X, position.Y, position.Z));
                        indices.Add(nextIndex++);
                    }
                    count++;
                    last = position;
                }
            }

            solid.setData(vertices.ToArray(), indices.ToArray());

            return(solid);
        }
Пример #2
0
        private static bool loadModelSTL_binary(SimpleMeshCollection simpleModel, string filename, Matrix4X4 matrix)
        {
            var 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;
                var    br              = new BinaryReader(stlStream);
                byte[] fileContents    = br.ReadBytes((int)stlStream.Length);
                int    currentPosition = 80;
                if (fileContents.Length < currentPosition)
                {
                    return(false);
                }

                uint numTriangles      = 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);
                }

                var 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 VectorMath.Vector3(
                            BitConverter.ToSingle(fileContents, currentPosition + 0 * 4),
                            BitConverter.ToSingle(fileContents, currentPosition + 1 * 4),
                            BitConverter.ToSingle(fileContents, currentPosition + 2 * 4));

                        var new0 = 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 <SimpleMesh.SimpleFace>();
                }

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

            return(false);
        }