CCA
NemoTransformation.h
Go to the documentation of this file.
00001 /* ============================================================
00002  *
00003  * This file is a part of CCA project
00004  *
00005  * Copyright (C) 2011 by Arne Nordmann <anordman at cor-lab dot uni-bielefeld dot de>
00006  *
00007  * This file may be licensed under the terms of the
00008  * GNU Lesser General Public License Version 3 (the ``LGPL''),
00009  * or (at your option) any later version.
00010  *
00011  * Software distributed under the License is distributed
00012  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
00013  * express or implied. See the LGPL for the specific language
00014  * governing rights and limitations.
00015  *
00016  * You should have received a copy of the LGPL along with this
00017  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
00018  * or write to the Free Software Foundation, Inc.,
00019  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  * The development of this software was supported by:
00022  *   CoR-Lab, Research Institute for Cognition and Robotics
00023  *     Bielefeld University
00024  *
00025  * ============================================================ */
00026 
00027 #pragma once
00028 
00029 #include <string>
00030 
00031 #include <boost/shared_ptr.hpp>
00032 
00033 #include <nemo/Vector.h>
00034 #include <nemo/Mapping.h>
00035 
00036 #include "cca/Node.h"
00037 #include "cca/processing/Moderate.h"
00038 
00039 typedef nemo::Mapping<nemo::RealVector, nemo::RealVector> VectorMapping;
00040 
00041 namespace cca {
00042 
00050 template<class T>
00051 class NemoTransformation: public Node {
00052 public:
00053 
00060     void onProcess() {
00061         nemo::RealVector in(nemo::dim(2), 0.0), out(nemo::dim(2), 0.0);
00062 
00063         in[0] = boost::static_pointer_cast<T>(this->inputs[0]->get())->x();
00064         in[1] = boost::static_pointer_cast<T>(this->inputs[0]->get())->y();
00065 
00066         out = this->mapping(in);
00067 
00068         typename boost::shared_ptr<T> send(new T(out[0], out[1], 0));
00069         this->publish(0, send);
00070     }
00071 
00078     static NodePtr create(const std::string &nodename,
00079             VectorMapping mapping) {
00080         return NodePtr(new NemoTransformation<T>(nodename, mapping));
00081     }
00082 
00089     friend std::ostream & operator<<(std::ostream& os,
00090             const NemoTransformation<T> &node) {
00091         os.precision(3); // Precision when printing double values
00092         os << std::endl << "<NemoTransformation '" << node.getName() << "'>"
00093                 << std::endl;
00094         return os;
00095     }
00096 
00097     ~NemoTransformation() {
00098     }
00099 
00100 protected:
00106     NemoTransformation(const std::string &nodename, VectorMapping mappng) :
00107             Node(nodename), mapping(mappng) {
00108 
00109         add("in", InputPort < T > ::create());
00110         add("out", OutputPort < T > ::create());
00111 
00112         // Define default strategy - moderate
00113         this->setProcessingStrategy(Moderate::timeout());
00114     }
00115 
00116 private:
00117     VectorMapping mapping;
00118 };
00119 
00120 }