Fawkes API Fawkes Development Version
|
00001 00002 /*************************************************************************** 00003 * hom_coord.cpp - Homogeneous coordinate 00004 * 00005 * Created: Thu Sep 27 16:21:24 2007 00006 * Copyright 2007-2008 Daniel Beck 00007 * 00008 ****************************************************************************/ 00009 00010 /* This program is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2 of the License, or 00013 * (at your option) any later version. A runtime exception applies to 00014 * this software (see LICENSE.GPL_WRE file mentioned below for details). 00015 * 00016 * This program is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00019 * GNU Library General Public License for more details. 00020 * 00021 * Read the full text in the LICENSE.GPL_WRE file in the doc directory. 00022 */ 00023 00024 #include <geometry/hom_coord.h> 00025 #include <geometry/hom_transform.h> 00026 #include <geometry/vector.h> 00027 00028 #include <cstdio> 00029 #include <iomanip> 00030 00031 namespace fawkes { 00032 00033 /** @class HomCoord geometry/hom_coord.h 00034 * Base class for homogeneous primitives (vector and point). 00035 * @author Daniel Beck 00036 */ 00037 00038 /** @var HomCoord::m_vector 00039 * The internal data container. 00040 */ 00041 00042 /** Constructor. 00043 * @param x the x-coordinate 00044 * @param y the y-coordinate 00045 * @param z the z-coordinate 00046 * @param w the w-coordinate 00047 */ 00048 HomCoord::HomCoord(float x, float y, float z, float w) 00049 { 00050 m_vector = new Vector(4); 00051 00052 m_vector->set(0, x); 00053 m_vector->set(1, y); 00054 m_vector->set(2, z); 00055 m_vector->set(3, w); 00056 } 00057 00058 /** Copy constructor. 00059 * @param c another HomCoord 00060 */ 00061 HomCoord::HomCoord(const HomCoord& c) 00062 { 00063 const Vector v = *(c.m_vector); 00064 m_vector = new Vector(v); 00065 } 00066 00067 /** Constructor. 00068 * @param v a vector 00069 */ 00070 HomCoord::HomCoord(const Vector& v) 00071 { 00072 m_vector = new Vector(v); 00073 } 00074 00075 /** Destructor. */ 00076 HomCoord::~HomCoord() 00077 { 00078 delete m_vector; 00079 } 00080 00081 /** RO-getter for x. 00082 * @return the value 00083 */ 00084 float 00085 HomCoord::x() const 00086 { 00087 return m_vector->get(0); 00088 } 00089 00090 /** RW-getter for x. 00091 * @return a reference to the x-element 00092 */ 00093 float& 00094 HomCoord::x() 00095 { 00096 float& val = m_vector->get(0); 00097 return val; 00098 } 00099 00100 /** Setter function for x. 00101 * @param x the new x value 00102 * @return reference to this 00103 */ 00104 HomCoord& 00105 HomCoord::x(float x) 00106 { 00107 m_vector->set(0, x); 00108 return *this; 00109 } 00110 00111 /** RO-getter for y. 00112 * @return the value 00113 */ 00114 float 00115 HomCoord::y() const 00116 { 00117 return m_vector->get(1); 00118 } 00119 00120 /** RW-getter for y. 00121 * @return a reference to the y-element 00122 */ 00123 float& 00124 HomCoord::y() 00125 { 00126 float& val = m_vector->get(1); 00127 return val; 00128 } 00129 00130 /** Setter function for y. 00131 * @param y the new y value 00132 * @return reference to this 00133 */ 00134 HomCoord& 00135 HomCoord::y(float y) 00136 { 00137 m_vector->set(1, y); 00138 return *this; 00139 } 00140 00141 /** RO-getter for z. 00142 * @return the value 00143 */ 00144 float 00145 HomCoord::z() const 00146 { 00147 return m_vector->get(2); 00148 } 00149 00150 /** RW-getter for z. 00151 * @return a reference to the z-element 00152 */ 00153 float& 00154 HomCoord::z() 00155 { 00156 float& val = m_vector->get(2); 00157 return val; 00158 } 00159 00160 /** Setter function for z. 00161 * @param z the new z value 00162 * @return reference to this 00163 */ 00164 HomCoord& 00165 HomCoord::z(float z) 00166 { 00167 m_vector->set(2, z); 00168 return *this; 00169 } 00170 00171 /** RO-getter for w. 00172 * @return the value 00173 */ 00174 float 00175 HomCoord::w() const 00176 { 00177 return m_vector->get(3); 00178 } 00179 00180 /** RW-getter for w. 00181 * @return a reference to the w-element 00182 */ 00183 float& 00184 HomCoord::w() 00185 { 00186 float& val = m_vector->get(3); 00187 return val; 00188 } 00189 00190 /** Setter function for w. 00191 * @param w the new w value 00192 * @return reference to this 00193 */ 00194 HomCoord& 00195 HomCoord::w(float w) 00196 { 00197 m_vector->set(3, w); 00198 return *this; 00199 } 00200 00201 /** Convenience function to rotate the HomCoord around the x-axis. 00202 * @param rad the roation angle in rad 00203 * @return reference to this 00204 */ 00205 HomCoord& 00206 HomCoord::rotate_x(float rad) 00207 { 00208 HomTransform t; 00209 t.rotate_x(rad); 00210 transform(t); 00211 00212 return *this; 00213 } 00214 00215 /** Convenience function to rotate the HomCoord around the y-axis. 00216 * @param rad the roation angle in rad 00217 * @return reference to this 00218 */ 00219 HomCoord& 00220 HomCoord::rotate_y(float rad) 00221 { 00222 HomTransform t; 00223 t.rotate_y(rad); 00224 transform(t); 00225 00226 return *this; 00227 } 00228 00229 /** Convenience function to rotate the HomCoord around the z-axis. 00230 * @param rad the roation angle in rad 00231 * @return reference to this 00232 */ 00233 HomCoord& 00234 HomCoord::rotate_z(float rad) 00235 { 00236 HomTransform t; 00237 t.rotate_z(rad); 00238 transform(t); 00239 00240 return *this; 00241 } 00242 00243 /** Subtraction operator. 00244 * @param h the rhs HomCoord 00245 * @return the resulting HomCoord 00246 */ 00247 HomCoord 00248 HomCoord::operator-(const HomCoord& h) const 00249 { 00250 Vector v = (*m_vector) - (*h.m_vector); 00251 HomCoord result(v); 00252 float w = result.w(); 00253 result.w() = (w > 1.0) ? 1.0 : w; 00254 return result; 00255 } 00256 00257 /** Substraction-assignment operator. 00258 * @param h the rhs HomCoord 00259 * @return reference to the resulting HomCoord 00260 */ 00261 HomCoord& 00262 HomCoord::operator-=(const HomCoord& h) 00263 { 00264 (*m_vector) -= (*h.m_vector); 00265 float w = this->w(); 00266 this->w() = (w > 1.0) ? 1.0 : w; 00267 return *this; 00268 } 00269 00270 /** Addition operator. 00271 * @param h the rhs HomCoord 00272 * @return the resulting HomCoord 00273 */ 00274 HomCoord 00275 HomCoord::operator+(const HomCoord& h) const 00276 { 00277 Vector v = (*m_vector) + (*h.m_vector); 00278 HomCoord result(v); 00279 float w = result.w(); 00280 result.w() = (w > 1.0) ? 1.0 : w; 00281 return result; 00282 } 00283 00284 /** Addition-assignment operator. 00285 * @param h the rhs HomCoord 00286 * @return reference to the resulting HomCoord 00287 */ 00288 HomCoord& 00289 HomCoord::operator+=(const HomCoord& h) 00290 { 00291 (*m_vector) += (*h.m_vector); 00292 float w = this->w(); 00293 this->w() = (w > 1.0) ? 1.0 : w; 00294 return *this; 00295 } 00296 00297 00298 /** Assignment operator. 00299 * @param h the rhs HomCoord 00300 * @return a reference of the lhs vector (this) 00301 */ 00302 HomCoord& 00303 HomCoord::operator=(const HomCoord& h) 00304 { 00305 (*m_vector) = (*h.m_vector); 00306 00307 return *this; 00308 } 00309 00310 /** Calculates the dot product of two coords. 00311 * @param h the rhs HomCoord 00312 * @return the scalar product 00313 */ 00314 float 00315 HomCoord::operator*(const HomCoord& h) const 00316 { 00317 return x() * h.x() + y() * h.y() + z() * h.z(); 00318 } 00319 00320 /** Mulitplication operator. 00321 * Multiply the vector with a scalar. 00322 * @param s a scalar 00323 * @return the result of multiplying the vector with the scalar 00324 */ 00325 HomCoord 00326 HomCoord::operator*(const float s) const 00327 { 00328 HomCoord result; 00329 result.x() = x() * s; 00330 result.y() = y() * s; 00331 result.z() = z() * s; 00332 result.w() = w(); 00333 00334 return result; 00335 } 00336 00337 /** Multiplication-assignment operator. 00338 * Multiply the vector with a scalar. 00339 * @param s a scalar 00340 * @return a reference to the modified vector (this) 00341 */ 00342 HomCoord& 00343 HomCoord::operator*=(const float s) 00344 { 00345 x() *= s; 00346 y() *= s; 00347 z() *= s; 00348 00349 return *this; 00350 } 00351 00352 /** Comparison operator. 00353 * @param h the other HomCoord 00354 * @return true if h is equal to *this, false otherwise 00355 */ 00356 bool 00357 HomCoord::operator==(const HomCoord& h) const 00358 { 00359 return (*m_vector == *h.m_vector) ? true : false; 00360 } 00361 00362 /** Inequality operator. 00363 * @param h the other HomCoord 00364 * @return true if h is not equal to *this, false otherwise 00365 */ 00366 bool 00367 HomCoord::operator!=(const HomCoord& h) const 00368 { 00369 return (*m_vector == *h.m_vector) ? false : true; 00370 } 00371 00372 /** Appends the components of the HomCoord to the ostream. 00373 * @param stream to be extended 00374 * @return the extended stream 00375 */ 00376 std::ostream& 00377 HomCoord::print(std::ostream& stream) const 00378 { 00379 return stream << "[" << x() << ", " << y() << ", " << z() << ", " << w() << "]T"; 00380 } 00381 00382 /** Transform the vector with the given transform. 00383 * @param t a transform 00384 * @return reference to the modified vector (this) 00385 */ 00386 HomCoord& 00387 HomCoord::transform(const HomTransform& t) 00388 { 00389 Matrix m = t.get_matrix(); 00390 (*m_vector) = m * (*m_vector); 00391 00392 return *this; 00393 } 00394 00395 } // end namespace fawkes 00396