3 #include <VirtualRobot/SceneObject.h>
4 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
5 #include <VirtualRobot/XML/RobotIO.h>
13 #include <Inventor/SbColor.h>
14 #include <Inventor/nodes/SoMaterial.h>
31 std::string fullFilename;
36 <<
"You specified the absolute path to the robot file:"
38 <<
"\nConsider specifying the containing ArmarX package and relative "
39 "data path instead to "
40 <<
"improve portability to other systems.";
47 <<
"Unable to find readable file for name: " <<
filename;
61 ARMARX_INFO <<
"Loading robot from " << fullFilename;
62 result = VirtualRobot::RobotIO::loadRobot(fullFilename, loadMode);
65 result->setThreadsafe(
false);
66 result->setPropagatingJointValuesEnabled(
true);
70 ARMARX_WARNING <<
"Could not load robot from file: " << fullFilename
71 <<
"\nReason: loadRobot() returned nullptr";
74 catch (std::exception
const& ex)
76 ARMARX_WARNING <<
"Could not load robot from file: " << fullFilename
77 <<
"\nReason: " << ex.what();
83 struct RobotInstancePool
88 std::vector<VirtualRobot::RobotPtr>
robots;
91 static std::vector<RobotInstancePool> robotCache;
94 getRobotFromCache(std::string
const&
project, std::string
const&
filename)
102 for (RobotInstancePool& instancePool : robotCache)
104 if (instancePool.project ==
project && instancePool.filename ==
filename)
107 if (instancePool.usedInstances < instancePool.robots.size())
112 result.robot = instancePool.robots[instancePool.usedInstances];
113 instancePool.usedInstances += 1;
122 if (!instancePool.robots.empty())
125 instancePool.robots.front();
126 float scaling = 1.0f;
127 bool preventCloningMeshesIfScalingIs1 =
true;
135 result.robot = robotToClone->clone(
136 nullptr, scaling, preventCloningMeshesIfScalingIs1);
140 instancePool.robots.push_back(result.robot);
141 instancePool.usedInstances += 1;
145 ARMARX_WARNING <<
"Encountered empty robot instance pool while trying "
146 "to clone new instance"
149 <<
"\nUsed instances: " << instancePool.usedInstances
150 <<
"\nRobots: " << instancePool.robots.size();
162 RobotInstancePool& instancePool = robotCache.emplace_back();
163 instancePool.project =
project;
165 instancePool.robots.push_back(result.robot);
166 instancePool.usedInstances = 1;
179 for (RobotInstancePool& instancePool : robotCache)
185 std::vector<VirtualRobot::RobotPtr>&
robots = instancePool.robots;
187 if (robotIter !=
robots.end())
192 if (instancePool.usedInstances > 0)
194 instancePool.usedInstances -= 1;
198 ARMARX_WARNING <<
"Expected there to be at least one used instance "
199 <<
"while trying to put robot instance back into the pool"
202 <<
"\nUsed instances: " << instancePool.usedInstances;
220 loaded = getRobotFromCache(element.project, element.filename);
225 <<
"Robot will not visualized since it could not be loaded."
226 <<
"\nID: " << element.id <<
"\nProject: " << element.project
227 <<
"\nFilename: " << element.filename;
233 if (robotChanged || drawStyleChanged)
253 for (
const auto& pair : element.jointValues)
255 std::string
const& nodeName = pair.first;
256 float newJointValue = pair.second;
257 VirtualRobot::RobotNodePtr robotNode = robot.getRobotNode(nodeName);
259 if (robotNode ==
nullptr)
264 const float oldJointValue = robotNode->getJointValue();
265 const float diff =
std::abs(newJointValue - oldJointValue);
266 const bool jointValuesChanged = diff > 0.001f;
267 if (jointValuesChanged)
271 robot.setJointValues(element.jointValues);
282 int numChildren =
node->getNumChildren();
283 for (
int i = 0; i < numChildren; i++)
285 SoSeparator* nodeSep =
static_cast<SoSeparator*
>(
node->getChild(i));
287 SoMaterial* m =
dynamic_cast<SoMaterial*
>(nodeSep->getChild(0));
294 auto color = element.color;
295 const float conv = 1.0f / 255.0f;
296 float a = color.a * conv;
297 SbColor coinColor(conv * color.r, conv * color.g, conv * color.b);
298 m->diffuseColor = coinColor;
299 m->ambientColor = coinColor;
300 m->transparency = 1.0f -
a;
301 m->setOverride(
true);
321 VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full;
322 if (drawStyle & data::ModelDrawStyle::COLLISION)
324 visuType = VirtualRobot::SceneObject::Collision;
327 node->removeAllChildren();
330 for (
size_t i = 0; i < robot.getRobotNodes().size(); ++i)
332 VirtualRobot::RobotNodePtr robNode = robot.getRobotNodes()[i];
333 SoSeparator* nodeSep =
new SoSeparator;
337 SoMaterial* nodeMat =
new SoMaterial;
338 nodeMat->setOverride(
false);
339 nodeSep->addChild(nodeMat);
341 auto robNodeVisu = robNode->getVisualization<VirtualRobot::CoinVisualization>(visuType);
344 SoNode* sepRobNode = robNodeVisu->getCoinVisualization();
348 nodeSep->addChild(sepRobNode);
352 node->addChild(nodeSep);