gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
MVertexRTree.h
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #ifndef MVERTEX_RTREE
7 #define MVERTEX_RTREE
8 
9 #include <vector>
10 #include "GmshMessage.h"
11 #include "MVertex.h"
12 #include "rtree.h"
13 
14 // Stores MVertex pointers in an R-Tree so we can query unique vertices by their
15 // coordinates, up to a prescribed tolerance.
16 class MVertexRTree {
17 private:
19  double _tol;
20  static bool rtree_callback(MVertex *v, void *ctx)
21  {
22  MVertex **out = static_cast<MVertex **>(ctx);
23  *out = v;
24  return false; // we're done searching
25  }
26 
27 public:
28  MVertexRTree(double tolerance = 1.e-8)
29  {
31  _tol = tolerance;
32  }
34  {
35  _rtree->RemoveAll();
36  delete _rtree;
37  }
38  MVertex *insert(MVertex *v, bool warnIfExists = false,
39  std::set<MVertex *, MVertexPtrLessThan> *duplicates = nullptr)
40  {
41  MVertex *out;
42  double _min[3] = {v->x() - _tol, v->y() - _tol, v->z() - _tol};
43  double _max[3] = {v->x() + _tol, v->y() + _tol, v->z() + _tol};
44  if(!_rtree->Search(_min, _max, rtree_callback, &out)) {
45  _rtree->Insert(_min, _max, v);
46  return nullptr;
47  }
48  else {
49  if(duplicates) {
50  duplicates->insert(out);
51  duplicates->insert(v);
52  }
53  if(warnIfExists) {
54  Msg::Warning("Node %d (%.16g, %.16g, %.16g) already exists in the "
55  "mesh with tolerance %g as node %d (%.16g, %.16g, %.16g)",
56  v->getNum(), v->x(), v->y(), v->z(), _tol, out->getNum(),
57  out->x(), out->y(), out->z());
58  }
59  return out;
60  }
61  }
62  int insert(std::vector<MVertex *> &v, bool warnIfExists = false,
63  std::set<MVertex *, MVertexPtrLessThan> *duplicates = nullptr)
64  {
65  int num = 0;
66  for(std::size_t i = 0; i < v.size(); i++)
67  num += (insert(v[i], warnIfExists, duplicates) ? 1 : 0);
68  return num; // number of vertices not inserted
69  }
70  MVertex *find(double x, double y, double z)
71  {
72  MVertex *out;
73  double _min[3] = {x - _tol, y - _tol, z - _tol};
74  double _max[3] = {x + _tol, y + _tol, z + _tol};
75  if(_rtree->Search(_min, _max, rtree_callback, &out)) return out;
76  return nullptr;
77  }
78  std::size_t size() { return _rtree->Count(); }
79 };
80 
81 #endif
RTree::Count
int Count()
Count the data elements in this container. This is slow as no internal counter is maintained.
MVertexRTree::_tol
double _tol
Definition: MVertexRTree.h:19
MVertexRTree::_rtree
RTree< MVertex *, double, 3, double > * _rtree
Definition: MVertexRTree.h:18
MVertexRTree::MVertexRTree
MVertexRTree(double tolerance=1.e-8)
Definition: MVertexRTree.h:28
MVertex
Definition: MVertex.h:24
Msg::Warning
static void Warning(const char *fmt,...)
Definition: GmshMessage.cpp:543
MVertex::z
double z() const
Definition: MVertex.h:62
MVertex::getNum
std::size_t getNum() const
Definition: MVertex.h:86
MVertexRTree::~MVertexRTree
~MVertexRTree()
Definition: MVertexRTree.h:33
GmshMessage.h
rtree.h
RTree< MVertex *, double, 3, double >
MVertexRTree::insert
MVertex * insert(MVertex *v, bool warnIfExists=false, std::set< MVertex *, MVertexPtrLessThan > *duplicates=nullptr)
Definition: MVertexRTree.h:38
tolerance
#define tolerance
Definition: curvature.cpp:11
MVertex.h
RTree::RemoveAll
void RemoveAll()
Remove all entries from tree.
RTree::Insert
void Insert(const ELEMTYPE a_min[NUMDIMS], const ELEMTYPE a_max[NUMDIMS], const DATATYPE &a_dataId)
MVertexRTree::rtree_callback
static bool rtree_callback(MVertex *v, void *ctx)
Definition: MVertexRTree.h:20
MVertexRTree::size
std::size_t size()
Definition: MVertexRTree.h:78
RTree::Search
int Search(const ELEMTYPE a_min[NUMDIMS], const ELEMTYPE a_max[NUMDIMS], bool a_resultCallback(DATATYPE a_data, void *a_context), void *a_context)
MVertexRTree::find
MVertex * find(double x, double y, double z)
Definition: MVertexRTree.h:70
z
const double z
Definition: GaussQuadratureQuad.cpp:56
MVertexRTree
Definition: MVertexRTree.h:16
MVertexRTree::insert
int insert(std::vector< MVertex * > &v, bool warnIfExists=false, std::set< MVertex *, MVertexPtrLessThan > *duplicates=nullptr)
Definition: MVertexRTree.h:62
MVertex::y
double y() const
Definition: MVertex.h:61
MVertex::x
double x() const
Definition: MVertex.h:60