Tiberian Technologies Scripts Reference Revision: 9000
Loading...
Searching...
No Matches
plane.h
1/* Renegade Scripts.dll
2 Copyright 2013 Tiberian Technologies
3
4 This file is part of the Renegade scripts.dll
5 The Renegade scripts.dll is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License as published by the Free
7 Software Foundation; either version 2, or (at your option) any later
8 version. See the file COPYING for more details.
9 In addition, an exemption is given to allow Run Time Dynamic Linking of this code with any closed source module that does not contain code covered by this licence.
10 Only the source code to the module(s) containing the licenced code has to be released.
11*/
12#ifndef _MATH_PLANE_H_
13#define _MATH_PLANE_H_
14
15#include "Vector3.h"
16#include "SphereClass.h"
17class PlaneClass
18{
19public:
20 Vector3 N;
21 float D;
22
23 PlaneClass() : N(0.0f, 0.0f, 1.0f), D(0.0f) {};
24
25 PlaneClass(float nx, float ny, float nz, float dist)
26 {
27 Set(nx, ny, nz, dist);
28 };
29
30 PlaneClass(const Vector3& normal, float dist)
31 {
32 Set(normal, dist);
33 };
34
35 PlaneClass(const Vector3& normal, const Vector3& point)
36 {
37 Set(normal, point);
38 };
39
40 PlaneClass(const Vector3& point1, const Vector3& point2, const Vector3& point3)
41 {
42 Set(point1, point2, point3);
43 };
44
45 inline void Set(float a, float b, float c, float d)
46 {
47 N.X = a;
48 N.Y = b;
49 N.Z = c;
50 D = d;
51 };
52
53 inline void Set(const Vector3& normal, float dist)
54 {
55 N = normal;
56 D = dist;
57 };
58
59 inline void Set(const Vector3& normal, const Vector3& point)
60 {
61 N = normal;
62 D = Vector3::Dot_Product(normal, point);
63 };
64
65 inline void Set(const Vector3& point1, const Vector3& point2, const Vector3& point3)
66 {
67 N = Vector3::Cross_Product((point2 - point1), (point3 - point1));
68 if (N != Vector3(0.0f, 0.0f, 0.0f)) {
69 // Points are not colinear. Normalize N and calculate D.
70 N.Normalize();
71 D = N * point1;
72 } else {
73 // They are colinear - return default plane (constructors can't fail).
74 N = Vector3(0.0f, 0.0f, 1.0f);
75 D = 0.0f;
76 }
77 }
78
79 inline void Normalize()
80 {
81 float oolength = 1.0f / N.Length();
82 N *= oolength;
83 D *= oolength;
84 }
85
86 inline PlaneClass operator - ()
87 {
88 return PlaneClass(-N, D);
89 }
90 bool In_Front(const Vector3 & point) const;
91 bool In_Front(const SphereClass & sphere) const;
92 bool Compute_Intersection(const Vector3 & p0,const Vector3 & p1,float * set_t) const;
93};
94inline bool PlaneClass::In_Front(const Vector3 & point) const
95{
96 float dist = Vector3::Dot_Product(point,N);
97 return (dist > D);
98}
99inline bool PlaneClass::In_Front(const SphereClass & sphere) const
100{
101 float dist = Vector3::Dot_Product(sphere.Center,N);
102 return ((dist - D) >= sphere.Radius);
103}
104inline bool PlaneClass::Compute_Intersection(const Vector3 & p0,const Vector3 & p1,float * set_t) const
105{
106 float num,den;
107 den = Vector3::Dot_Product(N,p1-p0);
108 if (den == 0.0f)
109 {
110 return false;
111 }
112 num = -(Vector3::Dot_Product(N,p0) - D);
113 *set_t = num/den;
114 if ((*set_t < 0.0f) || (*set_t > 1.0f))
115 {
116 return false;
117 }
118 return true;
119}
120#endif