00001 /** 00002 \file Robots/LoBot/util/LoMath.C 00003 \brief Math functions. 00004 */ 00005 00006 // //////////////////////////////////////////////////////////////////// // 00007 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00008 // by the University of Southern California (USC) and the iLab at USC. // 00009 // See http://iLab.usc.edu for information about this project. // 00010 // //////////////////////////////////////////////////////////////////// // 00011 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00012 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00013 // in Visual Environments, and Applications'' by Christof Koch and // 00014 // Laurent Itti, California Institute of Technology, 2001 (patent // 00015 // pending; application number 09/912,225 filed July 23, 2001; see // 00016 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00017 // //////////////////////////////////////////////////////////////////// // 00018 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00019 // // 00020 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00021 // redistribute it and/or modify it under the terms of the GNU General // 00022 // Public License as published by the Free Software Foundation; either // 00023 // version 2 of the License, or (at your option) any later version. // 00024 // // 00025 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00026 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00027 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00028 // PURPOSE. See the GNU General Public License for more details. // 00029 // // 00030 // You should have received a copy of the GNU General Public License // 00031 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00032 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00033 // Boston, MA 02111-1307 USA. // 00034 // //////////////////////////////////////////////////////////////////// // 00035 // 00036 // Primary maintainer for this file: mviswana usc edu 00037 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Robots/LoBot/util/LoMath.C $ 00038 // $Id: LoMath.C 13628 2010-06-28 23:48:02Z mviswana $ 00039 // 00040 00041 //------------------------------ HEADERS -------------------------------- 00042 00043 // lobot headers 00044 #include "Robots/LoBot/util/LoMath.H" 00045 00046 //----------------------------- NAMESPACE ------------------------------- 00047 00048 namespace lobot { 00049 00050 //---------------------------- BASIC STUFF ------------------------------ 00051 00052 // Returns the next power of two >= given number 00053 int next_power_of_two(int n) 00054 { 00055 const int m = abs(n) ; 00056 00057 int p = 1 ; 00058 while (p < m) 00059 p <<= 1 ; 00060 00061 return (n < 0) ? -p : p ; 00062 } 00063 00064 //-------------------------- FLOATING POINT ----------------------------- 00065 00066 // Round a floating-point number to the nearest integer 00067 int round(float t) 00068 { 00069 float rounder = (t < 0) ? -0.5f : 0.5f ; 00070 return static_cast<int>(t + rounder) ; 00071 } 00072 00073 //------------------------------ ANGLES --------------------------------- 00074 00075 // Clamp an angle (specified in degrees) to lie in [0, 360] 00076 float clamp_angle(float angle) 00077 { 00078 float A = angle - 360 * static_cast<int>(angle/360) ; 00079 return (A < 0) ? 360 + A : A ; 00080 } 00081 00082 //-------------------------- RANDOM NUMBERS ----------------------------- 00083 00084 // Return a random number in the specified range 00085 int random(int min, int max) 00086 { 00087 float n = static_cast<float>(std::rand())/RAND_MAX ; 00088 return static_cast<int>(min + n * (max - min)) ; 00089 } 00090 00091 // Return a random number in the specified range 00092 float randomf(float min, float max) 00093 { 00094 float n = static_cast<float>(std::rand())/RAND_MAX ; 00095 return min + n * (max - min) ; 00096 } 00097 00098 //-------------------- LOGARITHMS AND EXPONENTIALS ---------------------- 00099 00100 // Convert a [0,1] probability to its equivalent log-odds form 00101 float prob_to_log_odds(float p) 00102 { 00103 return ln(p/(1 - p)) ; 00104 } 00105 00106 // Convert log-odds likelihood to its equivalent [0,1] probability 00107 float log_odds_to_prob(float log_odds) 00108 { 00109 return 1/(1 + exp(-log_odds)) ; 00110 } 00111 00112 //--------------------------- MISCELLANEOUS ----------------------------- 00113 00114 // Evaluate Gaussian 00115 float gaussian(float x, float mu, float sigma) 00116 { 00117 const float sigma_inv = 1/sigma ; 00118 return 0.39894228f * sigma_inv * exp(-0.5f * sqr((x - mu) * sigma_inv)) ; 00119 } 00120 00121 // Return a random number sampled from a triangular distribution centered 00122 // at b. 00123 // 00124 // DEVNOTE: See page 124 of "Probabilistic Robotics" by Thrun, Burgard 00125 // and Fox. 00126 float sample_tri(float b) 00127 { 00128 return /*sqrt(6)/2*/ 1.22474487f * (randomf(-b, b) + randomf(-b, b)) ; 00129 } 00130 00131 //----------------------------------------------------------------------- 00132 00133 } // end of namespace encapsulating this file's definitions 00134 00135 /* So things look consistent in everyone's emacs... */ 00136 /* Local Variables: */ 00137 /* indent-tabs-mode: nil */ 00138 /* End: */