Skip to content
Snippets Groups Projects
Commit 0392614a authored by Florent Lamiraux's avatar Florent Lamiraux
Browse files

Add support for vector and matrix

    * include/CMakeLists.txt,
    * include/dynamic-graph/command-setter.t.cpp,
    * include/dynamic-graph/value.h,
    * src/command/value.cpp.

    Types for vector and matrix are boost::numeric::ublas::vector<double>
    and boost::numeric::ublas::matrix<double>.
parent 9af77dbf
No related branches found
No related tags found
No related merge requests found
...@@ -67,6 +67,7 @@ tracer.h ...@@ -67,6 +67,7 @@ tracer.h
tracer-real-time.h tracer-real-time.h
command.h command.h
linear-algebra.h
value.h value.h
command-setter.h command-setter.h
command-setter.t.cpp command-setter.t.cpp
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include <sstream> #include <sstream>
#include <boost/assign/list_of.hpp> #include <boost/assign/list_of.hpp>
#include "dynamic-graph/linear-algebra.h"
namespace dynamicgraph { namespace dynamicgraph {
class Entity; class Entity;
...@@ -241,6 +242,78 @@ namespace dynamicgraph { ...@@ -241,6 +242,78 @@ namespace dynamicgraph {
return Value(); return Value();
} }
//
// Template specialization: Vector
//
template <class E>
class Setter<E, Vector> : public Command {
public:
/// Pointer to method that sets paramter of type Vector
typedef void (E::*SetterMethod) (const Vector&);
/// Constructor
Setter(E& entity, SetterMethod);
protected:
virtual Value doExecute();
private:
SetterMethod setterMethod_;
}; // Class Setter
template <class E>
Setter<E, Vector>::Setter(E& entity, SetterMethod setterMethod) :
Command(entity, boost::assign::list_of(Value::VECTOR)),
setterMethod_(setterMethod)
{
}
template <class E>
Value Setter<E, Vector>::doExecute()
{
const std::vector<Value>& values = getParameterValues();
// Get parameter
Vector value = values[0].value();
E& entity = static_cast<E&>(owner());
(entity.*setterMethod_)(value);
return Value();
}
//
// Template specialization: Matrix
//
template <class E>
class Setter<E, Matrix> : public Command {
public:
/// Pointer to method that sets paramter of type Matrix
typedef void (E::*SetterMethod) (const Matrix&);
/// Constructor
Setter(E& entity, SetterMethod);
protected:
virtual Value doExecute();
private:
SetterMethod setterMethod_;
}; // Class Setter
template <class E>
Setter<E, Matrix>::Setter(E& entity, SetterMethod setterMethod) :
Command(entity, boost::assign::list_of(Value::MATRIX)),
setterMethod_(setterMethod)
{
}
template <class E>
Value Setter<E, Matrix>::doExecute()
{
const std::vector<Value>& values = getParameterValues();
// Get parameter
Matrix value = values[0].value();
E& entity = static_cast<E&>(owner());
(entity.*setterMethod_)(value);
return Value();
}
} // namespace command } // namespace command
} // namespace dynamicgraph } // namespace dynamicgraph
......
//
// Copyright 2010 CNRS
//
// Author: Florent Lamiraux
//
// This file is part of dynamic-graph.
// dynamic-graph is free software: you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License
// as published by the Free Software Foundation, either version 3 of
// the License, or (at your option) any later version.
// dynamic-graph is distributed in the hope that it will be
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details. You should
// have received a copy of the GNU Lesser General Public License along
// with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#ifndef DYNAMIC_GRAPH_LINEAR_ALGEBRA_H
#define DYNAMIC_GRAPH_LINEAR_ALGEBRA_H
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp>
namespace dynamicgraph {
typedef ::boost::numeric::ublas::vector<double> Vector;
typedef ::boost::numeric::ublas::matrix<double> Matrix;
}
#endif //DYNAMIC_GRAPH_LINEAR_ALGEBRA_H
...@@ -23,6 +23,7 @@ ...@@ -23,6 +23,7 @@
#include <cassert> #include <cassert>
#include <typeinfo> #include <typeinfo>
#include "dynamic-graph/dynamic-graph-api.h" #include "dynamic-graph/dynamic-graph-api.h"
#include "dynamic-graph/linear-algebra.h"
namespace dynamicgraph { namespace dynamicgraph {
namespace command { namespace command {
...@@ -37,6 +38,8 @@ namespace dynamicgraph { ...@@ -37,6 +38,8 @@ namespace dynamicgraph {
operator float () const; operator float () const;
operator double () const; operator double () const;
operator std::string () const; operator std::string () const;
operator Vector () const;
operator Matrix () const;
private: private:
const Value* value_; const Value* value_;
}; };
...@@ -51,19 +54,23 @@ namespace dynamicgraph { ...@@ -51,19 +54,23 @@ namespace dynamicgraph {
FLOAT, FLOAT,
DOUBLE, DOUBLE,
STRING, STRING,
VECTOR,
MATRIX,
NB_TYPES NB_TYPES
}; };
~Value(); ~Value();
Value(const bool& value); explicit Value(const bool& value);
Value(const unsigned& value); explicit Value(const unsigned& value);
Value(const int& value); explicit Value(const int& value);
Value(const float& value); explicit Value(const float& value);
Value(const double& value); explicit Value(const double& value);
Value(const std::string& value); explicit Value(const std::string& value);
explicit Value(const Vector& value);
explicit Value(const Matrix& value);
/// Copy constructor /// Copy constructor
Value(const Value& value); Value(const Value& value);
// Construct an empty value (None) // Construct an empty value (None)
Value(); explicit Value();
/// Return the type of the value /// Return the type of the value
Type type() const; Type type() const;
...@@ -92,6 +99,8 @@ namespace dynamicgraph { ...@@ -92,6 +99,8 @@ namespace dynamicgraph {
float floatValue() const; float floatValue() const;
double doubleValue() const; double doubleValue() const;
std::string stringValue() const; std::string stringValue() const;
Vector vectorValue() const;
Matrix matrixValue() const;
Type type_; Type type_;
const void* value_; const void* value_;
}; };
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
// have received a copy of the GNU Lesser General Public License along // have received a copy of the GNU Lesser General Public License along
// with dynamic-graph. If not, see <http://www.gnu.org/licenses/>. // with dynamic-graph. If not, see <http://www.gnu.org/licenses/>.
#include <boost/numeric/ublas/io.hpp>
#include "dynamic-graph/value.h" #include "dynamic-graph/value.h"
#include "dynamic-graph/exception-abstract.h" #include "dynamic-graph/exception-abstract.h"
...@@ -54,6 +55,14 @@ namespace dynamicgraph { ...@@ -54,6 +55,14 @@ namespace dynamicgraph {
{ {
return value_->stringValue(); return value_->stringValue();
} }
EitherType::operator Vector () const
{
return value_->vectorValue();
}
EitherType::operator Matrix () const
{
return value_->matrixValue();
}
Value::~Value() Value::~Value()
{ {
...@@ -76,6 +85,12 @@ namespace dynamicgraph { ...@@ -76,6 +85,12 @@ namespace dynamicgraph {
case STRING: case STRING:
delete (std::string*)value_; delete (std::string*)value_;
break; break;
case VECTOR:
delete (Vector*)value_;
break;
case MATRIX:
delete (Matrix*)value_;
break;
default:; default:;
} }
} }
...@@ -110,6 +125,16 @@ namespace dynamicgraph { ...@@ -110,6 +125,16 @@ namespace dynamicgraph {
value_ = new std::string(value); value_ = new std::string(value);
type_ = STRING; type_ = STRING;
} }
Value::Value(const Vector& value)
{
value_ = new Vector(value);
type_ = VECTOR;
}
Value::Value(const Matrix& value)
{
value_ = new Matrix(value);
type_ = MATRIX;
}
Value::Value(const Value& value) : type_(value.type_) Value::Value(const Value& value) : type_(value.type_)
...@@ -133,6 +158,12 @@ namespace dynamicgraph { ...@@ -133,6 +158,12 @@ namespace dynamicgraph {
case STRING: case STRING:
value_ = new std::string(value.stringValue()); value_ = new std::string(value.stringValue());
break; break;
case VECTOR:
value_ = new Vector(value.vectorValue());
break;
case MATRIX:
value_ = new Matrix(value.matrixValue());
break;
default: default:
type_ = NONE; type_ = NONE;
value_ = NULL; value_ = NULL;
...@@ -205,6 +236,22 @@ namespace dynamicgraph { ...@@ -205,6 +236,22 @@ namespace dynamicgraph {
"value is not an string"); "value is not an string");
} }
Vector Value::vectorValue () const
{
if (type_ == VECTOR)
return *((Vector*)value_);
throw ExceptionAbstract(ExceptionAbstract::TOOLS,
"value is not an vector");
}
Matrix Value::matrixValue () const
{
if (type_ == MATRIX)
return *((Matrix*)value_);
throw ExceptionAbstract(ExceptionAbstract::TOOLS,
"value is not a matrix");
}
std::string Value::typeName(Type type) std::string Value::typeName(Type type)
{ {
switch (type) { switch (type) {
...@@ -220,6 +267,10 @@ namespace dynamicgraph { ...@@ -220,6 +267,10 @@ namespace dynamicgraph {
return std::string("double"); return std::string("double");
case STRING: case STRING:
return std::string("string"); return std::string("string");
case VECTOR:
return std::string("vector");
case MATRIX:
return std::string("matrix");
default: default:
return std::string("unknown"); return std::string("unknown");
} }
...@@ -248,6 +299,12 @@ namespace dynamicgraph { ...@@ -248,6 +299,12 @@ namespace dynamicgraph {
case Value::STRING: case Value::STRING:
os << value.stringValue(); os << value.stringValue();
break; break;
case Value::VECTOR:
os << value.vectorValue();
break;
case Value::MATRIX:
os << value.matrixValue();
break;
default: default:
return os; return os;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment