Skip to content

Commit 21e4bb3

Browse files
authored
Merge pull request #259 from ebassi/box2d
Add 2D axis-aligned bounding box type
2 parents db2b756 + c25e7f1 commit 21e4bb3

13 files changed

+1949
-0
lines changed

doc/graphene-docs.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<xi:include href="xml/graphene-rect.xml"/>
3636
<xi:include href="xml/graphene-quad.xml"/>
3737
<xi:include href="xml/graphene-triangle.xml"/>
38+
<xi:include href="xml/graphene-box2d.xml"/>
3839
<xi:include href="xml/graphene-box.xml"/>
3940
<xi:include href="xml/graphene-sphere.xml"/>
4041
<xi:include href="xml/graphene-frustum.xml"/>
@@ -78,6 +79,10 @@
7879
<title>Index of new symbols in 1.10</title>
7980
<xi:include href="xml/api-index-1.10.xml"><xi:fallback /></xi:include>
8081
</index>
82+
<index role="1.12">
83+
<title>Index of new symbols in 1.12</title>
84+
<xi:include href="xml/api-index-1.12.xml"><xi:fallback /></xi:include>
85+
</index>
8186

8287
<xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
8388
</book>

doc/graphene-sections.txt

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,47 @@ graphene_box_empty
3535
graphene_box_infinite
3636
</SECTION>
3737

38+
<SECTION>
39+
<FILE>graphene-box2d</FILE>
40+
graphene_box2d_t
41+
graphene_box2d_alloc
42+
graphene_box2d_free
43+
graphene_box2d_init
44+
graphene_box2d_init_from_box
45+
graphene_box2d_init_from_points
46+
graphene_box2d_init_from_rect
47+
graphene_box2d_init_from_vec2
48+
graphene_box2d_init_from_vectors
49+
graphene_box2d_equal
50+
graphene_box2d_to_rect
51+
graphene_box2d_to_float
52+
graphene_box2d_expand
53+
graphene_box2d_expand_scalar
54+
graphene_box2d_expand_vec2
55+
graphene_box2d_get_minmax
56+
graphene_box2d_get_min
57+
graphene_box2d_get_max
58+
graphene_box2d_get_center
59+
graphene_box2d_get_height
60+
graphene_box2d_get_width
61+
graphene_box2d_get_size
62+
graphene_box2d_get_vertices
63+
graphene_box2d_union
64+
graphene_box2d_intersection
65+
graphene_box2d_intersects
66+
graphene_box2d_contains_box
67+
graphene_box2d_contains_point
68+
graphene_box2d_contains_rect
69+
graphene_box2d_scale_offset
70+
<SUBSECTION>
71+
graphene_box2d_zero
72+
graphene_box2d_one
73+
graphene_box2d_minus_one
74+
graphene_box2d_one_minus_one
75+
graphene_box2d_empty
76+
graphene_box2d_infinite
77+
</SECTION>
78+
3879
<SECTION>
3980
<FILE>graphene-euler</FILE>
4081
graphene_euler_t
@@ -81,6 +122,7 @@ graphene_frustum_equal
81122
<FILE>graphene-gobject</FILE>
82123
<SUBSECTION Standard>
83124
GRAPHENE_TYPE_BOX
125+
GRAPHENE_TYPE_BOX2D
84126
GRAPHENE_TYPE_EULER
85127
GRAPHENE_TYPE_FRUSTUM
86128
GRAPHENE_TYPE_MATRIX
@@ -98,6 +140,7 @@ GRAPHENE_TYPE_VEC2
98140
GRAPHENE_TYPE_VEC3
99141
GRAPHENE_TYPE_VEC4
100142
graphene_box_get_type
143+
graphene_box2d_get_type
101144
graphene_euler_get_type
102145
graphene_frustum_get_type
103146
graphene_matrix_get_type
@@ -446,6 +489,17 @@ graphene_simd4f_floor
446489
graphene_simd4f_union_t
447490
graphene_simd4i_union_t
448491
graphene_simd2f_t
492+
sw
493+
sx
494+
sy
495+
sz
496+
vandq_s32
497+
vcombine_f32
498+
vget_lane_f32
499+
vgetq_lane_u32
500+
vld1q_f32
501+
vmulq_f32
502+
vreinterpretq_f32_u32
449503
</SECTION>
450504

451505
<SECTION>

doc/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,5 @@ gnome.gtkdoc('graphene',
4747
],
4848
html_assets: html_images,
4949
install: true,
50+
check: true,
5051
)

include/graphene-box2d.h

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
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

include/graphene-gobject.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,11 @@ GType graphene_ray_get_type (void);
149149

150150
G_DEFINE_AUTOPTR_CLEANUP_FUNC(graphene_ray_t, graphene_ray_free)
151151

152+
#define GRAPHENE_TYPE_BOX2D (graphene_box2d_get_type ())
153+
154+
GRAPHENE_AVAILABLE_IN_1_12
155+
GType graphene_box2d_get_type (void);
156+
157+
G_DEFINE_AUTOPTR_CLEANUP_FUNC(graphene_box2d_t, graphene_box2d_free)
158+
152159
G_END_DECLS

include/graphene-types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ typedef struct _graphene_matrix_t graphene_matrix_t;
113113
typedef struct _graphene_point_t graphene_point_t;
114114
typedef struct _graphene_size_t graphene_size_t;
115115
typedef struct _graphene_rect_t graphene_rect_t;
116+
typedef struct _graphene_box2d_t graphene_box2d_t;
116117

117118
typedef struct _graphene_point3d_t graphene_point3d_t;
118119
typedef struct _graphene_quad_t graphene_quad_t;

include/graphene.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "graphene-point.h"
4747
#include "graphene-size.h"
4848
#include "graphene-rect.h"
49+
#include "graphene-box2d.h"
4950

5051
#include "graphene-point3d.h"
5152
#include "graphene-quad.h"

include/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
graphene_public_headers = files([
22
'graphene-box.h',
3+
'graphene-box2d.h',
34
'graphene-euler.h',
45
'graphene-frustum.h',
56
'graphene-macros.h',

0 commit comments

Comments
 (0)