Exemple #1
0
        public static List <Triangle> GetTriangles(PolyhedralSurface polyhedralsurface, int batchId, ShaderColors shadercolors = null)
        {
            var degenerated_triangles = 0;
            var allTriangles          = new List <Triangle>();

            for (var i = 0; i < polyhedralsurface.Geometries.Count; i++)
            {
                var geometry = polyhedralsurface.Geometries[i];
                var triangle = GetTriangle(geometry, batchId);

                if (triangle != null && shadercolors != null)
                {
                    shadercolors.Validate(polyhedralsurface.Geometries.Count);
                    triangle.Shader = shadercolors.ToShader(i);
                }

                if (triangle != null)
                {
                    allTriangles.Add(triangle);
                }
                else
                {
                    degenerated_triangles++;
                }
            }

            return(allTriangles);
        }
Exemple #2
0
        public static void CreateGltf(PolyhedralSurface triangulatedGeometry, string outputfile)
        {
            // convert to glTF to be able to inspect the result...
            var material1 = new MaterialBuilder()
                            .WithDoubleSide(true)
                            .WithSpecularGlossinessShader()
                            .WithChannelParam(KnownChannel.SpecularGlossiness, new Vector4(0.7f, 0, 0f, 1.0f))
                            .WithEmissive(new Vector3(0.2f, 0.3f, 0.1f));

            var mesh = new MeshBuilder <VERTEX>("mesh");

            var prim = mesh.UsePrimitive(material1);

            foreach (var t in triangulatedGeometry.Geometries)
            {
                prim.AddTriangle(
                    new VERTEX((float)t.ExteriorRing.Points[0].X, (float)t.ExteriorRing.Points[0].Y, (float)t.ExteriorRing.Points[0].Z),
                    new VERTEX((float)t.ExteriorRing.Points[1].X, (float)t.ExteriorRing.Points[1].Y, (float)t.ExteriorRing.Points[1].Z),
                    new VERTEX((float)t.ExteriorRing.Points[2].X, (float)t.ExteriorRing.Points[2].Y, (float)t.ExteriorRing.Points[2].Z));
            }

            var scene = new SharpGLTF.Scenes.SceneBuilder();

            scene.AddRigidMesh(mesh, Matrix4x4.Identity);
            var model = scene.ToGltf2();

            model.SaveGLTF(outputfile);
        }
        public static (PolyhedralSurface polyhedral, List <string> colors) MakePolyHedral(Polygon footprint, double fromZ, double height, BuildingStyle buildingStyle)
        {
            var polyhedral = new PolyhedralSurface();

            var res = MakeBuilding(footprint, fromZ, height, buildingStyle);

            foreach (var t in res.polygons)
            {
                polyhedral.Geometries.Add(t);
            }

            return(polyhedral, res.colors);
        }
Exemple #4
0
        public static PolyhedralSurface ToPolyhedralSurface(this MultiPolygon multipolygon)
        {
            var polyhedralsurface = new PolyhedralSurface
            {
                Dimension = Dimension.Xyz
            };

            foreach (var polygon in multipolygon.Geometries)
            {
                polyhedralsurface.Geometries.Add(polygon);
            }

            return(polyhedralsurface);
        }
Exemple #5
0
        public static PolyhedralSurface ToPolyhedralSurface(this MultiLineString multilinestring)
        {
            var polyhedralsurface = new PolyhedralSurface
            {
                Dimension = Dimension.Xyz
            };

            foreach (var linestring in multilinestring.Geometries)
            {
                var polygon = GetPolygon(linestring);
                polyhedralsurface.Geometries.Add(polygon);
            }

            return(polyhedralsurface);
        }
Exemple #6
0
        public static PolyhedralSurface Triangulate(PolyhedralSurface polyhedral)
        {
            var result = new PolyhedralSurface
            {
                Dimension = Dimension.Xyz
            };

            foreach (var g in polyhedral.Geometries)
            {
                var triangles = Triangulate(g);
                result.Geometries.AddRange(triangles);
            }

            return(result);
        }
Exemple #7
0
        public static BoundingBox3D GetBoundingBox3D(this PolyhedralSurface surface)
        {
            var bb = new BoundingBox3D();

            bb.XMax = -double.MaxValue;
            bb.YMax = -double.MaxValue;
            bb.ZMax = -double.MaxValue;

            bb.XMin = double.MaxValue;
            bb.YMin = double.MaxValue;
            bb.ZMin = double.MaxValue;

            foreach (var geometry in surface.Geometries)
            {
                foreach (var point in geometry.ExteriorRing.Points)
                {
                    if (point.X < bb.XMin)
                    {
                        bb.XMin = (double)point.X;
                    }
                    if (point.Y < bb.YMin)
                    {
                        bb.YMin = (double)point.Y;
                    }
                    if (point.Z < bb.ZMin)
                    {
                        bb.ZMin = (double)point.Z;
                    }

                    if (point.X > bb.XMax)
                    {
                        bb.XMax = (double)point.X;
                    }
                    if (point.Y > bb.YMax)
                    {
                        bb.YMax = (double)point.Y;
                    }
                    if (point.Z > bb.ZMax)
                    {
                        bb.ZMax = (double)point.Z;
                    }
                }
            }
            return(bb);
        }
Exemple #8
0
        public static TriangleCollection GetTriangles(PolyhedralSurface polyhedralsurface, string[] hexColors, int batchId)
        {
            var degenerated_triangles = 0;
            var allTriangles          = new TriangleCollection();

            for (var i = 0; i < polyhedralsurface.Geometries.Count; i++)
            {
                var      geometry = polyhedralsurface.Geometries[i];
                Triangle triangle;
                if (hexColors.Length > 0)
                {
                    if (hexColors.Length == 1)
                    {
                        triangle = GetTriangle(geometry, batchId, hexColors[0]);
                    }
                    else
                    {
                        if (hexColors.Length != polyhedralsurface.Geometries.Count)
                        {
                            throw new ArgumentOutOfRangeException($"Expected number of colors: {polyhedralsurface.Geometries.Count}, actual: {hexColors.Length}");
                        }
                        triangle = GetTriangle(geometry, batchId, hexColors[i]);
                    }
                }
                else
                {
                    triangle = GetTriangle(geometry, batchId, String.Empty);
                }

                if (triangle != null)
                {
                    allTriangles.Add(triangle);
                }
                else
                {
                    degenerated_triangles++;
                }
            }

            return(allTriangles);
        }
Exemple #9
0
        public static (PolyhedralSurface polyhedral, List <string> colors) MakeBuilding(Polygon footprint, double fromZ, double height, BuildingStyle buildingStyle)
        {
            var colors     = new List <string>();
            var polyhedral = new PolyhedralSurface();

            polyhedral.Dimension = Dimension.Xyz;

            polyhedral.Geometries.Add(GetPolygonZ(footprint, fromZ));
            polyhedral.Geometries.Add(GetPolygonZ(footprint, fromZ + height));
            if (buildingStyle.Storeys == null)
            {
                var walls = MakeWalls(footprint, fromZ, height - fromZ);
                polyhedral.Geometries.AddRange(walls);
            }
            else
            {
                {
                    foreach (var storey in buildingStyle.Storeys)
                    {
                        var walls = MakeWalls(footprint, fromZ + storey.From, storey.To - storey.From);
                        polyhedral.Geometries.AddRange(walls);
                    }
                }
            }

            var stream = new MemoryStream();

            polyhedral.Serialize <WkbSerializer>(stream);
            var wkb             = stream.ToArray();
            var triangulatedWkb = Triangulator.Triangulate(wkb);
            var polyhedralNew   = (PolyhedralSurface)Geometry.Deserialize <WkbSerializer>(triangulatedWkb);

            foreach (var polygon in polyhedralNew.Geometries)
            {
                var normal = polygon.GetNormal();

                if (Math.Abs(normal.X) > Math.Abs(normal.Y) && Math.Abs(normal.X) > Math.Abs(normal.Z) ||
                    (Math.Abs(normal.Y) > Math.Abs(normal.Z)))
                {
                    //  (yz) projection
                    if (buildingStyle.WallsColor == null)
                    {
                        // use storeys
                        var storeyColor = GetStoreyColor(polygon, buildingStyle.Storeys);
                        colors.Add(storeyColor);
                    }
                    else
                    {
                        colors.Add(buildingStyle.WallsColor);
                    }
                }
                else
                {
                    // (xy) projextion
                    if (polygon.ExteriorRing.Points[0].Z == fromZ)
                    {
                        // floor
                        colors.Add(buildingStyle.FloorColor);
                    }
                    else
                    {
                        // roof
                        colors.Add(buildingStyle.RoofColor);
                    }
                }
            }
            return(polyhedralNew, colors);
        }