iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-State.C
/*! @file
@author Laurent Itti <itti@usc.edu>
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit 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 General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
struct Pos3D
{
Pos3D(double storage[]) : x(storage[0]), y(storage[1]), z(storage[2])
{ std::cout << "Pos3D addresses: " << &x << ' ' << &y << ' ' << &z <<std::endl; }
double & x;
double & y;
double & z;
static size_t const size = 3;
};
std::ostream & operator<<(std::ostream & os, Pos3D const & pos)
{ os << "Pos3D(" << pos.x << ", " << pos.y << ", " << pos.z << ')'; return os; }
struct Vel3D
{
Vel3D(double storage[]) : vx(storage[0]), vy(storage[1]), vz(storage[2])
{ std::cout << "Vel3D addresses: " << &vx << ' ' << &vy << ' ' << &vz <<std::endl; }
double & vx;
double & vy;
double & vz;
static size_t const size = 3;
};
std::ostream & operator<<(std::ostream & os, Vel3D const & vel)
{ os << "Vel3D(" << vel.vx << ", " << vel.vy << ", " << vel.vz << ')'; return os; }
int main(int argc, char const * argv[])
{
// ########## Simple scenario:
typedef nrt::StateDef<Pos3D, Vel3D> myState;
myState::datatype data;
myState state(data);
state.get<Pos3D>().x = 1.0;
state.get<Pos3D>().y = 2.0;
state.get<Pos3D>().z = 3.0;
state.get<Vel3D>().vx = 4.0;
state.get<Vel3D>().vy = 5.0;
state.get<Vel3D>().vz = 6.0;
std::cout << "Pos3D only: " << state.get<Pos3D>() << std::endl;
std::cout << "Full state: " << state << std::endl;
// ########## Composition works:
typedef nrt::StateDef<Pos3D, Vel3D> MachineState;
FactoryState::datatype factorydata;
FactoryState s(factorydata);
std::cout << "FactoryState size = " << FactoryState::size << std::endl;
ComplicatedState::datatype csdata;
ComplicatedState cs(csdata);
std::cout << "ComplicatedState size = " << ComplicatedState::size << std::endl;
cs.get<Vel3D>().vy = 123.0;
cs.get<FactoryState>().get<Pos3D>().x = 456.0;
cs.get<FactoryState>().get<MachineState>().get<Pos3D>().z = 789.0;
std::cout << "ComplicatedState: " << cs << std::endl;
size_t idx = cs.indexof(cs.get<FactoryState>().get<MachineState>().get<Vel3D>().vy);
// modify our raw data directly:
csdata[idx] = 42.0;
// should be reflected in our State:
std::cout << "index test: index = " << idx << ", value = " << cs[idx] << std::endl;
std::cout << "index test: value = " << cs.get<FactoryState>().get<MachineState>().get<Vel3D>().vy << std::endl;
// Say we want to get the address of the FactoryState; then we want to work from there
FactoryState & baseref = cs.get<FactoryState>();
size_t baseidx = cs.index<FactoryState>();
size_t offset = baseref.indexof(baseref.get<Vel3D>().vx);
std::cout << "baseidx = " << baseidx << ", offset = " << offset <<std::endl;
cs[baseidx + offset] = 27.0;
std::cout << "ComplicatedState: " << cs << std::endl;
gauss.mean(gauss.elem->get<Pos3D>().x) = 10;
gauss.mean(gauss.elem->get<Pos3D>().y) = 20;
gauss.mean(gauss.elem->get<Pos3D>().z) = 30;
gauss.covariance(gauss.elem->get<Pos3D>().x) = 4*4;
gauss.covariance(gauss.elem->get<Pos3D>().y) = 4*4;
gauss.covariance(gauss.elem->get<Pos3D>().z) = 4*4;
printf("Set Particle\n");
particles.at(1, particles.elem->get<Pos3D>().x) = 10;
particles.at(1) = 4.5;
printf("Show Particle\n");
printf("Pos 3d %f %f %f %f\n",
particles.at(1, particles.elem->get<Pos3D>().x),
particles.at(1, particles.elem->get<Pos3D>().y),
particles.at(1, particles.elem->get<Pos3D>().z),
particles.at(1));
printf("Samples\n");
for(uint i=0; i<samples.size(); i++)
{
printf("%i\n", i);
printf("Pos 3d (%f %f %f) %f\n",
samples.at(i, samples.elem->get<Pos3D>().x),
samples.at(i, samples.elem->get<Pos3D>().y),
samples.at(i, samples.elem->get<Pos3D>().z),
samples.at(i));
}
return 0;
}