Fawkes API  Fawkes Development Version
hom_point.cpp
00001 
00002 /***************************************************************************
00003  *  hom_point.cpp - Homogenous point
00004  *
00005  *  Created: Thu Sep 27 17:01:55 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 "hom_point.h"
00025 #include "hom_vector.h"
00026 #include <cmath>
00027 #include <cstdio>
00028 #include <exception>
00029 
00030 namespace fawkes {
00031 
00032 /** @class HomPoint geometry/hom_point.h
00033  * A homogeneous point.
00034  * @author Daniel Beck
00035  */
00036 
00037 /**Constructor.
00038  * @param x the x-coordinate
00039  * @param y the y-coordinate
00040  * @param z the z-coordinate
00041  */
00042 HomPoint::HomPoint(float x, float y, float z)
00043   : HomCoord(x, y, z, 1.0)
00044 {
00045 }
00046 
00047 /** Constructor.
00048  * @param h a HomCoord
00049  */
00050 HomPoint::HomPoint(const HomCoord& h)
00051   : HomCoord(h)
00052 {
00053   if ( 1.0 != w() )
00054     { 
00055       printf("HomPoint(const HomCoord& h): The forth component of a "
00056              "homogeneous point has to be 1.0 but is %f\n", w());
00057       throw std::exception(); 
00058     }
00059 }
00060 
00061 /** Destructor */
00062 HomPoint::~HomPoint()
00063 {
00064 }
00065 
00066 /** Obtain distance from the point to the origin.
00067  * @return distance to origin
00068  */
00069 float
00070 HomPoint::distance() const
00071 {
00072   float d = sqrt( x() * x() + y() * y() + z() * z() );
00073   return d;
00074 }
00075 
00076 /** Move the point by the given coordiantes.
00077  * @param dx x-offset
00078  * @param dy y-offset
00079  * @param dz z-offset
00080  * @return reference to the moved point
00081  */
00082 HomPoint&
00083 HomPoint::move(float dx, float dy, float dz)
00084 {
00085   this->x() += dx;
00086   this->y() += dy;
00087   this->z() += dz;
00088 
00089   return *this;
00090 }
00091 
00092 /** Move the point to the given coordiantes.
00093  * @param x new x-coordinate
00094  * @param y new y-coordinate
00095  * @param z new z-coordinate
00096  * @return reference to the moved point
00097  */
00098 HomPoint&
00099 HomPoint::move_to(float x, float y, float z)
00100 {
00101   this->x() = x;
00102   this->y() = y;
00103   this->z() = z;
00104 
00105   return *this;
00106 }
00107 
00108 /** Compute the vector between two points.                                                                                                
00109  * @param p the other point                                                                                                               
00110  * @return the vector between the two points                                                                                              
00111  */
00112 HomVector
00113 HomPoint::operator-(const HomPoint& p) const
00114 {
00115   HomVector v;
00116   v.x( x() - p.x() );
00117   v.y( y() - p.y() );
00118   v.z( z() - p.z() );
00119   
00120   return v;
00121 }
00122 
00123 } // end namespace fawkes