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

Bugfix/fix initialize with grid #48

Merged
merged 2 commits into from
Oct 26, 2021
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
52 changes: 32 additions & 20 deletions CDT/extras/InitializeWithGrid.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace detail
/**
* Generate grid vertices given of X- and Y-ticks
*
* @tparam OutputIt output iterator
* @tparam OutputVertIt output vertices iterator
* @tparam OutputTriIt output triangles iterator
* @tparam TXCoordIter iterator dereferencing to X coordinate
* @tparam TYCoordIter iterator dereferencing to Y coordinate
* @param outFirst the beginning of the destination range
Expand All @@ -35,9 +36,14 @@ namespace detail
* @param yfirst beginning of Y-ticks range
* @param ylast end of Y-ticks range
*/
template <typename OutputIt, typename TXCoordIter, typename TYCoordIter>
template <
typename OutputVertIt,
typename OutputTriIt,
typename TXCoordIter,
typename TYCoordIter>
void generateGridVertices(
OutputIt outFirst,
OutputVertIt outVertsFirst,
OutputTriIt outTrisFirst,
const TXCoordIter xfirst,
const TXCoordIter xlast,
const TYCoordIter yfirst,
Expand All @@ -53,33 +59,33 @@ void generateGridVertices(
TXCoordIter xiter = xfirst;
for(std::size_t ix = 0; xiter != xlast; ++xiter, ++ix)
{
Vertex<T> v;
v.pos = V2d<T>::make(*xiter, *yiter);

*outVertsFirst++ = V2d<T>::make(*xiter, *yiter);
const std::size_t i = iy * xres + ix;
TriIndVec vTris;
vTris.reserve(6);
// left-up
if(ix > 0 && iy < yres)
{
v.triangles.push_back(2 * (i - 1));
v.triangles.push_back(2 * (i - 1) + 1);
vTris.push_back(2 * (i - 1));
vTris.push_back(2 * (i - 1) + 1);
}
// right-up
if(ix < xres && iy < yres)
{
v.triangles.push_back(2 * i);
vTris.push_back(2 * i);
}
// left-down
if(ix > 0 && iy > 0)
{
v.triangles.push_back(2 * (i - xres - 1) + 1);
vTris.push_back(2 * (i - xres - 1) + 1);
}
// right-down
if(ix < xres && iy > 0)
{
v.triangles.push_back(2 * (i - xres));
v.triangles.push_back(2 * (i - xres) + 1);
vTris.push_back(2 * (i - xres));
vTris.push_back(2 * (i - xres) + 1);
}
*outFirst++ = v;
*outTrisFirst++ = vTris;
}
}
}
Expand All @@ -95,20 +101,20 @@ void generateGridVertices(
template <typename OutputIt>
void generateGridTriangles(
OutputIt outFirst,
const std::size_t xres,
const std::size_t yres)
const IndexSizeType xres,
const IndexSizeType yres)
{
for(std::size_t iy = 0; iy < yres; ++iy)
for(IndexSizeType iy = 0; iy < yres; ++iy)
{
for(std::size_t ix = 0; ix < xres; ++ix)
for(IndexSizeType ix = 0; ix < xres; ++ix)
{
// 2___3 v3
// |\ | /\
// | \ | n3/ \n2
// |__\| /____\
// 0 1 v1 n1 v2
const std::size_t i = iy * xres + ix;
const std::size_t iv = iy * (xres + 1) + ix;
const IndexSizeType i = iy * xres + ix;
const IndexSizeType iv = iy * (xres + 1) + ix;
const VertInd vv[4] = {iv, iv + 1, iv + xres + 1, iv + xres + 2};
Triangle t;

Expand Down Expand Up @@ -196,8 +202,14 @@ void initializeWithIrregularGrid(
const std::size_t yres = std::distance(yfirst, ylast) - 1;
out.triangles.reserve(xres * yres * 2);
out.vertices.reserve((xres + 1) * (yres + 1));
out.vertTris.reserve((xres + 1) * (yres + 1));
detail::generateGridVertices(
std::back_inserter(out.vertices), xfirst, xlast, yfirst, ylast);
std::back_inserter(out.vertices),
std::back_inserter(out.vertTris),
xfirst,
xlast,
yfirst,
ylast);
detail::generateGridTriangles(
std::back_inserter(out.triangles), xres, yres);
out.initializedWithCustomSuperGeometry();
Expand Down
3 changes: 2 additions & 1 deletion CDT/extras/VerifyTopology.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ namespace CDT
* - each of triangle's vertices has triangle as adjacent
*
* @tparam T type of vertex coordinates (e.g., float, double)
* @tparam TNearPointLocator class providing locating near point for efficiently
*/
template <typename T, typename TNearPointLocator>
template <typename T, typename TNearPointLocator = LocatorKDTree<T> >
inline bool verifyTopology(const CDT::Triangulation<T, TNearPointLocator>& cdt)
{
// Check if vertices' adjacent triangles contain vertex
Expand Down
22 changes: 22 additions & 0 deletions CDT/src/CDT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

#include "CDT.hpp"
#include "CDTUtils.hpp"
#include "InitializeWithGrid.h"
#include "VerifyTopology.h"

namespace CDT
{
Expand All @@ -39,6 +41,26 @@ template DuplicatesInfo RemoveDuplicatesAndRemapEdges<double>(
std::vector<V2d<double> >&,
std::vector<Edge>&);

template bool verifyTopology<float>(const CDT::Triangulation<float>&);
template bool verifyTopology<double>(const CDT::Triangulation<double>&);

template void initializeWithRegularGrid<float>(
float,
float,
float,
float,
std::size_t,
std::size_t,
Triangulation<float>&);
template void initializeWithRegularGrid<double>(
double,
double,
double,
double,
std::size_t,
std::size_t,
Triangulation<double>&);

} // namespace CDT

#endif