KernelCollectionFlexiblePoint.h
Go to the documentation of this file.
1 // (C) Copyright Renaud Detry 2007-2015.
2 // Distributed under the GNU General Public License and under the
3 // BSD 3-Clause License (See accompanying file LICENSE.txt).
4 
5 /** @file */
6 
7 #ifndef NUKLEI_KERNEL_COLLECTION_FLEXIBLE_POINT_H
8 #define NUKLEI_KERNEL_COLLECTION_FLEXIBLE_POINT_H
9 
10 
11 #include <nuklei/Definitions.h>
12 
13 namespace nuklei
14 {
15 
16  struct FlexiblePoint {
17  // libkdtree++:
18  typedef coord_t value_type;
19 
20  FlexiblePoint() : idx_(-1) { vec_[0]= vec_[1] = vec_[2] = 0; }
21  FlexiblePoint (value_type x, value_type y, value_type z) : idx_(-1)
22  { vec_[0]=x; vec_[1]=y; vec_[2]=z; }
23  FlexiblePoint (value_type x, value_type y, value_type z, int idx) : idx_(idx)
24  { vec_[0]=x; vec_[1]=y; vec_[2]=z; }
25 
26  // cgal:
27  inline value_type x() const { return vec_[ 0 ]; }
28  inline value_type y() const { return vec_[ 1 ]; }
29  inline value_type z() const { return vec_[ 2 ]; }
30 
31  // cgal:
32  inline value_type& x() { return vec_[ 0 ]; }
33  inline value_type& y() { return vec_[ 1 ]; }
34  inline value_type& z() { return vec_[ 2 ]; }
35 
36  // cgal:
37  inline const value_type* vec() const { return vec_; }
38  inline value_type* vec() { return vec_; }
39 
40  // libkdtree++:
41  inline value_type operator[](const size_t i) const { return vec_[i]; }
42 
43  // cgal&libkdtree++:
44  inline bool operator==(const FlexiblePoint& p) const
45  {
46  return (x() == p.x()) && (y() == p.y()) && (z() == p.z()) && (idx() == p.idx());
47  }
48 
49  // cgal:
50  inline bool operator!=(const FlexiblePoint& p) const
51  { return ! (*this == p); }
52 
53 
54  inline int idx() const { return idx_; }
55  inline int& idx() { return idx_; }
56 
58  const value_type* operator()(const FlexiblePoint& p) const
59  { return static_cast<const value_type*>(p.vec()); }
60 
61  const value_type* operator()(const FlexiblePoint& p, int) const
62  { return static_cast<const value_type*>(p.vec()+3); }
63  };
64 
65  struct Accessor
66  {
67  typedef value_type result_type;
68 
69  inline result_type
70  operator()(const FlexiblePoint& p, const size_t i) const throw ()
71  { return p[i]; }
72  };
73 
74  struct Distance {
75  typedef FlexiblePoint Query_item;
76 
77  value_type transformed_distance(const FlexiblePoint& p1,
78  const FlexiblePoint& p2) const
79  {
80  value_type distx= p1.x()-p2.x();
81  value_type disty= p1.y()-p2.y();
82  value_type distz= p1.z()-p2.z();
83  return distx*distx+disty*disty+distz*distz;
84  }
85 
86 #ifdef NUKLEI_USE_CGAL
87  template <class TreeTraits>
88  value_type min_distance_to_rectangle(const FlexiblePoint& p,
89  const CGAL::Kd_tree_rectangle<TreeTraits>& b) const
90  {
91  value_type distance(0.0), h = p.x();
92  if (h < b.min_coord(0)) distance += (b.min_coord(0)-h)*(b.min_coord(0)-h);
93  if (h > b.max_coord(0)) distance += (h-b.max_coord(0))*(h-b.max_coord(0));
94  h=p.y();
95  if (h < b.min_coord(1)) distance += (b.min_coord(1)-h)*(b.min_coord(1)-h);
96  if (h > b.max_coord(1)) distance += (h-b.max_coord(1))*(h-b.min_coord(1));
97  h=p.z();
98  if (h < b.min_coord(2)) distance += (b.min_coord(2)-h)*(b.min_coord(2)-h);
99  if (h > b.max_coord(2)) distance += (h-b.max_coord(2))*(h-b.max_coord(2));
100  return distance;
101  }
102 
103  template <class TreeTraits>
104  value_type max_distance_to_rectangle(const FlexiblePoint& p,
105  const CGAL::Kd_tree_rectangle<TreeTraits>& b) const
106  {
107  value_type h = p.x();
108 
109  value_type d0 = (h >= (b.min_coord(0)+b.max_coord(0))/2.0) ?
110  (h-b.min_coord(0))*(h-b.min_coord(0)) : (b.max_coord(0)-h)*(b.max_coord(0)-h);
111 
112  h=p.y();
113  value_type d1 = (h >= (b.min_coord(1)+b.max_coord(1))/2.0) ?
114  (h-b.min_coord(1))*(h-b.min_coord(1)) : (b.max_coord(1)-h)*(b.max_coord(1)-h);
115  h=p.z();
116  value_type d2 = (h >= (b.min_coord(2)+b.max_coord(2))/2.0) ?
117  (h-b.min_coord(2))*(h-b.min_coord(2)) : (b.max_coord(2)-h)*(b.max_coord(2)-h);
118  return d0 + d1 + d2;
119  }
120 #endif
121 
122  value_type new_distance(value_type& dist, value_type old_off, value_type new_off,
123  int /* cutting_dimension */) const
124  {
125  return dist + new_off*new_off - old_off*old_off;
126  }
127 
128  value_type transformed_distance(value_type d) const { return d*d; }
129 
130  value_type inverse_of_transformed_distance(value_type d) { return std::sqrt(d); }
131 
132  }; // end of struct Distance
133 
134  private:
135  value_type vec_[3];
136  int idx_;
137  };
138 
139  inline std::ostream& operator<<(std::ostream& out, const FlexiblePoint& p)
140  {
141  out << p.x() << " " << p.y() << " " << p.z() << " " << p.idx();
142  return out;
143  }
144 
145 }
146 
147 
148 #endif
Public namespace.
Definition: Color.cpp:9
double coord_t
Type for point coordinates.
Definition: Definitions.h:25
Definition: GenericKernel.h:73
© Copyright 2007-2013 Renaud Detry.
Distributed under the terms of the GNU General Public License (GPL).
(See accompanying file LICENSE.txt or copy at http://www.gnu.org/copyleft/gpl.html.)
Revised Sun Sep 13 2020 19:10:06.