Memosa-FVM  0.2
KSearchTree.h
Go to the documentation of this file.
1 // This file os part of FVM
2 // Copyright (c) 2012 FVM Authors
3 // See LICENSE file for terms.
4 
5 #ifndef _KSEARCHTREE_H_
6 #define _KSEARCHTREE_H_
7 
8 #include "GeomFields.h"
9 
10 #include "Mesh.h"
11 
12 #include <CGAL/basic.h>
13 #include <CGAL/Search_traits.h>
14 #include <CGAL/Orthogonal_k_neighbor_search.h>
15 
16 
17 
18 struct MyPoint
19 {
21 
23  int index;
24 
25  MyPoint(const MyPoint& o) :
26  vec(o.vec),
27  index(o.index)
28  {}
29 
30  MyPoint(const Vec3D& vec_, const int index_) :
31  vec(vec_),
32  index(index_)
33  {}
34 };
35 
36 namespace CGAL
37 {
38 
39 template <>
40 struct Kernel_traits<MyPoint> {
41  struct Kernel {
42  typedef double FT;
43  typedef double RT;
44  };
45 };
46 }
47 
49 {
50  const double* operator()(const MyPoint& p) const
51  { return &(p.vec[0]); }
52 
53  const double* operator()(const MyPoint& p, int) const
54  { return &(p.vec[0])+3; }
55 };
56 
57 
58 struct Distance
59 {
61 
62  double transformed_distance(const MyPoint& p1, const MyPoint& p2) const
63  {
64  double distx= p1.vec[0]-p2.vec[0];
65  double disty= p1.vec[1]-p2.vec[1];
66  double distz= p1.vec[2]-p2.vec[2];
67  return distx*distx+disty*disty+distz*distz;
68  }
69 
70  template <class TreeTraits>
72  const CGAL::Kd_tree_rectangle<TreeTraits>& b) const
73  {
74  double distance(0.0), h = p.vec[0];
75  if (h < b.min_coord(0)) distance += (b.min_coord(0)-h)*(b.min_coord(0)-h);
76  if (h > b.max_coord(0)) distance += (h-b.max_coord(0))*(h-b.max_coord(0));
77  h=p.vec[1];
78  if (h < b.min_coord(1)) distance += (b.min_coord(1)-h)*(b.min_coord(1)-h);
79  if (h > b.max_coord(1)) distance += (h-b.max_coord(1))*(h-b.min_coord(1));
80  h=p.vec[2];
81  if (h < b.min_coord(2)) distance += (b.min_coord(2)-h)*(b.min_coord(2)-h);
82  if (h > b.max_coord(2)) distance += (h-b.max_coord(2))*(h-b.max_coord(2));
83  return distance;
84  }
85 
86  template <class TreeTraits>
88  const CGAL::Kd_tree_rectangle<TreeTraits>& b) const
89  {
90  double h = p.vec[0];
91  double d0 = (h >= (b.min_coord(0)+b.max_coord(0))/2.0) ?
92  (h-b.min_coord(0))*(h-b.min_coord(0)) : (b.max_coord(0)-h)*(b.max_coord(0)-h);
93 
94  h=p.vec[1];
95  double d1 = (h >= (b.min_coord(1)+b.max_coord(1))/2.0) ?
96  (h-b.min_coord(1))*(h-b.min_coord(1)) : (b.max_coord(1)-h)*(b.max_coord(1)-h);
97 
98  h=p.vec[2];
99  double d2 = (h >= (b.min_coord(2)+b.max_coord(2))/2.0) ?
100  (h-b.min_coord(2))*(h-b.min_coord(2)) : (b.max_coord(2)-h)*(b.max_coord(2)-h);
101  return d0 + d1 + d2;
102  }
103 
104  double new_distance(double& dist, double old_off, double new_off,
105  int /* cutting_dimension */) const {
106  return dist + new_off*new_off - old_off*old_off;
107  }
108 
109  double transformed_distance(double d) const { return d*d; }
110 
111  double inverse_of_transformed_distance(double d) { return std::sqrt(d); }
112 
113 }; // end of struct Distance
114 
115 
122 {
123 public:
127 
128  KSearchTree();
129  KSearchTree(const Vec3DArray& points);
130  void insert(const Vec3D& v, const int n);
131 
132  void findNeighbors(const Vec3D& p, const int k, Array<int>& neighbors);
133 
134 private:
135 
136 
137  typedef CGAL::Search_traits<double, MyPoint, const double*,
139  typedef CGAL::Orthogonal_k_neighbor_search<Traits> K_neighbor_search;
140  typedef K_neighbor_search::Tree Tree;
141 
142  boost::shared_ptr<Tree> _tree;
143 };
144 
145 #endif
const double * operator()(const MyPoint &p, int) const
Definition: KSearchTree.h:53
int index
Definition: KSearchTree.h:23
Vector< double, 3 > Vec3D
Definition: KSearchTree.h:20
const double * operator()(const MyPoint &p) const
Definition: KSearchTree.h:50
Tangent sqrt(const Tangent &a)
Definition: Tangent.h:317
CGAL::Orthogonal_k_neighbor_search< Traits > K_neighbor_search
Definition: KSearchTree.h:139
MyPoint Query_item
Definition: KSearchTree.h:60
double max_distance_to_rectangle(const MyPoint &p, const CGAL::Kd_tree_rectangle< TreeTraits > &b) const
Definition: KSearchTree.h:87
void findNeighbors(const Vec3D &p, const int k, Array< int > &neighbors)
Definition: KSearchTree.cpp:30
double transformed_distance(const MyPoint &p1, const MyPoint &p2) const
Definition: KSearchTree.h:62
Array< int > IntArray
Definition: KSearchTree.h:126
Vector< double, 3 > Vec3D
Definition: KSearchTree.h:124
Definition: Array.h:14
MyPoint(const MyPoint &o)
Definition: KSearchTree.h:25
Array< Vec3D > Vec3DArray
Definition: KSearchTree.h:125
double new_distance(double &dist, double old_off, double new_off, int) const
Definition: KSearchTree.h:104
double inverse_of_transformed_distance(double d)
Definition: KSearchTree.h:111
double min_distance_to_rectangle(const MyPoint &p, const CGAL::Kd_tree_rectangle< TreeTraits > &b) const
Definition: KSearchTree.h:71
boost::shared_ptr< Tree > _tree
Definition: KSearchTree.h:142
K_neighbor_search::Tree Tree
Definition: KSearchTree.h:140
double transformed_distance(double d) const
Definition: KSearchTree.h:109
Vec3D vec
Definition: KSearchTree.h:22
MyPoint(const Vec3D &vec_, const int index_)
Definition: KSearchTree.h:30
CGAL::Search_traits< double, MyPoint, const double *, Construct_coord_iterator > Traits
Definition: KSearchTree.h:138
void insert(const Vec3D &v, const int n)
Definition: KSearchTree.cpp:23