9 #include <VirtualRobot/SceneObject.h>
10 #include <VirtualRobot/XML/RobotIO.h>
11 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
18 VirtualRobot::RobotPtr loadRobot(std::string
const&
project, std::string
const&
filename)
20 VirtualRobot::RobotPtr result;
28 std::string fullFilename;
33 <<
"You specified the absolute path to the robot file:"
35 <<
"\nConsider specifying the containing ArmarX package and relative data path instead to "
36 <<
"improve portability to other systems.";
43 <<
"Unable to find readable file for name: " <<
filename;
57 ARMARX_INFO <<
"Loading robot from " << fullFilename;
58 result = VirtualRobot::RobotIO::loadRobot(fullFilename, loadMode);
61 result->setThreadsafe(
false);
64 result->setPropagatingJointValuesEnabled(
false);
68 ARMARX_WARNING <<
"Could not load robot from file: " << fullFilename
69 <<
"\nReason: loadRobot() returned nullptr";
72 catch (std::exception
const& ex)
74 ARMARX_WARNING <<
"Could not load robot from file: " << fullFilename
75 <<
"\nReason: " << ex.what();
81 struct RobotInstancePool
86 std::vector<VirtualRobot::RobotPtr>
robots;
89 static std::vector<RobotInstancePool> robotCache;
91 LoadedRobot 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())
108 result.robot = instancePool.robots[instancePool.usedInstances];
109 instancePool.usedInstances += 1;
117 if (instancePool.robots.size() > 0)
119 VirtualRobot::RobotPtr
const& robotToClone = instancePool.robots.front();
120 float scaling = 1.0f;
121 bool preventCloningMeshesIfScalingIs1 =
true;
122 result.robot = robotToClone->clone(
nullptr, scaling, preventCloningMeshesIfScalingIs1);
125 instancePool.robots.push_back(result.robot);
126 instancePool.usedInstances += 1;
130 ARMARX_WARNING <<
"Encountered empty robot instance pool while trying to clone new instance"
132 <<
"\nUsed instances: " << instancePool.usedInstances
133 <<
"\nRobots: " << instancePool.robots.size();
144 RobotInstancePool& instancePool = robotCache.emplace_back();
145 instancePool.project =
project;
147 instancePool.robots.push_back(result.robot);
148 instancePool.usedInstances = 1;
159 for (RobotInstancePool& instancePool : robotCache)
164 std::vector<VirtualRobot::RobotPtr>&
robots = instancePool.robots;
166 if (robotIter !=
robots.end())
170 std::swap(*robotIter,
robots.back());
171 if (instancePool.usedInstances > 0)
173 instancePool.usedInstances -= 1;
177 ARMARX_WARNING <<
"Expected there to be at least one used instance "
178 <<
"while trying to put robot instance back into the pool"
180 <<
"\nUsed instances: " << instancePool.usedInstances;
196 loaded = getRobotFromCache(element.project, element.filename);
201 <<
"Robot will not visualized since it could not be loaded."
202 <<
"\nID: " << element.id
203 <<
"\nProject: " << element.project
204 <<
"\nFilename: " << element.filename;
210 if (robotChanged || drawStyleChanged)
230 for (
const auto& pair : element.jointValues)
232 std::string
const& nodeName = pair.first;
233 float newJointValue = pair.second;
234 VirtualRobot::RobotNodePtr robotNode = robot.getRobotNode(nodeName);
236 if (robotNode ==
nullptr)
241 const float oldJointValue = robotNode->getJointValue();
242 const float diff =
std::abs(newJointValue - oldJointValue);
243 const bool jointValuesChanged = diff > 0.001f;
244 if (jointValuesChanged)
248 robot.setJointValues(element.jointValues);
261 int numChildren =
node->getNumChildren();
262 for (
int i = 0; i < numChildren; i++)
264 SoSeparator* nodeSep =
static_cast<SoSeparator*
>(
node->getChild(i));
266 SoMaterial* m =
dynamic_cast<SoMaterial*
>(nodeSep->getChild(0));
273 auto color = element.color;
274 const float conv = 1.0f / 255.0f;
275 float a = color.a * conv;
276 SbColor coinColor(conv * color.r, conv * color.g, conv * color.b);
277 m->diffuseColor = coinColor;
278 m->ambientColor = coinColor;
279 m->transparency = 1.0f -
a;
280 m->setOverride(
true);
299 VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full;
300 if (drawStyle & data::ModelDrawStyle::COLLISION)
302 visuType = VirtualRobot::SceneObject::Collision;
305 node->removeAllChildren();
308 for (
size_t i = 0; i < robot.getRobotNodes().size(); ++i)
310 VirtualRobot::RobotNodePtr robNode = robot.getRobotNodes()[i];
311 SoSeparator* nodeSep =
new SoSeparator;
315 SoMaterial* nodeMat =
new SoMaterial;
316 nodeMat->setOverride(
false);
317 nodeSep->addChild(nodeMat);
319 auto robNodeVisu = robNode->getVisualization<VirtualRobot::CoinVisualization>(visuType);
322 SoNode* sepRobNode = robNodeVisu->getCoinVisualization();
326 nodeSep->addChild(sepRobNode);
330 node->addChild(nodeSep);