00001 /*!@file Image/Point3D.H A basic 3D point class */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2001 by the // 00005 // University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Lior Elazary 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Image/Point3D.H $ 00035 // $Id: Point3D.H 14628 2011-03-23 16:49:36Z lior $ 00036 // 00037 00038 #ifndef POINT3D_H_DEFINED 00039 #define POINT3D_H_DEFINED 00040 00041 #include "Image/Dims.H" 00042 #include "Image/Image.H" 00043 #include "Util/Promotions.H" 00044 #include "Util/log.H" 00045 #include <cmath> 00046 #include <string> // for string conversions 00047 #include <vector> 00048 00049 //! This is a basic class to encode 3D coordinates 00050 /*! This is a completely public class whose goal is just to provide a 00051 shorthand notation for 3D coordinates. 00052 */ 00053 template <class T> 00054 class Point3D 00055 { 00056 public: 00057 //! The default constructor initializes the coordinates to (0,0) 00058 inline Point3D(); 00059 00060 //! Initialize the Point3D from horizontal & vertical coordinates 00061 inline Point3D(const T xx, const T yy, const T zz); 00062 00063 //! Initialize the Point3D from an image 00064 inline Point3D(const Image<T>& mat); 00065 00066 //! Explicit conversion from type T to another type U 00067 /*! Note that this simply uses clamped_convert, so it will clamp 00068 coordinates to the available range of T, and may round down. */ 00069 template <class U> 00070 explicit inline Point3D(const Point3D<U>& a); 00071 00072 00073 00074 //! += operator 00075 inline Point3D<T>& operator+=(const Point3D<T> &p); 00076 //! -= operator 00077 inline Point3D<T>& operator-=(const Point3D<T> &p); 00078 //! *= operator 00079 inline Point3D<T>& operator*=(const Point3D<T> &p); 00080 //! /= operator 00081 inline Point3D<T>& operator/=(const Point3D<T> &p); 00082 00083 //! + operator 00084 template <class U> 00085 inline Point3D<typename promote_trait<T,U>::TP> 00086 operator+(const Point3D<U> &p) const; 00087 //! - operator 00088 template <class U> 00089 inline Point3D<typename promote_trait<T,U>::TP> 00090 operator-(const Point3D<U> &p) const; 00091 //! * operator 00092 template <class U> 00093 inline Point3D<typename promote_trait<T,U>::TP> 00094 operator*(const Point3D<U> &p) const; 00095 //! / operator 00096 template <class U> 00097 inline Point3D<typename promote_trait<T,U>::TP> 00098 operator/(const Point3D<U> &p) const; 00099 00100 //! += operator 00101 inline Point3D<T>& operator+=(const T val); 00102 //! -= operator 00103 inline Point3D<T>& operator-=(const T val); 00104 //! *= operator 00105 inline Point3D<T>& operator*=(const T val); 00106 //! /= operator 00107 inline Point3D<T>& operator/=(const T val); 00108 00109 //! + operator 00110 template <class U> 00111 inline Point3D<typename promote_trait<T,U>::TP> operator+(const U val) const; 00112 00113 //! - operator 00114 template <class U> 00115 inline Point3D<typename promote_trait<T,U>::TP> operator-(const U val) const; 00116 00117 //! * operator 00118 template <class U> 00119 inline Point3D<typename promote_trait<T,U>::TP> operator*(const U val) const; 00120 00121 //! / operator 00122 template <class U> 00123 inline Point3D<typename promote_trait<T,U>::TP> operator/(const U val) const; 00124 00125 //! test whether x and y and z are all positive 00126 inline bool isValid() const; 00127 00128 //! the square of the euclidean distance 00129 inline typename promote_trait<T,float>::TP 00130 squdist(const Point3D<T>& p) const; 00131 00132 //! the euclidean distance from p 00133 inline typename promote_trait<T,float>::TP 00134 distance(const Point3D<T>& p) const; 00135 00136 //! the magnitude 00137 inline typename promote_trait<T,float>::TP 00138 magnitude() const; 00139 00140 //! Get an image representation used for matrix operations [x,y,z] 00141 inline Image<T> getImage(); 00142 00143 //! 3D coordinates 00144 T x, y, z; 00145 }; 00146 00147 00148 //! == operator 00149 template <class T, class U> 00150 inline bool operator==(const Point3D<T>& p1, const Point3D<U>& p2); 00151 00152 //! != operator 00153 template <class T, class U> 00154 inline bool operator!=(const Point3D<T>& p1, const Point3D<U>& p2); 00155 00156 //! > operator 00157 template <class T, class U> 00158 inline bool operator>(const Point3D<T>& p1, const Point3D<U>& p2); 00159 00160 //! < operator 00161 template <class T, class U> 00162 inline bool operator<(const Point3D<T>& p1, const Point3D<U>& p2); 00163 00164 00165 //! Point3D<T> overload: format is "<i>,<j>" 00166 template <class T> 00167 std::string convertToString(const Point3D<T>& val); 00168 00169 //! Point3D<T> overload: format is "<i>,<j>" 00170 template <class T> 00171 void convertFromString(const std::string& str, Point3D<T>& val); 00172 00173 // ###################################################################### 00174 template <class T> 00175 inline Point3D<T>::Point3D() 00176 { z = 0; y = 0; z = 0;} 00177 00178 // ###################################################################### 00179 template <class T> 00180 inline Point3D<T>::Point3D(const T xx, const T yy, const T zz) 00181 { x = xx; y = yy; z= zz;} 00182 00183 // ###################################################################### 00184 template <class T> 00185 inline Point3D<T>::Point3D(const Image<T>& mat) 00186 { 00187 ASSERT(mat.size() == 3); 00188 x = mat[0]; y = mat[1]; z= mat[2]; 00189 } 00190 00191 // ####################################################################### 00192 template <class T> 00193 template <class U> 00194 inline Point3D<T>::Point3D(const Point3D<U>& a) 00195 : x(clamped_convert<T>(a.x)), y(clamped_convert<T>(a.y)), z(clamped_convert<T>(a.z)) 00196 { } 00197 00198 // ###################################################################### 00199 template <class T> 00200 inline Point3D<T>& Point3D<T>::operator+=(const Point3D<T> &p) 00201 { x += p.x; y += p.y; z += p.z; return *this; } 00202 00203 // ###################################################################### 00204 template <class T> 00205 inline Point3D<T>& Point3D<T>::operator-=(const Point3D<T> &p) 00206 { x -= p.x; y -= p.y; z -= p.z; return *this; } 00207 00208 // ###################################################################### 00209 template <class T> 00210 inline Point3D<T>& Point3D<T>::operator*=(const Point3D<T> &p) 00211 { x *= p.x; y *= p.y; z *= p.z; return *this; } 00212 00213 // ###################################################################### 00214 template <class T> 00215 inline Point3D<T>& Point3D<T>::operator/=(const Point3D<T> &p) 00216 { x /= p.x; y /= p.y; z /- p.z; return *this; } 00217 00218 // ###################################################################### 00219 template <class T> 00220 template <class U> 00221 inline Point3D<typename promote_trait<T,U>::TP> 00222 Point3D<T>::operator+(const Point3D<U> &p) const 00223 { return Point3D<typename promote_trait<T,U>::TP>(x + p.x, y + p.y, z + p.z); } 00224 00225 // ###################################################################### 00226 template <class T> 00227 template <class U> 00228 inline Point3D<typename promote_trait<T,U>::TP> 00229 Point3D<T>::operator-(const Point3D<U> &p) const 00230 { return Point3D<typename promote_trait<T,U>::TP>(x - p.x, y - p.y, z - p.z); } 00231 00232 // ###################################################################### 00233 template <class T> 00234 template <class U> 00235 inline Point3D<typename promote_trait<T,U>::TP> 00236 Point3D<T>::operator*(const Point3D<U> &p) const 00237 { return Point3D<typename promote_trait<T,U>::TP>(x * p.x, y * p.y, z * p.z); } 00238 00239 // ###################################################################### 00240 template <class T> 00241 template <class U> 00242 inline Point3D<typename promote_trait<T,U>::TP> 00243 Point3D<T>::operator/(const Point3D<U> &p) const 00244 { return Point3D<typename promote_trait<T,U>::TP>(x / p.x, y / p.y, z / p.z); } 00245 00246 // ###################################################################### 00247 template <class T> 00248 inline Point3D<T>& Point3D<T>::operator+=(const T val) 00249 { x += val; y += val; z += val; return *this; } 00250 00251 // ###################################################################### 00252 template <class T> 00253 inline Point3D<T>& Point3D<T>::operator-=(const T val) 00254 { x -= val; y -= val; z -= val; return *this; } 00255 00256 // ###################################################################### 00257 template <class T> 00258 inline Point3D<T>& Point3D<T>::operator*=(const T val) 00259 { x *= val; y *= val; z *= val; return *this; } 00260 00261 // ###################################################################### 00262 template <class T> 00263 inline Point3D<T>& Point3D<T>::operator/=(const T val) 00264 { x /= val; y /= val; z /= val; return *this; } 00265 00266 // ###################################################################### 00267 template <class T> 00268 template <class U> 00269 inline Point3D<typename promote_trait<T,U>::TP> 00270 Point3D<T>::operator+(const U val) const 00271 { return Point3D<typename promote_trait<T,U>::TP>(this->x+val, this->y+val, this->z+val); } 00272 00273 // ###################################################################### 00274 template <class T> 00275 template <class U> 00276 inline Point3D<typename promote_trait<T,U>::TP> 00277 Point3D<T>::operator-(const U val) const 00278 { return Point3D<typename promote_trait<T,U>::TP>(this->x-val, this->y-val, this->z-val); } 00279 00280 // ###################################################################### 00281 template <class T> 00282 template <class U> 00283 inline Point3D<typename promote_trait<T,U>::TP> 00284 Point3D<T>::operator*(const U val) const 00285 { return Point3D<typename promote_trait<T,U>::TP>(this->x*val, this->y*val, this->z*val); } 00286 00287 // ###################################################################### 00288 template <class T> 00289 template <class U> 00290 inline Point3D<typename promote_trait<T,U>::TP> 00291 Point3D<T>::operator/(const U val) const 00292 { return Point3D<typename promote_trait<T,U>::TP>(this->x/val, this->y/val, this->z/val); } 00293 00294 // ###################################################################### 00295 template <class T, class U> 00296 inline bool operator==(const Point3D<T>& p1, const Point3D<U>& p2) 00297 { return p1.x == p2.x && p1.y == p2.y && p1.z == p2.z; } 00298 00299 // ###################################################################### 00300 template <class T, class U> 00301 inline bool operator!=(const Point3D<T>& p1, const Point3D<U>& p2) 00302 { return p1.x != p2.x || p1.y != p2.y || p1.z != p2.z; } 00303 00304 // ###################################################################### 00305 template <class T, class U> 00306 inline bool operator>(const Point3D<T>& p1, const Point3D<U>& p2) 00307 { return p1.x > p2.x && p1.y > p2.y && p1.z > p2.z; } 00308 00309 // ###################################################################### 00310 template <class T, class U> 00311 inline bool operator<(const Point3D<T>& p1, const Point3D<U>& p2) 00312 { return p1.x < p2.x && p1.y < p2.y && p1.z < p2.z; } 00313 00314 // ###################################################################### 00315 template <class T> 00316 inline bool Point3D<T>::isValid() const 00317 { return ((x >= 0) && (y >= 0) && (z >= 0)); } 00318 00319 // ###################################################################### 00320 template <class T> 00321 inline typename promote_trait<T,float>::TP 00322 Point3D<T>::squdist(const Point3D<T>& p) const 00323 { 00324 typedef typename promote_trait<T,float>::TP TF; 00325 TF d1 = p.x - x, d2 = p.y - y, d3 = p.z - z; 00326 return (d1 * d1 + d2 * d2 + d3 * d3); 00327 } 00328 00329 // ###################################################################### 00330 template <class T> 00331 inline typename promote_trait<T,float>::TP 00332 Point3D<T>::distance(const Point3D<T>& p) const 00333 { return sqrt(squdist(p)); } 00334 00335 // ###################################################################### 00336 template <class T> 00337 inline typename promote_trait<T,float>::TP 00338 Point3D<T>::magnitude() const 00339 { return sqrt((x*x) + (y*y) + (z*z)); } 00340 00341 00342 template <class T> 00343 inline Image<T> Point3D<T>::getImage() 00344 { 00345 Image<T> mat(1,3,NO_INIT); 00346 mat[0] = x; 00347 mat[1] = y; 00348 mat[2] = z; 00349 00350 return mat; 00351 } 00352 // ###################################################################### 00353 /* So things look consistent in everyone's emacs... */ 00354 /* Local Variables: */ 00355 /* indent-tabs-mode: nil */ 00356 /* End: */ 00357 00358 #endif