Ejemplo n.º 1
0
        public IOReadResult ReadFile(Stream stream, IMeshBuilder builder, ReadOptions options, ParsingMessagesHandler messages)
        {
            // detect binary STL
            //BinaryReader binReader = new BinaryReader(stream);
            //byte[] header = binReader.ReadBytes(80);
            bool bIsBinary = Util.IsBinaryStream(stream, 500);

            // [RMS] Thingi10k includes some files w/ unicode string in ascii header...
            //   How can we detect this? can we check that each character is a character?
            //string sUTF8 = System.Text.Encoding.UTF8.GetString(header);
            //string sAscii = System.Text.Encoding.ASCII.GetString(header);
            //if (sUTF8.Contains("solid") == false && sAscii.Contains("solid") == false)
            //    bIsBinary = true;

            // if first 80 bytes contain non-text chars, probably a binary file
            //if (Util.IsTextString(header) == false)
            //    bIsBinary = true;
            //// if we don't see "solid" string in first 80 chars, probably binary
            //if (bIsBinary == false) {
            //    string sText = System.Text.Encoding.ASCII.GetString(header);
            //    if (sText.Contains("solid") == false)
            //        bIsBinary = true;
            //}

            stream.Seek(0, SeekOrigin.Begin);             // reset stream

            var reader = new STLReader();

            reader.warningEvent += messages;
            IOReadResult result = (bIsBinary) ?
                                  reader.Read(new BinaryReader(stream), options, builder) :
                                  reader.Read(new StreamReader(stream), options, builder);

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// This is basically a utility function, returns first mesh in file, with default options.
        /// </summary>
        static public DMesh3 ReadMesh(Stream stream, string sExtension)
        {
            var          builder = new DMesh3Builder();
            IOReadResult result  = ReadFile(stream, sExtension, ReadOptions.Defaults, builder);

            return((result.code == IOCode.Ok) ? builder.Meshes[0] : null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// This is basically a utility function, returns first mesh in file, with default options.
        /// </summary>
        static public DMesh3 ReadMesh(string sFilename)
        {
            var          builder = new DMesh3Builder();
            IOReadResult result  = ReadFile(sFilename, ReadOptions.Defaults, builder);

            return((result.code == IOCode.Ok) ? builder.Meshes[0] : null);
        }
Ejemplo n.º 4
0
        public IOReadResult ReadFile(Stream stream, IMeshBuilder builder, ReadOptions options, ParsingMessagesHandler messages)
        {
            // detect binary STL
            BinaryReader binReader = new BinaryReader(stream);

            byte[] header    = binReader.ReadBytes(80);
            bool   bIsBinary = false;

            // if first 80 bytes contain non-text chars, probably a binary file
            if (Util.IsTextString(header) == false)
            {
                bIsBinary = true;
            }
            // if we don't see "solid" string in first 80 chars, probably binary
            if (bIsBinary == false)
            {
                string sText = System.Text.Encoding.ASCII.GetString(header);
                if (sText.Contains("solid") == false)
                {
                    bIsBinary = true;
                }
            }

            stream.Seek(0, SeekOrigin.Begin); // reset stream

            STLReader reader = new STLReader();

            reader.warningEvent += messages;
            IOReadResult result = (bIsBinary) ?
                                  reader.Read(new BinaryReader(stream), options, builder) :
                                  reader.Read(new StreamReader(stream), options, builder);

            return(result);
        }
Ejemplo n.º 5
0
        public IOReadResult ReadFile(Stream stream, IMeshBuilder builder, ReadOptions options, ParsingMessagesHandler messages)
        {
            var reader = new BinaryG3Reader();
            //reader.warningEvent += messages;
            IOReadResult result = reader.Read(new BinaryReader(stream), options, builder);

            return(result);
        }
Ejemplo n.º 6
0
        public IOReadResult ReadFile(string sFilename, IMeshBuilder builder, ReadOptions options, ParsingMessagesHandler messages)
        {
            // detect binary STL
            FileStream stream = null;

            try {
                stream = File.Open(sFilename, FileMode.Open, FileAccess.Read);
            } catch (Exception e) {
                return(new IOReadResult(IOCode.FileAccessError, "Could not open file " + sFilename + " for reading : " + e.Message));
            }

            BinaryReader binReader = new BinaryReader(stream);

            byte[] header    = binReader.ReadBytes(80);
            bool   bIsBinary = false;

            // if first 80 bytes contain non-text chars, probably a binary file
            if (Util.IsTextString(header) == false)
            {
                bIsBinary = true;
            }
            // if we don't see "solid" string in first 80 chars, probably binary
            if (bIsBinary == false)
            {
                string sText = System.Text.Encoding.ASCII.GetString(header);
                if (sText.Contains("solid") == false)
                {
                    bIsBinary = true;
                }
            }

            stream.Seek(0, SeekOrigin.Begin); // reset stream

            STLReader reader = new STLReader();

            reader.warningEvent += messages;
            IOReadResult result = (bIsBinary) ?
                                  reader.Read(new BinaryReader(stream), options, builder) :
                                  reader.Read(new StreamReader(stream), options, builder);

            stream.Close();
            return(result);
        }
Ejemplo n.º 7
0
        public IOReadResult ParseInput(TextReader reader, ReadOptions options)
        {
            vPositions = new DVector <double>();
            vNormals   = new DVector <float>();
            vUVs       = new DVector <float>();
            vColors    = new DVector <float>();
            vTriangles = new DVector <Triangle>();

            bool        bVerticesHaveColors = false;
            int         nMaxUVLength        = 0;
            OBJMaterial activeMaterial      = null;

            var GroupNames    = new Dictionary <string, int>();
            int nGroupCounter = 0;
            int nActiveGroup  = Triangle.InvalidGroupID;

            int nLines = 0;

            while (reader.Peek() >= 0)
            {
                string line = reader.ReadLine();
                nLines++;
                string[] tokens = line.Split((char[])null, StringSplitOptions.RemoveEmptyEntries);
                if (tokens.Length == 0)
                {
                    continue;
                }

                // [RMS] this will hang VS on large models...
                //if (nWarningLevel >= 2)
                //    emit_warning("Parsing line " + line);
                try
                {
                    if (tokens[0][0] == 'v')
                    {
                        if (tokens[0].Length == 1)
                        {
                            if (tokens.Length == 7)
                            {
                                vPositions.Add(Double.Parse(tokens[1]));
                                vPositions.Add(Double.Parse(tokens[2]));
                                vPositions.Add(Double.Parse(tokens[3]));

                                vColors.Add(float.Parse(tokens[4]));
                                vColors.Add(float.Parse(tokens[5]));
                                vColors.Add(float.Parse(tokens[6]));
                                bVerticesHaveColors = true;
                            }
                            else if (tokens.Length >= 4)
                            {
                                vPositions.Add(Double.Parse(tokens[1]));
                                vPositions.Add(Double.Parse(tokens[2]));
                                vPositions.Add(Double.Parse(tokens[3]));
                            }
                            if (tokens.Length != 4 && tokens.Length != 7)
                            {
                                emit_warning("[OBJReader] vertex has unknown format: " + line);
                            }
                        }
                        else if (tokens[0][1] == 'n')
                        {
                            if (tokens.Length >= 4)
                            {
                                vNormals.Add(float.Parse(tokens[1]));
                                vNormals.Add(float.Parse(tokens[2]));
                                vNormals.Add(float.Parse(tokens[3]));
                            }
                            if (tokens.Length != 4)
                            {
                                emit_warning("[OBJReader] normal has more than 3 coordinates: " + line);
                            }
                        }
                        else if (tokens[0][1] == 't')
                        {
                            if (tokens.Length >= 3)
                            {
                                vUVs.Add(float.Parse(tokens[1]));
                                vUVs.Add(float.Parse(tokens[2]));
                                nMaxUVLength = Math.Max(nMaxUVLength, tokens.Length);
                            }
                            if (tokens.Length != 3)
                            {
                                emit_warning("[OBJReader] UV has unknown format: " + line);
                            }
                        }
                    }
                    else if (tokens[0][0] == 'f')
                    {
                        if (tokens.Length < 4)
                        {
                            emit_warning("[OBJReader] degenerate face specified : " + line);
                        }
                        else if (tokens.Length == 4)
                        {
                            var tri = new Triangle();
                            parse_triangle(tokens, ref tri);

                            tri.nGroupID = nActiveGroup;

                            if (activeMaterial != null)
                            {
                                tri.nMaterialID = activeMaterial.id;
                                UsedMaterials[activeMaterial.id] = activeMaterial.name;
                            }

                            vTriangles.Add(tri);
                            if (tri.is_complex())
                            {
                                HasComplexVertices = true;
                            }
                        }
                        else
                        {
                            append_face(tokens, activeMaterial, nActiveGroup);
                        }
                    }
                    else if (tokens[0][0] == 'g')
                    {
                        string sGroupName = (tokens.Length == 2) ? tokens[1] : line.Substring(line.IndexOf(tokens[1]));
                        if (GroupNames.ContainsKey(sGroupName))
                        {
                            nActiveGroup = GroupNames[sGroupName];
                        }
                        else
                        {
                            nActiveGroup           = nGroupCounter;
                            GroupNames[sGroupName] = nGroupCounter++;
                        }
                    }
                    else if (tokens[0][0] == 'o')
                    {
                        // TODO multi-object support
                    }
                    else if (tokens[0] == "mtllib" && options.ReadMaterials)
                    {
                        if (MTLFileSearchPaths.Count == 0)
                        {
                            emit_warning("Materials requested but Material Search Paths not initialized!");
                        }

                        string sMTLPathString = (tokens.Length == 2) ? tokens[1] :
                                                line.Substring(line.IndexOf(tokens[1]));
                        string sFile = FindMTLFile(sMTLPathString);
                        if (sFile != null)
                        {
                            IOReadResult result = ReadMaterials(sFile);
                            if (result.code != IOCode.Ok)
                            {
                                emit_warning("error parsing " + sFile + " : " + result.message);
                            }
                        }
                        else
                        {
                            emit_warning("material file " + sMTLPathString + " could not be found in material search paths");
                        }
                    }
                    else if (tokens[0] == "usemtl" && options.ReadMaterials)
                    {
                        activeMaterial = find_material(tokens[1]);
                    }
                }
                catch (Exception e)
                {
                    emit_warning("error parsing line " + nLines.ToString() + ": " + line + ", exception " + e.Message);
                }
            }

            m_bOBJHasPerVertexColors = bVerticesHaveColors;
            m_bOBJHasTriangleGroups  = (nActiveGroup != Triangle.InvalidGroupID);
            m_nSetInvalidGroupsTo    = nGroupCounter++;
            m_nUVComponents          = nMaxUVLength;

            return(new IOReadResult(IOCode.Ok, ""));
        }