Skip to content

Commit

Permalink
add multipolygonZ functions
Browse files Browse the repository at this point in the history
  • Loading branch information
bertt committed May 17, 2021
1 parent 631b0e1 commit 044ec59
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 35 deletions.
34 changes: 2 additions & 32 deletions src/triangulator.tests/CityGmlTests.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
using NUnit.Framework;
using OSGeo.OGR;
using System;
using Wkx;

namespace Triangulate.Tests
{
public class CityGmlTests
{
//[Test]
[Test]
// disabled this test because there are issues with OSGeo.OGR dependency on buildserver
public void FirstTestCityGml()
{
Expand All @@ -33,7 +32,7 @@ public void FirstTestCityGml()

Assert.IsTrue(multilinestring.Geometries.Count == 5);

var polyhedral= GetPolyhedral(multilinestring);
var polyhedral= multilinestring.ToPolyhedralSurface();

var triangulatedPolyhedral = Triangulator.Triangulate(polyhedral);

Expand All @@ -43,34 +42,5 @@ public void FirstTestCityGml()
// needs something different for parsing gml
GltfCreator.CreateGltf(triangulatedPolyhedral, @"gml.gltf");
}

private PolyhedralSurface GetPolyhedral(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;
}

private Polygon GetPolygon(LineString linestring)
{
var polygon = new Polygon()
{
Dimension = Dimension.Xyz
};

polygon.ExteriorRing.Points.AddRange(linestring.Points);
polygon.ExteriorRing.Points.Add(linestring.Points[linestring.Points.Count-1]);

return polygon;
}
}
}
28 changes: 28 additions & 0 deletions src/triangulator.tests/GeometryExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using NUnit.Framework;
using Wkx;

namespace Triangulate.Tests
{
public class GeometryExtensionsTests
{
[Test]
public void MultilinestringToPolyhedralSurfaceTest()
{
var wkt = "MULTILINESTRING ((280986.414 5657433.423 41.401,280986.416 5657433.416 41.402,280986.527 5657433.0 41.426,280986.627 5657432.627 41.451,280986.795 5657432.0 41.502,280986.838 5657431.838 41.508,280987.0 5657431.232 41.525,280987.049 5657431.049 41.531,280987.062 5657431.0 41.529,280987.126 5657430.761 41.524),(280986.414 5657433.423 41.401,280986.423 5657433.423 41.402,280987.0 5657433.438 41.418,280987.45 5657433.45 41.431,280988.0 5657433.464 41.458,280988.476 5657433.476 41.476,280989.0 5657433.49 41.491,280989.503 5657433.503 41.505,280990.0 5657433.516 41.524,280990.529 5657433.529 41.534,280991.0 5657433.542 41.542,280991.556 5657433.556 41.541,280992.0 5657433.567 41.549,280992.198 5657433.572 41.558),(280992.198 5657433.572 41.558,280992.231 5657433.231 41.591,280992.254 5657433.0 41.605,280992.322 5657432.322 41.64,280992.354 5657432.0 41.657,280992.412 5657431.412 41.658,280992.421 5657431.324 41.659),(280992.421 5657431.324 41.659,280992.453 5657431.0 41.664,280992.485 5657430.68 41.649),(280987.126 5657430.761 41.524,280987.752 5657430.752 41.518,280988.0 5657430.748 41.525,280988.737 5657430.737 41.54,280989.0 5657430.733 41.545,280989.722 5657430.722 41.596,280990.0 5657430.718 41.599,280990.707 5657430.707 41.605,280991.0 5657430.702 41.608,280991.692 5657430.692 41.628,280992.0 5657430.687 41.634,280992.485 5657430.68 41.649))";
var multilinestring = (MultiLineString)Wkx.Geometry.Deserialize<WktSerializer>(wkt);
var polyhedralsurface = multilinestring.ToPolyhedralSurface();
Assert.IsTrue(polyhedralsurface != null);
Assert.IsTrue(polyhedralsurface.Geometries.Count == multilinestring.Geometries.Count);
}

[Test]
public void MultipolygonZToPolyhedralSurfaceTest()
{
var buildingWkt = "MULTIPOLYGON(((43603.770435546874 361514.6418164063 1.2790000438690186,43603.50600073242 361509.7590039063 1.2790000438690186,43598.60899755859 361510.0461132813 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186)),((43599.11101171875 361520.242890625 1.2790000438690186,43604.058002685546 361519.95211914065 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186,43599.11101171875 361520.242890625 1.2790000438690186)),((43598.85057470703 361514.95309570315 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186,43598.60899755859 361510.0461132813 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186)),((43604.058002685546 361519.95211914065 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186,43604.058002685546 361519.95211914065 1.2790000438690186)),((43598.60899755859 361510.0461132813 6.778176307678223,43598.85057470703 361514.95309570315 7.397906303405762,43598.85057470703 361514.95309570315 1.2790000438690186,43598.60899755859 361510.0461132813 6.778176307678223)),((43598.60899755859 361510.0461132813 1.2790000438690186,43598.60899755859 361510.0461132813 6.778176307678223,43598.85057470703 361514.95309570315 1.2790000438690186,43598.60899755859 361510.0461132813 1.2790000438690186)),((43603.770435546874 361514.6418164063 1.2790000438690186,43603.50600073242 361509.7590039063 6.764927387237549,43603.50600073242 361509.7590039063 1.2790000438690186,43603.770435546874 361514.6418164063 1.2790000438690186)),((43603.770435546874 361514.6418164063 1.2790000438690186,43603.770435546874 361514.6418164063 7.381703853607178,43603.50600073242 361509.7590039063 6.764927387237549,43603.770435546874 361514.6418164063 1.2790000438690186)),((43598.85057470703 361514.95309570315 1.2790000438690186,43599.11101171875 361520.242890625 6.854876518249512,43599.11101171875 361520.242890625 1.2790000438690186,43598.85057470703 361514.95309570315 1.2790000438690186)),((43598.85057470703 361514.95309570315 1.2790000438690186,43598.85057470703 361514.95309570315 7.397906303405762,43599.11101171875 361520.242890625 6.854876518249512,43598.85057470703 361514.95309570315 1.2790000438690186)),((43604.058002685546 361519.95211914065 6.836284160614014,43603.770435546874 361514.6418164063 7.381703853607178,43603.770435546874 361514.6418164063 1.2790000438690186,43604.058002685546 361519.95211914065 6.836284160614014)),((43604.058002685546 361519.95211914065 1.2790000438690186,43604.058002685546 361519.95211914065 6.836284160614014,43603.770435546874 361514.6418164063 1.2790000438690186,43604.058002685546 361519.95211914065 1.2790000438690186)),((43599.11101171875 361520.242890625 1.2790000438690186,43604.058002685546 361519.95211914065 6.836284160614014,43604.058002685546 361519.95211914065 1.2790000438690186,43599.11101171875 361520.242890625 1.2790000438690186)),((43599.11101171875 361520.242890625 1.2790000438690186,43599.11101171875 361520.242890625 6.854876518249512,43604.058002685546 361519.95211914065 6.836284160614014,43599.11101171875 361520.242890625 1.2790000438690186)),((43603.50600073242 361509.7590039063 6.764927387237549,43598.60899755859 361510.0461132813 6.778176307678223,43598.60899755859 361510.0461132813 1.2790000438690186,43603.50600073242 361509.7590039063 6.764927387237549)),((43603.50600073242 361509.7590039063 1.2790000438690186,43603.50600073242 361509.7590039063 6.764927387237549,43598.60899755859 361510.0461132813 1.2790000438690186,43603.50600073242 361509.7590039063 1.2790000438690186)),((43603.770435546874 361514.6418164063 7.381703853607178,43598.60899755859 361510.0461132813 6.778176307678223,43603.50600073242 361509.7590039063 6.764927387237549,43603.770435546874 361514.6418164063 7.381703853607178)),((43603.770435546874 361514.6418164063 7.381703853607178,43598.85057470703 361514.95309570315 7.397906303405762,43598.60899755859 361510.0461132813 6.778176307678223,43603.770435546874 361514.6418164063 7.381703853607178)),((43604.058002685546 361519.95211914065 6.836284160614014,43598.85057470703 361514.95309570315 7.397906303405762,43603.770435546874 361514.6418164063 7.381703853607178,43604.058002685546 361519.95211914065 6.836284160614014)),((43604.058002685546 361519.95211914065 6.836284160614014,43599.11101171875 361520.242890625 6.854876518249512,43598.85057470703 361514.95309570315 7.397906303405762,43604.058002685546 361519.95211914065 6.836284160614014)))";
var multipolygon = (MultiPolygon)Wkx.Geometry.Deserialize<WktSerializer>(buildingWkt);
var polyhedralsurface = multipolygon.ToPolyhedralSurface();
Assert.IsTrue(polyhedralsurface != null);
Assert.IsTrue(polyhedralsurface.Geometries.Count == multipolygon.Geometries.Count);
}
}
}
2 changes: 0 additions & 2 deletions src/triangulator.tests/GltfCreator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public static void CreateGltf(PolyhedralSurface triangulatedGeometry, string out
var prim = mesh.UsePrimitive(material1);
foreach (var t in triangulatedGeometry.Geometries)
{
Assert.IsTrue(t.Dimension == Dimension.Xyz);
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),
Expand All @@ -36,6 +35,5 @@ public static void CreateGltf(PolyhedralSurface triangulatedGeometry, string out
var model = scene.ToGltf2();
model.SaveGLTF(outputfile);
}

}
}
4 changes: 3 additions & 1 deletion src/triangulator.tests/WkbTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using NUnit.Framework;
using System.IO;
using System.Text;
using Wkx;

namespace Triangulate.Tests
Expand All @@ -9,13 +10,14 @@ public class Tests
[Test]
public void TriangulateWkbTest()
{
// "POLYHEDRALSURFACE Z (((-7.750331999966875 -7.355232000350952 1.9086260322265503,-7.812167000025511 -1.4992249999195337 1.9086260322265503,-7.812167000025511 -1.4992249999195337 -2.053857967773439,-7.750331999966875 -7.355232000350952 -2.053857967773439,-7.750331999966875 -7.355232000350952 1.9086260322265503)),((3.968241000082344 -7.231492999941111 1.9086260322265503,-7.750331999966875 -7.355232000350952 1.9086260322265503,-7.750331999966875 -7.355232000350952 -2.053857967773439,3.968241000082344 -7.231492999941111 -2.053857967773439,3.968241000082344 -7.231492999941111 1.9086260322265503)),((3.968241000082344 -7.231492999941111 -2.053857967773439,3.9064060000237077 -1.3754860004410148 -2.053857967773439,3.9064060000237077 -1.3754860004410148 1.9086260322265503,3.968241000082344 -7.231492999941111 1.9086260322265503,3.968241000082344 -7.231492999941111 -2.053857967773439)),((8.803642000071704 -1.3237749999389052 -2.053857967773439,8.712588999886066 7.299309999682009 -2.053857967773439,8.712588999886066 7.299309999682009 2.053861032226564,8.803642000071704 -1.3237749999389052 2.053861032226564,8.803642000071704 -1.3237749999389052 -2.053857967773439)),((8.712588999886066 7.299309999682009 -2.053857967773439,-8.747484999941662 7.114945000037551 -2.053857967773439,-8.747484999941662 7.114945000037551 2.053861032226564,8.712588999886066 7.299309999682009 2.053861032226564,8.712588999886066 7.299309999682009 -2.053857967773439)),((-8.65643099998124 -1.5081399995833635 2.053861032226564,-8.747484999941662 7.114945000037551 2.053861032226564,-8.747484999941662 7.114945000037551 -2.053857967773439,-8.65643099998124 -1.5081399995833635 -2.053857967773439,-8.65643099998124 -1.5081399995833635 2.053861032226564)),((-7.812167000025511 -1.4992249999195337 2.053861032226564,-8.23429899988696 -1.5036829998716712 -2.053857967773439,-7.812167000025511 -1.4992249999195337 1.9086260322265503,-7.812167000025511 -1.4992249999195337 2.053861032226564)),((-7.812167000025511 -1.4992249999195337 1.9086260322265503,-8.23429899988696 -1.5036829998716712 -2.053857967773439,-7.812167000025511 -1.4992249999195337 -2.053857967773439,-7.812167000025511 -1.4992249999195337 1.9086260322265503)),((-8.23429899988696 -1.5036829998716712 -2.053857967773439,-8.65643099998124 -1.5081399995833635 2.053861032226564,-8.65643099998124 -1.5081399995833635 -2.053857967773439,-8.23429899988696 -1.5036829998716712 -2.053857967773439)),((-7.812167000025511 -1.4992249999195337 2.053861032226564,-8.65643099998124 -1.5081399995833635 2.053861032226564,-8.23429899988696 -1.5036829998716712 -2.053857967773439,-7.812167000025511 -1.4992249999195337 2.053861032226564)),((8.803642000071704 -1.3237749999389052 2.053861032226564,6.355023999931291 -1.3496310003101826 -2.053857967773439,8.803642000071704 -1.3237749999389052 -2.053857967773439,8.803642000071704 -1.3237749999389052 2.053861032226564)),((8.803642000071704 -1.3237749999389052 2.053861032226564,3.9064060000237077 -1.3754860004410148 2.053861032226564,6.355023999931291 -1.3496310003101826 -2.053857967773439,8.803642000071704 -1.3237749999389052 2.053861032226564)),((6.355023999931291 -1.3496310003101826 -2.053857967773439,3.9064060000237077 -1.3754860004410148 2.053861032226564,3.9064060000237077 -1.3754860004410148 1.9086260322265503,6.355023999931291 -1.3496310003101826 -2.053857967773439)),((6.355023999931291 -1.3496310003101826 -2.053857967773439,3.9064060000237077 -1.3754860004410148 1.9086260322265503,3.9064060000237077 -1.3754860004410148 -2.053857967773439,6.355023999931291 -1.3496310003101826 -2.053857967773439)),((3.9064060000237077 -1.3754860004410148 2.053861032226564,-7.812167000025511 -1.4992249999195337 2.053861032226564,-7.812167000025511 -1.4992249999195337 1.9086260322265503,3.9064060000237077 -1.3754860004410148 1.9086260322265503,3.9064060000237077 -1.3754860004410148 2.053861032226564)))"
var buildingWkb = File.ReadAllBytes(@"testdata/building.wkb");

var wkbTriangulated = Triangulator.Triangulate(buildingWkb);
var polyhedral = (PolyhedralSurface)Wkx.Geometry.Deserialize<WkbSerializer>(wkbTriangulated);
Assert.IsTrue(polyhedral.Geometries.Count == 22);

GltfCreator.CreateGltf(polyhedral, @"wkb.gltf");
}

}
}
51 changes: 51 additions & 0 deletions src/triangulator/GeometryExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using Wkx;

namespace Triangulate
{
public static class GeometryExtensions
{
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;
}

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;
}

private static Polygon GetPolygon(LineString linestring)
{
var polygon = new Polygon()
{
Dimension = Dimension.Xyz
};

polygon.ExteriorRing.Points.AddRange(linestring.Points);
polygon.ExteriorRing.Points.Add(linestring.Points[linestring.Points.Count - 1]);

return polygon;
}
}
}

0 comments on commit 044ec59

Please sign in to comment.