gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
NearestNeighbor.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 "GmshConfig.h"
7 #include "NearestNeighbor.h"
8 
9 #if defined(HAVE_ANN)
10 #include "ANN/ANN.h"
11 #endif
12 
14  {GMSH_FULLRC, "View", nullptr, -1.},
15 };
16 
17 extern "C" {
19 {
20  return new GMSH_NearestNeighborPlugin();
21 }
22 }
23 
25 {
26  return "Plugin(NearestNeighbor) computes the distance from each "
27  "point in `View' to its nearest neighbor.\n\n"
28  "If `View' < 0, the plugin is run on the current view.\n\n"
29  "Plugin(NearestNeighbor) is executed in-place.";
30 }
31 
33 {
34  return sizeof(NearestNeighborOptions_Number) / sizeof(StringXNumber);
35 }
36 
38 {
39  return &NearestNeighborOptions_Number[iopt];
40 }
41 
43 {
44  int iView = (int)NearestNeighborOptions_Number[0].def;
45 
46  PView *v1 = getView(iView, v);
47  if(!v1) return v;
48  PViewData *data1 = v1->getData();
49 
50  int totpoints = data1->getNumPoints();
51  if(!totpoints) {
52  Msg::Error("View[%d] contains no points", iView);
53  return nullptr;
54  }
55 
56 #if defined(HAVE_ANN)
57  ANNpointArray zeronodes = annAllocPts(totpoints, 3);
58  int k = 0, step = 0;
59  for(int ent = 0; ent < data1->getNumEntities(step); ent++) {
60  for(int ele = 0; ele < data1->getNumElements(step, ent); ele++) {
61  if(data1->skipElement(step, ent, ele)) continue;
62  int numNodes = data1->getNumNodes(step, ent, ele);
63  if(numNodes != 1) continue;
64  data1->getNode(step, ent, ele, 0, zeronodes[k][0], zeronodes[k][1],
65  zeronodes[k][2]);
66  k++;
67  }
68  }
69  ANNkd_tree *kdtree = new ANNkd_tree(zeronodes, totpoints, 3);
70  ANNidxArray index = new ANNidx[2];
71  ANNdistArray dist = new ANNdist[2];
72 
73  v1->setChanged(true);
74  for(int ent = 0; ent < data1->getNumEntities(step); ent++) {
75  for(int ele = 0; ele < data1->getNumElements(step, ent); ele++) {
76  if(data1->skipElement(step, ent, ele)) continue;
77  int numNodes = data1->getNumNodes(step, ent, ele);
78  if(numNodes != 1) continue;
79  double xyz[3];
80  data1->getNode(step, ent, ele, 0, xyz[0], xyz[1], xyz[2]);
81  kdtree->annkSearch(xyz, 2, index, dist);
82  data1->setValue(step, ent, ele, 0, 0, sqrt(dist[1]));
83  }
84  }
85 
86  delete kdtree;
87  annDeallocPts(zeronodes);
88  delete[] index;
89  delete[] dist;
90 #else
91  Msg::Error("Nearest neighbor computation requires ANN");
92 #endif
93 
94  data1->setName(v1->getData()->getName() + "_NearestNeighbor");
95  data1->finalize();
96 
97  return v1;
98 }
PView
Definition: PView.h:27
PViewData::skipElement
virtual bool skipElement(int step, int ent, int ele, bool checkVisibility=false, int samplingRate=1)
Definition: PViewData.cpp:90
GMSH_Plugin
Definition: Plugin.h:26
PViewData::setValue
virtual void setValue(int step, int ent, int ele, int nod, int comp, double val)
Definition: PViewData.cpp:130
PViewData::getNode
virtual int getNode(int step, int ent, int ele, int nod, double &x, double &y, double &z)
Definition: PViewData.h:141
Msg::Error
static void Error(const char *fmt,...)
Definition: GmshMessage.cpp:482
StringXNumber
Definition: Options.h:918
PViewData::getNumPoints
virtual int getNumPoints(int step=-1)
Definition: PViewData.h:114
PViewData::getNumEntities
virtual int getNumEntities(int step=-1)
Definition: PViewData.h:127
GMSH_NearestNeighborPlugin::getOption
StringXNumber * getOption(int iopt)
Definition: NearestNeighbor.cpp:37
PView::setChanged
void setChanged(bool val)
Definition: PView.cpp:241
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
PView::getData
PViewData * getData(bool useAdaptiveIfAvailable=false)
Definition: PView.cpp:233
GMSH_NearestNeighborPlugin::getNbOptions
int getNbOptions() const
Definition: NearestNeighbor.cpp:32
PViewData::getNumNodes
virtual int getNumNodes(int step, int ent, int ele)
Definition: PViewData.h:137
PViewData::setName
virtual void setName(const std::string &val)
Definition: PViewData.h:71
NearestNeighbor.h
PViewData
Definition: PViewData.h:29
GMSH_RegisterNearestNeighborPlugin
GMSH_Plugin * GMSH_RegisterNearestNeighborPlugin()
Definition: NearestNeighbor.cpp:18
GMSH_PostPlugin::getView
virtual PView * getView(int index, PView *view)
Definition: Plugin.cpp:81
GMSH_NearestNeighborPlugin::execute
PView * execute(PView *)
Definition: NearestNeighbor.cpp:42
PViewData::getNumElements
virtual int getNumElements(int step=-1, int ent=-1)
Definition: PViewData.h:131
PViewData::getName
virtual std::string getName()
Definition: PViewData.h:70
NearestNeighborOptions_Number
StringXNumber NearestNeighborOptions_Number[]
Definition: NearestNeighbor.cpp:13
GMSH_NearestNeighborPlugin
Definition: NearestNeighbor.h:15
GMSH_NearestNeighborPlugin::getHelp
std::string getHelp() const
Definition: NearestNeighbor.cpp:24
PViewData::finalize
virtual bool finalize(bool computeMinMax=true, const std::string &interpolationScheme="")
Definition: PViewData.cpp:30