Ejemplo n.º 1
0
        /// <summary>
        /// Converts a voxelgrid to a mesh hull.
        /// TODO: Add hashmap vertex id  > pixel/pixelface; to compute the pixel to which the mesh belongs.
        /// </summary>
        /// <param name="vg"></param>
        /// <returns></returns>
        public static Mesh VoxelGridToMesh(VoxelGrid3D vg)
        {
            Vector3d[] FaceDirections;
            Vector3d[] AxisDirections;
            Point3i[]  RelativePosition;
            int[,] FaceAxis;
            double[] FaceSizes;

            FaceDirections = new Vector3d[6] {
                vg.BBox.Plane.XAxis, -vg.BBox.Plane.XAxis, vg.BBox.Plane.YAxis, -vg.BBox.Plane.YAxis, vg.BBox.Plane.ZAxis, -vg.BBox.Plane.ZAxis
            };
            AxisDirections = new Vector3d[3] {
                vg.BBox.Plane.XAxis, vg.BBox.Plane.YAxis, vg.BBox.Plane.ZAxis
            };
            // the different faces
            RelativePosition = new Point3i[6] {
                new Point3i(1, 0, 0), new Point3i(-1, 0, 0), new Point3i(0, 1, 0), new Point3i(0, -1, 0), new Point3i(0, 0, 1), new Point3i(0, 0, -1)
            };

            FaceAxis = new int[3, 2] {
                { 1, 2 },
                { 2, 0 },
                { 0, 1 }
            };

            // the sizes of the faces
            FaceSizes = new double[3] {
                vg.SizeUVW.X, vg.SizeUVW.Y, vg.SizeUVW.Z
            };

            var m = new Mesh();

            for (var i = 0; i < vg.Count; i++)
            {
                if (vg[i] == false)
                {
                    continue;
                }

                for (var faceId = 0; faceId < 6; faceId++)
                {
                    if (vg.GetRelativePointValue(i, RelativePosition[faceId]) != 1)
                    {
                        AddFacadeToMesh(vg, i, faceId, ref m, FaceDirections, AxisDirections, FaceAxis);
                    }
                }
            }
            //m.Vertices.CombineIdentical(false, true);
            m.Normals.ComputeNormals();
            m.Compact();
            return(m);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the voxelgrid mesh by plane
        /// Return a seperate list for each face direction
        /// </summary>
        /// <param name="vg"></param>
        /// <returns></returns>
        public static List <Mesh> VoxelGridToMeshByPlanes(VoxelGrid3D vg)
        {
            Vector3d[] FaceDirections;
            Vector3d[] AxisDirections;
            Point3i[]  RelativePosition;
            int[,] FaceAxis;
            double[] FaceSizes;
            var      Meshes = new List <Mesh>();

            for (var i = 0; i < 6; i++)
            {
                Meshes.Add(new Mesh());
            }


            FaceDirections = new Vector3d[6] {
                vg.BBox.Plane.XAxis, -vg.BBox.Plane.XAxis, vg.BBox.Plane.YAxis, -vg.BBox.Plane.YAxis, vg.BBox.Plane.ZAxis, -vg.BBox.Plane.ZAxis
            };
            AxisDirections = new Vector3d[3] {
                vg.BBox.Plane.XAxis, vg.BBox.Plane.YAxis, vg.BBox.Plane.ZAxis
            };
            // the different faces
            RelativePosition = new Point3i[6] {
                new Point3i(1, 0, 0), new Point3i(-1, 0, 0), new Point3i(0, 1, 0), new Point3i(0, -1, 0), new Point3i(0, 0, 1), new Point3i(0, 0, -1)
            };

            FaceAxis = new int[3, 2] {
                { 1, 2 },
                { 2, 0 },
                { 0, 1 }
            };

            // the sizes of the faces
            FaceSizes = new double[3] {
                vg.SizeUVW.X, vg.SizeUVW.Y, vg.SizeUVW.Z
            };

            for (var i = 0; i < vg.Count; i++)
            {
                if (vg[i] == false)
                {
                    continue;
                }
                for (var faceId = 0; faceId < 6; faceId++)
                {
                    var m = Meshes[faceId];
                    if (vg.GetRelativePointValue(i, RelativePosition[faceId]) != 1)
                    {
                        try
                        {
                            AddFacadeToMesh(vg, i, faceId, ref m, FaceDirections, AxisDirections, FaceAxis);
                        }
                        catch (Exception e)
                        {
                            throw new Exception(
                                      $"Adding Facade {faceId} of Voxel {i} to Mesh failed: {e.ToString()}");
                        }
                    }
                }
            }
            //m.Vertices.CombineIdentical(false, true);
            for (var i = 0; i < 6; i++)
            {
                Meshes[i].Normals.ComputeNormals();
                Meshes[i].Compact();
            }
            return(Meshes);
        }