Fawkes API  Fawkes Development Version
hom_vector.cpp
1 
2 /***************************************************************************
3  * hom_vector.cpp - Homogenous vector
4  *
5  * Created: Wed Sep 26 17:14:08 2007
6  * Copyright 2007-2008 Daniel Beck
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <geometry/hom_vector.h>
25 #include <cmath>
26 #include <cstdio>
27 #include <exception>
28 
29 namespace fawkes {
30 
31 /** @class HomVector geometry/hom_vector.h
32  * A homogeneous vector.
33  * @author Daniel Beck
34  */
35 
36 /**Constructor.
37  * @param x the x-coordinate
38  * @param y the y-coordinate
39  * @param z the z-coordinate
40  */
41 HomVector::HomVector(float x, float y, float z)
42  : HomCoord(x, y, z, 0.0)
43 {
44 }
45 
46 /** Constructor.
47  * @param h a HomCoord
48  */
50  : HomCoord(h)
51 {
52  if ( 0.0 != w() )
53  {
54  printf("HomVector(const HomCoord& h): The fourth component of a "
55  "homogeneous vector has to be 0.0 but it is %f\n", w());
56  throw std::exception();
57  }
58 }
59 
60 /** Destructor. */
62 {
63 }
64 
65 /** Calculates the length of the vector
66  * @return the length
67  */
68 float
70 {
71  return sqrt( length_square() );
72 }
73 
74 /** Calculates the squared length length of the vector (faster than length
75  * @return the squared length
76  */
77 float
79 {
80  return x() * x() + y() * y() + z() * z();
81 }
82 
83 /** Brings the vector to unit-length.
84  * @return a reference to itself
85  */
86 HomVector&
88 {
89  set_length(1.0);
90 
91  return *this;
92 }
93 
94 /** Scales the vector such that it has the given length.
95  * @param length the new length
96  * @return reference to a vector with given length
97  */
98 HomVector&
99 HomVector::set_length(float length)
100 {
101  if (this->length() == 0.0) return *this;
102 
103  float scale_factor = length / this->length();
104 
105  x() = x() * scale_factor;
106  y() = y() * scale_factor;
107  z() = z() * scale_factor;
108 
109  return *this;
110 }
111 
112 /** Compute the angle between two vectors.
113  * @param v the other vector
114  * @return the angle (-M_PI ... M_PI)
115  */
116 float
118 {
119  if ( 0.0 == length() || 0.0 == v.length() )
120  { return 0.0; }
121 
122  float a = atan2f(v.y(), v.x()) - atan2f(y(), x());
123 
124  if ( a > M_PI ) { a -= 2 * M_PI; }
125  else if ( a < -M_PI ) { a += 2 * M_PI; }
126 
127  return a;
128 }
129 
130 } // end namespace fawkes
virtual float y() const
RO-getter for y.
Definition: hom_coord.cpp:115
float angle_xy(const HomVector &h) const
Compute the angle between two vectors.
Definition: hom_vector.cpp:117
Fawkes library namespace.
HomVector & set_length(float length)
Scales the vector such that it has the given length.
Definition: hom_vector.cpp:99
HomVector & unit()
Brings the vector to unit-length.
Definition: hom_vector.cpp:87
HomVector(float x=0, float y=0, float z=0)
Constructor.
Definition: hom_vector.cpp:41
float length() const
Calculates the length of the vector.
Definition: hom_vector.cpp:69
float length_square() const
Calculates the squared length length of the vector (faster than length.
Definition: hom_vector.cpp:78
virtual float z() const
RO-getter for z.
Definition: hom_coord.cpp:145
virtual float w() const
RO-getter for w.
Definition: hom_coord.cpp:175
Base class for homogeneous primitives (vector and point).
Definition: hom_coord.h:34
A homogeneous vector.
Definition: hom_vector.h:31
virtual ~HomVector()
Destructor.
Definition: hom_vector.cpp:61
virtual float x() const
RO-getter for x.
Definition: hom_coord.cpp:85