Skip to content

Commit db7454c

Browse files
authored
Integrate Mapbox earcut.hpp for robust triangluation (tinyobjloader#298)
* Use simple triangulation rule for the quad face when triangulation. This partially solves issue no. 295. * Embed mapbox/earcut.hpp code(for robust triangulation) * Use mapbox/earcut.hpp for the polygon tessellation(for a polygon with 5 or more vertices). * Use Mapbox earcut(robust triangulation) by default for python binding. * Fix compile of Mapbox earcut code path.
1 parent 662d5e5 commit db7454c

4 files changed

Lines changed: 992 additions & 18 deletions

File tree

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Old version is available as `v0.9.x` branch https://github.com/syoyo/tinyobjload
2626

2727
## What's new
2828

29+
* 29 Jul, 2021 : Added Mapbox's earcut for robust triangulation. Also fixes triangulation bug.
2930
* 19 Feb, 2020 : The repository has been moved to https://github.com/tinyobjloader/tinyobjloader !
3031
* 18 May, 2019 : Python binding!(See `python` folder. Also see https://pypi.org/project/tinyobjloader/)
3132
* 14 Apr, 2019 : Bump version v2.0.0 rc0. New C++ API and python bindings!(1.x API still exists for backward compatibility)
@@ -139,6 +140,7 @@ TinyObjLoader is licensed under MIT license.
139140
### Third party licenses.
140141

141142
* pybind11 : BSD-style license.
143+
* mapbox earcut.hpp: ISC License.
142144

143145
## Usage
144146

@@ -241,10 +243,22 @@ TinyObjLoader now use `real_t` for floating point data type.
241243
Default is `float(32bit)`.
242244
You can enable `double(64bit)` precision by using `TINYOBJLOADER_USE_DOUBLE` define.
243245

246+
### Robust triangulation
247+
248+
When you enable `triangulation`(default is enabled),
249+
TinyObjLoader triangulate polygons(faces with 4 or more vertices).
250+
251+
Built-in trinagulation code may not work well in some polygon shape.
252+
253+
You can define `TINYOBJLOADER_USE_MAPBOX_EARCUT` for robust triangulation using `mapbox/earcut.hpp`.
254+
This requires C++11 compiler though.
255+
244256
#### Example code (Deprecated API)
245257

246258
```c++
247259
#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
260+
// Optional. define TINYOBJLOADER_USE_MAPBOX_EARCUT gives robust trinagulation. Requires C++11
261+
//#define TINYOBJLOADER_USE_MAPBOX_EARCUT
248262
#include "tiny_obj_loader.h"
249263

250264
std::string inputfile = "cornell_box.obj";
@@ -315,6 +329,8 @@ for (size_t s = 0; s < shapes.size(); s++) {
315329

316330
```c++
317331
#define TINYOBJLOADER_IMPLEMENTATION // define this in only *one* .cc
332+
// Optional. define TINYOBJLOADER_USE_MAPBOX_EARCUT gives robust trinagulation. Requires C++11
333+
//#define TINYOBJLOADER_USE_MAPBOX_EARCUT
318334
#include "tiny_obj_loader.h"
319335

320336

@@ -353,7 +369,7 @@ for (size_t s = 0; s < shapes.size(); s++) {
353369
tinyobj::real_t vx = attrib.vertices[3*size_t(idx.vertex_index)+0];
354370
tinyobj::real_t vy = attrib.vertices[3*size_t(idx.vertex_index)+1];
355371
tinyobj::real_t vz = attrib.vertices[3*size_t(idx.vertex_index)+2];
356-
372+
357373
// Check if `normal_index` is zero or positive. negative = no normal data
358374
if (idx.normal_index >= 0) {
359375
tinyobj::real_t nx = attrib.normals[3*size_t(idx.normal_index)+0];

examples/viewer/viewer.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,24 @@
2323
#include <GLFW/glfw3.h>
2424

2525
#define TINYOBJLOADER_IMPLEMENTATION
26+
// TINYOBJLOADER_USE_MAPBOX_EARCUT: Enable better triangulation. Requires C++11
27+
//#define TINYOBJLOADER_USE_MAPBOX_EARCUT
2628
#include "../../tiny_obj_loader.h"
2729

2830
#include "trackball.h"
2931

32+
#ifdef __clang__
33+
#pragma clang diagnostic push
34+
#pragma clang diagnostic ignored "-Weverything"
35+
#endif
36+
3037
#define STB_IMAGE_IMPLEMENTATION
3138
#include "stb_image.h"
3239

40+
#ifdef __clang__
41+
#pragma clang diagnostic pop
42+
#endif
43+
3344
#ifdef _WIN32
3445
#ifdef __cplusplus
3546
extern "C" {

python/tiny_obj_loader.cc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
// Need also define this in `binding.cc`(and all compilation units)
33
#define TINYOBJLOADER_USE_DOUBLE
44

5+
// Use robust triangulation by using Mapbox earcut.
6+
#define TINYOBJLOADER_USE_MAPBOX_EARCUT
7+
58
#define TINYOBJLOADER_IMPLEMENTATION
69
#include "tiny_obj_loader.h"

0 commit comments

Comments
 (0)