3 #include <VirtualRobot/math/Helpers.h>
4 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
5 #include <VirtualRobot/Visualization/TriMeshModel.h>
6 #include <VirtualRobot/Visualization/VisualizationNode.h>
7 #include <VirtualRobot/XML/mujoco/Mesh.h>
8 #include <VirtualRobot/XML/mujoco/MeshConverter.h>
21 const std::string& loggingTag) :
23 scaling(lengthScaling)
30 const std::string& relativeFrom)
32 std::string absolute = path;
35 std::filesystem::path absol = absolute;
36 absol.relative_path();
38 if (std::filesystem::exists(absolute))
41 document.addInclude(relative);
46 <<
"\nProceeding without including the base MJCF.";
53 mjcf::DefaultClass objectClass = document.default_().getClass(className);
55 mjcf::Geom geom = objectClass.addChild<mjcf::Geom>();
56 geom.rgba = Eigen::Vector4f::Ones();
58 mjcf::Joint joint = objectClass.addChild<mjcf::Joint>();
59 joint.frictionloss = 1;
61 mjcf::Mesh mesh = objectClass.addChild<mjcf::Mesh>();
62 mesh.scale = scaling.
milliToMeter(Eigen::Vector3f::Ones().eval());
67 document.option().timestep = timestep;
71 const std::string& name,
float size)
75 ARMARX_INFO <<
"Adding floor (texture: " << texture <<
")";
78 mjcf::DefaultClass defaults = document.default_().getClass(name);
79 mjcf::Geom defaultGeom = defaults.addChild<mjcf::Geom>();
80 defaultGeom.type =
"plane";
81 defaultGeom.size = Eigen::Vector3f(size, size, 5);
82 defaultGeom.material = name;
85 bool addDefaultTexture =
true;
89 std::string absolute = texture;
92 if (std::filesystem::exists(absolute))
94 document.asset().addTextureFile(name, absolute);
95 addDefaultTexture =
false;
100 <<
"\nProceeding with generated floor texture";
103 if (addDefaultTexture)
105 mjcf::Texture texture = document.asset().addChild<mjcf::Texture>();
108 texture.builtin =
"checker";
110 texture.height = 512;
111 texture.rgb1 = { .35f, .375f, .4f };
112 texture.rgb2 = { .10f, .150f, .2f };
116 mjcf::Material material = document.asset().addMaterial(name, name);
117 material.reflectance = 0.3f;
118 material.texrepeat = { 1, 1 };
119 material.texuniform =
true;
122 mjcf::Body body = document.worldbody().addBody(name);
123 body.childclass = name;
125 mjcf::Geom geom = body.addChild<mjcf::Geom>();
127 geom.pos = Eigen::Vector3f::Zero();
132 VirtualRobot::SceneObject::Physics::SimulationType simType,
133 const std::string& className,
134 const std::filesystem::path& meshDir)
136 const std::string objectName =
object->getName();
138 if (object->getParent())
140 ARMARX_WARNING <<
"Object has parent, which is currently not implemented.";
143 std::string meshName;
145 if (
false && object->getVisualization())
148 ARMARX_INFO <<
"Visu filename: " <<
object->getVisualization()->getFilename();
149 ARMARX_INFO <<
"Num faces: " <<
object->getVisualization()->getNumFaces();
150 ARMARX_INFO <<
"Has tri mesh?: " <<
object->getVisualization()->getTriMeshModel().operator bool();
151 ARMARX_INFO <<
"Primitives: " <<
object->getVisualization()->primitives.size();
154 if (object->getCollisionModel())
156 VirtualRobot::CollisionModelPtr collisionModel =
object->getCollisionModel();
158 const std::string colModelName = collisionModel->getName();
160 const std::string
filename = colModelName +
".msh";
161 const std::filesystem::path meshFilePath = meshDir /
filename;
166 std::filesystem::create_directory(meshDir);
168 catch (
const std::filesystem::filesystem_error& e)
170 ARMARX_ERROR <<
"Could not create temporary mesh dir " << meshDir <<
"."
171 <<
"\nReason: " << e.what();
174 if (std::filesystem::is_regular_file(meshFilePath))
176 ARMARX_VERBOSE <<
"Collision model " << colModelName <<
" already converted.";
180 VirtualRobot::TriMeshModelPtr meshModel = collisionModel->getTriMeshModel();
184 <<
" (" << meshModel->faces.size() <<
" faces) ...";
186 VirtualRobot::mujoco::Mesh mjMesh = VirtualRobot::mujoco::MeshConverter::fromVirtualRobot(*meshModel);
187 mjMesh.write(meshFilePath.string());
191 meshName = colModelName;
194 if (!document.asset().hasChild<mjcf::Mesh>(
"name", meshName))
196 mjcf::Mesh mesh = document.asset().addMesh(meshName, meshFilePath.string());
197 mesh.class_ = className;
201 mjcf::Body body = document.worldbody().addBody(objectName);
202 body.childclass = className;
206 mjcf::Geom geom = body.addGeomMesh(meshName);
207 geom.name = objectName;
209 if (object->getFriction() > 0)
211 Eigen::Vector3f friction = geom.friction;
212 friction(0) =
object->getFriction();
213 geom.friction = friction;
216 mjcf::Inertial inertial = body.addInertial();
217 inertial.mass =
object->getMass();
218 inertial.pos = scaling.
milliToMeter(object->getCoMLocal());
219 inertial.inertiaFromMatrix(object->getInertiaMatrix());
223 case VirtualRobot::SceneObject::Physics::SimulationType::eKinematic:
228 case VirtualRobot::SceneObject::Physics::SimulationType::eDynamic:
231 mjcf::Joint joint = body.addJoint();
232 joint.name = objectName;
237 case VirtualRobot::SceneObject::Physics::SimulationType::eStatic:
238 case VirtualRobot::SceneObject::Physics::SimulationType::eUnknown: