Richard Boegli's CnC_Generals_Zero_Hour Fork WIP
This is documentation of Richard Boegil's Zero Hour Fork
 
Loading...
Searching...
No Matches
lineseg.cpp
Go to the documentation of this file.
1/*
2** Command & Conquer Generals Zero Hour(tm)
3** Copyright 2025 Electronic Arts Inc.
4**
5** This program is free software: you can redistribute it and/or modify
6** it under the terms of the GNU General Public License as published by
7** the Free Software Foundation, either version 3 of the License, or
8** (at your option) any later version.
9**
10** This program is distributed in the hope that it will be useful,
11** but WITHOUT ANY WARRANTY; without even the implied warranty of
12** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13** GNU General Public License for more details.
14**
15** You should have received a copy of the GNU General Public License
16** along with this program. If not, see <http://www.gnu.org/licenses/>.
17*/
18
19/***********************************************************************************************
20 *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
21 ***********************************************************************************************
22 * *
23 * Project Name : WWMath *
24 * *
25 * $Archive:: /Commando/Code/wwmath/lineseg.cpp $*
26 * *
27 * Author:: Greg_h *
28 * *
29 * $Modtime:: 3/16/00 3:16p $*
30 * *
31 * $Revision:: 25 $*
32 * *
33 *---------------------------------------------------------------------------------------------*
34 * Functions: *
35 * LineSegClass::Set -- Initialize this 'lineseg' by transforming another 'lineseg' *
36 * LineSegClass::Set_Random -- create a random linesegment within the given space *
37 * LineSegClass::Find_Point_Closest_To -- Finds point on line closest to point supplied. *
38 * LineSegClass::Find_Intersection -- Finds the closest points on the two lines *
39 * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
40
41#include "lineseg.h"
42//#include <stdlib.h>
43
44#include "matrix3d.h"
45
46/***********************************************************************************************
47 * LineSegClass::Set -- Initialize this 'lineseg' by transforming another 'lineseg' *
48 * *
49 * INPUT: *
50 * *
51 * OUTPUT: *
52 * *
53 * WARNINGS: *
54 * *
55 * HISTORY: *
56 * 6/17/98 GTH : Created. *
57 *=============================================================================================*/
58void LineSegClass::Set(const LineSegClass & that,const Matrix3D & tm)
59{
60 /*
61 ** Transform P0 and P1
62 */
65
66 /*
67 ** Just calculate DP
68 */
69 DP = P1 - P0;
70
71 /*
72 ** Rotate the direction vector
73 */
75
76 /*
77 ** Length should be un-changed
78 */
79 Length = that.Length;
80}
81
82/***********************************************************************************************
83 * LineSegClass::Set_Random -- create a random linesegment within the given space *
84 * *
85 * INPUT: *
86 * *
87 * OUTPUT: *
88 * *
89 * WARNINGS: *
90 * *
91 * HISTORY: *
92 * 4/21/98 GTH : Created. *
93 *=============================================================================================*/
95{
96 float frac;
97
98 frac = WWMath::Random_Float();
99 P0.X = min.X + frac * (max.X - min.X);
100 frac = WWMath::Random_Float();
101 P0.Y = min.Y + frac * (max.Y - min.Y);
102 frac = WWMath::Random_Float();
103 P0.Z = min.Z + frac * (max.Z - min.Z);
104
105 frac = WWMath::Random_Float();
106 P1.X = min.X + frac * (max.X - min.X);
107 frac = WWMath::Random_Float();
108 P1.Y = min.Y + frac * (max.Y - min.Y);
109 frac = WWMath::Random_Float();
110 P1.Z = min.Z + frac * (max.Z - min.Z);
111
112 DP = P1 - P0;
113 Dir = DP;
114 Dir.Normalize();
115 Length = DP.Length();
116}
117
118
119
120
121/***********************************************************************************************
122 * LineSegClass::Find_Point_Closest_To -- Finds point on line closest to point supplied. *
123 * *
124 * INPUT: *
125 * *
126 * OUTPUT: *
127 * *
128 * WARNINGS: *
129 * *
130 * HISTORY: *
131 * 07/26/1999 SKB : Created. *
132 *=============================================================================================*/
134{
135 // Get a vector from one line endpoint to point in question.
136 Vector3 v_0_pos = (pos - P0);
137 float dotprod = Vector3::Dot_Product(Dir, v_0_pos);
138
139 // Check to see if point is past either of the endpoints.
140 // (Unable to draw a perpendicular line from the point to the line segment.)
141 if (dotprod <= 0.0) {
142 return(P0);
143 } else if (dotprod >= Length) {
144 return(P1);
145 }
146
147 // Find point on line seg that is closest to pos passed in.
148 Vector3 point = P0 + (dotprod * Dir);
149 return(point);
150}
151
152
153/***********************************************************************************************
154 * LineSegClass::Find_Intersection -- Finds the closest points on the two lines.. *
155 * *
156 * INPUT: *
157 * *
158 * OUTPUT: *
159 * *
160 * WARNINGS: *
161 * *
162 * HISTORY: *
163 * 03/03/2000 PDS : Created. *
164 *=============================================================================================*/
165bool
167(
168 const LineSegClass & other_line,
169 Vector3 * p1,
170 float * fraction1,
171 Vector3 * p2,
172 float * fraction2
173) const
174{
175 bool retval = false;
176
177#ifdef ALLOW_TEMPORARIES
178 Vector3 cross1 = Vector3::Cross_Product (Dir, other_line.Dir);
179 Vector3 cross2 = Vector3::Cross_Product (other_line.P0 - P0, other_line.Dir);
180 float top1 = cross2 * cross1;
181 float bottom1 = cross1 * cross1;
182
183 Vector3 cross3 = Vector3::Cross_Product (other_line.Dir, Dir);
184 Vector3 cross4 = Vector3::Cross_Product (P0 - other_line.P0, Dir);
185 float top2 = cross4 * cross3;
186 float bottom2 = cross3 * cross3;
187#else
188 Vector3 cross1, cross2, cross3, cross4;
189
190 Vector3::Cross_Product(Dir, other_line.Dir, &cross1);
191 Vector3::Cross_Product(other_line.P0 - P0, other_line.Dir, &cross2);
192 float top1 = Vector3::Dot_Product(cross2, cross1);
193 float bottom1 = Vector3::Dot_Product(cross1, cross1);
194
195 Vector3::Cross_Product(other_line.Dir, Dir, &cross3);
196 Vector3::Cross_Product(P0 - other_line.P0, Dir, &cross4);
197 float top2 = Vector3::Dot_Product(cross4, cross3);
198 float bottom2 = Vector3::Dot_Product(cross3, cross3);
199#endif
200
201 //
202 // If either of the divisors are 0, then the lines are parallel
203 //
204 if (bottom1 != 0 && bottom2 != 0) {
205 float length1 = top1 / bottom1;
206 float length2 = top2 / bottom2;
207
208 //
209 // Calculate the closest points on both lines.
210 // Note: If the points are the same, then the lines intersect.
211 //
212 (*p1) = P0 + (Dir * length1);
213 (*p2) = other_line.P0 + (other_line.Dir * length2);
214
215 //
216 // Return the fractions if they caller wants them
217 //
218 if (fraction1 != NULL) {
219 (*fraction1) = length1 / Length;
220 }
221
222 if (fraction2 != NULL) {
223 (*fraction2) = length2 / Length;
224 }
225
226 retval = true;
227 }
228
229 return retval;
230}
231
#define NULL
Definition BaseType.h:92
#define min(x, y)
Definition BaseType.h:101
#define max(x, y)
Definition BaseType.h:105
Vector3 P0
Definition lineseg.h:85
Vector3 DP
Definition lineseg.h:87
LineSegClass(void)
Definition lineseg.h:62
Vector3 Find_Point_Closest_To(const Vector3 &pos) const
Definition lineseg.cpp:133
float Length
Definition lineseg.h:89
void Set(const Vector3 &p0, const Vector3 &p1)
Definition lineseg.h:66
Vector3 Dir
Definition lineseg.h:88
bool Find_Intersection(const LineSegClass &other_line, Vector3 *p1, float *fraction1, Vector3 *p2, float *fraction2) const
Definition lineseg.cpp:167
void Set_Random(const Vector3 &min, const Vector3 &max)
Definition lineseg.cpp:94
Vector3 P1
Definition lineseg.h:86
Vector3 Rotate_Vector(const Vector3 &vect) const
Definition matrix3d.cpp:300
static WWINLINE void Transform_Vector(const Matrix3D &tm, const Vector3 &in, Vector3 *out)
Definition matrix3d.h:1742
static WWINLINE float Dot_Product(const Vector3 &a, const Vector3 &b)
Definition vector3.h:293
static WWINLINE void Cross_Product(const Vector3 &a, const Vector3 &b, Vector3 *result)
Definition vector3.h:374
static float Random_Float(void)
Definition wwmath.cpp:78