00001 /* 00002 * Copyright (c) 2011, Willow Garage, Inc. 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * 00008 * * Redistributions of source code must retain the above copyright 00009 * notice, this list of conditions and the following disclaimer. 00010 * * Redistributions in binary form must reproduce the above copyright 00011 * notice, this list of conditions and the following disclaimer in the 00012 * documentation and/or other materials provided with the distribution. 00013 * * Neither the name of the Willow Garage, Inc. nor the names of its 00014 * contributors may be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00018 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00019 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00020 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00021 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00022 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00023 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00024 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00025 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00026 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00027 * POSSIBILITY OF SUCH DAMAGE. 00028 */ 00029 00030 #include "rviz_interaction_tools/mesh_object_switcher.h" 00031 #include "rviz_interaction_tools/mesh_object.h" 00032 00033 #include <OGRE/OgreMaterialManager.h> 00034 #include <OGRE/OgreTechnique.h> 00035 #include <OGRE/OgrePass.h> 00036 #include <OGRE/OgreEdgeListBuilder.h> 00037 00038 #include <ros/ros.h> 00039 00040 namespace rviz_interaction_tools 00041 { 00042 00043 MeshObjectSwitcher::MeshObjectSwitcher( std::string valid_mat_name, 00044 std::string valid_sel_mat_name, 00045 std::string invalid_mat_name, 00046 std::string invalid_sel_mat_name ) : 00047 visible_mesh_(-1), 00048 valid_(true), 00049 selected_(false), 00050 valid_mat_name_(valid_mat_name), 00051 valid_sel_mat_name_(valid_sel_mat_name), 00052 invalid_mat_name_(invalid_mat_name), 00053 invalid_sel_mat_name_(invalid_sel_mat_name) 00054 { 00055 } 00056 00057 00058 MeshObjectSwitcher::~MeshObjectSwitcher() 00059 { 00060 } 00061 00062 00063 void MeshObjectSwitcher::addObject( MeshObject* mesh_object ) 00064 { 00065 mesh_object->setVisible(false); 00066 mesh_objects_.push_back( boost::shared_ptr<MeshObject>( mesh_object ) ); 00067 updateMaterials(); 00068 } 00069 00070 00071 void MeshObjectSwitcher::setSelected( bool selected ) 00072 { 00073 selected_ = selected; 00074 updateMaterials(); 00075 } 00076 00077 void MeshObjectSwitcher::setValid( bool valid ) 00078 { 00079 valid_ = valid; 00080 updateMaterials(); 00081 } 00082 00083 00084 void MeshObjectSwitcher::setVisible( unsigned index ) 00085 { 00086 if ( visible_mesh_ < mesh_objects_.size() ) 00087 { 00088 ROS_INFO_STREAM( "Hiding mesh " << visible_mesh_ ); 00089 mesh_objects_[visible_mesh_]->setVisible(false); 00090 } 00091 00092 if ( index < mesh_objects_.size() ) 00093 { 00094 ROS_INFO_STREAM( "Showing mesh " << index << "." 00095 << " Entity: " << mesh_objects_[index]->getEntity()->getName() 00096 << " Mesh: " << mesh_objects_[index]->getEntity()->getMesh()->getName() 00097 << " Triangles: " << mesh_objects_[index]->getEntity()->getMesh()->getEdgeList()->triangles.size() 00098 ); 00099 mesh_objects_[index]->setVisible(true); 00100 } 00101 00102 visible_mesh_ = index; 00103 } 00104 00105 void MeshObjectSwitcher::next() 00106 { 00107 setVisible( (visible_mesh_ + 1) % mesh_objects_.size() ); 00108 } 00109 00110 void MeshObjectSwitcher::updateMaterials() 00111 { 00112 for ( unsigned i=0; i<mesh_objects_.size(); ++i ) 00113 { 00114 if ( valid_ && selected_ ) 00115 { 00116 mesh_objects_[i]->setMaterialName( valid_sel_mat_name_ ); 00117 } 00118 else if ( valid_ ) 00119 { 00120 mesh_objects_[i]->setMaterialName( valid_mat_name_ ); 00121 } 00122 else if ( selected_ ) 00123 { 00124 mesh_objects_[i]->setMaterialName( invalid_sel_mat_name_ ); 00125 } 00126 else 00127 { 00128 mesh_objects_[i]->setMaterialName( invalid_mat_name_ ); 00129 } 00130 } 00131 } 00132 00133 }