ObjectFinder.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <filesystem>
4 #include <unordered_map>
5 
6 #include <VirtualRobot/VirtualRobot.h>
7 
9 
11 
12 #include "ObjectInfo.h"
13 #include "ObjectPose.h"
14 
15 
16 namespace armarx
17 {
18  /**
19  * @brief Used to find objects in the ArmarX objects repository [1] (formerly [2]).
20  *
21  * @see [1] https://git.h2t.iar.kit.edu/sw/armarx/prior-knowledge-data
22  */
24  {
25  public:
26 
27  using path = std::filesystem::path;
28  inline static const std::string DefaultObjectsPackageName = "PriorKnowledgeData";
29  inline static const std::string DefaultObjectsDirectory = "objects";
30 
31 
32  public:
33 
34  ObjectFinder(const std::string& objectsPackageName = DefaultObjectsPackageName,
35  const path& relObjectsDir = DefaultObjectsDirectory);
36 
37  ObjectFinder(ObjectFinder&&) = default;
38  ObjectFinder(const ObjectFinder&) = default;
39  ObjectFinder& operator=(ObjectFinder&&) = default;
40  ObjectFinder& operator=(const ObjectFinder&) = default;
41 
42 
43  void setPath(const std::string& path);
44 
45  std::string getPackageName() const;
46 
47  std::optional<ObjectInfo> findObject(const std::string& dataset, const std::string& name) const;
48  std::optional<ObjectInfo> findObject(const std::string& nameOrID) const;
49  std::optional<ObjectInfo> findObject(const ObjectID& id) const;
50  std::optional<ObjectInfo> findObject(const objpose::ObjectPose& obj) const;
51 
52  std::vector<std::string> getDatasets() const;
53  std::vector<path> getDatasetDirectories() const;
54 
55  std::vector<ObjectInfo> findAllObjects(bool checkPaths = true) const;
56  std::map<std::string, std::vector<ObjectInfo>> findAllObjectsByDataset(bool checkPaths = true) const;
57  std::vector<ObjectInfo> findAllObjectsOfDataset(const std::string& dataset, bool checkPaths = true) const;
58 
59  std::vector<armem::articulated_object::ArticulatedObjectDescription> findAllArticulatedObjects(bool checkPaths) const;
60  std::vector<armem::articulated_object::ArticulatedObjectDescription> findAllArticulatedObjectsOfDataset(const std::string& dataset, bool checkPaths) const;
61  std::unordered_map<std::string, std::vector<armem::articulated_object::ArticulatedObjectDescription>> findAllArticulatedObjectsByDataset(bool checkPaths = true) const;
62 
63  VirtualRobot::ManipulationObjectPtr
64  static loadManipulationObject(const std::optional<ObjectInfo>& ts);
65  VirtualRobot::ManipulationObjectPtr
67 
68  VirtualRobot::ObstaclePtr
69  static loadObstacle(const std::optional<ObjectInfo>& ts);
70  VirtualRobot::ObstaclePtr
71  loadObstacle(const objpose::ObjectPose& obj) const;
72 
73 
74  /**
75  * @brief Load names to use when matched when recognizing an object by name.
76  *
77  * If the object's names JSON file does not exist, no names will be added from a file.
78  * If you would like to detect this case, first `findObject()`, then use
79  * `ObjectInfo::loadRecognizedNames()`, which returns a `std::optional`.
80  *
81  * @param includeClassName If true, include the raw class name in the result.
82  * @see `ObjectInfo::loadRecognizedNames()`
83  */
84  std::vector<std::string> loadRecognizedNames(const ObjectID& objectID, bool includeClassName = false) const;
85  /**
86  * @brief Load names to use when verbalizing an object name.
87  *
88  * If the object's names JSON file does not exist, no names will be added from a file.
89  * If you would like to detect this case, first `findObject()`, then use
90  * `ObjectInfo::loadSpokenNames()`, which returns a `std::optional`.
91  *
92  * @param includeClassName If true, include the raw class name in the result.
93  * @see `ObjectInfo::loadSpokenNames()`
94  */
95  std::vector<std::string> loadSpokenNames(const ObjectID& objectID, bool includeClassName = false) const;
96 
97  void setLogObjectDiscoveryError(bool logEnabled);
98 
99 
100  private:
101 
102  void init() const;
103  bool isDatasetDirValid(const std::filesystem::path& path) const;
104 
105  path _rootDirAbs() const;
106  path _rootDirRel() const;
107 
108  bool _ready() const;
109 
110 
111  private:
112 
113  /// Name of package containing the object models (ArmarXObjects by default).
114  mutable std::string packageName;
115 
116  /**
117  * @brief Absolute path to data directory (e.g. "/.../repos/ArmarXObjects/data").
118  * Empty if package could not be found.
119  */
120  mutable path absPackageDataDir;
121 
122  /// Path to the directory containing objects in the package's data directory.
123  path relObjectsDir;
124 
125 
126  // see: ObjectInfo::checkPaths()
127  bool logObjectDiscoveryError = true;
128 
129  };
130 }
armarx::ObjectFinder::findAllArticulatedObjectsOfDataset
std::vector< armem::articulated_object::ArticulatedObjectDescription > findAllArticulatedObjectsOfDataset(const std::string &dataset, bool checkPaths) const
Definition: ObjectFinder.cpp:260
armarx::ObjectID
A known object ID of the form "Dataset/ClassName" or "Dataset/ClassName/InstanceName".
Definition: ObjectID.h:11
armarx::ObjectFinder::DefaultObjectsPackageName
static const std::string DefaultObjectsPackageName
Definition: ObjectFinder.h:28
armarx::ObjectFinder::loadObstacle
static VirtualRobot::ObstaclePtr loadObstacle(const std::optional< ObjectInfo > &ts)
Definition: ObjectFinder.cpp:321
armarx::ObjectFinder::findAllObjects
std::vector< ObjectInfo > findAllObjects(bool checkPaths=true) const
Definition: ObjectFinder.cpp:144
armarx::ObjectFinder::ObjectFinder
ObjectFinder(const std::string &objectsPackageName=DefaultObjectsPackageName, const path &relObjectsDir=DefaultObjectsDirectory)
Definition: ObjectFinder.cpp:20
armarx::ObjectFinder::path
std::filesystem::path path
Definition: ObjectFinder.h:27
armarx::ObjectFinder::loadManipulationObject
static VirtualRobot::ManipulationObjectPtr loadManipulationObject(const std::optional< ObjectInfo > &ts)
Definition: ObjectFinder.cpp:300
armarx::ObjectFinder::setLogObjectDiscoveryError
void setLogObjectDiscoveryError(bool logEnabled)
Definition: ObjectFinder.cpp:379
armarx::ObjectFinder
Used to find objects in the ArmarX objects repository [1] (formerly [2]).
Definition: ObjectFinder.h:23
armarx::ObjectFinder::setPath
void setPath(const std::string &path)
Definition: ObjectFinder.cpp:26
armarx::ObjectFinder::findAllObjectsOfDataset
std::vector< ObjectInfo > findAllObjectsOfDataset(const std::string &dataset, bool checkPaths=true) const
Definition: ObjectFinder.cpp:201
types.h
armarx::ObjectFinder::findAllArticulatedObjectsByDataset
std::unordered_map< std::string, std::vector< armem::articulated_object::ArticulatedObjectDescription > > findAllArticulatedObjectsByDataset(bool checkPaths=true) const
Definition: ObjectFinder.cpp:236
armarx::ObjectFinder::loadSpokenNames
std::vector< std::string > loadSpokenNames(const ObjectID &objectID, bool includeClassName=false) const
Load names to use when verbalizing an object name.
Definition: ObjectFinder.cpp:371
armarx::ObjectFinder::operator=
ObjectFinder & operator=(ObjectFinder &&)=default
ObjectPose.h
ObjectInfo.h
armarx::ObjectFinder::loadRecognizedNames
std::vector< std::string > loadRecognizedNames(const ObjectID &objectID, bool includeClassName=false) const
Load names to use when matched when recognizing an object by name.
Definition: ObjectFinder.cpp:364
armarx::ObjectFinder::findAllObjectsByDataset
std::map< std::string, std::vector< ObjectInfo > > findAllObjectsByDataset(bool checkPaths=true) const
Definition: ObjectFinder.cpp:190
armarx::ObjectFinder::getDatasetDirectories
std::vector< path > getDatasetDirectories() const
Definition: ObjectFinder.cpp:124
armarx::Logging
Base Class for all Logging classes.
Definition: Logging.h:232
armarx::ObjectFinder::findObject
std::optional< ObjectInfo > findObject(const std::string &dataset, const std::string &name) const
Definition: ObjectFinder.cpp:65
armarx::ObjectFinder::findAllArticulatedObjects
std::vector< armem::articulated_object::ArticulatedObjectDescription > findAllArticulatedObjects(bool checkPaths) const
Definition: ObjectFinder.cpp:167
armarx::ObjectFinder::getDatasets
std::vector< std::string > getDatasets() const
Definition: ObjectFinder.cpp:113
Logging.h
armarx::ObjectFinder::getPackageName
std::string getPackageName() const
Definition: ObjectFinder.cpp:32
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::objpose::ObjectPose
An object pose as stored by the ObjectPoseStorage.
Definition: ObjectPose.h:36
armarx::ObjectFinder::DefaultObjectsDirectory
static const std::string DefaultObjectsDirectory
Definition: ObjectFinder.h:29