room101/PLANE.HPP

119 lines
3.5 KiB
Text
Raw Permalink Normal View History

#ifndef __PLANE_H
#define __PLANE_H
#include "vector.hpp"
#include <string.h>
#define PLANE_FRONT 0
#define PLANE_BACK 1
typedef float Plane[4];
/*******************************************************************************
** PlaneCopy
*******************************************************************************/
inline void
PlaneCopy(Plane dst, Plane src)
{
memcpy(dst, src, sizeof(Plane));
}
/*******************************************************************************
** PlanesAreCollinear
*******************************************************************************/
inline int
PlanesAreCollinear(Plane A, Plane B)
{
return
(
EpsilonEqual(A[0], B[0]) &&
EpsilonEqual(A[1], B[1]) &&
EpsilonEqual(A[2], B[2]) &&
EpsilonEqual(A[3], B[3])
) || (
EpsilonEqual(A[0], -B[0]) &&
EpsilonEqual(A[1], -B[1]) &&
EpsilonEqual(A[2], -B[2]) &&
EpsilonEqual(A[3], -B[3])
);
}
/*******************************************************************************
** PlanesAreEqual
*******************************************************************************/
inline int
PlanesAreEqual(Plane A, Plane B)
{
return
EpsilonEqual(A[0], B[0]) &&
EpsilonEqual(A[1], B[1]) &&
EpsilonEqual(A[2], B[2]) &&
EpsilonEqual(A[3], B[3]);
}
inline void
PlaneCreate(Plane dst, float x, float y, float z, float w)
{
dst[0] = x;
dst[1] = y;
dst[2] = z;
dst[3] = w;
}
/*******************************************************************************
** PlaneCreate
*******************************************************************************/
inline void
PlaneInit(Plane plane, Vector normal, Vector point)
{
VectorNormalize(plane, normal);
plane[3] = -VectorDotProduct(plane, point);
}
/*******************************************************************************
** PlaneNegate
*******************************************************************************/
inline void
PlaneNegate(Plane plane)
{
plane[0] = -plane[0];
plane[1] = -plane[1];
plane[2] = -plane[2];
plane[3] = -plane[3];
}
/*******************************************************************************
** PlaneDistanceTo
*******************************************************************************/
inline float
PlaneDistanceTo(Plane plane, Vector vector)
{
return VectorDotProduct(plane, vector) + plane[3];
}
/*******************************************************************************
** PlaneIntersection
*******************************************************************************/
inline int
PlaneIntersection(Plane plane, Vector A, Vector B, Vector intersect)
{
float scale;
float dist1 = PlaneDistanceTo(plane, A);
float dist2 = PlaneDistanceTo(plane, B);
if (dist1 < 0.0 ^ dist2 < 0.0)
{
scale = dist1 / (dist1 - dist2);
intersect[0] = Interpolate(scale, A[0], B[0]);
intersect[1] = Interpolate(scale, A[1], B[1]);
intersect[2] = Interpolate(scale, A[2], B[2]);
return 1;
}
return 0;
}
/*******************************************************************************
** PlaneSide
*******************************************************************************/
inline int
PlaneSide(Plane plane, Vector point)
{
return PlaneDistanceTo(plane, point) < 0.0f;
}
#endif