This forum has been archived. All content is frozen. Please use KDE Discuss instead.

dot product with custom type

Tags: None
(comma "," separated)
User avatar
dzenanz
Registered Member
Posts
35
Karma
0
OS

dot product with custom type

Fri Oct 22, 2010 8:13 am
I have an equivalent of this code (slightly more convoluted):
Code: Select all
std::vector<osg::Vec3> vertex(N);
Eigen::Matrix<osg::Vec3,Eigen::Dynamic,1> S0; //m elements
Eigen::MatrixXd W=createWeightMatrix(...); //N columns, m rows
for (int i=0; i<N; i++)
    vertex[i]=S0.dot(W.col(i)); //error on this line

When I try to compile it I get the following error (verbosely explaining that osg::Vec3f is not properly supported type). Is there a way around it?

Full error text:
Code: Select all
d:\sdk\eigen\eigen\src/Core/Redux.h(352): error C2440: '<function-style-cast>' : cannot convert from 'int' to 'osg::Vec3f'
          No constructor could take the source type, or constructor overload resolution was ambiguous
          d:\sdk\eigen\eigen\src/Core/Redux.h(350) : while compiling class template member function 'osg::Vec3f Eigen::DenseBase<Derived>::sum(void) const'
          with
          [
              Derived=Eigen::CwiseBinaryOp<Eigen::ei_scalar_conj_product_op<osg::Vec3f>,Eigen::Matrix<osg::Vec3,-1,1>,Eigen::Block<Eigen::Matrix<double,-1,-1>,-1,1,true>>
          ]
          d:\sdk\eigen\eigen\src/Core/MatrixBase.h(60) : see reference to class template instantiation 'Eigen::DenseBase<Derived>' being compiled
          with
          [
              Derived=Eigen::CwiseBinaryOp<Eigen::ei_scalar_conj_product_op<osg::Vec3f>,Eigen::Matrix<osg::Vec3,-1,1>,Eigen::Block<Eigen::Matrix<double,-1,-1>,-1,1,true>>
          ]
          d:\sdk\eigen\eigen\src/Core/CwiseBinaryOp.h(175) : see reference to class template instantiation 'Eigen::MatrixBase<Derived>' being compiled
          with
          [
              Derived=Eigen::CwiseBinaryOp<Eigen::ei_scalar_conj_product_op<osg::Vec3f>,Eigen::Matrix<osg::Vec3,-1,1>,Eigen::Block<Eigen::Matrix<double,-1,-1>,-1,1,true>>
          ]
          d:\sdk\eigen\eigen\src/Core/CwiseBinaryOp.h(121) : see reference to class template instantiation 'Eigen::CwiseBinaryOpImpl<BinaryOp,Lhs,Rhs,StorageKind>' being compiled
          with
          [
              BinaryOp=Eigen::ei_scalar_conj_product_op<osg::Vec3f>,
              Lhs=Eigen::Matrix<osg::Vec3,-1,1>,
              Rhs=Eigen::Block<Eigen::Matrix<double,-1,-1>,-1,1,true>,
              StorageKind=Eigen::Dense
          ]
          d:\sdk\eigen\eigen\src/Core/Dot.h(44) : see reference to class template instantiation 'Eigen::CwiseBinaryOp<BinaryOp,Lhs,Rhs>' being compiled
          with
          [
              BinaryOp=Eigen::ei_scalar_conj_product_op<osg::Vec3f>,
              Lhs=Eigen::Matrix<osg::Vec3,-1,1>,
              Rhs=Eigen::Block<Eigen::Matrix<double,-1,-1>,-1,1,true>
          ]
          d:\sdk\eigen\eigen\src/Core/Dot.h(43) : while compiling class template member function 'osg::Vec3f Eigen::ei_dot_nocheck<T,U>::run(const Eigen::MatrixBase<Derived> &,const Eigen::MatrixBase<Eigen::Block<XprType,BlockRows,BlockCols,InnerPanel>> &)'
          with
          [
              T=Eigen::Matrix<osg::Vec3,-1,1>,
              U=Eigen::Block<Eigen::Matrix<double,-1,-1>,-1,1,true>,
              Derived=Eigen::Matrix<osg::Vec3,-1,1>,
              XprType=Eigen::Matrix<double,-1,-1>,
              BlockRows=-1,
              BlockCols=1,
              InnerPanel=true
          ]
          d:\sdk\eigen\eigen\src/Core/Dot.h(80) : see reference to class template instantiation 'Eigen::ei_dot_nocheck<T,U>' being compiled
          with
          [
              T=Eigen::Matrix<osg::Vec3,-1,1>,
              U=Eigen::Block<Eigen::Matrix<double,-1,-1>,-1,1,true>
          ]
          MainLogic.cpp(726) : see reference to function template instantiation 'osg::Vec3f Eigen::MatrixBase<Derived>::dot<Eigen::Block<XprType,BlockRows,BlockCols,InnerPanel>>(const Eigen::MatrixBase<Eigen::Block<XprType,BlockRows,BlockCols,InnerPanel>> &) const' being compiled
          with
          [
              Derived=Eigen::Matrix<osg::Vec3,-1,1>,
              XprType=Eigen::Matrix<double,-1,-1>,
              BlockRows=-1,
              BlockCols=1,
              InnerPanel=true
          ]
User avatar
dzenanz
Registered Member
Posts
35
Karma
0
OS

Re: dot product with custom type

Fri Oct 22, 2010 9:18 am
It is not a problem to do dot product "by hand":
Code: Select all
v->pos=osg::Vec3(0,0,0);
for (unsigned i=0; i<S0.size(); i++)
    v->pos+=S0[i]*W.coeff(i,v->realIndex());

but I wondered whether there is a more "elegant" way.

Regards,
Dženan
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: dot product with custom type

Mon Oct 25, 2010 10:40 am
Apparently you're trying to use osg::Vec3 as Scalar type but you need first to tell Eigen about it:
http://eigen.tuxfamily.org/dox-devel/To ... ScalarType

or you can use Eigen's Array class here as Scalar type, e.g. Eigen::Array3f

or you can use a 3xN matrix, e.g. Matrix<float, 3, Dynamic>.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
User avatar
dzenanz
Registered Member
Posts
35
Karma
0
OS

Re: dot product with custom type

Mon Oct 25, 2010 10:46 am
I tried customizing with scalar type - however I am not familiar with the library and that approach is verbose. In the meanwhile the code got more complicated and I switched to N*3 matrix. That required minimum amount of coding.

Thanks
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
yes, actually, what you are trying to do boils down to a standard matrix product:

Code: Select all
Eigen::Matrix<double,3,Dynamic> vertices(3,N);
Eigen::Matrix<double,3,Dynamic> S0(3,m);
Eigen::MatrixXd W=createWeightMatrix(...); //N columns, m rows

vertices = S0 * W;
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: dot product with custom type

Sun Oct 31, 2010 1:29 pm
for your convenience you could also try the following specialization allow you to declare use a Matrix<osg::Vec3,1,Dynamic> as a 3xN Eigen matrix with a convenient ctor and operator[] to access the elements.

I've not tried it so there are probably a few shortcomings...

Code: Select all
namespace Eigen {

template<> class Matrix<osg::Vec3,1,Dynamic,0,1,Dynamic> : public Matrix<double,3,Dynamic> {
  typedef Matrix<double,3,Dynamic> Base;
public:

  using Base::operator=;
 
  Matrix(int size) : Base(3,size) {}

  osg::Vec3& operator[] (int i) { return *reinterpret_cast<osg::Vec3*>(this->col(i).data()); }
  const osg::Vec3& operator[] (int i) const { return *reinterpret_cast<const osg::Vec3*>(this->col(i).data()); }
};

namespace internal {

template<> struct traits<Matrix<osg::Vec3,1,Dynamic,0,1,Dynamic> > : public traits<Matrix<double,3,Dynamic> > {};

}
User avatar
dzenanz
Registered Member
Posts
35
Karma
0
OS

Re: dot product with custom type

Sun Oct 31, 2010 4:58 pm
Thanks for the hint. If I need to change the code in the future, I might use this way.


Bookmarks



Who is online

Registered users: Bing [Bot], Evergrowing, Google [Bot], rockscient