29 #include <VirtualRobot/Visualization/TriMeshUtils.h>
30 #include <VirtualRobot/Visualization/VisualizationNode.h>
44 connect(widget.pushButtonAddObjects, SIGNAL(clicked()),
this, SLOT(
addObjects()));
49 priorPrx = getProxy<memoryx::PriorKnowledgeInterfacePrx>(dialog->getProxyName(
"PriorKnowledge"));
50 classesSegmentPrx = priorPrx->getObjectClassesSegment();
52 databasePrx = priorPrx->getCommonStorage();
54 QMetaObject::invokeMethod(
this,
"updateDBs", Qt::QueuedConnection);
62 dialog->addProxyFinder<memoryx::PriorKnowledgeInterfacePrx>({
"PriorKnowledge",
"PriorKnowledge",
"PriorKnowledge*"});
64 return qobject_cast<SimpleConfigDialog*>(dialog);
69 const NameList dbNames = databasePrx->getDBNames();
70 widget.comboBoxDBs->clear();
71 for (
const auto& db : dbNames)
73 widget.comboBoxDBs->addItem(QString::fromStdString(db));
75 ARMARX_INFO <<
"setting list of DBs to: " << dbNames;
80 namespace fs = std::filesystem;
82 const std::string dir = widget.lineEditDir->text().toStdString();
83 const std::string collection = widget.lineEditCollection->text().toStdString();
84 const std::string db = widget.comboBoxDBs->currentText().toStdString();
85 const std::string tmpdir =
"/tmp/LoadObjectsIntoMemoryWidget/" +
std::to_string(std::chrono::high_resolution_clock::now().time_since_epoch().count()) +
"/";
87 fs::create_directories(tmpdir);
94 if (!collection.empty())
96 const std::string fullCollectionName = db +
"." + collection;
97 ARMARX_INFO <<
"setting write collection to: " << fullCollectionName;
98 memoryx::CollectionInterfacePrx coll = databasePrx->requestCollection(fullCollectionName);
100 classesSegmentPrx->setWriteCollection(coll);
104 for (fs::directory_iterator dit{dir}; dit != fs::directory_iterator{}; ++dit)
106 const fs::path p = *dit;
107 if (!fs::is_regular_file(p))
112 const std::string fname = p.filename().string();
113 const std::string fpath = p.string();
114 const auto endswith = [&](
const std::string & suff)
116 const auto pos = fname.rfind(suff);
117 return pos != std::string::npos && pos == (fname.size() - suff.size());
119 std::string className = fname;
124 if (widget.radioButtonWRL->isChecked())
126 if (endswith(
".wrl"))
128 className = className.substr(0, className.size() - 4);
130 else if (endswith(
".iv"))
132 className = className.substr(0, className.size() - 3);
142 if (endswith(
".xml"))
144 className = className.substr(0, className.size() - 4);
156 const bool isNewClass = !classesSegmentPrx->hasEntityByName(className);
160 if (!widget.checkBoxOverride->isChecked())
162 ARMARX_INFO <<
"object class " << className <<
" already exists in memory";
165 objectClass = memoryx::ObjectClassPtr::dynamicCast(classesSegmentPrx->getEntityByName(className));
179 objectClass->setName(className);
180 objectClass->clearParentClasses();
182 const std::string parentClass = widget.lineEditParentClass->text().toStdString();
183 if (!parentClass.empty())
185 objectClass->addParentClass(parentClass);
188 objectClass->setInstanceable(
true);
196 if (widget.radioButtonWRL->isChecked())
199 simoxWrapper->setAndStoreModelIVFiles(fpath, fpath, getFilesDBName());
204 simoxWrapper->setAndStoreManipulationFile(fpath, getFilesDBName());
206 if (widget.checkBoxScale->isChecked() || widget.checkBoxBBDensity->isChecked())
209 Eigen::Vector3f bbmin;
210 Eigen::Vector3f bbmax;
211 auto mo = simoxWrapper->getManipulationObject();
212 auto col = mo->getCollisionModel();
214 Eigen::Vector3f bbsz = bbmax - bbmin;
216 if (widget.checkBoxBBDensity->isChecked())
218 const float vol = bbsz(0) * bbsz(1) * bbsz(2);
219 const float mass = vol * widget.doubleSpinBoxBBDensity->value();
220 if (mo->getMass() == mass)
226 if (widget.checkBoxScale->isChecked())
228 const double sidesz = bbsz(widget.spinBoxScaleDim->value());
229 bool doScale =
false;
231 if (sidesz > widget.doubleSpinBoxScaleMax->value())
235 factor = widget.doubleSpinBoxScaleMax->value() / sidesz;
237 else if (sidesz < widget.doubleSpinBoxScaleMin->
value())
241 factor = widget.doubleSpinBoxScaleMin->value() / sidesz;
245 ARMARX_INFO <<
"scaling mesh with factor " << factor;
246 const Eigen::Vector3f scalev {factor, factor, factor};
247 auto triNew = col->getTriMeshModel()->clone(scalev);
248 auto moNew = VirtualRobot::ManipulationObject::createFromMesh(triNew);
249 moNew->setMass(mo->getMass());
250 moNew->setInertiaMatrix(mo->getInertiaMatrix());
251 moNew->setCoMLocal(mo->getCoMLocal());
252 const std::string meshName = tmpdir + className +
".wrl";
253 moNew->getVisualization()->setFilename(meshName,
false);
254 moNew->getCollisionModel()->getVisualization()->setFilename(meshName,
false);
260 mo->setName(className);
261 mo->saveModelFiles(tmpdir,
false);
262 const std::string tmpxml = tmpdir + className +
".xml";
263 std::ofstream{tmpxml} << mo->toXML(tmpdir);
266 simoxWrapper->setAndStoreManipulationFile(tmpxml, getFilesDBName());
272 if (!widget.checkBoxDryRun->isChecked())
277 const std::string
id = classesSegmentPrx->addEntity(objectClass);
283 const std::string
id = objectClass->getId();
284 classesSegmentPrx->updateEntity(
id, objectClass);