5

Can Eigen do a 2D cross product?

I was trying to replace this code:

Eigen::Vector2f a, b;
float result = a.x()*b.y() - b.x()*a.y();

With this:

Eigen::Vector2f a, b;
float result = a.cross(b);

However, this gives me the following error:

error C2338: THIS_METHOD_IS_ONLY_FOR_VECTORS_OF_A_SPECIFIC_SIZE

Update

Of course Avi Ginsburg is right and its not really defined. So to clarify: What I'm looking for is the length of the cross product (basically the sine of the angle between the vectors, if I understand it correctly).

Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
  • What do you have against the first solution ? You can consider computing the 2x2 determinant. –  Oct 08 '15 at 10:01
  • 1
    @YvesDaoust I just thought it would be nicer to use a predefined function for such a "standard" computation, instead of doing the computation myself... – Jan Rüegg Oct 08 '15 at 10:58
  • For the 2x2 case, using generic functions is most probably overkill. –  Oct 08 '15 at 12:06
  • This question has already been considered, see this [feature request](http://eigen.tuxfamily.org/bz/show_bug.cgi?id=1037). – ggael Oct 08 '15 at 07:23

1 Answers1

0

The result of a cross product is a vector, not a float. And anyway, a cross product in 2D doesn't make sense. In 2D the result vector would have to be perpendicular to both a and b and they already define the plane, so the result would have to be in the 3rd dimension.

Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56
  • Thanks, Updated my question – Jan Rüegg Oct 08 '15 at 07:27
  • 1
    What Jan really wants is the exterior/outer product. This exists for any dimension. For 3D it is the dual of the cross product (pseudo vector) and for 2D the dual of a scalar (pseudo scalar). It would be handy to have this in Eigen. The best way would be to return a row vector for column vector arguments and vice versa. (For 2D unfortunately you cannot distinguish pseudo scalars from scalars just by their dimension (1x1)) See Geometric Algebra for a more rigorous notation. – koraxkorakos May 22 '21 at 12:39