示例#1
0
        /// <summary>
        /// Creates a Mesh object based on a Wavefront OBJ file.
        /// </summary>
        /// <param name="path">Path to the file</param>
        /// <returns></returns>
        public static Mesh FromOBJ(string path, bool center = false)
        {
            Mesh NewMesh = new Mesh();

            // Parse vertices
            using (TextReader Reader = new StreamReader(File.OpenRead(path)))
            {
                string Line = Reader.ReadLine();
                while (Line != null)
                {
                    string[] Parts = Line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (Parts.Length > 0 && Parts[0] == "v")
                        NewMesh.Vertices.Add(new Vertex(new Vector3(float.Parse(Parts[1]), float.Parse(Parts[2]), float.Parse(Parts[3])), new Vector3(1, 0, 0)));

                    Line = Reader.ReadLine();
                }
            }

            if (center)
            {
                Vector3 Center = Vector3.Zero;
                Vector3 CenterVolume = Vector3.Zero;
                foreach (var v in NewMesh.Vertices)
                {
                    Center += v.Position;
                    CenterVolume += v.VolumePosition;
                }

                Center /= NewMesh.Vertices.Count;
                CenterVolume /= NewMesh.Vertices.Count;

                foreach (var v in NewMesh.Vertices)
                {
                    v.Position -= Center;
                    v.VolumePosition -= CenterVolume;
                }
            }

            // Parse faces
            using (TextReader Reader = new StreamReader(File.OpenRead(path)))
            {
                string Line = Reader.ReadLine();
                while (Line != null)
                {
                    string[] Parts = Line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    if (Parts.Length > 0 && Parts[0] == "f")
                    {
                        string[] FaceParts0 = Parts[1].Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        string[] FaceParts1 = Parts[2].Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        string[] FaceParts2 = Parts[3].Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                        NewMesh.Triangles.Add(new Triangle(NewMesh.Triangles.Count,
                                                           NewMesh.Vertices[int.Parse(FaceParts0[0]) - 1],
                                                           NewMesh.Vertices[int.Parse(FaceParts1[0]) - 1],
                                                           NewMesh.Vertices[int.Parse(FaceParts2[0]) - 1]));
                    }
                    Line = Reader.ReadLine();
                }
            }

            // Amira likes to put unused vertices into the OBJ
            NewMesh.Vertices.RemoveAll(v => v.Triangles.Count == 0);

            NewMesh.UpdateGraph();
            NewMesh.UpdateVertexIDs();

            return NewMesh;
        }
示例#2
0
        /// <summary>
        /// Implementation of Marching Cubes based on http://paulbourke.net/geometry/polygonise/
        /// </summary>
        /// <param name="volume">Volume intensity values</param>
        /// <param name="dims">Volume dimensions</param>
        /// <param name="angpix">Angstrom per pixel</param>
        /// <param name="threshold">Isosurface threshold</param>
        /// <returns></returns>
        public static Mesh FromVolume(float[] volume, int3 dims, float angpix, float threshold)
        {
            Triangle[][] CellTriangles = new Triangle[volume.Length][];

            unsafe
            {
                fixed (float* volumePtr = volume)
                    for (int z = 0; z < dims.Z - 1; z++)
                    {
                        float zz = (z - dims.Z / 2) * angpix;
                        for (int y = 0; y < dims.Y - 1; y++)
                        {
                            float yy = (y - dims.Y / 2) * angpix;
                            for (int x = 0; x < dims.X - 1; x++)
                            {
                                float xx = (x - dims.X / 2) * angpix;

                                Vector3[] p = new Vector3[8];
                                float[] val = new float[8];

                                p[0] = new Vector3(xx, yy, zz);
                                p[1] = new Vector3(xx + angpix, yy, zz);
                                p[2] = new Vector3(xx + angpix, yy + angpix, zz);
                                p[3] = new Vector3(xx, yy + angpix, zz);
                                p[4] = new Vector3(xx, yy, zz + angpix);
                                p[5] = new Vector3(xx + angpix, yy, zz + angpix);
                                p[6] = new Vector3(xx + angpix, yy + angpix, zz + angpix);
                                p[7] = new Vector3(xx, yy + angpix, zz + angpix);

                                val[0] = volumePtr[((z + 0) * dims.Y + (y + 0)) * dims.X + (x + 0)];
                                val[1] = volumePtr[((z + 0) * dims.Y + (y + 0)) * dims.X + (x + 1)];
                                val[2] = volumePtr[((z + 0) * dims.Y + (y + 1)) * dims.X + (x + 1)];
                                val[3] = volumePtr[((z + 0) * dims.Y + (y + 1)) * dims.X + (x + 0)];
                                val[4] = volumePtr[((z + 1) * dims.Y + (y + 0)) * dims.X + (x + 0)];
                                val[5] = volumePtr[((z + 1) * dims.Y + (y + 0)) * dims.X + (x + 1)];
                                val[6] = volumePtr[((z + 1) * dims.Y + (y + 1)) * dims.X + (x + 1)];
                                val[7] = volumePtr[((z + 1) * dims.Y + (y + 1)) * dims.X + (x + 0)];

                                CellTriangles[(z * dims.Y + y) * dims.X + x] = Polygonize(p, val, threshold);
                            }
                        }
                    }
            }

            Mesh NewMesh = new Mesh();

            for (int i = 0; i < CellTriangles.Length; i++)
            {
                if (CellTriangles[i] == null)
                    continue;

                foreach (var tri in CellTriangles[i])
                {
                    NewMesh.Vertices.Add(tri.V0);
                    NewMesh.Vertices.Add(tri.V1);
                    NewMesh.Vertices.Add(tri.V2);

                    tri.ID = NewMesh.Triangles.Count;
                    NewMesh.Triangles.Add(tri);
                }
            }

            NewMesh.UpdateGraph();
            NewMesh.UpdateVertexIDs();

            return NewMesh;
        }
示例#3
0
        public SurfacePatch(Membrane membrane, string name, Color color, IEnumerable<Triangle> triangles)
        {
            Membrane = membrane;
            Name = name;
            Color = color;

            Dictionary<Vertex, Vertex> VertexToTransformed = new Dictionary<Vertex, Vertex>();
            List<Triangle> NewTriangles = new List<Triangle>(triangles.Count());

            foreach (var t in triangles)
            {
                foreach (var v in t.Vertices)
                    if (!VertexToTransformed.ContainsKey(v))
                        VertexToTransformed.Add(v, new Vertex(v.VolumePosition, v.VolumeNormal));

                Triangle NewTriangle = new Triangle(t.ID, VertexToTransformed[t.V0], VertexToTransformed[t.V1], VertexToTransformed[t.V2]);
                NewTriangles.Add(NewTriangle);
                OriginalToTransformed.Add(t, NewTriangle);
                TransformedToOriginal.Add(NewTriangle, t);
            }

            SurfaceMesh = new Mesh();
            SurfaceMesh.Vertices.AddRange(VertexToTransformed.Values);
            SurfaceMesh.Triangles.AddRange(OriginalToTransformed.Values);

            SurfaceMesh.UpdateGraph();
            SurfaceMesh.UpdateVertexIDs();

            TurnUpsideUp();
            // Don't update buffers because there is no OpenGL context yet.

            UpdateStats();
            UpdatePlanarizationStats();

            Membrane.DisplayedPatches.CollectionChanged += MembraneDisplayedPatches_CollectionChanged;
            Membrane.PointGroups.CollectionChanged += MembranePointGroups_CollectionChanged;
            MembranePointGroups_CollectionChanged(null, null);
        }