Easy3D 2.6.1
Loading...
Searching...
No Matches
rect.h
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#ifndef EASY3D_CORE_RECTANGLE_H
28#define EASY3D_CORE_RECTANGLE_H
29
30#include <algorithm>
31
32#include <easy3d/core/vec.h>
33
34
35namespace easy3d {
36
44 template <typename FT>
46 public:
52 GenericRect(const Vec<2, FT>& p, const Vec<2, FT>& q) {
53 x_min_ = std::min(q.x, p.x);
54 x_max_ = std::max(q.x, p.x);
55 y_min_ = std::min(q.y, p.y);
56 y_max_ = std::max(q.y, p.y);
57 }
58
67 GenericRect(FT xmin, FT xmax, FT ymin, FT ymax)
68 : x_min_(std::min(xmin, xmax))
69 , y_min_(std::min(ymin, ymax))
70 , x_max_(std::max(xmin, xmax))
71 , y_max_(std::max(ymin, ymax))
72 {
73 }
74
79 FT& x_min() { return x_min_; }
84 FT& y_min() { return y_min_; }
89 FT& x_max() { return x_max_; }
94 FT& y_max() { return y_max_; }
99 FT x_min() const { return x_min_; }
104 FT y_min() const { return y_min_; }
109 FT x_max() const { return x_max_; }
114 FT y_max() const { return y_max_; }
115
120 FT& x() { return x_min_; }
125 FT& y() { return y_min_; }
130 FT x() const { return x_min_; }
135 FT y() const { return y_min_; }
136
141 FT width() const { return x_max() - x_min(); }
146 FT height() const { return y_max() - y_min(); }
147
152 FT& left() { return x_min_; }
157 FT& top() { return y_min_; }
162 FT& right() { return x_max_; }
167 FT& bottom() { return y_max_; }
172 FT left() const { return x_min_; }
177 FT top() const { return y_min_; }
182 FT right() const { return x_max_; }
187 FT bottom() const { return y_max_; }
188
193 Vec<2, FT> top_left() const { return Vec<2, FT>(x_min_, y_min_); }
198 Vec<2, FT> bottom_right() const { return Vec<2, FT>(x_max_, y_max_); }
203 Vec<2, FT> top_right() const { return Vec<2, FT>(x_max_, y_min_); }
208 Vec<2, FT> bottom_left() const { return Vec<2, FT>(x_min_, y_max_); }
209
214 Vec<2, FT> min_point() const { return Vec<2, FT>(x_min_, y_min_); }
219 Vec<2, FT> max_point() const { return Vec<2, FT>(x_max_, y_max_); }
220
226 return Vec<2, FT>(FT(0.5) * (x_max() + x_min()), FT(0.5) * (y_max() + y_min()));
227 }
228
229 private:
230 FT x_min_;
231 FT y_min_;
232 FT x_max_;
233 FT y_max_;
234 };
235
236}
237
238
239#endif // EASY3D_CORE_RECTANGLE_H
FT & y()
Returns the minimum y-coordinate (alias for y_min).
Definition rect.h:125
FT & x_min()
Returns the minimum x-coordinate.
Definition rect.h:79
FT x_min() const
Returns the minimum x-coordinate (const version).
Definition rect.h:99
FT x_max() const
Returns the maximum x-coordinate (const version).
Definition rect.h:109
FT & top()
Returns the top coordinate (alias for y_min).
Definition rect.h:157
FT height() const
Returns the height of the rectangle.
Definition rect.h:146
FT top() const
Returns the top coordinate (alias for y_min) (const version).
Definition rect.h:177
FT right() const
Returns the right coordinate (alias for x_max) (const version).
Definition rect.h:182
FT y() const
Returns the minimum y-coordinate (alias for y_min) (const version).
Definition rect.h:135
FT width() const
Returns the width of the rectangle.
Definition rect.h:141
FT left() const
Returns the left coordinate (alias for x_min) (const version).
Definition rect.h:172
Vec< 2, FT > top_right() const
Returns the position of the rectangle's top-right corner.
Definition rect.h:203
FT & bottom()
Returns the bottom coordinate (alias for y_max).
Definition rect.h:167
FT & x_max()
Returns the maximum x-coordinate.
Definition rect.h:89
GenericRect(const Vec< 2, FT > &p, const Vec< 2, FT > &q)
Constructs a rectangle from two points p and q.
Definition rect.h:52
FT y_min() const
Returns the minimum y-coordinate (const version).
Definition rect.h:104
FT & left()
Returns the left coordinate (alias for x_min).
Definition rect.h:152
FT & y_min()
Returns the minimum y-coordinate.
Definition rect.h:84
Vec< 2, FT > top_left() const
Returns the position of the rectangle's top-left corner.
Definition rect.h:193
FT & y_max()
Returns the maximum y-coordinate.
Definition rect.h:94
FT & x()
Returns the minimum x-coordinate (alias for x_min).
Definition rect.h:120
Vec< 2, FT > bottom_right() const
Returns the position of the rectangle's bottom-right corner.
Definition rect.h:198
FT & right()
Returns the right coordinate (alias for x_max).
Definition rect.h:162
Vec< 2, FT > center() const
Returns the center point of the rectangle.
Definition rect.h:225
FT y_max() const
Returns the maximum y-coordinate (const version).
Definition rect.h:114
Vec< 2, FT > max_point() const
Returns the maximum point of the rectangle.
Definition rect.h:219
Vec< 2, FT > bottom_left() const
Returns the position of the rectangle's bottom-left corner.
Definition rect.h:208
Vec< 2, FT > min_point() const
Returns the minimum point of the rectangle.
Definition rect.h:214
FT x() const
Returns the minimum x-coordinate (alias for x_min) (const version).
Definition rect.h:130
FT bottom() const
Returns the bottom coordinate (alias for y_max) (const version).
Definition rect.h:187
GenericRect(FT xmin, FT xmax, FT ymin, FT ymax)
Constructs a rectangle from its min coordinates (xmin and ymin) and max coordinates (xmax and ymax).
Definition rect.h:67
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
Definition collider.cpp:182
FT min()
Function returning minimum representable value for a given type.
FT max()
Function returning maximum representable value for a given type.