ClutteredSceneGenerator.h
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package ArmarXSimulation::ArmarXObjects::ClutteredSceneGenerator
17  * @author Patrick Hegemann ( patrick dot hegemann at kit dot edu )
18  * @date 2021
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 #pragma once
24 
25 #include <ArmarXSimulation/interface/simulator/SimulatorInterface.h>
27 
29 
30 #include <VirtualRobot/VirtualRobot.h>
31 
32 #include <boost/random/mersenne_twister.hpp>
33 
34 #include <memory>
35 
37 {
38  /**
39  * @defgroup Library-ClutteredSceneGenerator ClutteredSceneGenerator
40  * @ingroup ArmarXSimulation
41  * A description of the library ClutteredSceneGenerator.
42  *
43  * @class ClutteredSceneGenerator
44  * @ingroup Library-ClutteredSceneGenerator
45  * @brief Brief description of class ClutteredSceneGenerator.
46  *
47  * Detailed description of class ClutteredSceneGenerator.
48  */
50  {
51  public:
52  struct Config
53  {
54  // Object sets
55  std::vector<ObjectSet> objectSets;
56 
57  /** Number of objects to generate in a scene */
58  uint amountObjects = 6;
59 
60  /** Number of simulation steps executed for letting objects fall onto the table */
61  uint fallingSteps = 100;
62 
63  /** Whether to automatically reset invalid objects after scene generation */
65  /** Maximum number of retries when resetting objects */
66  uint maxResetTries = 1;
67 
68  // Table position and dimensions
69  float boxX = 0;
70  float boxY = 0;
71  float boxZ = 0;
72  float boxW = 1500;
73  float boxH = 750;
74  float boxD = 1000; // approximately
75 
76  // Minimum distance between object pivot and table edge
77  float objectMarginSide = 200;
78  // Initial vertical distance between objects
79  float objectSpacingZ = 100;
80 
81  // Bounding box for creating objects
82  float minObjectX = -550; // BOX_X - BOX_W / 2 + MARGIN
83  float minObjectY = -175; // BOX_Y - BOX_H / 2 + MARGIN
84  float maxObjectX = 550; // BOX_X + BOX_W / 2 - MARGIN
85  float maxObjectY = 175; // BOX_Y + BOX_H / 2 - MARGIN
86  float minObjectZ = 150;
87 
88  // If an object has a Z position less than this it will be reset
90  };
91 
92  ClutteredSceneGenerator(const armarx::SimulatorInterfacePrx& simulator,
93  const Config& config);
94 
95  void generateScene(int seed);
96 
97  [[nodiscard]] const Config &getConfig() const;
98  void setConfig(const Config &conf);
99 
100  [[nodiscard]] const SimulatorInterfacePrx &getSimulator() const;
101  void setSimulator(const SimulatorInterfacePrx &simulator);
102 
103  private:
104  void createCollisionWalls();
105 
106  /**
107  * @brief randomObjectPose
108  * Generates a random pose for an object.
109  * @param heightIndex
110  * How far above the box the pose should be (z = MIN_OBJECT_Z + heightIndex * OBJECT_SPACING_Z)
111  * @return
112  * a random pose.
113  */
114  armarx::PosePtr randomObjectPose(uint heightIndex);
115 
116  /**
117  * @brief deleteLocalObjectCopies
118  * Deletes all local copies of the objects in the scene
119  */
120  void deleteLocalObjectCopies();
121 
122  static std::unique_ptr<SimulatedObject> makeObject(const std::string& name,
123  const ObjectSource& objectSource);
124 
125  /**
126  * @brief updateLocalObjectCopies
127  * Updates local copies of objects by retrieving their poses from the simulator.
128  */
129  void updateLocalObjectCopies();
130 
131  void dropObjects();
132  void resetInvalidObjects();
133 
134  Config config_;
135  armarx::SimulatorInterfacePrx simulator_;
136 
137  boost::mt19937 rnd_;
138 
139  std::vector<std::unique_ptr<SimulatedObject>> localObjectCopies_;
140 
141  // Walls for collision detection to ensure objects are inside the box
142  VirtualRobot::SceneObjectPtr frontWall_;
145  VirtualRobot::SceneObjectPtr rightWall_;
146  VirtualRobot::SceneObjectSetPtr walls_;
147  };
148 } // namespace armarx::simulation::scene_generation
armarx::simulation::scene_generation::ObjectSource
Definition: SimulatedObject.h:44
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::minObjectZOtherwiseReset
float minObjectZOtherwiseReset
Definition: ClutteredSceneGenerator.h:89
armarx::simulation::scene_generation::ClutteredSceneGenerator::setConfig
void setConfig(const Config &conf)
Definition: ClutteredSceneGenerator.cpp:236
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::maxResetTries
uint maxResetTries
Maximum number of retries when resetting objects.
Definition: ClutteredSceneGenerator.h:66
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config
Definition: ClutteredSceneGenerator.h:52
Pose.h
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::boxX
float boxX
Definition: ClutteredSceneGenerator.h:69
armarx::simulation::scene_generation
Definition: ClutteredSceneGenerator.cpp:48
armarx::simulation::scene_generation::ClutteredSceneGenerator::getSimulator
const SimulatorInterfacePrx & getSimulator() const
Definition: ClutteredSceneGenerator.cpp:241
armarx::simulation::scene_generation::ClutteredSceneGenerator
Brief description of class ClutteredSceneGenerator.
Definition: ClutteredSceneGenerator.h:49
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::maxObjectY
float maxObjectY
Definition: ClutteredSceneGenerator.h:85
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::objectMarginSide
float objectMarginSide
Definition: ClutteredSceneGenerator.h:77
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::minObjectX
float minObjectX
Definition: ClutteredSceneGenerator.h:82
armarx::simulation::scene_generation::ClutteredSceneGenerator::generateScene
void generateScene(int seed)
Definition: ClutteredSceneGenerator.cpp:58
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::minObjectZ
float minObjectZ
Definition: ClutteredSceneGenerator.h:86
IceInternal::Handle< Pose >
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::boxD
float boxD
Definition: ClutteredSceneGenerator.h:74
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::minObjectY
float minObjectY
Definition: ClutteredSceneGenerator.h:83
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::boxH
float boxH
Definition: ClutteredSceneGenerator.h:73
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::objectSpacingZ
float objectSpacingZ
Definition: ClutteredSceneGenerator.h:79
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::boxZ
float boxZ
Definition: ClutteredSceneGenerator.h:71
SimulatedObject.h
armarx::simulation::scene_generation::ClutteredSceneGenerator::getConfig
const Config & getConfig() const
Definition: ClutteredSceneGenerator.cpp:231
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::amountObjects
uint amountObjects
Number of objects to generate in a scene.
Definition: ClutteredSceneGenerator.h:58
armarx::simulation::scene_generation::ClutteredSceneGenerator::ClutteredSceneGenerator
ClutteredSceneGenerator(const armarx::SimulatorInterfacePrx &simulator, const Config &config)
Definition: ClutteredSceneGenerator.cpp:50
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::objectSets
std::vector< ObjectSet > objectSets
Definition: ClutteredSceneGenerator.h:55
scene3D::SceneObjectPtr
boost::intrusive_ptr< SceneObject > SceneObjectPtr
Definition: PointerDefinitions.h:40
armarx::simulation::scene_generation::ClutteredSceneGenerator::setSimulator
void setSimulator(const SimulatorInterfacePrx &simulator)
Definition: ClutteredSceneGenerator.cpp:246
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::boxY
float boxY
Definition: ClutteredSceneGenerator.h:70
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::maxObjectX
float maxObjectX
Definition: ClutteredSceneGenerator.h:84
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::autoResetInvalidObjects
bool autoResetInvalidObjects
Whether to automatically reset invalid objects after scene generation.
Definition: ClutteredSceneGenerator.h:64
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::boxW
float boxW
Definition: ClutteredSceneGenerator.h:72
armarx::simulation::scene_generation::ClutteredSceneGenerator::Config::fallingSteps
uint fallingSteps
Number of simulation steps executed for letting objects fall onto the table.
Definition: ClutteredSceneGenerator.h:61