Skip to content

Commit ceb3b3a

Browse files
authored
--[BE Week] Articulated Object Loading refactors and cleanup (#2291)
* --initial commit Add 'applyScaleToMass' method to determine whether geometric scaling should be applied to mass calc for objects as well as articulated objects * --remove unused urdf-related add function * --update docs * --add tests * --remove unused addArticulatedObjectFromURDF method
1 parent 004b3ea commit ceb3b3a

15 files changed

+232
-297
lines changed

docs/pages/attributesJSON.rst

+6
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,9 @@ All rigid and articulated objects instanced in the scene during initialization s
189189
- "non_uniform_scale"
190190
- 3-vector
191191
- A non-uniform scale vector to apply in addition to the uniform scale.
192+
"apply_scale_to_mass"
193+
- boolean
194+
- Whether or not to apply the product of the geometric scaling to the mass.
192195
- "translation_origin"
193196
- string
194197
- One of ('COM', 'asset_local'). Defines whether the translation provided for this object instance is applied in render asset local space or center of mass (COM) aligned space. All rigid object translations within Habitat-sim are in COM space, but external translations (e.g. exported from Blender) may not be.
@@ -223,6 +226,9 @@ All rigid and articulated objects instanced in the scene during initialization s
223226
- "mass_scale"
224227
- float
225228
- Mass does not scale linearly with object scale, so you can customize this.
229+
"apply_scale_to_mass"
230+
- boolean
231+
- Whether or not to apply the product of the geometric scaling to the mass. This is in addition to any scaling specified via "mass_scale" .
226232
- "translation_origin"
227233
- string
228234
- One of ('COM', 'asset_local'). Defines whether the translation provided for this object instance is applied in render asset local space or center of mass (COM) aligned space.

src/esp/metadata/attributes/SceneInstanceAttributes.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ SceneObjectInstanceAttributes::SceneObjectInstanceAttributes(
3333
setUniformScale(1.0);
3434
setNonUniformScale({1.0, 1.0, 1.0});
3535
setMassScale(1.0);
36+
setApplyScaleToMass(true);
3637
}
3738

3839
std::string SceneObjectInstanceAttributes::getObjectInfoHeaderInternal() const {
@@ -117,6 +118,9 @@ void SceneObjectInstanceAttributes::writeValuesToJson(
117118
if (getNonUniformScale() != Mn::Vector3(1.0, 1.0, 1.0)) {
118119
writeValueToJson("non_uniform_scale", jsonObj, allocator);
119120
}
121+
if (!getApplyScaleToMass()) {
122+
writeValueToJson("apply_scale_to_mass", jsonObj, allocator);
123+
}
120124
if (getMassScale() != 1.0) {
121125
writeValueToJson("mass_scale", jsonObj, allocator);
122126
}

src/esp/metadata/attributes/SceneInstanceAttributes.h

+16
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,22 @@ class SceneObjectInstanceAttributes : public AbstractAttributes {
189189
set("non_uniform_scale", non_uniform_scale);
190190
}
191191

192+
/**
193+
* @brief Object and Articulated Object Instance only. Get whether or
194+
* not to apply geometric scaling to mass (i.e. to maintain object mean
195+
* density)
196+
*/
197+
bool getApplyScaleToMass() const { return get<bool>("apply_scale_to_mass"); }
198+
199+
/**
200+
* @brief Object and Articulated Object Instance only. Set whether or
201+
* not to apply geometric scaling to mass (i.e. to maintain object mean
202+
* density)
203+
*/
204+
void setApplyScaleToMass(bool apply_scale_to_mass) {
205+
set("apply_scale_to_mass", apply_scale_to_mass);
206+
}
207+
192208
/**
193209
* @brief Get or set the mass scaling of the instanced object.
194210
*/

src/esp/metadata/managers/SceneInstanceAttributesManager.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,12 @@ void SceneInstanceAttributesManager::setAbstractObjectAttributesFromJson(
461461
instanceAttrs->setNonUniformScale(non_uniform_scale);
462462
});
463463

464+
// whether geometric scaling should be applied to mass to maintain density.
465+
io::jsonIntoSetter<bool>(
466+
jCell, "apply_scale_to_mass", [instanceAttrs](bool apply_scale_to_mass) {
467+
instanceAttrs->setApplyScaleToMass(apply_scale_to_mass);
468+
});
469+
464470
// whether particular instance is visible or not - only modify if actually
465471
// present in instance json
466472
io::jsonIntoSetter<bool>(

src/esp/physics/PhysicsManager.cpp

+64-59
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,14 @@ int PhysicsManager::addObjectInstance(
157157
// non-uniform scaling
158158
objAttributes->setScale(objAttributes->getScale() *
159159
objInstAttributes->getNonUniformScale());
160+
161+
// If boolean specifies to do so, apply geometric scaling to mass (product of
162+
// scale values)
163+
if (objInstAttributes->getApplyScaleToMass()) {
164+
objAttributes->setMass(
165+
objAttributes->getMass() *
166+
static_cast<double>(objAttributes->getScale().product()));
167+
}
160168
// set scaled mass
161169
objAttributes->setMass(objAttributes->getMass() *
162170
objInstAttributes->getMassScale());
@@ -367,16 +375,9 @@ int PhysicsManager::addArticulatedObjectInstance(
367375
int visSet = aObjInstAttributes->getIsInstanceVisible();
368376
if (visSet != ID_UNDEFINED) {
369377
// specfied in scene instance
370-
// objAttributes->setIsVisible(visSet == 1);
378+
// artObjAttributes->setIsVisible(visSet == 1);
371379
// TODO: manage articulated object visibility.
372380
}
373-
// set uniform scale
374-
artObjAttributes->setUniformScale(artObjAttributes->getUniformScale() *
375-
aObjInstAttributes->getUniformScale());
376-
// set scaled mass
377-
artObjAttributes->setMassScale(artObjAttributes->getMassScale() *
378-
aObjInstAttributes->getMassScale());
379-
380381
// set shader type to use for articulated object instance, which may override
381382
// shadertype specified in articulated object attributes.
382383
const auto artObjShaderType = aObjInstAttributes->getShaderType();
@@ -385,6 +386,22 @@ int PhysicsManager::addArticulatedObjectInstance(
385386
artObjAttributes->setShaderType(getShaderTypeName(artObjShaderType));
386387
}
387388

389+
// set uniform scale
390+
artObjAttributes->setUniformScale(artObjAttributes->getUniformScale() *
391+
aObjInstAttributes->getUniformScale());
392+
393+
// If boolean specifies to do so, apply geometric scaling to mass (product of
394+
// scale values)
395+
if (aObjInstAttributes->getApplyScaleToMass()) {
396+
artObjAttributes->setMassScale(
397+
artObjAttributes->getMassScale() *
398+
static_cast<double>(artObjAttributes->getUniformScale()));
399+
}
400+
401+
// set scaled mass
402+
artObjAttributes->setMassScale(artObjAttributes->getMassScale() *
403+
aObjInstAttributes->getMassScale());
404+
388405
const auto baseType = aObjInstAttributes->getBaseType();
389406
if (baseType !=
390407
metadata::attributes::ArticulatedObjectBaseType::Unspecified) {
@@ -467,59 +484,47 @@ int PhysicsManager::addArticulatedObjectFromURDF(
467484
// acquire context if available
468485
simulator_->getRenderGLContext();
469486
auto& drawables = simulator_->getDrawableGroup();
470-
return addArticulatedObjectFromURDF(
471-
filepath, &drawables, fixedBase, globalScale, massScale, forceReload,
472-
maintainLinkOrder, intertiaFromURDF, lightSetup);
487+
488+
// Retrieve or create the appropriate ArticulatedObjectAttributes to create
489+
// this AO.
490+
491+
bool attribsFound =
492+
resourceManager_.getAOAttributesManager()->getObjectLibHasHandle(
493+
filepath);
494+
495+
esp::metadata::attributes::ArticulatedObjectAttributes::ptr
496+
artObjAttributes =
497+
attribsFound
498+
? resourceManager_.getAOAttributesManager()
499+
->getObjectCopyByHandle(filepath)
500+
: resourceManager_.getAOAttributesManager()->createObject(
501+
filepath, true);
502+
503+
// Set pertinent values
504+
artObjAttributes->setUniformScale(globalScale);
505+
artObjAttributes->setMassScale(static_cast<double>(massScale));
506+
507+
artObjAttributes->setBaseType(metadata::attributes::getAOBaseTypeName(
508+
fixedBase ? metadata::attributes::ArticulatedObjectBaseType::Fixed
509+
: metadata::attributes::ArticulatedObjectBaseType::Free));
510+
511+
artObjAttributes->setInertiaSource(
512+
metadata::attributes::getAOInertiaSourceName(
513+
intertiaFromURDF
514+
? metadata::attributes::ArticulatedObjectInertiaSource::URDF
515+
: metadata::attributes::ArticulatedObjectInertiaSource::
516+
Computed));
517+
518+
artObjAttributes->setLinkOrder(metadata::attributes::getAOLinkOrderName(
519+
maintainLinkOrder
520+
? metadata::attributes::ArticulatedObjectLinkOrder::URDFOrder
521+
: metadata::attributes::ArticulatedObjectLinkOrder::TreeTraversal));
522+
523+
return addArticulatedObject(artObjAttributes, &drawables, forceReload,
524+
lightSetup);
473525
}
474526
return ID_UNDEFINED;
475-
} // PhysicsManager::addArticulatedObjectFromURDF
476-
477-
int PhysicsManager::addArticulatedObjectFromURDF(
478-
const std::string& filepath,
479-
DrawableGroup* drawables,
480-
bool fixedBase,
481-
float globalScale,
482-
float massScale,
483-
bool forceReload,
484-
bool maintainLinkOrder,
485-
bool intertiaFromURDF,
486-
const std::string& lightSetup) {
487-
// Retrieve or create the appropriate ArticulatedObjectAttributes to create
488-
// this AO.
489-
490-
bool attribsFound =
491-
resourceManager_.getAOAttributesManager()->getObjectLibHasHandle(
492-
filepath);
493-
494-
esp::metadata::attributes::ArticulatedObjectAttributes::ptr artObjAttributes =
495-
attribsFound
496-
? resourceManager_.getAOAttributesManager()->getObjectCopyByHandle(
497-
filepath)
498-
: resourceManager_.getAOAttributesManager()->createObject(filepath,
499-
true);
500-
501-
// Set pertinent values
502-
artObjAttributes->setUniformScale(globalScale);
503-
artObjAttributes->setMassScale(static_cast<double>(massScale));
504-
505-
artObjAttributes->setBaseType(metadata::attributes::getAOBaseTypeName(
506-
fixedBase ? metadata::attributes::ArticulatedObjectBaseType::Fixed
507-
: metadata::attributes::ArticulatedObjectBaseType::Free));
508-
509-
artObjAttributes->setInertiaSource(
510-
metadata::attributes::getAOInertiaSourceName(
511-
intertiaFromURDF
512-
? metadata::attributes::ArticulatedObjectInertiaSource::URDF
513-
: metadata::attributes::ArticulatedObjectInertiaSource::
514-
Computed));
515-
516-
artObjAttributes->setLinkOrder(metadata::attributes::getAOLinkOrderName(
517-
maintainLinkOrder
518-
? metadata::attributes::ArticulatedObjectLinkOrder::URDFOrder
519-
: metadata::attributes::ArticulatedObjectLinkOrder::TreeTraversal));
520-
521-
return addArticulatedObject(artObjAttributes, drawables, forceReload,
522-
lightSetup);
527+
//} // PhysicsManager::addArticulatedObjectFromURDF
523528

524529
} // PhysicsManager::addArticulatedObjectFromURDF
525530

src/esp/physics/PhysicsManager.h

-39
Original file line numberDiff line numberDiff line change
@@ -576,45 +576,6 @@ class PhysicsManager : public std::enable_shared_from_this<PhysicsManager> {
576576
bool intertiaFromURDF = false,
577577
const std::string& lightSetup = DEFAULT_LIGHTING_KEY);
578578

579-
/**
580-
* @brief Load, parse, and import a URDF file instantiating an @ref
581-
* ArticulatedObject in the world.
582-
*
583-
* Not implemented in base PhysicsManager.
584-
* @param filepath The fully-qualified filename for the URDF file describing
585-
* the model the articulated object is to be built from.
586-
* @param drawables Reference to the scene graph drawables group to enable
587-
* rendering of the newly initialized @ref ArticulatedObject.
588-
* @param fixedBase Whether the base of the @ref ArticulatedObject should be
589-
* fixed.
590-
* @param globalScale A scale multiplier to be applied uniformly in 3
591-
* dimensions to the entire @ref ArticulatedObject.
592-
* @param massScale A scale multiplier to be applied to the mass of the all
593-
* the components of the @ref ArticulatedObject.
594-
* @param forceReload If true, reload the source URDF from file, replacing the
595-
* cached model.
596-
* @param maintainLinkOrder If true, maintain the order of link definitions
597-
* from the URDF file as the link indices.
598-
* @param intertiaFromURDF If true, load the link inertia matrices from the
599-
* URDF file instead of computing automatically from collision shapes.
600-
* @param lightSetup The string name of the desired lighting setup to use.
601-
*
602-
* @return The instanced @ref ArticulatedObject 's ID, mapping to the articulated
603-
* object in @ref PhysicsManager::existingObjects_ if successful, or
604-
* @ref esp::ID_UNDEFINED. These values come from the same pool used
605-
* by rigid objects.
606-
*/
607-
int addArticulatedObjectFromURDF(
608-
const std::string& filepath,
609-
DrawableGroup* drawables,
610-
bool fixedBase = false,
611-
float globalScale = 1.0,
612-
float massScale = 1.0,
613-
bool forceReload = false,
614-
bool maintainLinkOrder = false,
615-
bool intertiaFromURDF = false,
616-
const std::string& lightSetup = DEFAULT_LIGHTING_KEY);
617-
618579
//! Remove an @ref ArticulatedObject from the world by unique id.
619580
virtual void removeArticulatedObject(int objectId);
620581

0 commit comments

Comments
 (0)