Skip to content

Commit

Permalink
adding helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
bertt committed May 17, 2021
1 parent 044ec59 commit b8c6312
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 38 deletions.
3 changes: 1 addition & 2 deletions src/triangulator.tests/CityGmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ public void FirstTestCityGml()
var featureGml = buildingLayer.GetNextFeature();

var geometry = featureGml.GetGeometryRef();
var wkt = string.Empty;
geometry.ExportToWkt(out wkt);
geometry.ExportToWkt(out string wkt);
Assert.IsTrue(wkt.Contains("MULTILINESTRING"));
var multilinestring = (MultiLineString)Wkx.Geometry.Deserialize<WktSerializer>(wkt);

Expand Down
56 changes: 28 additions & 28 deletions src/triangulator.tests/CityJsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,62 @@ public void FirstCityJsonParsingTest()

var res = JsonConvert.DeserializeObject<CityJSON>(json);

Assert.IsTrue(res.type == "CityJSON");
Assert.IsTrue(res.version == "1.0");
Assert.IsTrue(res.vertices.Length== 255);
Assert.IsTrue(res.Type == "CityJSON");
Assert.IsTrue(res.Version == "1.0");
Assert.IsTrue(res.Vertices.Length== 255);
Assert.IsTrue(res.CityObjects.Count == 2);
var firstBuilding = res.CityObjects.First();
var building = firstBuilding.Value;
Assert.IsTrue(building.type == "Building");
Assert.IsTrue(building.geometry.Length == 1);
Assert.IsTrue(building.geometry.First().type == "MultiSurface");
Assert.IsTrue(building.geometry.First().boundaries.Length == 110);
Assert.IsTrue(building.geometry.First().boundaries[0].Length == 1);
Assert.IsTrue(building.geometry.First().boundaries[0][0].Length == 3);
Assert.IsTrue(building.geometry.First().boundaries[0][0][0] == 198);
Assert.IsTrue(building.geometry.First().boundaries[0][0][1] == 199);
Assert.IsTrue(building.geometry.First().boundaries[0][0][2] == 200);
Assert.IsTrue(building.Type == "Building");
Assert.IsTrue(building.Geometry.Length == 1);
Assert.IsTrue(building.Geometry.First().Type == "MultiSurface");
Assert.IsTrue(building.Geometry.First().Boundaries.Length == 110);
Assert.IsTrue(building.Geometry.First().Boundaries[0].Length == 1);
Assert.IsTrue(building.Geometry.First().Boundaries[0][0].Length == 3);
Assert.IsTrue(building.Geometry.First().Boundaries[0][0][0] == 198);
Assert.IsTrue(building.Geometry.First().Boundaries[0][0][1] == 199);
Assert.IsTrue(building.Geometry.First().Boundaries[0][0][2] == 200);
}
}

public class CityJSON
{
public string type { get; set; }
public string version { get; set; }
public Metadata metadata { get; set; }
public string Type { get; set; }
public string Version { get; set; }
public Metadata Metadata { get; set; }
public Dictionary<string, Building> CityObjects { get; set; }
public float[][] vertices { get; set; }
public float[][] Vertices { get; set; }
}

public class Metadata
{
public float[] geographicalExtent { get; set; }
public float[] GeographicalExtent { get; set; }
}

public class Building
{
public Geometry[] geometry { get; set; }
public string type { get; set; }
public Geometry[] Geometry { get; set; }
public string Type { get; set; }
}

public class Geometry
{
public int[][][] boundaries { get; set; }
public int lod { get; set; }
public Semantics semantics { get; set; }
public Texture texture { get; set; }
public string type { get; set; }
public int[][][] Boundaries { get; set; }
public int Lod { get; set; }
public Semantics Semantics { get; set; }
public Texture Texture { get; set; }
public string Type { get; set; }
}

public class Semantics
{
public Surface[] surfaces { get; set; }
public int[] values { get; set; }
public Surface[] Surfaces { get; set; }
public int[] Values { get; set; }
}

public class Surface
{
public string type { get; set; }
public string Type { get; set; }
}

public class Texture
Expand All @@ -81,6 +81,6 @@ public class Texture

public class RhinoTexturing
{
public int[][][] values { get; set; }
public int[][][] Values { get; set; }
}
}
21 changes: 21 additions & 0 deletions src/triangulator.tests/GeometryExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,27 @@ public void MultipolygonZToPolyhedralSurfaceTest()
var polyhedralsurface = multipolygon.ToPolyhedralSurface();
Assert.IsTrue(polyhedralsurface != null);
Assert.IsTrue(polyhedralsurface.Geometries.Count == multipolygon.Geometries.Count);
Assert.IsTrue(Triangulator.Triangulate(polyhedralsurface).Geometries.Count == polyhedralsurface.Geometries.Count);
}

[Test]
public void AsTextTest()
{
var expected = "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>(expected);
var actual = multipolygon.AsText();
Assert.IsTrue(actual == expected);
}

[Test]
public void AsBinaryTest()
{
var expected = "POLYHEDRALSURFACE Z (((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 polyhedralsurface = (PolyhedralSurface)Wkx.Geometry.Deserialize<WktSerializer>(expected);
var wkb = polyhedralsurface.AsBinary();
var polyhedralsurface_after = (PolyhedralSurface)Wkx.Geometry.Deserialize<WkbSerializer>(wkb);
var polyhedralsurface_after_wkt = polyhedralsurface_after.AsText();
Assert.IsTrue(expected == polyhedralsurface_after_wkt);
}
}
}
4 changes: 2 additions & 2 deletions src/triangulator.tests/triangulator.tests.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net5.0</TargetFramework>

<IsPackable>false</IsPackable>

Expand All @@ -12,7 +12,7 @@
<PackageReference Include="Gdal.Core" Version="2.3.0-beta-023" />
<PackageReference Include="Gdal.Core.WindowsRuntime" Version="2.3.0-beta-023" />
<PackageReference Include="nunit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.15.1" />
<PackageReference Include="NUnit3TestAdapter" Version="3.17.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.4.0" />
<PackageReference Include="SharpGLTF.Toolkit" Version="1.0.0-alpha0017" />
<PackageReference Include="Wkx" Version="0.5.1" />
Expand Down
21 changes: 19 additions & 2 deletions src/triangulator/GeometryExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
using Wkx;
using System.IO;
using System.Text;
using Wkx;

namespace Triangulate
{
public static class GeometryExtensions
{
public static string AsText(this Geometry geometry)
{
var stream = new MemoryStream();
geometry.Serialize<WktSerializer>(stream);
var wkt = Encoding.UTF8.GetString(stream.ToArray());
return wkt;
}

public static byte[] AsBinary(this Geometry geometry)
{
var stream = new MemoryStream();
geometry.Serialize<WkbSerializer>(stream);
return stream.ToArray();
}

public static PolyhedralSurface ToPolyhedralSurface(this MultiPolygon multipolygon)
{
var polyhedralsurface = new PolyhedralSurface
Expand Down Expand Up @@ -43,7 +60,7 @@ private static Polygon GetPolygon(LineString linestring)
};

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

return polygon;
}
Expand Down
5 changes: 1 addition & 4 deletions src/triangulator/Triangulator.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using EarcutNet;
using System;
using System.Collections.Generic;
using System.IO;
using System.Numerics;
using Wkx;

Expand All @@ -28,9 +27,7 @@ public static byte[] Triangulate(byte[] wkb)
{
var polyhedral = (PolyhedralSurface)Geometry.Deserialize<WkbSerializer>(wkb);
var triangulatedPolyhedral = Triangulate(polyhedral);
var stream = new MemoryStream();
triangulatedPolyhedral.Serialize<WkbSerializer>(stream);
return stream.ToArray();
return triangulatedPolyhedral.AsBinary();
}

public static List<Polygon> Triangulate(Polygon inputpolygon)
Expand Down

0 comments on commit b8c6312

Please sign in to comment.