hom_coord.cpp

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  */
00103 HomCoord&
00104 HomCoord::x(float x)
00105 {
00106   m_vector->set(0, x);
00107   return *this;
00108 }
00109 
00110 /** RO-getter for y.
00111  * @return the value
00112  */
00113 float
00114 HomCoord::y() const
00115 {
00116   return m_vector->get(1);
00117 }
00118 
00119 /** RW-getter for y.
00120  * @return a reference to the y-element
00121  */
00122 float&
00123 HomCoord::y()
00124 {
00125   float& val = m_vector->get(1);
00126   return val;
00127 }
00128 
00129 /** Setter function for y.
00130  * @param y the new y value
00131  */
00132 HomCoord&
00133 HomCoord::y(float y)
00134 {
00135   m_vector->set(1, y);
00136   return *this;
00137 }
00138 
00139 /** RO-getter for z.
00140  * @return the value
00141  */
00142 float
00143 HomCoord::z() const
00144 {
00145   return m_vector->get(2);
00146 }
00147 
00148 /** RW-getter for z.
00149  * @return a reference to the z-element
00150  */
00151 float&
00152 HomCoord::z()
00153 {
00154   float& val = m_vector->get(2);
00155   return val;
00156 }
00157 
00158 /** Setter function for z.
00159  * @param z the new z value
00160  */
00161 HomCoord&
00162 HomCoord::z(float z)
00163 {
00164   m_vector->set(2, z);
00165   return *this;
00166 }
00167 
00168 /** RO-getter for w.
00169  * @return the value
00170  */
00171 float
00172 HomCoord::w() const
00173 {
00174   return m_vector->get(3);
00175 }
00176 
00177 /** RW-getter for w.
00178  * @return a reference to the w-element
00179  */
00180 float&
00181 HomCoord::w()
00182 {
00183   float& val = m_vector->get(3);
00184   return val;
00185 }
00186 
00187 /** Setter function for w.
00188  * @param w the new w value
00189  */
00190 HomCoord&
00191 HomCoord::w(float w)
00192 {
00193   m_vector->set(3, w);
00194   return *this;
00195 }
00196 
00197 /** Convenience function to rotate the HomCoord around the x-axis.
00198  * @param rad the roation angle in rad
00199  */
00200 HomCoord&
00201 HomCoord::rotate_x(float rad)
00202 {
00203   HomTransform t;
00204   t.rotate_x(rad);
00205   transform(t);
00206
00207   return *this;
00208 }
00209 
00210 /** Convenience function to rotate the HomCoord around the y-axis.
00211  * @param rad the roation angle in rad
00212  */
00213 HomCoord&
00214 HomCoord::rotate_y(float rad)
00215 {
00216   HomTransform t;
00217   t.rotate_y(rad);
00218   transform(t);
00219
00220   return *this;
00221 }
00222 
00223 /** Convenience function to rotate the HomCoord around the z-axis.
00224  * @param rad the roation angle in rad
00225  */
00226 HomCoord&
00227 HomCoord::rotate_z(float rad)
00228 {
00229   HomTransform t;
00230   t.rotate_z(rad);
00231   transform(t);
00232
00233   return *this;
00234 }
00235 
00236 /** Subtraction operator.
00237  * @param h the rhs HomCoord
00238  * @return the resulting HomCoord
00239  */
00240 HomCoord
00241 HomCoord::operator-(const HomCoord& h) const
00242 {
00243   Vector v = (*m_vector) - (*h.m_vector);
00244   HomCoord result(v);
00245   float w = result.w();
00246   result.w() = (w > 1.0) ? 1.0 : w;
00247   return result;
00248 }
00249 
00250 /** Substraction-assignment operator.
00251  * @param h the rhs HomCoord
00252  * @return reference to the resulting HomCoord
00253  */
00254 HomCoord&
00255 HomCoord::operator-=(const HomCoord& h)
00256 {
00257   (*m_vector) -= (*h.m_vector);
00258   float w = this->w();
00259   this->w() = (w > 1.0) ? 1.0 : w;
00260   return *this;
00261 }
00262 
00263 /** Addition operator.
00264  * @param h the rhs HomCoord
00265  * @return the resulting HomCoord
00266  */
00267 HomCoord
00268 HomCoord::operator+(const HomCoord& h) const
00269 {
00270   Vector v = (*m_vector) + (*h.m_vector);
00271   HomCoord result(v);
00272   float w = result.w();
00273   result.w() = (w > 1.0) ? 1.0 : w;
00274   return result;
00275 }
00276 
00277 /** Addition-assignment operator.
00278  * @param h the rhs HomCoord
00279  * @return reference to the resulting HomCoord
00280  */
00281 HomCoord&
00282 HomCoord::operator+=(const HomCoord& h)
00283 {
00284   (*m_vector) += (*h.m_vector);
00285   float w = this->w();
00286   this->w() = (w > 1.0) ? 1.0 : w;
00287   return *this;
00288 }
00289
00290 
00291 /** Assignment operator.
00292  * @param h the rhs HomCoord
00293  * @return a reference of the lhs vector (this)
00294  */
00295 HomCoord&
00296 HomCoord::operator=(const HomCoord& h)
00297 {
00298   (*m_vector) = (*h.m_vector);
00299
00300   return *this;
00301 }
00302 
00303 /** Calculates the dot product of two coords.
00304  * @param h the rhs HomCoord
00305  * @return the scalar product
00306  */
00307 float
00308 HomCoord::operator*(const HomCoord& h) const
00309 {
00310   return x() * h.x() + y() * h.y() + z() * h.z();
00311 }
00312 
00313 /** Mulitplication operator.
00314  * Multiply the vector with a scalar.
00315  * @param s a scalar
00316  * @return the result of multiplying the vector with the scalar
00317  */
00318 HomCoord
00319 HomCoord::operator*(const float s) const
00320 {
00321   HomCoord result;
00322   result.x() = x() * s;
00323   result.y() = y() * s;
00324   result.z() = z() * s;
00325   result.w() = w();
00326
00327   return result;
00328 }
00329 
00330 /** Multiplication-assignment operator.
00331  * Multiply the vector with a scalar.
00332  * @param s a scalar
00333  * @return a reference to the modified vector (this)
00334  */
00335 HomCoord&
00336 HomCoord::operator*=(const float s)
00337 {
00338   x() *= s;
00339   y() *= s;
00340   z() *= s;
00341
00342   return *this;
00343 }
00344 
00345 /** Comparison operator.
00346  * @param h the other HomCoord
00347  * @return true if h is equal to *this, false otherwise
00348  */
00349 bool
00350 HomCoord::operator==(const HomCoord& h) const
00351 {
00352   return (*m_vector == *h.m_vector) ? true : false;
00353 }
00354 
00355 /** Inequality operator.
00356  * @param h the other HomCoord
00357  * @return true if h is not equal to *this, false otherwise
00358  */
00359 bool
00360 HomCoord::operator!=(const HomCoord& h) const
00361 {
00362   return (*m_vector == *h.m_vector) ? false : true;
00363 }
00364 
00365 /** Appends the components of the HomCoord to the ostream.
00366  * @param stream to be extended
00367  * @return the extended stream
00368  */
00369 std::ostream&
00370 HomCoord::print(std::ostream& stream) const
00371 {
00372   return stream << "[" << x() << ", "  << y() << ", "  << z() << ", "  << w() << "]T";
00373 }
00374 
00375 /** Transform the vector with the given transform.
00376  * @param t a transform
00377  * @return reference to the modified vector (this)
00378  */
00379 HomCoord&
00380 HomCoord::transform(const HomTransform& t)
00381 {
00382   Matrix m = t.get_matrix();
00383   (*m_vector) = m * (*m_vector);
00384
00385   return *this;
00386 }
00387
00388 } // end namespace fawkes
00389