1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
22 /** \file
23  * \ingroup bli
24  */
25 
26 #include "BLI_compiler_compat.h"
27 #include "BLI_sys_types.h" /* bool */
28 #include "DNA_vec_types.h"
29 
30 struct rctf;
31 struct rcti;
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 bool BLI_rcti_is_empty(const struct rcti *rect);
38 bool BLI_rctf_is_empty(const struct rctf *rect);
39 void BLI_rctf_init(struct rctf *rect, float xmin, float xmax, float ymin, float ymax);
40 void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax);
41 bool BLI_rctf_is_valid(const struct rctf *rect);
42 bool BLI_rcti_is_valid(const struct rcti *rect);
43 void BLI_rctf_sanitize(struct rctf *rect);
44 void BLI_rcti_sanitize(struct rcti *rect);
45 void BLI_rctf_init_pt_radius(struct rctf *rect, const float xy[2], float size);
46 void BLI_rcti_init_pt_radius(struct rcti *rect, const int xy[2], int size);
47 void BLI_rcti_init_minmax(struct rcti *rect);
48 void BLI_rctf_init_minmax(struct rctf *rect);
49 void BLI_rcti_do_minmax_v(struct rcti *rect, const int xy[2]);
50 void BLI_rctf_do_minmax_v(struct rctf *rect, const float xy[2]);
51 
52 void BLI_rctf_transform_pt_v(const rctf *dst,
53                              const rctf *src,
54                              float xy_dst[2],
55                              const float xy_src[2]);
56 void BLI_rctf_transform_calc_m4_pivot_min_ex(
57     const rctf *dst, const rctf *src, float matrix[4][4], uint x, uint y);
58 void BLI_rctf_transform_calc_m4_pivot_min(const rctf *dst, const rctf *src, float matrix[4][4]);
59 
60 void BLI_rctf_translate(struct rctf *rect, float x, float y);
61 void BLI_rcti_translate(struct rcti *rect, int x, int y);
62 void BLI_rcti_recenter(struct rcti *rect, int x, int y);
63 void BLI_rctf_recenter(struct rctf *rect, float x, float y);
64 void BLI_rcti_resize(struct rcti *rect, int x, int y);
65 void BLI_rcti_resize_x(struct rcti *rect, int x);
66 void BLI_rcti_resize_y(struct rcti *rect, int y);
67 void BLI_rcti_pad(struct rcti *rect, int pad_x, int pad_y);
68 void BLI_rctf_pad(struct rctf *rect, float pad_x, float pad_y);
69 void BLI_rctf_resize(struct rctf *rect, float x, float y);
70 void BLI_rctf_resize_x(struct rctf *rect, float x);
71 void BLI_rctf_resize_y(struct rctf *rect, float y);
72 void BLI_rcti_scale(rcti *rect, const float scale);
73 void BLI_rctf_scale(rctf *rect, const float scale);
74 void BLI_rctf_pad_y(struct rctf *rect,
75                     const float boundary_size,
76                     const float pad_min,
77                     const float pad_max);
78 void BLI_rctf_interp(struct rctf *rect,
79                      const struct rctf *rect_a,
80                      const struct rctf *rect_b,
81                      const float fac);
82 // void BLI_rcti_interp(struct rctf *rect, struct rctf *rect_a, struct rctf *rect_b, float fac);
83 bool BLI_rctf_clamp_pt_v(const struct rctf *rect, float xy[2]);
84 bool BLI_rcti_clamp_pt_v(const struct rcti *rect, int xy[2]);
85 bool BLI_rctf_clamp(struct rctf *rect, const struct rctf *rect_bounds, float r_xy[2]);
86 bool BLI_rcti_clamp(struct rcti *rect, const struct rcti *rect_bounds, int r_xy[2]);
87 bool BLI_rctf_compare(const struct rctf *rect_a, const struct rctf *rect_b, const float limit);
88 bool BLI_rcti_compare(const struct rcti *rect_a, const struct rcti *rect_b);
89 bool BLI_rctf_isect(const struct rctf *src1, const struct rctf *src2, struct rctf *dest);
90 bool BLI_rcti_isect(const struct rcti *src1, const struct rcti *src2, struct rcti *dest);
91 bool BLI_rctf_isect_rect_x(const struct rctf *src1, const struct rctf *src2, float range_x[2]);
92 bool BLI_rctf_isect_rect_y(const struct rctf *src1, const struct rctf *src2, float range_y[2]);
93 bool BLI_rcti_isect_rect_x(const struct rcti *src1, const struct rcti *src2, int range_x[2]);
94 bool BLI_rcti_isect_rect_y(const struct rcti *src1, const struct rcti *src2, int range_y[2]);
95 bool BLI_rcti_isect_x(const rcti *rect, const int x);
96 bool BLI_rcti_isect_y(const rcti *rect, const int y);
97 bool BLI_rcti_isect_pt(const struct rcti *rect, const int x, const int y);
98 bool BLI_rcti_isect_pt_v(const struct rcti *rect, const int xy[2]);
99 bool BLI_rctf_isect_x(const rctf *rect, const float x);
100 bool BLI_rctf_isect_y(const rctf *rect, const float y);
101 bool BLI_rctf_isect_pt(const struct rctf *rect, const float x, const float y);
102 bool BLI_rctf_isect_pt_v(const struct rctf *rect, const float xy[2]);
103 int BLI_rcti_length_x(const rcti *rect, const int x);
104 int BLI_rcti_length_y(const rcti *rect, const int y);
105 float BLI_rctf_length_x(const rctf *rect, const float x);
106 float BLI_rctf_length_y(const rctf *rect, const float y);
107 bool BLI_rcti_isect_segment(const struct rcti *rect, const int s1[2], const int s2[2]);
108 bool BLI_rctf_isect_segment(const struct rctf *rect, const float s1[2], const float s2[2]);
109 bool BLI_rcti_isect_circle(const struct rcti *rect, const float xy[2], const float radius);
110 bool BLI_rctf_isect_circle(const struct rctf *rect, const float xy[2], const float radius);
111 bool BLI_rcti_inside_rcti(const rcti *rct_a, const rcti *rct_b);
112 bool BLI_rctf_inside_rctf(const rctf *rct_a, const rctf *rct_b);
113 void BLI_rcti_union(struct rcti *rct1, const struct rcti *rct2);
114 void BLI_rctf_union(struct rctf *rct1, const struct rctf *rct2);
115 void BLI_rcti_rctf_copy(struct rcti *dst, const struct rctf *src);
116 void BLI_rctf_rcti_copy(struct rctf *dst, const struct rcti *src);
117 void BLI_rcti_rctf_copy_floor(struct rcti *dst, const struct rctf *src);
118 void BLI_rcti_rctf_copy_round(struct rcti *dst, const struct rctf *src);
119 
120 void BLI_rctf_rotate_expand(rctf *dst, const rctf *src, const float angle);
121 
122 void print_rctf(const char *str, const struct rctf *rect);
123 void print_rcti(const char *str, const struct rcti *rect);
124 
125 #define print_rctf_id(rect) print_rctf(STRINGIFY(rect), rect)
126 #define print_rcti_id(rect) print_rcti(STRINGIFY(rect), rect)
127 
BLI_rcti_cent_x_fl(const struct rcti * rct)128 BLI_INLINE float BLI_rcti_cent_x_fl(const struct rcti *rct)
129 {
130   return (float)(rct->xmin + rct->xmax) / 2.0f;
131 }
BLI_rcti_cent_y_fl(const struct rcti * rct)132 BLI_INLINE float BLI_rcti_cent_y_fl(const struct rcti *rct)
133 {
134   return (float)(rct->ymin + rct->ymax) / 2.0f;
135 }
BLI_rcti_cent_x(const struct rcti * rct)136 BLI_INLINE int BLI_rcti_cent_x(const struct rcti *rct)
137 {
138   return (rct->xmin + rct->xmax) / 2;
139 }
BLI_rcti_cent_y(const struct rcti * rct)140 BLI_INLINE int BLI_rcti_cent_y(const struct rcti *rct)
141 {
142   return (rct->ymin + rct->ymax) / 2;
143 }
BLI_rctf_cent_x(const struct rctf * rct)144 BLI_INLINE float BLI_rctf_cent_x(const struct rctf *rct)
145 {
146   return (rct->xmin + rct->xmax) / 2.0f;
147 }
BLI_rctf_cent_y(const struct rctf * rct)148 BLI_INLINE float BLI_rctf_cent_y(const struct rctf *rct)
149 {
150   return (rct->ymin + rct->ymax) / 2.0f;
151 }
152 
BLI_rcti_size_x(const struct rcti * rct)153 BLI_INLINE int BLI_rcti_size_x(const struct rcti *rct)
154 {
155   return (rct->xmax - rct->xmin);
156 }
BLI_rcti_size_y(const struct rcti * rct)157 BLI_INLINE int BLI_rcti_size_y(const struct rcti *rct)
158 {
159   return (rct->ymax - rct->ymin);
160 }
BLI_rctf_size_x(const struct rctf * rct)161 BLI_INLINE float BLI_rctf_size_x(const struct rctf *rct)
162 {
163   return (rct->xmax - rct->xmin);
164 }
BLI_rctf_size_y(const struct rctf * rct)165 BLI_INLINE float BLI_rctf_size_y(const struct rctf *rct)
166 {
167   return (rct->ymax - rct->ymin);
168 }
169 
170 #ifdef __cplusplus
171 }
172 #endif
173