ClutteredSceneGeneratorWidgetController.cpp
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::gui-plugins::ClutteredSceneGeneratorWidgetController
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 
24 
27 
30 
31 #include <boost/algorithm/string/predicate.hpp>
32 
33 #include <QListWidget>
34 #include <QFlags>
35 #include <qnamespace.h>
36 
37 #include <algorithm>
38 #include <iterator>
39 #include <filesystem>
40 #include <optional>
41 
42 #include <glob.h>
43 
44 namespace armarx
45 {
47  {
48  widget_.setupUi(getWidget());
49 
50  // TODO(patrick.hegemann): Refactor
51  // armarx::CMakePackageFinder finder("ArmarXSimulation");
52  // std::filesystem::path objectSetsPath = finder.getDataDir();
53  // objectSetsPath /= "ArmarXSimulation/random_objects";
54  // ARMARX_INFO << "Finding object sets in " << objectSetsPath;
55  // QString pathText(objectSetsPath.c_str());
56  // widget_.lineEditObjectsOutputPath->setText(pathText);
57  // widget_.lineEditObjectsInputPath->setText(pathText);
58 
59  refreshSets();
60 
62  }
63 
65 
67  {
68  }
69 
71  {
72  }
73 
75  {
76  usingProxy(simulatorProxyName_);
77  }
78 
80  {
81  // Scene generation tab
82  connect(widget_.pushButtonRefreshObjectSets, &QPushButton::clicked, this, &ClutteredSceneGeneratorWidgetController::refreshSets);
83  connect(widget_.pushButtonGenerateNewScene, &QPushButton::clicked, this, &ClutteredSceneGeneratorWidgetController::generateScene);
84 
85  connect(widget_.checkBoxAutoPoseConstraints, &QCheckBox::stateChanged, this, &ClutteredSceneGeneratorWidgetController::updateObjectPoseConstraints);
86  connect(widget_.spinBoxTableX, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &ClutteredSceneGeneratorWidgetController::updateObjectPoseConstraints);
87  connect(widget_.spinBoxTableY, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &ClutteredSceneGeneratorWidgetController::updateObjectPoseConstraints);
88  connect(widget_.spinBoxTableZ, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &ClutteredSceneGeneratorWidgetController::updateObjectPoseConstraints);
89  connect(widget_.spinBoxTableW, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &ClutteredSceneGeneratorWidgetController::updateObjectPoseConstraints);
90  connect(widget_.spinBoxTableH, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &ClutteredSceneGeneratorWidgetController::updateObjectPoseConstraints);
91  connect(widget_.spinBoxTableD, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &ClutteredSceneGeneratorWidgetController::updateObjectPoseConstraints);
92 
93  simulator_ = getProxy<SimulatorInterfacePrx>(simulatorProxyName_);
94  }
95 
97 
98  }
99 
100  std::map<std::string, simulation::scene_generation::ObjectSet>
101  ClutteredSceneGeneratorWidgetController::getObjectSets(const armarx::PackagePath& packagePath)
102  {
103  const auto& directoryPackagePath = packagePath.serialize();
104  std::map<std::string, simulation::scene_generation::ObjectSet> sets;
105 
106  // TODO(patrick.hegemann): Refactor
107  ARMARX_INFO << "Finding objects in " << packagePath.toSystemPath();
108 
109  for (const auto & setEntry : std::filesystem::directory_iterator(packagePath.toSystemPath()))
110  {
111  if (!setEntry.is_directory())
112  {
113  continue;
114  }
115  const std::string setName = setEntry.path().filename();
116 
117  std::vector<simulation::scene_generation::ObjectSource> objectSources;
118  for (const auto & objectEntry : std::filesystem::directory_iterator(setEntry.path()))
119  {
120  if (objectEntry.path().extension().string() == ".xml")
121  {
122  std::filesystem::path newPath(directoryPackagePath.path);
123  newPath /= setName / objectEntry.path().filename();
124  PackagePath objectPackagePath(directoryPackagePath.package, newPath);
125 
126  simulation::scene_generation::ObjectSource objectSource = {
127  .path = objectPackagePath,
129  .objectID = std::nullopt
130  };
131  objectSources.push_back(objectSource);
132  }
133  }
134  simulation::scene_generation::ObjectSet currentObjectSet {.objects = objectSources};
135 
136  sets[setName] = currentObjectSet;
137  ARMARX_INFO << "added set " << setName;
138  }
139 
140  return sets;
141  }
142 
144  // boost::filesystem::path path(widget_.lineEditObjectsInputPath->text().toStdString());
145  PackagePath path("ArmarXSimulation", "random_objects");
146  objectSets_ = getObjectSets(path);
147 
148  // Clear list and delete all old items
149  widget_.listWidgetObjectSets->clear();
150  objectSetListItems_.clear();
151  for (auto& item : objectSetListItems_)
152  {
153  delete item;
154  }
155 
156  // Fill list widget
157  for (const auto& [setName, _] : objectSets_)
158  {
159  auto* item = new QListWidgetItem;
160  objectSetListItems_.push_back(item);
161 
162  // TODO(patrick.hegemann): Find a standard method to get the name of a directory
163  std::string basename = setName.substr(setName.find_last_of('/') + 1);
164  item->setText(QString::fromStdString(basename));
165  item->setData(1, QString::fromStdString(setName));
166  item->setFlags((item->flags() | Qt::ItemIsUserCheckable) & ~Qt::ItemIsSelectable);
167  item->setCheckState(Qt::Checked);
168  widget_.listWidgetObjectSets->addItem(item);
169  }
170  }
171 
173  if (widget_.checkBoxAutoPoseConstraints->isChecked()) {
174  const double tableX = widget_.spinBoxTableX->value();
175  const double tableY = widget_.spinBoxTableY->value();
176  const double tableZ = widget_.spinBoxTableZ->value();
177  const double tableW = widget_.spinBoxTableW->value();
178  const double tableH = widget_.spinBoxTableH->value();
179  const double tableD = widget_.spinBoxTableD->value();
180  const double margin = widget_.spinBoxMarginBoxWalls->value();
181 
182  const double minX = tableX - tableW / 2 + margin;
183  const double minY = tableY - tableH / 2 + margin;
184  const double maxX = tableX + tableW / 2 - margin;
185  const double maxY = tableY + tableH / 2 - margin;
186  const double minZ = tableZ + tableD + 150;
187 
188  widget_.spinBoxObjectMinX->setValue(minX);
189  widget_.spinBoxObjectMinY->setValue(minY);
190  widget_.spinBoxObjectMaxX->setValue(maxX);
191  widget_.spinBoxObjectMaxY->setValue(maxY);
192  widget_.spinBoxObjectMinZ->setValue(minZ);
193  }
194  }
195 
197  {
198  simulation::scene_generation::ClutteredSceneGenerator generator(simulator_, getGenConfig());
199  int seed = widget_.spinBoxSceneSeed->value();
200  generator.generateScene(seed);
201  }
202 
203 
204 
205  simulation::scene_generation::ClutteredSceneGenerator::Config ClutteredSceneGeneratorWidgetController::getGenConfig() {
206  simulation::scene_generation::ClutteredSceneGenerator::Config genConfig;
207 
208  // Set selected object sets
209  std::vector<simulation::scene_generation::ObjectSet> selectedSets;
210 
211  for (int i = 0; i < widget_.listWidgetObjectSets->count(); ++i)
212  {
213  auto* item = widget_.listWidgetObjectSets->item(i);
214  if (item->checkState() == Qt::CheckState::Checked)
215  {
216  std::string setPath = item->data(1).toString().toStdString();
217  selectedSets.push_back(objectSets_[setPath]);
218  }
219  }
220  genConfig.objectSets = selectedSets;
221 
222  genConfig.amountObjects = widget_.spinBoxSceneObjectAmount->value();
223  // genConfig.objectsPerSetLimit = widget_.checkBoxLimitOnePerSet->isChecked() ? 1 : 0;
224  genConfig.fallingSteps = widget_.spinBoxObjectFallingSteps->value();
225  genConfig.autoResetInvalidObjects = widget_.checkBoxResetInvalidObjects->isChecked();
226  genConfig.maxResetTries = widget_.spinBoxMaxResetAttempts->value();
227 
228  genConfig.boxX = static_cast<float>(widget_.spinBoxTableX->value());
229  genConfig.boxY = static_cast<float>(widget_.spinBoxTableY->value());
230  genConfig.boxZ = static_cast<float>(widget_.spinBoxTableZ->value());
231  genConfig.boxW = static_cast<float>(widget_.spinBoxTableW->value());
232  genConfig.boxH = static_cast<float>(widget_.spinBoxTableH->value());
233  genConfig.boxD = static_cast<float>(widget_.spinBoxTableD->value());
234 
235  genConfig.objectMarginSide = static_cast<float>(widget_.spinBoxMarginBoxWalls->value());
236  genConfig.objectSpacingZ = static_cast<float>(widget_.spinBoxObjectSpacingZ->value());
237 
238  genConfig.minObjectX = static_cast<float>(widget_.spinBoxObjectMinX->value());
239  genConfig.minObjectY = static_cast<float>(widget_.spinBoxObjectMinY->value());
240  genConfig.minObjectZ = static_cast<float>(widget_.spinBoxObjectMinZ->value());
241  genConfig.maxObjectX = static_cast<float>(widget_.spinBoxObjectMaxX->value());
242  genConfig.maxObjectY = static_cast<float>(widget_.spinBoxObjectMaxY->value());
243 
244  genConfig.minObjectZOtherwiseReset = static_cast<float>(widget_.spinBoxZPosResetting->value());
245 
246  return genConfig;
247  }
248 } // namespace armarx
armarx::ManagedIceObject::setName
void setName(std::string name)
Override name of well-known object.
Definition: ManagedIceObject.cpp:426
armarx::ClutteredSceneGeneratorWidgetController::generateObjects
void generateObjects()
Definition: ClutteredSceneGeneratorWidgetController.cpp:96
armarx::ClutteredSceneGeneratorWidgetController::~ClutteredSceneGeneratorWidgetController
virtual ~ClutteredSceneGeneratorWidgetController()
Controller destructor.
armarx::ClutteredSceneGeneratorWidgetController::onInitComponent
void onInitComponent() override
Definition: ClutteredSceneGeneratorWidgetController.cpp:74
armarx::ClutteredSceneGeneratorWidgetController::loadSettings
void loadSettings(QSettings *settings) override
Implement to load the settings that are part of the GUI configuration.
Definition: ClutteredSceneGeneratorWidgetController.cpp:66
armarx::PackagePath::serialize
data::PackagePath serialize() const
Definition: PackagePath.cpp:76
armarx::ClutteredSceneGeneratorWidgetController::onConnectComponent
void onConnectComponent() override
Definition: ClutteredSceneGeneratorWidgetController.cpp:79
armarx::PackagePath::toSystemPath
static std::filesystem::path toSystemPath(const data::PackagePath &pp)
Definition: PackagePath.cpp:54
armarx::simulation::scene_generation::AsRobot
@ AsRobot
Definition: SimulatedObject.h:40
armarx::ClutteredSceneGeneratorWidgetController::generateScene
void generateScene()
Definition: ClutteredSceneGeneratorWidgetController.cpp:196
SimulatedObject.h
CMakePackageFinder.h
ARMARX_INFO
#define ARMARX_INFO
Definition: Logging.h:174
armarx::ClutteredSceneGeneratorWidgetController::updateObjectPoseConstraints
void updateObjectPoseConstraints()
Definition: ClutteredSceneGeneratorWidgetController.cpp:172
ClutteredSceneGenerator.h
armarx::ArmarXWidgetController::getWidget
virtual QPointer< QWidget > getWidget()
getWidget returns a pointer to the a widget of this controller.
Definition: ArmarXWidgetController.cpp:54
armarx::ClutteredSceneGeneratorWidgetController::ClutteredSceneGeneratorWidgetController
ClutteredSceneGeneratorWidgetController()
Controller Constructor.
Definition: ClutteredSceneGeneratorWidgetController.cpp:46
ClutteredSceneGeneratorWidgetController.h
armarx::ClutteredSceneGeneratorWidgetController::saveSettings
void saveSettings(QSettings *settings) override
Definition: ClutteredSceneGeneratorWidgetController.cpp:70
armarx::PackagePath
Definition: PackagePath.h:55
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::ClutteredSceneGeneratorWidgetController::refreshSets
void refreshSets()
Definition: ClutteredSceneGeneratorWidgetController.cpp:143
PackagePath.h