Esempio n. 1
0
        public static bool loadModelSTL_ascii(SimpleModel simpleModel, string filename, FMatrix3x3 matrix)
        {
            SimpleVolume vol = new SimpleVolume();
            using (StreamReader f = new StreamReader(filename))
            {
                // check for "SOLID"

                FPoint3 vertex = new FPoint3();
                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);
                while (line != null)
                {
                    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.volumes.Add(vol);
                return true;
            }

            return false;
        }
Esempio n. 2
0
        public static bool loadModelFromFile(SimpleModel simpleModel, string filename, FMatrix3x3 matrix)
        {
            if (!loadModelSTL_ascii(simpleModel, filename, matrix))
            {
                return loadModelSTL_binary(simpleModel, filename, matrix);
            }

            return true;
        }
Esempio n. 3
0
        public static SimpleModel loadModelSTL_ascii(string filename, FMatrix3x3 matrix)
        {
            SimpleModel m = new SimpleModel();

            m.volumes.Add(new SimpleVolume());
            SimpleVolume vol = m.volumes[0];

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

                FPoint3 vertex           = new FPoint3();
                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);
                while (line != null)
                {
                    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();
                }

                return(m);
            }
        }
Esempio n. 4
0
        public static SimpleModel loadModelFromFile(string filename, FMatrix3x3 matrix)
        {
            SimpleModel fromAsciiModel = loadModelSTL_ascii(filename, matrix);

            if (fromAsciiModel.volumes[0].faceTriangles.Count == 0)
            {
                return(loadModelSTL_binary(filename, matrix));
            }

            return(fromAsciiModel);
        }
Esempio n. 5
0
        static bool loadModelSTL_binary(SimpleModel simpleModel, string filename, FMatrix3x3 matrix)
        {
            SimpleVolume vol = new SimpleVolume();

            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 < 4)
                {
                    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 > 3)
            {
                simpleModel.volumes.Add(vol);
                return(true);
            }

            return(false);
        }
Esempio n. 6
0
        public static bool loadModelFromFile(SimpleModel simpleModel, string filename, FMatrix3x3 matrix)
        {
            if (!loadModelSTL_ascii(simpleModel, filename, matrix))
            {
                return(loadModelSTL_binary(simpleModel, filename, matrix));
            }

            return(true);
        }
Esempio n. 7
0
        private static bool loadModelSTL_binary(SimpleModel simpleModel, string filename, FMatrix3x3 matrix)
        {
            SimpleVolume vol = new SimpleVolume();
            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 < 4)
                {
                    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 > 3)
            {
                simpleModel.volumes.Add(vol);
                return true;
            }

            return false;
        }
Esempio n. 8
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);
        }