diff --git a/src/rviz/robot/robot.cpp b/src/rviz/robot/robot.cpp index dddb4b702a..22c1b894ae 100644 --- a/src/rviz/robot/robot.cpp +++ b/src/rviz/robot/robot.cpp @@ -705,13 +705,13 @@ void Robot::update(const LinkUpdater& updater) { RobotLink* link = link_it->second; - link->setToNormalMaterial(); - Ogre::Vector3 visual_position, collision_position; Ogre::Quaternion visual_orientation, collision_orientation; if (updater.getLinkTransforms(link->getName(), visual_position, visual_orientation, collision_position, collision_orientation)) { + link->setToNormalMaterial(); + // Check if visual_orientation, visual_position, collision_orientation, and collision_position are // NaN. if (visual_orientation.isNaN()) diff --git a/src/rviz/robot/robot_link.cpp b/src/rviz/robot/robot_link.cpp index 802b47fcdd..0c0210fa17 100644 --- a/src/rviz/robot/robot_link.cpp +++ b/src/rviz/robot/robot_link.cpp @@ -171,7 +171,7 @@ RobotLink::RobotLink(Robot* robot, , robot_alpha_(1.0) , only_render_depth_(false) , is_selectable_(true) - , using_color_(false) + , material_mode_flags_(ORIGINAL) { link_property_ = new Property(link->name.c_str(), true, "", nullptr, SLOT(updateVisibility()), this); link_property_->setIcon(rviz::loadPixmap("package://rviz/icons/classes/RobotLink.png")); @@ -900,38 +900,36 @@ void RobotLink::setTransforms(const Ogre::Vector3& visual_position, void RobotLink::setToErrorMaterial() { - for (size_t i = 0; i < visual_meshes_.size(); i++) - { - visual_meshes_[i]->setMaterialName("BaseWhiteNoLighting"); - } - for (size_t i = 0; i < collision_meshes_.size(); i++) - { - collision_meshes_[i]->setMaterialName("BaseWhiteNoLighting"); - } + setMaterialMode(material_mode_flags_ | ERROR); } void RobotLink::setToNormalMaterial() { - if (using_color_) - { - for (size_t i = 0; i < visual_meshes_.size(); i++) - { - visual_meshes_[i]->setMaterial(color_material_); - } - for (size_t i = 0; i < collision_meshes_.size(); i++) - { - collision_meshes_[i]->setMaterial(color_material_); - } - } - else + setMaterialMode(material_mode_flags_ & ~ERROR); +} + +void RobotLink::setMaterialMode(unsigned char mode_flags) +{ + if (material_mode_flags_ == mode_flags) + return; // nothing to change + + material_mode_flags_ = mode_flags; + + if (mode_flags == ORIGINAL) { - M_SubEntityToMaterial::iterator it = materials_.begin(); - M_SubEntityToMaterial::iterator end = materials_.end(); - for (; it != end; ++it) - { - it->first->setMaterial(it->second); - } + for (const auto& item : materials_) + item.first->setMaterial(item.second); + return; } + + auto error_material = Ogre::MaterialManager::getSingleton().getByName( + "BaseWhiteNoLighting", Ogre::ResourceGroupManager::INTERNAL_RESOURCE_GROUP_NAME); + auto material = mode_flags == COLOR ? color_material_ : error_material; + + for (const auto& mesh : visual_meshes_) + mesh->setMaterial(material); + for (const auto& mesh : collision_meshes_) + mesh->setMaterial(material); } void RobotLink::setColor(float red, float green, float blue) @@ -943,14 +941,12 @@ void RobotLink::setColor(float red, float green, float blue) color_material_->getTechnique(0)->setAmbient(0.5 * color); color_material_->getTechnique(0)->setDiffuse(color); - using_color_ = true; - setToNormalMaterial(); + setMaterialMode(COLOR | (material_mode_flags_ & ERROR)); } void RobotLink::unsetColor() { - using_color_ = false; - setToNormalMaterial(); + setMaterialMode(ORIGINAL | (material_mode_flags_ & ERROR)); } bool RobotLink::setSelectable(bool selectable) diff --git a/src/rviz/robot/robot_link.h b/src/rviz/robot/robot_link.h index c9090ed12a..eb10a5b751 100644 --- a/src/rviz/robot/robot_link.h +++ b/src/rviz/robot/robot_link.h @@ -84,6 +84,14 @@ typedef boost::shared_ptr RobotLinkSelectionHandlerPt class RobotLink : public QObject { Q_OBJECT + + enum MaterialMode + { + ORIGINAL = 0, + COLOR = 1, + ERROR = 2, + }; + public: RobotLink(Robot* robot, const urdf::LinkConstSharedPtr& link, @@ -182,6 +190,7 @@ private Q_SLOTS: void updateAxes(); private: + void setMaterialMode(unsigned char mode_flags); void setRenderQueueGroup(Ogre::uint8 group); bool getEnabled() const; void createEntityForGeometryElement(const urdf::LinkConstSharedPtr& link, @@ -249,7 +258,7 @@ private Q_SLOTS: RobotLinkSelectionHandlerPtr selection_handler_; Ogre::MaterialPtr color_material_; - bool using_color_; + unsigned char material_mode_flags_; friend class RobotLinkSelectionHandler; };