Fawkes API  Fawkes Development Version
hom_coord.cpp
1 
2 /***************************************************************************
3  * hom_coord.cpp - Homogeneous coordinate
4  *
5  * Created: Thu Sep 27 16:21:24 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_coord.h>
25 #include <geometry/hom_transform.h>
26 #include <geometry/vector.h>
27 
28 #include <cstdio>
29 #include <iomanip>
30 
31 namespace fawkes {
32 
33 /** @class HomCoord geometry/hom_coord.h
34  * Base class for homogeneous primitives (vector and point).
35  * @author Daniel Beck
36  */
37 
38 /** @var HomCoord::m_vector
39  * The internal data container.
40  */
41 
42 /** Constructor.
43  * @param x the x-coordinate
44  * @param y the y-coordinate
45  * @param z the z-coordinate
46  * @param w the w-coordinate
47  */
48 HomCoord::HomCoord(float x, float y, float z, float w)
49 {
50  m_vector = new Vector(4);
51 
52  m_vector->set(0, x);
53  m_vector->set(1, y);
54  m_vector->set(2, z);
55  m_vector->set(3, w);
56 }
57 
58 /** Copy constructor.
59  * @param c another HomCoord
60  */
62 {
63  const Vector v = *(c.m_vector);
64  m_vector = new Vector(v);
65 }
66 
67 /** Constructor.
68  * @param v a vector
69  */
71 {
72  m_vector = new Vector(v);
73 }
74 
75 /** Destructor. */
77 {
78  delete m_vector;
79 }
80 
81 /** RO-getter for x.
82  * @return the value
83  */
84 float
85 HomCoord::x() const
86 {
87  return m_vector->get(0);
88 }
89 
90 /** RW-getter for x.
91  * @return a reference to the x-element
92  */
93 float&
95 {
96  float& val = m_vector->get(0);
97  return val;
98 }
99 
100 /** Setter function for x.
101  * @param x the new x value
102  * @return reference to this
103  */
104 HomCoord&
106 {
107  m_vector->set(0, x);
108  return *this;
109 }
110 
111 /** RO-getter for y.
112  * @return the value
113  */
114 float
115 HomCoord::y() const
116 {
117  return m_vector->get(1);
118 }
119 
120 /** RW-getter for y.
121  * @return a reference to the y-element
122  */
123 float&
125 {
126  float& val = m_vector->get(1);
127  return val;
128 }
129 
130 /** Setter function for y.
131  * @param y the new y value
132  * @return reference to this
133  */
134 HomCoord&
136 {
137  m_vector->set(1, y);
138  return *this;
139 }
140 
141 /** RO-getter for z.
142  * @return the value
143  */
144 float
145 HomCoord::z() const
146 {
147  return m_vector->get(2);
148 }
149 
150 /** RW-getter for z.
151  * @return a reference to the z-element
152  */
153 float&
155 {
156  float& val = m_vector->get(2);
157  return val;
158 }
159 
160 /** Setter function for z.
161  * @param z the new z value
162  * @return reference to this
163  */
164 HomCoord&
166 {
167  m_vector->set(2, z);
168  return *this;
169 }
170 
171 /** RO-getter for w.
172  * @return the value
173  */
174 float
175 HomCoord::w() const
176 {
177  return m_vector->get(3);
178 }
179 
180 /** RW-getter for w.
181  * @return a reference to the w-element
182  */
183 float&
185 {
186  float& val = m_vector->get(3);
187  return val;
188 }
189 
190 /** Setter function for w.
191  * @param w the new w value
192  * @return reference to this
193  */
194 HomCoord&
196 {
197  m_vector->set(3, w);
198  return *this;
199 }
200 
201 /** Convenience function to rotate the HomCoord around the x-axis.
202  * @param rad the roation angle in rad
203  * @return reference to this
204  */
205 HomCoord&
207 {
208  HomTransform t;
209  t.rotate_x(rad);
210  transform(t);
211 
212  return *this;
213 }
214 
215 /** Convenience function to rotate the HomCoord around the y-axis.
216  * @param rad the roation angle in rad
217  * @return reference to this
218  */
219 HomCoord&
221 {
222  HomTransform t;
223  t.rotate_y(rad);
224  transform(t);
225 
226  return *this;
227 }
228 
229 /** Convenience function to rotate the HomCoord around the z-axis.
230  * @param rad the roation angle in rad
231  * @return reference to this
232  */
233 HomCoord&
235 {
236  HomTransform t;
237  t.rotate_z(rad);
238  transform(t);
239 
240  return *this;
241 }
242 
243 /** Subtraction operator.
244  * @param h the rhs HomCoord
245  * @return the resulting HomCoord
246  */
247 HomCoord
249 {
250  Vector v = (*m_vector) - (*h.m_vector);
251  HomCoord result(v);
252  float w = result.w();
253  result.w() = (w > 1.0) ? 1.0 : w;
254  return result;
255 }
256 
257 /** Substraction-assignment operator.
258  * @param h the rhs HomCoord
259  * @return reference to the resulting HomCoord
260  */
261 HomCoord&
263 {
264  (*m_vector) -= (*h.m_vector);
265  float w = this->w();
266  this->w() = (w > 1.0) ? 1.0 : w;
267  return *this;
268 }
269 
270 /** Addition operator.
271  * @param h the rhs HomCoord
272  * @return the resulting HomCoord
273  */
274 HomCoord
276 {
277  Vector v = (*m_vector) + (*h.m_vector);
278  HomCoord result(v);
279  float w = result.w();
280  result.w() = (w > 1.0) ? 1.0 : w;
281  return result;
282 }
283 
284 /** Addition-assignment operator.
285  * @param h the rhs HomCoord
286  * @return reference to the resulting HomCoord
287  */
288 HomCoord&
290 {
291  (*m_vector) += (*h.m_vector);
292  float w = this->w();
293  this->w() = (w > 1.0) ? 1.0 : w;
294  return *this;
295 }
296 
297 
298 /** Assignment operator.
299  * @param h the rhs HomCoord
300  * @return a reference of the lhs vector (this)
301  */
302 HomCoord&
304 {
305  (*m_vector) = (*h.m_vector);
306 
307  return *this;
308 }
309 
310 /** Calculates the dot product of two coords.
311  * @param h the rhs HomCoord
312  * @return the scalar product
313  */
314 float
316 {
317  return x() * h.x() + y() * h.y() + z() * h.z();
318 }
319 
320 /** Mulitplication operator.
321  * Multiply the vector with a scalar.
322  * @param s a scalar
323  * @return the result of multiplying the vector with the scalar
324  */
325 HomCoord
326 HomCoord::operator*(const float s) const
327 {
328  HomCoord result;
329  result.x() = x() * s;
330  result.y() = y() * s;
331  result.z() = z() * s;
332  result.w() = w();
333 
334  return result;
335 }
336 
337 /** Multiplication-assignment operator.
338  * Multiply the vector with a scalar.
339  * @param s a scalar
340  * @return a reference to the modified vector (this)
341  */
342 HomCoord&
343 HomCoord::operator*=(const float s)
344 {
345  x() *= s;
346  y() *= s;
347  z() *= s;
348 
349  return *this;
350 }
351 
352 /** Comparison operator.
353  * @param h the other HomCoord
354  * @return true if h is equal to *this, false otherwise
355  */
356 bool
358 {
359  return (*m_vector == *h.m_vector) ? true : false;
360 }
361 
362 /** Inequality operator.
363  * @param h the other HomCoord
364  * @return true if h is not equal to *this, false otherwise
365  */
366 bool
368 {
369  return (*m_vector == *h.m_vector) ? false : true;
370 }
371 
372 /** Appends the components of the HomCoord to the ostream.
373  * @param stream to be extended
374  * @return the extended stream
375  */
376 std::ostream&
377 HomCoord::print(std::ostream& stream) const
378 {
379  return stream << "[" << x() << ", " << y() << ", " << z() << ", " << w() << "]T";
380 }
381 
382 /** Transform the vector with the given transform.
383  * @param t a transform
384  * @return reference to the modified vector (this)
385  */
386 HomCoord&
388 {
389  Matrix m = t.get_matrix();
390  (*m_vector) = m * (*m_vector);
391 
392  return *this;
393 }
394 
395 } // end namespace fawkes
396 
virtual float y() const
RO-getter for y.
Definition: hom_coord.cpp:115
virtual HomCoord & rotate_x(float rad)
Convenience function to rotate the HomCoord around the x-axis.
Definition: hom_coord.cpp:206
const Matrix & get_matrix() const
Returns a copy of the matrix.
virtual HomCoord & operator=(const HomCoord &h)
Assignment operator.
Definition: hom_coord.cpp:303
Fawkes library namespace.
A simple column vector.
Definition: vector.h:31
A general matrix class.
Definition: matrix.h:33
virtual bool operator!=(const HomCoord &h) const
Inequality operator.
Definition: hom_coord.cpp:367
virtual bool operator==(const HomCoord &h) const
Comparison operator.
Definition: hom_coord.cpp:357
Vector * m_vector
The internal data container.
Definition: hom_coord.h:85
float get(unsigned int d) const
Get a certain element.
Definition: vector.cpp:146
void rotate_z(float rad)
Add rotation around the z-axis.
virtual ~HomCoord()
Destructor.
Definition: hom_coord.cpp:76
virtual HomCoord & rotate_y(float rad)
Convenience function to rotate the HomCoord around the y-axis.
Definition: hom_coord.cpp:220
This class describes a homogeneous transformation.
Definition: hom_transform.h:31
virtual HomCoord operator-(const HomCoord &h) const
Subtraction operator.
Definition: hom_coord.cpp:248
virtual HomCoord & operator-=(const HomCoord &h)
Substraction-assignment operator.
Definition: hom_coord.cpp:262
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
HomCoord(const HomCoord &c)
Copy constructor.
Definition: hom_coord.cpp:61
virtual HomCoord & operator+=(const HomCoord &h)
Addition-assignment operator.
Definition: hom_coord.cpp:289
void rotate_y(float rad)
Add rotation around the y-axis.
HomCoord & transform(const HomTransform &t)
Transform the vector with the given transform.
Definition: hom_coord.cpp:387
virtual float operator*(const HomCoord &h) const
Calculates the dot product of two coords.
Definition: hom_coord.cpp:315
virtual HomCoord & rotate_z(float rad)
Convenience function to rotate the HomCoord around the z-axis.
Definition: hom_coord.cpp:234
virtual std::ostream & print(std::ostream &stream) const
Appends the components of the HomCoord to the ostream.
Definition: hom_coord.cpp:377
virtual HomCoord operator+(const HomCoord &h) const
Addition operator.
Definition: hom_coord.cpp:275
void rotate_x(float rad)
Add rotation around the x-axis.
virtual HomCoord & operator*=(const float s)
Multiplication-assignment operator.
Definition: hom_coord.cpp:343
virtual float x() const
RO-getter for x.
Definition: hom_coord.cpp:85
void set(unsigned int d, float v)
Set a certain element.
Definition: vector.cpp:176