SlidingWindowMathExpressionFilter.cpp
Go to the documentation of this file.
00001 /*
00002  * SlidingWindowMathExpressionFilter.cpp
00003  *
00004  *  Created on: Aug 18, 2017
00005  *      Author: Peter Fankhauser
00006  *   Institute: ETH Zurich, Robotic Systems Lab
00007  */
00008 
00009 #include "grid_map_filters/SlidingWindowMathExpressionFilter.hpp"
00010 
00011 #include <pluginlib/class_list_macros.h>
00012 
00013 using namespace filters;
00014 
00015 namespace grid_map {
00016 
00017 template<typename T>
00018 SlidingWindowMathExpressionFilter<T>::SlidingWindowMathExpressionFilter()
00019 : windowSize_(3),
00020   useWindowLength_(false),
00021   windowLength_(0.0),
00022   isComputeEmptyCells_(true),
00023   edgeHandling_(SlidingWindowIterator::EdgeHandling::INSIDE)
00024 {
00025 }
00026 
00027 template<typename T>
00028 SlidingWindowMathExpressionFilter<T>::~SlidingWindowMathExpressionFilter()
00029 {
00030 }
00031 
00032 template<typename T>
00033 bool SlidingWindowMathExpressionFilter<T>::configure()
00034 {
00035   if (!FilterBase<T>::getParam(std::string("input_layer"), inputLayer_)) {
00036     ROS_ERROR("SlidingWindowMathExpressionFilter did not find parameter 'input_layer'.");
00037     return false;
00038   }
00039 
00040   if (!FilterBase<T>::getParam(std::string("output_layer"), outputLayer_)) {
00041     ROS_ERROR("SlidingWindowMathExpressionFilter did not find parameter 'output_layer'.");
00042     return false;
00043   }
00044 
00045   if (!FilterBase<T>::getParam(std::string("expression"), expression_)) {
00046     ROS_ERROR("SlidingWindowMathExpressionFilter did not find parameter 'expression'.");
00047     return false;
00048   }
00049 
00050   if (!FilterBase<T>::getParam(std::string("window_size"), windowSize_)) {
00051     if (FilterBase<T>::getParam(std::string("window_length"), windowLength_)) {
00052       useWindowLength_ = true;
00053     }
00054   }
00055 
00056   if (!FilterBase<T>::getParam(std::string("compute_empty_cells"), isComputeEmptyCells_)) {
00057     ROS_ERROR("SlidingWindowMathExpressionFilter did not find parameter 'compute_empty_cells'.");
00058     return false;
00059   }
00060 
00061   std::string edgeHandlingMethod;
00062   if (!FilterBase<T>::getParam(std::string("edge_handling"), edgeHandlingMethod)) {
00063     ROS_ERROR("SlidingWindowMathExpressionFilter did not find parameter 'edge_handling'.");
00064     return false;
00065   }
00066   if (edgeHandlingMethod == "inside") edgeHandling_ = SlidingWindowIterator::EdgeHandling::INSIDE;
00067   else if (edgeHandlingMethod == "crop") edgeHandling_ = SlidingWindowIterator::EdgeHandling::CROP;
00068   else if (edgeHandlingMethod == "empty") edgeHandling_ = SlidingWindowIterator::EdgeHandling::EMPTY;
00069   else if (edgeHandlingMethod == "mean") edgeHandling_ = SlidingWindowIterator::EdgeHandling::MEAN;
00070   else {
00071     ROS_ERROR("SlidingWindowMathExpressionFilter did not find method '%s' for edge handling.", edgeHandlingMethod.c_str());
00072     return false;
00073   }
00074 
00075   // TODO Can we make caching work with changing shared variable?
00076 //  parser_.setCacheExpressions(true);
00077   return true;
00078 }
00079 
00080 template<typename T>
00081 bool SlidingWindowMathExpressionFilter<T>::update(const T& mapIn, T& mapOut)
00082 {
00083   mapOut = mapIn;
00084   mapOut.add(outputLayer_);
00085   Matrix& outputData = mapOut[outputLayer_];
00086   grid_map::SlidingWindowIterator iterator(mapIn, inputLayer_, edgeHandling_, windowSize_);
00087   if (useWindowLength_) iterator.setWindowLength(mapIn, windowLength_);
00088   for (; !iterator.isPastEnd(); ++iterator) {
00089     parser_.var(inputLayer_).setLocal(iterator.getData());
00090     EigenLab::Value<Eigen::MatrixXf> result(parser_.eval(expression_));
00091     if (result.matrix().cols() == 1 && result.matrix().rows() == 1) {
00092       outputData(iterator.getLinearIndex()) = result.matrix()(0);
00093     } else {
00094       ROS_ERROR("SlidingWindowMathExpressionFilter could not apply filter because expression has to result in a scalar!");
00095     }
00096   }
00097   return true;
00098 }
00099 
00100 } /* namespace */
00101 
00102 PLUGINLIB_EXPORT_CLASS(grid_map::SlidingWindowMathExpressionFilter<grid_map::GridMap>, filters::FilterBase<grid_map::GridMap>)


grid_map_filters
Author(s): Péter Fankhauser , Martin Wermelinger
autogenerated on Mon Oct 9 2017 03:09:30