1 #include <VirtualRobot/XML/ObjectIO.h>
5 #include <SimoxUtility/algorithm/string.h>
6 #include <SimoxUtility/filesystem/list_directory.h>
17 namespace fs = std::filesystem;
21 packageName(objectsPackageName), relObjectsDir(relObjectsDir)
29 absPackageDataDir.clear();
37 void ObjectFinder::init()
const
39 if (absPackageDataDir.empty())
42 absPackageDataDir = packageFinder.getDataDir();
43 if (absPackageDataDir.empty())
45 ARMARX_WARNING <<
"Could not find package '" << packageName <<
"'.";
59 bool ObjectFinder::isDatasetDirValid(
const path& path)
const
61 return std::filesystem::is_directory(
path);
74 return ObjectInfo(packageName, absPackageDataDir, relObjectsDir, dataset, name);
77 const std::vector<std::string>& datasets =
getDatasets();
78 for (
const std::string& ds : datasets)
80 if (fs::is_directory(_rootDirAbs() / ds / name))
82 return ObjectInfo(packageName, absPackageDataDir, relObjectsDir, ds, name);
87 ss <<
"Did not find object '" << name <<
"' in any of these datasets:\n";
88 for (
const auto& ds : datasets)
90 ss <<
"- " << ds <<
"\n";
92 ss <<
"Objects root directory: " << _rootDirAbs();
105 return findObject(
id.dataset(),
id.className());
116 std::vector<std::string> datasets;
119 datasets.push_back(dir.filename());
131 const bool local =
false;
132 std::vector<path> dirs = simox::fs::list_directory(_rootDirAbs(), local);
133 std::vector<path> datasetDirs;
134 for (
const path& p : dirs)
136 if (isDatasetDirValid(p))
138 datasetDirs.push_back(p);
151 const bool local =
true;
152 std::vector<ObjectInfo> objects;
153 for (
const path& datasetDir : simox::fs::list_directory(_rootDirAbs(), local))
155 if (isDatasetDirValid(_rootDirAbs() / datasetDir))
158 for (
const auto& o : dataset)
160 objects.push_back(o);
175 const bool local =
true;
177 std::vector<armem::articulated_object::ArticulatedObjectDescription> objects;
178 for (
const path& datasetDir : simox::fs::list_directory(_rootDirAbs(), local))
180 if (isDatasetDirValid(_rootDirAbs() / datasetDir))
183 objects.insert(objects.end(), dataset.begin(), dataset.end());
189 std::map<std::string, std::vector<ObjectInfo>>
193 std::map<std::string, std::vector<ObjectInfo>> objects;
208 path datasetDir = _rootDirAbs() / dataset;
209 if (!fs::is_directory(datasetDir))
211 ARMARX_WARNING <<
"Expected dataset directory for dataset '" << dataset <<
"': \n"
216 std::vector<ObjectInfo> objects;
217 const bool local =
true;
218 for (
const path& dir : simox::fs::list_directory(datasetDir, local))
220 if (fs::is_directory(datasetDir / dir))
222 ObjectInfo object(packageName, absPackageDataDir, relObjectsDir, dataset, dir.filename());
223 object.setLogError(logObjectDiscoveryError);
225 if (!checkPaths ||
object.checkPaths())
227 objects.push_back(
object);
235 std::unordered_map<std::string, std::vector<armem::articulated_object::ArticulatedObjectDescription>>
244 const bool local =
true;
246 std::unordered_map<std::string, std::vector<armem::articulated_object::ArticulatedObjectDescription>> datasets;
247 for (
const path& datasetDir : simox::fs::list_directory(_rootDirAbs(), local))
249 if (isDatasetDirValid(_rootDirAbs() / datasetDir))
252 datasets[datasetDir] = dataset;
259 std::vector<armem::articulated_object::ArticulatedObjectDescription>
267 path datasetDir = _rootDirAbs() / dataset;
268 if (!isDatasetDirValid(datasetDir))
270 ARMARX_WARNING <<
"Expected dataset directory for dataset '" << dataset <<
"': \n"
275 std::vector<armem::articulated_object::ArticulatedObjectDescription> objects;
276 const bool local =
true;
277 for (
const path& dir : simox::fs::list_directory(datasetDir, local))
279 if (fs::is_directory(datasetDir / dir))
281 ObjectInfo object(packageName, absPackageDataDir, relObjectsDir, dataset, dir.filename());
282 std::optional<PackageFileLocation> modelFile =
object.getArticulatedModel();
283 if (modelFile.has_value())
287 .
name =
object.idStr(),
288 .xml = {modelFile->package, modelFile->relativePath},
299 VirtualRobot::ManipulationObjectPtr
306 const auto data = ts->simoxXML();
313 return VirtualRobot::ObjectIO::loadManipulationObject(
abs);
315 VirtualRobot::ManipulationObjectPtr
320 VirtualRobot::ObstaclePtr
327 const auto data = ts->simoxXML();
334 return VirtualRobot::ObjectIO::loadObstacle(
abs);
336 VirtualRobot::ObstaclePtr
343 static std::vector<std::string> _loadNames(
346 const bool includeClassName,
347 const std::function<std::optional<std::vector<std::string>>(
const ObjectInfo&)> loadNamesFn)
349 std::vector<std::string>
names;
350 if (includeClassName)
354 if (std::optional<ObjectInfo> info = finder.
findObject(objectID))
356 if (std::optional<std::vector<std::string>> loadedNames = loadNamesFn(*info))
359 names.insert(
names.end(), loadedNames->begin(), loadedNames->end());
366 return _loadNames(*
this, objectID, includeClassName, [](
const ObjectInfo & info)
373 return _loadNames(*
this, objectID, includeClassName, [](
const ObjectInfo & info)
381 logObjectDiscoveryError = logEnabled;
386 return absPackageDataDir / packageName / relObjectsDir;
394 bool ObjectFinder::_ready()
const
396 return !absPackageDataDir.empty();