Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I use the casadi::MX type in a logical conditions in an if-else statement? #3692

Open
sm3304love opened this issue May 9, 2024 · 0 comments

Comments

@sm3304love
Copy link

hello. I want to apply different algorithms depending on the value of data of type casadi::MX.

My code is below.

casadi::MX distanceBetweenLSS(const LSS &lss1, const LSS &lss2)
    {
        using namespace casadi;
        MX dir1 = lss1.direction();
        MX dir2 = lss2.direction();

        MX crossProduct = MX::cross(dir1, dir2);

        MX normCrossProduct = MX::norm_2(crossProduct);

        if (normCrossProduct < MX(1e-8))
        {

            MX ab = lss2.startPoint - lss1.startPoint;
            MX proj = mtimes(dir1.T(), ab) * dir1 / MX::dot(dir1, dir1);

            MX distance = MX::norm_2(ab - proj);
            return distance;
        }
        else
        {
            MX ab = lss2.startPoint - lss1.startPoint;
            MX ab_T = ab.T();

            MX numerator = mtimes(ab_T, crossProduct);

            MX distance = (MX::abs(numerator) / normCrossProduct) - (lss1.radius + lss2.radius);

            return distance;
        }
    }

where if (normCrossProduct < MX(1e-8)) the following error occurs in this part:

 error: invalid user-defined conversion from ‘casadi::MX’ to ‘bool’ [-fpermissive]
   49 |         if (normCrossProduct < MX(1e-8))
      |             ~~~~~~~~~~~~~~~~~^~~~~~~~~~
In file included from /usr/local/include/casadi/core/core.hpp:44,
                 from /usr/local/include/casadi/casadi.hpp:29,
                 from /home/harco/roboe_ws/src/casADi_motion_planner/include/nmpc_motion_planner/collisions.hpp:2,
                 from /home/harco/roboe_ws/src/casADi_motion_planner/src/nmpc_planner.cpp:6:
/usr/local/include/casadi/core/mx.hpp:257:14: note: candidate is: ‘casadi::MX::operator double() const’ (near match)
  257 |     explicit operator double() const;
      |              ^~~~~~~~
/usr/local/include/casadi/core/mx.hpp:257:14: note:   return type ‘double’ of explicit conversion function cannot be converted to ‘bool’ with a qualification conversion

Can't I use casadi::MX type as logical conditions in a traditional if-else statement? If so, how should I solve my problem?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant