|
| 1 | +/* graphene-box2d.h: An 2D, axis aligned bounding box |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: MIT |
| 4 | + * SPDX-FileCopyrightText: 2023 Emmanuele Bassi |
| 5 | + */ |
| 6 | + |
| 7 | +#pragma once |
| 8 | + |
| 9 | +#if !defined(GRAPHENE_H_INSIDE) && !defined(GRAPHENE_COMPILATION) |
| 10 | +#error "Only graphene.h can be included directly." |
| 11 | +#endif |
| 12 | + |
| 13 | +#include "graphene-types.h" |
| 14 | +#include "graphene-point.h" |
| 15 | +#include "graphene-simd4f.h" |
| 16 | +#include "graphene-vec2.h" |
| 17 | +#include "graphene-vec4.h" |
| 18 | + |
| 19 | +GRAPHENE_BEGIN_DECLS |
| 20 | + |
| 21 | +/** |
| 22 | + * graphene_box2d_t: |
| 23 | + * |
| 24 | + * A 2D box, described as the axis-aligned area between a minimum and |
| 25 | + * a maximum vertices lying on the same plane. |
| 26 | + * |
| 27 | + * Since: 1.12 |
| 28 | + */ |
| 29 | +struct _graphene_box2d_t |
| 30 | +{ |
| 31 | + /*< private >*/ |
| 32 | + GRAPHENE_PRIVATE_FIELD (graphene_vec4_t, minmax); |
| 33 | +}; |
| 34 | + |
| 35 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 36 | +graphene_box2d_t * graphene_box2d_alloc (void); |
| 37 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 38 | +void graphene_box2d_free (graphene_box2d_t *box); |
| 39 | + |
| 40 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 41 | +graphene_box2d_t * graphene_box2d_init (graphene_box2d_t *box, |
| 42 | + const graphene_point_t *min, |
| 43 | + const graphene_point_t *max); |
| 44 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 45 | +graphene_box2d_t * graphene_box2d_init_from_points (graphene_box2d_t *box, |
| 46 | + unsigned int n_points, |
| 47 | + const graphene_point_t *points); |
| 48 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 49 | +graphene_box2d_t * graphene_box2d_init_from_vectors (graphene_box2d_t *box, |
| 50 | + unsigned int n_vectors, |
| 51 | + const graphene_vec2_t *vectors); |
| 52 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 53 | +graphene_box2d_t * graphene_box2d_init_from_box (graphene_box2d_t *box, |
| 54 | + const graphene_box2d_t *src); |
| 55 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 56 | +graphene_box2d_t * graphene_box2d_init_from_vec2 (graphene_box2d_t *box, |
| 57 | + const graphene_vec2_t *min, |
| 58 | + const graphene_vec2_t *max); |
| 59 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 60 | +graphene_box2d_t * graphene_box2d_init_from_rect (graphene_box2d_t *box, |
| 61 | + const graphene_rect_t *src); |
| 62 | + |
| 63 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 64 | +void graphene_box2d_expand (const graphene_box2d_t *box, |
| 65 | + const graphene_point_t *point, |
| 66 | + graphene_box2d_t *res); |
| 67 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 68 | +void graphene_box2d_expand_vec2 (const graphene_box2d_t *box, |
| 69 | + const graphene_vec2_t *vec, |
| 70 | + graphene_box2d_t *res); |
| 71 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 72 | +void graphene_box2d_expand_scalar (const graphene_box2d_t *box, |
| 73 | + float scalar, |
| 74 | + graphene_box2d_t *res); |
| 75 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 76 | +void graphene_box2d_scale_offset (const graphene_box2d_t *box, |
| 77 | + const graphene_vec2_t *scale, |
| 78 | + const graphene_point_t *offset, |
| 79 | + graphene_box2d_t *res); |
| 80 | + |
| 81 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 82 | +void graphene_box2d_union (const graphene_box2d_t *a, |
| 83 | + const graphene_box2d_t *b, |
| 84 | + graphene_box2d_t *res); |
| 85 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 86 | +bool graphene_box2d_intersection (const graphene_box2d_t *a, |
| 87 | + const graphene_box2d_t *b, |
| 88 | + graphene_box2d_t *res); |
| 89 | + |
| 90 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 91 | +float graphene_box2d_get_width (const graphene_box2d_t *box); |
| 92 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 93 | +float graphene_box2d_get_height (const graphene_box2d_t *box); |
| 94 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 95 | +void graphene_box2d_get_size (const graphene_box2d_t *box, |
| 96 | + graphene_vec2_t *size); |
| 97 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 98 | +void graphene_box2d_get_center (const graphene_box2d_t *box, |
| 99 | + graphene_point_t *center); |
| 100 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 101 | +void graphene_box2d_get_minmax (const graphene_box2d_t *box, |
| 102 | + graphene_point_t *min, |
| 103 | + graphene_point_t *max); |
| 104 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 105 | +void graphene_box2d_get_min (const graphene_box2d_t *box, |
| 106 | + graphene_point_t *min); |
| 107 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 108 | +void graphene_box2d_get_max (const graphene_box2d_t *box, |
| 109 | + graphene_point_t *max); |
| 110 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 111 | +void graphene_box2d_get_vertices (const graphene_box2d_t *box, |
| 112 | + graphene_vec2_t vertices[]); |
| 113 | + |
| 114 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 115 | +void graphene_box2d_to_float (const graphene_box2d_t *box, |
| 116 | + float v[4]); |
| 117 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 118 | +void graphene_box2d_to_rect (const graphene_box2d_t *box, |
| 119 | + graphene_rect_t *rect); |
| 120 | + |
| 121 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 122 | +bool graphene_box2d_contains_point (const graphene_box2d_t *box, |
| 123 | + const graphene_point_t *point); |
| 124 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 125 | +bool graphene_box2d_contains_box (const graphene_box2d_t *a, |
| 126 | + const graphene_box2d_t *b); |
| 127 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 128 | +bool graphene_box2d_contains_rect (const graphene_box2d_t *box, |
| 129 | + const graphene_rect_t *rect); |
| 130 | + |
| 131 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 132 | +bool graphene_box2d_equal (const graphene_box2d_t *a, |
| 133 | + const graphene_box2d_t *b); |
| 134 | + |
| 135 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 136 | +const graphene_box2d_t * graphene_box2d_zero (void); |
| 137 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 138 | +const graphene_box2d_t * graphene_box2d_one (void); |
| 139 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 140 | +const graphene_box2d_t * graphene_box2d_minus_one (void); |
| 141 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 142 | +const graphene_box2d_t * graphene_box2d_one_minus_one (void); |
| 143 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 144 | +const graphene_box2d_t * graphene_box2d_infinite (void); |
| 145 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 146 | +const graphene_box2d_t * graphene_box2d_empty (void); |
| 147 | + |
| 148 | +GRAPHENE_AVAILABLE_IN_1_12 |
| 149 | +bool graphene_box2d_intersects (const graphene_box2d_t *a, |
| 150 | + const graphene_box2d_t *b); |
| 151 | + |
| 152 | +#ifndef __GTK_DOC_IGNORE__ |
| 153 | + |
| 154 | +#define graphene_box2d_intersects(a,b) \ |
| 155 | + graphene_box2d_intersects_inline ((a), (b)) |
| 156 | + |
| 157 | +static inline bool |
| 158 | +graphene_box2d_intersects_inline (const graphene_box2d_t *a, |
| 159 | + const graphene_box2d_t *b) |
| 160 | +{ |
| 161 | + graphene_point_t min_a, max_a; |
| 162 | + graphene_box2d_get_minmax (a, &min_a, &max_a); |
| 163 | + |
| 164 | + graphene_point_t min_b, max_b; |
| 165 | + graphene_box2d_get_minmax (b, &min_b, &max_b); |
| 166 | + |
| 167 | + graphene_simd4f_t min_v = |
| 168 | + graphene_simd4f_max (graphene_simd4f_init (min_a.x, min_a.y, 0.f, 0.f), |
| 169 | + graphene_simd4f_init (min_b.x, min_b.y, 0.f, 0.f)); |
| 170 | + graphene_simd4f_t max_v = |
| 171 | + graphene_simd4f_min (graphene_simd4f_init (max_a.x, max_a.y, 0.f, 0.f), |
| 172 | + graphene_simd4f_init (max_b.x, max_b.y, 0.f, 0.f)); |
| 173 | + |
| 174 | + if (!graphene_simd4f_cmp_le (min_v, max_v)) |
| 175 | + return false; |
| 176 | + |
| 177 | + return true; |
| 178 | +} |
| 179 | +#endif /* __GTK_DOC_IGNORE__ */ |
| 180 | + |
| 181 | +GRAPHENE_END_DECLS |
0 commit comments