source: rrlib_geometry/tLineSegment.hpp @ 24:627bf1924b86

Last change on this file since 24:627bf1924b86 was 24:627bf1924b86, checked in by Tobias Föhst <foehst@…>, 8 years ago

Added streaming operators for rrlib_canvas

File size: 6.9 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or
8// modify it under the terms of the GNU General Public License
9// as published by the Free Software Foundation; either version 2
10// of the License, or (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program; if not, write to the Free Software
19// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20//
21//----------------------------------------------------------------------
22/*!\file    tLineSegment.hpp
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2010-12-27
27 *
28 */
29//----------------------------------------------------------------------
30
31//----------------------------------------------------------------------
32// External includes (system with <>, local with "")
33//----------------------------------------------------------------------
34#include "rrlib/math/tLUDecomposition.h"
35
36//----------------------------------------------------------------------
37// Internal includes with ""
38//----------------------------------------------------------------------
39
40//----------------------------------------------------------------------
41// Debugging
42//----------------------------------------------------------------------
43#include <cassert>
44
45//----------------------------------------------------------------------
46// Namespace declaration
47//----------------------------------------------------------------------
48namespace rrlib
49{
50namespace geometry
51{
52
53//----------------------------------------------------------------------
54// Forward declarations / typedefs / enums
55//----------------------------------------------------------------------
56
57//----------------------------------------------------------------------
58// Const values
59//----------------------------------------------------------------------
60
61//----------------------------------------------------------------------
62// Implementation
63//----------------------------------------------------------------------
64
65//----------------------------------------------------------------------
66// tLineSegment constructors
67//----------------------------------------------------------------------
68template <size_t Tdimension, typename TElement>
69tLineSegment<Tdimension, TElement>::tLineSegment()
70{
71  this->end[0] = 1;
72}
73
74template <size_t Tdimension, typename TElement>
75tLineSegment<Tdimension, TElement>::tLineSegment(const typename tShape::tPoint &begin, const typename tShape::tPoint &end)
76    : tLine(begin, end - begin),
77    end(end)
78{}
79
80//----------------------------------------------------------------------
81// tLineSegment Set
82//----------------------------------------------------------------------
83template <size_t Tdimension, typename TElement>
84void tLineSegment<Tdimension, TElement>::Set(const typename tShape::tPoint &begin, const typename tShape::tPoint &end)
85{
86  tLine::Set(begin, end - begin);
87  this->end = end;
88}
89
90//----------------------------------------------------------------------
91// tLineSegment GetClosestPoint
92//----------------------------------------------------------------------
93template <size_t Tdimension, typename TElement>
94const typename tShape<Tdimension, TElement>::tPoint tLineSegment<Tdimension, TElement>::GetClosestPoint(const typename tShape::tPoint &reference_point) const
95{
96  math::tVector<Tdimension, TElement> from_begin_to_point(reference_point - this->Begin());
97  math::tVector<Tdimension, TElement> from_end_to_point(reference_point - this->End());
98
99  // angle between direction and vector begin->point larger than 90°
100  if (from_begin_to_point * this->Direction() < 0)
101  {
102    return this->Begin();
103  }
104
105  // angle between direction and vector end->point larger than 90°
106  if (from_end_to_point * this->Direction() > 0)
107  {
108    return this->End();
109  }
110
111  return tLine::GetClosestPoint(reference_point);
112}
113
114//----------------------------------------------------------------------
115// tLineSegment Translate
116//----------------------------------------------------------------------
117template <size_t Tdimension, typename TElement>
118tLineSegment<Tdimension, TElement> &tLineSegment<Tdimension, TElement>::Translate(const math::tVector<Tdimension, TElement> &translation)
119{
120  tLine::Translate(translation);
121  this->end += translation;
122  return *this;
123}
124
125//----------------------------------------------------------------------
126// tLineSegment Rotate
127//----------------------------------------------------------------------
128template <size_t Tdimension, typename TElement>
129tLineSegment<Tdimension, TElement> &tLineSegment<Tdimension, TElement>::Rotate(const math::tMatrix<Tdimension, Tdimension, TElement> &rotation)
130{
131  tLine::Rotate(rotation);
132  this->end = rotation * this->end;
133  return *this;
134}
135
136//----------------------------------------------------------------------
137// tLineSegment Transform
138//----------------------------------------------------------------------
139template <size_t Tdimension, typename TElement>
140tLineSegment<Tdimension, TElement> &tLineSegment<Tdimension, TElement>::Transform(const math::tMatrix < Tdimension + 1, Tdimension + 1, TElement > &transformation)
141{
142  tLine::Transform(transformation);
143  this->end = transformation.MultiplyHomogeneously(this->end);
144  return *this;
145}
146
147//----------------------------------------------------------------------
148// tLineSegment UpdateBoundingBox
149//----------------------------------------------------------------------
150template <size_t Tdimension, typename TElement>
151void tLineSegment<Tdimension, TElement>::UpdateBoundingBox(typename tShape::tBoundingBox &bounding_box) const
152{
153  bounding_box.Add(this->Begin());
154  bounding_box.Add(this->End());
155}
156
157//----------------------------------------------------------------------
158// tLineSegment UpdateCenterOfGravity
159//----------------------------------------------------------------------
160template <size_t Tdimension, typename TElement>
161void tLineSegment<Tdimension, TElement>::UpdateCenterOfGravity(typename tShape::tPoint &center_of_gravity) const
162{
163  center_of_gravity = 0.5 * (this->Begin() + this->End());
164}
165
166//----------------------------------------------------------------------
167// Operators for rrlib_canvas
168//----------------------------------------------------------------------
169#ifdef _LIB_RRLIB_CANVAS_PRESENT_
170
171template <typename TElement>
172canvas::tCanvas2D &operator << (canvas::tCanvas2D &canvas, const tLineSegment<2, TElement> &line_segment)
173{
174  canvas.DrawLineSegment(line_segment.Begin(), line_segment.End());
175
176  return canvas;
177}
178
179#endif
180
181//----------------------------------------------------------------------
182// End of namespace declaration
183//----------------------------------------------------------------------
184}
185}
Note: See TracBrowser for help on using the repository browser.