Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
TwoVectors.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Misc/AssertionMacros.h"
7#include "Math/UnrealMathUtility.h"
8#include "Containers/UnrealString.h"
9#include "Math/Vector.h"
10
11/**
12 * A pair of 3D vectors.
13 */
14struct FTwoVectors
15{
16public:
17
18 /** Holds the first vector. */
19 FVector v1;
20
21 /** Holds the second vector. */
22 FVector v2;
23
24public:
25
26 /** Default constructor. */
27 FORCEINLINE FTwoVectors();
28
29 /**
30 * Creates and initializes a new instance with the specified vectors.
31 *
32 * @param In1 The first Vector.
33 * @param In2 The second Vector.
34 */
35 FORCEINLINE FTwoVectors( FVector In1, FVector In2 );
36
37 /**
38 * Constructor.
39 *
40 * @param EForceInit Force Init Enum
41 */
42 explicit FORCEINLINE FTwoVectors( EForceInit );
43
44public:
45
46 /**
47 * Gets result of addition of two pairs of vectors.
48 *
49 * @param V The pair to add.
50 * @return Result of addition.
51 */
52 FORCEINLINE FTwoVectors operator+( const FTwoVectors& V ) const;
53
54 /**
55 * Gets result of subtraction of two pairs of vectors.
56 *
57 * @param V The pair to subtract.
58 * @return Result of subtraction.
59 */
60 FORCEINLINE FTwoVectors operator-( const FTwoVectors& V ) const;
61
62 /**
63 * Gets result of scaling pair of vectors.
64 *
65 * @param Scale The scaling factor.
66 * @return Result of Scaling.
67 */
68 FORCEINLINE FTwoVectors operator*( float Scale ) const;
69
70 /**
71 * Gets result of dividing pair of vectors.
72 *
73 * @param Scale What to divide by.
74 * @return Result of division.
75 */
76 FTwoVectors operator/( float Scale ) const;
77
78 /**
79 * Gets result of multiplying two pairs of vectors.
80 *
81 * @param V The pair to multiply with.
82 * @return Result of multiplication.
83 */
84 FORCEINLINE FTwoVectors operator*( const FTwoVectors& V ) const;
85
86 /**
87 * Gets result of division of two pairs of vectors.
88 *
89 * @param V The pair to divide by.
90 * @return Result of division.
91 */
92 FORCEINLINE FTwoVectors operator/( const FTwoVectors& V ) const;
93
94 // Binary comparison operators.
95
96 /**
97 * Checks two pairs for equality.
98 *
99 * @param V The other pair.
100 * @return true if the two pairs are equal, false otherwise..
101 */
102 bool operator==( const FTwoVectors& V ) const;
103
104 /**
105 * Checks two pairs for inequality.
106 *
107 * @param V The other pair.
108 * @return true if the two pairs are different, false otherwise..
109 */
110 bool operator!=( const FTwoVectors& V ) const;
111
112 /**
113 * Error-tolerant comparison.
114 *
115 * @param V The other pair.
116 * @param Tolerance Error Tolerance.
117 * @return true if two pairs are equal within specified tolerance, false otherwise..
118 */
119 bool Equals( const FTwoVectors& V, float Tolerance = UE_KINDA_SMALL_NUMBER ) const;
120
121 // Unary operators.
122
123 /**
124 * Get a negated copy of the pair.
125 *
126 * @return A negated copy of the pair.
127 */
128 FORCEINLINE FTwoVectors operator-() const;
129
130 // Assignment operators.
131
132 /**
133 * Add a pair to this.
134 *
135 * @param The pair to add.
136 * @return Copy of the pair after addition.
137 */
138 FORCEINLINE FTwoVectors operator+=( const FTwoVectors& V );
139
140 /**
141 * Subtract a pair from this.
142 *
143 * @param The pair to subtract.
144 * @return Copy of the pair after subtraction.
145 */
146 FORCEINLINE FTwoVectors operator-=( const FTwoVectors& V );
147
148 /**
149 * Scale the pair.
150 *
151 * @param Scale What to scale by.
152 * @return Copy of the pair after scaling.
153 */
154 FORCEINLINE FTwoVectors operator*=( float Scale );
155
156 /**
157 * Divide the pair.
158 *
159 * @param What to divide by.
160 * @return Copy of the pair after division.
161 */
162 FTwoVectors operator/=( float V );
163
164 /**
165 * Multiply the pair by another.
166 *
167 * @param The other pair.
168 * @return Copy of the pair after multiplication.
169 */
170 FTwoVectors operator*=( const FTwoVectors& V );
171
172 /**
173 * Divide the pair by another.
174 *
175 * @param The other pair.
176 * @return Copy of the pair after multiplication.
177 */
178 FTwoVectors operator/=( const FTwoVectors& V );
179
180 /**
181 * Get a specific component from the pair.
182 *
183 * @param i The index of the component, even indices are for the first vector,
184 * odd ones are for the second. Returns index 5 if out of range.
185 * @return Reference to the specified component.
186 */
187 FVector::FReal& operator[]( int32 i );
188
189public:
190
191 /**
192 * Get the maximum value of all the vector coordinates.
193 *
194 * @return The maximum value of all the vector coordinates.
195 */
196 FVector::FReal GetMax() const;
197
198 /**
199 * Get the minimum value of all the vector coordinates.
200 *
201 * @return The minimum value of all the vector coordinates.
202 */
203 FVector::FReal GetMin() const;
204
205 /**
206 * Get a textual representation of this two-vector.
207 *
208 * @return A string describing the two-vector.
209 */
210 FString ToString() const;
211
212public:
213
214 /**
215 * Serializes the two-vector.
216 *
217 * @param Ar The archive to serialize into.
218 * @param TwoVectors The two-vector to serialize.
219 * @return Reference to the Archive after serialization.
220 */
221 friend FArchive& operator<<( FArchive& Ar, FTwoVectors& TwoVectors )
222 {
223 return Ar << TwoVectors.v1 << TwoVectors.v2;
224 }
225
226 bool Serialize( FArchive& Ar )
227 {
228 Ar << *this;
229 return true;
230 }
231};
232
233
234/* FTwoVectors inline functions
235 *****************************************************************************/
236
237FORCEINLINE FTwoVectors operator*( float Scale, const FTwoVectors& V )
238{
239 return V.operator*( Scale );
240}
241
242
243FORCEINLINE FTwoVectors::FTwoVectors() :
244 v1(0.0f),
245 v2(0.0f)
246{ }
247
248
249FORCEINLINE FTwoVectors::FTwoVectors( FVector In1, FVector In2 )
250 : v1(In1)
251 , v2(In2)
252{ }
253
254
255FORCEINLINE FTwoVectors::FTwoVectors( EForceInit )
256 : v1(ForceInit)
257 , v2(ForceInit)
258{ }
259
260
261FORCEINLINE FTwoVectors FTwoVectors::operator+( const FTwoVectors& V ) const
262{
263 return FTwoVectors(
264 FVector(v1 + V.v1),
265 FVector(v2 + V.v2)
266 );
267}
268
269
270FORCEINLINE FTwoVectors FTwoVectors::operator-( const FTwoVectors& V ) const
271{
272 return FTwoVectors(
273 FVector(v1 - V.v1),
274 FVector(v2 - V.v2)
275 );
276}
277
278
279FORCEINLINE FTwoVectors FTwoVectors::operator*( float Scale ) const
280{
281 return FTwoVectors(
282 FVector(v1 * Scale),
283 FVector(v2 * Scale)
284 );
285}
286
287
288FORCEINLINE FTwoVectors FTwoVectors::operator/( float Scale ) const
289{
290 const float RScale = 1.f / Scale;
291
292 return FTwoVectors(
293 FVector(v1 * RScale),
294 FVector(v2 * RScale)
295 );
296}
297
298
299FORCEINLINE FTwoVectors FTwoVectors::operator*( const FTwoVectors& V ) const
300{
301 return FTwoVectors(
302 FVector(v1 * V.v1),
303 FVector(v2 * V.v2)
304 );
305}
306
307
308FORCEINLINE FTwoVectors FTwoVectors::operator/( const FTwoVectors& V ) const
309{
310 return FTwoVectors(
311 FVector(v1 / V.v1),
312 FVector(v2 / V.v2)
313 );
314}
315
316
317FORCEINLINE bool FTwoVectors::operator==( const FTwoVectors& V ) const
318{
319 return ((v1 == V.v1) && (v2 == V.v2));
320}
321
322
323FORCEINLINE bool FTwoVectors::operator!=( const FTwoVectors& V ) const
324{
325 return ((v1 != V.v1) || (v2 != V.v2));
326}
327
328
329FORCEINLINE bool FTwoVectors::Equals( const FTwoVectors& V, float Tolerance ) const
330{
331 return v1.Equals(V.v1, Tolerance) && v2.Equals(V.v2, Tolerance);
332}
333
334
335FORCEINLINE FTwoVectors FTwoVectors::operator-() const
336{
337 return FTwoVectors(
338 FVector(-v1),
339 FVector(-v2)
340 );
341}
342
343
344FORCEINLINE FTwoVectors FTwoVectors::operator+=( const FTwoVectors& V )
345{
346 v1 += V.v1;
347 v2 += V.v2;
348
349 return *this;
350}
351
352
353FORCEINLINE FTwoVectors FTwoVectors::operator-=( const FTwoVectors& V )
354{
355 v1 -= V.v1;
356 v2 -= V.v2;
357
358 return *this;
359}
360
361
362FORCEINLINE FTwoVectors FTwoVectors::operator*=( float Scale )
363{
364 v1 *= Scale;
365 v2 *= Scale;
366
367 return *this;
368}
369
370
371FORCEINLINE FTwoVectors FTwoVectors::operator/=( float V )
372{
373 const float RV = 1.f/V;
374
375 v1 *= RV;
376 v2 *= RV;
377
378 return *this;
379}
380
381
382FORCEINLINE FTwoVectors FTwoVectors::operator*=( const FTwoVectors& V )
383{
384 v1 *= V.v1;
385 v2 *= V.v2;
386 return *this;
387}
388
389
390FORCEINLINE FTwoVectors FTwoVectors::operator/=( const FTwoVectors& V )
391{
392 v1 /= V.v1;
393 v2 /= V.v2;
394
395 return *this;
396}
397
398
399FORCEINLINE FVector::FReal FTwoVectors::GetMax() const
400{
401 const FVector::FReal MaxMax = FMath::Max(FMath::Max(v1.X, v1.Y), v1.Z);
402 const FVector::FReal MaxMin = FMath::Max(FMath::Max(v2.X, v2.Y), v2.Z);
403
404 return FMath::Max(MaxMax, MaxMin);
405}
406
407
408FORCEINLINE FVector::FReal FTwoVectors::GetMin() const
409{
410 const FVector::FReal MinMax = FMath::Min(FMath::Min(v1.X, v1.Y), v1.Z);
411 const FVector::FReal MinMin = FMath::Min(FMath::Min(v2.X, v2.Y), v2.Z);
412
413 return FMath::Min(MinMax, MinMin);
414}
415
416
417FORCEINLINE FVector::FReal& FTwoVectors::operator[]( int32 i )
418{
419 check(i > -1);
420 check(i < 6);
421
422 switch(i)
423 {
424 case 0: return v1.X;
425 case 1: return v2.X;
426 case 2: return v1.Y;
427 case 3: return v2.Y;
428 case 4: return v1.Z;
429 default: return v2.Z;
430 }
431}
432
433
434FORCEINLINE FString FTwoVectors::ToString() const
435{
436 return FString::Printf(TEXT("V1=(%s) V2=(%s)"), *v1.ToString(), *v2.ToString());
437}
438
439template <> struct TIsPODType<FTwoVectors> { enum { Value = true }; };
#define check(expr)
#define TEXT(x)
Definition Platform.h:1108
#define FORCEINLINE
Definition Platform.h:644
FORCEINLINE FTwoVectors operator*(float Scale, const FTwoVectors &V)
Definition TwoVectors.h:237
#define UE_KINDA_SMALL_NUMBER