common.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id: common.cpp 6161 2012-07-05 17:37:29Z rusu $
00035  *
00036  */
00037 
00038 #include <pcl/visualization/common/common.h>
00039 #include <stdlib.h>
00040 
00042 void
00043 pcl::visualization::getRandomColors (double &r, double &g, double &b, double min, double max)
00044 {
00045   double sum;
00046   static unsigned stepRGBA = 100;
00047   do
00048   {
00049     sum = 0;
00050     r = (rand () % stepRGBA) / static_cast<double> (stepRGBA);
00051     while ((g = (rand () % stepRGBA) / static_cast<double> (stepRGBA)) == r) {}
00052     while (((b = (rand () % stepRGBA) / static_cast<double> (stepRGBA)) == r) && (b == g)) {}
00053     sum = r + g + b;
00054   }
00055   while (sum <= min || sum >= max);
00056 }
00057 
00059 
00060 void
00061 pcl::visualization::Camera::computeViewMatrix(Eigen::Matrix4d& view_mat) const
00062 {
00063 //constructs view matrix from camera pos, view up, and the point it is looking at
00064 //this code is based off of gluLookAt http://www.opengl.org/wiki/GluLookAt_code
00065         Eigen::Vector3d focal_point (focal[0], focal[1], focal[2]);
00066         Eigen::Vector3d posv        (pos[0]  , pos[1]  , pos[2]);
00067         Eigen::Vector3d up          (view[0] , view[1] , view[2]);
00068 
00069         Eigen::Vector3d zAxis = (focal_point - posv).normalized();
00070   Eigen::Vector3d xAxis = zAxis.cross(up).normalized();
00071   // make sure the y-axis is orthogonal to the other two
00072   Eigen::Vector3d yAxis = xAxis.cross (zAxis);
00073 
00074         view_mat.block <1, 3> (0, 0) = xAxis;
00075         view_mat.block <1, 3> (1, 0) = yAxis;
00076         view_mat.block <1, 3> (2, 0) = -zAxis;
00077         view_mat.row (3) << 0, 0, 0, 1;
00078 
00079         view_mat.block <3, 1> (0, 3) = view_mat.topLeftCorner<3, 3> () * (-posv);
00080 }
00081 
00083 void
00084 pcl::visualization::Camera::computeProjectionMatrix (Eigen::Matrix4d& proj) const
00085 {
00086   float top    = static_cast<float> (clip[0]) * tanf (0.5f * static_cast<float> (fovy));
00087   float left   = -top * static_cast<float> (window_size[0] / window_size[1]);
00088   float right  = -left;
00089   float bottom = -top;
00090 
00091   float temp1, temp2, temp3, temp4;
00092         temp1 = 2.0f * static_cast<float> (clip[0]);
00093         temp2 = 1.0f / (right - left);
00094         temp3 = 1.0f / (top - bottom);
00095         temp4 = 1.0f / static_cast<float> (clip[1] - clip[0]);
00096 
00097   proj.setZero ();
00098 
00099         proj(0,0) = temp1 * temp2;
00100         proj(1,1) = temp1 * temp3;
00101         proj(0,2) = (right + left) * temp2;
00102         proj(1,2) = (top + bottom) * temp3;
00103         proj(2,2) = (-clip[1] - clip[0]) * temp4;
00104         proj(3,2) = -1.0;
00105         proj(2,3) = (-temp1 * clip[1]) * temp4;
00106 }


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:14:41