1 // Copyright 2006-2009 Brad King, Chuck Stewart
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file rgtl_license_1_0.txt or copy at
4 // http://www.boost.org/LICENSE_1_0.txt)
5 #ifndef rgtl_object_array_points_hxx
6 #define rgtl_object_array_points_hxx
7 
8 #include <iostream>
9 #include <limits>
10 #include "rgtl_object_array_points.h"
11 
12 #ifdef _MSC_VER
13 #  include <vcl_msvc_warnings.h>
14 #endif
15 
16 //----------------------------------------------------------------------------
17 template <unsigned int D>
rgtl_object_array_points(int n)18 rgtl_object_array_points<D>::rgtl_object_array_points(int n): points_(n)
19 {
20 }
21 
22 //----------------------------------------------------------------------------
23 template <unsigned int D>
24 rgtl_object_array_points<D>
rgtl_object_array_points(std::vector<point_type> const & points)25 ::rgtl_object_array_points(std::vector<point_type> const& points):
26   points_(points)
27 {
28 }
29 
30 //----------------------------------------------------------------------------
31 template <unsigned int D>
set_number_of_points(int n)32 void rgtl_object_array_points<D>::set_number_of_points(int n)
33 {
34   this->points_.resize(n);
35 }
36 
37 //----------------------------------------------------------------------------
38 template <unsigned int D>
get_number_of_points() const39 int rgtl_object_array_points<D>::get_number_of_points() const
40 {
41   return static_cast<int>(this->points_.size());
42 }
43 
44 //----------------------------------------------------------------------------
45 template <unsigned int D>
get_point(int id,double x[D]) const46 void rgtl_object_array_points<D>::get_point(int id, double x[D]) const
47 {
48   this->points_[id].copy_out(x);
49 }
50 
51 //----------------------------------------------------------------------------
52 template <unsigned int D>
set_point(int id,double const x[D])53 void rgtl_object_array_points<D>::set_point(int id, double const x[D])
54 {
55   this->points_[id].copy_in(x);
56 }
57 
58 //----------------------------------------------------------------------------
59 template <unsigned int D>
add_point(double const x[D])60 int rgtl_object_array_points<D>::add_point(double const x[D])
61 {
62   point_type p(x);
63   int id = this->number_of_objects();
64   this->points_.push_back(p);
65   return id;
66 }
67 
68 //----------------------------------------------------------------------------
69 template <unsigned int D>
number_of_objects() const70 int rgtl_object_array_points<D>::number_of_objects() const
71 {
72   return this->get_number_of_points();
73 }
74 
75 //----------------------------------------------------------------------------
76 template <unsigned int D>
77 bool
78 rgtl_object_array_points<D>
object_intersects_object(int idA,int idB) const79 ::object_intersects_object(int idA, int idB) const
80 {
81   return this->points_[idA] == this->points_[idB];
82 }
83 
84 //----------------------------------------------------------------------------
85 template <unsigned int D>
86 bool
87 rgtl_object_array_points<D>
object_intersects_box(int id,double const[D],double const,double const lower[D],double const upper[D],double const[1<<D][D]) const88 ::object_intersects_box(int id,
89                         double const [D],
90                         double const,
91                         double const lower[D],
92                         double const upper[D],
93                         double const [1<<D][D]) const
94 {
95   point_type const& x = this->points_[id];
96   for (unsigned int a=0; a < D; ++a)
97   {
98     // Use an asymmetric test so that points on the boundary between
99     // adjacent boxes are given to only one box.
100     if (x[a] < lower[a] || x[a] >= upper[a])
101     {
102       return false;
103     }
104   }
105   return true;
106 }
107 
108 //----------------------------------------------------------------------------
109 template <unsigned int D>
110 bool
111 rgtl_object_array_points<D>
object_closest_point(int id,double const[D],double y[D],double) const112 ::object_closest_point(int id,
113                        double const[D],
114                        double y[D],
115                        double) const
116 {
117   this->points_[id].copy_out(y);
118   return true;
119 }
120 
121 //----------------------------------------------------------------------------
122 template <unsigned int D>
123 bool
124 rgtl_object_array_points<D>
object_intersects_ray(int,double const[D],double const[D],double[D],double *) const125 ::object_intersects_ray(int,
126                         double const[D],
127                         double const[D],
128                         double [D],
129                         double*) const
130 {
131   return false;
132 }
133 
134 //----------------------------------------------------------------------------
135 template <unsigned int D>
compute_bounds(double bounds[D][2]) const136 void rgtl_object_array_points<D>::compute_bounds(double bounds[D][2]) const
137 {
138   // Initialize bounds to empty.
139   for (unsigned int a=0; a < D; ++a)
140   {
141     bounds[a][0] = +std::numeric_limits<double>::max();
142     bounds[a][1] = -std::numeric_limits<double>::max();
143   }
144 
145   // Update the bounds for each point.
146   int n = this->get_number_of_points();
147   for (int i=0; i < n; ++i)
148   {
149     point_type const& p = this->points_[i];
150     for (unsigned int a=0; a < D; ++a)
151     {
152       if (p[a] < bounds[a][0])
153       {
154         bounds[a][0] = p[a];
155       }
156       if (p[a] > bounds[a][1])
157       {
158         bounds[a][1] = p[a];
159       }
160     }
161   }
162 }
163 
164 #undef RGTL_OBJECT_ARRAY_POINTS_INSTANTIATE
165 #define RGTL_OBJECT_ARRAY_POINTS_INSTANTIATE( D ) \
166   template class rgtl_object_array_points<D >
167 
168 #endif
169