gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
closestPoint.cpp
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 #include "closestPoint.h"
7 #include "GEntity.h"
8 #include "GEdge.h"
9 #include "GFace.h"
10 #include <vector>
11 
12 #if defined(HAVE_ANN)
13 static void oversample(std::vector<SPoint3> &s, double tol)
14 {
15  std::vector<SPoint3> t;
16  for(std::size_t i = 1; i < s.size(); i++) {
17  SPoint3 p0 = s[i - 1];
18  SPoint3 p1 = s[i];
19  double d = p0.distance(p1);
20  int N = (int)(d / tol);
21  t.push_back(p0);
22  for(int j = 1; j < N; j++) {
23  const double xi = (double)j / N;
24  t.push_back(p0 + (p1 - p0) * xi);
25  }
26  t.push_back(p1);
27  }
28  s = t;
29 }
30 #endif
31 
32 closestPointFinder::closestPointFinder(GEntity *ge, double e) : _tolerance(e)
33 {
34 #if defined(HAVE_ANN)
35  std::vector<SPoint3> pts;
36  if(ge->dim() == 1) {
37  GEdge *edge = ge->cast2Edge();
38  if(edge) {
39  std::vector<double> ts;
40  edge->discretize(_tolerance, pts, ts);
41  oversample(pts, _tolerance);
42  }
43  else {
44  Msg::Error("Unknown curve in closestPointFinder");
45  }
46  }
47  index = new ANNidx[1];
48  dist = new ANNdist[1];
49  zeronodes = annAllocPts(pts.size(), 3);
50  for(std::size_t k = 0; k < pts.size(); k++) {
51  zeronodes[k][0] = pts[k].x();
52  zeronodes[k][1] = pts[k].y();
53  zeronodes[k][2] = pts[k].z();
54  }
55  kdtree = new ANNkd_tree(zeronodes, pts.size(), 3);
56 #else
57  Msg::Error("Gmsh should be compiled with ANN to support closestPointFinder");
58 #endif
59 }
60 
62 {
63 #if defined(HAVE_ANN)
64  if(kdtree) delete kdtree;
65  if(zeronodes) annDeallocPts(zeronodes);
66  delete[] index;
67  delete[] dist;
68 #endif
69 }
70 
72 {
73 #if defined(HAVE_ANN)
74  double xyz[3] = {p.x(), p.y(), p.z()};
75  kdtree->annkSearch(xyz, 1, index, dist);
76  return SPoint3(zeronodes[index[0]][0], zeronodes[index[0]][1],
77  zeronodes[index[0]][2]);
78 #else
79  return p;
80 #endif
81 }
closestPointFinder::operator()
SPoint3 operator()(const SPoint3 &p)
Definition: closestPoint.cpp:71
GFace.h
closestPointFinder::closestPointFinder
closestPointFinder(GEntity *, double)
Definition: closestPoint.cpp:32
GEntity.h
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
SPoint3
Definition: SPoint3.h:14
GEntity
Definition: GEntity.h:31
GEdge::discretize
virtual void discretize(double tol, std::vector< SPoint3 > &dpts, std::vector< double > &ts)
Definition: GEdge.cpp:806
SPoint3::x
double x(void) const
Definition: SPoint3.h:125
GEdge.h
closestPoint.h
SPoint3::y
double y(void) const
Definition: SPoint3.h:127
SPoint3::distance
double distance(const SPoint3 &p) const
Definition: SPoint3.h:176
GEdge
Definition: GEdge.h:26
SPoint3::z
double z(void) const
Definition: SPoint3.h:129
GEntity::cast2Edge
GEdge * cast2Edge()
Definition: GEntity.cpp:123
GEntity::dim
virtual int dim() const
Definition: GEntity.h:196
closestPointFinder::_tolerance
double _tolerance
Definition: closestPoint.h:25
closestPointFinder::~closestPointFinder
~closestPointFinder()
Definition: closestPoint.cpp:61