I have a particle class which keeps the position, velocity and the acceleration of an object. I can access the related part of the vector via .position()
, .velocity()
and .acceleration()
. I also can access each single number like .velocity_x()
, .velocity_y()
, .velocity_z()
etc. what I would like to do is to access the z part of velocity this way:
p.velocity().z()
Is this implementation possible in c++?
#include <iostream>
#include <armadillo>
class Particle
{
public:
arma::vec::fixed<9> data;
inline double velocity_z() const
{
return data(5);
}
inline double& velocity_z()
{
return data(5);
}
inline const arma::subview_col<double> position() const
{
return data.subvec(0,2);
}
inline arma::subview_col<double> position()
{
return data.subvec(0,2);
}
inline const arma::subview_col<double> velocity() const
{
return data.subvec(3,5);
}
inline arma::subview_col<double> velocity()
{
return data.subvec(3,5);
}
inline const arma::subview_col<double> acceleration() const
{
return data.subvec(6,8);
}
inline arma::subview_col<double> acceleration()
{
return data.subvec(6,8);
}
};
arma::vec vector3(double x,double y,double z)
{
return {x,y,z};
}
int main()
{
Particle p;
p.position()=vector3(1.1,2.1,3.1);
p.velocity()=vector3(1.2,2.2,3.2);
p.velocity_z()=10.0;
p.acceleration()=vector3(1.3,2.3,3.3);
p.data.print();
return 0;
}
// output:
// 1.1000
// 2.1000
// 3.1000
// 1.2000
// 2.2000
// 10.0000
// 1.3000
// 2.3000
// 3.3000
make:
g++ -std=c++11 test1.cpp -larmadillo
link to armadillo documentation
update
I would like to use both .velocity()
(as a subvector) and .velocity().z()
(as a single number) at the same time.
I also would like to avoid defining any extra variable. I prefer everything be interpreted at the compile time (due to performance priority).
Aucun commentaire:
Enregistrer un commentaire