3 #include <SimoxUtility/algorithm/string/string_tools.h>
4 #include <VirtualRobot/Import/MeshImport/AssimpReader.h>
5 #include <VirtualRobot/ManipulationObject.h>
6 #include <VirtualRobot/SceneObject.h>
7 #include <VirtualRobot/Visualization/CoinVisualization/CoinVisualization.h>
8 #include <VirtualRobot/XML/ObjectIO.h>
9 #include <VirtualRobot/XML/RobotIO.h>
17 #include <Inventor/SbColor.h>
18 #include <Inventor/nodes/SoMaterial.h>
26 findObjectInArmarXObjects(
const std::string&
filename)
29 std::string objectName = std::filesystem::path(
filename).filename().stem();
32 std::string fullFilename;
36 if (std::optional<armarx::ObjectInfo> info = objectFinder.
findObject(
"", objectName))
38 fullFilename = info->simoxXML().absolutePath;
39 ss <<
"Found '" << objectName <<
"' in ArmarXObjects as " << *info <<
" \nat '"
40 << fullFilename <<
"'.";
44 ss <<
"Did not find '" << objectName <<
"' in ArmarXObjects.";
46 ss <<
"\n(Lookup took " << (IceUtil::Time::now() - start).toMilliSecondsDouble()
53 VirtualRobot::ManipulationObjectPtr
56 VirtualRobot::ManipulationObjectPtr result;
64 bool checkArmarXObjects =
true;
79 std::string fullFilename;
83 if (checkArmarXObjects)
85 fullFilename = findObjectInArmarXObjects(
filename);
87 if (fullFilename.empty())
97 ARMARX_INFO <<
"Loading object from " << fullFilename;
99 std::filesystem::path path{fullFilename};
100 const std::string ext = simox::alg::to_lower(path.extension());
102 if (ext ==
".wrl" || ext ==
".iv")
104 VirtualRobot::VisualizationFactoryPtr factory =
105 VirtualRobot::VisualizationFactory::fromName(
"inventor",
nullptr);
106 VirtualRobot::VisualizationNodePtr vis =
107 factory->getVisualizationFromFile(fullFilename);
108 result = VirtualRobot::ManipulationObjectPtr(
109 new VirtualRobot::ManipulationObject(
filename, vis));
111 else if (ext ==
".xml" || ext ==
".moxml")
113 result = VirtualRobot::ObjectIO::loadManipulationObject(fullFilename);
115 else if (VirtualRobot::AssimpReader::can_load(fullFilename))
117 const auto& tri = VirtualRobot::AssimpReader{}.readFileAsTriMesh(fullFilename);
118 result = VirtualRobot::ManipulationObjectPtr(
119 new VirtualRobot::ManipulationObject(
filename, tri));
123 ARMARX_WARNING <<
"Could not load object from file: " << fullFilename
124 <<
"\nunknown extension '" << ext <<
"'";
127 catch (std::exception
const& ex)
129 ARMARX_WARNING <<
"Could not load object from file: " << fullFilename
130 <<
"\nReason: " << ex.what();
136 static std::vector<LoadedObject> objectcache;
139 getObjectFromCache(std::string
const&
project, std::string
const&
filename)
145 for (LoadedObject
const& loaded : objectcache)
150 result.object = loaded.object;
160 objectcache.push_back(result);
173 loaded = getObjectFromCache(element.project, element.filename);
178 <<
"Object will not be visualized since it could not be loaded."
179 <<
"\nID: " << element.id <<
"\nProject: " << element.project
180 <<
"\nFilename: " << element.filename;
185 if (fileChanged || drawStyleChanged)
193 int numChildren =
node->getNumChildren();
194 for (
int i = 0; i < numChildren; i++)
196 SoSeparator* nodeSep =
static_cast<SoSeparator*
>(
node->getChild(i));
198 SoMaterial* m =
dynamic_cast<SoMaterial*
>(nodeSep->getChild(0));
205 auto color = element.color;
206 const float conv = 1.0f / 255.0f;
207 float a = color.a * conv;
208 SbColor coinColor(conv * color.r, conv * color.g, conv * color.b);
209 m->diffuseColor = coinColor;
210 m->ambientColor = coinColor;
211 m->transparency = 1.0f -
a;
212 m->setOverride(
true);
222 VirtualRobot::SceneObject::VisualizationType visuType = VirtualRobot::SceneObject::Full;
223 if (drawStyle & data::ModelDrawStyle::COLLISION)
225 visuType = VirtualRobot::SceneObject::Collision;
228 node->removeAllChildren();
230 VirtualRobot::ManipulationObject&
object = *
loaded.
object;
232 SoSeparator* nodeSep =
new SoSeparator;
236 SoMaterial* nodeMat =
new SoMaterial;
237 nodeMat->setOverride(
false);
238 nodeSep->addChild(nodeMat);
241 object.getVisualization<VirtualRobot::CoinVisualization>(visuType);
244 SoNode* sepRobNode = nodeVisu->getCoinVisualization();
248 nodeSep->addChild(sepRobNode);
252 node->addChild(nodeSep);