CDT  1.4.5
C++ library for constrained Delaunay triangulation
Loading...
Searching...
No Matches
InitializeWithGrid.h
Go to the documentation of this file.
1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
10
11#ifndef CDT_HmXGv083vZCrT3OXASD9
12#define CDT_HmXGv083vZCrT3OXASD9
13
14#include <CDT.h>
15#include <CDTUtils.h>
16
17#include <algorithm>
18#include <cstddef>
19#include <iterator>
20#include <vector>
21
22namespace CDT
23{
24namespace detail
25{
26
40template <
41 typename OutputVertIt,
42 typename OutputTriIt,
43 typename TXCoordIter,
44 typename TYCoordIter>
46 OutputVertIt outVertsFirst,
47 OutputTriIt outTrisFirst,
48 const TXCoordIter xfirst,
49 const TXCoordIter xlast,
50 const TYCoordIter yfirst,
51 const TYCoordIter ylast)
52{
53 typedef typename std::iterator_traits<TXCoordIter>::value_type T;
54 const std::size_t xres = std::distance(xfirst, xlast) - 1;
55 const std::size_t yres = std::distance(yfirst, ylast) - 1;
56
57 TXCoordIter yiter = yfirst;
58 for(std::size_t iy = 0; yiter != ylast; ++yiter, ++iy)
59 {
60 TXCoordIter xiter = xfirst;
61 for(std::size_t ix = 0; xiter != xlast; ++xiter, ++ix)
62 {
63 *outVertsFirst++ = V2d<T>(*xiter, *yiter);
64 const std::size_t i = iy * xres + ix;
65 TriIndVec vTris;
66 vTris.reserve(6);
67 // left-up
68 if(ix > 0 && iy < yres)
69 {
70 vTris.push_back(static_cast<TriInd>(2 * (i - 1)));
71 vTris.push_back(static_cast<TriInd>(2 * (i - 1) + 1));
72 }
73 // right-up
74 if(ix < xres && iy < yres)
75 {
76 vTris.push_back(static_cast<TriInd>(2 * i));
77 }
78 // left-down
79 if(ix > 0 && iy > 0)
80 {
81 vTris.push_back(static_cast<TriInd>(2 * (i - xres - 1) + 1));
82 }
83 // right-down
84 if(ix < xres && iy > 0)
85 {
86 vTris.push_back(static_cast<TriInd>(2 * (i - xres)));
87 vTris.push_back(static_cast<TriInd>(2 * (i - xres) + 1));
88 }
89 // note: only one adjacent triangle per vertex is stored
90 *outTrisFirst++ = vTris.front();
91 }
92 }
93}
94
103template <typename OutputIt>
105 OutputIt outFirst,
106 const IndexSizeType xres,
107 const IndexSizeType yres)
108{
109 for(IndexSizeType iy = 0; iy < yres; ++iy)
110 {
111 for(IndexSizeType ix = 0; ix < xres; ++ix)
112 {
113 // 2___3 v3
114 // |\ | /\
115 // | \ | n3/ \n2
116 // |__\| /____\
117 // 0 1 v1 n1 v2
118 const IndexSizeType i = iy * xres + ix;
119 const IndexSizeType iv = iy * (xres + 1) + ix;
120 const VertInd vv[4] = {
121 VertInd(iv),
122 VertInd(iv + 1),
123 VertInd(iv + xres + 1),
124 VertInd(iv + xres + 2)};
125 {
126 const Triangle t(
127 arr3(vv[0], vv[1], vv[2]),
128 arr3(
129 TriInd(iy ? 2 * i - xres * 2 + 1 : noNeighbor),
130 TriInd(2 * i + 1),
131 TriInd(ix ? 2 * i - 1 : noNeighbor)));
132 *outFirst++ = t;
133 }
134 {
135 const Triangle t(
136 arr3(vv[1], vv[3], vv[2]),
137 arr3(
138 TriInd(ix < xres - 1 ? 2 * i + 2 : noNeighbor),
139 TriInd(iy < yres - 1 ? 2 * i + xres * 2 : noNeighbor),
140 TriInd(2 * i)));
141 *outFirst++ = t;
142 }
143 }
144 }
145}
146
147} // namespace detail
148
164template <typename T, typename TNearPointLocator>
166 const T xmin,
167 const T xmax,
168 const T ymin,
169 const T ymax,
170 const std::size_t xres,
171 const std::size_t yres,
173{
174 std::vector<T> xcoords;
175 std::vector<T> ycoords;
176 xcoords.reserve(xres + 1);
177 ycoords.reserve(yres + 1);
178 const T xstep = (xmax - xmin) / xres;
179 T x = xmin;
180 for(std::size_t ix = 0; ix <= xres; ++ix, x += xstep)
181 xcoords.push_back(x);
182 const T ystep = (ymax - ymin) / yres;
183 T y = ymin;
184 for(std::size_t iy = 0; iy <= yres; ++iy, y += ystep)
185 ycoords.push_back(y);
186
188 xcoords.begin(), xcoords.end(), ycoords.begin(), ycoords.end(), out);
189}
190
206template <
207 typename T,
208 typename TNearPointLocator,
209 typename TXCoordIter,
210 typename TYCoordIter>
212 const TXCoordIter xfirst,
213 const TXCoordIter xlast,
214 const TYCoordIter yfirst,
215 const TYCoordIter ylast,
217{
218 const std::size_t xres = std::distance(xfirst, xlast) - 1;
219 const std::size_t yres = std::distance(yfirst, ylast) - 1;
220 out.triangles.reserve(xres * yres * 2);
221 out.vertices.reserve((xres + 1) * (yres + 1));
222 out.VertTrisInternal().reserve((xres + 1) * (yres + 1));
224 std::back_inserter(out.vertices),
225 std::back_inserter(out.VertTrisInternal()),
226 xfirst,
227 xlast,
228 yfirst,
229 ylast);
231 std::back_inserter(out.triangles),
232 static_cast<IndexSizeType>(xres),
233 static_cast<IndexSizeType>(yres));
235}
236
237} // namespace CDT
238
239#endif
Utilities and helpers.
Public API.
void generateGridVertices(OutputVertIt outVertsFirst, OutputTriIt outTrisFirst, const TXCoordIter xfirst, const TXCoordIter xlast, const TYCoordIter yfirst, const TYCoordIter ylast)
Generate grid vertices given of X- and Y-ticks.
void generateGridTriangles(OutputIt outFirst, const IndexSizeType xres, const IndexSizeType yres)
Generate grid triangles.
Data structure representing a 2D constrained Delaunay triangulation.
V2dVec vertices
triangulation's vertices
TriangleVec triangles
triangulation's triangles
void initializedWithCustomSuperGeometry()
Call this method after directly setting custom super-geometry via vertices and triangles members.
TriIndVec & VertTrisInternal()
Access internal vertex adjacent triangles.
Namespace containing triangulation functionality.
std::vector< TriInd > TriIndVec
Vector of triangle indices.
Definition CDTUtils.h:225
IndexSizeType VertInd
Vertex index.
Definition CDTUtils.h:207
array< T, 3 > arr3(const T &v0, const T &v1, const T &v2)
Needed for c++03 compatibility (no uniform initialization available)
Definition CDTUtils.h:128
void initializeWithIrregularGrid(const TXCoordIter xfirst, const TXCoordIter xlast, const TYCoordIter yfirst, const TYCoordIter ylast, Triangulation< T, TNearPointLocator > &out)
Make a triangulation that uses irregular grid triangles instead of super-triangle.
IndexSizeType TriInd
Triangle index.
Definition CDTUtils.h:209
void initializeWithRegularGrid(const T xmin, const T xmax, const T ymin, const T ymax, const std::size_t xres, const std::size_t yres, Triangulation< T, TNearPointLocator > &out)
Make a triangulation that uses regular grid triangles instead of super-triangle.
Triangulation triangle (counter-clockwise winding)
Definition CDTUtils.h:360
2D vector
Definition CDTUtils.h:145