Add mjs_bodyToFrame() to convert a body to a frame.

Also add test for attaching the world body.

PiperOrigin-RevId: 685603933
Change-Id: I3cfe7d226d6f032b5a587e44d0f4f18207977188
This commit is contained in:
Alessio Quaglino
2024-10-14 00:38:26 -07:00
committed by Copybara-Service
parent 52a0149cd1
commit 766cd20273
12 changed files with 169 additions and 6 deletions
+11
View File
@@ -666,6 +666,17 @@ const char* mjs_resolveOrientation(double quat[4], mjtByte degree, const char* s
// Transform body into a frame.
mjsFrame* mjs_bodyToFrame(mjsBody** body) {
mjCBody* bodyC = static_cast<mjCBody*>((*body)->element);
mjCFrame* frameC = bodyC->ToFrame();
delete bodyC;
*body = nullptr;
return &frameC->spec;
}
// get id
int mjs_getId(mjsElement* element) {
if (!element) {
+3
View File
@@ -365,6 +365,9 @@ MJAPI void mjs_setFrame(mjsElement* dest, mjsFrame* frame);
MJAPI const char* mjs_resolveOrientation(double quat[4], mjtByte degree, const char* sequence,
const mjsOrientation* orientation);
// Transform body into a frame.
MJAPI mjsFrame* mjs_bodyToFrame(mjsBody** body);
//---------------------------------- Initialization -----------------------------------------------
+33
View File
@@ -1165,6 +1165,39 @@ mjCLight* mjCBody::AddLight(mjCDef* _def) {
// create a frame in the parent body and move all contents of this body into it
mjCFrame* mjCBody::ToFrame() {
if (parentid < 0) {
// TODO: store the parent pointer instead of using the id
throw mjCError(this, "parent body is not defined, please compile the model first");
}
mjCBody* parent = model->Bodies()[parentid];
mjCFrame* newframe = parent->AddFrame(frame);
mjuu_copyvec(newframe->spec.pos, spec.pos, 3);
mjuu_copyvec(newframe->spec.quat, spec.quat, 4);
parent->bodies.insert(parent->bodies.end(), bodies.begin(), bodies.end());
parent->geoms.insert(parent->geoms.end(), geoms.begin(), geoms.end());
parent->joints.insert(parent->joints.end(), joints.begin(), joints.end());
parent->sites.insert(parent->sites.end(), sites.begin(), sites.end());
parent->cameras.insert(parent->cameras.end(), cameras.begin(), cameras.end());
parent->lights.insert(parent->lights.end(), lights.begin(), lights.end());
parent->frames.insert(parent->frames.end(), frames.begin(), frames.end());
bodies.clear();
geoms.clear();
joints.clear();
sites.clear();
cameras.clear();
lights.clear();
frames.clear();
parent->bodies.erase(
std::remove_if(parent->bodies.begin(), parent->bodies.end(),
[this](mjCBody* body) { return body == this; }),
parent->bodies.end());
return newframe;
}
// get number of objects of specified type
int mjCBody::NumObjects(mjtObj type) {
switch (type) {
+3
View File
@@ -331,6 +331,9 @@ class mjCBody : public mjCBody_, private mjsBody {
// reset keyframe references for allowing self-attach
void ForgetKeyframes() const;
// create a frame and move all contents of this body into it
mjCFrame* ToFrame();
// get mocap position and quaternion
mjtNum* mpos(const std::string& state_name);
mjtNum* mquat(const std::string& state_name);