Go to the documentation of this file.00001 #ifndef OPENTISSUE_COLLISION_SPATIAL_HASHING_HASH_QUERIES_SPATIAL_HASHING_LINE_DATA_QUERY_H
00002 #define OPENTISSUE_COLLISION_SPATIAL_HASHING_HASH_QUERIES_SPATIAL_HASHING_LINE_DATA_QUERY_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/collision/spatial_hashing/spatial_hashing_query.h>
00013 #include <cassert>
00014
00015 namespace OpenTissue
00016 {
00017 namespace spatial_hashing
00018 {
00019
00020 template<typename hash_grid, typename collision_policy>
00021 class LineDataQuery
00022 : public Query< LineDataQuery<hash_grid,collision_policy>, hash_grid, collision_policy >
00023 {
00024 public:
00025 typedef typename hash_grid::triplet_type triplet_type;
00026 typedef typename hash_grid::cell_type cell_type;
00027 typedef typename hash_grid::point_type point_type;
00028 typedef typename point_type::value_type real_type;
00029
00030 template< typename data_iterator >
00031 void first_pass(data_iterator begin, data_iterator end )
00032 {
00033 real_type parameters[6];
00034 bool indices[6];
00035
00036
00037
00038 size_t stamp = 0;
00039 typename hash_grid::cell_iterator Cbegin = this->begin();
00040 typename hash_grid::cell_iterator Cend = this->end();
00041 for( typename hash_grid::cell_iterator cell = Cbegin;cell!=Cend;++cell)
00042 cell->m_query_stamp = stamp;
00043
00044 real_type dx = this->get_spacing();
00045
00046 for(data_iterator data = begin;data!=end;++data)
00047 {
00048 ++stamp;
00049
00050 point_type o = origin(*data);
00051 point_type d = destination(*data);
00052 point_type u = d - o;
00053 real_type s = 0;
00054 triplet_type T = get_triplet(o);
00055
00056 while (s<=1)
00057 {
00058 cell_type & cell = get_cell(T);
00059 if(cell.m_query_stamp!=stamp)
00060 {
00061 cell.m_query_stamp=stamp;
00062 cell.add( *data );
00063 }
00064 update_parameters(T,dx,o,u,parameters);
00065 s = find_indices(parameters,s,indices);
00066 update_triplet(T,indices);
00067 }
00068 }
00069 }
00070
00071
00072 template< typename data_iterator >
00073 void remove_data(data_iterator begin, data_iterator end )
00074 {
00075 real_type parameters[6];
00076 bool indices[6];
00077
00078
00079
00080 real_type dx = this->get_spacing();
00081 for(data_iterator data = begin;data!=end;++data)
00082 {
00083 point_type o = origin(*data);
00084 point_type d = destination(*data);
00085 point_type u = d - o;
00086 real_type s = 0;
00087 triplet_type T = get_triplet(o);
00088
00089 while (s<=1)
00090 {
00091 cell_type & cell = get_cell(T);
00092 cell.remove( *data );
00093 update_parameters(T,dx,o,u,parameters);
00094 s = find_indices(parameters,s,indices);
00095 update_triplet(T,indices);
00096 }
00097 }
00098 }
00099
00100 protected:
00101
00102 void update_triplet(triplet_type & t,bool * indices)
00103 {
00104 if(indices[0]) t(0) -= 1;
00105 if(indices[1]) t(0) += 1;
00106 if(indices[2]) t(1) -= 1;
00107 if(indices[3]) t(1) += 1;
00108 if(indices[4]) t(2) -= 1;
00109 if(indices[5]) t(2) += 1;
00110 }
00111
00112 void update_parameters(triplet_type & t,real_type dx,point_type const & o,point_type const & u,real_type * param)
00113 {
00114 if(u(0)!=0)
00115 {
00116 param[0] = ( t(0)*dx - o(0)) / u(0);
00117 param[1] = ( (t(0)+1)*dx - o(0)) / u(0);
00118 }
00119 else
00120 {
00121 param[0] = 10e30;
00122 param[1] = 10e30;
00123 }
00124 if(u(1)!=0)
00125 {
00126 param[2] = ( t(1)*dx - o(1)) / u(1);
00127 param[3] = ( (t(1)+1)*dx - o(1)) / u(1);
00128 }
00129 else
00130 {
00131 param[2] = 10e30;
00132 param[3] = 10e30;
00133 }
00134 if(u(2)!=0)
00135 {
00136 param[4] = ( t(2)*dx - o(2)) / u(2);
00137 param[5] = ( (t(2)+1)*dx - o(2)) / u(2);
00138 }
00139 else
00140 {
00141 param[4] = 10e30;
00142 param[5] = 10e30;
00143 }
00144 }
00145
00146 real_type find_indices(real_type * param,real_type s,bool * indices)
00147 {
00148 real_type min_val = 10e30;
00149 for(int i = 0;i<6;++i)
00150 {
00151 if(param[i]>s && param[i]<min_val)
00152 min_val = param[i];
00153 }
00154 for(int i = 0;i<6;++i)
00155 {
00156 if(param[i]==min_val)
00157 indices[i] = true;
00158 else
00159 indices[i] = false;
00160 }
00161 return min_val;
00162 }
00163
00164 };
00165 }
00166
00167 }
00168
00169
00170 #endif