aboutsummaryrefslogtreecommitdiff
path: root/src/Math/Plane.c
blob: 23711c5d292d1769fc0e7f470a8934fcc44524ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <BH/Math.h>
#include <string.h>


#define EPSILON 0.00001f
#define PI      3.14159265358979323846f


int BH_PlaneFromPoints(const float *a,
                       const float *b,
                       const float *c,
                       float *out)
{
    float tmp1[3], tmp2[3];

    BH_Vec3fSub(b, a, tmp1);
    BH_Vec3fSub(c, a, tmp2);
    BH_Vec3fCross(tmp2, tmp1, tmp1);
    if (BH_Vec3fNormalEx(tmp1, tmp1) < EPSILON)
        return BH_ERROR;

    out[3] = BH_Vec3fDot(a, tmp1);
    memcpy(out, tmp1, sizeof(tmp1));
    return BH_OK;
}


float BH_PlaneDistance(const float *plane,
                       const float *point)
{
    return BH_Vec3fDot(plane, point) - plane[3];
}


void BH_PlaneClosestPoint(const float *plane,
                          const float *point,
                          float *out)
{
    float tmp[3];

    BH_Vec3fScale(plane, BH_PlaneDistance(plane, point), tmp);
    BH_Vec3fSub(point, tmp, out);
}