RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
Point.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2009-2014 Klaus Post
5 Copyright (C) 2017 Roman Lebedev
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library 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 GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#pragma once
23
24#include "rawspeedconfig.h"
25#include "adt/Casts.h"
26#include <algorithm>
27#include <cmath>
28#include <cstdint>
29#include <cstdlib>
30#include <tuple>
31#include <type_traits>
32
33namespace rawspeed {
34
35class iPoint2D final {
36public:
39
40 constexpr iPoint2D() = default;
41 constexpr iPoint2D(value_type a, value_type b) : x(a), y(b) {}
42
43 constexpr iPoint2D operator+(const iPoint2D& rhs) const {
44 return {x + rhs.x, y + rhs.y};
45 }
46 constexpr iPoint2D operator-(const iPoint2D& rhs) const {
47 return {x - rhs.x, y - rhs.y};
48 }
49
51 *this = operator+(rhs);
52 return *this;
53 }
55 *this = operator-(rhs);
56 return *this;
57 }
58
59 constexpr bool RAWSPEED_READONLY operator==(const iPoint2D& rhs) const {
60 return x == rhs.x && y == rhs.y;
61 }
62
63 constexpr bool RAWSPEED_READONLY operator>(const iPoint2D& rhs) const {
64 return x > rhs.x && y > rhs.y;
65 }
66 constexpr bool RAWSPEED_READONLY operator<(const iPoint2D& rhs) const {
67 return x < rhs.x && y < rhs.y;
68 }
69
70 constexpr bool RAWSPEED_READONLY operator>=(const iPoint2D& rhs) const {
71 return x >= rhs.x && y >= rhs.y;
72 }
73 constexpr bool RAWSPEED_READONLY operator<=(const iPoint2D& rhs) const {
74 return x <= rhs.x && y <= rhs.y;
75 }
76
77 [[nodiscard]] bool RAWSPEED_READONLY hasPositiveArea() const {
78 return *this > iPoint2D(0, 0);
79 }
80
81 [[nodiscard]] area_type RAWSPEED_READONLY area() const {
82 using signed_area = std::make_signed_t<area_type>;
83
84 area_type x_abs = std::abs(static_cast<signed_area>(x));
85 area_type y_abs = std::abs(static_cast<signed_area>(y));
86
87 return x_abs * y_abs;
88 }
89
90 [[nodiscard]] constexpr bool RAWSPEED_READONLY
91 isThisInside(const iPoint2D& rhs) const {
92 return *this <= rhs;
93 }
94
95 [[nodiscard]] constexpr iPoint2D getSmallest(const iPoint2D& rhs) const {
96 return {
97 std::min(x, rhs.x),
98 std::min(y, rhs.y),
99 };
100 }
101
104};
105
106/* Helper class for managing a rectangle in 2D space. */
107class iRectangle2D final {
108public:
109 constexpr iRectangle2D() = default;
110 constexpr iRectangle2D(const iPoint2D& pos_, const iPoint2D& dim_)
111 : pos(pos_), dim(dim_) {}
112
113 constexpr iRectangle2D(int w, int h) : dim({w, h}) {}
114 constexpr iRectangle2D(int x_pos, int y_pos, int w, int h)
115 : pos({x_pos, y_pos}), dim({w, h}) {}
116
117 [[nodiscard]] constexpr int getTop() const { return pos.y; }
118 [[nodiscard]] constexpr int getBottom() const { return pos.y + dim.y; }
119 [[nodiscard]] constexpr int getLeft() const { return pos.x; }
120 [[nodiscard]] constexpr int getRight() const { return pos.x + dim.x; }
121 [[nodiscard]] constexpr int getWidth() const { return dim.x; }
122 [[nodiscard]] constexpr int getHeight() const { return dim.y; }
123 [[nodiscard]] constexpr iPoint2D getTopLeft() const { return pos; }
124 [[nodiscard]] constexpr iPoint2D getBottomRight() const { return pos + dim; }
125 [[nodiscard]] constexpr iPoint2D getTopRight() const {
126 return pos + iPoint2D(dim.x, 0);
127 }
128 [[nodiscard]] constexpr iPoint2D getBottomLeft() const {
129 return pos + iPoint2D(0, dim.y);
130 }
131 [[nodiscard]] constexpr bool RAWSPEED_READONLY hasPositiveArea() const {
132 return (dim.x > 0) && (dim.y > 0);
133 }
134
135 [[nodiscard]] constexpr bool RAWSPEED_READONLY
136 isPointInside(const iPoint2D& subPoint) const {
137 return subPoint >= getTopLeft() && subPoint < getBottomRight();
138 }
139
140 [[nodiscard]] constexpr bool RAWSPEED_READONLY
141 isPointInsideInclusive(const iPoint2D& subPoint) const {
142 return subPoint >= getTopLeft() && subPoint <= getBottomRight();
143 }
144
145 [[nodiscard]] constexpr bool RAWSPEED_READONLY
146 isThisInside(const iRectangle2D& superRect) const {
147 return getTopLeft() >= superRect.getTopLeft() &&
148 getBottomRight() <= superRect.getBottomRight();
149 }
150
151 [[nodiscard]] auto area() const {
152 return implicit_cast<unsigned int>(dim.area());
153 }
154
155 void offset(const iPoint2D& offset_) { pos += offset_; }
156
157 /* Retains size */
158 void setTopLeft(const iPoint2D& top_left) { pos = top_left; }
159
160 /* Set BR */
161 void setBottomRightAbsolute(const iPoint2D& bottom_right) {
162 dim = bottom_right - pos;
163 }
164
165 void setAbsolute(const iPoint2D& top_left, const iPoint2D& bottom_right) {
166 pos = top_left;
167 setBottomRightAbsolute(bottom_right);
168 }
169 void setAbsolute(int x1, int y1, int x2, int y2) {
170 setAbsolute({x1, y1}, {x2, y2});
171 }
172
173 void setSize(const iPoint2D& size) { dim = size; }
174
175 /* Crop, so area is positive, and return true, if there is any area left */
176 /* This will ensure that bottomright is never on the left/top of the offset */
177 bool cropArea() {
178 dim.x = std::max(0, dim.x);
179 dim.y = std::max(0, dim.y);
180 return hasPositiveArea();
181 }
182
183 /* This will make sure that offset is positive, and make the area smaller if
184 * needed */
185 /* This will return true if there is any area left */
187 iPoint2D crop_pixels;
188 if (pos.x < 0) {
189 crop_pixels.x = -(pos.x);
190 pos.x = 0;
191 }
192 if (pos.y < 0) {
193 crop_pixels.y = -pos.y;
194 pos.y = 0;
195 }
196 dim -= crop_pixels;
197 return cropArea();
198 }
199
200 [[nodiscard]] iRectangle2D getOverlap(const iRectangle2D& other) const {
201 iRectangle2D overlap;
202 iPoint2D br1 = getBottomRight();
203 iPoint2D br2 = other.getBottomRight();
204 overlap.setAbsolute(std::max(pos.x, other.pos.x),
205 std::max(pos.y, other.pos.y), std::min(br1.x, br2.x),
206 std::min(br1.y, br2.y));
207 return overlap;
208 }
209
210 [[nodiscard]] iRectangle2D combine(const iRectangle2D& other) const {
211 iRectangle2D combined;
212 iPoint2D br1 = getBottomRight();
213 iPoint2D br2 = other.getBottomRight();
214 combined.setAbsolute(std::min(pos.x, other.pos.x),
215 std::min(pos.y, other.pos.y), std::max(br1.x, br2.x),
216 std::max(br2.y, br2.y));
217 return combined;
218 }
219
222};
223
224inline bool RAWSPEED_READONLY operator==(const iRectangle2D& a,
225 const iRectangle2D b) {
226 return std::tie(a.pos, a.dim) == std::tie(b.pos, b.dim);
227}
228
229} // namespace rawspeed
constexpr iPoint2D()=default
value_type x
Definition Point.h:102
value_type y
Definition Point.h:103
constexpr iPoint2D operator-(const iPoint2D &rhs) const
Definition Point.h:46
constexpr iPoint2D()=default
constexpr bool RAWSPEED_READONLY isThisInside(const iPoint2D &rhs) const
Definition Point.h:91
iPoint2D & operator+=(const iPoint2D &rhs)
Definition Point.h:50
constexpr bool RAWSPEED_READONLY operator<=(const iPoint2D &rhs) const
Definition Point.h:73
constexpr bool RAWSPEED_READONLY operator<(const iPoint2D &rhs) const
Definition Point.h:66
area_type RAWSPEED_READONLY area() const
Definition Point.h:81
constexpr bool RAWSPEED_READONLY operator>=(const iPoint2D &rhs) const
Definition Point.h:70
constexpr bool RAWSPEED_READONLY operator>(const iPoint2D &rhs) const
Definition Point.h:63
constexpr iPoint2D getSmallest(const iPoint2D &rhs) const
Definition Point.h:95
uint64_t area_type
Definition Point.h:38
constexpr iPoint2D(value_type a, value_type b)
Definition Point.h:41
bool RAWSPEED_READONLY hasPositiveArea() const
Definition Point.h:77
iPoint2D & operator-=(const iPoint2D &rhs)
Definition Point.h:54
constexpr bool RAWSPEED_READONLY operator==(const iPoint2D &rhs) const
Definition Point.h:59
constexpr iPoint2D operator+(const iPoint2D &rhs) const
Definition Point.h:43
int32_t value_type
Definition Point.h:37
constexpr iRectangle2D(const iPoint2D &pos_, const iPoint2D &dim_)
Definition Point.h:110
constexpr bool RAWSPEED_READONLY isPointInsideInclusive(const iPoint2D &subPoint) const
Definition Point.h:141
void setTopLeft(const iPoint2D &top_left)
Definition Point.h:158
constexpr int getTop() const
Definition Point.h:117
constexpr iRectangle2D(int w, int h)
Definition Point.h:113
constexpr bool RAWSPEED_READONLY isPointInside(const iPoint2D &subPoint) const
Definition Point.h:136
constexpr bool RAWSPEED_READONLY isThisInside(const iRectangle2D &superRect) const
Definition Point.h:146
constexpr iPoint2D getTopRight() const
Definition Point.h:125
constexpr bool RAWSPEED_READONLY hasPositiveArea() const
Definition Point.h:131
iRectangle2D combine(const iRectangle2D &other) const
Definition Point.h:210
constexpr int getBottom() const
Definition Point.h:118
constexpr iRectangle2D(int x_pos, int y_pos, int w, int h)
Definition Point.h:114
void setBottomRightAbsolute(const iPoint2D &bottom_right)
Definition Point.h:161
constexpr iRectangle2D()=default
bool cropOffsetToZero()
Definition Point.h:186
void setAbsolute(int x1, int y1, int x2, int y2)
Definition Point.h:169
constexpr int getHeight() const
Definition Point.h:122
constexpr int getRight() const
Definition Point.h:120
iRectangle2D getOverlap(const iRectangle2D &other) const
Definition Point.h:200
constexpr iPoint2D getBottomLeft() const
Definition Point.h:128
void offset(const iPoint2D &offset_)
Definition Point.h:155
constexpr iPoint2D getTopLeft() const
Definition Point.h:123
constexpr int getWidth() const
Definition Point.h:121
void setAbsolute(const iPoint2D &top_left, const iPoint2D &bottom_right)
Definition Point.h:165
constexpr int getLeft() const
Definition Point.h:119
void setSize(const iPoint2D &size)
Definition Point.h:173
constexpr iPoint2D getBottomRight() const
Definition Point.h:124
auto area() const
Definition Point.h:151
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32
bool operator==(const AlignedAllocator< T1, A1 > &, const AlignedAllocator< T2, A2 > &)