rdiankov / openrave

Open Robotics Automation Virtual Environment: An environment for testing, developing, and deploying robotics motion planning algorithms.
http://www.openrave.org
Other
687 stars 340 forks source link

Make IkParameterization Get/SetValues Template #1312

Closed cielavenir closed 9 months ago

cielavenir commented 10 months ago

With this patch, IkParameterization GetValues/SetValues can accept anything that behaves as iterator, including vector/C-array/array/span.

GraspingCommon extracts std::vector<dReal> from std::vector<unsigned char> by hacking stdc++, which is not universal.


Actually there are some other functions whose arg is vector::iterator, but I have not touch those as template functions have to be inline.

cielavenir commented 10 months ago

AutoTester Pipeline #632835

-----BEGIN PGP MESSAGE-----

hQIMAwAinXiLUbjzAQ/+LdQOzvU2KyCASyZJ8hoov+rXWGU6PS49iFVnotOggHw+
dB+cZ7rK/JmQtoVWh2Hds5smSQ91mtAOLUmoGE9EZe07i1bvw4yxURcIg7fZ4/5M
1RJB+ThchOSfuYUVOwa7fMeCmnU/29PD3Zcd0AybMI9LtjMBFrJi/rdV3nkJAsxm
uJqLTtf7xdoSp9YdfHFVACvQ52Wgk8lOZnHXuVfXEQHpDInDJ64aWAetS0FoUuM5
edQcrNwjojQzrfiP1/KJaFJXjN2Mvyvj40ZS4wTKdUms9FjPmDtypuYhK/yoGE4Q
yXAePCSEAWsyVghK2Ze30DzVrsz7WE5Td7t88CF+CN1AGG3EmIaCUhVjdb/PABxQ
I1JionI2o4K0i6FDbLgCH0kWHNPUNeRdULd4JKrvVvVNE5v8McMKtMJP6zCiPJTB
8n72ewLoJ1sONrpKK+hwy6sHDgaZ+y0HaCa97ebCxjGgvjvdxdXcWPVr0YY/5pb6
LOYktZa5r8e+vdjx994j6CeAKQopKin0JoVIauCXVoFeSkb8IaYqh0hrStwKFZE+
9toEUK3kdYhy/dFFmgqmBcMLDZMj2UFDH/J2u458052KLFH8/T7kYcENJYOdIfOc
qcPVW7jZGtBFtiFu7w4zATFn2w2d6h1oFgp7F3gO262REtDdzkz9WkkJGYaPz1/S
ggHP2x9vnM8r6Yslkeqoy3Ktx3e7ktc6M1PUlTXvBWZV2dxH66hk8qcZkyY4fY95
zY7h6r4Xk30QpWm9zwRAPbGXQaVkPjfLG0GIXWY9MXYi6SsmQNpkC8Cm9elfErTc
8WZxWvRE7ZjcZP5tkVPCDWNiTawV40pGdVKDuWpOI65k2h0=
=oSMQ
-----END PGP MESSAGE-----
cielavenir commented 10 months ago

The feature itself has been tested by the following C++ (with bash bootstrap)

About graspingcommon, unfortunately,

so we would need to stick with raw pointer...

//usr/bin/env true; tmpfile=$(mktemp)
//usr/bin/env true; srcdir=$(dirname $(realpath "$0"))
//usr/bin/env true; if [ ! -f $srcdir/span.hpp ]; then wget -q -O $srcdir/span.hpp https://github.com/boostorg/core/raw/boost-1.78.0/include/boost/core/span.hpp; fi
//usr/bin/env true; g++ -O2 -I $MUJIN_INSTALL_DIR/include -I $MUJIN_INSTALL_DIR/include/openrave-* -L $MUJIN_INSTALL_DIR/lib -xc++ -o $tmpfile $0 -llog4cxx && $tmpfile "$@"; rm $tmpfile; exit

#include <openrave/openrave.h>
#include <iostream>

#include <vector>
#include <array>

// #if __cplusplus >= 202002L
// #include <span>
// using std::span;
// #else
#include "span.hpp"
using boost::span;
// #endif

namespace OpenRAVE {
    // some definition to avoid linking openrave while using headers
    dReal RaveSqrt(dReal f){return sqrt(f);}

    // partly copied from https://github.com/rdiankov/openrave/blob/tw2019_production/include/openrave/openravejson.h
    OpenRAVEException::OpenRAVEException(const std::string& s, OpenRAVEErrorCode error) : std::exception() {
        _error = error;
        _s = "openrave (";
        _s += "): ";
        _s += s;
    }
    char const* OpenRAVEException::what() const throw()   {return _s.c_str();}
    const std::string& OpenRAVEException::message() const {return _s;}
    OpenRAVEErrorCode OpenRAVEException::GetCode() const  {return _error;}

    int RaveGetDebugLevel()            {return Level_Info;}
    log4cxx::LoggerPtr RaveGetLogger() {return log4cxx::Logger::getLogger("openrave");}
}

using OpenRAVE::dReal;
using OpenRAVE::IkParameterization;
using OpenRAVE::Transform;

IkParameterization CopyByVector(const IkParameterization &src){
    IkParameterization dst;
    std::vector<dReal> v(7);
    src.GetValues(v.begin());
    dst.SetValues(v.cbegin(), src.GetType());
    return dst;
}

IkParameterization CopyByPointer(const IkParameterization &src){
    IkParameterization dst;
    dReal *v = new dReal[7];
    src.GetValues(v);
    dst.SetValues(v, src.GetType());
    delete []v;
    return dst;
}

IkParameterization CopyByArray(const IkParameterization &src){
    IkParameterization dst;
    std::array<dReal, 7> v;
    src.GetValues(v.begin());
    dst.SetValues(v.cbegin(), src.GetType());
    return dst;
}

IkParameterization CopyBySpan(const IkParameterization &src){
    IkParameterization dst;
    char buf[8*7];
    span<dReal> v((dReal*)buf, 7);
    src.GetValues(v.begin());
    dst.SetValues(v.begin(), src.GetType()); // cbegin is C++23
    return dst;
}

int main(){
    Transform t;
    t.rot.x = 0;
    t.rot.y = 1;
    t.trans.x = 1;
    t.trans.y = 2;
    t.trans.z = 3;
    IkParameterization src(t);

    {
        IkParameterization dst = CopyByVector(src);
        std::cout << (t == dst.GetTransform6D()) << std::endl;
    }
    {
        IkParameterization dst = CopyByPointer(src);
        std::cout << (t == dst.GetTransform6D()) << std::endl;
    }
    {
        IkParameterization dst = CopyByArray(src);
        std::cout << (t == dst.GetTransform6D()) << std::endl;
    }
    {
        IkParameterization dst = CopyBySpan(src);
        std::cout << (t == dst.GetTransform6D()) << std::endl;
    }
}
cielavenir commented 10 months ago

/cc @ntohge

(could you add him to the member?)

cielavenir commented 9 months ago

Neither robotics team nor testing team gave me clue, so I'm completely lost, but for iterator type, https://github.com/llvm/llvm-project/blob/main/libcxx/include/__algorithm/any_of.h and https://cplusplus.com/reference/algorithm/fill/ uses InputIterator/OutputIterator term, so I changed to that terms.

Please note that I'm still lost about T (value type) term. libcxx uses Tp term. If that two-letter name is unacceptable I will not be able to add new template function to mujin code base.


This is super-complicated problem so I will let product team (@felixvd) subscribe it as well.

cielavenir commented 9 months ago

Pipeline #635575

rdiankov commented 9 months ago

@cielavenir thanks~ yes, the T is not great, we should rename it to something better