HighMap library (C++)
Loading...
Searching...
No Matches
bbox2d.hpp
Go to the documentation of this file.
1/* Copyright (c) 2026 Otto Link. Distributed under the terms of the GNU General
2 Public License. The full license is in the file LICENSE, distributed with
3 this software. */
4
11#pragma once
12
13#include <algorithm>
14
15#include <glm/vec2.hpp>
16#include <glm/vec4.hpp>
17
18namespace hmap
19{
20
24struct BBox2D
25{
26 glm::vec2 min{0.f, 0.f};
27 glm::vec2 max{1.f, 1.f};
28
29 constexpr BBox2D() = default;
30 constexpr BBox2D(const glm::vec2 &min_p, const glm::vec2 &max_p)
31 : min(min_p), max(max_p)
32 {
33 }
34 constexpr BBox2D(float xmin, float xmax, float ymin, float ymax)
35 : min(xmin, ymin), max(xmax, ymax)
36 {
37 }
38
39 static constexpr BBox2D unit()
40 {
41 return BBox2D(glm::vec2(0.f, 0.f), glm::vec2(1.f, 1.f));
42 }
43
44 constexpr bool contains(const glm::vec2 &p) const
45 {
46 return p.x >= min.x && p.x <= max.x && p.y >= min.y && p.y <= max.y;
47 }
48
49 constexpr glm::vec2 clamp(const glm::vec2 &p) const
50 {
51 return {std::clamp(p.x, min.x, max.x), std::clamp(p.y, min.y, max.y)};
52 }
53
54 constexpr glm::vec2 size() const
55 {
56 return max - min;
57 }
58
59 constexpr glm::vec2 center() const
60 {
61 return 0.5f * (min + max);
62 }
63
64 constexpr glm::vec4 to_vec4() const
65 {
66 return {min.x, max.x, min.y, max.y};
67 }
68
69 static constexpr BBox2D from_vec4(const glm::vec4 &v)
70 {
71 return {{v.x, v.z}, {v.y, v.w}};
72 }
73};
74
75} // namespace hmap
Definition opencv.hpp:20
constexpr BBox2D(float xmin, float xmax, float ymin, float ymax)
Definition bbox2d.hpp:34
constexpr glm::vec4 to_vec4() const
Definition bbox2d.hpp:64
glm::vec2 min
Definition bbox2d.hpp:26
constexpr glm::vec2 center() const
Definition bbox2d.hpp:59
static constexpr BBox2D unit()
Definition bbox2d.hpp:39
constexpr BBox2D()=default
glm::vec2 max
Definition bbox2d.hpp:27
constexpr bool contains(const glm::vec2 &p) const
Definition bbox2d.hpp:44
constexpr glm::vec2 size() const
Definition bbox2d.hpp:54
constexpr BBox2D(const glm::vec2 &min_p, const glm::vec2 &max_p)
Definition bbox2d.hpp:30
static constexpr BBox2D from_vec4(const glm::vec4 &v)
Definition bbox2d.hpp:69
constexpr glm::vec2 clamp(const glm::vec2 &p) const
Definition bbox2d.hpp:49