3 #include <VirtualRobot/SceneObject.h>
4 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
5 #include <VirtualRobot/XML/RobotIO.h>
28 std::string fullFilename;
33 <<
"You specified the absolute path to the robot file:"
35 <<
"\nConsider specifying the containing ArmarX package and relative "
36 "data path instead to "
37 <<
"improve portability to other systems.";
44 <<
"Unable to find readable file for name: " <<
filename;
58 ARMARX_INFO <<
"Loading robot from " << fullFilename;
59 result = VirtualRobot::RobotIO::loadRobot(fullFilename, loadMode);
62 result->setThreadsafe(
false);
63 result->setPropagatingJointValuesEnabled(
true);
67 ARMARX_WARNING <<
"Could not load robot from file: " << fullFilename
68 <<
"\nReason: loadRobot() returned nullptr";
71 catch (std::exception
const& ex)
73 ARMARX_WARNING <<
"Could not load robot from file: " << fullFilename
74 <<
"\nReason: " << ex.what();
80 struct RobotInstancePool
85 std::vector<VirtualRobot::RobotPtr>
robots;
88 static std::vector<RobotInstancePool> robotCache;
91 getRobotFromCache(std::string
const&
project, std::string
const&
filename)
99 for (RobotInstancePool& instancePool : robotCache)
101 if (instancePool.project ==
project && instancePool.filename ==
filename)
104 if (instancePool.usedInstances < instancePool.robots.size())
109 result.robot = instancePool.robots[instancePool.usedInstances];
110 instancePool.usedInstances += 1;
119 if (!instancePool.robots.empty())
122 instancePool.robots.front();
123 float scaling = 1.0f;
124 bool preventCloningMeshesIfScalingIs1 =
true;
130 result.robot = robotToClone->clone(
131 nullptr, scaling, preventCloningMeshesIfScalingIs1);
135 instancePool.robots.push_back(result.robot);
136 instancePool.usedInstances += 1;
140 ARMARX_WARNING <<
"Encountered empty robot instance pool while trying "
141 "to clone new instance"
144 <<
"\nUsed instances: " << instancePool.usedInstances
145 <<
"\nRobots: " << instancePool.robots.size();
157 RobotInstancePool& instancePool = robotCache.emplace_back();
158 instancePool.project =
project;
160 instancePool.robots.push_back(result.robot);
161 instancePool.usedInstances = 1;
174 for (RobotInstancePool& instancePool : robotCache)
180 std::vector<VirtualRobot::RobotPtr>&
robots = instancePool.robots;
182 if (robotIter !=
robots.end())
187 if (instancePool.usedInstances > 0)
189 instancePool.usedInstances -= 1;
193 ARMARX_WARNING <<
"Expected there to be at least one used instance "
194 <<
"while trying to put robot instance back into the pool"
197 <<
"\nUsed instances: " << instancePool.usedInstances;
215 loaded = getRobotFromCache(element.project, element.filename);
220 <<
"Robot will not visualized since it could not be loaded."
221 <<
"\nID: " << element.id <<
"\nProject: " << element.project
222 <<
"\nFilename: " << element.filename;
228 if (robotChanged || drawStyleChanged)
248 for (
const auto& pair : element.jointValues)
250 std::string
const& nodeName = pair.first;
251 float newJointValue = pair.second;
252 VirtualRobot::RobotNodePtr robotNode = robot.getRobotNode(nodeName);
254 if (robotNode ==
nullptr)
259 const float oldJointValue = robotNode->getJointValue();
260 const float diff =
std::abs(newJointValue - oldJointValue);
261 const bool jointValuesChanged = diff > 0.001f;
262 if (jointValuesChanged)
266 robot.setJointValues(element.jointValues);
277 int numChildren =
node->getNumChildren();
278 for (
int i = 0; i < numChildren; i++)
280 SoSeparator* nodeSep =
static_cast<SoSeparator*
>(
node->getChild(i));
282 SoMaterial* m =
dynamic_cast<SoMaterial*
>(nodeSep->getChild(0));
289 auto color = element.color;
290 const float conv = 1.0f / 255.0f;
291 float a = color.a * conv;
292 SbColor coinColor(conv * color.r, conv * color.g, conv * color.b);
293 m->diffuseColor = coinColor;
294 m->ambientColor = coinColor;
295 m->transparency = 1.0f -
a;
296 m->setOverride(
true);
316 VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full;
317 if (drawStyle & data::ModelDrawStyle::COLLISION)
319 visuType = VirtualRobot::SceneObject::Collision;
322 node->removeAllChildren();
325 for (
size_t i = 0; i < robot.getRobotNodes().size(); ++i)
327 VirtualRobot::RobotNodePtr robNode = robot.getRobotNodes()[i];
328 SoSeparator* nodeSep =
new SoSeparator;
332 SoMaterial* nodeMat =
new SoMaterial;
333 nodeMat->setOverride(
false);
334 nodeSep->addChild(nodeMat);
336 auto robNodeVisu = robNode->getVisualization<VirtualRobot::CoinVisualization>(visuType);
339 SoNode* sepRobNode = robNodeVisu->getCoinVisualization();
343 nodeSep->addChild(sepRobNode);
347 node->addChild(nodeSep);