Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for TIN geometry type #178

Merged
merged 11 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Features:

- Cesium: LOD support and Outlines support (using CESIUM_primitive_outline);

- Triangulation of input geometries LineStrings/Polygon/MultiPolygon/PolyhedralSurface with Z values;
- Triangulation of input geometries LineStrings/Polygon/MultiPolygon/PolyhedralSurface/TIN with Z values;

- Docker support.

Expand Down
2 changes: 1 addition & 1 deletion src/b3dm.tileset/GeometricErrorCalculator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public static double[] GetGeometricErrors(double maxGeometricError, List<int> lo
res.Add(0);
}

return res.ToArray(); ;
return res.ToArray();
}
}
32 changes: 32 additions & 0 deletions src/pg2b3dm.database.tests/UnitTest1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public async Task Setup()
await _containerPostgres.ExecScriptAsync(initScript1);
var initScript2 = File.ReadAllText("./postgres-db/2_create_delaware_table.sql");
await _containerPostgres.ExecScriptAsync(initScript2);
var initScript3 = File.ReadAllText("./postgres-db/3_create_geom_table.sql");
await _containerPostgres.ExecScriptAsync(initScript3);
}

[TearDown]
Expand Down Expand Up @@ -105,4 +107,34 @@ public void LodTest()
new List<Tile>());
Assert.That(tiles.Count, Is.EqualTo(145));
}


[Test]
public void GeometryTest()
{
var connectionString = _containerPostgres.GetConnectionString();
var conn = new NpgsqlConnection(connectionString);
var bbox_wgs84 = BoundingBoxRepository.GetBoundingBoxForTable(conn, "geom_test", "geom3d");
Directory.CreateDirectory("output/content");
var center_wgs84 = bbox_wgs84.bbox.GetCenter();
var translation = SpatialConverter.GeodeticToEcef((double)center_wgs84.X!, (double)center_wgs84.Y!, 0);
var trans = new double[] { translation.X, translation.Y, translation.Z };
var implicitTiler = new QuadtreeTiler(conn, "geom_test", 4326, "geom3d", 50, string.Empty,
trans,
string.Empty,
string.Empty,
string.Empty,
"output/content",
new List<int>() { 0 },
skipCreateTiles: false);
var tile = new Tile(0, 0, 0) {
BoundingBox = bbox_wgs84.bbox.ToArray()
};
var tiles = implicitTiler.GenerateTiles(
bbox_wgs84.bbox,
tile,
new List<Tile>(), createGltf:true);
Assert.That(tiles.Count, Is.EqualTo(1));
}

}
3 changes: 3 additions & 0 deletions src/pg2b3dm.database.tests/pg2b3dm.database.tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<None Update="postgres-db\2_create_delaware_table.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="postgres-db\3_create_geom_table.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="testfixtures\delaware.sqlite">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
10 changes: 10 additions & 0 deletions src/pg2b3dm.database.tests/postgres-db/3_create_geom_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS geom_test
(
ID SERIAL PRIMARY KEY,
geom3d geometry(GeometryZ,4326)
);

CREATE INDEX ON geom_test USING gist(st_centroid(st_envelope(geom3d)));

INSERT INTO geom_test(geom3d) VALUES('SRID=4326;TIN Z (((-46.15991163092363 25.647893009411945 0,-41.68044158790308 36.46229033951616 0,-43.38761402466139 30.834497029047316 0,-46.15991163092363 25.647893009411945 0)),((-49.89079656440356 21.10179656440359 0,-41.104 42.315 0,-41.68044158790308 36.46229033951616 0,-49.89079656440356 21.10179656440359 0)),((-46.15991163092363 25.647893009411945 0,-49.89079656440356 21.10179656440359 0,-41.68044158790308 36.46229033951616 0,-46.15991163092363 25.647893009411945 0)),((-54.436893009411904 17.370911630923658 0,-59.62349702904727 14.598614024661408 0,-49.89079656440356 21.10179656440359 0,-54.436893009411904 17.370911630923658 0)),((-59.62349702904727 14.598614024661408 0,-65.25129033951612 12.891441587903092 0,-49.89079656440356 21.10179656440359 0,-59.62349702904727 14.598614024661408 0)),((-65.25129033951612 12.891441587903092 0,-71.10399999999996 12.314999999999998 0,-82.58450297095263 14.598614024661373 0,-65.25129033951612 12.891441587903092 0)),((-71.10399999999996 12.314999999999998 0,-76.9567096604838 12.891441587903074 0,-82.58450297095263 14.598614024661373 0,-71.10399999999996 12.314999999999998 0)),((-65.25129033951612 12.891441587903092 0,-82.58450297095263 14.598614024661373 0,-49.89079656440356 21.10179656440359 0,-65.25129033951612 12.891441587903092 0)),((-59.62349702904745 70.03138597533865 0,-98.82038597533864 53.79550297095259 0,-65.2512903395163 71.73855841209694 0,-59.62349702904745 70.03138597533865 0)),((-87.77110699058801 17.370911630923608 0,-96.04808836907631 25.64789300941187 0,-82.58450297095263 14.598614024661373 0,-87.77110699058801 17.370911630923608 0)),((-92.31720343559638 21.101796564403525 0,-96.04808836907631 25.64789300941187 0,-87.77110699058801 17.370911630923608 0,-92.31720343559638 21.101796564403525 0)),((-96.04808836907631 25.64789300941187 0,-98.82038597533864 53.79550297095259 0,-59.62349702904745 70.03138597533865 0,-96.04808836907631 25.64789300941187 0)),((-98.82038597533857 30.834497029047228 0,-101.104 42.3149999999999 0,-98.82038597533864 53.79550297095259 0,-98.82038597533857 30.834497029047228 0)),((-100.5275584120969 36.46229033951606 0,-101.104 42.3149999999999 0,-98.82038597533857 30.834497029047228 0,-100.5275584120969 36.46229033951606 0)),((-98.82038597533857 30.834497029047228 0,-98.82038597533864 53.79550297095259 0,-96.04808836907631 25.64789300941187 0,-98.82038597533857 30.834497029047228 0)),((-100.52755841209694 48.167709660483744 0,-98.82038597533864 53.79550297095259 0,-101.104 42.3149999999999 0,-100.52755841209694 48.167709660483744 0)),((-82.58450297095263 14.598614024661373 0,-96.04808836907631 25.64789300941187 0,-59.62349702904745 70.03138597533865 0,-82.58450297095263 14.598614024661373 0)),((-96.04808836907642 58.982106990587965 0,-92.31720343559651 63.52820343559634 0,-98.82038597533864 53.79550297095259 0,-96.04808836907642 58.982106990587965 0)),((-92.31720343559651 63.52820343559634 0,-87.77110699058818 67.25908836907628 0,-82.58450297095281 70.03138597533855 0,-92.31720343559651 63.52820343559634 0)),((-92.31720343559651 63.52820343559634 0,-82.58450297095281 70.03138597533855 0,-98.82038597533864 53.79550297095259 0,-92.31720343559651 63.52820343559634 0)),((-82.58450297095281 70.03138597533855 0,-71.10400000000014 72.315 0,-65.2512903395163 71.73855841209694 0,-82.58450297095281 70.03138597533855 0)),((-76.956709660484 71.73855841209689 0,-71.10400000000014 72.315 0,-82.58450297095281 70.03138597533855 0,-76.956709660484 71.73855841209689 0)),((-82.58450297095281 70.03138597533855 0,-65.2512903395163 71.73855841209694 0,-98.82038597533864 53.79550297095259 0,-82.58450297095281 70.03138597533855 0)),((-82.58450297095263 14.598614024661373 0,-59.62349702904745 70.03138597533865 0,-49.89079656440356 21.10179656440359 0,-82.58450297095263 14.598614024661373 0)),((-59.62349702904745 70.03138597533865 0,-41.104 42.315 0,-49.89079656440356 21.10179656440359 0,-59.62349702904745 70.03138597533865 0)),((-54.436893009412074 67.25908836907645 0,-49.89079656440369 63.52820343559654 0,-59.62349702904745 70.03138597533865 0,-54.436893009412074 67.25908836907645 0)),((-49.89079656440369 63.52820343559654 0,-41.680441587903125 48.16770966048404 0,-59.62349702904745 70.03138597533865 0,-49.89079656440369 63.52820343559654 0)),((-46.159911630923744 58.98210699058822 0,-43.38761402466147 53.79550297095286 0,-49.89079656440369 63.52820343559654 0,-46.159911630923744 58.98210699058822 0)),((-43.38761402466147 53.79550297095286 0,-41.680441587903125 48.16770966048404 0,-49.89079656440369 63.52820343559654 0,-43.38761402466147 53.79550297095286 0)),((-41.680441587903125 48.16770966048404 0,-41.104 42.315 0,-59.62349702904745 70.03138597533865 0,-41.680441587903125 48.16770966048404 0)))')

14 changes: 14 additions & 0 deletions src/wkb2gltf.core.tests/GlbCreatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,20 @@ public static void CreateGlbForMultiLineString()
var fileName = Path.Combine(TestContext.CurrentContext.WorkDirectory, "multilinestring.glb");
File.WriteAllBytes(@"multilinestring.glb", bytes);
}

[Test]
public static void CreateGlbForTin()
{
var pipelineWkt = "TIN Z (((0 0 0,0 0 1,0 1 0,0 0 0)),((0 0 0,0 1 0,1 1 0,0 0 0)))";
var g = Geometry.Deserialize<WktSerializer>(pipelineWkt);
var translation = new double[] { 0, 0, 0 };
var triangles = GeometryProcessor.GetTriangles(g, 100, translation);
Assert.That(triangles.Count, Is.EqualTo(2));

var bytes = GlbCreator.GetGlb(new List<List<Triangle>>() { triangles });
var fileName = Path.Combine(TestContext.CurrentContext.WorkDirectory, "tin.glb");
File.WriteAllBytes(@"tin.glb", bytes);
}

[Test]
public static void CreateGlbForLineString()
Expand Down
53 changes: 30 additions & 23 deletions src/wkb2gltf.core/GeometryProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Triangulate;
using Wkx;

Expand All @@ -9,21 +10,28 @@ public static class GeometryProcessor
{
public static List<Triangle> GetTriangles(Geometry geometry, int batchId, double[] translation = null, double[] scale = null, ShaderColors shadercolors = null, float? radius = null)
{
if (geometry is not Polygon && geometry is not MultiPolygon && geometry is not PolyhedralSurface && geometry is not LineString && geometry is not MultiLineString) {
throw new NotSupportedException($"Geometry type {geometry.GeometryType} is not supported");
}

var r = radius.HasValue ? radius.Value : (float)1.0f;

List<Polygon> geometries;
if (geometry is LineString) {
geometries = GetTrianglesFromLines((LineString)geometry, r, translation, scale);
}
else if (geometry is MultiLineString) {
geometries = GetTrianglesFromLines((MultiLineString)geometry, r, translation, scale);
}
else {
geometries = GetTrianglesFromPolygons(geometry, translation, scale);
switch (geometry)
{
case LineString lineString:
geometries = GetTrianglesFromLines(lineString, r, translation, scale);
break;
case MultiLineString multiLineString:
geometries = GetTrianglesFromLines(multiLineString, r, translation, scale);
break;
case Polygon:
case MultiPolygon:
case PolyhedralSurface:
geometries = GetTrianglesFromPolygons(geometry, translation);
break;
case Tin tin:
var nmp = new PolyhedralSurface(tin.Geometries.Select(t => new Polygon(t.ExteriorRing.Points)));
geometries = GetTrianglesFromPolygons(nmp, translation);
break;
default:
throw new NotSupportedException($"Geometry type {geometry.GeometryType} is not supported");
}

var result = GetTriangles(batchId, shadercolors, geometries);
Expand Down Expand Up @@ -67,17 +75,16 @@ private static List<Polygon> GetTrianglesFromPolygons(Geometry geometry, double[

private static List<Polygon> GetGeometries(Geometry geometry)
{
if (geometry is Polygon) {
return new List<Polygon>() { (Polygon)geometry };
}
else if (geometry is MultiPolygon) {
return ((MultiPolygon)geometry).Geometries;
}
else if (geometry is PolyhedralSurface) {
return ((PolyhedralSurface)geometry).Geometries;
}
else {
throw new NotSupportedException($"Geometry type {geometry.GeometryType} is not supported");
switch (geometry)
{
case Polygon polygon:
return new List<Polygon>() { polygon };
case MultiPolygon multiPolygon:
return multiPolygon.Geometries;
case PolyhedralSurface surface:
return surface.Geometries;
default:
throw new NotSupportedException($"Geometry type {geometry.GeometryType} is not supported");
}
}

Expand Down