Location.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef LOCATION_H
00011 #define LOCATION_H
00012 
00013 #include <math.h>
00014 #include <utility>
00015 #include "AppAssert.h"
00016 #include "AppConfig.h"
00017 
00018 /**
00019  * This namespace is a collection of the general API classes.
00020  */
00021 namespace Opticks
00022 {
00023 
00024 /**
00025  * A point location.
00026  *
00027  * This can represent any two or three dimensional location either
00028  * integral or floating point. Usually, there will be typedefs for
00029  * specific uses such as geographic location and pixel location so this
00030  * class is not typically used directly.
00031  *
00032  * The template argument T is the data type used for each value in the location. This
00033  * is usually and int or double.
00034  * The template argument SZ must be either 2 or 3 and indicates the dimensionality
00035  * of the Location. If this value does not match the dimensionality of the contructor
00036  * used, an assertion exceptions will be thrown.
00037  *
00038  * @see LocationType, PixelLocation
00039  */
00040 template<typename T, int SZ>
00041 class Location
00042 {
00043 public:
00044    /**
00045     *  Creates a Location object with a given two dimensional position.
00046     *
00047     *  @param   x
00048     *           The first dimension's value.
00049     *  @param   y
00050     *           The second dimension's value.
00051     *  @throw   AssertException if the dimensionality of this Location is not 2.
00052     */
00053    Location(T x, T y) : mX(x), mY(y), mZ(0)
00054    {
00055       ENSURE(SZ == 2);
00056    }
00057 
00058    /**
00059     *  Creates a Location object with a given two dimensional position.
00060     *
00061     *  @param   point
00062     *           A point that is the source for the copy. point.first is
00063     *           used for mX, point.second for mY.
00064     *  @throw   AssertException if the dimensionality of this Location is not 2.
00065     */
00066    template<typename U>
00067    Location(std::pair<U, U> point) : mX(point.first), mY(point.second), mZ(0)
00068    {
00069       ENSURE(SZ == 2);
00070    }
00071 
00072    /**
00073     *  Creates a Location object with a given three dimensional position.
00074     *
00075     *  @param   x
00076     *           The first dimension's value.
00077     *  @param   y
00078     *           The second dimension's value.
00079     *  @param   z
00080     *           The third dimension's value.
00081     *  @throw   AssertException if the dimensionality of this Location is not 3.
00082     */
00083    Location(T x, T y, T z): mX(x), mY(y), mZ(z)
00084    {
00085       ENSURE(SZ == 3);
00086    }
00087 
00088 
00089   /**
00090    *  Creates a Location object with a given position.
00091    *
00092    *  The dimensionality of the two Locations must be the same.
00093    *
00094    *  @param   point
00095    *           A point that is the source for the copy.
00096    */
00097    Location(const Location<T,SZ> &point): mX(point.mX), mY(point.mY), mZ(point.mZ) {}
00098 
00099    /**
00100     *  Creates a default Location object.
00101     *
00102     *  A default location contains zero values for the X, Y, and Z
00103     *  positions.
00104     */
00105    Location(): mX(0), mY(0), mZ(0) {}
00106 
00107    /**
00108     *  Destructor
00109     */
00110    ~Location() {}
00111 
00112    /**
00113     *  Returns the dimensionality of the Location.
00114     *
00115     *  @return The dimensionality. Either 2 or 3.
00116     */
00117    static int dimensionality() { return SZ; }
00118 
00119    /**
00120     *  Returns the distance between the values comprising the Location.
00121     *
00122     *  The length will always be a floating point value regardless of the
00123     *  underlying data type of the Location.
00124     *
00125     *  @return  The length of the vector represented by the LocationType.
00126     */
00127    double length() const
00128    {
00129       if(SZ == 2)
00130       {
00131          return sqrt(static_cast<double>(mX * mX) + mY * mY);
00132       }
00133       else if(SZ == 3)
00134       {
00135          return sqrt(static_cast<double>(mX * mX) + mY * mY + mZ * mZ);
00136       }
00137       return 0.0; // not reached
00138    }
00139 
00140    /**
00141     *  Adds two Locations.
00142     *
00143     *  Both Locations must have the same dimensionality.
00144     *
00145     *  @param   point
00146     *           The other Location to add.
00147     *
00148     *  @return  A Location containing the sum of the coordinates
00149     */
00150    Location<T,SZ> operator+(const Location<T,SZ> &point) const
00151    {
00152       if(SZ == 2)
00153       {
00154          return Location<T,SZ>(mX + point.mX, mY + point.mY);
00155       }
00156       else if(SZ == 3)
00157       {
00158          return Location<T,SZ>(mX + point.mX, mY + point.mY, mZ + point.mZ);
00159       }
00160       return Location<T,SZ>(); // not reached
00161    }
00162 
00163    /**
00164     *  Adds a scalar to the values in the Location.
00165     *
00166     *  @param   scalar
00167     *           The scalar to add to the components of the Location
00168     *
00169     *  @return  A Location where each component has scalar added to it
00170     */
00171    Location<T,SZ> operator+(T scalar) const
00172    {
00173       if(SZ == 2)
00174       {
00175          return Location<T,SZ>(mX + scalar, mY + scalar);
00176       }
00177       else if(SZ == 3)
00178       {
00179          return Location<T,SZ>(mX + scalar, mY + scalar, mZ + scalar);
00180       }
00181       return Location<T,SZ>(); // not reached
00182    }
00183 
00184    /**
00185     *  Adds two Locations.
00186     *
00187     *  @param   point
00188     *           The other Location to add.
00189     *
00190     *  @return  A Location containing the sum of the coordinates
00191     */
00192    Location<T,SZ> &operator+=(const Location<T,SZ> &point)
00193    {
00194       mX += point.mX;
00195       mY += point.mY;
00196       if(SZ == 3)
00197       {
00198          mZ += point.mZ;
00199       }
00200 
00201       return *this;
00202    }
00203 
00204    /**
00205     *  Subtracts two Locations.
00206     *
00207     *  @param   point
00208     *           The other Location to subtract.
00209     *
00210     *  @return  A Location containing the difference of the coordinates
00211     */
00212    Location<T,SZ> operator-(const Location<T,SZ> &point) const
00213    {
00214       if(SZ == 2)
00215       {
00216          return Location<T,SZ>(mX - point.mX, mY - point.mY);
00217       }
00218       else if(SZ == 3)
00219       {
00220          return Location<T,SZ>(mX - point.mX, mY - point.mY, mZ - point.mZ);
00221       }
00222       return Location<T,SZ>(); // not reached
00223    }
00224 
00225    /**
00226     *  Subtracts a scalar from the values in the Location.
00227     *
00228     *  @param   scalar
00229     *           The scalar to subtract from the components of the Location
00230     *
00231     *  @return  A Location where each component has scalar subtracted from it
00232     */
00233    Location<T,SZ> operator-(T scalar) const
00234    {
00235       if(SZ == 2)
00236       {
00237          return Location<T,SZ>(mX - scalar, mY - scalar);
00238       }
00239       else if(SZ == 3)
00240       {
00241          return Location<T,SZ>(mX - scalar, mY - scalar, mZ - scalar);
00242       }
00243       return Location<T,SZ>(); // not reached
00244    }
00245 
00246    /**
00247     *  Subtracts two Locations.
00248     *
00249     *  @param   point
00250     *           The other Location to subtract.
00251     *
00252     *  @return  A Location containing the difference of the coordinates
00253     */
00254    Location<T,SZ> &operator-=(const Location<T,SZ> &point)
00255    {
00256       mX -= point.mX;
00257       mY -= point.mY;
00258       if(SZ == 3)
00259       {
00260          mZ -= point.mZ;
00261       }
00262 
00263       return *this;
00264    }
00265 
00266    /**
00267     *  Multiplies the values of the Location by a scalar.
00268     *
00269     *  @param   scalar
00270     *           The scalar to multiply the components of the Location
00271     *
00272     *  @return  A Location where each component has scalar multiplied by it.
00273     */
00274    Location<T,SZ> operator*(T scalar) const
00275    {
00276       if(SZ == 2)
00277       {
00278          return Location<T,SZ>(mX * scalar, mY * scalar);
00279       }
00280       if(SZ == 3)
00281       {
00282          return Location<T,SZ>(mX * scalar, mY * scalar, mZ * scalar);
00283       }
00284       return Location<T,SZ>(); // not reached
00285    }
00286 
00287    /**
00288     *  Assigns another Location to this.
00289     *
00290     *  @param   point
00291     *           The source Location.
00292     *
00293     *  @return  A reference to *this, which has been changed to have a copy of the contents of point.
00294     */
00295    BROKEN_INLINE_HINT Location<T,SZ> &operator=(const Location<T,SZ> &point)
00296    {
00297       if(this != &point)
00298       {
00299          mX = point.mX;
00300          mY = point.mY;
00301          mZ = point.mZ;
00302       }
00303 
00304       return *this;
00305    }
00306 
00307    /**
00308     *  Compares two Locations.
00309     *
00310     *  @param   point
00311     *           The other Location.
00312     *
00313     *  @return  Returns true if the coordinates of *this are equal to those of point.
00314     */
00315    bool operator==(const Location<T,SZ> &point) const
00316    {
00317       return (mX == point.mX) && (mY == point.mY) && (SZ == 2 || (mZ == point.mZ));
00318    }
00319 
00320    /**
00321     *  Compares two Locations.
00322     *
00323     *  @param   point
00324     *           The other Location.
00325     *
00326     *  @return  Returns true if the coordinates of *this are NOT equal to those of point.
00327     */
00328    bool operator!=(const Location<T,SZ> &point) const
00329    {
00330       return !(*this == point);
00331    }
00332 
00333    /**
00334     *  Compares two Locations.
00335     *
00336     *  @param   point
00337     *           The other Location.
00338     *
00339     *  @return  Returns true if all coordinates of *this are smaller than those of point.
00340     */
00341    bool operator<(const Location<T,SZ> &point) const
00342    {
00343       return (mX < point.mX) && (mY < point.mY) && (SZ == 2 || mZ < point.mZ);
00344    }
00345 
00346    /**
00347     *  Ensures that both coordinates of this Location are not smaller than another.
00348     *
00349     *  @param   point
00350     *           The coordinates of the smallest allowable Location
00351     */
00352    void clampMinimum(const Location<T,SZ> &point)
00353    {
00354       mX = std::max(mX, point.mX);
00355       mY = std::max(mY, point.mY);
00356       if(SZ == 3)
00357       {
00358          mZ = std::max(mZ, point.mZ);
00359       }
00360    }
00361 
00362    /**
00363     *  Ensures that both coordinates of this Location are not larger than another.
00364     *
00365     *  @param   point
00366     *           The coordinates of the largest allowable Location.
00367     */
00368    void clampMaximum(const Location<T,SZ> &point)
00369    {
00370       mX = std::min(mX, point.mX);
00371       mY = std::min(mY, point.mY);
00372       if(SZ == 3)
00373       {
00374          mZ = std::min(mZ, point.mZ);
00375       }
00376    }
00377 
00378    T mX;
00379    T mY;
00380    T mZ;
00381 };
00382 
00383 /**
00384  * This represents a pixel location in cube or screen coordinates.
00385  */
00386 typedef Location<int,2> PixelLocation;
00387 
00388 /**
00389  * This represents a pixel offset in cube or screen coordinates.
00390  */
00391 typedef Location<int,2> PixelOffset;
00392 
00393 }
00394 
00395 #endif

Software Development Kit - Opticks 4.9.0 Build 16218