/** * graphene_triangle_contains_point: * @t: a #graphene_triangle_t * @p: a #graphene_point3d_t * * Checks whether the given triangle @t contains the point @p. * * Returns: `true` if the point is inside the triangle * * Since: 1.2 */ bool graphene_triangle_contains_point (const graphene_triangle_t *t, const graphene_point3d_t *p) { graphene_vec3_t point, v1, v2, v3; float dot_11, dot_12, dot_13; float dot_22, dot_23; float inv_denom, u, v; graphene_point3d_to_vec3 (p, &point); /* we use the barycoordinates from the given point to check * if the point is inside the triangle. * * see: http://www.blackpawn.com/texts/pointinpoly/default.html */ graphene_vec3_subtract (&t->c, &t->a, &v1); graphene_vec3_subtract (&t->b, &t->a, &v2); graphene_vec3_subtract (&point, &t->a, &v3); dot_11 = graphene_vec3_dot (&v1, &v1); dot_12 = graphene_vec3_dot (&v1, &v2); dot_13 = graphene_vec3_dot (&v1, &v3); dot_22 = graphene_vec3_dot (&v2, &v2); dot_23 = graphene_vec3_dot (&v2, &v3); inv_denom = 1.f / (dot_11 * dot_22 - dot_12 * dot_12); u = (dot_22 * dot_13 - dot_12 * dot_23) * inv_denom; v = (dot_11 * dot_23 - dot_12 * dot_13) * inv_denom; return (u >= 0.f) && (v >= 0.f) && (u + v < 1.f); }
/** * graphene_ray_get_distance_to_point: * @r: a #graphene_ray_t * @p: a #graphene_point3d_t * * Computes the distance from the origin of the given ray to the given point. * * Returns: the distance of the point * * Since: 1.4 */ float graphene_ray_get_distance_to_point (const graphene_ray_t *r, const graphene_point3d_t *p) { graphene_vec3_t point; graphene_vec3_t tmp; float distance; graphene_point3d_to_vec3 (p, &point); graphene_vec3_subtract (&point, &r->origin, &tmp); distance = graphene_vec3_dot (&tmp, &r->direction); /* the point is behind the ray */ if (distance < 0) { graphene_vec3_subtract (&r->origin, &point, &tmp); return graphene_vec3_length (&tmp); } /* get the position on the ray at the given distance */ graphene_vec3_scale (&r->direction, distance, &tmp); graphene_vec3_add (&tmp, &r->origin, &tmp); graphene_vec3_subtract (&tmp, &point, &tmp); return graphene_vec3_length (&tmp); }
/** * graphene_triangle_init_from_point3d: * @t: the #graphene_triangle_t to initialize * @a: (nullable): a #graphene_point3d_t * @b: (nullable): a #graphene_point3d_t * @c: (nullable): a #graphene_point3d_t * * Initializes a #graphene_triangle_t using the three given 3D points. * * Returns: (transfer none): the initialized #graphene_triangle_t * * Since: 1.2 */ graphene_triangle_t * graphene_triangle_init_from_point3d (graphene_triangle_t *t, const graphene_point3d_t *a, const graphene_point3d_t *b, const graphene_point3d_t *c) { if (a != NULL) graphene_point3d_to_vec3 (a, &t->a); else graphene_vec3_init_from_vec3 (&t->a, graphene_vec3_zero ()); if (b != NULL) graphene_point3d_to_vec3 (b, &t->b); else graphene_vec3_init_from_vec3 (&t->b, graphene_vec3_zero ()); if (c != NULL) graphene_point3d_to_vec3 (c, &t->c); else graphene_vec3_init_from_vec3 (&t->b, graphene_vec3_zero ()); return t; }
/** * graphene_ray_init: * @r: the #graphene_ray_t to initialize * @origin: (nullable): the origin of the ray * @direction: (nullable): the direction vector * * Initializes the given #graphene_ray_t using the given @origin * and @direction values. * * Returns: (transfer none): the initialized ray * * Since: 1.4 */ graphene_ray_t * graphene_ray_init (graphene_ray_t *r, const graphene_point3d_t *origin, const graphene_vec3_t *direction) { if (origin != NULL) graphene_point3d_to_vec3 (origin, &r->origin); else graphene_vec3_init_from_vec3 (&r->origin, graphene_vec3_zero ()); if (direction != NULL) graphene_vec3_normalize (direction, &r->direction); else graphene_vec3_init_from_vec3 (&r->direction, graphene_vec3_zero ()); return r; }
/** * graphene_ray_get_closest_point_to_point: * @r: a #graphene_ray_t * @p: a #graphene_point3d_t * @res: (out caller-allocates): return location for the closest point3d * * Computes the point on the given #graphene_ray_t that is closest to the * given point @p. * * Since: 1.4 */ void graphene_ray_get_closest_point_to_point (const graphene_ray_t *r, const graphene_point3d_t *p, graphene_point3d_t *res) { graphene_vec3_t point, result; float distance; graphene_point3d_to_vec3 (p, &point); graphene_vec3_subtract (&point, &r->origin, &result); distance = graphene_vec3_dot (&result, &r->direction); if (distance < 0) graphene_vec3_init_from_vec3 (&result, &r->origin); else { graphene_vec3_scale (&r->direction, distance, &result); graphene_vec3_add (&result, &r->origin, &result); } graphene_point3d_init_from_vec3 (res, &result); }