Giter VIP home page Giter VIP logo

Comments (3)

rpapallas avatar rpapallas commented on June 7, 2024

Sorry, it was an error on my part, I managed to figure this out.

Still curious to know why there is flexvert_xpos when you can get the flex vertex xpos from xpos through flex_vertbodyid.

from mujoco.

dblanm avatar dblanm commented on June 7, 2024

Sorry, it was an error on my part, I managed to figure this out.

Still curious to know why there is flexvert_xpos when you can get the flex vertex xpos from xpos through flex_vertbodyid.

Hey! Would you mind sharing how you solved this?

from mujoco.

rpapallas avatar rpapallas commented on June 7, 2024

@dblanm
It appears that my assumption is correct: the flex_vertbodyid maps a flex vertex to a mujoco body.

Here is a demonstration code:

Getting the flex id from name:

int getFlexId(std::string flexName) {
    return mj_name2id(m, mjOBJ_FLEX, flexName.c_str());
}

Getting and setting all positions of the flex vertices:

std::vector<std::tuple<float, float, float>> getFlexPositions(std::string flexName) {
    std::vector<std::tuple<float, float, float>> positions;

    int flexId = getFlexId(flexName);
    int firstVertadr = m->flex_vertadr[flexId];
    int numVertices = m->flex_vertnum[flexId];

    for(int i=0; i < numVertices; i++) {
        int bodyId = m->flex_vertbodyid[firstVertadr + i];
        float x = d->xpos[(bodyId * 3) + 0];
        float y = d->xpos[(bodyId * 3) + 1];
        float z = d->xpos[(bodyId * 3) + 2];

        /* // or the same with:
        x = d->flexvert_xpos[firstVertadr + i * 3 + 0];
        y = d->flexvert_xpos[firstVertadr + i * 3 + 1];
        z = d->flexvert_xpos[firstVertadr + i * 3 + 2];
        */
        positions.push_back(std::make_tuple(x, y, z));
    }

    return positions;
}

void setFlexPositions(std::string flexName, std::vector<std::tuple<float, float, float>> positions) {
    int flexId = getFlexId(flexName);
    int firstVertadr = m->flex_vertadr[flexId];
    int numVertices = m->flex_vertnum[flexId];

    for(int i=0; i < numVertices; i++) {
        int bodyId = m->flex_vertbodyid[firstVertadr + i];
        const int jointIndex = m->body_jntadr[bodyId];
        const int qposIndex = m->jnt_qposadr[jointIndex];
        float x = std::get<0>(positions[i]);
        float y = std::get<1>(positions[i]);
        float z = std::get<2>(positions[i]);
        d->qpos[qposIndex + 0] = x;
        d->qpos[qposIndex + 1] = y;
        d->qpos[qposIndex + 2] = z;
    }
}

Getting and setting the flex vertex velocities:

std::vector<std::tuple<float, float, float>> getFlexVelocities(std::string flexName) {
    std::vector<std::tuple<float, float, float>> velocities;

    int flexId = getFlexId(flexName);
    int firstVertadr = m->flex_vertadr[flexId];
    int numVertices = m->flex_vertnum[flexId];

    for(int i=0; i < numVertices; i++) {
        int bodyId = m->flex_vertbodyid[firstVertadr + i];
        const int jointIndex = m->body_jntadr[bodyId];
        const int start = m->jnt_dofadr[jointIndex];
        double linearX = d->qvel[start + 0];
        double linearY = d->qvel[start + 1];
        double linearZ = d->qvel[start + 2];
        velocities.push_back(std::make_tuple(linearX, linearY, linearZ));
    }

    return velocities;
}

void setFlexVelocities(std::string flexName, std::vector<std::tuple<float, float, float>> velocities) {
    int flexId = getFlexId(flexName);
    int firstVertadr = m->flex_vertadr[flexId];
    int numVertices = m->flex_vertnum[flexId];

    for(int i=0; i < numVertices; i++) {
        int bodyId = m->flex_vertbodyid[firstVertadr + i];
        const int jointIndex = m->body_jntadr[bodyId];
        const int start = m->jnt_dofadr[jointIndex];
        float x = std::get<0>(velocities[i]);
        float y = std::get<1>(velocities[i]);
        float z = std::get<2>(velocities[i]);
        d->qvel[start + 0] = x;
        d->qvel[start + 1] = y;
        d->qvel[start + 2] = z;
    }
}

from mujoco.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.