TTK
Loading...
Searching...
No Matches
TopoMap.cpp
Go to the documentation of this file.
1#include <TopoMap.h>
2
4 // inherited from Debug: prefix will be printed at the beginning of every msg
5 this->setDebugMsgPrefix("TopoMap");
6}
7ttk::TopoMap::~TopoMap() = default;
8
9#if defined(TTK_ENABLE_QHULL) && defined(Qhull_FOUND)
10
11#include <libqhullcpp/Qhull.h>
12bool computeConvexHull_aux(const std::vector<double> &coords,
13 std::vector<size_t> &res,
14 std::string &errMsg) {
15 size_t nbPoint = coords.size() / 2;
16 char qHullFooStr[1] = ""; // We use no Qhull options.
17 orgQhull::Qhull qhull;
18 try {
19 qhull.runQhull(qHullFooStr, 2, nbPoint, coords.data(), qHullFooStr);
20 } catch(orgQhull::QhullError &e) {
21 errMsg = "Error with qHull module: " + std::string(e.what());
22 return false;
23 }
24
25 // Qhull gives us the coordinates of the points in the convex hull. Here we
26 // retrive the indices of this points in the list we provided. We will also
27 // compute the barycenter of the points in the convex hull.
28 for(const auto &u : qhull.vertexList()) {
29 const orgQhull::QhullPoint &qhullPt = u.point();
30 auto coordsCur = qhullPt.coordinates();
31 for(size_t j = 0; j < coords.size() / 2; j++) {
32 if(fabs(coords[2 * j] - coordsCur[0])
33 + fabs(coords[2 * j + 1] - coordsCur[1])
34 < EpsilonDBL) {
35 res.push_back(j);
36 break;
37 }
38 }
39 }
40
41 if(res.size() != qhull.vertexList().size()) {
42 errMsg = "Error : could not retrieve all vertices in the convex hull.";
43 return false;
44 }
45 return true;
46}
47
48#else // Using boost.
49
50#include <boost/geometry.hpp>
51#include <boost/geometry/geometries/adapted/boost_tuple.hpp>
52#include <boost/geometry/geometries/polygon.hpp>
53namespace bg = boost::geometry;
54BOOST_GEOMETRY_REGISTER_BOOST_TUPLE_CS(cs::cartesian)
55
56using Point = boost::tuple<double, double>;
57using Polygon = boost::geometry::model::polygon<Point>;
58using Mpoints = boost::geometry::model::multi_point<Point>;
59
60bool computeConvexHull_aux(const std::vector<double> &coords,
61 std::vector<size_t> &res,
62 std::string &errMsg) {
63 Mpoints multiPoints;
64 Polygon hull;
65 size_t nbPoint = coords.size() / 2;
66 for(size_t i = 0; i < nbPoint; i++) {
67 boost::geometry::append(
68 multiPoints, Point(coords[2 * i], coords[2 * i + 1]));
69 }
70
71 boost::geometry::convex_hull(multiPoints, hull);
72
73 // From the coordinates of the points in the convex hull, we find their
74 // indices.
75 for(const auto &boostPt : hull.outer()) {
76 double coordsCur[2] = {boostPt.get<0>(), boostPt.get<1>()};
77 for(size_t j = 0; j < coords.size() / 2; j++) {
78 if(fabs(coords[2 * j] - coordsCur[0])
79 + fabs(coords[2 * j + 1] - coordsCur[1])
80 < Epsilon) {
81 res.push_back(j);
82 break;
83 }
84 }
85 }
86 if(res.size() != hull.outer().size()) {
87 errMsg = "Error : could not retrieve all vertices in the convex hull.";
88 return false;
89 }
90
91 // Boost closes the polygon, hence the first and the last vertices are the
92 // identical. We do not want a duplicate vertex.
93 res.pop_back();
94 return true;
95}
96#endif
bool computeConvexHull_aux(const std::vector< double > &coords, std::vector< size_t > &res, std::string &errMsg)
Definition TopoMap.cpp:60
boost::geometry::model::multi_point< Point > Mpoints
Definition TopoMap.cpp:58
boost::geometry::model::polygon< Point > Polygon
Definition TopoMap.cpp:57
boost::tuple< double, double > Point
Definition TopoMap.cpp:56
void setDebugMsgPrefix(const std::string &prefix)
Definition Debug.h:364
~TopoMap() override