Skip to content

Commit

Permalink
Merge pull request #259 from ebassi/box2d
Browse files Browse the repository at this point in the history
Add 2D axis-aligned bounding box type
  • Loading branch information
ebassi authored Aug 15, 2024
2 parents db2b756 + c25e7f1 commit 21e4bb3
Show file tree
Hide file tree
Showing 13 changed files with 1,949 additions and 0 deletions.
5 changes: 5 additions & 0 deletions doc/graphene-docs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<xi:include href="xml/graphene-rect.xml"/>
<xi:include href="xml/graphene-quad.xml"/>
<xi:include href="xml/graphene-triangle.xml"/>
<xi:include href="xml/graphene-box2d.xml"/>
<xi:include href="xml/graphene-box.xml"/>
<xi:include href="xml/graphene-sphere.xml"/>
<xi:include href="xml/graphene-frustum.xml"/>
Expand Down Expand Up @@ -78,6 +79,10 @@
<title>Index of new symbols in 1.10</title>
<xi:include href="xml/api-index-1.10.xml"><xi:fallback /></xi:include>
</index>
<index role="1.12">
<title>Index of new symbols in 1.12</title>
<xi:include href="xml/api-index-1.12.xml"><xi:fallback /></xi:include>
</index>

<xi:include href="xml/annotation-glossary.xml"><xi:fallback /></xi:include>
</book>
54 changes: 54 additions & 0 deletions doc/graphene-sections.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,47 @@ graphene_box_empty
graphene_box_infinite
</SECTION>

<SECTION>
<FILE>graphene-box2d</FILE>
graphene_box2d_t
graphene_box2d_alloc
graphene_box2d_free
graphene_box2d_init
graphene_box2d_init_from_box
graphene_box2d_init_from_points
graphene_box2d_init_from_rect
graphene_box2d_init_from_vec2
graphene_box2d_init_from_vectors
graphene_box2d_equal
graphene_box2d_to_rect
graphene_box2d_to_float
graphene_box2d_expand
graphene_box2d_expand_scalar
graphene_box2d_expand_vec2
graphene_box2d_get_minmax
graphene_box2d_get_min
graphene_box2d_get_max
graphene_box2d_get_center
graphene_box2d_get_height
graphene_box2d_get_width
graphene_box2d_get_size
graphene_box2d_get_vertices
graphene_box2d_union
graphene_box2d_intersection
graphene_box2d_intersects
graphene_box2d_contains_box
graphene_box2d_contains_point
graphene_box2d_contains_rect
graphene_box2d_scale_offset
<SUBSECTION>
graphene_box2d_zero
graphene_box2d_one
graphene_box2d_minus_one
graphene_box2d_one_minus_one
graphene_box2d_empty
graphene_box2d_infinite
</SECTION>

<SECTION>
<FILE>graphene-euler</FILE>
graphene_euler_t
Expand Down Expand Up @@ -81,6 +122,7 @@ graphene_frustum_equal
<FILE>graphene-gobject</FILE>
<SUBSECTION Standard>
GRAPHENE_TYPE_BOX
GRAPHENE_TYPE_BOX2D
GRAPHENE_TYPE_EULER
GRAPHENE_TYPE_FRUSTUM
GRAPHENE_TYPE_MATRIX
Expand All @@ -98,6 +140,7 @@ GRAPHENE_TYPE_VEC2
GRAPHENE_TYPE_VEC3
GRAPHENE_TYPE_VEC4
graphene_box_get_type
graphene_box2d_get_type
graphene_euler_get_type
graphene_frustum_get_type
graphene_matrix_get_type
Expand Down Expand Up @@ -446,6 +489,17 @@ graphene_simd4f_floor
graphene_simd4f_union_t
graphene_simd4i_union_t
graphene_simd2f_t
sw
sx
sy
sz
vandq_s32
vcombine_f32
vget_lane_f32
vgetq_lane_u32
vld1q_f32
vmulq_f32
vreinterpretq_f32_u32
</SECTION>

<SECTION>
Expand Down
1 change: 1 addition & 0 deletions doc/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,5 @@ gnome.gtkdoc('graphene',
],
html_assets: html_images,
install: true,
check: true,
)
181 changes: 181 additions & 0 deletions include/graphene-box2d.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
/* graphene-box2d.h: An 2D, axis aligned bounding box
*
* SPDX-License-Identifier: MIT
* SPDX-FileCopyrightText: 2023 Emmanuele Bassi
*/

#pragma once

#if !defined(GRAPHENE_H_INSIDE) && !defined(GRAPHENE_COMPILATION)
#error "Only graphene.h can be included directly."
#endif

#include "graphene-types.h"
#include "graphene-point.h"
#include "graphene-simd4f.h"
#include "graphene-vec2.h"
#include "graphene-vec4.h"

GRAPHENE_BEGIN_DECLS

/**
* graphene_box2d_t:
*
* A 2D box, described as the axis-aligned area between a minimum and
* a maximum vertices lying on the same plane.
*
* Since: 1.12
*/
struct _graphene_box2d_t
{
/*< private >*/
GRAPHENE_PRIVATE_FIELD (graphene_vec4_t, minmax);
};

GRAPHENE_AVAILABLE_IN_1_12
graphene_box2d_t * graphene_box2d_alloc (void);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_free (graphene_box2d_t *box);

GRAPHENE_AVAILABLE_IN_1_12
graphene_box2d_t * graphene_box2d_init (graphene_box2d_t *box,
const graphene_point_t *min,
const graphene_point_t *max);
GRAPHENE_AVAILABLE_IN_1_12
graphene_box2d_t * graphene_box2d_init_from_points (graphene_box2d_t *box,
unsigned int n_points,
const graphene_point_t *points);
GRAPHENE_AVAILABLE_IN_1_12
graphene_box2d_t * graphene_box2d_init_from_vectors (graphene_box2d_t *box,
unsigned int n_vectors,
const graphene_vec2_t *vectors);
GRAPHENE_AVAILABLE_IN_1_12
graphene_box2d_t * graphene_box2d_init_from_box (graphene_box2d_t *box,
const graphene_box2d_t *src);
GRAPHENE_AVAILABLE_IN_1_12
graphene_box2d_t * graphene_box2d_init_from_vec2 (graphene_box2d_t *box,
const graphene_vec2_t *min,
const graphene_vec2_t *max);
GRAPHENE_AVAILABLE_IN_1_12
graphene_box2d_t * graphene_box2d_init_from_rect (graphene_box2d_t *box,
const graphene_rect_t *src);

GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_expand (const graphene_box2d_t *box,
const graphene_point_t *point,
graphene_box2d_t *res);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_expand_vec2 (const graphene_box2d_t *box,
const graphene_vec2_t *vec,
graphene_box2d_t *res);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_expand_scalar (const graphene_box2d_t *box,
float scalar,
graphene_box2d_t *res);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_scale_offset (const graphene_box2d_t *box,
const graphene_vec2_t *scale,
const graphene_point_t *offset,
graphene_box2d_t *res);

GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_union (const graphene_box2d_t *a,
const graphene_box2d_t *b,
graphene_box2d_t *res);
GRAPHENE_AVAILABLE_IN_1_12
bool graphene_box2d_intersection (const graphene_box2d_t *a,
const graphene_box2d_t *b,
graphene_box2d_t *res);

GRAPHENE_AVAILABLE_IN_1_12
float graphene_box2d_get_width (const graphene_box2d_t *box);
GRAPHENE_AVAILABLE_IN_1_12
float graphene_box2d_get_height (const graphene_box2d_t *box);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_get_size (const graphene_box2d_t *box,
graphene_vec2_t *size);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_get_center (const graphene_box2d_t *box,
graphene_point_t *center);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_get_minmax (const graphene_box2d_t *box,
graphene_point_t *min,
graphene_point_t *max);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_get_min (const graphene_box2d_t *box,
graphene_point_t *min);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_get_max (const graphene_box2d_t *box,
graphene_point_t *max);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_get_vertices (const graphene_box2d_t *box,
graphene_vec2_t vertices[]);

GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_to_float (const graphene_box2d_t *box,
float v[4]);
GRAPHENE_AVAILABLE_IN_1_12
void graphene_box2d_to_rect (const graphene_box2d_t *box,
graphene_rect_t *rect);

GRAPHENE_AVAILABLE_IN_1_12
bool graphene_box2d_contains_point (const graphene_box2d_t *box,
const graphene_point_t *point);
GRAPHENE_AVAILABLE_IN_1_12
bool graphene_box2d_contains_box (const graphene_box2d_t *a,
const graphene_box2d_t *b);
GRAPHENE_AVAILABLE_IN_1_12
bool graphene_box2d_contains_rect (const graphene_box2d_t *box,
const graphene_rect_t *rect);

GRAPHENE_AVAILABLE_IN_1_12
bool graphene_box2d_equal (const graphene_box2d_t *a,
const graphene_box2d_t *b);

GRAPHENE_AVAILABLE_IN_1_12
const graphene_box2d_t * graphene_box2d_zero (void);
GRAPHENE_AVAILABLE_IN_1_12
const graphene_box2d_t * graphene_box2d_one (void);
GRAPHENE_AVAILABLE_IN_1_12
const graphene_box2d_t * graphene_box2d_minus_one (void);
GRAPHENE_AVAILABLE_IN_1_12
const graphene_box2d_t * graphene_box2d_one_minus_one (void);
GRAPHENE_AVAILABLE_IN_1_12
const graphene_box2d_t * graphene_box2d_infinite (void);
GRAPHENE_AVAILABLE_IN_1_12
const graphene_box2d_t * graphene_box2d_empty (void);

GRAPHENE_AVAILABLE_IN_1_12
bool graphene_box2d_intersects (const graphene_box2d_t *a,
const graphene_box2d_t *b);

#ifndef __GTK_DOC_IGNORE__

#define graphene_box2d_intersects(a,b) \
graphene_box2d_intersects_inline ((a), (b))

static inline bool
graphene_box2d_intersects_inline (const graphene_box2d_t *a,
const graphene_box2d_t *b)
{
graphene_point_t min_a, max_a;
graphene_box2d_get_minmax (a, &min_a, &max_a);

graphene_point_t min_b, max_b;
graphene_box2d_get_minmax (b, &min_b, &max_b);

graphene_simd4f_t min_v =
graphene_simd4f_max (graphene_simd4f_init (min_a.x, min_a.y, 0.f, 0.f),
graphene_simd4f_init (min_b.x, min_b.y, 0.f, 0.f));
graphene_simd4f_t max_v =
graphene_simd4f_min (graphene_simd4f_init (max_a.x, max_a.y, 0.f, 0.f),
graphene_simd4f_init (max_b.x, max_b.y, 0.f, 0.f));

if (!graphene_simd4f_cmp_le (min_v, max_v))
return false;

return true;
}
#endif /* __GTK_DOC_IGNORE__ */

GRAPHENE_END_DECLS
7 changes: 7 additions & 0 deletions include/graphene-gobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,11 @@ GType graphene_ray_get_type (void);

G_DEFINE_AUTOPTR_CLEANUP_FUNC(graphene_ray_t, graphene_ray_free)

#define GRAPHENE_TYPE_BOX2D (graphene_box2d_get_type ())

GRAPHENE_AVAILABLE_IN_1_12
GType graphene_box2d_get_type (void);

G_DEFINE_AUTOPTR_CLEANUP_FUNC(graphene_box2d_t, graphene_box2d_free)

G_END_DECLS
1 change: 1 addition & 0 deletions include/graphene-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ typedef struct _graphene_matrix_t graphene_matrix_t;
typedef struct _graphene_point_t graphene_point_t;
typedef struct _graphene_size_t graphene_size_t;
typedef struct _graphene_rect_t graphene_rect_t;
typedef struct _graphene_box2d_t graphene_box2d_t;

typedef struct _graphene_point3d_t graphene_point3d_t;
typedef struct _graphene_quad_t graphene_quad_t;
Expand Down
1 change: 1 addition & 0 deletions include/graphene.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#include "graphene-point.h"
#include "graphene-size.h"
#include "graphene-rect.h"
#include "graphene-box2d.h"

#include "graphene-point3d.h"
#include "graphene-quad.h"
Expand Down
1 change: 1 addition & 0 deletions include/meson.build
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
graphene_public_headers = files([
'graphene-box.h',
'graphene-box2d.h',
'graphene-euler.h',
'graphene-frustum.h',
'graphene-macros.h',
Expand Down
Loading

0 comments on commit 21e4bb3

Please sign in to comment.