ebassi / graphene

A thin layer of graphic data types

Home Page:http://ebassi.github.io/graphene

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

graphene_box_intersection returns incorrect results

vanvugt opened this issue · comments

I started trying to use graphene_box_intersection today, but found it returns true when it most definitely should return false.

This happens for non-intersecting boxes whose intersection returns a negative volume in one of the dimensions (min.[xyz] > max.[xyz]). So it's easy to work around and just ignore any intersection with higher min than max values. But of course, the function itself should ensure that never happens and returns false instead of true.

The source of the problem appears to be graphene_box_intersection's use of:

bool
(graphene_simd4f_cmp_ge) (const graphene_simd4f_t a,
                          const graphene_simd4f_t b)
{
  return a.x >= b.x &&
         a.y >= b.y &&
         a.z >= b.z &&
         a.w >= b.w;
}

Actually we need a.x >= b.x || a.y >= b.y || a.z >= b.z for correct operation of:

bool
graphene_box_intersection (const graphene_box_t *a,
                           const graphene_box_t *b,
                           graphene_box_t       *res)
{
  graphene_simd4f_t min, max;

  min = graphene_simd4f_max (a->min.value, b->min.value);
  max = graphene_simd4f_min (a->max.value, b->max.value);

  if (graphene_simd4f_cmp_ge (min, max))
    {
      if (res != NULL)
        graphene_box_init_from_box (res, graphene_box_empty ());
      
      return false;
    }
  
  if (res != NULL)
    graphene_box_init_from_simd4f (res, min, max);
  
  return true;
}