source: rrlib_geometry/tBoundingBox.hpp @ 19:85538eb6ac6a

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

Imported tKDTree

File size: 5.7 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    tBoundingBox.hpp
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2009-05-25
27 *
28 */
29//----------------------------------------------------------------------
30
31//----------------------------------------------------------------------
32// External includes (system with <>, local with "")
33//----------------------------------------------------------------------
34#include <limits>
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// tBoundingBox constructors
67//----------------------------------------------------------------------
68template <size_t Tdimension, typename TElement>
69tBoundingBox<Tdimension, TElement>::tBoundingBox()
70{
71  this->Reset();
72}
73
74template <size_t Tdimension, typename TElement>
75template <typename TIterator>
76tBoundingBox<Tdimension, TElement>::tBoundingBox(TIterator begin, TIterator end)
77{
78  this->Reset();
79  this->Add(begin, end);
80}
81
82//----------------------------------------------------------------------
83// tBoundingBox Reset
84//----------------------------------------------------------------------
85template <size_t Tdimension, typename TElement>
86void tBoundingBox<Tdimension, TElement>::Reset()
87{
88  this->min = tPoint::Identity() * std::numeric_limits<TElement>::max();
89  this->max = tPoint::Identity() * -std::numeric_limits<TElement>::max();
90}
91
92//----------------------------------------------------------------------
93// tBoundingBox Add
94//----------------------------------------------------------------------
95template <size_t Tdimension, typename TElement>
96void tBoundingBox<Tdimension, TElement>::Add(const tPoint &point)
97{
98  for (size_t i = 0; i < Tdimension; ++i)
99  {
100    this->min[i] = std::min(this->min[i], point[i]);
101    this->max[i] = std::max(this->max[i], point[i]);
102  }
103}
104
105template <size_t Tdimension, typename TElement>
106template <typename TIterator>
107void tBoundingBox<Tdimension, TElement>::Add(TIterator begin, TIterator end)
108{
109  for (auto it = begin; it != end; ++it)
110  {
111    this->Add(*it);
112  }
113}
114
115template <size_t Tdimension, typename TElement>
116void tBoundingBox<Tdimension, TElement>::Add(const tBoundingBox &other)
117{
118  this->Add(other.Min());
119  this->Add(other.Max());
120}
121
122//----------------------------------------------------------------------
123// tBoundingBox Intersects
124//----------------------------------------------------------------------
125template <size_t Tdimension, typename TElement>
126const bool tBoundingBox<Tdimension, TElement>::Intersects(const tBoundingBox &other) const
127{
128  for (size_t i = 0; i < Tdimension; ++i)
129  {
130    if ((this->min[i] > other.max[i]) || (other.min[i] > this->max[i]))
131    {
132      return false;
133    }
134  }
135
136  return true;
137}
138
139//----------------------------------------------------------------------
140// tBoundingBox GetIntersection
141//----------------------------------------------------------------------
142template <size_t Tdimension, typename TElement>
143const tBoundingBox<Tdimension, TElement> tBoundingBox<Tdimension, TElement>::GetIntersection(const tBoundingBox &other) const
144{
145  tBoundingBox<Tdimension, TElement> result;
146  for (size_t i = 0; i < Tdimension; ++i)
147  {
148    result.min[i] = std::min(this->min[i], other.min[i]);
149    result.max[i] = std::max(this->max[i], other.max[i]);
150  }
151
152  return result;
153}
154
155//----------------------------------------------------------------------
156// tBoundingBox Contains
157//----------------------------------------------------------------------
158template <size_t Tdimension, typename TElement>
159const bool tBoundingBox<Tdimension, TElement>::Contains(const tPoint &point) const
160{
161  for (size_t i = 0; i < Tdimension; ++i)
162  {
163    if ((this->min[i] > point[i]) || (point[i] > this->max[i]))
164    {
165      return false;
166    }
167  }
168  return true;
169}
170
171//----------------------------------------------------------------------
172// End of namespace declaration
173//----------------------------------------------------------------------
174}
175}
Note: See TracBrowser for help on using the repository browser.