Exemplo n.º 1
0
        private MeshGeometryModel3D ConvertVoxelizedSolidtoObject3D(VoxelizedSolid vs)
        {
            if (false)
            {
                var ts = vs.ConvertToTessellatedSolid(
                    new Color(KnownColors.MediumSeaGreen)
                {
                    Af = 0.80f
                });
                return(ConvertTessellatedSolidtoObject3D(ts));
            }

            var normalsTemplate = new[]
            {
                new float[] { -1, 0, 0 }, new float[] { -1, 0, 0 },
                new float[] { 1, 0, 0 }, new float[] { 1, 0, 0 },
                new float[] { 0, -1, 0 }, new float[] { 0, -1, 0 },
                new float[] { 0, 1, 0 }, new float[] { 0, 1, 0 },
                new float[] { 0, 0, -1 }, new float[] { 0, 0, -1 },
                new float[] { 0, 0, 1 }, new float[] { 0, 0, 1 }
            };

            var coordOffsets = new[]
            {
                new[] { new float[] { 0, 0, 0 }, new float[] { 0, 0, 1 }, new float[] { 0, 1, 0 } },
                new[] { new float[] { 0, 1, 0 }, new float[] { 0, 0, 1 }, new float[] { 0, 1, 1 } }, //x-neg
                new[] { new float[] { 1, 0, 0 }, new float[] { 1, 1, 0 }, new float[] { 1, 0, 1 } },
                new[] { new float[] { 1, 1, 0 }, new float[] { 1, 1, 1 }, new float[] { 1, 0, 1 } }, //x-pos
                new[] { new float[] { 0, 0, 0 }, new float[] { 1, 0, 0 }, new float[] { 0, 0, 1 } },
                new[] { new float[] { 1, 0, 0 }, new float[] { 1, 0, 1 }, new float[] { 0, 0, 1 } }, //y-neg
                new[] { new float[] { 0, 1, 0 }, new float[] { 0, 1, 1 }, new float[] { 1, 1, 0 } },
                new[] { new float[] { 1, 1, 0 }, new float[] { 0, 1, 1 }, new float[] { 1, 1, 1 } }, //y-pos
                new[] { new float[] { 0, 0, 0 }, new float[] { 0, 1, 0 }, new float[] { 1, 0, 0 } },
                new[] { new float[] { 1, 0, 0 }, new float[] { 0, 1, 0 }, new float[] { 1, 1, 0 } }, //z-neg
                new[] { new float[] { 0, 0, 1 }, new float[] { 1, 0, 1 }, new float[] { 0, 1, 1 } },
                new[] { new float[] { 1, 0, 1 }, new float[] { 1, 1, 1 }, new float[] { 0, 1, 1 } }, //z-pos
            };
            var positions   = new Vector3Collection();
            var normals     = new Vector3Collection();
            var lowestLevel = (int)vs.VoxelSideLengths.Length - 1;

            foreach (var v in vs.Voxels()) //VoxelDiscretization.ExtraCoarse))
                                           // var v = vs.Voxels(VoxelDiscretization.ExtraCoarse).First(); //VoxelDiscretization.ExtraCoarse))
            {
                if (v.Role == VoxelRoleTypes.Partial && v.Level < lowestLevel)
                {
                    continue;
                }
                var neighbors = vs.GetNeighbors(v).ToList();
                if (neighbors.All(n => n != null && (n.Role == VoxelRoleTypes.Full || (n.Role == VoxelRoleTypes.Partial &&
                                                                                       v.Level == lowestLevel))))
                {
                    continue;
                }

                var x = (float)v.BottomCoordinate[0];
                var y = (float)v.BottomCoordinate[1];
                var z = (float)v.BottomCoordinate[2];
                var s = (float)v.SideLength;
                for (int i = 0; i < 12; i++)
                {
                    //  if (neighbors[i / 2] != null && neighbors[i / 2].Role == VoxelRoleTypes.Full) continue;
                    if (neighbors[i / 2] != null && (neighbors[i / 2].Role == VoxelRoleTypes.Full ||
                                                     (neighbors[i / 2].Role == VoxelRoleTypes.Partial && v.Level == lowestLevel)))
                    {
                        continue;
                    }
                    for (int j = 0; j < 3; j++)
                    {
                        positions.Add(new Vector3(x + coordOffsets[i][j][0] * s,
                                                  y + coordOffsets[i][j][1] * s, z + coordOffsets[i][j][2] * s));
                        normals.Add(new Vector3(normalsTemplate[i][0], normalsTemplate[i][1], normalsTemplate[i][2]));
                    }
                }
            }

            return(new MeshGeometryModel3D
            {
                Material = new PhongMaterial()
                {
                    DiffuseColor = new SharpDX.Color4(vs.SolidColor.Rf, vs.SolidColor.Gf, vs.SolidColor.Bf,
                                                      vs.SolidColor.Af)
                                   //(float)0.75 * vs.SolidColor.Af)
                },
                Geometry = new HelixToolkit.Wpf.SharpDX.MeshGeometry3D
                {
                    Positions = positions,
                    Indices = new IntCollection(Enumerable.Range(0, positions.Count)),
                    Normals = normals
                }
            });
        }
Exemplo n.º 2
0
        private static TVGLFileData MakeFileData(VoxelizedSolid vs)
        {
            double[] ConvexHullCenter;
            double   ConvexHullArea;
            double   ConvexHullVolume;

            if (vs.ConvexHull is null)
            {
                ConvexHullCenter = null;
                ConvexHullArea   = 0;
                ConvexHullVolume = 0;
            }
            else
            {
                ConvexHullCenter = vs.ConvexHull.Center;
                ConvexHullArea   = vs.ConvexHull.SurfaceArea;
                ConvexHullVolume = vs.ConvexHull.Volume;
            }

            var result = new TVGLFileData
            {
                Center               = vs.Center,
                ConvexHullCenter     = ConvexHullCenter,
                ConvexHullArea       = ConvexHullArea,
                ConvexHullVolume     = ConvexHullVolume,
                HasUniformColor      = true,
                Language             = vs.Language,
                Mass                 = vs.Mass,
                Name                 = vs.Name,
                Primitives           = vs.Primitives,
                SurfaceArea          = vs.SurfaceArea,
                Units                = vs.Units,
                Volume               = vs.Volume,
                XMax                 = vs.XMax,
                XMin                 = vs.XMin,
                YMax                 = vs.YMax,
                YMin                 = vs.YMin,
                ZMax                 = vs.ZMax,
                ZMin                 = vs.ZMin,
                BitLevelDistribution = (int[])vs.bitLevelDistribution.Clone()
            };

            result.Voxels = new string[vs.bitLevelDistribution.Length];

            for (int i = 0; i < vs.NumberOfLevels; i++)
            {
                var byteArray = (vs.Voxels(i, true)
                                 .SelectMany(v => BitConverter.GetBytes(v.ID))).ToArray();
                result.Voxels[i] = BitConverter.ToString(byteArray).Replace("-", "");
            }

            result.Colors = vs.SolidColor.ToString();
            result.Comments.AddRange(vs.Comments);
            if (vs._inertiaTensor != null)
            {
                var tensorAsArray = new double[9];
                for (int i = 0; i < 3; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        tensorAsArray[3 * i + j] = vs._inertiaTensor[i, j];
                    }
                }
                result.InertiaTensor = string.Join(",", tensorAsArray);
            }
            return(result);
        }