1// This code is in the public domain -- Ignacio Casta�o <castano@gmail.com>
2
3#pragma once
4#ifndef NV_MATH_PLANE_INL
5#define NV_MATH_PLANE_INL
6
7#include "plane.h"
8#include "vector.inl"
9
10namespace nv
11{
12    inline Plane::Plane() {}
13    inline Plane::Plane(float x, float y, float z, float w) : v(x, y, z, w) {}
14    inline Plane::Plane(const Vector4 & v) : v(v) {}
15    inline Plane::Plane(const Vector3 & v, float d) : v(v, d) {}
16    inline Plane::Plane(const Vector3 & normal, const Vector3 & point) : v(normal, -dot(normal, point)) {}
17    inline Plane::Plane(const Vector3 & v0, const Vector3 & v1, const Vector3 & v2) {
18        Vector3 n = cross(v1-v0, v2-v0);
19        float d = -dot(n, v0);
20        v = Vector4(n, d);
21    }
22
23    inline const Plane & Plane::operator=(const Plane & p) { v = p.v; return *this; }
24
25    inline Vector3 Plane::vector() const { return v.xyz(); }
26    inline float Plane::offset() const { return v.w; }
27
28    // Normalize plane.
29    inline Plane normalize(const Plane & plane, float epsilon = NV_EPSILON)
30    {
31        const float len = length(plane.vector());
32        const float inv = isZero(len, epsilon) ? 0 : 1.0f / len;
33        return Plane(plane.v * inv);
34    }
35
36    // Get the signed distance from the given point to this plane.
37    inline float distance(const Plane & plane, const Vector3 & point)
38    {
39        return dot(plane.vector(), point) + plane.offset();
40    }
41
42    inline void Plane::operator*=(float s)
43    {
44        v *= s;
45    }
46
47} // nv namespace
48
49#endif // NV_MATH_PLANE_H
50