Skip to content

Commit

Permalink
box2d: Add inlined intersection operator
Browse files Browse the repository at this point in the history
If we want to just know if two boxes intersect each other, we can do
that with an inlined function.

It's not the fastest possible function, because it still requires
calling into graphene_box2d_get_minmax(), as the calling code does not
have access to the private fields of the graphene_box2d_t type.
  • Loading branch information
ebassi committed Aug 11, 2024
1 parent c176f94 commit ac76bb9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
42 changes: 42 additions & 0 deletions include/graphene-box2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#endif

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

Expand Down Expand Up @@ -121,6 +123,46 @@ GRAPHENE_AVAILABLE_IN_1_12
bool graphene_box2d_equal (const graphene_box2d_t *a,
const graphene_box2d_t *b);

static inline bool
graphene_box2d_intersects (const graphene_box2d_t *a,
const graphene_box2d_t *b);

/**
* graphene_box2d_intersects:
* @a: a #graphene_box2d_t
* @b: a #graphene_box2d_t
*
* Checks whether two boxes intersect.
*
* See also: graphene_box2d_intersection()
*
* Returns: true if the boxes intersect, and false otherwise
*
* Since: 1.12
*/
static inline bool
graphene_box2d_intersects (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;
}

GRAPHENE_AVAILABLE_IN_1_12
const graphene_box2d_t * graphene_box2d_zero (void);
GRAPHENE_AVAILABLE_IN_1_12
Expand Down
19 changes: 19 additions & 0 deletions tests/box2d.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,25 @@ box2d_intersection (mutest_spec_t *spec)
mutest_bool_value (graphene_box2d_intersection (&top, &bottom, NULL)),
mutest_to_be_false,
NULL);

graphene_box2d_t a, b, c;
graphene_box2d_init (&a, &GRAPHENE_POINT_INIT (0.f, 0.f), &GRAPHENE_POINT_INIT (2.f, 2.f));
graphene_box2d_init (&b, &GRAPHENE_POINT_INIT (1.f, 1.f), &GRAPHENE_POINT_INIT (2.f, 2.f));
graphene_box2d_init (&c, &GRAPHENE_POINT_INIT (3.f, 3.f), &GRAPHENE_POINT_INIT (4.f, 4.f));

bool a_b = graphene_box2d_intersects (&a, &b);
mutest_expect ("intersect to match intersection (positive)",
mutest_bool_value (a_b),
mutest_to_be_true,
mutest_to_be, graphene_box2d_intersection (&a, &b, NULL),
NULL);

bool a_c = graphene_box2d_intersects (&a, &c);
mutest_expect ("intersect to match intersection (negative)",
mutest_bool_value (a_c),
mutest_to_be_false,
mutest_to_be, graphene_box2d_intersection (&a, &c, NULL),
NULL);
}

static void
Expand Down

0 comments on commit ac76bb9

Please sign in to comment.