last update 20 Sep 2009 |
00001 /* 00002 * Copyright (C) 1998-2004 00003 * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany 00004 * 00005 * 00006 * This file is part of the Computer Vision and Robotics Library (CVR-Lib) 00007 * 00008 * The CVR-Lib is free software; you can redistribute it and/or 00009 * modify it under the terms of the BSD License. 00010 * 00011 * All rights reserved. 00012 * 00013 * Redistribution and use in source and binary forms, with or without 00014 * modification, are permitted provided that the following conditions are met: 00015 * 00016 * 1. Redistributions of source code must retain the above copyright notice, 00017 * this list of conditions and the following disclaimer. 00018 * 00019 * 2. Redistributions in binary form must reproduce the above copyright notice, 00020 * this list of conditions and the following disclaimer in the documentation 00021 * and/or other materials provided with the distribution. 00022 * 00023 * 3. Neither the name of the authors nor the names of its contributors may be 00024 * used to endorse or promote products derived from this software without 00025 * specific prior written permission. 00026 * 00027 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00028 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00029 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00030 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 00031 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 00032 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 00033 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00034 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 00035 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 00036 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00037 * POSSIBILITY OF SUCH DAMAGE. 00038 */ 00039 00040 00041 00042 /** 00043 * \file cvrPoint3D.h 00044 * Defines the template type cvr::point3D<T> and all shortcuts like 00045 * cvr::ipoint3D. 00046 * \author Pablo Alvarado 00047 * \date 01.11.2002 00048 * 00049 * $Id: cvrPoint3D.h,v 1.4 2006/06/06 09:50:03 doerfler Exp $ 00050 */ 00051 00052 #ifndef _CVR_POINT_3_D_H_ 00053 #define _CVR_POINT_3_D_H_ 00054 00055 #include "cvrConfig.h" 00056 #include "cvrAssert.h" 00057 #include "cvrIoHandler.h" 00058 00059 #include <iostream> 00060 00061 namespace cvr { 00062 00063 /** 00064 * Three dimensional point, containing the coordinates x, y, z. 00065 * 00066 * The template type T will be the one used for each coordinate. For 00067 * example point3D<float> uses float for \a x, \a y, and \a z. 00068 * 00069 * This data structure simplifies the manipulation of 3D points providing 00070 * simple interfaces for adding, substracting, distance (L2), and more. 00071 * 00072 * There are some alias to shorten the notation: 00073 * -ipoint3D is equivalent to point3D<int> 00074 * -fpoint3D is equivalent to point3D<sreal> 00075 * -dpoint3D is equivalent to point3D<dreal> 00076 * 00077 * @ingroup gGeomData 00078 */ 00079 template <typename T> 00080 class point3D { 00081 public: 00082 /** 00083 * Used for the template-based interface for pixels as vectors. 00084 */ 00085 typedef T value_type; 00086 00087 /** 00088 * Return type of the size() member 00089 */ 00090 typedef int size_type; 00091 00092 /* 00093 * Anonymous union to allow the fastes access to the elements as 00094 * semantic tokes (x and y) and as array as well 00095 */ 00096 union { 00097 __extension__ struct { 00098 /** 00099 * Coordinate x 00100 */ 00101 value_type x; 00102 00103 /** 00104 * Coordinate y; 00105 */ 00106 value_type y; 00107 00108 /** 00109 * Coordinate z; 00110 */ 00111 value_type z; 00112 }; 00113 00114 /** 00115 * Array that shares the same memory with x and y 00116 */ 00117 value_type data[3]; 00118 }; 00119 00120 /** 00121 * Default constructor 00122 */ 00123 explicit point3D(const T newx=T(),const T newy=T(),const T newz=T()); 00124 00125 /** 00126 * Copy constructor 00127 */ 00128 template <typename U> 00129 point3D(const point3D<U>& p); 00130 00131 /** 00132 * Casts all elements of \c p to T and sets these values in this point. 00133 */ 00134 template <typename U> 00135 point3D<T>& castFrom(const point3D<U>& p); 00136 00137 /** 00138 * Set the coordinate values and return a reference to this point3D 00139 */ 00140 inline point3D<T>& set(const T tx,const T ty,const T tz); 00141 00142 /** 00143 * Get the coordinate values 00144 */ 00145 inline void get(T& tx,T& ty,T& tz) const; 00146 00147 /** 00148 * Calculate %square of distance to the point3D c. This 00149 * method is faster than distanceTo (because it does not compute 00150 * the square root). 00151 */ 00152 inline T distanceSqr(const point3D<T>& c) const; 00153 00154 /** 00155 * Return the square of the magnitude of the point3D 00156 */ 00157 inline T absSqr() const; 00158 00159 /** 00160 * Dot product with another point3D. 00161 * 00162 * @return this->x*p.x + this->y*p.y + this->z*p.z. 00163 */ 00164 inline T dot(const point3D<T>& p) const; 00165 00166 /** 00167 * Cross product with another point3D 00168 */ 00169 inline point3D<T>& cross(const point3D<T>& c); 00170 00171 /** 00172 * Cross product between the given two point3D 00173 */ 00174 inline point3D<T>& cross(const point3D<T>& a,const point3D<T>& b); 00175 00176 /** 00177 * Multiply point3D<T> with a given factor 00178 */ 00179 template<typename U> 00180 inline point3D<T>& multiply(const U c); 00181 00182 /** 00183 * Multiply point3D<T> with a given factor 00184 */ 00185 template<typename U> 00186 inline point3D<T> operator*(const U c) const; 00187 00188 /** 00189 * Multiply the other point3D point3D<T> with a given factor 00190 */ 00191 template<typename U> 00192 inline point3D<T>& multiply(const point3D<T>& other,const U c); 00193 00194 /** 00195 * Multiply point3D<T> with a given factor 00196 */ 00197 template<typename U> 00198 inline point3D<T>& operator*=(const U c); 00199 00200 /** 00201 * Multiplies elementwise the components of this and the point3D c 00202 * 00203 * @see dot(),cross() 00204 */ 00205 inline point3D<T> operator*(const point3D<T>& c) const; 00206 00207 /** 00208 * Multiplies elementwise the components of this and the point3D c 00209 * 00210 * @see dot(),cross() 00211 */ 00212 inline point3D<T>& operator*=(const point3D<T>& c); 00213 00214 /** 00215 * Multiplies elementwise the components of this and the point3D c, and 00216 * leave the result here. 00217 * 00218 * @see dot(),cross() 00219 */ 00220 inline point3D<T>& emultiply(const point3D<T>& c); 00221 00222 /** 00223 * Multiplies elementwise the components of \a a and \a b and leave the 00224 * result here. 00225 * 00226 * @see dot(),cross() 00227 */ 00228 inline point3D<T>& emultiply(const point3D<T>& a,const point3D<T>& b); 00229 00230 /** 00231 * Divide each component of point3D<T> with a given factor 00232 */ 00233 template<typename U> 00234 inline point3D<T>& divide(const U c); 00235 00236 /** 00237 * Divide each component of other other point3D<T> with a given factor 00238 */ 00239 template<typename U> 00240 inline point3D<T>& divide(const point3D<T>& other,const U c); 00241 00242 /** 00243 * Divide each component of point3D<T> by a given factor 00244 */ 00245 template <typename U> 00246 inline point3D<T> operator/(const U c) const; 00247 00248 /** 00249 * Divide each component of point3D<T> by a given factor 00250 */ 00251 template <typename U> 00252 inline point3D<T>& operator/=(const U c); 00253 00254 /** 00255 * Elementwise division of each component of the point3Ds 00256 */ 00257 inline point3D<T> operator/(const point3D<T>& c) const; 00258 00259 /** 00260 * Elementwise division of each component of the point3Ds 00261 */ 00262 inline point3D<T>& operator/=(const point3D<T>& c); 00263 00264 /** 00265 * Elementwise division of each component of the point3Ds 00266 */ 00267 inline point3D<T>& edivide(const point3D<T>& c); 00268 00269 /** 00270 * Elementwise division of each component of the point3Ds 00271 */ 00272 inline point3D<T>& edivide(const point3D<T>& a,const point3D<T>& b); 00273 00274 /** 00275 * Modulo c of the integer part of each component of the point3D 00276 */ 00277 inline point3D<T> operator%(const int c) const; 00278 00279 /** 00280 * Add given point3D to this point3D and leave the result here. 00281 * @param p the other point3D to be added to this one 00282 * @return a reference to this point3D 00283 */ 00284 inline point3D<T>& add(const point3D<T>& p); 00285 00286 /** 00287 * Add the two other point3Ds and leave the result here 00288 * @param a first point3D to be added 00289 * @param b second point3D to be added 00290 * @return a reference to this point3D, which will contain a+b 00291 */ 00292 inline point3D<T>& add(const point3D<T>& a, 00293 const point3D<T>& b); 00294 /** 00295 * Operator + is simmilar to add, but a new point3D is returned, i.e. 00296 * this point3D will not change. 00297 */ 00298 inline point3D<T> operator+(const point3D<T>& p) const; 00299 00300 /** 00301 * Operator += is an alias for add() 00302 */ 00303 inline point3D<T>& operator+=(const point3D<T>& p); 00304 00305 /** 00306 * Subtract 00307 */ 00308 inline point3D<T>& subtract(const point3D<T>& p); 00309 00310 00311 /** 00312 * Subtract the two other point3Ds and leave the result here 00313 * @param a first point3D 00314 * @param b point3D to be subtracted from the first one 00315 * @return a reference to this point3D, which will contain a-b 00316 */ 00317 inline point3D<T>& subtract(const point3D<T>& a, 00318 const point3D<T>& b); 00319 00320 /** 00321 * Operator - 00322 */ 00323 inline point3D<T> operator-(const point3D<T>& p) const; 00324 00325 /** 00326 * Operator -= 00327 */ 00328 inline point3D<T>& operator-=(const point3D<T>& p); 00329 00330 /** 00331 * Copy operator 00332 */ 00333 inline point3D<T>& copy(const point3D<T>& p); 00334 00335 /** 00336 * Operator = 00337 */ 00338 inline point3D<T>& operator=(const point3D<T>& p); 00339 00340 /** 00341 * Operator == 00342 */ 00343 inline bool operator==(const point3D<T>& p) const; 00344 00345 /** 00346 * Operator != 00347 */ 00348 inline bool operator!=(const point3D<T>& p) const; 00349 00350 /** 00351 * Operator < 00352 * 00353 * A point3D is "smaller" than another one if its coordinates produce 00354 * an earlier display of the point3D in the monitor. i.e. if it 00355 * has a smaller y component or (if the y components are the same) if 00356 * the x component is smaller 00357 */ 00358 inline bool operator<(const point3D<T>& p) const; 00359 00360 /** 00361 * Operator > 00362 * 00363 * A point3D is "bigger" than another one if its coordinates produce 00364 * an later display of the point3D in the monitor. i.e. if it 00365 * has a bigger y component or (if the y components are the same) if 00366 * the x component is bigger 00367 */ 00368 inline bool operator>(const point3D<T>& p) const; 00369 00370 /** 00371 * @name Access as vector 00372 */ 00373 //@{ 00374 /** 00375 * Used to simulate vector access. 00376 * 00377 * The correspondence between the elements of the vector and 00378 * the components will be [0] for x, [1] for y, and [2] for z 00379 */ 00380 inline T& operator[](const int i); 00381 00382 /** 00383 * Used to simulate read-only vector access. It allows the use of point3D 00384 * in templates expecting a vector-like structure. 00385 * 00386 * The correspondence between the elements of the vector and 00387 * the components will be [0] for x, [1] for y, and [2] for z 00388 */ 00389 inline const T& operator[](const int i) const; 00390 00391 /** 00392 * Used to simulate the vector size 00393 */ 00394 inline size_type size() const; 00395 //@} 00396 00397 private: 00398 /** 00399 * Square of the given value 00400 * 00401 * Reimplemented to avoid including cvrMath.h just for this extremely 00402 * simple function. 00403 */ 00404 inline value_type sqr(const value_type x) const; 00405 }; 00406 00407 /** 00408 * Read the vector from the given ioHandler. The complete flag indicates 00409 * if the enclosing begin and end should be also be readed 00410 * 00411 * @ingroup gStorable 00412 */ 00413 template <typename T> 00414 bool read(ioHandler& handler,point3D<T>& p,const bool complete=true); 00415 00416 /** 00417 * Write the vector in the given ioHandler. The complete flag indicates 00418 * if the enclosing begin and end should be also be written or not 00419 * 00420 * @ingroup gStorable 00421 */ 00422 template<typename T> 00423 bool write(ioHandler& handler,const point3D<T>& p,const bool complete=true); 00424 00425 /** 00426 * A point3D with integer coordinates 00427 */ 00428 typedef point3D<int> ipoint3D; 00429 00430 /** 00431 * A point3D with float coordinates 00432 */ 00433 typedef point3D<float> fpoint3D; 00434 00435 /** 00436 * A point3D with double coordinates 00437 */ 00438 typedef point3D<double> dpoint3D; 00439 00440 } 00441 00442 namespace std { 00443 00444 //inline ostream& operator<<(ostream& s,const cvr::point3D& p); 00445 template <typename T> 00446 inline ostream& operator<<(ostream& s,const cvr::point3D<T>& p); 00447 00448 //inline ostream& operator>>(istream& s,const cvr::point3D& p); 00449 template <typename T> 00450 inline istream& operator>>(istream& s,cvr::point3D<T>& p); 00451 00452 } 00453 00454 #include "cvrPoint3D_template.h" 00455 00456 #endif 00457 00458 00459