00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #ifndef PCL_FILTER_IMPL_FIELD_VAL_CONDITION_H_
00039 #define PCL_FILTER_IMPL_FIELD_VAL_CONDITION_H_
00040
00041 #include <pcl/io/io.h>
00042
00046 template <typename PointT>
00047 pcl::FieldComparison<PointT>::FieldComparison (
00048 std::string field_name, ComparisonOps::CompareOp op, double compare_val) :
00049 compare_val_(compare_val), point_data_(NULL)
00050 {
00051 field_name_ = field_name;
00052 op_ = op;
00053
00054
00055 std::vector<sensor_msgs::PointField> point_fields;
00056
00057 PointCloud<PointT> dummyCloud;
00058 pcl::getFields (dummyCloud, point_fields);
00059
00060
00061 if (point_fields.empty ())
00062 {
00063 ROS_WARN ("[pcl::FieldComparison::FieldComparison] no fields found!");
00064 capable_ = false;
00065 return;
00066 }
00067
00068
00069 size_t d;
00070 for (d = 0; d < point_fields.size (); ++d)
00071 {
00072 if (point_fields[d].name == field_name)
00073 break;
00074 }
00075
00076 if (d == point_fields.size ())
00077 {
00078 ROS_WARN ("[pcl::FieldComparison::FieldComparison] field not found!");
00079 capable_ = false;
00080 return;
00081 }
00082 uint8_t datatype = point_fields[d].datatype;
00083 uint32_t offset = point_fields[d].offset;
00084
00085 point_data_ = new PointDataAtOffset<PointT>(datatype, offset);
00086 capable_ = true;
00087 }
00088
00090 template <typename PointT>
00091 pcl::FieldComparison<PointT>::~FieldComparison()
00092 {
00093 if (point_data_ != NULL)
00094 {
00095 delete point_data_;
00096 point_data_ = NULL;
00097 }
00098 }
00099
00101 template <typename PointT> inline bool
00102 pcl::FieldComparison<PointT>::evaluate (const PointT &point) const
00103 {
00104 if (!this->capable_)
00105 {
00106 ROS_WARN ("[pcl::FieldComparison::evaluate] invalid compariosn!");
00107 return (false);
00108 }
00109
00110
00111
00112
00113 int compare_result = point_data_->compare (point, compare_val_);
00114
00115 switch (this->op_)
00116 {
00117 case pcl::ComparisonOps::GT :
00118 return (compare_result > 0);
00119 case pcl::ComparisonOps::GE :
00120 return (compare_result >= 0);
00121 case pcl::ComparisonOps::LT :
00122 return (compare_result < 0);
00123 case pcl::ComparisonOps::LE :
00124 return (compare_result <= 0);
00125 case pcl::ComparisonOps::EQ :
00126 return (compare_result == 0);
00127 default:
00128 ROS_WARN ("[pcl::FieldComparison::evaluate] unrecognized op_!");
00129 return (false);
00130 }
00131 }
00132
00136 template <typename PointT>
00137 pcl::PackedRGBComparison<PointT>::PackedRGBComparison (
00138 std::string component_name, ComparisonOps::CompareOp op, double compare_val)
00139 {
00140
00141 std::vector<sensor_msgs::PointField> point_fields;
00142
00143 PointCloud<PointT> dummyCloud;
00144 pcl::getFields (dummyCloud, point_fields);
00145
00146
00147 size_t d;
00148 for (d = 0; d < point_fields.size (); ++d)
00149 {
00150 if (point_fields[d].name == "rgb" || point_fields[d].name == "rgba")
00151 break;
00152 }
00153 if (d == point_fields.size ())
00154 {
00155 ROS_WARN ("[pcl::PackedRGBComparison::PackedRGBComparison] rgb field not found!");
00156 capable_ = false;
00157 return;
00158 }
00159
00160
00161 uint8_t datatype = point_fields[d].datatype;
00162 if (datatype != sensor_msgs::PointField::FLOAT32 &&
00163 datatype != sensor_msgs::PointField::UINT32 &&
00164 datatype != sensor_msgs::PointField::INT32)
00165 {
00166 ROS_WARN ("[pcl::PackedRGBComparison::PackedRGBComparison] has unusable type!");
00167 capable_ = false;
00168 return;
00169 }
00170
00171
00172 if (component_name == "r")
00173 {
00174 component_offset_ = point_fields[d].offset + 2;
00175 }
00176 else if (component_name == "g")
00177 {
00178 component_offset_ = point_fields[d].offset + 1;
00179 }
00180 else if (component_name == "b")
00181 {
00182 component_offset_ = point_fields[d].offset;
00183 }
00184 else
00185 {
00186 ROS_WARN ("[pcl::PackedRGBComparison::PackedRGBComparison] unrecognized component name!");
00187 capable_ = false;
00188 return;
00189 }
00190 component_name_ = component_name;
00191
00192
00193 capable_ = true;
00194 op_ = op;
00195 compare_val_ = compare_val;
00196 }
00197
00198
00200 template <typename PointT> inline bool
00201 pcl::PackedRGBComparison<PointT>::evaluate (const PointT &point) const
00202 {
00203
00204 uint8_t* pt_data = (uint8_t*)&point;
00205 uint8_t my_val = *(pt_data + component_offset_);
00206
00207
00208 switch (this->op_)
00209 {
00210 case pcl::ComparisonOps::GT :
00211 return (my_val > this->compare_val_);
00212 case pcl::ComparisonOps::GE :
00213 return (my_val >= this->compare_val_);
00214 case pcl::ComparisonOps::LT :
00215 return (my_val < this->compare_val_);
00216 case pcl::ComparisonOps::LE :
00217 return (my_val <= this->compare_val_);
00218 case pcl::ComparisonOps::EQ :
00219 return (my_val == this->compare_val_);
00220 default:
00221 ROS_WARN ("[pcl::PackedRGBComparison::evaluate] unrecognized op_!");
00222 return (false);
00223 }
00224 }
00225
00229 template <typename PointT>
00230 pcl::PackedHSIComparison<PointT>::PackedHSIComparison (
00231 std::string component_name, ComparisonOps::CompareOp op, double compare_val)
00232 {
00233
00234 std::vector<sensor_msgs::PointField> point_fields;
00235
00236 PointCloud<PointT> dummyCloud;
00237 pcl::getFields (dummyCloud, point_fields);
00238
00239
00240 size_t d;
00241 for (d = 0; d < point_fields.size (); ++d)
00242 if (point_fields[d].name == "rgb" || point_fields[d].name == "rgba")
00243 break;
00244 if (d == point_fields.size ())
00245 {
00246 ROS_WARN ("[pcl::PackedHSIComparison::PackedHSIComparison] rgb field not found!");
00247 capable_ = false;
00248 return;
00249 }
00250
00251
00252 uint8_t datatype = point_fields[d].datatype;
00253 if (datatype != sensor_msgs::PointField::FLOAT32 &&
00254 datatype != sensor_msgs::PointField::UINT32 &&
00255 datatype != sensor_msgs::PointField::INT32)
00256 {
00257 ROS_WARN ("[pcl::PackedHSIComparison::PackedHSIComparison] has unusable type!");
00258 capable_ = false;
00259 return;
00260 }
00261
00262
00263 uint32_t offset = point_fields[d].offset;
00264 if (offset % 4 != 0)
00265 {
00266 ROS_WARN ("[pcl::PackedHSIComparison::PackedHSIComparison] rgb field is not 32 bit aligned!");
00267 capable_ = false;
00268 return;
00269 }
00270 rgb_offset_ = point_fields[d].offset;
00271
00272
00273 if (component_name == "h" )
00274 {
00275 component_id_ = H;
00276 }
00277 else if (component_name == "s")
00278 {
00279 component_id_ = S;
00280 }
00281 else if (component_name == "i")
00282 {
00283 component_id_ = I;
00284 }
00285 else
00286 {
00287 ROS_WARN ("[pcl::PackedRGBComparison::PackedRGBComparison] unrecognized component name!");
00288 capable_ = false;
00289 return;
00290 }
00291 component_name_ = component_name;
00292
00293
00294 capable_ = true;
00295 op_ = op;
00296 compare_val_ = compare_val;
00297 }
00298
00300 template <typename PointT> inline bool
00301 pcl::PackedHSIComparison<PointT>::evaluate (const PointT &point) const
00302 {
00303
00304 static uint32_t rgb_val_ = 0;
00305 static uint8_t r_ = 0;
00306 static uint8_t g_ = 0;
00307 static uint8_t b_ = 0;
00308 static int8_t h_ = 0;
00309 static uint8_t s_ = 0;
00310 static uint8_t i_ = 0;
00311
00312
00313 uint8_t* pt_data = (uint8_t*)&point;
00314 uint32_t* rgb_data = (uint32_t*)(pt_data + rgb_offset_);
00315 uint32_t new_rgb_val = *rgb_data;
00316
00317 if (rgb_val_ != new_rgb_val)
00318 {
00319 rgb_val_ = new_rgb_val;
00320
00321 r_ = (uint8_t)(rgb_val_ >> 16);
00322 g_ = (uint8_t)(rgb_val_ >> 8);
00323 b_ = (uint8_t)(rgb_val_);
00324
00325
00326 float hx = (2*r_ - g_ - b_)/4.0;
00327 float hy = (g_ - b_) * 111.0 / 255.0;
00328 h_ = (int8_t) (atan2(hy, hx) * 128.0 / M_PI);
00329
00330 int32_t i = (r_+g_+b_)/3;
00331 i_ = i;
00332
00333 int32_t m;
00334 m = (r_ < g_) ? r_ : g_;
00335 m = (m < b_) ? m : b_;
00336
00337 s_ = (i == 0) ? 0 : 255 - (m*255)/i;
00338 }
00339
00340 float my_val;
00341
00342 switch (component_id_)
00343 {
00344 case H:
00345 my_val = (float)h_;
00346 break;
00347 case S:
00348 my_val = (float)s_;
00349 break;
00350 case I:
00351 my_val = (float)i_;
00352 break;
00353 default:
00354 assert (false);
00355 }
00356
00357
00358 switch (this->op_)
00359 {
00360 case pcl::ComparisonOps::GT :
00361 return (my_val > this->compare_val_);
00362 case pcl::ComparisonOps::GE :
00363 return (my_val >= this->compare_val_);
00364 case pcl::ComparisonOps::LT :
00365 return (my_val < this->compare_val_);
00366 case pcl::ComparisonOps::LE :
00367 return (my_val <= this->compare_val_);
00368 case pcl::ComparisonOps::EQ :
00369 return (my_val == this->compare_val_);
00370 default:
00371 ROS_WARN ("[pcl::PackedHSIComparison::evaluate] unrecognized op_!");
00372 return (false);
00373 }
00374 }
00375
00376
00380 template <typename PointT> inline int
00381 pcl::PointDataAtOffset<PointT>::compare (const PointT& p, const double& val)
00382 {
00383
00384
00385
00386
00387 uint8_t* pt_data = (uint8_t*)&p;
00388
00389 switch (datatype_)
00390 {
00391 case sensor_msgs::PointField::INT8 :
00392 {
00393 int8_t pt_val;
00394 memcpy (&pt_val, pt_data + this->offset_, sizeof(int8_t));
00395 return ( pt_val > (int8_t)val ) - ( pt_val < (int8_t)val );
00396 }
00397 case sensor_msgs::PointField::UINT8 :
00398 {
00399 uint8_t pt_val;
00400 memcpy (&pt_val, pt_data + this->offset_, sizeof(uint8_t));
00401 return ( pt_val > (uint8_t)val ) - ( pt_val < (uint8_t)val );
00402 }
00403 case sensor_msgs::PointField::INT16 :
00404 {
00405 int16_t pt_val;
00406 memcpy (&pt_val, pt_data + this->offset_, sizeof(int16_t));
00407 return ( pt_val > (int16_t)val ) - ( pt_val < (int16_t)val );
00408 }
00409 case sensor_msgs::PointField::UINT16 :
00410 {
00411 uint16_t pt_val;
00412 memcpy (&pt_val, pt_data + this->offset_, sizeof(uint16_t));
00413 return ( pt_val > (uint16_t)val ) - ( pt_val < (uint16_t)val );
00414 }
00415 case sensor_msgs::PointField::INT32 :
00416 {
00417 int32_t pt_val;
00418 memcpy (&pt_val, pt_data + this->offset_, sizeof(int32_t));
00419 return ( pt_val > (int32_t)val ) - ( pt_val < (int32_t)val );
00420 }
00421 case sensor_msgs::PointField::UINT32 :
00422 {
00423 uint32_t pt_val;
00424 memcpy (&pt_val, pt_data + this->offset_, sizeof(uint32_t));
00425 return ( pt_val > (uint32_t)val ) - ( pt_val < (uint32_t)val );
00426 }
00427 case sensor_msgs::PointField::FLOAT32 :
00428 {
00429 float pt_val;
00430 memcpy (&pt_val, pt_data + this->offset_, sizeof(float));
00431 return ( pt_val > (float)val ) - ( pt_val < (float)val );
00432 }
00433 case sensor_msgs::PointField::FLOAT64 :
00434 {
00435 double pt_val;
00436 memcpy (&pt_val, pt_data + this->offset_, sizeof(double));
00437 return ( pt_val > val ) - ( pt_val < val );
00438 }
00439 default :
00440 ROS_WARN ("[pcl::pcl::PointDataAtOffset::compare] unknown data_type!");
00441 return (0);
00442 }
00443 }
00444
00448 template <typename PointT> void
00449 pcl::ConditionBase<PointT>::addComparison (ComparisonBaseConstPtr comparison)
00450 {
00451 if (!comparison->isCapable ())
00452 capable_ = false;
00453 comparisons_.push_back (comparison);
00454 }
00455
00457 template <typename PointT> void
00458 pcl::ConditionBase<PointT>::addCondition (ConstPtr condition)
00459 {
00460 if (!condition->isCapable ())
00461 capable_ = false;
00462 conditions_.push_back (condition);
00463 }
00464
00468 template <typename PointT> inline bool
00469 pcl::ConditionAnd<PointT>::evaluate (const PointT &point) const
00470 {
00471 for (size_t i = 0; i < comparisons_.size (); ++i)
00472 if (!comparisons_[i]->evaluate (point))
00473 return (false);
00474
00475 for (size_t i = 0; i < conditions_.size (); ++i)
00476 if (!conditions_[i]->evaluate (point))
00477 return (false);
00478
00479 return (true);
00480 }
00481
00485 template <typename PointT> inline bool
00486 pcl::ConditionOr<PointT>::evaluate (const PointT &point) const
00487 {
00488 if (comparisons_.empty () && conditions_.empty ())
00489 return (true);
00490 for (size_t i = 0; i < comparisons_.size (); ++i)
00491 if (comparisons_[i]->evaluate(point))
00492 return (true);
00493
00494 for (size_t i = 0; i < conditions_.size (); ++i)
00495 if (conditions_[i]->evaluate (point))
00496 return (true);
00497
00498 return (false);
00499 }
00500
00504 template <typename PointT> void
00505 pcl::ConditionalRemoval<PointT>::setCondition (ConditionBasePtr condition)
00506 {
00507 condition_ = condition;
00508 capable_ = condition_->isCapable ();
00509 }
00510
00512 template <typename PointT> void
00513 pcl::ConditionalRemoval<PointT>::applyFilter (PointCloud &output)
00514 {
00515 if (capable_ == false)
00516 {
00517 ROS_WARN ("[pcl::%s::applyFilter] not capable!", getClassName ().c_str ());
00518 return;
00519 }
00520
00521 if (!input_)
00522 {
00523 ROS_WARN ("[pcl::%s::applyFilter] No input dataset given!", getClassName ().c_str ());
00524 return;
00525 }
00526
00527 if (condition_.get () == NULL)
00528 {
00529 ROS_WARN ("[pcl::%s::applyFilter] No filtering condition given!", getClassName ().c_str ());
00530 return;
00531 }
00532
00533
00534 output.header = input_->header;
00535 if (keep_organized_)
00536 {
00537 output.height = 1;
00538 output.is_dense = true;
00539 }
00540 else
00541 {
00542 output.height = this->input_->height;
00543 output.width = this->input_->width;
00544 output.is_dense = this->input_->is_dense;
00545 }
00546 output.points.resize (input_->points.size ());
00547
00548 int nr_p = 0;
00549
00550 if (keep_organized_ == true)
00551 {
00552 for (size_t cp = 0; cp < input_->points.size (); ++cp)
00553 {
00554
00555 if (!pcl_isfinite (input_->points[cp].x) ||
00556 !pcl_isfinite (input_->points[cp].y) ||
00557 !pcl_isfinite (input_->points[cp].z))
00558 continue;
00559
00560 if (condition_->evaluate (input_->points[cp]))
00561 {
00562 pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointT, PointT> (input_->points[cp], output.points[nr_p]));
00563 nr_p++;
00564 }
00565 }
00566
00567 output.width = nr_p;
00568 output.points.resize (nr_p);
00569 }
00570 else
00571 {
00572 float bad_point = std::numeric_limits<float>::quiet_NaN ();
00573
00574 for (size_t cp = 0; cp < input_->points.size (); ++cp)
00575 {
00576
00577 pcl::for_each_type <FieldList> (pcl::NdConcatenateFunctor <PointT, PointT> (input_->points[cp], output.points[cp]));
00578 if (!condition_->evaluate (input_->points[cp]))
00579 output.points[cp].getVector4fMap ().setConstant (bad_point);
00580 }
00581 }
00582 }
00583
00584 #define PCL_INSTANTIATE_PointDataAtOffset(T) template class pcl::PointDataAtOffset<T>;
00585 #define PCL_INSTANTIATE_FieldComparison(T) template class pcl::FieldComparison<T>;
00586 #define PCL_INSTANTIATE_PackedRGBComparison(T) template class pcl::PackedRGBComparison<T>;
00587 #define PCL_INSTANTIATE_PackedHSIComparison(T) template class pcl::PackedHSIComparison<T>;
00588 #define PCL_INSTANTIATE_ConditionAnd(T) template class pcl::ConditionAnd<T>;
00589 #define PCL_INSTANTIATE_ConditionOr(T) template class pcl::ConditionOr<T>;
00590 #define PCL_INSTANTIATE_ConditionalRemoval(T) template class pcl::ConditionalRemoval<T>;
00591
00592 #endif