OrientedBox.hh
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 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_MATH_ORIENTEDBOX_HH_
18 #define IGNITION_MATH_ORIENTEDBOX_HH_
19 
20 #include <iostream>
21 #include <ignition/math/Helpers.hh>
22 #include <ignition/math/Matrix4.hh>
23 #include <ignition/math/Pose3.hh>
24 #include <ignition/math/Vector3.hh>
25 
26 namespace ignition
27 {
28  namespace math
29  {
32  template<typename T>
34  {
36  public: OrientedBox() : size(Vector3<T>::Zero), pose(Pose3<T>::Zero)
37  {
38  }
39 
44  public: OrientedBox(const Vector3<T> &_size, const Pose3<T> &_pose)
45  : size(_size), pose(_pose)
46  {
47  // Enforce non-negative size
48  this->size = this->size.Abs();
49  }
50 
54  public: explicit OrientedBox(const Vector3<T> &_size)
55  : size(_size), pose(Pose3<T>::Zero)
56  {
57  // Enforce non-negative size
58  this->size = this->size.Abs();
59  }
60 
63  public: OrientedBox(const OrientedBox<T> &_b)
64  : size(_b.size), pose(_b.pose)
65  {
66  }
67 
69  public: virtual ~OrientedBox()
70  {
71  }
72 
75  public: T XLength() const
76  {
77  return this->size.X();
78  }
79 
82  public: T YLength() const
83  {
84  return this->size.Y();
85  }
86 
89  public: T ZLength() const
90  {
91  return this->size.Z();
92  }
93 
96  public: const Vector3<T> &Size() const
97  {
98  return this->size;
99  }
100 
103  public: const Pose3<T> &Pose() const
104  {
105  return this->pose;
106  }
107 
111  public: void Size(Vector3<T> &_size)
112  {
113  // Enforce non-negative size
114  this->size = _size.Abs();
115  }
116 
119  public: void Pose(Pose3<T> &_pose)
120  {
121  this->pose = _pose;
122  }
123 
128  {
129  this->size = _b.size;
130  this->pose = _b.pose;
131  return *this;
132  }
133 
137  public: bool operator==(const OrientedBox<T> &_b) const
138  {
139  return this->size == _b.size && this->pose == _b.pose;
140  }
141 
145  public: bool operator!=(const OrientedBox<T> &_b) const
146  {
147  return this->size != _b.size || this->pose != _b.pose;
148  }
149 
154  public: friend std::ostream &operator<<(std::ostream &_out,
155  const OrientedBox<T> &_b)
156  {
157  _out << "Size[" << _b.Size() << "] Pose[" << _b.Pose() << "]";
158  return _out;
159  }
160 
164  public: bool Contains(const Vector3d &_p) const
165  {
166  // Move point to box frame
167  auto t = Matrix4<T>(this->pose).Inverse();
168  auto p = t *_p;
169 
170  return p.X() >= -this->size.X()*0.5 && p.X() <= this->size.X()*0.5 &&
171  p.Y() >= -this->size.Y()*0.5 && p.Y() <= this->size.Y()*0.5 &&
172  p.Z() >= -this->size.Z()*0.5 && p.Z() <= this->size.Z()*0.5;
173  }
174 
176  private: Vector3<T> size;
177 
179  private: Pose3<T> pose;
180  };
181 
185  }
186 }
187 #endif
T X() const
Get the x value.
Definition: Vector3.hh:635
OrientedBox & operator=(const OrientedBox< T > &_b)
Assignment operator.
Definition: OrientedBox.hh:127
const Pose3< T > & Pose() const
Get the box pose, which is the pose of its center.
Definition: OrientedBox.hh:103
OrientedBox(const Vector3< T > &_size)
Constructor which takes only the size.
Definition: OrientedBox.hh:54
const Vector3< T > & Size() const
Get the size of the box.
Definition: OrientedBox.hh:96
Encapsulates a position and rotation in three space.
Definition: Pose3.hh:30
A 4x4 matrix class.
Definition: Matrix4.hh:33
Vector3 Abs() const
Get the absolute value of the vector.
Definition: Vector3.hh:219
OrientedBox()
Default constructor.
Definition: OrientedBox.hh:36
OrientedBox< int > OrientedBoxi
Definition: OrientedBox.hh:182
bool operator!=(const OrientedBox< T > &_b) const
Inequality test operator.
Definition: OrientedBox.hh:145
void Size(Vector3< T > &_size)
Set the box size.
Definition: OrientedBox.hh:111
OrientedBox< double > OrientedBoxd
Definition: OrientedBox.hh:183
virtual ~OrientedBox()
Destructor.
Definition: OrientedBox.hh:69
T ZLength() const
Get the length along the z dimension.
Definition: OrientedBox.hh:89
friend std::ostream & operator<<(std::ostream &_out, const OrientedBox< T > &_b)
Output operator.
Definition: OrientedBox.hh:154
The Vector3 class represents the generic vector containing 3 elements.
Definition: Vector3.hh:36
bool operator==(const OrientedBox< T > &_b) const
Equality test operator.
Definition: OrientedBox.hh:137
OrientedBox< float > OrientedBoxf
Definition: OrientedBox.hh:184
Mathematical representation of a box which can be arbitrarily positioned and rotated.
Definition: OrientedBox.hh:33
OrientedBox(const OrientedBox< T > &_b)
Copy constructor.
Definition: OrientedBox.hh:63
OrientedBox(const Vector3< T > &_size, const Pose3< T > &_pose)
Constructor which takes size and pose.
Definition: OrientedBox.hh:44
bool Contains(const Vector3d &_p) const
Check if a point lies inside the box.
Definition: OrientedBox.hh:164
void Pose(Pose3< T > &_pose)
Set the box pose.
Definition: OrientedBox.hh:119
Definition: Angle.hh:38
T XLength() const
Get the length along the x dimension.
Definition: OrientedBox.hh:75
T YLength() const
Get the length along the y dimension.
Definition: OrientedBox.hh:82