Fawkes API
Fawkes Development Version
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
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
*/
37
LineSegment::LineSegment
(
const
HomPoint
& a,
const
HomPoint
& b)
38
: m_p1(a),
39
m_p2(b)
40
{
41
register_primitives
();
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
*/
49
LineSegment::LineSegment
(
const
HomPoint
& p,
const
HomVector
& v)
50
: m_p1(p),
51
m_p2(p+v)
52
{
53
register_primitives
();
54
}
55
56
/** Copy constructor.
57
* @param l another line segment
58
*/
59
LineSegment::LineSegment
(
const
LineSegment
& l)
60
: m_p1(l.m_p1),
61
m_p2(l.m_p2)
62
{
63
clear_primitives
();
64
register_primitives
();
65
}
66
67
/** Destructor. */
68
LineSegment::~LineSegment
()
69
{
70
}
71
72
/** Get the length of the line segment.
73
* @return the length of the line segment
74
*/
75
float
76
LineSegment::length
()
const
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
&
87
LineSegment::p1
()
const
88
{
89
return
m_p1;
90
}
91
92
/** Get the endpoint.
93
* @return the endpoint
94
*/
95
const
HomPoint
&
96
LineSegment::p2
()
const
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
106
LineSegment::project
(
const
HomPoint
&p)
const
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
118
LineSegment::register_primitives
()
119
{
120
add_primitive
(&m_p1);
121
add_primitive
(&m_p2);
122
}
123
124
void
125
LineSegment::post_transform
()
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
src
libs
geometry
line_segment.cpp
Generated by
1.8.3.1