Grok 12.0.1
geometry.h
Go to the documentation of this file.
1
17#pragma once
18
19#include "grok.h"
20
21#include <iostream>
22#include <cstdint>
23#include <limits>
24#include <sstream>
25#include <atomic>
26
27#include "Logger.h"
28#include "grk_intmath.h"
29
30namespace grk
31{
32template<typename T>
33struct grk_pt
34{
35 grk_pt() : x(0), y(0) {}
36 grk_pt(T _x, T _y) : x(_x), y(_y) {}
37 T x;
38 T y;
39};
42
43template<typename T>
45{
46 grk_line() : x0(0), x1(0) {}
47 grk_line(T _x0, T _x1) : x0(_x0), x1(_x1) {}
48 T x0;
49 T x1;
50
51 T length() const
52 {
53 assert(x1 >= x0);
54 return (T)(x1 - x0);
55 }
56};
58
59template<typename T>
60struct grk_rect;
64
65template<typename T>
67{
68 static_assert(sizeof(T) <= 4);
69 if(val < (std::numeric_limits<T>::min)())
70 val = (std::numeric_limits<T>::min)();
71 else if(val > (std::numeric_limits<T>::max)())
72 val = (std::numeric_limits<T>::max)();
73 return (T)val;
74}
75
76template<typename T>
78{
79 return clip<T>(lhs + rhs);
80}
81
82template<typename T>
83T satAdd(T lhs, T rhs)
84{
85 return clip<T>((int64_t)lhs + rhs);
86}
87
88template<typename T>
89T satSub(T lhs, T rhs)
90{
91 return clip<T>((int64_t)lhs - rhs);
92}
93
94template<typename T>
96{
97 return clip<T>(lhs - rhs);
98}
99
100template<typename T>
102{
107 grk_rect(T x0, T y0, T x1, T y1) : grk_rect(x0, y0, x0, y0, x1, y1) {}
110 {
111 origin_x0 = rhs->origin_x0;
112 origin_y0 = rhs->origin_y0;
113 x0 = rhs->x0;
114 y0 = rhs->y0;
115 x1 = rhs->x1;
116 y1 = rhs->y1;
117 absoluteCoordinates = rhs->absoluteCoordinates;
118 }
119 grk_rect(void) : grk_rect(0, 0, 0, 0) {}
120 virtual ~grk_rect() = default;
121
124 T x0, y0, x1, y1;
125
127 {
129
130 assert(x0 >= origx);
131 assert(y0 >= origy);
132
135
136 return *this;
137 }
139 {
140 return setOrigin(&rhs, absolute);
141 }
143 {
145
146 if(rhs)
147 {
148 assert(x0 >= rhs->origin_x0);
149 assert(y0 >= rhs->origin_y0);
150 origin_x0 = rhs->origin_x0;
151 origin_y0 = rhs->origin_y0;
152 }
153
154 return *this;
155 }
157 {
158 assert(x0 >= origin_x0);
159 assert(y0 >= origin_y0);
162 absoluteCoordinates = false;
163
164 return *this;
165 }
167 {
170 absoluteCoordinates = true;
171
172 return *this;
173 }
174 virtual void print(void) const
175 {
176 Logger::logger_.info("[%u,%u,%u,%u,%u,%u]", origin_x0, origin_y0, x0, y0, x1, y1);
177 }
178 std::string boundsString() const
179 {
180 std::ostringstream os;
181 os << "[" << origin_x0 << "," << origin_y0 << "," << x0 << "," << y0 << "," << x1 << "," << y1
182 << "]";
183 return os.str();
184 }
185 bool valid(void) const
186 {
187 return x0 <= x1 && y0 <= y1;
188 }
189 bool empty(void) const
190 {
191 return x0 >= x1 || y0 >= y1;
192 }
194 {
195 return contains(pt.x, pt.y);
196 }
197 bool contains(T x, T y)
198 {
199 return x >= x0 && y >= y0 && x < x1 && y < y1;
200 }
202 {
203 return operator=(&rhs);
204 }
206 {
207 assert(rhs);
208 if(rhs && (this != rhs))
209 { // self-assignment check expected
210 absoluteCoordinates = rhs->absoluteCoordinates;
211 origin_x0 = rhs->origin_x0;
212 origin_y0 = rhs->origin_y0;
213 x0 = rhs->x0;
214 y0 = rhs->y0;
215 x1 = rhs->x1;
216 y1 = rhs->y1;
217 }
218 return *this;
219 }
220 bool operator==(const grk_rect<T>& rhs) const
221 {
222 if(this == &rhs)
223 return true;
224 return absoluteCoordinates == rhs.absoluteCoordinates && origin_x0 == rhs.origin_x0 &&
225 origin_y0 == rhs.origin_y0 && x0 == rhs.x0 && y0 == rhs.y0 && x1 == rhs.x1 &&
226 y1 == rhs.y1;
227 }
229 {
230 *this = *rhs;
231 }
233 {
234 setRect(&rhs);
235 }
247 {
248 return grk_rect<T>((T)(origin_x0 / denx), (T)(origin_y0 / deny), (T)(x0 / denx),
249 (T)(y0 / deny), (T)ceildiv<uint64_t>(x1, denx),
251 }
253 {
254 return grk_rect<T>((T)(origin_x0 >> powx), (T)(origin_y0 >> powy), (T)(x0 >> powx),
255 (T)(y0 >> powy), (T)ceildivpow2<uint64_t>(x1, powx),
257 }
259 {
260 return scaleDownPow2(pow.x, pow.y);
261 }
282 {
283 assert(absoluteCoordinates == rhs.absoluteCoordinates);
284
285 return intersection(&rhs);
286 }
287 bool isContainedIn(const grk_rect<T> rhs) const
288 {
289 return (intersection(&rhs) == *this);
290 }
292 {
293 assert(absoluteCoordinates == rhs->absoluteCoordinates);
294 return grk_rect<T>(std::max<T>(x0, rhs->x0), std::max<T>(y0, rhs->y0),
295 std::min<T>(x1, rhs->x1), std::min<T>(y1, rhs->y1));
296 }
298 {
299 return clip(&rhs);
300 }
302 {
303 assert(absoluteCoordinates == rhs.absoluteCoordinates);
304 *this = grk_rect<T>(std::max<T>(x0, rhs.x0), std::max<T>(y0, rhs.y0), std::min<T>(x1, rhs.x1),
305 std::min<T>(y1, rhs.y1));
306
307 return *this;
308 }
310 {
311 assert(absoluteCoordinates == rhs->absoluteCoordinates);
312 return grk_rect<T>(std::max<T>(x0, rhs->x0), std::max<T>(y0, rhs->y0),
313 std::min<T>(x1, rhs->x1), std::min<T>(y1, rhs->y1));
314 }
316 {
317 assert(absoluteCoordinates == rhs->absoluteCoordinates);
318 return std::max<T>(x0, rhs->x0) < std::min<T>(x1, rhs->x1) &&
319 std::max<T>(y0, rhs->y0) < std::min<T>(y1, rhs->y1);
320 }
322 {
323 assert(absoluteCoordinates == rhs->absoluteCoordinates);
324 return grk_rect<T>(std::min<T>(x0, rhs->x0), std::min<T>(y0, rhs->y0),
325 std::max<T>(x1, rhs->x1), std::max<T>(y1, rhs->y1));
326 }
328 {
329 return rectUnion(&rhs);
330 }
331 uint64_t area(void) const
332 {
333 return (uint64_t)(x1 - x0) * (y1 - y0);
334 }
335 T width() const
336 {
337 return x1 - x0;
338 }
339 T height() const
340 {
341 return y1 - y0;
342 }
344 {
345 return grk_line<T>(x0, x1);
346 }
348 {
349 return grk_line<T>(y0, y1);
350 }
351 // pan doesn't affect origin
353 {
354 auto rc = (*this);
355
356 return rc.pan_IN_PLACE(x, y);
357 }
359 {
360 x0 = satAdd<T>((int64_t)x0, (int64_t)x);
361 y0 = satAdd<T>((int64_t)y0, (int64_t)y);
362 x1 = satAdd<T>((int64_t)x1, (int64_t)x);
363 y1 = satAdd<T>((int64_t)y1, (int64_t)y);
364
365 return *this;
366 }
367 // grow doesn't affect origin
369 {
370 return grow_IN_PLACE(boundary, boundary, (std::numeric_limits<T>::max)(),
371 (std::numeric_limits<T>::max)());
372 }
374 {
375 return grow_IN_PLACE(boundaryx, boundaryy, (std::numeric_limits<T>::max)(),
376 (std::numeric_limits<T>::max)());
377 }
387 {
388 return grow_IN_PLACE(boundary, boundary, bounds);
389 }
391 {
392 x0 = std::max<T>(satSub<T>(x0, boundaryx), bounds.x0);
393 y0 = std::max<T>(satSub<T>(y0, boundaryy), bounds.y0);
394 x1 = std::min<T>(satAdd<T>(x1, boundaryx), bounds.x1);
395 y1 = std::min<T>(satAdd<T>(y1, boundaryy), bounds.y1);
396
397 return *this;
398 }
399 T parityX(void) const
400 {
401 return T(x0 & 1);
402 }
403 T parityY(void) const
404 {
405 return T(y0 & 1);
406 }
407};
408
409using grk_rect32 = grk_rect<uint32_t>;
410using grk_rect16 = grk_rect<uint16_t>;
411
412} // namespace grk
Copyright (C) 2016-2024 Grok Image Compression Inc.
Definition ICacheable.h:20
T satAdd(int64_t lhs, int64_t rhs)
Definition geometry.h:77
T clip(int64_t val)
Definition geometry.h:66
void grk_read(const uint8_t *buffer, TYPE *value, uint32_t numBytes)
Definition BufferedStream.h:239
uint32_t ceildiv(T a, T b)
Divide an integer by another integer and round upwards.
Definition grk_intmath.h:33
T satSub(T lhs, T rhs)
Definition geometry.h:89
grk_rect< uint32_t > grk_rect32
Definition geometry.h:61
grk_rect< uint16_t > grk_rect16
Definition geometry.h:62
T ceildivpow2(T a, uint32_t b)
Definition grk_intmath.h:40
void info(const char *fmt,...) override
Definition Logger.h:35
static Logger logger_
Definition Logger.h:70
Definition geometry.h:45
T x0
Definition geometry.h:48
grk_line(T _x0, T _x1)
Definition geometry.h:47
grk_line()
Definition geometry.h:46
T x1
Definition geometry.h:49
T length() const
Definition geometry.h:51
Definition geometry.h:34
T x
Definition geometry.h:37
T y
Definition geometry.h:38
grk_pt(T _x, T _y)
Definition geometry.h:36
grk_pt()
Definition geometry.h:35
Definition geometry.h:102
uint64_t area(void) const
Definition geometry.h:331
T width() const
Definition geometry.h:335
grk_rect< T > & pan_IN_PLACE(int64_t x, int64_t y)
Definition geometry.h:358
bool absoluteCoordinates
Definition geometry.h:122
grk_rect< T > pan(int64_t x, int64_t y) const
Definition geometry.h:352
void setRect(grk_rect< T > rhs)
Definition geometry.h:232
T y1
Definition geometry.h:124
grk_rect< T > & setOrigin(T origx, T origy, bool absolute)
Definition geometry.h:126
T x0
Definition geometry.h:124
grk_rect< T > intersection(const grk_rect< T > rhs) const
Definition geometry.h:281
bool operator==(const grk_rect< T > &rhs) const
Definition geometry.h:220
grk_rect< T > & grow_IN_PLACE(T boundaryx, T boundaryy)
Definition geometry.h:373
grk_rect< T > & toRelative(void)
Definition geometry.h:156
grk_rect< T > & grow_IN_PLACE(T boundaryx, T boundaryy, grk_rect< T > bounds)
Definition geometry.h:390
T x1
Definition geometry.h:124
grk_rect< T > clip(const grk_rect< T > *rhs) const
Definition geometry.h:291
grk_rect< T > scaleDownCeil(uint64_t denx, uint64_t deny) const
Definition geometry.h:262
T origin_y0
Definition geometry.h:123
T height() const
Definition geometry.h:339
bool valid(void) const
Definition geometry.h:185
T parityY(void) const
Definition geometry.h:403
grk_rect< T > & toAbsolute(void)
Definition geometry.h:166
grk_rect< T > & operator=(const grk_rect< T > &rhs)
Definition geometry.h:201
grk_rect< T > & grow_IN_PLACE(T boundary, grk_rect< T > bounds)
Definition geometry.h:386
grk_line< T > dimX() const
Definition geometry.h:343
grk_rect< T > & clip_IN_PLACE(const grk_rect< T > &rhs)
Definition geometry.h:301
bool nonEmptyIntersection(const grk_rect< T > *rhs) const
Definition geometry.h:315
T parityX(void) const
Definition geometry.h:399
virtual ~grk_rect()=default
grk_rect< T > & setOrigin(grk_rect< T > *rhs, bool absolute)
Definition geometry.h:142
grk_rect< T > rectUnion(const grk_rect< T > &rhs) const
Definition geometry.h:327
grk_rect< T > scale(uint32_t scalex, uint32_t scaley) const
Definition geometry.h:241
grk_rect< T > scaleDownCeilPow2(uint32_t power) const
Definition geometry.h:268
grk_rect< T > & grow_IN_PLACE(T boundary, T maxX, T maxY)
Definition geometry.h:378
bool contains(grk_pt< T > pt)
Definition geometry.h:193
grk_rect< T > & grow_IN_PLACE(T boundary)
Definition geometry.h:368
bool empty(void) const
Definition geometry.h:189
grk_rect< T > & operator=(const grk_rect< T > *rhs)
Definition geometry.h:205
grk_rect(T x0, T y0, T x1, T y1)
Definition geometry.h:107
grk_rect(const grk_rect *rhs)
Definition geometry.h:109
grk_rect< T > scaleDownPow2(grk_pt< T > pow) const
Definition geometry.h:258
T origin_x0
Definition geometry.h:123
virtual void print(void) const
Definition geometry.h:174
grk_line< T > dimY() const
Definition geometry.h:347
grk_rect< T > clip(const grk_rect< T > &rhs) const
Definition geometry.h:297
grk_rect< T > scaleDown(uint64_t denx, uint64_t deny) const
Definition geometry.h:246
void setRect(grk_rect< T > *rhs)
Definition geometry.h:228
grk_rect(void)
Definition geometry.h:119
grk_rect< T > rectUnion(const grk_rect< T > *rhs) const
Definition geometry.h:321
grk_rect< T > scaleDownCeil(uint32_t den) const
Definition geometry.h:236
bool contains(T x, T y)
Definition geometry.h:197
bool isContainedIn(const grk_rect< T > rhs) const
Definition geometry.h:287
grk_rect(const grk_rect &rhs)
Definition geometry.h:108
grk_rect< T > scaleDownCeilPow2(uint32_t powx, uint32_t powy) const
Definition geometry.h:274
grk_rect(T origin_x0, T origin_y0, T x0, T y0, T x1, T y1)
Definition geometry.h:103
std::string boundsString() const
Definition geometry.h:178
grk_rect< T > & grow_IN_PLACE(T boundaryx, T boundaryy, T maxX, T maxY)
Definition geometry.h:382
grk_rect< T > & setOrigin(grk_rect< T > &rhs, bool absolute)
Definition geometry.h:138
T y0
Definition geometry.h:124
grk_rect< T > intersection(const grk_rect< T > *rhs) const
Definition geometry.h:309
grk_rect< T > scaleDownPow2(uint32_t powx, uint32_t powy) const
Definition geometry.h:252