Skip to main content

A lite wrapper around Shewchuk's triangle.

Project description

TriangleLite

TriangleLite is a lite wrapper of the triangle library written in C++. It is created by Qingnan Zhou as a coding exercise to make using the triangle library less tedious.

Quick start

#include <trianglelite/trianglelite.h>

using Scalar = trianglelite::Scalar;
using Index = trianglelite::Index;

// Initialize a single unit triangle as input.
std::vector<Scalar> points{ x0, y0, x1, y1, ... };
std::vector<int> segments { s00, s01, s10, s11, ... };

// Set input.
trianglelite::Engine engine;
engine.set_in_points(points.data(), points.size()/2);
engine.set_in_segments(segments.data(), segments.size()/2);

// Set triangle configuration.
trianglelite::Config config;
config.max_area = 0.1;

// Run!
engine.run(config);

// Extract output.
Eigen::Matrix<Scalar, -1, 2> out_points = engine.get_out_points();
Eigen::Matrix<Index, -1, 2> out_triangles = engine.get_out_triangles();

A few notable things about TriangleLite:

  • All data structure and their member fields are properly initialized. Users only need to set a few relevant fields.
  • Data can be passed into and out of TriangleLite without any redundant copying or conversion. All inputs are passed to trianglelite::Engine in the form of raw C array, and all output can be extracted as Eigen::Map.
  • TriangleLite uses more human-readable Config object instead of command line switches.
  • TriangleLite comes with support for winding-number-based automatic hole detection (not shown in this example).

Build

mkdir build
cd build
cmake ..
make

Detailed usage

There are 4 steps involved in using TriangleLite: import input, configure, run and extract output. In the following code snippets, we assume engine is of type trianglelite::Engine.

Input

In order to be fully generic, all inputs to TriangleLite are passed in using raw C arrays. TriangleLite does not assume the ownership of these memories, and the user is responsible for keeping them valid throughout the lifetime of the engine object.

Import points

Input points must be stored in contiguous memory of Scalars in the form of [x0, y0, x1, y1, ...]. For example:

std::vector<Scalar> points{
    0.0, 0.0,  // point 0
    1.0, 0.0,  // point 1
    0.0, 1.0   // point 2
};
engine.set_in_points(points.data(), 3);

Import segments

Input segments are stored in contiguous memory of point indices. For example:

std::vector<Index> segments {
    0, 1,  // segment 1 connects vertex 0 and 1.
    1, 2,  // segment 2
    2, 0   // segemnt 3
};
engine.set_in_segments(segments.data(), 3);

Note that while triangle works with unoriented segments, it is actually beneficial to use oriented segments (i.e. each segments should be oriented in a counterclockwise fashion, which has interior on the left hand side). With oriented segments, TriangleLite can deduce holes using winding-number-based auto hole detection feature.

Import hole list

Without using the auto hole detection feature, users must provide a set of hole points to help triangle to identify holes. To set these hole points:

std::vector<Scalar> holes {
    0.0, 0.0,  // hole point 0
    1.0, 0.0   // hole point 1
};
engine.set_in_holes(holes.data(), 2);

Import triangles

Sometimes, we have a triangulation to start with and want to have it refined. The triangulation can be imported with the following snippet:

std::vector<Index> triangles {
    0, 1, 2,  // triangle 0
    2, 1, 3   // triangle 1
};
engine.set_in_triangles(triangles.data(), 2)

Import area constraint

For triangle refinement, one can import an area field to control triangle density. To do that:

std::vector<Scalar> areas {
   0.1, 0.2, 0.5, 0.1, ...
};
engine.set_in_areas(areas.data(), areas.size());

Set point and segment markers

One of the important features of triangle is to track the input points and segments in the output triangulation. This is done by setting the input point and segment markers:

std::vector<int> point_markers {
    ...
};
engine.set_in_point_markers(point_markers.data(), point_markers.size());

std::vector<int> segment_markers {
    ...
};
engine.set_in_segemnt_markers(segment_markers.data(), segment_markers.size());

Configure

The triangle library uses a set of command line switches to configure triangulation parameters. I find it a bit difficult to memorize. Instead, TriangleLite uses a Config struct to make the code more readable. Here is a list of fields one can configure:

Field name Type Description
min_angle Scalar Controls the triangulation quality. Default is 20 degree.
max_area Scalar Controls the triangulation density. Default is -1 (i.e. unconstrained).
max_num_steiner Index Number of inserted Steiner points. Default is -1 (i.e. unlimited).
verbose_level Index Verbosity level ranges from 0 to 4. 0: quiet, 4: debug only. Default is 1.
algorithm Enum DIVIDE_AND_CONQUER (default), SWEEP_LINE or INCREMENTAL.
convex_hull Bool Whether to triangulate the entire convex hull. Default is false.
conforming Bool Enforce all triangle to be Delaunay, not just constrained Delaunay. Default is false.
exact Bool Use exact arithmetic. Default is true.
split_boundary Bool Allow mesh boundary to be split. Default is true.
auto_hole_detection Bool Using winding number to automatically detect holes. Default is false.

Run

Once all inputs are imported, the next step is to triangulate the domain with the specified configuration:

engine.run(config);

Output

To extract the output triangulation:

Eigen::Matrix<Scalar, -1, 2> points = engine.get_out_points();
Eigen::Matrix<Index, -1, 3> triangles = engine.get_out_triangles();

Note that the above code copies data out of engine object. The return types of get_out_* methods are Eigen::Maps, which wrap around internal memories managed by TriangleLite. For users who are familiar with Eigen, it is possible to avoid this copy by working directly with Eigen::Map objects while keeping the engine object alive.

In addition to the basic triangulation, it is also possible to extract output edges, segments, triangle connectivity, point/segment markers:

Eigen::Matrix<Index, -1, 2> edges = engine.get_out_edges();
Eigen::Matrix<Index, -1, 2> segments = engine.get_out_segments();
Eigen::Matrix<Index, -1, 3> tri_neighbors = engine.get_out_triangle_neighbors();
Eigen::Matrix<Index, -1, 1> point_marker = engine.get_out_point_markers();
Eigen::Matrix<Index, -1, 1> segment_marker = engine.get_out_segment_markers();
Eigen::Matrix<Index, -1, 1> edge_marker = engine.get_out_edge_markers();
  • Edges are the edges of the triangulation.
  • Segments are a set of edges that maps back to input segments.
  • Triangle neighbors provide information of triangle-triangle connectivity.
  • Point/segment markers are markers that got mapped from the input point/segments.
  • Edge markers are markers that got mapped from the input segments to output edges.

More details about boundary markers can be found here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

trianglelite-0.1.0-cp312-cp312-win_amd64.whl (176.2 kB view details)

Uploaded CPython 3.12 Windows x86-64

trianglelite-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (228.0 kB view details)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

trianglelite-0.1.0-cp312-cp312-macosx_11_0_x86_64.whl (194.6 kB view details)

Uploaded CPython 3.12 macOS 11.0+ x86-64

trianglelite-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (173.4 kB view details)

Uploaded CPython 3.12 macOS 11.0+ ARM64

trianglelite-0.1.0-cp311-cp311-win_amd64.whl (176.7 kB view details)

Uploaded CPython 3.11 Windows x86-64

trianglelite-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (229.0 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

trianglelite-0.1.0-cp311-cp311-macosx_11_0_x86_64.whl (195.3 kB view details)

Uploaded CPython 3.11 macOS 11.0+ x86-64

trianglelite-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (174.2 kB view details)

Uploaded CPython 3.11 macOS 11.0+ ARM64

trianglelite-0.1.0-cp310-cp310-win_amd64.whl (176.8 kB view details)

Uploaded CPython 3.10 Windows x86-64

trianglelite-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (229.2 kB view details)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64

trianglelite-0.1.0-cp310-cp310-macosx_11_0_x86_64.whl (195.6 kB view details)

Uploaded CPython 3.10 macOS 11.0+ x86-64

trianglelite-0.1.0-cp310-cp310-macosx_11_0_arm64.whl (174.4 kB view details)

Uploaded CPython 3.10 macOS 11.0+ ARM64

trianglelite-0.1.0-cp39-cp39-win_amd64.whl (177.3 kB view details)

Uploaded CPython 3.9 Windows x86-64

trianglelite-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (229.5 kB view details)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64

trianglelite-0.1.0-cp39-cp39-macosx_11_0_x86_64.whl (195.9 kB view details)

Uploaded CPython 3.9 macOS 11.0+ x86-64

trianglelite-0.1.0-cp39-cp39-macosx_11_0_arm64.whl (174.6 kB view details)

Uploaded CPython 3.9 macOS 11.0+ ARM64

File details

Details for the file trianglelite-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 093432040071626d159d5e9719607e7473009710045f21d8e051d1e9f01df243
MD5 f4c8e140f52f8c3873e8ea7ba322d2bf
BLAKE2b-256 941bf3fc0ea3043cec38e25fdfa8baf0c4fe8c35f74cd24c1e2f65cba8a214e6

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ff85a6df7b4854978c0935850f61775ad38668a89ff3bfa093510c28763f034f
MD5 81dfab0f9661114e0ced7be81b640d90
BLAKE2b-256 360dc6a03b730713a1e66337ac41c7943be96c33601add0f07253052af0e7f7e

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp312-cp312-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp312-cp312-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 8f598579edc69231dcd30b6ef761d4b975d5aed8a8b4a7f696824905a0c07c54
MD5 b63cd8f453e8f72f0e9109aac051c4f9
BLAKE2b-256 6a495bcc00da59bdb696d770ea39047d15fb97831dc1e6582e1ed1d55cce7010

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 be67c04aaaf38f2e46048b681524f3cd335ef19998ac70fc3e6f8f3af332378f
MD5 7e16307808c96fc3e26c92f593f8153e
BLAKE2b-256 dfbf7ba9e1106924b29a4efddb5e39506bf50736ce60ad147bf27f7f69bf1819

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 f035cbe59cc25ad041c8676af9f3e2d0978fd62dc91fc5b08541dacd38209e06
MD5 732bb5d0d653498687eb4c95a4244b63
BLAKE2b-256 8263bf46e92e4d16613014803879b785a54d7968f07c22bd6137f40055f3167b

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 1fdc8a979b75d7e5e64b187cb276c9c416b43813f73de1c5139e80759a5cd31d
MD5 faaad8b51fe766b6dbb63fd62bd814ac
BLAKE2b-256 3f01ce5012774631ef75fe82b68ef86f5b94250e445a43eebb4466a707173d23

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp311-cp311-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp311-cp311-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 6ec39d28939b6f8c2f5cf6d9d249d10119c261e38af9a747307949bf782cfb9b
MD5 70732841791b5ac89fd54eafcee5bd19
BLAKE2b-256 4e6664f37373f2666a17b38bdd514ae979b521a40e46cca46eb8ef2ea18b1231

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 9d84979fab38c1cbb1940bb1369a56297b433356068dbcae05fc94ec0b9334e9
MD5 06e54f9ac860d65ba594b159eaef27c4
BLAKE2b-256 a6b2eaec067c5a39ced786acde96e8cdb4fecbe9da325457c2457031e0620567

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp310-cp310-win_amd64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp310-cp310-win_amd64.whl
Algorithm Hash digest
SHA256 f2a44c33cf3324a467ff2b3de1150fcb16ef4b40586007ff93ab7153c00e1749
MD5 52e79d744ccf4c2e5113664687f1d48f
BLAKE2b-256 0b5d236c615c2bb983f00df7408d6b39ed2bed7e5871b6c1eadc2d0698324742

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a0af524b2fb1c5cd2729cfece70f641d59afc2aa9e8d9319308691219aa4a89f
MD5 5d72020ecf701f9131e14a97772937bc
BLAKE2b-256 6d50ff72fc064a0de1fe92bb112289db2e3a41628f37305438df85f87d7473d3

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp310-cp310-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp310-cp310-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 9e205975a6787c74926439d013ba9fb2448d9c2b481a18ac6074c40a5006c1e6
MD5 41ec2eec260922263f8f7914528b2ea4
BLAKE2b-256 cbc12af66f1b1e081884057e19eadfaa5d95b3518f7fc3016d21dd63d304d623

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp310-cp310-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp310-cp310-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 2ed5e78c42c49121f8ab53f85fc57017320b5f4785f59041196f1f0c76e7f75d
MD5 1d3f7b32e3e356c5662a58ba77ed32d8
BLAKE2b-256 dfb0a2be46276e8f5bd555e1a6ec3ab9cdfab268d85621151314eb9376d1d81e

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp39-cp39-win_amd64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp39-cp39-win_amd64.whl
Algorithm Hash digest
SHA256 5ae9e291992d82d07a2a8ec09d56e47a863854f9990d6a98fde9377649067116
MD5 83814f4fe28a6c100766ab7642be4c7d
BLAKE2b-256 82657f05b82e2adb6c666c273da6b2dc923224254dd7cd2a90ac3180de05ab30

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 3528b82757479f0a38ffae62b5f789ff2cbc4546fe6c8093cc591207b4d56d2f
MD5 705352f525aa9a7720774b8175182fcb
BLAKE2b-256 cbf35e73d674261943795c4c955a78603fd6c323b0c54ed4d744de4077901c8a

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp39-cp39-macosx_11_0_x86_64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp39-cp39-macosx_11_0_x86_64.whl
Algorithm Hash digest
SHA256 b4760deb5e5da43e1acf85e30b0ed43a9dcc7ffa9093ee984e090a3a4aed76cc
MD5 22d6e50ba9d85ea339b612a666a1f0cd
BLAKE2b-256 39540ea090a41d52a60dfb16868de395294d697904454df310cbef3d5e089db7

See more details on using hashes here.

File details

Details for the file trianglelite-0.1.0-cp39-cp39-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for trianglelite-0.1.0-cp39-cp39-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 bae40b22e4317cf649c08ad9506caaf2d83ca4111eeea94ffa4f819e66a54802
MD5 9106400e54867e69439a6d673c51e871
BLAKE2b-256 7f93668d6a4b6c891edde23be595defba245e143b7541e0db88771dc5c08793b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page