measure_tool.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2008, 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  * measure_tool.cpp
00031  *
00032  *  Created on: Aug 8, 2012
00033  *      Author: gossow
00034  */
00035 
00036 #include "measure_tool.h"
00037 
00038 #include "rviz/ogre_helpers/line.h"
00039 #include "rviz/viewport_mouse_event.h"
00040 #include "rviz/display_context.h"
00041 #include "rviz/selection/selection_manager.h"
00042 #include "rviz/load_resource.h"
00043 
00044 #include <OgreSceneNode.h>
00045 
00046 #include <sstream>
00047 
00048 namespace rviz
00049 {
00050 
00051 MeasureTool::MeasureTool() :
00052     state_(START),
00053     length_(-1)
00054 {
00055 }
00056 
00057 MeasureTool::~MeasureTool()
00058 {
00059   delete line_;
00060 }
00061 
00062 void MeasureTool::onInitialize()
00063 {
00064   line_ = new Line(context_->getSceneManager());
00065 
00066   std_cursor_ = getDefaultCursor();
00067   hit_cursor_ = makeIconCursor( "package://rviz/icons/crosshair.svg" );
00068 }
00069 
00070 void MeasureTool::activate()
00071 {
00072   state_ = START;
00073 }
00074 
00075 void MeasureTool::deactivate()
00076 {
00077 }
00078 
00079 int MeasureTool::processMouseEvent( ViewportMouseEvent& event )
00080 {
00081   int flags = 0;
00082 
00083   Ogre::Vector3 pos;
00084 
00085   std::stringstream ss;
00086 
00087   bool success = context_->getSelectionManager()->get3DPoint( event.viewport, event.x, event.y, pos );
00088   setCursor( success ? hit_cursor_ : std_cursor_ );
00089 
00090   switch ( state_ )
00091   {
00092     case START:
00093       break;
00094     case END:
00095       if ( success )
00096       {
00097         line_->setPoints(start_,pos);
00098         length_ = (start_-pos).length();
00099       }
00100       break;
00101   }
00102 
00103   if ( length_ > 0.0 )
00104   {
00105     ss << "[Length: " << length_ << "m] ";
00106   }
00107 
00108   ss << "Click on two points to measure their distance. Right-click to reset.";
00109   setStatus( QString( ss.str().c_str() ) );
00110 
00111   if( event.leftUp() && success )
00112   {
00113     switch ( state_ )
00114     {
00115       case START:
00116         start_ = pos;
00117         state_ = END;
00118         break;
00119       case END:
00120         end_ = pos;
00121         state_ = START;
00122         line_->setPoints(start_,end_);
00123         break;
00124     }
00125 
00126     flags |= Render;
00127   }
00128 
00129   if ( event.rightUp() )
00130   {
00131     state_ = START;
00132     line_->setVisible(false);
00133   }
00134 
00135   return flags;
00136 }
00137 
00138 } /* namespace rviz */
00139 
00140 
00141 #include <pluginlib/class_list_macros.h>
00142 PLUGINLIB_EXPORT_CLASS( rviz::MeasureTool, rviz::Tool )


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Jun 6 2019 18:02:15