substate.h
Go to the documentation of this file.
00001 //=================================================================================================
00002 // Copyright (c) 2013, Johannes Meyer, TU Darmstadt
00003 // All rights reserved.
00004 
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are met:
00007 //     * Redistributions of source code must retain the above copyright
00008 //       notice, this list of conditions and the following disclaimer.
00009 //     * Redistributions in binary form must reproduce the above copyright
00010 //       notice, this list of conditions and the following disclaimer in the
00011 //       documentation and/or other materials provided with the distribution.
00012 //     * Neither the name of the Flight Systems and Automatic Control group,
00013 //       TU Darmstadt, nor the names of its contributors may be used to
00014 //       endorse or promote products derived from this software without
00015 //       specific prior written permission.
00016 
00017 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00018 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00019 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00020 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //=================================================================================================
00028 
00029 #ifndef HECTOR_POSE_ESTIMATION_SUBSTATE_H
00030 #define HECTOR_POSE_ESTIMATION_SUBSTATE_H
00031 
00032 #include <hector_pose_estimation/state.h>
00033 
00034 namespace hector_pose_estimation {
00035 
00036 class SubState
00037 {
00038 public:
00039   SubState(State& state) : state_(state) {}
00040   virtual ~SubState() {}
00041 
00042   virtual int getDimension() const = 0;
00043   virtual int getIndex() const = 0;
00044 
00045   virtual void reset() {}
00046   virtual void updated() {}
00047   virtual void normalize() {}
00048 
00049 protected:
00050   State& state_;
00051   template <int _Dimension> class initializer;
00052 };
00053 
00054 template <int _Dimension> class SubState::initializer {
00055 public:
00056   enum { Dimension = _Dimension };
00057   initializer(State& state) : index_(state.getDimension()) {
00058     IndexType newDimension = index_ + Dimension;
00059     state.x().conservativeResize(newDimension);
00060     state.P().conservativeResize(newDimension);
00061   }
00062 protected:
00063   const IndexType index_;
00064 };
00065 
00066 template <> class SubState::initializer<0> {
00067 public:
00068   enum { Dimension = State::Dimension };
00069   initializer(State&) : index_(0) {}
00070 protected:
00071   const IndexType index_;
00072 };
00073 
00074 template <int _Dimension>
00075 class SubState_ : public SubState, public SubState::initializer<_Dimension>
00076 {
00077 public:
00078   enum { Dimension = SubState::initializer<_Dimension>::Dimension };
00079   typedef ColumnVector_<Dimension> Vector;
00080 
00081   typedef VectorBlock<State::Vector,Dimension> VectorSegment;
00082   typedef Block<State::Covariance::Base,Dimension,Dimension> CovarianceBlock;
00083   typedef Block<State::Covariance::Base,State::Dimension,Dimension> CrossVarianceBlock;
00084 
00085   typedef VectorBlock<const State::Vector,Dimension> ConstVectorSegment;
00086   typedef Block<const State::Covariance::Base,Dimension,Dimension> ConstCovarianceBlock;
00087   typedef Block<const State::Covariance::Base,State::Dimension,Dimension> ConstCrossVarianceBlock;
00088 
00089   typedef boost::shared_ptr<SubState_<_Dimension> > Ptr;
00090 
00091   using SubState::initializer<_Dimension>::index_;
00092 
00093 public:
00094   SubState_(State& state)
00095     : SubState(state)
00096     , SubState::initializer<_Dimension>(state)
00097 //    , vector_(state.x().template segment<Dimension>(index_))
00098 //    , covariance_(state.P().template block<Dimension,Dimension>(index_, index_))
00099 //    , cross_variance_(state.P().template block<State::Dimension,Dimension>(0, index_))
00100   {
00101   }
00102   virtual ~SubState_() {}
00103 
00104   int getDimension() const { return Dimension; }
00105   int getIndex() const { return index_; }
00106 
00107   ConstVectorSegment getVector() const { return state_.getVector().segment<Dimension>(index_); }
00108   ConstCovarianceBlock getCovariance() const { return state_.getCovariance().block<Dimension,Dimension>(index_,index_); }
00109   template <int Size> VectorBlock<const typename Vector::Base, Size> getSegment(IndexType start) const { return state_.getVector().segment<Dimension>(index_ + start); }
00110 
00111   VectorSegment x() { return state_.x().segment<Dimension>(index_); }
00112   CovarianceBlock P() { return state_.P().block<Dimension,Dimension>(index_,index_); }
00113   CrossVarianceBlock P01() { return state_.P().block<State::Dimension,Dimension>(0,index_); }
00114 };
00115 
00116 extern template class SubState_<0>;
00117 
00118 template <int SubDimension>
00119 boost::shared_ptr<SubState_<SubDimension> > State::getSubState(const Model *model) const {
00120   return boost::shared_dynamic_cast<SubState_<SubDimension> >(substates_by_model_.count(model) ? substates_by_model_.at(model).lock() : SubStatePtr());
00121 }
00122 
00123 template <>
00124 inline boost::shared_ptr<BaseState> State::getSubState<0>(const Model *) const {
00125   return base_;
00126 }
00127 
00128 template <int SubDimension>
00129 boost::shared_ptr<SubState_<SubDimension> > State::getSubState(const std::string& name) const {
00130   return boost::shared_dynamic_cast<SubState_<SubDimension> >(substates_by_name_.count(name) ? substates_by_name_.at(name).lock() : SubStatePtr());
00131 }
00132 
00133 template <int SubDimension>
00134 boost::shared_ptr<SubState_<SubDimension> > State::addSubState(const Model *model, const std::string& name) {
00135   boost::shared_ptr<SubState_<SubDimension> > substate;
00136   if (!name.empty()) substate = getSubState<SubDimension>(name);
00137   if (!substate) {
00138     substate.reset(new SubState_<SubDimension>(*this));
00139     substates_.push_back(boost::shared_static_cast<SubState>(substate));
00140     if (!name.empty()) substates_by_name_[name] = SubStateWPtr(boost::shared_static_cast<SubState>(substate));
00141   }
00142   substates_by_model_[model] = SubStateWPtr(boost::shared_static_cast<SubState>(substate));
00143   return substate;
00144 }
00145 
00146 } // namespace hector_pose_estimation
00147 
00148 #endif // HECTOR_POSE_ESTIMATION_SUBSTATE_H


hector_pose_estimation_core
Author(s): Johannes Meyer
autogenerated on Mon Oct 6 2014 00:24:16