From eb59a31fd507ad330f81bf2da84c7f1bdaa85c20 Mon Sep 17 00:00:00 2001 From: Thomas Fischer <thomas.fischer@ufz.de> Date: Tue, 15 Jun 2021 14:41:19 +0200 Subject: [PATCH] [GL/OctTree] Clang format. --- GeoLib/OctTree-impl.h | 199 +++++++++++++++++++++++++----------------- GeoLib/OctTree.h | 52 ++++++----- 2 files changed, 145 insertions(+), 106 deletions(-) diff --git a/GeoLib/OctTree-impl.h b/GeoLib/OctTree-impl.h index 297ec0e19e9..e044e7609e6 100644 --- a/GeoLib/OctTree-impl.h +++ b/GeoLib/OctTree-impl.h @@ -10,43 +10,53 @@ * http://www.opengeosys.org/project/license */ -namespace GeoLib { - +namespace GeoLib +{ template <typename POINT, std::size_t MAX_POINTS> template <typename T> -OctTree<POINT, MAX_POINTS>* OctTree<POINT, MAX_POINTS>::createOctTree(T ll, T ur, - double eps) +OctTree<POINT, MAX_POINTS>* OctTree<POINT, MAX_POINTS>::createOctTree( + T ll, T ur, double eps) { // compute an axis aligned cube around the points ll and ur const double dx(ur[0] - ll[0]); const double dy(ur[1] - ll[1]); const double dz(ur[2] - ll[2]); - if (dx >= dy && dx >= dz) { - ll[1] -= (dx-dy)/2.0; - ur[1] += (dx-dy)/2.0; - ll[2] -= (dx-dz)/2.0; - ur[2] += (dx-dz)/2.0; - } else { - if (dy >= dx && dy >= dz) { - ll[0] -= (dy-dx)/2.0; - ur[0] += (dy-dx)/2.0; - ll[2] -= (dy-dz)/2.0; - ur[2] += (dy-dz)/2.0; - } else { - ll[0] -= (dz-dx)/2.0; - ur[0] += (dz-dx)/2.0; - ll[1] -= (dz-dy)/2.0; - ur[1] += (dz-dy)/2.0; + if (dx >= dy && dx >= dz) + { + ll[1] -= (dx - dy) / 2.0; + ur[1] += (dx - dy) / 2.0; + ll[2] -= (dx - dz) / 2.0; + ur[2] += (dx - dz) / 2.0; + } + else + { + if (dy >= dx && dy >= dz) + { + ll[0] -= (dy - dx) / 2.0; + ur[0] += (dy - dx) / 2.0; + ll[2] -= (dy - dz) / 2.0; + ur[2] += (dy - dz) / 2.0; + } + else + { + ll[0] -= (dz - dx) / 2.0; + ur[0] += (dz - dx) / 2.0; + ll[1] -= (dz - dy) / 2.0; + ur[1] += (dz - dy) / 2.0; } } if (eps == 0.0) { eps = std::numeric_limits<double>::epsilon(); } - for (std::size_t k(0); k<3; ++k) { - if (ur[k] - ll[k] > 0.0) { + for (std::size_t k(0); k < 3; ++k) + { + if (ur[k] - ll[k] > 0.0) + { ur[k] += (ur[k] - ll[k]) * 1e-6; - } else { + } + else + { ur[k] += eps; } } @@ -63,19 +73,19 @@ OctTree<POINT, MAX_POINTS>::~OctTree() } template <typename POINT, std::size_t MAX_POINTS> -bool OctTree<POINT, MAX_POINTS>::addPoint(POINT * pnt, POINT *& ret_pnt) +bool OctTree<POINT, MAX_POINTS>::addPoint(POINT* pnt, POINT*& ret_pnt) { // first do a range query using a epsilon box around the point pnt std::vector<POINT*> query_pnts; - MathLib::Point3d min( - std::array<double,3>{{(*pnt)[0]-_eps, (*pnt)[1]-_eps, (*pnt)[2]-_eps}}); - MathLib::Point3d max( - std::array<double,3>{{(*pnt)[0]+_eps, (*pnt)[1]+_eps, (*pnt)[2]+_eps}}); + MathLib::Point3d min(std::array<double, 3>{ + {(*pnt)[0] - _eps, (*pnt)[1] - _eps, (*pnt)[2] - _eps}}); + MathLib::Point3d max(std::array<double, 3>{ + {(*pnt)[0] + _eps, (*pnt)[1] + _eps, (*pnt)[2] + _eps}}); getPointsInRange(min, max, query_pnts); - auto const it = std::find_if( - query_pnts.begin(), query_pnts.end(), [pnt, this](auto const* p) { - return MathLib::sqrDist(*p, *pnt) < _eps * _eps; - }); + auto const it = + std::find_if(query_pnts.begin(), query_pnts.end(), + [pnt, this](auto const* p) + { return MathLib::sqrDist(*p, *pnt) < _eps * _eps; }); if (it != query_pnts.end()) { ret_pnt = *it; @@ -83,15 +93,19 @@ bool OctTree<POINT, MAX_POINTS>::addPoint(POINT * pnt, POINT *& ret_pnt) } // the point pnt is not yet in the OctTree - if (isOutside(pnt)) { + if (isOutside(pnt)) + { ret_pnt = nullptr; return false; } // at this place it holds true that the point is within [_ll, _ur] - if (!_is_leaf) { - for (auto c : _children) { - if (c->addPoint_(pnt, ret_pnt)) { + if (!_is_leaf) + { + for (auto c : _children) + { + if (c->addPoint_(pnt, ret_pnt)) + { return true; } if (ret_pnt != nullptr) @@ -103,9 +117,12 @@ bool OctTree<POINT, MAX_POINTS>::addPoint(POINT * pnt, POINT *& ret_pnt) ret_pnt = pnt; - if (_pnts.size () < MAX_POINTS) { + if (_pnts.size() < MAX_POINTS) + { _pnts.push_back(pnt); - } else { // i.e. _pnts.size () == MAX_POINTS + } + else + { // i.e. _pnts.size () == MAX_POINTS splitNode(pnt); _pnts.clear(); } @@ -114,8 +131,8 @@ bool OctTree<POINT, MAX_POINTS>::addPoint(POINT * pnt, POINT *& ret_pnt) template <typename POINT, std::size_t MAX_POINTS> template <typename T> -void OctTree<POINT, MAX_POINTS>::getPointsInRange(T const& min, T const& max, - std::vector<POINT*> &pnts) const +void OctTree<POINT, MAX_POINTS>::getPointsInRange( + T const& min, T const& max, std::vector<POINT*>& pnts) const { if (_ur[0] < min[0] || _ur[1] < min[1] || _ur[2] < min[2]) { @@ -127,40 +144,49 @@ void OctTree<POINT, MAX_POINTS>::getPointsInRange(T const& min, T const& max, return; } - if (_is_leaf) { + if (_is_leaf) + { std::copy_if(_pnts.begin(), _pnts.end(), std::back_inserter(pnts), - [&min, &max](auto const* p) { + [&min, &max](auto const* p) + { return (min[0] <= (*p)[0] && (*p)[0] < max[0] && min[1] <= (*p)[1] && (*p)[1] < max[1] && min[2] <= (*p)[2] && (*p)[2] < max[2]); }); - } else { - for (std::size_t k(0); k<8; k++) { + } + else + { + for (std::size_t k(0); k < 8; k++) + { _children[k]->getPointsInRange(min, max, pnts); } } } template <typename POINT, std::size_t MAX_POINTS> -OctTree<POINT, MAX_POINTS>::OctTree( - MathLib::Point3d const& ll, MathLib::Point3d const& ur, double eps) +OctTree<POINT, MAX_POINTS>::OctTree(MathLib::Point3d const& ll, + MathLib::Point3d const& ur, double eps) : _ll(ll), _ur(ur), _is_leaf(true), _eps(eps) { _children.fill(nullptr); } template <typename POINT, std::size_t MAX_POINTS> -bool OctTree<POINT, MAX_POINTS>::addPoint_(POINT * pnt, POINT *& ret_pnt) +bool OctTree<POINT, MAX_POINTS>::addPoint_(POINT* pnt, POINT*& ret_pnt) { - if (isOutside(pnt)) { + if (isOutside(pnt)) + { ret_pnt = nullptr; return false; } // at this place it holds true that the point is within [_ll, _ur] - if (!_is_leaf) { - for (auto c : _children) { - if (c->addPoint_(pnt, ret_pnt)) { + if (!_is_leaf) + { + for (auto c : _children) + { + if (c->addPoint_(pnt, ret_pnt)) + { return true; } if (ret_pnt != nullptr) @@ -171,9 +197,12 @@ bool OctTree<POINT, MAX_POINTS>::addPoint_(POINT * pnt, POINT *& ret_pnt) } ret_pnt = pnt; - if (_pnts.size() < MAX_POINTS) { + if (_pnts.size() < MAX_POINTS) + { _pnts.push_back(pnt); - } else { // i.e. _pnts.size () == MAX_POINTS + } + else + { // i.e. _pnts.size () == MAX_POINTS splitNode(pnt); _pnts.clear(); } @@ -181,16 +210,19 @@ bool OctTree<POINT, MAX_POINTS>::addPoint_(POINT * pnt, POINT *& ret_pnt) } template <typename POINT, std::size_t MAX_POINTS> -bool OctTree<POINT, MAX_POINTS>::addPointToChild(POINT * pnt) +bool OctTree<POINT, MAX_POINTS>::addPointToChild(POINT* pnt) { if (isOutside(pnt)) { return false; } - if (_pnts.size() < MAX_POINTS) { + if (_pnts.size() < MAX_POINTS) + { _pnts.push_back(pnt); - } else { // i.e. _pnts.size () == MAX_POINTS + } + else + { // i.e. _pnts.size () == MAX_POINTS splitNode(pnt); _pnts.clear(); } @@ -198,39 +230,39 @@ bool OctTree<POINT, MAX_POINTS>::addPointToChild(POINT * pnt) } template <typename POINT, std::size_t MAX_POINTS> -void OctTree<POINT, MAX_POINTS>::splitNode(POINT * pnt) +void OctTree<POINT, MAX_POINTS>::splitNode(POINT* pnt) { const double x_mid((_ur[0] + _ll[0]) / 2.0); const double y_mid((_ur[1] + _ll[1]) / 2.0); const double z_mid((_ur[2] + _ll[2]) / 2.0); - MathLib::Point3d p0(std::array<double,3>{{x_mid, y_mid, _ll[2]}}); - MathLib::Point3d p1(std::array<double,3>{{_ur[0], _ur[1], z_mid}}); + MathLib::Point3d p0(std::array<double, 3>{{x_mid, y_mid, _ll[2]}}); + MathLib::Point3d p1(std::array<double, 3>{{_ur[0], _ur[1], z_mid}}); // create child NEL - _children[static_cast<std::int8_t>(Quadrant::NEL)] - = new OctTree<POINT, MAX_POINTS> (p0, p1, _eps); + _children[static_cast<std::int8_t>(Quadrant::NEL)] = + new OctTree<POINT, MAX_POINTS>(p0, p1, _eps); // create child NWL p0[0] = _ll[0]; p1[0] = x_mid; - _children[static_cast<std::int8_t>(Quadrant::NWL)] - = new OctTree<POINT, MAX_POINTS> (p0, p1, _eps); + _children[static_cast<std::int8_t>(Quadrant::NWL)] = + new OctTree<POINT, MAX_POINTS>(p0, p1, _eps); // create child SWL p0[1] = _ll[1]; p1[1] = y_mid; - _children[static_cast<std::int8_t>(Quadrant::SWL)] - = new OctTree<POINT, MAX_POINTS> (_ll, p1, _eps); + _children[static_cast<std::int8_t>(Quadrant::SWL)] = + new OctTree<POINT, MAX_POINTS>(_ll, p1, _eps); // create child NEU - _children[static_cast<std::int8_t>(Quadrant::NEU)] - = new OctTree<POINT, MAX_POINTS> (p1, _ur, _eps); + _children[static_cast<std::int8_t>(Quadrant::NEU)] = + new OctTree<POINT, MAX_POINTS>(p1, _ur, _eps); // create child SEL p0[0] = x_mid; p1[0] = _ur[0]; - _children[static_cast<std::int8_t>(Quadrant::SEL)] - = new OctTree<POINT, MAX_POINTS> (p0, p1, _eps); + _children[static_cast<std::int8_t>(Quadrant::SEL)] = + new OctTree<POINT, MAX_POINTS>(p0, p1, _eps); // create child NWU p0[0] = _ll[0]; @@ -239,25 +271,26 @@ void OctTree<POINT, MAX_POINTS>::splitNode(POINT * pnt) p1[0] = x_mid; p1[1] = _ur[1]; p1[2] = _ur[2]; - _children[static_cast<std::int8_t>(Quadrant::NWU)] - = new OctTree<POINT, MAX_POINTS> (p0, p1, _eps); + _children[static_cast<std::int8_t>(Quadrant::NWU)] = + new OctTree<POINT, MAX_POINTS>(p0, p1, _eps); // create child SWU p0[1] = _ll[1]; p1[1] = y_mid; - _children[static_cast<std::int8_t>(Quadrant::SWU)] - = new OctTree<POINT, MAX_POINTS> (p0, p1, _eps); + _children[static_cast<std::int8_t>(Quadrant::SWU)] = + new OctTree<POINT, MAX_POINTS>(p0, p1, _eps); // create child SEU p0[0] = x_mid; p1[0] = _ur[0]; p1[1] = y_mid; p1[2] = _ur[2]; - _children[static_cast<std::int8_t>(Quadrant::SEU)] - = new OctTree<POINT, MAX_POINTS> (p0, p1, _eps); + _children[static_cast<std::int8_t>(Quadrant::SEU)] = + new OctTree<POINT, MAX_POINTS>(p0, p1, _eps); // add the passed point pnt to the children at first - for (std::size_t k(0); k < 8; k++) { + for (std::size_t k(0); k < 8; k++) + { if (_children[k]->addPointToChild(pnt)) { break; @@ -266,9 +299,12 @@ void OctTree<POINT, MAX_POINTS>::splitNode(POINT * pnt) // distribute points to sub quadtrees const std::size_t n_pnts(_pnts.size()); - for (std::size_t j(0); j < n_pnts; j++) { - for (auto c : _children) { - if (c->addPointToChild(_pnts[j])) { + for (std::size_t j(0); j < n_pnts; j++) + { + for (auto c : _children) + { + if (c->addPointToChild(_pnts[j])) + { break; } } @@ -277,7 +313,7 @@ void OctTree<POINT, MAX_POINTS>::splitNode(POINT * pnt) } template <typename POINT, std::size_t MAX_POINTS> -bool OctTree<POINT, MAX_POINTS>::isOutside(POINT * pnt) const +bool OctTree<POINT, MAX_POINTS>::isOutside(POINT* pnt) const { if ((*pnt)[0] < _ll[0] || (*pnt)[1] < _ll[1] || (*pnt)[2] < _ll[2]) { @@ -289,5 +325,4 @@ bool OctTree<POINT, MAX_POINTS>::isOutside(POINT * pnt) const } return false; } -} // end namespace GeoLib - +} // end namespace GeoLib diff --git a/GeoLib/OctTree.h b/GeoLib/OctTree.h index 880f5d65a72..3b1d17ff42a 100644 --- a/GeoLib/OctTree.h +++ b/GeoLib/OctTree.h @@ -15,18 +15,19 @@ #pragma once #include <cstdint> -#include <vector> #include <limits> +#include <vector> #include "MathLib/MathTools.h" #include "MathLib/Point3d.h" -namespace GeoLib { - +namespace GeoLib +{ /// @tparam POINT point data type the OctTree will use /// @tparam MAX_POINTS maximum number of pointers of POINT in a leaf template <typename POINT, std::size_t MAX_POINTS> -class OctTree { +class OctTree +{ public: /// Create an OctTree object. The arguments ll and ur are used to compute a /// cube domain the OctTree will living in. @@ -45,8 +46,8 @@ public: /// inside a OctTree leaf may be more expensive. The value should be /// chosen application dependent. [default 8] template <typename T> - static OctTree<POINT, MAX_POINTS>* createOctTree(T ll, T ur, - double eps = std::numeric_limits<double>::epsilon()); + static OctTree<POINT, MAX_POINTS>* createOctTree( + T ll, T ur, double eps = std::numeric_limits<double>::epsilon()); /// Destroys the children of this node. @attention Does not destroy the /// pointers to the managed objects. @@ -64,14 +65,15 @@ public: /// (3) In case ret_pnt is neither equal to pnt nor equal to nullptr, /// another item within the eps distance is already in the OctTree and the /// pointer to this object is returned. - /// @return If the point can be inserted the method returns true, else false. - bool addPoint(POINT * pnt, POINT *& ret_pnt); + /// @return If the point can be inserted the method returns true, else + /// false. + bool addPoint(POINT* pnt, POINT*& ret_pnt); /// range query - returns all points inside the range [min[0], max[0]) x /// [min[1], max[1]) x [min[2], max[2]) template <typename T> - void - getPointsInRange(T const& min, T const& max, std::vector<POINT*> &pnts) const; + void getPointsInRange(T const& min, T const& max, + std::vector<POINT*>& pnts) const; #ifndef NDEBUG MathLib::Point3d const& getLowerLeftCornerPoint() const { return _ll; } @@ -90,22 +92,24 @@ private: /// @param eps the euclidean distance as a threshold to make objects unique OctTree(MathLib::Point3d const& ll, MathLib::Point3d const& ur, double eps); - enum class Quadrant : std::int8_t { - NEL = 0, //!< north east lower - NWL, //!< north west lower - SWL, //!< south west lower - SEL, //!< south east lower - NEU, //!< south west upper - NWU, //!< south west upper - SWU, //!< south west upper - SEU //!< south east upper + enum class Quadrant : std::int8_t + { + NEL = 0, //!< north east lower + NWL, //!< north west lower + SWL, //!< south west lower + SEL, //!< south east lower + NEU, //!< south west upper + NWU, //!< south west upper + SWU, //!< south west upper + SEU //!< south east upper }; /// This method tries to add the given point to the OctTree. If necessary /// for adding the point, new nodes will be inserted into the OctTree. /// @param pnt, ret_pnt see documentation of addPoint() - /// @return If the point can be inserted the method returns true, else false. - bool addPoint_(POINT * pnt, POINT *& ret_pnt); + /// @return If the point can be inserted the method returns true, else + /// false. + bool addPoint_(POINT* pnt, POINT*& ret_pnt); /** * This method adds the given point to the OctTree. If necessary, @@ -118,12 +122,12 @@ private: /// Creates the child nodes of this leaf and distribute the points stored /// in _pnts to the children. /// @param pnt the pointer to the points that is responsible for the split - void splitNode(POINT * pnt); + void splitNode(POINT* pnt); /// checks if the given point pnt is outside of the OctTree node. /// @param pnt the point that check is performed on /// @return true if the point is outside of the OctTree node. - bool isOutside(POINT * pnt) const; + bool isOutside(POINT* pnt) const; /// children are sorted: /// _children[0] is north east lower child @@ -147,6 +151,6 @@ private: double const _eps; }; -} // end namespace GeoLib +} // end namespace GeoLib #include "OctTree-impl.h" -- GitLab