Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games. 2 : * This file is part of 0 A.D. 3 : * 4 : * 0 A.D. is free software: you can redistribute it and/or modify 5 : * it under the terms of the GNU General Public License as published by 6 : * the Free Software Foundation, either version 2 of the License, or 7 : * (at your option) any later version. 8 : * 9 : * 0 A.D. is distributed in the hope that it will be useful, 10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 : * GNU General Public License for more details. 13 : * 14 : * You should have received a copy of the GNU General Public License 15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. 16 : */ 17 : 18 : /* 19 : * A Plane in R3 and several utility methods. 20 : */ 21 : 22 : // Note that the format used for the plane equation is 23 : // Ax + By + Cz + D = 0, where <A,B,C> is the normal vector. 24 : 25 : #include "precompiled.h" 26 : 27 : #include "Plane.h" 28 : 29 : #include "maths/MathUtil.h" 30 : #include "maths/Vector3D.h" 31 : 32 : const float CPlane::m_EPS = 0.001f; 33 : 34 530 : CPlane::CPlane() 35 : { 36 530 : m_Dist = 0.0f; 37 530 : } 38 : 39 : //sets the plane equation from 3 points on that plane 40 0 : void CPlane::Set(const CVector3D& p1, const CVector3D& p2, const CVector3D& p3) 41 : { 42 0 : CVector3D D1, D2; 43 0 : CVector3D Norm; 44 : 45 : //calculate two vectors on the surface of the plane 46 0 : D1 = p2-p1; 47 0 : D2 = p3-p1; 48 : 49 : //cross multiply gives normal 50 0 : Norm = D2.Cross(D1); 51 : 52 0 : Set(Norm, p1); 53 0 : } 54 : 55 : //sets the plane equation from a normal and a point on 56 : //that plane 57 0 : void CPlane::Set(const CVector3D& norm, const CVector3D& point) 58 : { 59 0 : m_Norm = norm; 60 : 61 0 : m_Dist = - (norm.X * point.X + norm.Y * point.Y + norm.Z * point.Z); 62 : 63 : // Normalize (); 64 0 : } 65 : 66 : //normalizes the plane equation 67 24 : void CPlane::Normalize() 68 : { 69 24 : float Scale = 1.0f/m_Norm.Length(); 70 : 71 24 : m_Norm.X *= Scale; 72 24 : m_Norm.Y *= Scale; 73 24 : m_Norm.Z *= Scale; 74 24 : m_Dist *= Scale; 75 24 : } 76 : 77 : //returns the side of the plane on which this point 78 : //lies. 79 0 : PLANESIDE CPlane::ClassifyPoint(const CVector3D& point) const 80 : { 81 0 : float Dist = DistanceToPlane(point); 82 : 83 0 : if (Dist > m_EPS) 84 : return PS_FRONT; 85 0 : else if (Dist < -m_EPS) 86 0 : return PS_BACK; 87 : 88 : return PS_ON; 89 : } 90 : 91 : //calculates the intersection point of a line with this 92 : //plane. Returns false if there is no intersection 93 12 : bool CPlane::FindLineSegIntersection(const CVector3D& start, const CVector3D& end, CVector3D* intsect) const 94 : { 95 12 : float dist1 = DistanceToPlane(start); 96 12 : float dist2 = DistanceToPlane(end); 97 : 98 12 : if( (dist1 < 0 && dist2 < 0) || (dist1 >= 0 && dist2 >= 0) ) 99 : return false; 100 : 101 8 : float t = (-dist1) / (dist2-dist1); 102 8 : *intsect = Interpolate(start, end, t); 103 : 104 8 : return true; 105 : } 106 : 107 0 : bool CPlane::FindRayIntersection(const CVector3D& start, const CVector3D& direction, CVector3D* intsect) const 108 : { 109 0 : float dot = m_Norm.Dot(direction); 110 0 : if (dot == 0.0f) 111 : return false; 112 : 113 0 : *intsect = start - (direction * (DistanceToPlane(start)/dot)); 114 0 : return true; 115 : }