iCub-main
ScaleTransformer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007-2011 RobotCub Consortium, European Commission FP6 Project IST-004370
3  * author: Arjan Gijsberts
4  * email: arjan.gijsberts@iit.it
5  * website: www.robotcub.org
6  * Permission is granted to copy, distribute, and/or modify this program
7  * under the terms of the GNU General Public License, version 2 or any
8  * later version published by the Free Software Foundation.
9  *
10  * A copy of the license can be found at
11  * http://www.robotcub.org/icub/license/gpl.txt
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
16  * Public License for more details
17  */
18 
19 #include <stdexcept>
20 #include <cassert>
21 #include <sstream>
22 
25 
26 namespace iCub {
27 namespace learningmachine {
28 
29 
30 
32  this->setName("Scaler");
33  this->setDomainSize(dom);
34 }
35 
37  this->deleteAll();
38 }
39 
41  : IFixedSizeTransformer(other) {
42  this->scalers.resize(other.scalers.size());
43  for(unsigned int i = 0; i < other.scalers.size(); i++) {
44  this->scalers[i] = other.scalers[i]->clone();
45  }
46 }
47 
49  if(this == &other) return *this; // handle self initialization
50 
51  this->IFixedSizeTransformer::operator=(other);
52 
53  this->deleteAll(other.scalers.size());
54  for(unsigned int i = 0; i < other.scalers.size(); i++) {
55  this->scalers[i] = other.scalers[i]->clone();
56  }
57 
58  return *this;
59 }
60 
62  if (index >= 0 && index < int(this->scalers.size())) {
63  return this->scalers[index];
64  } else {
65  throw std::runtime_error("Index for scaler out of bounds!");
66  }
67 }
68 
69 
71  this->deleteAll(this->scalers.size());
72 }
73 
75  for(std::vector<IScaler*>::iterator it = this->scalers.begin(); it != this->scalers.end(); it++) {
76  delete *it;
77  }
78  this->scalers.clear();
79  this->scalers.resize(size);
80  this->setAll("null");
81 }
82 
83 void ScaleTransformer::setAt(int index, std::string type) {
84  if(index >= 0 && index < int(this->scalers.size())) {
85  delete this->scalers[index];
86  this->scalers[index] = (IScaler *) 0;
87  this->scalers[index] = FactoryT<std::string, IScaler>::instance().create(type);
88  } else {
89  throw std::runtime_error("Index for scaler out of bounds!");
90  }
91 }
92 
93 void ScaleTransformer::setAll(std::string type) {
94  //this->clearVector();
95  for(unsigned int i = 0; i < this->scalers.size(); i++) {
96  this->setAt(i, type);
97  }
98 }
99 
100 yarp::sig::Vector ScaleTransformer::transform(const yarp::sig::Vector& input) {
101  yarp::sig::Vector output = this->IFixedSizeTransformer::transform(input);
102  assert(input.size() == int(this->scalers.size()));
103  assert(output.size() == int(this->scalers.size()));
104 
105  for(size_t i = 0; i < output.size(); i++) {
106  output(i) = this->getAt(i)->transform(input(i));
107  }
108  return output;
109 }
110 
111 void ScaleTransformer::setDomainSize(unsigned int size) {
112  // domain size and codomain have to be equally sized
115  this->reset();
116 }
117 
118 void ScaleTransformer::setCoDomainSize(unsigned int size) {
119  // domain size and codomain have to be equally sized
120  this->setDomainSize(size);
121 }
122 
124  this->ITransformer::reset();
125  this->deleteAll(this->getDomainSize());
126 }
127 
129 
130  std::ostringstream buffer;
131  buffer << this->IFixedSizeTransformer::getInfo();
132  buffer << "Scalers:" << std::endl;
133  for(unsigned int i = 0; i < this->scalers.size(); i++) {
134  buffer << " [" << (i + 1) << "] ";
135  buffer << this->scalers[i]->getInfo() << std::endl;
136  }
137  return buffer.str();
138 }
139 
141  std::ostringstream buffer;
142  buffer << this->IFixedSizeTransformer::getConfigHelp();
143  buffer << " type idx|all id Scaler type" << std::endl;
144  buffer << " config idx|all key v Set scaler configuration option" << std::endl;
145  //buffer << " update idx|all Toggles updating on data on or off" << std::endl;
146  return buffer.str();
147 }
148 
149 void ScaleTransformer::writeBottle(yarp::os::Bottle& bot) {
150  // write all scalers
151  for(unsigned int i = 0; i < this->getDomainSize(); i++) {
152  bot.addString(this->getAt(i)->toString().c_str());
153  bot.addString(this->getAt(i)->getName().c_str());
154  }
155 
156  // make sure to call the superclass's method
158 }
159 
160 void ScaleTransformer::readBottle(yarp::os::Bottle& bot) {
161  // make sure to call the superclass's method (will reset transformer)
163 
164  // read all scalers in reverse order
165  for(int i = this->getDomainSize() - 1; i >= 0; i--) {
166  this->setAt(i, bot.pop().asString().c_str());
167  this->getAt(i)->fromString(bot.pop().asString().c_str());
168  }
169 }
170 
171 bool ScaleTransformer::configure(yarp::os::Searchable &config) {
172  bool success = this->IFixedSizeTransformer::configure(config);
173 
174  // format: set type (ScalerName ScalerName)
175  if(config.find("type").isList()) {
176  yarp::os::Bottle* scaleList = config.find("type").asList();
177  for(int i = 0; i < scaleList->size(); i++) {
178  if(scaleList->get(i).isString()) {
179  this->setAt(i, scaleList->get(i).asString().c_str());
180  success = true;
181  }
182  // NOTE TO SELF: consider throwing an exception in the else clause
183  }
184  }
185 
186  // format: set type idx|all ScalerName
187  if(!config.findGroup("type").isNull()) {
188  //success = true;
189  yarp::os::Bottle list = config.findGroup("type").tail();
190  if(list.get(0).isInt32() && list.get(1).isString()) {
191  // shift index, since internal numbering in vector starts at 0, the user starts at 1
192  this->setAt(list.get(0).asInt32() - 1, list.get(1).asString().c_str());
193  success = true;
194  } else if(list.get(0).asString() == "all" && list.get(1).isString()) {
195  this->setAll(list.get(1).asString().c_str());
196  success = true;
197  }
198  }
199 
200  // format: set config idx|all key val
201  if(!config.findGroup("config").isNull()) {
202  yarp::os::Bottle property;
203  yarp::os::Bottle list = config.findGroup("config").tail();
204  property.addList() = list.tail();
205  if(list.get(0).isInt32()) {
206  // format: set config idx key val
207  int i = list.get(0).asInt32() - 1;
208  success = this->getAt(i)->configure(property);
209  } else if(list.get(0).asString() == "all") {
210  // format: set config all key val
211  for(unsigned int i = 0; i < this->scalers.size(); i++) {
212  success |= this->getAt(i)->configure(property);
213  }
214  }
215  }
216 
217  return success;
218 }
219 
220 
221 } // learningmachine
222 } // iCub
223 
static FactoryT< K, T > & instance()
An instance retrieval method that follows the Singleton pattern.
Definition: FactoryT.h:86
An generalized interface for an ITransformer with a fixed domain and codomain size.
virtual std::string getConfigHelp()
Asks the transformer to return a string containing the list of configuration options that it supports...
virtual yarp::sig::Vector transform(const yarp::sig::Vector &input)
Transforms an input vector.
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a transformer from a bottle.
virtual void setDomainSize(unsigned int size)
Mutator for the domain size.
virtual void writeBottle(yarp::os::Bottle &bot) const
Writes a serialization of the transformer into a bottle.
virtual bool configure(yarp::os::Searchable &config)
virtual void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
virtual std::string getInfo()
Asks the transformer to return a string containing statistics on its operation so far.
unsigned int getDomainSize() const
Returns the size (dimensionality) of the input domain.
The IScaler is a linear scaler based scaler.
Definition: IScaler.h:41
virtual std::string toString()
Asks the scaler to return a string serialization.
Definition: IScaler.cpp:49
std::string getName() const
Retrieve the name of this scaler.
Definition: IScaler.h:133
virtual double transform(double val)
Transforms a single sample value according to the state of the scaler.
Definition: IScaler.cpp:30
virtual bool fromString(const std::string &str)
Asks the scaler to initialize from a string serialization.
Definition: IScaler.cpp:55
virtual bool configure(yarp::os::Searchable &config)
Definition: IScaler.cpp:73
virtual void reset()
Forget everything and start over.
Definition: ITransformer.h:125
void setName(std::string name)
Set the name of this transformer.
Definition: ITransformer.h:143
The ScaleTransformer is a ITransformer that supports element-based scaling transformations.
virtual void setDomainSize(unsigned int size)
Mutator for the domain size.
IScaler * getAt(int index)
Returns a pointer to the scaler at a certain position.
virtual std::string getConfigHelp()
Asks the transformer to return a string containing the list of configuration options that it supports...
virtual bool configure(yarp::os::Searchable &config)
virtual void readBottle(yarp::os::Bottle &bot)
Unserializes a transformer from a bottle.
ScaleTransformer(unsigned int dom=1)
Constructor.
virtual void writeBottle(yarp::os::Bottle &bot)
std::vector< IScaler * > scalers
The vector of IScaler objects.
virtual yarp::sig::Vector transform(const yarp::sig::Vector &input)
Transforms an input vector.
void setAt(int index, std::string type)
Sets the scaler at a certain position to a given type.
void setAll(std::string type)
Sets all scalers to a given type.
virtual void reset()
Forget everything and start over.
virtual void setCoDomainSize(unsigned int size)
Mutator for the codomain size.
ScaleTransformer & operator=(const ScaleTransformer &other)
Assignment operator.
void deleteAll()
Resets the vector of scalers and deletes each element.
virtual std::string getInfo()
Asks the transformer to return a string containing statistics on its operation so far.
This file contains the definition of unique IDs for the body parts and the skin parts of the robot.