bertt.triangulator 1.7.0

dotnet add package bertt.triangulator --version 1.7.0
                    
NuGet\Install-Package bertt.triangulator -Version 1.7.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="bertt.triangulator" Version="1.7.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="bertt.triangulator" Version="1.7.0" />
                    
Directory.Packages.props
<PackageReference Include="bertt.triangulator" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add bertt.triangulator --version 1.7.0
                    
#r "nuget: bertt.triangulator, 1.7.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package bertt.triangulator@1.7.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=bertt.triangulator&version=1.7.0
                    
Install as a Cake Addin
#tool nuget:?package=bertt.triangulator&version=1.7.0
                    
Install as a Cake Tool

Triangulator

.NET Standard 2.0 library for triangulating 2D/3D WKB geometries (PolyhedralSurface/MultiPolygon/Polygon/Multiline/Line) using Earcut algorithm.

NuGet

https://www.nuget.org/packages/bertt.triangulator/

Sample code

var buildingWkb = File.ReadAllBytes(@"testdata/building.wkb");
var wkbTriangulated = Triangulator.Triangulate(buildingWkb);
var triangulatedGeometry = (PolyhedralSurface)Geometry.Deserialize<WkbSerializer>(wkbTriangulated);
Assert.IsTrue(triangulatedGeometry.Geometries.Count == 22);

Sample result triangulating polyhedralsurface:

From:

POLYHEDRALSURFACE Z (((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)),((0 0 0, 0 1 0, 0 1 1, 0 0 1, 0 0 0)), ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)), ((1 1 1, 1 0 1, 0 0 1, 0 1 1, 1 1 1)),((1 1 1, 1 0 1, 1 0 0, 1 1 0, 1 1 1)),((1 1 1, 1 1 0, 0 1 0, 0 1 1, 1 1 1)))

To:

POLYHEDRALSURFACE Z (((0 0 0,0 1 0,1 0 0,0 0 0)),((1 1 0,1 0 0,0 1 0,1 1 0)),((0 0 1,0 0 0,0 1 0,0 0 1)),((0 1 0,0 1 1,0 0 1,0 1 0)),((0 0 1,0 0 0,1 0 0,0 0 1)),((1 0 0,1 0 1,0 0 1,1 0 0)),((1 1 1,1 0 1,0 1 1,1 1 1)),((0 0 1,0 1 1,1 0 1,0 0 1)),((1 1 0,1 1 1,1 0 1,1 1 0)),((1 0 1,1 0 0,1 1 0,1 0 1)),((1 1 1,1 1 0,0 1 1,1 1 1)),((0 1 0,0 1 1,1 1 0,0 1 0)))

Sample result triangulation with interior rings:

<img src="holes.png" width="300" />

Sample with multiple interior rings:

image

Remarks

  • Input wkb must be of type PolyhedralSurface/MultiPolygon/Polygon/Line/Multiline, otherwise an error will occur;
  • Triangulated geometry is returned as WKB (as PolyhedralSurface/MultiPolygon);

Polygons Method

3D Triangulation is performed in 2D mode, by projecting each input polygon to yz, zx or xy plane. The plane to project to is determined by the normal vector of the input polygon.

Pseudo code for calculating plane to project to (nb: using absolute normals here):

yz: when (normal(x) > normal(y)) and (normal(x) > normal(z))
zx: else when (normal(y) > normal(z))
xy: all other cases

For triangulation the fast Earcut method is used. After triangulation the resulting triangles are 'unflattened' to get the 3D triangles.

After that, each triangle is checked to have the same direction as the original polygon by calculating the dot product between the normal of the polygon and the normal of the triangle. If the dot product is negative, then the two vectors point in opposite directions and the triangle will be inverted.

lines Method

Triangles of (multi)lines can be calculate using the Triangulate method using the following parameters:

  • (Multi)LineString lineString: line geometry

  • float radius = 1

  • int radialSegments = 8

Sample code:

var wkt = "LINESTRING(-10 0 0,0 0 0,0 10 0)";
var line = (LineString)Geometry.Deserialize<WktSerializer>(wkt);
var triangles = Triangulator.Triangulate(line,2);

Benchmark

todo

Unit Testing

In the unit test there is an conversion method from triangles to glTF 2.0 using SharpGLTF (https://github.com/vpenades/SharpGLTF) for visual inspections.

Dependencies

wkx-sharp - https://github.com/cschwarz/wkx-sharp for handling geometries

History

2025-12-16: release 1.7.0: to .NET Standard 2.0 library

2025-03-05: release 1.6.0: remove THREE dependency for lines + to .NET 6.0

2024-09-03: release 1.5.2: fix interior rings triangulation

2024-08-08: release 1.5.1: add support for lines without z (assuming z=0)

2024-07-30: release 1.5.0: to .NET 8 + use LineCurve instead of CatmullRomCurve (better performance + more accurate)

2024-02-20: release 1.4.3: fix lines of 2 points

2024-02-20: release 1.4.2: add multi-line support

2024-02-14: release 1.4.1: make tubularSegments and radialSegments optional

2024-02-14: release 1.4.0: add support for lines

2024-02-07: release 1.3.1: normals algorithm fix for higher precision

2024-02-07: release 1.3.0: add support for interior rings + polygons

2023-09-26: release 1.2.3: fix normal close to 0

2023-09-22: release 1.2.2: fix normals calculation

2023-08-30: release 1.2.1: add support for Geometry type as input (in addition of WKB)

2023-08-30: release 1.2: add support for MultiPolygon

2020-09-01: release 1.1: to NET 6

2020-06-10: release 1.0.3

2020-06-09: release 1.0.2

2020-06-08: release 1.0.1

2020-06-07: release 1.0.0 Initial release

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on bertt.triangulator:

Repository Stars
Geodan/pg2b3dm
Tool for creating 3D Tiles from PostGIS geometries
Version Downloads Last Updated
1.7.0 1,397 12/16/2025
1.6.0 1,251 3/5/2025
1.5.2 5,785 9/3/2024
1.5.1 499 8/8/2024
1.5.0 356 7/30/2024
1.4.3 982 2/20/2024
1.4.2 321 2/20/2024
1.4.1 363 2/14/2024
1.4.0 291 2/14/2024
1.3.1 352 2/8/2024
1.3.0 312 2/7/2024
1.2.3 767 9/26/2023
1.2.2 322 9/22/2023
1.2.1 422 8/30/2023
1.2.0 319 8/30/2023
1.1.0 851 9/1/2022
1.0.3 1,040 6/10/2020
1.0.2 706 6/9/2020
1.0.1 762 6/8/2020
1.0.0 803 6/7/2020

Fix interior rings