footstep_state_discrete_close_list.h
Go to the documentation of this file.
00001 // -*- mode: c++ -*-
00002 /*********************************************************************
00003  * Software License Agreement (BSD License)
00004  *
00005  *  Copyright (c) 2015, JSK Lab
00006  *  All rights reserved.
00007  *
00008  *  Redistribution and use in source and binary forms, with or without
00009  *  modification, are permitted provided that the following conditions
00010  *  are met:
00011  *
00012  *   * Redistributions of source code must retain the above copyright
00013  *     notice, this list of conditions and the following disclaimer.
00014  *   * Redistributions in binary form must reproduce the above
00015  *     copyright notice, this list of conditions and the following
00016  *     disclaimer in the documentation and/o2r other materials provided
00017  *     with the distribution.
00018  *   * Neither the name of the JSK Lab nor the names of its
00019  *     contributors may be used to endorse or promote products derived
00020  *     from this software without specific prior written permission.
00021  *
00022  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00023  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00024  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00025  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00026  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00027  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00028  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00029  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00030  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00032  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00033  *  POSSIBILITY OF SUCH DAMAGE.
00034  *********************************************************************/
00035 
00036 
00037 #ifndef JSK_FOOTSTEP_PLANNER_FOOTSTEP_STATE_DISCRETE_CLOSE_LIST_H_
00038 #define JSK_FOOTSTEP_PLANNER_FOOTSTEP_STATE_DISCRETE_CLOSE_LIST_H_
00039 
00040 #include "jsk_footstep_planner/footstep_state.h"
00041 #include <boost/tuple/tuple.hpp>
00042 #include <boost/tuple/tuple_comparison.hpp>
00043 
00044 namespace jsk_footstep_planner
00045 {
00046   class FootstepStateDiscreteCloseListLocal
00047   {
00048     // x_offset, x_offset + 1, ...., x_offset + X - 1
00049   public:
00050     typedef boost::shared_ptr<FootstepStateDiscreteCloseListLocal> Ptr;
00051     FootstepStateDiscreteCloseListLocal(
00052       int x_offset, int y_offset, int theta_offset,
00053       size_t x_num, size_t y_num, size_t theta_num);
00054     
00055     inline FootstepState::Ptr get(int x, int y, int theta)
00056     {
00057       return data_[x - x_offset_][y - y_offset_][theta - theta_offset_];
00058     }
00059 
00060     inline void add(FootstepState::Ptr state)
00061     {
00062       int x = state->indexX();
00063       int y = state->indexY();
00064       int theta = state->indexT();
00065       if (!data_[x - x_offset_][y - y_offset_][theta - theta_offset_]) {
00066         size_++;
00067       }
00068       data_[x - x_offset_][y - y_offset_][theta - theta_offset_] = state;
00069     }
00070 
00071     // Use push_back to inser points
00072     template <class PointT>
00073     void insertToPointCloud(pcl::PointCloud<PointT>& output)
00074     {
00075       for (size_t xi = 0; xi < x_num_; xi++) {
00076         for (size_t yi = 0; yi < y_num_; yi++) {
00077           for (size_t thetai = 0; thetai < theta_num_; thetai++) {
00078             if (data_[xi][yi][thetai]) {
00079               FootstepState::Ptr state = data_[xi][yi][thetai];
00080               PointT p = state->toPoint<PointT>();
00081               output.points.push_back(p);
00082             }
00083           }
00084         }
00085       }
00086     }
00087 
00088     inline size_t size() { return size_; }
00089   protected:
00090     size_t size_;
00091     const size_t x_num_;
00092     const size_t y_num_;
00093     const size_t theta_num_;
00094     const int x_offset_;
00095     const int y_offset_;
00096     const int theta_offset_;
00097     std::vector<std::vector<std::vector<FootstepState::Ptr> > > data_;
00098   private:
00099     
00100   };
00101 
00108   class FootstepStateDiscreteCloseList
00109   {
00110   public:
00111     typedef boost::shared_ptr<FootstepStateDiscreteCloseList> Ptr;
00112     typedef boost::tuple<int, int, int> VolumeKey;
00113     FootstepStateDiscreteCloseList(const size_t local_x_num,
00114                                    const size_t local_y_num,
00115                                    const size_t local_theta_num);
00116     inline int keyDivide(int x, int y)
00117     {
00118       if (x < 0) {
00119         // return -1, while -1 <= x <= -local_num
00120         // return -2, while -local_num -1 <= x <= - 2*local_num
00121         // ...
00122         return (x + 1)/ y - 1;
00123       }
00124       else {
00125         // return 0, while 0 <= x <= local_num -1
00126         // return 1, while local_num <= x <= 2*local_num -1
00127         // ...
00128         return x / y;
00129       }
00130     }
00131     inline VolumeKey volumeKey(
00132       int xi, int yi, int ti)
00133     {
00134       int kx = keyDivide(xi, local_x_num_);
00135       int ky = keyDivide(yi, local_y_num_);
00136       int kt = keyDivide(ti, local_theta_num_);
00137       return boost::make_tuple(kx, ky, kt);
00138     }
00139 
00140     inline size_t size()
00141     {
00142       std::map<VolumeKey, FootstepStateDiscreteCloseListLocal::Ptr>::iterator it;
00143       size_t s = 0;
00144       for (it = local_volumes_.begin(); it != local_volumes_.end(); ++it) {
00145         s += it->second->size();
00146       }
00147       return s;
00148     }
00149     
00150     inline void push_back(FootstepState::Ptr state)
00151     {
00152       int xi = state->indexX();
00153       int yi = state->indexY();
00154       int ti = state->indexT();
00155       VolumeKey local_volume_key = volumeKey(xi, yi, ti);
00156       std::map<VolumeKey, FootstepStateDiscreteCloseListLocal::Ptr>::iterator it
00157         = local_volumes_.find(local_volume_key);
00158       if (it != local_volumes_.end()) { // found!
00159         it->second->add(state);
00160       }
00161       else {
00162         // add new local volume
00163         FootstepStateDiscreteCloseListLocal::Ptr new_local(
00164           new FootstepStateDiscreteCloseListLocal(local_volume_key.get<0>() * local_x_num_,
00165                                                   local_volume_key.get<1>() * local_y_num_,
00166                                                   local_volume_key.get<2>() * local_theta_num_,
00167                                                   local_x_num_,
00168                                                   local_y_num_,
00169                                                   local_theta_num_));
00170         local_volumes_[local_volume_key] = new_local;
00171         new_local->add(state);
00172       }
00173     }
00174 
00175     
00176     inline bool find(FootstepState::Ptr state)
00177     {
00178       int xi = state->indexX();
00179       int yi = state->indexY();
00180       int ti = state->indexT();
00181       VolumeKey key = volumeKey(xi, yi, ti);
00182       std::map<VolumeKey, FootstepStateDiscreteCloseListLocal::Ptr>::iterator it
00183         = local_volumes_.find(key);
00184       if (it != local_volumes_.end()) { // found!
00185         return it->second->get(xi, yi, ti);
00186       }
00187       else {
00188         return false;
00189       }
00190     }
00191 
00192     // This method may be slow
00193     template <class PointT>
00194     void toPointCloud(pcl::PointCloud<PointT>& output)
00195     {
00196       output.points.reserve(size());
00197       for (std::map<VolumeKey, FootstepStateDiscreteCloseListLocal::Ptr>::iterator it
00198              = local_volumes_.begin();
00199            it != local_volumes_.end();
00200            ++it) {
00201         it->second->insertToPointCloud<PointT>(output);
00202       }
00203     }
00204     
00205   protected:
00206     const size_t local_x_num_;
00207     const size_t local_y_num_;
00208     const size_t local_theta_num_;
00209     // local volume := [int][int][int] -> FootstepState::Ptr
00210     // global volume := [int][int][int] -> local volume
00211     std::map<VolumeKey, FootstepStateDiscreteCloseListLocal::Ptr> local_volumes_;
00212   private:
00213     
00214   };
00215 }
00216 
00217 #endif


jsk_footstep_planner
Author(s): Ryohei Ueda
autogenerated on Fri Apr 19 2019 03:45:28