30 #include <VirtualRobot/CollisionDetection/CollisionModel.h>
31 #include <VirtualRobot/Visualization/TriMeshUtils.h>
32 #include <VirtualRobot/Visualization/VisualizationNode.h>
44 connect(widget.pushButtonAddObjects, SIGNAL(clicked()),
this, SLOT(
addObjects()));
51 getProxy<memoryx::PriorKnowledgeInterfacePrx>(dialog->getProxyName(
"PriorKnowledge"));
52 classesSegmentPrx = priorPrx->getObjectClassesSegment();
54 databasePrx = priorPrx->getCommonStorage();
56 QMetaObject::invokeMethod(
this,
"updateDBs", Qt::QueuedConnection);
65 dialog->addProxyFinder<memoryx::PriorKnowledgeInterfacePrx>(
66 {
"PriorKnowledge",
"PriorKnowledge",
"PriorKnowledge*"});
68 return qobject_cast<SimpleConfigDialog*>(dialog);
74 const NameList dbNames = databasePrx->getDBNames();
75 widget.comboBoxDBs->clear();
76 for (
const auto& db : dbNames)
78 widget.comboBoxDBs->addItem(QString::fromStdString(db));
80 ARMARX_INFO <<
"setting list of DBs to: " << dbNames;
86 namespace fs = std::filesystem;
88 const std::string dir = widget.lineEditDir->text().toStdString();
89 const std::string collection = widget.lineEditCollection->text().toStdString();
90 const std::string db = widget.comboBoxDBs->currentText().toStdString();
91 const std::string tmpdir =
92 "/tmp/LoadObjectsIntoMemoryWidget/" +
93 std::to_string(std::chrono::high_resolution_clock::now().time_since_epoch().count()) +
"/";
95 fs::create_directories(tmpdir);
102 if (!collection.empty())
104 const std::string fullCollectionName = db +
"." + collection;
105 ARMARX_INFO <<
"setting write collection to: " << fullCollectionName;
106 memoryx::CollectionInterfacePrx coll = databasePrx->requestCollection(fullCollectionName);
108 classesSegmentPrx->setWriteCollection(coll);
112 for (fs::directory_iterator dit{dir}; dit != fs::directory_iterator{}; ++dit)
114 const fs::path p = *dit;
115 if (!fs::is_regular_file(p))
120 const std::string fname = p.filename().string();
121 const std::string fpath = p.string();
122 const auto endswith = [&](
const std::string& suff)
124 const auto pos = fname.rfind(suff);
125 return pos != std::string::npos && pos == (fname.size() - suff.size());
127 std::string className = fname;
132 if (widget.radioButtonWRL->isChecked())
134 if (endswith(
".wrl"))
136 className = className.substr(0, className.size() - 4);
138 else if (endswith(
".iv"))
140 className = className.substr(0, className.size() - 3);
150 if (endswith(
".xml"))
152 className = className.substr(0, className.size() - 4);
164 const bool isNewClass = !classesSegmentPrx->hasEntityByName(className);
168 if (!widget.checkBoxOverride->isChecked())
170 ARMARX_INFO <<
"object class " << className <<
" already exists in memory";
173 objectClass = memoryx::ObjectClassPtr::dynamicCast(
174 classesSegmentPrx->getEntityByName(className));
188 objectClass->setName(className);
189 objectClass->clearParentClasses();
191 const std::string parentClass = widget.lineEditParentClass->text().toStdString();
192 if (!parentClass.empty())
194 objectClass->addParentClass(parentClass);
197 objectClass->setInstanceable(
true);
206 if (widget.radioButtonWRL->isChecked())
209 simoxWrapper->setAndStoreModelIVFiles(fpath, fpath, getFilesDBName());
214 simoxWrapper->setAndStoreManipulationFile(fpath, getFilesDBName());
216 if (widget.checkBoxScale->isChecked() || widget.checkBoxBBDensity->isChecked())
219 Eigen::Vector3f bbmin;
220 Eigen::Vector3f bbmax;
221 auto mo = simoxWrapper->getManipulationObject();
222 auto col = mo->getCollisionModel();
224 Eigen::Vector3f bbsz = bbmax - bbmin;
226 if (widget.checkBoxBBDensity->isChecked())
228 const float vol = bbsz(0) * bbsz(1) * bbsz(2);
229 const float mass = vol * widget.doubleSpinBoxBBDensity->value();
230 if (mo->getMass() == mass)
236 if (widget.checkBoxScale->isChecked())
238 const double sidesz = bbsz(widget.spinBoxScaleDim->value());
239 bool doScale =
false;
241 if (sidesz > widget.doubleSpinBoxScaleMax->value())
245 factor = widget.doubleSpinBoxScaleMax->value() / sidesz;
247 else if (sidesz < widget.doubleSpinBoxScaleMin->
value())
251 factor = widget.doubleSpinBoxScaleMin->value() / sidesz;
255 ARMARX_INFO <<
"scaling mesh with factor " << factor;
256 const Eigen::Vector3f scalev{factor, factor, factor};
257 auto triNew = col->getTriMeshModel()->clone(scalev);
258 auto moNew = VirtualRobot::ManipulationObject::createFromMesh(triNew);
259 moNew->setMass(mo->getMass());
260 moNew->setInertiaMatrix(mo->getInertiaMatrix());
261 moNew->setCoMLocal(mo->getCoMLocal());
262 const std::string meshName = tmpdir + className +
".wrl";
263 moNew->getVisualization()->setFilename(meshName,
false);
264 moNew->getCollisionModel()->getVisualization()->setFilename(meshName,
271 mo->setName(className);
272 mo->saveModelFiles(tmpdir,
false);
273 const std::string tmpxml = tmpdir + className +
".xml";
274 std::ofstream{tmpxml} << mo->toXML(tmpdir);
277 simoxWrapper->setAndStoreManipulationFile(tmpxml, getFilesDBName());
283 if (!widget.checkBoxDryRun->isChecked())
288 const std::string
id = classesSegmentPrx->addEntity(objectClass);
294 const std::string
id = objectClass->getId();
295 classesSegmentPrx->updateEntity(
id, objectClass);