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

Vector size dependent behavior

Tags: None
(comma "," separated)
arennuit
Registered Member
Posts
28
Karma
0

Vector size dependent behavior

Mon Nov 21, 2016 6:13 pm
Hi all,

I am implementing method perpendicular(...) which is supposed to compute a vector orthogonal to this. My code is only meaningful with Vector2 and Vector3. Though I do not manage to implement the behaviors for Vector2 and Vector3 in the same method (in MatrixBase_plugin.h).

Code: Select all

PlainObject perpendicular(Scalar epsilon = Scalar(1e-4)) const
{
    // Vector2.
    if (this->size() == 2)
       return Matrix<Scalar, 2, 1>(this->y(), this->x());

    // Vector3.
    if (this->size() == 3)
    {
        Matrix<Scalar, 3, 1> perp = this->cross(Matrix<Scalar, 3, 1>(1.0, 0.0, 0.0));

        // Check whether X is aligned with this, use Y if so.
        if(perp.norm() < epsilon)
        {
            perp = this->cross(Matrix<Scalar, 3, 1>(0.0, 1.0, 0.0));
        }

        perp.normalize();

        return perp;
    }
}



When I do this I get YOU_MIXED_MATRICES_OF_DIFFERENT_SIZES so I currently had to implement it in 2 differently named functions (which is dirty).

Any idea of what I should do?

Thanks,

Antoine.
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Vector size dependent behavior

Mon Nov 21, 2016 9:45 pm
You need a "compile-time if" using meta programming, or simply call vec.unitOrthogonal()! If you really want to re-implement it, have a look at its implementation first: https://bitbucket.org/eigen/eigen/src/1 ... hods.h-137
arennuit
Registered Member
Posts
28
Karma
0

Re: Vector size dependent behavior

Tue Nov 22, 2016 8:04 am
Awesome, thanks!


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]