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

Fix supertriangle calculation for tiny bounding boxes #175

Merged
merged 2 commits into from
May 2, 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
13 changes: 9 additions & 4 deletions CDT/include/Triangulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,8 +988,12 @@ void Triangulation<T, TNearPointLocator>::addSuperTriangle(const Box2d<T>& box)
(box.min.x + box.max.x) / T(2), (box.min.y + box.max.y) / T(2)};
const T w = box.max.x - box.min.x;
const T h = box.max.y - box.min.y;
T r = std::sqrt(w * w + h * h) / T(2); // incircle radius
r = r > 0 ? r * T(1.1) : T(1e-6);
T r = std::max(w, h); // incircle radius upper bound

// Note: make sure radius is big enough. Constants chosen experimentally.
// - for tiny bounding boxes: use 1.0 as the smallest radius
// - multiply radius by 2.0 for extra safety margin
r = std::max(T(2) * r, T(1));

// Note: for very large floating point numbers rounding can lead to wrong
// super-triangle coordinates. This is a very rare corner-case so the
Expand All @@ -999,8 +1003,9 @@ void Triangulation<T, TNearPointLocator>::addSuperTriangle(const Box2d<T>& box)
r *= T(2);
}

const T R = T(2) * r; // excircle radius
const T shiftX = R * std::sqrt(T(3)) / T(2); // R * cos(30 deg)
const T R = T(2) * r; // excircle radius
const T cos_30_deg = 0.8660254037844386; // note: (std::sqrt(3.0) / 2.0)
const T shiftX = R * cos_30_deg;
const V2d<T> posV1 = {center.x - shiftX, center.y - r};
const V2d<T> posV2 = {center.x + shiftX, center.y - r};
const V2d<T> posV3 = {center.x, center.y + R};
Expand Down
14 changes: 14 additions & 0 deletions CDT/tests/cdt.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -945,3 +945,17 @@ TEST_CASE("Regression test: hanging edge in pseudo-poly", "")
cdt.insertEdges(ee);
REQUIRE(CDT::verifyTopology(cdt));
}

TEST_CASE("Regression test #174: super-triangle of tiny bounding box", "")
{
auto cdt = Triangulation<double>{};
const auto vv = Vertices<double>{
{{45802.2779561576462583616375923, 169208.540894783218391239643097},
{45802.2779561576317064464092255, 169208.540894783218391239643097},
{45802.2779561576462583616375923, 169208.540894783247495070099831}},
};
REQUIRE_NOTHROW(cdt.insertVertices(vv));
REQUIRE(CDT::verifyTopology(cdt));
cdt.eraseSuperTriangle();
REQUIRE(cdt.triangles.size() == std::size_t(1));
}
88 changes: 44 additions & 44 deletions CDT/tests/expected/cdt__as-provided_ignore_all.txt
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
203
0 1 59 4294967295 13 11
0 9 2 2 22 4294967295
0 10 9 3 36 1
0 11 10 4 38 2
0 12 11 5 40 3
0 13 12 6 42 4
0 14 13 7 44 5
0 15 14 8 46 6
0 16 15 9 48 7
0 17 16 10 50 8
0 18 17 11 52 9
0 59 18 0 56 10
1 2 65 4294967295 23 15
1 63 59 14 142 0
1 64 63 15 146 13
1 65 64 12 147 14
2 3 51 17 26 23
2 4 3 18 24 16
2 5 4 19 27 17
2 6 5 20 29 18
2 7 6 21 31 19
2 8 7 22 32 20
2 9 8 1 34 21
2 51 65 16 108 12
3 4 41 17 28 25
0 1 59 4294967295 12 10
0 10 2 2 22 4294967295
0 11 10 3 38 1
0 12 11 4 40 2
0 13 12 5 42 3
0 14 13 6 44 4
0 15 14 7 46 5
0 16 15 8 48 6
0 17 16 9 50 7
0 18 17 10 52 8
0 59 18 0 56 9
1 2 65 4294967295 23 14
1 63 59 13 142 0
1 64 63 14 146 12
1 65 64 11 147 13
2 3 51 16 26 23
2 4 3 17 24 15
2 5 4 18 27 16
2 6 5 19 29 17
2 7 6 20 31 18
2 8 7 21 32 19
2 9 8 22 34 20
2 10 9 1 36 21
2 51 65 15 108 11
3 4 41 16 28 25
3 41 42 24 92 26
3 42 51 25 93 16
4 5 40 18 30 28
3 42 51 25 93 15
4 5 40 17 30 28
4 40 41 27 92 24
5 6 39 19 31 30
5 6 39 18 31 30
5 39 40 29 90 27
6 7 39 20 33 29
7 8 38 21 35 33
6 7 39 19 33 29
7 8 38 20 35 33
7 38 39 32 90 31
8 9 37 22 37 35
8 9 37 21 37 35
8 37 38 34 89 32
9 10 36 2 39 37
9 10 36 22 39 37
9 36 37 36 87 34
10 11 35 3 41 39
10 11 35 2 41 39
10 35 36 38 86 36
11 12 34 4 43 41
11 12 34 3 43 41
11 34 35 40 82 38
12 13 33 5 45 43
12 13 33 4 45 43
12 33 34 42 77 40
13 14 32 6 47 45
13 14 32 5 47 45
13 32 33 44 76 42
14 15 31 7 49 47
14 15 31 6 49 47
14 31 32 46 81 44
15 16 30 8 51 49
15 16 30 7 51 49
15 30 31 48 80 46
16 17 29 9 53 51
16 17 29 8 53 51
16 29 30 50 80 48
17 18 28 10 55 53
17 18 28 9 55 53
17 28 29 52 79 50
18 19 27 56 57 55
18 27 28 54 75 52
18 59 19 11 58 54
18 59 19 10 58 54
19 20 27 59 62 54
19 59 70 56 141 60
19 69 20 60 63 57
Expand Down Expand Up @@ -141,12 +141,12 @@
57 77 76 129 169 138
58 59 60 141 142 128
58 70 59 132 58 140
59 63 60 13 144 140
59 63 60 12 144 140
60 62 61 144 145 125
60 63 62 142 146 143
61 62 66 143 148 126
62 63 64 144 14 147
62 64 65 146 15 148
62 63 64 144 13 147
62 64 65 146 14 148
62 65 66 147 108 145
67 83 82 150 179 114
67 84 83 151 181 149
Expand Down
88 changes: 44 additions & 44 deletions CDT/tests/expected/cdt__as-provided_resolve_all.txt
Original file line number Diff line number Diff line change
@@ -1,61 +1,61 @@
203
0 1 59 4294967295 13 11
0 9 2 2 22 4294967295
0 10 9 3 36 1
0 11 10 4 38 2
0 12 11 5 40 3
0 13 12 6 42 4
0 14 13 7 44 5
0 15 14 8 46 6
0 16 15 9 48 7
0 17 16 10 50 8
0 18 17 11 52 9
0 59 18 0 56 10
1 2 65 4294967295 23 15
1 63 59 14 142 0
1 64 63 15 146 13
1 65 64 12 147 14
2 3 51 17 26 23
2 4 3 18 24 16
2 5 4 19 27 17
2 6 5 20 29 18
2 7 6 21 31 19
2 8 7 22 32 20
2 9 8 1 34 21
2 51 65 16 108 12
3 4 41 17 28 25
0 1 59 4294967295 12 10
0 10 2 2 22 4294967295
0 11 10 3 38 1
0 12 11 4 40 2
0 13 12 5 42 3
0 14 13 6 44 4
0 15 14 7 46 5
0 16 15 8 48 6
0 17 16 9 50 7
0 18 17 10 52 8
0 59 18 0 56 9
1 2 65 4294967295 23 14
1 63 59 13 142 0
1 64 63 14 146 12
1 65 64 11 147 13
2 3 51 16 26 23
2 4 3 17 24 15
2 5 4 18 27 16
2 6 5 19 29 17
2 7 6 20 31 18
2 8 7 21 32 19
2 9 8 22 34 20
2 10 9 1 36 21
2 51 65 15 108 11
3 4 41 16 28 25
3 41 42 24 92 26
3 42 51 25 93 16
4 5 40 18 30 28
3 42 51 25 93 15
4 5 40 17 30 28
4 40 41 27 92 24
5 6 39 19 31 30
5 6 39 18 31 30
5 39 40 29 90 27
6 7 39 20 33 29
7 8 38 21 35 33
6 7 39 19 33 29
7 8 38 20 35 33
7 38 39 32 90 31
8 9 37 22 37 35
8 9 37 21 37 35
8 37 38 34 89 32
9 10 36 2 39 37
9 10 36 22 39 37
9 36 37 36 87 34
10 11 35 3 41 39
10 11 35 2 41 39
10 35 36 38 86 36
11 12 34 4 43 41
11 12 34 3 43 41
11 34 35 40 82 38
12 13 33 5 45 43
12 13 33 4 45 43
12 33 34 42 77 40
13 14 32 6 47 45
13 14 32 5 47 45
13 32 33 44 76 42
14 15 31 7 49 47
14 15 31 6 49 47
14 31 32 46 81 44
15 16 30 8 51 49
15 16 30 7 51 49
15 30 31 48 80 46
16 17 29 9 53 51
16 17 29 8 53 51
16 29 30 50 80 48
17 18 28 10 55 53
17 18 28 9 55 53
17 28 29 52 79 50
18 19 27 56 57 55
18 27 28 54 75 52
18 59 19 11 58 54
18 59 19 10 58 54
19 20 27 59 62 54
19 59 70 56 141 60
19 69 20 60 63 57
Expand Down Expand Up @@ -141,12 +141,12 @@
57 77 76 129 169 138
58 59 60 141 142 128
58 70 59 132 58 140
59 63 60 13 144 140
59 63 60 12 144 140
60 62 61 144 145 125
60 63 62 142 146 143
61 62 66 143 148 126
62 63 64 144 14 147
62 64 65 146 15 148
62 63 64 144 13 147
62 64 65 146 14 148
62 65 66 147 108 145
67 83 82 150 179 114
67 84 83 151 181 149
Expand Down
Loading
Loading