9 #include <VirtualRobot/ManipulationObject.h>
10 #include <VirtualRobot/SceneObject.h>
11 #include <VirtualRobot/XML/RobotIO.h>
12 #include <VirtualRobot/XML/ObjectIO.h>
13 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
14 #include <VirtualRobot/Import/MeshImport/AssimpReader.h>
16 #include <SimoxUtility/algorithm/string/string_tools.h>
23 std::string findObjectInArmarXObjects(
const std::string&
filename)
26 std::string objectName = std::filesystem::path(
filename).filename().stem();
29 std::string fullFilename;
33 if (std::optional<armarx::ObjectInfo> info = objectFinder.
findObject(
"", objectName))
35 fullFilename = info->simoxXML().absolutePath;
36 ss <<
"Found '" << objectName <<
"' in ArmarXObjects as " << *info <<
" \nat '" << fullFilename <<
"'.";
40 ss <<
"Did not find '" << objectName <<
"' in ArmarXObjects.";
42 ss <<
"\n(Lookup took " << (IceUtil::Time::now() - start).toMilliSecondsDouble() <<
" ms.)";
49 VirtualRobot::ManipulationObjectPtr loadObject(std::string
const&
project, std::string
const&
filename)
51 VirtualRobot::ManipulationObjectPtr result;
59 bool checkArmarXObjects =
true;
74 std::string fullFilename;
78 if (checkArmarXObjects)
80 fullFilename = findObjectInArmarXObjects(
filename);
82 if (fullFilename.empty())
85 <<
"Unable to find readable file for name "
93 ARMARX_INFO <<
"Loading object from " << fullFilename;
95 std::filesystem::path path{fullFilename};
96 const std::string ext = simox::alg::to_lower(path.extension());
98 if (ext ==
".wrl" || ext ==
".iv")
100 VirtualRobot::VisualizationFactoryPtr factory = VirtualRobot::VisualizationFactory::fromName(
"inventor",
nullptr);
101 VirtualRobot::VisualizationNodePtr vis = factory->getVisualizationFromFile(fullFilename);
102 result = VirtualRobot::ManipulationObjectPtr(
new VirtualRobot::ManipulationObject(
filename, vis));
104 else if (ext ==
".xml" || ext ==
".moxml")
106 result = VirtualRobot::ObjectIO::loadManipulationObject(fullFilename);
109 else if (VirtualRobot::AssimpReader::can_load(fullFilename))
111 const auto& tri = VirtualRobot::AssimpReader{}.readFileAsTriMesh(fullFilename);
112 result = VirtualRobot::ManipulationObjectPtr(
new VirtualRobot::ManipulationObject(
filename, tri));
116 ARMARX_WARNING <<
"Could not load object from file: " << fullFilename
117 <<
"\nunknown extension '" << ext <<
"'";
120 catch (std::exception
const& ex)
122 ARMARX_WARNING <<
"Could not load object from file: " << fullFilename
123 <<
"\nReason: " << ex.what();
129 static std::vector<LoadedObject> objectcache;
131 LoadedObject getObjectFromCache(std::string
const&
project, std::string
const&
filename)
137 for (LoadedObject
const& loaded : objectcache)
142 result.object = loaded.object;
152 objectcache.push_back(result);
164 loaded = getObjectFromCache(element.project, element.filename);
169 <<
"Object will not be visualized since it could not be loaded."
170 <<
"\nID: " << element.id
171 <<
"\nProject: " << element.project
172 <<
"\nFilename: " << element.filename;
177 if (fileChanged || drawStyleChanged)
185 int numChildren =
node->getNumChildren();
186 for (
int i = 0; i < numChildren; i++)
188 SoSeparator* nodeSep =
static_cast<SoSeparator*
>(
node->getChild(i));
190 SoMaterial* m =
dynamic_cast<SoMaterial*
>(nodeSep->getChild(0));
197 auto color = element.color;
198 const float conv = 1.0f / 255.0f;
199 float a = color.a * conv;
200 SbColor coinColor(conv * color.r, conv * color.g, conv * color.b);
201 m->diffuseColor = coinColor;
202 m->ambientColor = coinColor;
203 m->transparency = 1.0f -
a;
204 m->setOverride(
true);
213 VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full;
214 if (drawStyle & data::ModelDrawStyle::COLLISION)
216 visuType = VirtualRobot::SceneObject::Collision;
219 node->removeAllChildren();
221 VirtualRobot::ManipulationObject&
object = *
loaded.
object;
223 SoSeparator* nodeSep =
new SoSeparator;
227 SoMaterial* nodeMat =
new SoMaterial;
228 nodeMat->setOverride(
false);
229 nodeSep->addChild(nodeMat);
234 SoNode* sepRobNode = nodeVisu->getCoinVisualization();
238 nodeSep->addChild(sepRobNode);
242 node->addChild(nodeSep);