I'm wrapping two Eigen3 vectors in a templated LineSegment<T,dim>
class. You might use it like this:
typedef LineSegment<double,2> LineSegment2d;
typedef LineSegment<double,3> LineSegment3d;
typedef LineSegment<int,3> LineSegment3i;
It contains a templated method to change the dimensions of the components. Here is the trimmed definition:
template<typename T,int dim>
struct LineSegment
{
public:
template<int newDim>
LineSegment<T,newDim> to() const
{
Eigen::Matrix<T,newDim,1> newp1;
Eigen::Matrix<T,newDim,1> newp2;
// TODO initialise newp1 and newp2 from d_p1 and d_p2
return LineSegment<T,newDim>(newp1, newp2);
}
// ... other members ...
protected:
Eigen::Matrix<T,dim,1> d_p1;
Eigen::Matrix<T,dim,1> d_p2;
}
So my question is, how can I compose the return value, as shown above? This should support both increasing and decreasing the dimension.
I tried using the Eigen3 resize(int) method, but couldn't get it to work without seeing warnings about mixing matrix sizes.
Ultimately, this should work:
LineSegment2d ls2d;
LineSegment3d ls3d = ls2d.to<3>(); // increase dim
ls2d = ls3d.to<2>(); // decrease dim
I'm relatively new to C++ templates, and would appreciate a bit of explanation if this isn't just an API question and is related to templates.