CDT  1.4.1
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
11#ifndef CDT_HmXGv083vZCrT3OXASD9
12#define CDT_HmXGv083vZCrT3OXASD9
13
14#include <CDT.h>
15#include <CDTUtils.h>
16
17#ifdef CDT_CXX11_IS_SUPPORTED
18#include <algorithm>
19#endif
20#include <cstddef>
21#include <iterator>
22#include <vector>
23
24namespace CDT
25{
26namespace detail
27{
28
42template <
43 typename OutputVertIt,
44 typename OutputTriIt,
45 typename TXCoordIter,
46 typename TYCoordIter>
48 OutputVertIt outVertsFirst,
49 OutputTriIt outTrisFirst,
50 const TXCoordIter xfirst,
51 const TXCoordIter xlast,
52 const TYCoordIter yfirst,
53 const TYCoordIter ylast)
54{
55 typedef typename std::iterator_traits<TXCoordIter>::value_type T;
56 const std::size_t xres = std::distance(xfirst, xlast) - 1;
57 const std::size_t yres = std::distance(yfirst, ylast) - 1;
58
59 TXCoordIter yiter = yfirst;
60 for(std::size_t iy = 0; yiter != ylast; ++yiter, ++iy)
61 {
62 TXCoordIter xiter = xfirst;
63 for(std::size_t ix = 0; xiter != xlast; ++xiter, ++ix)
64 {
65 *outVertsFirst++ = V2d<T>::make(*xiter, *yiter);
66 const std::size_t i = iy * xres + ix;
67 TriIndVec vTris;
68 vTris.reserve(6);
69 // left-up
70 if(ix > 0 && iy < yres)
71 {
72 vTris.push_back(static_cast<TriInd>(2 * (i - 1)));
73 vTris.push_back(static_cast<TriInd>(2 * (i - 1) + 1));
74 }
75 // right-up
76 if(ix < xres && iy < yres)
77 {
78 vTris.push_back(static_cast<TriInd>(2 * i));
79 }
80 // left-down
81 if(ix > 0 && iy > 0)
82 {
83 vTris.push_back(static_cast<TriInd>(2 * (i - xres - 1) + 1));
84 }
85 // right-down
86 if(ix < xres && iy > 0)
87 {
88 vTris.push_back(static_cast<TriInd>(2 * (i - xres)));
89 vTris.push_back(static_cast<TriInd>(2 * (i - xres) + 1));
90 }
91#ifdef CDT_CXX11_IS_SUPPORTED
92 *outTrisFirst++ = std::move(vTris.front());
93#else
94 *outTrisFirst++ = vTris;
95#endif
96 }
97 }
98}
99
108template <typename OutputIt>
110 OutputIt outFirst,
111 const IndexSizeType xres,
112 const IndexSizeType yres)
113{
114 for(IndexSizeType iy = 0; iy < yres; ++iy)
115 {
116 for(IndexSizeType ix = 0; ix < xres; ++ix)
117 {
118 // 2___3 v3
119 // |\ | /\
120 // | \ | n3/ \n2
121 // |__\| /____\
122 // 0 1 v1 n1 v2
123 const IndexSizeType i = iy * xres + ix;
124 const IndexSizeType iv = iy * (xres + 1) + ix;
125 const VertInd vv[4] = {
126 VertInd(iv),
127 VertInd(iv + 1),
128 VertInd(iv + xres + 1),
129 VertInd(iv + xres + 2)};
130 {
131 const Triangle t = {
132 {vv[0], vv[1], vv[2]},
133 {TriInd(iy ? 2 * i - xres * 2 + 1 : noNeighbor),
134 TriInd(2 * i + 1),
135 TriInd(ix ? 2 * i - 1 : noNeighbor)}};
136 *outFirst++ = t;
137 }
138 {
139 const Triangle t = {
140 {vv[1], vv[3], vv[2]},
141 {TriInd(ix < xres - 1 ? 2 * i + 2 : noNeighbor),
142 TriInd(iy < yres - 1 ? 2 * i + xres * 2 : noNeighbor),
143 TriInd(2 * i)}};
144 *outFirst++ = t;
145 }
146 }
147 }
148}
149
150} // namespace detail
151
167template <typename T, typename TNearPointLocator>
169 const T xmin,
170 const T xmax,
171 const T ymin,
172 const T ymax,
173 const std::size_t xres,
174 const std::size_t yres,
176{
177 std::vector<T> xcoords;
178 std::vector<T> ycoords;
179 xcoords.reserve(xres + 1);
180 ycoords.reserve(yres + 1);
181 const T xstep = (xmax - xmin) / xres;
182 T x = xmin;
183 for(std::size_t ix = 0; ix <= xres; ++ix, x += xstep)
184 xcoords.push_back(x);
185 const T ystep = (ymax - ymin) / yres;
186 T y = ymin;
187 for(std::size_t iy = 0; iy <= yres; ++iy, y += ystep)
188 ycoords.push_back(y);
189
191 xcoords.begin(), xcoords.end(), ycoords.begin(), ycoords.end(), out);
192}
193
209template <
210 typename T,
211 typename TNearPointLocator,
212 typename TXCoordIter,
213 typename TYCoordIter>
215 const TXCoordIter xfirst,
216 const TXCoordIter xlast,
217 const TYCoordIter yfirst,
218 const TYCoordIter ylast,
220{
221 const std::size_t xres = std::distance(xfirst, xlast) - 1;
222 const std::size_t yres = std::distance(yfirst, ylast) - 1;
223 out.triangles.reserve(xres * yres * 2);
224 out.vertices.reserve((xres + 1) * (yres + 1));
225 out.VertTrisInternal().reserve((xres + 1) * (yres + 1));
226 detail::generateGridVertices(
227 std::back_inserter(out.vertices),
228 std::back_inserter(out.VertTrisInternal()),
229 xfirst,
230 xlast,
231 yfirst,
232 ylast);
233 detail::generateGridTriangles(
234 std::back_inserter(out.triangles),
235 static_cast<IndexSizeType>(xres),
236 static_cast<IndexSizeType>(yres));
238}
239
240} // namespace CDT
241
242#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:155
IndexSizeType VertInd
Vertex index.
Definition CDTUtils.h:142
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:144
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:259
static V2d make(T x, T y)
Create vector from X and Y coordinates.
Definition CDTUtils.hpp:23