All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Line3.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Open Source Robotics Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16 */
17 #ifndef _IGNITION_LINE3_HH_
18 #define _IGNITION_LINE3_HH_
19 
20 #include <algorithm>
21 #include <ignition/math/Vector3.hh>
23 
24 namespace ignition
25 {
26  namespace math
27  {
31  template<typename T>
32  class Line3
33  {
35  public: Line3() = default;
36 
39  public: Line3(const Line3<T> &_line)
40  {
41  this->pts[0] = _line[0];
42  this->pts[1] = _line[1];
43  }
44 
48  public: Line3(const math::Vector3<T> &_ptA, const math::Vector3<T> &_ptB)
49  {
50  this->Set(_ptA, _ptB);
51  }
52 
58  public: Line3(const double _x1, const double _y1,
59  const double _x2, const double _y2)
60  {
61  this->Set(_x1, _y1, _x2, _y2);
62  }
63 
71  public: Line3(const double _x1, const double _y1,
72  const double _z1, const double _x2,
73  const double _y2, const double _z2)
74  {
75  this->Set(_x1, _y1, _z1, _x2, _y2, _z2);
76  }
77 
81  public: void Set(const math::Vector3<T> &_ptA,
82  const math::Vector3<T> &_ptB)
83  {
84  this->pts[0] = _ptA;
85  this->pts[1] = _ptB;
86  }
87 
90  public: void SetA(const math::Vector3<T> &_ptA)
91  {
92  this->pts[0] = _ptA;
93  }
94 
97  public: void SetB(const math::Vector3<T> &_ptB)
98  {
99  this->pts[1] = _ptB;
100  }
101 
110  public: void Set(const double _x1, const double _y1,
111  const double _x2, const double _y2,
112  const double _z = 0)
113  {
114  this->pts[0].Set(_x1, _y1, _z);
115  this->pts[1].Set(_x2, _y2, _z);
116  }
117 
125  public: void Set(const double _x1, const double _y1,
126  const double _z1, const double _x2,
127  const double _y2, const double _z2)
128  {
129  this->pts[0].Set(_x1, _y1, _z1);
130  this->pts[1].Set(_x2, _y2, _z2);
131  }
132 
135  public: math::Vector3<T> Direction() const
136  {
137  return (this->pts[1] - this->pts[0]).Normalize();
138  }
139 
142  public: T Length() const
143  {
144  return this->pts[0].Distance(this->pts[1]);
145  }
146 
150  public: bool operator==(const Line3<T> &_line) const
151  {
152  return this->pts[0] == _line[0] && this->pts[1] == _line[1];
153  }
154 
158  public: bool operator!=(const Line3<T> &_line) const
159  {
160  return !(*this == _line);
161  }
162 
166  public: math::Vector3<T> operator[](const size_t _index) const
167  {
168  if (_index > 1)
169  throw IndexException();
170  return this->pts[_index];
171  }
172 
177  public: friend std::ostream &operator<<(
178  std::ostream &_out, const Line3<T> &_line)
179  {
180  _out << _line[0] << " " << _line[1];
181  return _out;
182  }
183 
187  public: Line3 &operator=(const Line3<T> &_line)
188  {
189  this->pts[0] = _line[0];
190  this->pts[1] = _line[1];
191 
192  return *this;
193  }
194 
196  private: math::Vector3<T> pts[2];
197  };
198 
202  }
203 }
204 #endif
math::Vector3< T > Direction() const
Get the direction of the line.
Definition: Line3.hh:135
T Length() const
Get the length of the line.
Definition: Line3.hh:142
void Set(const double _x1, const double _y1, const double _z1, const double _x2, const double _y2, const double _z2)
Set the start and end point of the line segment.
Definition: Line3.hh:125
void SetA(const math::Vector3< T > &_ptA)
Set the start point of the line segment.
Definition: Line3.hh:90
Line3(const Line3< T > &_line)
Copy constructor.
Definition: Line3.hh:39
Line3< int > Line3i
Definition: Line3.hh:199
Line3()=default
Line Constructor.
Line3< float > Line3f
Definition: Line3.hh:201
friend std::ostream & operator<<(std::ostream &_out, const Line3< T > &_line)
Stream extraction operator.
Definition: Line3.hh:177
void SetB(const math::Vector3< T > &_ptB)
Set the end point of the line segment.
Definition: Line3.hh:97
bool operator!=(const Line3< T > &_line) const
Inequality operator.
Definition: Line3.hh:158
void Set(const math::Vector3< T > &_ptA, const math::Vector3< T > &_ptB)
Set the start and end point of the line segment.
Definition: Line3.hh:81
Exception that is thrown when an out-of-bounds index is encountered.
Definition: IndexException.hh:37
bool operator==(const Line3< T > &_line) const
Equality operator.
Definition: Line3.hh:150
void Set(const double _x1, const double _y1, const double _x2, const double _y2, const double _z=0)
Set the start and end point of the line segment, assuming that both points have the same height...
Definition: Line3.hh:110
Line3(const math::Vector3< T > &_ptA, const math::Vector3< T > &_ptB)
Constructor.
Definition: Line3.hh:48
math::Vector3< T > operator[](const size_t _index) const
Get the start or end point.
Definition: Line3.hh:166
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:37
Line3 & operator=(const Line3< T > &_line)
Assignment operator.
Definition: Line3.hh:187
A three dimensional line segment.
Definition: Line3.hh:32
Line3< double > Line3d
Definition: Line3.hh:200
Line3(const double _x1, const double _y1, const double _z1, const double _x2, const double _y2, const double _z2)
Constructor.
Definition: Line3.hh:71
Line3(const double _x1, const double _y1, const double _x2, const double _y2)
2D Constructor where Z coordinates are 0
Definition: Line3.hh:58