Point.h
Go to the documentation of this file.00001 /**<!--------------------------------------------------------------------> 00002 @class Point 00003 @author Travis Fischer (fisch0920@gmail.com) 00004 @date Fall 2008 00005 00006 @brief 00007 Provides basic functionality for a homogeneous point 00008 <!-------------------------------------------------------------------->**/ 00009 00010 #ifndef POINT_H_ 00011 #define POINT_H_ 00012 00013 #include <common/math/Vector.h> 00014 #include <ostream> 00015 #include <limits> 00016 00017 #define POINT_NO_ELEMENTS (N) 00018 #define POINT_SIZE (sizeof(T) * POINT_NO_ELEMENTS) 00019 00020 /** 00021 * @brief 00022 * A Point is templated to store N elements of data of type T. If 00023 * left off, T will default to a "real_t" data type. 00024 * 00025 * Included in this definition are typedefs for the most commonly used 00026 * points (Point4, Point3, etc), and these can be thought of as shortcuts 00027 * to reference their associated Point templates. For example, 00028 * Point4 can be used to refer to Point<4, real_t>. 00029 * 00030 * A Point stores its data internally as an N-length array of type T. 00031 */ 00032 template <unsigned N = 4, typename T = real_t> 00033 struct Point { 00034 00035 /// Underlying data stored as an N-length array of type T 00036 T data[POINT_NO_ELEMENTS]; 00037 00038 00039 ///@name Constructors 00040 //@{----------------------------------------------------------------- 00041 00042 /// Expects N arguments of type T 00043 /// @note explicitly sets the homogeneous coordinate to 1 (w=1) 00044 inline Point(const T *srcData); 00045 00046 /// Constructs a point at the origin (w=1) 00047 inline Point(); 00048 00049 /// Copy Constructor 00050 inline Point(const Point<N, T> &v); 00051 00052 00053 //@}----------------------------------------------------------------- 00054 ///@name Static convenience constructors to generate common points 00055 //@{----------------------------------------------------------------- 00056 00057 /// Generates a point full of zeroes 00058 static inline Point<N, T> origin() { 00059 return Point<N, T>(); 00060 } 00061 00062 /// Generates a point filled with the specified value 00063 static inline Point<N, T> fill(const T &val) { 00064 Point<N, T> ret = Point<N, T>(); 00065 00066 for(unsigned i = N; i--;) 00067 ret[i] = val; 00068 00069 return ret; 00070 } 00071 00072 /// @returns the element-wise minimum point from the two given points 00073 static inline Point<N, T> min(const Point<N, T> &v1, const Point<N, T> &v2) { 00074 Point<N, T> ret; 00075 00076 for(unsigned i = N; i--;) 00077 ret[i] = MIN(v1[i], v2[i]); 00078 00079 return ret; 00080 } 00081 00082 /// @returns the element-wise maximum point from the two given points 00083 static inline Point<N, T> max(const Point<N, T> &v1, const Point<N, T> &v2) { 00084 Point<N, T> ret; 00085 00086 for(unsigned i = N; i--;) 00087 ret[i] = MAX(v1[i], v2[i]); 00088 00089 return ret; 00090 } 00091 00092 /// Generates a point filled with the maximum T (infinity) 00093 static inline Point<N, T> infinity() { 00094 return Point<N, T>::fill(std::numeric_limits<T>::max()); 00095 } 00096 00097 /// Generates a point filled with the minimum T (negative infinity) 00098 static inline Point<N, T> negativeInfinity() { 00099 return Point<N, T>::fill(std::numeric_limits<T>::min()); 00100 } 00101 00102 00103 //@}----------------------------------------------------------------- 00104 ///@name Accessor Operators 00105 //@{----------------------------------------------------------------- 00106 00107 /// @returns a const reference to the element at the given index 00108 inline const T &operator[](const unsigned index) const; 00109 /// @returns a reference to the element at the given index 00110 /// @note changes to the returned element will affect this point 00111 inline T &operator[](const unsigned index); 00112 00113 /// @returns a pointer to the underlying data (N-length array of type T) 00114 inline const T *operator*() const; 00115 /// @returns a pointer to the underlying data (N-length array of type T) 00116 inline T *operator*(); 00117 00118 00119 //@}----------------------------------------------------------------- 00120 ///@name Equality Operators 00121 //@{----------------------------------------------------------------- 00122 00123 inline bool operator==(const Point<N, T> &v) const; 00124 inline bool operator!=(const Point<N, T> &v) const; 00125 00126 00127 //@}----------------------------------------------------------------- 00128 ///@name Mutator Operators 00129 //@{----------------------------------------------------------------- 00130 00131 inline Point<N, T> &operator =(const Point<N, T> &v); 00132 inline Point<N, T> &operator+=(const Vector<N - 1, T> &rhs); 00133 inline Point<N, T> &operator-=(const Vector<N - 1, T> &rhs); 00134 00135 00136 //@}----------------------------------------------------------------- 00137 ///@name Scalar Mutator Operators 00138 //@{----------------------------------------------------------------- 00139 00140 inline Point<N, T> &operator*=(const T &scale); 00141 inline Point<N, T> &operator/=(const T &scale); 00142 00143 00144 //@}----------------------------------------------------------------- 00145 ///@name Arithmetic Operators 00146 //@{----------------------------------------------------------------- 00147 00148 inline Point<N, T> operator+ (const Vector<N - 1, T> &rhs) const; 00149 inline Point<N, T> operator- (const Vector<N - 1, T> &rhs) const; 00150 inline Vector<N - 1, T> operator- (const Point<N, T> &rhs) const; 00151 00152 00153 //@}----------------------------------------------------------------- 00154 ///@name Scalar Arithmetic Operators 00155 //@{----------------------------------------------------------------- 00156 00157 inline Point<N, T> operator* (const T &scale) const; 00158 inline Point<N, T> operator/ (const T &scale) const; 00159 00160 00161 //@}----------------------------------------------------------------- 00162 ///@name More Complex Functionality 00163 //@{----------------------------------------------------------------- 00164 00165 /// @returns whether or not this Point is the origin 00166 inline bool isZero() const; 00167 00168 /// @returns the magnitude of the vector connecting this point to the one 00169 /// passed in 00170 inline T getDistance(const Point<N, T> &v) const; 00171 00172 /// @returns the squared magnitude of the vector connecting this point to 00173 /// the one passed in 00174 inline T getDistance2(const Point<N, T> &v) const; 00175 00176 /// @returns the sum of the components in this Point 00177 inline T getSum() const; 00178 00179 /// @returns the average of the components in this Point 00180 inline T getAverage() const; 00181 00182 /// Cleans up point (0's out entries that are less than epsilon) 00183 void cleanup(); 00184 00185 00186 //@}----------------------------------------------------------------- 00187 ///@name Specialized Point Functionality 00188 //@{----------------------------------------------------------------- 00189 00190 /// Convenience Constructor (note w = 1 by default) 00191 inline explicit Point(const T &v0, const T &v1 = 0, const T &v2 = 0, 00192 const T &v3 = 1); 00193 00194 00195 //@}----------------------------------------------------------------- 00196 }; 00197 00198 00199 /// @brief Homogeneous point in 3-space (contains a 4th "w" coordinate) 00200 /// @note Point3 contains 4 real_ts 00201 typedef Point<4> Point3; 00202 00203 /// @brief Homogeneous point in 2-space (contains a 3rd "w" coordinate) 00204 /// @note Point2 contains 3 real_ts 00205 typedef Point<3> Point2; 00206 00207 00208 ///@name Extra operators where Point is on right-hand side 00209 //@{--------------------------------------------------------------------- 00210 00211 /// @returns the N-length vector resulting from multiplying a scalar by an 00212 /// N-length point 00213 template <unsigned N, typename T> 00214 inline Point<N, T> operator* (const T &scale, const Point<N, T> &rhs); 00215 00216 /// @returns (-1) * rhs, which is a negated version of the original right 00217 /// hand side point 00218 template <unsigned N, typename T> 00219 inline Point<N, T> operator- (const Point<N, T> &rhs); 00220 00221 /// Prints a Point to an output stream 00222 template <unsigned N, typename T> 00223 inline std::ostream &operator<<(std::ostream &os, const Point<N, T> &v); 00224 00225 //@}--------------------------------------------------------------------- 00226 00227 00228 /* Include inline implementations */ 00229 #include "common/math/Point.inl" 00230 00231 00232 ///@name Routines with point-specific functionality 00233 //@{--------------------------------------------------------------------------- 00234 00235 /// @brief Homogenizes the point passed in such that the 4th value ("w") is one 00236 extern void homogenize(Point3 &p); 00237 00238 //@}--------------------------------------------------------------------------- 00239 00240 00241 #endif // POINT_H_ 00242
Generated on 28 Feb 2009 for Milton by
1.5.6