예제 #1
0
 /// <summary>
 /// Reads the model from the specified stream.
 /// </summary>
 /// <param name="s">The stream.</param>
 /// <returns>The model.</returns>
 public Object3DGroup Read(string path, ModelInfo info = default(ModelInfo))
 {
     using (var s = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
     {
         return this.Read(s, info);
     }            
 }
예제 #2
0
파일: StLReader.cs 프로젝트: alir14/3DModel
        /// <summary>
        /// Reads the model from the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="info"></param>
        /// <returns>The model.</returns>
        public override Mesh3DGroup Read(Stream stream, ModelInfo info = default(ModelInfo))
        {
            // Try to read in BINARY format
            var success = this.TryReadBinary(stream);

            if (!success)
            {
                // Reset position of stream
                stream.Position = 0;

                // Read in ASCII format
                success = this.TryReadAscii(stream);
            }

            if (success)
            {
                return(this.ToModel3D());
            }

            return(null);
        }
예제 #3
0
        /// <summary>
        /// Reads the model from the specified stream.
        /// </summary>
        /// <param name="s">
        /// The stream.
        /// </param>
        /// <returns>
        /// The model.
        /// </returns>
        public Object3DGroup Read(Stream s, ModelInfo info = default(ModelInfo))
        {
            using (this.Reader = new StreamReader(s))
            {
                this.currentLineNo = 0;
                while (!this.Reader.EndOfStream)
                {
                    this.currentLineNo++;
                    var line = this.Reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    line = line.Trim();
                    while (line.EndsWith("\\"))
                    {
                        var nextLine = this.Reader.ReadLine();
                        while (nextLine.Length == 0)
                        {
                            nextLine = this.Reader.ReadLine();
                        }

                        line = line.TrimEnd('\\') + nextLine;
                    }

                    if (line.StartsWith("#") || line.Length == 0)
                    {
                        continue;
                    }

                    string keyword, values;
                    SplitLine(line, out keyword, out values);

                    try
                    {
                        switch (keyword.ToLower())
                        {
                        // Vertex data
                        case "v":     // geometric vertices
                            this.AddVertex(values);
                            break;

                        case "vt":     // texture vertices
                            this.AddTexCoord(values);
                            break;

                        case "vn":     // vertex normals
                            this.AddNormal(values);
                            break;

                        case "vp":     // parameter space vertices
                        case "cstype": // rational or non-rational forms of curve or surface type: basis matrix, Bezier, B-spline, Cardinal, Taylor
                        case "degree": // degree
                        case "bmat":   // basis matrix
                        case "step":   // step size
                            // not supported
                            break;

                        // Elements
                        case "f":     // face
                            this.AddFace(values);
                            break;

                        case "p":     // point
                        case "l":     // line
                        case "curv":  // curve
                        case "curv2": // 2D curve
                        case "surf":  // surface
                            // not supported
                            break;

                        // Free-form curve/surface body statements
                        case "parm":    // parameter name
                        case "trim":    // outer trimming loop (trim)
                        case "hole":    // inner trimming loop (hole)
                        case "scrv":    // special curve (scrv)
                        case "sp":      // special point (sp)
                        case "end":     // end statement (end)
                            // not supported
                            break;

                        // Connectivity between free-form surfaces
                        case "con":     // connect
                            // not supported
                            break;

                        // Grouping
                        case "g":     // group name
                            this.AddGroup(values);
                            break;

                        case "s":     // smoothing group
                            this.SetSmoothingGroup(values);
                            break;

                        case "mg":     // merging group
                            break;

                        case "o":     // object name
                            // not supported
                            break;

                        // Display/render attributes
                        case "mtllib":     // material library
                            this.LoadMaterialLib(values);
                            break;

                        case "usemtl":     // material name
                            this.EnsureNewMesh();

                            this.SetMaterial(values);
                            break;

                        case "usemap":     // texture map name
                            this.EnsureNewMesh();

                            break;

                        case "bevel":      // bevel interpolation
                        case "c_interp":   // color interpolation
                        case "d_interp":   // dissolve interpolation
                        case "lod":        // level of detail
                        case "shadow_obj": // shadow casting
                        case "trace_obj":  // ray tracing
                        case "ctech":      // curve approximation technique
                        case "stech":      // surface approximation technique
                            // not supported
                            break;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Windows.MessageBox.Show(string.Format("Error loading object: {0}", ex.Message), "Error", MessageBoxButton.OKCancel);
                    }
                }
            }

            return(this.BuildModel());
        }
예제 #4
0
        /// <summary>
        /// Reads the model from the specified stream.
        /// </summary>
        /// <param name="s">
        /// The stream.
        /// </param>
        /// <returns>
        /// The model.
        /// </returns>
        public Object3DGroup Read(Stream s, ModelInfo info = default(ModelInfo))
        {
            using (this.Reader = new StreamReader(s))
            {
                this.currentLineNo = 0;
                while (!this.Reader.EndOfStream)
                {
                    this.currentLineNo++;
                    var line = this.Reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }

                    line = line.Trim();
                    while (line.EndsWith("\\")) 
                    {
                        var nextLine = this.Reader.ReadLine();
                        while (nextLine.Length == 0) 
                        {
                            nextLine = this.Reader.ReadLine();
                        }

                        line = line.TrimEnd('\\') + nextLine;
                    }

                    if (line.StartsWith("#") || line.Length == 0)
                    {
                        continue;
                    }

                    string keyword, values;
                    SplitLine(line, out keyword, out values);

                    try
                    {

                        switch (keyword.ToLower())
                        {
                            // Vertex data
                            case "v": // geometric vertices
                                this.AddVertex(values);
                                break;
                            case "vt": // texture vertices
                                this.AddTexCoord(values);
                                break;
                            case "vn": // vertex normals
                                this.AddNormal(values);
                                break;
                            case "vp": // parameter space vertices
                            case "cstype": // rational or non-rational forms of curve or surface type: basis matrix, Bezier, B-spline, Cardinal, Taylor
                            case "degree": // degree
                            case "bmat": // basis matrix
                            case "step": // step size
                                // not supported
                                break;

                            // Elements
                            case "f": // face
                                this.AddFace(values);
                                break;
                            case "p": // point
                            case "l": // line
                            case "curv": // curve
                            case "curv2": // 2D curve
                            case "surf": // surface
                                // not supported
                                break;

                            // Free-form curve/surface body statements
                            case "parm": // parameter name
                            case "trim": // outer trimming loop (trim)
                            case "hole": // inner trimming loop (hole)
                            case "scrv": // special curve (scrv)
                            case "sp":  // special point (sp)
                            case "end": // end statement (end)
                                // not supported
                                break;

                            // Connectivity between free-form surfaces
                            case "con": // connect
                                // not supported
                                break;

                            // Grouping
                            case "g": // group name
                                this.AddGroup(values);
                                break;
                            case "s": // smoothing group
                                this.SetSmoothingGroup(values);
                                break;
                            case "mg": // merging group
                                break;
                            case "o": // object name
                                // not supported
                                break;

                            // Display/render attributes
                            case "mtllib": // material library
                                this.LoadMaterialLib(values);
                                break;
                            case "usemtl": // material name
                                this.EnsureNewMesh();

                                this.SetMaterial(values);
                                break;
                            case "usemap": // texture map name
                                this.EnsureNewMesh();

                                break;
                            case "bevel": // bevel interpolation
                            case "c_interp": // color interpolation
                            case "d_interp": // dissolve interpolation
                            case "lod": // level of detail
                            case "shadow_obj": // shadow casting
                            case "trace_obj": // ray tracing
                            case "ctech": // curve approximation technique
                            case "stech": // surface approximation technique
                                // not supported
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        System.Windows.MessageBox.Show(string.Format("Error loading object: {0}", ex.Message), "Error", MessageBoxButton.OKCancel);
                    }
                }
            }

            return this.BuildModel();
        }
예제 #5
0
        /// <summary>
        /// Reads the model from the specified path.
        /// </summary>
        /// <param name="path">
        /// The path.
        /// </param>
        /// <returns>
        /// The model.
        /// </returns>
        public Object3DGroup Read(string path, ModelInfo info = default(ModelInfo))
        {
            this.TexturePath = Path.GetDirectoryName(path);
            this.ModelInfo = info;

            using (var s = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
            {
                return this.Read(s);
            }
        }
예제 #6
0
            /// <summary>
            /// Creates the models of the group.
            /// </summary>
            /// <returns>The models.</returns>
            public IEnumerable<Object3D> CreateModels(ModelInfo info)
            {
                for (int i = 0; i < this.meshBuilders.Count; i++)
                {
                    this.meshBuilders[i].ComputeNormalsAndTangents(info.Faces, true);
                    yield return new Object3D
                    {

                        Geometry = this.meshBuilders[i].ToMeshGeometry3D(),
                        Material = this.materials[i],
                        Transform = Matrix.Identity,
                        //BackMaterial = this.materials[i]
                    };
                }
            }
예제 #7
0
        /// <summary>
        /// Creates a MeshGeometry3D object from the loaded file. Polygons are triangulated using triangle fans.
        /// </summary>
        /// <returns>
        /// A MeshGeometry3D.
        /// </returns>
        public MeshGeometry3D CreateMeshGeometry3D(ModelInfo info = default(ModelInfo))
        {
            var mb = new MeshBuilder(info.Normals, info.Tangents);
            foreach (var p in this.Vertices)
            {
                mb.Positions.Add(p);
            }

            foreach (var face in this.Faces)
            {
                mb.AddTriangleFan(face);
            }
            mb.ComputeNormalsAndTangents(info.Faces);
            return mb.ToMeshGeometry3D();
        }
예제 #8
0
 /// <summary>
 /// Reads the model from the specified stream.
 /// </summary>
 /// <param name="s">The stream.</param>
 /// <returns>The model.</returns>
 public Object3DGroup Read(Stream s, ModelInfo info = default(ModelInfo))
 {
     this.Load(s);
     return this.BuildModel(info);
 }
예제 #9
0
        /// <summary>
        /// Creates a Model3D object from the loaded file.
        /// </summary>
        /// <returns>A Model3D group.</returns>
        public Object3DGroup BuildModel(ModelInfo info = default(ModelInfo))
        {
            Object3DGroup modelGroup = null;

            modelGroup = new Object3DGroup();
            var g = this.CreateMeshGeometry3D(info);
            var gm = new Object3D() { Geometry = g, Material = PhongMaterials.DefaultVRML, Transform =  Matrix.Identity, };            
            modelGroup.Add(gm);
            return modelGroup;
        }
예제 #10
0
 /// <summary>
 /// Reads the model from the specified stream.
 /// </summary>
 /// <param name="s">The stream.</param>
 /// <param name="info">The model info.</param>
 /// <returns>The model.</returns>
 public Object3DGroup Read(Stream s, ModelInfo info = default(ModelInfo))
 {
     this.Load(s);
     return(this.BuildModel(info));
 }
예제 #11
0
    public Object3DGroup Read(Stream s,ModelInfo info=default(ModelInfo))
    {
      using (var reader = new BinaryReader(s))
      {
        long length = reader.BaseStream.Length;
        var headerId = this.ReadChunkId(reader);
        if (headerId != ChunkID.MAIN3DS)
        {
          throw new FileFormatException("Unknown file");
        }
        int headerSize = this.ReadChunkSize(reader);
        if (headerSize != length)
        {
          throw new FileFormatException("Incomplete file (file length does not match header)");
        }
        while (reader.BaseStream.Position < reader.BaseStream.Length)
        {
          var id = this.ReadChunkId(reader);
          int size = this.ReadChunkSize(reader);
          switch (id)
          {
            case ChunkID.EDIT_MATERIAL:
              this.ReadMaterial(reader, size);
              break;
            case ChunkID.EDIT_OBJECT:
              this.ReadObject(reader, size);
              break;
            case ChunkID.EDIT3DS:
            case ChunkID.OBJ_CAMERA:
            case ChunkID.OBJ_LIGHT:
            case ChunkID.OBJ_TRIMESH:

              // don't read the whole chunk, read the sub-defines...
              break;

            default:

              // download the whole chunk
              this.ReadData(reader, size - 6);
              break;
          }
        }

      }
      return obGroup;
    }
예제 #12
0
 public Object3DGroup Read(string path, ModelInfo info = default(ModelInfo))
 {
   this.Directory = Path.GetDirectoryName(path);
   using (var s = File.OpenRead(path))
   {
     return this.Read(s);
   };
 }
예제 #13
0
 /// <summary>
 /// Reads the model from the specified stream.
 /// </summary>
 /// <param name="s">The stream.</param>
 /// <param name="info"></param>
 /// <returns>The model.</returns>
 public abstract Mesh3DGroup Read(Stream s, ModelInfo info = default(ModelInfo));