3 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
4 #include <VirtualRobot/Visualization/TriMeshModel.h>
5 #include <VirtualRobot/Visualization/VisualizationNode.h>
6 #include <VirtualRobot/XML/mujoco/Mesh.h>
7 #include <VirtualRobot/XML/mujoco/MeshConverter.h>
8 #include <VirtualRobot/math/Helpers.h>
19 const std::string& loggingTag) :
20 document(document), scaling(lengthScaling)
28 std::string absolute = path;
31 std::filesystem::path absol = absolute;
32 absol.relative_path();
34 if (std::filesystem::exists(absolute))
37 document.addInclude(relative);
42 <<
"\nProceeding without including the base MJCF.";
49 mjcf::DefaultClass objectClass = document.default_().getClass(className);
51 mjcf::Geom geom = objectClass.addChild<mjcf::Geom>();
52 geom.rgba = Eigen::Vector4f::Ones();
54 mjcf::Joint joint = objectClass.addChild<mjcf::Joint>();
55 joint.frictionloss = 1;
57 mjcf::Mesh mesh = objectClass.addChild<mjcf::Mesh>();
58 mesh.scale = scaling.
milliToMeter(Eigen::Vector3f::Ones().eval());
64 document.option().timestep = timestep;
72 ARMARX_INFO <<
"Adding floor (texture: " << texture <<
")";
75 mjcf::DefaultClass defaults = document.default_().getClass(name);
76 mjcf::Geom defaultGeom = defaults.addChild<mjcf::Geom>();
77 defaultGeom.type =
"plane";
78 defaultGeom.size = Eigen::Vector3f(size, size, 5);
79 defaultGeom.material = name;
82 bool addDefaultTexture =
true;
86 std::string absolute = texture;
89 if (std::filesystem::exists(absolute))
91 document.asset().addTextureFile(name, absolute);
92 addDefaultTexture =
false;
97 <<
"\nProceeding with generated floor texture";
100 if (addDefaultTexture)
102 mjcf::Texture texture = document.asset().addChild<mjcf::Texture>();
105 texture.builtin =
"checker";
107 texture.height = 512;
108 texture.rgb1 = {.35f, .375f, .4f};
109 texture.rgb2 = {.10f, .150f, .2f};
113 mjcf::Material material = document.asset().addMaterial(name, name);
114 material.reflectance = 0.3f;
115 material.texrepeat = {1, 1};
116 material.texuniform =
true;
119 mjcf::Body body = document.worldbody().addBody(name);
120 body.childclass = name;
122 mjcf::Geom geom = body.addChild<mjcf::Geom>();
124 geom.pos = Eigen::Vector3f::Zero();
130 VirtualRobot::SceneObject::Physics::SimulationType simType,
131 const std::string& className,
132 const std::filesystem::path& meshDir)
134 const std::string objectName =
object->getName();
136 if (object->getParent())
138 ARMARX_WARNING <<
"Object has parent, which is currently not implemented.";
141 std::string meshName;
143 if (
false && object->getVisualization())
146 ARMARX_INFO <<
"Visu filename: " <<
object->getVisualization()->getFilename();
147 ARMARX_INFO <<
"Num faces: " <<
object->getVisualization()->getNumFaces();
149 <<
object->getVisualization()->getTriMeshModel().operator bool();
150 ARMARX_INFO <<
"Primitives: " <<
object->getVisualization()->primitives.size();
153 if (object->getCollisionModel())
155 VirtualRobot::CollisionModelPtr collisionModel =
object->getCollisionModel();
157 const std::string colModelName = collisionModel->getName();
159 const std::string
filename = colModelName +
".msh";
160 const std::filesystem::path meshFilePath = meshDir /
filename;
165 std::filesystem::create_directory(meshDir);
167 catch (
const std::filesystem::filesystem_error& e)
169 ARMARX_ERROR <<
"Could not create temporary mesh dir " << meshDir <<
"."
170 <<
"\nReason: " << e.what();
173 if (std::filesystem::is_regular_file(meshFilePath))
175 ARMARX_VERBOSE <<
"Collision model " << colModelName <<
" already converted.";
179 VirtualRobot::TriMeshModelPtr meshModel = collisionModel->getTriMeshModel();
182 ARMARX_VERBOSE <<
"Converting collision model " << colModelName <<
" ("
183 << meshModel->faces.size() <<
" faces) ...";
185 VirtualRobot::mujoco::Mesh mjMesh =
186 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: