Ejemplo n.º 1
0
        public int AppendTriangle(int i, int j, int k, int g = -1)
        {
            int ti = Triangles.Length / 3;

            if (HasTriangleGroups)
            {
                FaceGroups.Add((g == -1) ? 0 : g);
            }
            Triangles.Add(i); Triangles.Add(j); Triangles.Add(k);
            return(ti);
        }
Ejemplo n.º 2
0
        public static int[] GetTrisOnPositiveSide(DMesh3 mesh, Frame3f plane)
        {
            DVector <int> keep_tris = new DVector <int>();

            Vector3d[] tri = new Vector3d[3];
            foreach (int tid in mesh.TriangleIndices())
            {
                mesh.GetTriVertices(tid, ref tri[0], ref tri[1], ref tri[2]);
                bool ok = true;
                for (int j = 0; j < 3; ++j)
                {
                    double d = (tri[j] - plane.Origin).Dot(plane.Z);
                    if (d < 0)
                    {
                        ok = false;
                    }
                }
                if (ok)
                {
                    keep_tris.Add(tid);
                }
            }

            return(keep_tris.GetBuffer());
        }
Ejemplo n.º 3
0
        public int AppendUV(Vector2F uv)
        {
            int id = UVs.Length;

            UVs.Add(uv);
            return(id);
        }
Ejemplo n.º 4
0
        /*
         * Construction
         */
        public int AppendVertex(double x, double y, double z)
        {
            int i = Vertices.Length / 3;

            if (HasVertexNormals)
            {
                Normals.Add(0); Normals.Add(1); Normals.Add(0);
            }
            if (HasVertexColors)
            {
                Colors.Add(1); Colors.Add(1); Colors.Add(1);
            }
            if (HasVertexUVs)
            {
                UVs.Add(0); UVs.Add(0);
            }
            Vertices.Add(x); Vertices.Add(y); Vertices.Add(z);
            return(i);
        }
Ejemplo n.º 5
0
        // todo:
        //   insert
        //   remove
        //   clear


        public void Rebuild_free_list()
        {
            free_indices = new DVector <int>();
            used_count   = 0;

            int N = ref_counts.Length;

            for (int i = 0; i < N; ++i)
            {
                if (ref_counts[i] > 0)
                {
                    used_count++;
                }
                else
                {
                    free_indices.Add(i);
                }
            }
        }
Ejemplo n.º 6
0
        private unsafe void append_face(string[] tokens, OBJMaterial activeMaterial, int nActiveGroup)
        {
            int nMode = 0;

            if (tokens[1].IndexOf("//") != -1)
            {
                nMode = 1;
            }
            else if (tokens[1].IndexOf('/') != -1)
            {
                nMode = 2;
            }

            Triangle t = new Triangle();

            t.clear();
            for (int ti = 0; ti < tokens.Length - 1; ++ti)
            {
                int j = (ti < 3) ? ti : 2;
                if (ti >= 3)
                {
                    t.move_vertex(2, 1);
                }

                // parse next vertex
                if (nMode == 0)
                {
                    // "f v1 v2 v3"
                    t.set_vertex(j, parse_v(tokens[ti + 1]));
                }
                else if (nMode == 1)
                {
                    // "f v1//vn1 v2//vn2 v3//vn3"
                    string[] parts = tokens[ti + 1].Split(this.splitDoubleSlash, StringSplitOptions.RemoveEmptyEntries);
                    t.set_vertex(j, parse_v(parts[0]), parse_n(parts[1]));
                }
                else if (nMode == 2)
                {
                    string[] parts = tokens[ti + 1].Split(this.splitSlash, StringSplitOptions.RemoveEmptyEntries);
                    if (parts.Length == 2)
                    {
                        // "f v1/vt1 v2/vt2 v3/vt3"
                        t.set_vertex(j, parse_v(parts[0]), -1, parse_u(parts[1]));
                    }
                    else if (parts.Length == 3)
                    {
                        // "f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3"
                        t.set_vertex(j, parse_v(parts[0]), parse_n(parts[2]), parse_u(parts[1]));
                    }
                    else
                    {
                        emit_warning("parse_triangle unexpected face component " + tokens[j]);
                    }
                }


                // do append
                if (ti >= 2)
                {
                    if (activeMaterial != null)
                    {
                        t.nMaterialID = activeMaterial.id;
                        UsedMaterials[activeMaterial.id] = activeMaterial.name;
                    }
                    t.nGroupID = nActiveGroup;
                    vTriangles.Add(t);
                    if (t.is_complex())
                    {
                        HasComplexVertices = true;
                    }
                }
            }
        }
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;

            Dictionary <string, int> 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)
                        {
                            Triangle 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, ""));
        }