shapes.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Point Cloud Library (PCL) - www.pointclouds.org
00005  *  Copyright (c) 2010, Willow Garage, Inc.
00006  *  Copyright (c) 2012-, Open Perception, Inc.
00007  *
00008  *  All rights reserved.
00009  *
00010  *  Redistribution and use in source and binary forms, with or without
00011  *  modification, are permitted provided that the following conditions
00012  *  are met:
00013  *
00014  *   * Redistributions of source code must retain the above copyright
00015  *     notice, this list of conditions and the following disclaimer.
00016  *   * Redistributions in binary form must reproduce the above
00017  *     copyright notice, this list of conditions and the following
00018  *     disclaimer in the documentation and/or other materials provided
00019  *     with the distribution.
00020  *   * Neither the name of Willow Garage, Inc. nor the names of its
00021  *     contributors may be used to endorse or promote products derived
00022  *     from this software without specific prior written permission.
00023  *
00024  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00025  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00026  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00027  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00028  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00029  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00030  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00031  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00033  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00034  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00035  *  POSSIBILITY OF SUCH DAMAGE.
00036  *
00037  */
00038 #include <pcl/visualization/common/shapes.h>
00039 #include <pcl/common/angles.h>
00040 #include <vtkLineSource.h>
00041 #include <vtkTubeFilter.h>
00042 #include <vtkConeSource.h>
00043 #include <vtkTransformPolyDataFilter.h>
00044 #include <vtkTransform.h>
00045 #include <vtkSphereSource.h>
00046 #include <vtkDiskSource.h>
00047 #include <vtkPlaneSource.h>
00048 #include <vtkCubeSource.h>
00049 
00051 vtkSmartPointer<vtkDataSet> 
00052 pcl::visualization::createCylinder (const pcl::ModelCoefficients &coefficients, int numsides)
00053 {
00054   vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New ();
00055   line->SetPoint1 (coefficients.values[0], coefficients.values[1], coefficients.values[2]);
00056   line->SetPoint2 (coefficients.values[3]+coefficients.values[0], coefficients.values[4]+coefficients.values[1], coefficients.values[5]+coefficients.values[2]);
00057 
00058   vtkSmartPointer<vtkTubeFilter> tuber = vtkSmartPointer<vtkTubeFilter>::New ();
00059   tuber->SetInputConnection (line->GetOutputPort ());
00060   tuber->SetRadius (coefficients.values[6]);
00061   tuber->SetNumberOfSides (numsides);
00062 
00063   return (tuber->GetOutput ());
00064 }
00065 
00067 vtkSmartPointer<vtkDataSet> 
00068 pcl::visualization::createSphere (const pcl::ModelCoefficients &coefficients, int res)
00069 {
00070   // Set the sphere origin
00071   vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New ();
00072   t->Identity (); t->Translate (coefficients.values[0], coefficients.values[1], coefficients.values[2]);
00073 
00074   vtkSmartPointer<vtkSphereSource> s_sphere = vtkSmartPointer<vtkSphereSource>::New ();
00075   s_sphere->SetRadius (coefficients.values[3]);
00076   s_sphere->SetPhiResolution (res);
00077   s_sphere->SetThetaResolution (res);
00078   s_sphere->LatLongTessellationOff ();
00079   
00080   vtkSmartPointer<vtkTransformPolyDataFilter> tf = vtkSmartPointer<vtkTransformPolyDataFilter>::New ();
00081   tf->SetTransform (t);
00082   tf->SetInputConnection (s_sphere->GetOutputPort ());
00083 
00084   return (tf->GetOutput ());
00085 }
00086 
00088 vtkSmartPointer<vtkDataSet> 
00089 pcl::visualization::createCube (const pcl::ModelCoefficients &coefficients)
00090 {
00091   // coefficients = [Tx, Ty, Tz, Qx, Qy, Qz, Qw, width, height, depth]
00092   vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New ();
00093   t->Identity ();
00094   t->Translate (coefficients.values[0], coefficients.values[1], coefficients.values[2]);
00095   
00096   Eigen::AngleAxisf a (Eigen::Quaternionf (coefficients.values[6], coefficients.values[3],
00097                                            coefficients.values[4], coefficients.values[5]));
00098   t->RotateWXYZ (pcl::rad2deg (a.angle ()), a.axis ()[0], a.axis ()[1], a.axis ()[2]);
00099   
00100   vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New ();
00101   cube->SetXLength (coefficients.values[7]);
00102   cube->SetYLength (coefficients.values[8]);
00103   cube->SetZLength (coefficients.values[9]);
00104   
00105   vtkSmartPointer<vtkTransformPolyDataFilter> tf = vtkSmartPointer<vtkTransformPolyDataFilter>::New ();
00106   tf->SetTransform (t);
00107   tf->SetInputConnection (cube->GetOutputPort ());
00108   
00109   return (tf->GetOutput ());
00110 }
00111 
00113 vtkSmartPointer<vtkDataSet> 
00114 pcl::visualization::createCube (const Eigen::Vector3f &translation, const Eigen::Quaternionf &rotation,
00115                                 double width, double height, double depth)
00116 {
00117   // coefficients = [Tx, Ty, Tz, Qx, Qy, Qz, Qw, width, height, depth]
00118   vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New ();
00119   t->Identity ();
00120   t->Translate (translation.x (), translation.y (), translation.z ());
00121   
00122   Eigen::AngleAxisf a (rotation);
00123   t->RotateWXYZ (pcl::rad2deg (a.angle ()), a.axis ()[0], a.axis ()[1], a.axis ()[2]);
00124   
00125   vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New ();
00126   cube->SetXLength (width);
00127   cube->SetYLength (height);
00128   cube->SetZLength (depth);
00129   
00130   vtkSmartPointer<vtkTransformPolyDataFilter> tf = vtkSmartPointer<vtkTransformPolyDataFilter>::New ();
00131   tf->SetTransform (t);
00132   tf->SetInputConnection (cube->GetOutputPort ());
00133   
00134   return (tf->GetOutput ());
00135 }
00136 
00138 vtkSmartPointer<vtkDataSet> 
00139 pcl::visualization::createCube (double x_min, double x_max,
00140                                 double y_min, double y_max,
00141                                 double z_min, double z_max)
00142 {
00143   vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New ();
00144   
00145   vtkSmartPointer<vtkCubeSource> cube = vtkSmartPointer<vtkCubeSource>::New ();
00146   cube->SetBounds (x_min, x_max, y_min, y_max, z_min, z_max);
00147 
00148   return (cube->GetOutput ());
00149 }
00150 
00152 vtkSmartPointer<vtkDataSet> 
00153 pcl::visualization::createLine (const pcl::ModelCoefficients &coefficients)
00154 {
00155   vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New ();
00156   line->SetPoint1 (coefficients.values[0], coefficients.values[1], coefficients.values[2]);
00157   line->SetPoint2 (coefficients.values[3] + coefficients.values[0], 
00158                    coefficients.values[4] + coefficients.values[1], 
00159                    coefficients.values[5] + coefficients.values[2]);
00160   line->Update ();
00161 
00162   return (line->GetOutput ());
00163 }
00164 
00166 vtkSmartPointer<vtkDataSet> 
00167 pcl::visualization::createPlane (const pcl::ModelCoefficients &coefficients)
00168 {
00169   vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New ();
00170   plane->SetNormal (coefficients.values[0], coefficients.values[1], coefficients.values[2]);
00171 
00172   double norm_sqr = coefficients.values[0] * coefficients.values[0]
00173                   + coefficients.values[1] * coefficients.values[1]
00174                   + coefficients.values[2] * coefficients.values[2];
00175 
00176   plane->Push (-coefficients.values[3] / sqrt(norm_sqr));
00177   return (plane->GetOutput ());
00178 }
00179 
00181 vtkSmartPointer<vtkDataSet> 
00182 pcl::visualization::createPlane (const pcl::ModelCoefficients &coefficients, double x, double y, double z)
00183 {
00184   vtkSmartPointer<vtkPlaneSource> plane = vtkSmartPointer<vtkPlaneSource>::New ();
00185   
00186 
00187   double norm_sqr = 1.0 / (coefficients.values[0] * coefficients.values[0] +
00188                            coefficients.values[1] * coefficients.values[1] +
00189                            coefficients.values[2] * coefficients.values[2] );
00190 
00191 //  double nx = coefficients.values [0] * norm;
00192 //  double ny = coefficients.values [1] * norm;
00193 //  double nz = coefficients.values [2] * norm;
00194 //  double d  = coefficients.values [3] * norm;
00195   
00196 //  plane->SetNormal (nx, ny, nz);
00197   plane->SetNormal (coefficients.values[0], coefficients.values[1], coefficients.values[2]);
00198 
00199   double t = x * coefficients.values[0] + y * coefficients.values[1] + z * coefficients.values[2] + coefficients.values[3];
00200   x -= coefficients.values[0] * t * norm_sqr;
00201   y -= coefficients.values[1] * t * norm_sqr;
00202   z -= coefficients.values[2] * t * norm_sqr;
00203   plane->SetCenter (x, y, z);
00204   
00205   return (plane->GetOutput ());
00206 }
00207 
00208 
00210 vtkSmartPointer<vtkDataSet> 
00211 pcl::visualization::create2DCircle (const pcl::ModelCoefficients &coefficients, double z)
00212 {
00213   vtkSmartPointer<vtkDiskSource> disk = vtkSmartPointer<vtkDiskSource>::New ();
00214   // Maybe the resolution should be lower e.g. 50 or 25 
00215   disk->SetCircumferentialResolution (100);
00216   disk->SetInnerRadius (coefficients.values[2] - 0.001);
00217   disk->SetOuterRadius (coefficients.values[2] + 0.001);
00218   disk->SetCircumferentialResolution (20);
00219 
00220   // An alternative to <vtkDiskSource> could be <vtkRegularPolygonSource> with <vtkTubeFilter>
00221   /*
00222   vtkSmartPointer<vtkRegularPolygonSource> circle = vtkSmartPointer<vtkRegularPolygonSource>::New();
00223   circle->SetRadius (coefficients.values[2]);
00224   circle->SetNumberOfSides (100);
00225   
00226   vtkSmartPointer<vtkTubeFilter> tube = vtkSmartPointer<vtkTubeFilter>::New();
00227   tube->SetInput (circle->GetOutput());
00228   tube->SetNumberOfSides (25);
00229   tube->SetRadius (0.001);
00230   */ 
00231 
00232   // Set the circle origin
00233   vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New ();
00234   t->Identity (); 
00235   t->Translate (coefficients.values[0], coefficients.values[1], z);
00236 
00237   vtkSmartPointer<vtkTransformPolyDataFilter> tf = vtkSmartPointer<vtkTransformPolyDataFilter>::New ();
00238   tf->SetTransform (t);
00239   tf->SetInputConnection (disk->GetOutputPort ());
00240   /*
00241   tf->SetInputConnection (tube->GetOutputPort ());
00242   */
00243 
00244   return (tf->GetOutput ());
00245 }
00246 
00248 vtkSmartPointer<vtkDataSet> 
00249 pcl::visualization::createCone (const pcl::ModelCoefficients &coefficients)
00250 {
00251   vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New ();
00252   cone->SetHeight (1.0);
00253   cone->SetCenter (coefficients.values[0] + coefficients.values[3] * 0.5, 
00254                    coefficients.values[1] + coefficients.values[1] * 0.5,
00255                    coefficients.values[2] + coefficients.values[2] * 0.5);
00256   cone->SetDirection (-coefficients.values[3], -coefficients.values[4], -coefficients.values[5]);
00257   cone->SetResolution (100);
00258   cone->SetAngle (coefficients.values[6]);
00259 
00260   return (cone->GetOutput ());
00261 }
00262 
00264 vtkSmartPointer<vtkDataSet> 
00265 pcl::visualization::createSphere (const Eigen::Vector4f &center, double radius, int res)
00266 {
00267   // Set the sphere origin
00268   vtkSmartPointer<vtkTransform> t = vtkSmartPointer<vtkTransform>::New ();
00269   t->Identity (); 
00270   t->Translate (center[0], center[1], center[2]);
00271 
00272   vtkSmartPointer<vtkSphereSource> s_sphere = vtkSmartPointer<vtkSphereSource>::New ();
00273   s_sphere->SetRadius (radius);
00274   s_sphere->SetPhiResolution (res);
00275   s_sphere->SetThetaResolution (res);
00276   s_sphere->LatLongTessellationOff ();
00277   
00278   vtkSmartPointer<vtkTransformPolyDataFilter> tf = vtkSmartPointer<vtkTransformPolyDataFilter>::New ();
00279   tf->SetTransform (t);
00280   tf->SetInputConnection (s_sphere->GetOutputPort ());
00281   tf->Update ();
00282 
00283   return (tf->GetOutput ());
00284 }
00285 
00287 vtkSmartPointer<vtkDataSet>
00288 pcl::visualization::createLine (const Eigen::Vector4f &pt1, const Eigen::Vector4f &pt2)
00289 {
00290   vtkSmartPointer<vtkLineSource> line = vtkSmartPointer<vtkLineSource>::New ();
00291   line->SetPoint1 (pt1.x (), pt1.y (), pt1.z ());
00292   line->SetPoint2 (pt2.x (), pt2.y (), pt2.z ());
00293   line->Update ();
00294 
00295   return (line->GetOutput ());
00296 }
00298 void
00299 pcl::visualization::allocVtkUnstructuredGrid (vtkSmartPointer<vtkUnstructuredGrid> &polydata)
00300 {
00301   polydata = vtkSmartPointer<vtkUnstructuredGrid>::New ();
00302 }
00303 
00304 


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:33:37