Skip to content
0.2.0-dev Download ZIP
118 lines ·4.1 KB Raw
1// Copyright 2026 The Manifold Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <cstdint>
18#include <unordered_map>
19#include <vector>
20
21#include "manifold/polygon.h"
22#include "shared.h"
23
24namespace manifold {
25
26struct HalfedgeTriangulation {
27 std::vector<Halfedge> halfedges;
28 size_t contourEnd = 0;
29 double epsilon = -1;
30
31 void AddContours(const PolygonsIdx& polys) {
32 size_t numContourEdges = 0;
33 for (const SimplePolygonIdx& poly : polys) numContourEdges += poly.size();
34 halfedges.reserve(halfedges.size() + numContourEdges);
35 edge2halfedge.reserve(edge2halfedge.size() + numContourEdges);
36 for (const SimplePolygonIdx& poly : polys) {
37 for (size_t i = 0; i < poly.size(); ++i) {
38 const int start = poly[i].idx;
39 const int end = poly[i + 1 < poly.size() ? i + 1 : 0].idx;
40 // Store the exterior contour halfedge, opposite the filled contour.
41 AddHalfedge(end, start);
42 }
43 }
44 contourEnd = halfedges.size();
45 }
46
47 void ReserveTriangles(size_t numTri) {
48 halfedges.reserve(contourEnd + 3 * numTri);
49 edge2halfedge.reserve(edge2halfedge.size() + numTri);
50 }
51
52 void AddTriangle(int first, int second, int third) {
53 AddHalfedge(first, second);
54 AddHalfedge(second, third);
55 AddHalfedge(third, first);
56 }
57
58 size_t NumTri() const { return (halfedges.size() - contourEnd) / 3; }
59
60 std::vector<ivec3> Triangles() const {
61 std::vector<ivec3> triangles;
62 triangles.reserve(NumTri());
63 for (size_t edge = contourEnd; edge < halfedges.size(); edge += 3) {
64 triangles.push_back({halfedges[edge].startVert,
65 halfedges[edge + 1].startVert,
66 halfedges[edge + 2].startVert});
67 }
68 return triangles;
69 }
70
71 void Finalize() {
72#ifdef MANIFOLD_DEBUG
73 DEBUG_ASSERT(edge2halfedge.empty(), topologyErr,
74 "triangulation has unpaired halfedges");
75 for (size_t i = 0; i < halfedges.size(); ++i) {
76 const int pair = halfedges[i].pairedHalfedge;
77 DEBUG_ASSERT(pair >= 0 && pair < static_cast<int>(halfedges.size()),
78 topologyErr, "invalid paired halfedge");
79 DEBUG_ASSERT(halfedges[pair].pairedHalfedge == static_cast<int>(i),
80 topologyErr, "halfedge pair is not reciprocal");
81 DEBUG_ASSERT(halfedges[i].startVert == halfedges[pair].endVert &&
82 halfedges[i].endVert == halfedges[pair].startVert,
83 topologyErr, "halfedge pair endpoints do not match");
84 }
85#endif
86 edge2halfedge.clear();
87 edge2halfedge.rehash(0);
88 }
89
90 private:
91 std::unordered_map<uint64_t, std::vector<int>> edge2halfedge;
92
93 static uint64_t EdgeKey(int start, int end) {
94 return (uint64_t{static_cast<uint32_t>(start)} << 32) |
95 static_cast<uint32_t>(end);
96 }
97
98 void AddHalfedge(int start, int end) {
99 const int halfedge = halfedges.size();
100 Halfedge data = {start, end, -1, -1};
101 auto reverse = edge2halfedge.find(EdgeKey(end, start));
102 if (reverse != edge2halfedge.end() && !reverse->second.empty()) {
103 data.pairedHalfedge = reverse->second.back();
104 halfedges[data.pairedHalfedge].pairedHalfedge = halfedge;
105 reverse->second.pop_back();
106 if (reverse->second.empty()) edge2halfedge.erase(reverse);
107 } else {
108 edge2halfedge[EdgeKey(start, end)].push_back(halfedge);
109 }
110 halfedges.push_back(data);
111 }
112};
113
114HalfedgeTriangulation TriangulateIdxHalfedges(const PolygonsIdx& polys,
115 double epsilon = -1,
116 bool allowConvex = true);
117
118} // namespace manifold