Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
line_segment.cpp
1 
2 /***************************************************************************
3  * line_segment.cpp - A line segment
4  *
5  * Created: Thu Oct 02 17:05:52 2008
6  * Copyright 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/line_segment.h>
25 
26 namespace fawkes {
27 
28 /** @class fawkes::LineSegment <geometry/line_segment.h>
29  * A line segment.
30  * @author Daniel Beck
31  */
32 
33 /** Constructor.
34  * @param a the starting point of the line segment
35  * @param b the endpoint of of the line segment
36  */
38  : m_p1(a),
39  m_p2(b)
40 {
42 }
43 
44 /** Constructor.
45  * @param p the starting point of the line segment
46  * @param v a vector defining orientation and length of the line
47  * segment
48  */
50  : m_p1(p),
51  m_p2(p+v)
52 {
54 }
55 
56 /** Copy constructor.
57  * @param l another line segment
58  */
60  : m_p1(l.m_p1),
61  m_p2(l.m_p2)
62 {
65 }
66 
67 /** Destructor. */
69 {
70 }
71 
72 /** Get the length of the line segment.
73  * @return the length of the line segment
74  */
75 float
77 {
78  HomVector v;
79  v = m_p2 - m_p1;
80  return v.length();
81 }
82 
83 /** Get the starting point.
84  * @return the starting point
85  */
86 const HomPoint&
88 {
89  return m_p1;
90 }
91 
92 /** Get the endpoint.
93  * @return the endpoint
94  */
95 const HomPoint&
97 {
98  return m_p2;
99 }
100 
101 /** Project a point on this LineSegment
102  * @param p a point
103  * @return the projected point
104  */
105 HomPoint
107 {
108  HomVector direction = (m_p2 - m_p1).unit();
109  float factor = direction * (p - m_p1);
110  if (factor <= 0.0)
111  return m_p1;
112  if (factor >= length())
113  return m_p2;
114  return m_p1 + (direction * factor);
115 }
116 
117 void
119 {
120  add_primitive(&m_p1);
121  add_primitive(&m_p2);
122 }
123 
124 void
126 {
127 }
128 
129 std::ostream&
130 LineSegment::print(std::ostream& stream) const
131 {
132  stream << "P1: " << m_p1 << " P2: " << m_p2;
133  return stream;
134 }
135 
136 } // end namespace fawkes
void clear_primitives()
Clear the list of primitives.
const HomPoint & p1() const
Get the starting point.
A line segment.
Definition: line_segment.h:34
LineSegment(const HomPoint &a, const HomPoint &b)
Constructor.
float length() const
Get the length of the line segment.
const HomPoint & p2() const
Get the endpoint.
virtual void post_transform()
This method is called after the primitives are transformed.
A homogeneous point.
Definition: hom_point.h:33
float length() const
Calculates the length of the vector.
Definition: hom_vector.cpp:69
virtual void register_primitives()
Here, a derived class should register its primitives (HomPoints and HomVectors) by calling add_primit...
virtual ~LineSegment()
Destructor.
A homogeneous vector.
Definition: hom_vector.h:31
virtual std::ostream & print(std::ostream &stream) const
This method is called by the overloaded &lt;&lt;-operator.
void add_primitive(HomCoord *c)
Add a primitive to the list of primitives that is transformed.
HomPoint project(const HomPoint &p) const
Project a point on this LineSegment.