Pioneer
Loading...
Searching...
No Matches
MathUtil.h
Go to the documentation of this file.
1// Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#ifndef _MATHUTIL_H
5#define _MATHUTIL_H
6
7#include "matrix3x3.h"
8#include "matrix4x4.h"
9#include "vector3.h"
10
11namespace MathUtil {
12
13 // random point on a sphere, distributed uniformly by area
14 vector3d RandomPointOnSphere(double minRadius, double maxRadius);
15 inline vector3d RandomPointOnSphere(double radius) { return RandomPointOnSphere(radius, radius); }
16
17 vector3d RandomPointInCircle(double minRadius, double maxRadius);
18 inline vector3d RandomPointInCircle(double radius) { return RandomPointInCircle(0.0, radius); }
19 inline vector3d RandomPointOnCircle(double radius) { return RandomPointInCircle(radius, radius); }
20
21 // interpolation, glsl style naming "mix"
22 template <class T, class F>
23 inline T mix(const T &v1, const T &v2, const F t)
24 {
25 return t * v2 + (F(1.0) - t) * v1;
26 }
27 template <class T, class F>
28 inline T Lerp(const T &v1, const T &v2, const F t)
29 {
30 return mix(v1, v2, t);
31 }
32
33 inline float Dot(const vector3f &a, const vector3f &b) { return a.x * b.x + a.y * b.y + a.z * b.z; }
34
35 // unit vector orthogonal to given vector
36 template <typename T>
38 {
39 vector3<T> b;
40 if (std::abs(a.x) > std::abs(a.y)) {
41 if (std::abs(a.y) > std::abs(a.z))
42 b = vector3<T>(-a.y, a.x, 0);
43 else
44 b = vector3<T>(a.z, 0, -a.x);
45 } else {
46 if (std::abs(a.x) > std::abs(a.z))
47 b = vector3<T>(-a.y, a.x, 0);
48 else
49 b = vector3<T>(0, -a.z, a.y);
50 }
51 return b.Normalized();
52 }
53
54 // matrix4x4f utility functions
58
59 // matrix3x3f utility functions
62
63 // distance from a line segment:
64 float DistanceFromLineSegment(const vector3f &start, const vector3f &end, const vector3f &pos, bool &isWithinLineSegment);
65 float DistanceFromLine(const vector3f &start, const vector3f &end, const vector3f &pos);
66
67 inline static matrix3x3d LookAt(const vector3d eye, const vector3d target, const vector3d up)
68 {
69 const vector3d z = (eye - target).NormalizedSafe();
70 const vector3d x = (up.Cross(z)).NormalizedSafe();
71 const vector3d y = (z.Cross(x)).NormalizedSafe();
72
73 return matrix3x3d::FromVectors(x, y, z);
74 }
75
76//#define TEST_MATHUTIL
77#ifdef TEST_MATHUTIL
78 bool TestDistanceFromLine();
79#endif
80} // namespace MathUtil
81
82#endif
static matrix3x3 FromVectors(const vector3< double > &rx, const vector3< double > &ry, const vector3< double > &rz)
Definition matrix3x3.h:68
T y
Definition vector3.h:18
T x
Definition vector3.h:18
T z
Definition vector3.h:18
vector3 Normalized() const
Definition vector3.h:125
vector3 Cross(const vector3 &b) const
Definition vector3.h:117
Definition MathUtil.cpp:7
vector3d RandomPointOnSphere(double minRadius, double maxRadius)
Definition MathUtil.cpp:9
matrix4x4f Inverse(const matrix4x4f &cell)
Definition MathUtil.cpp:33
vector3< T > OrthogonalDirection(const vector3< T > &a)
Definition MathUtil.h:37
T mix(const T &v1, const T &v2, const F t)
Definition MathUtil.h:23
T Lerp(const T &v1, const T &v2, const F t)
Definition MathUtil.h:28
float Dot(const vector3f &a, const vector3f &b)
Definition MathUtil.h:33
float DistanceFromLineSegment(const vector3f &start, const vector3f &end, const vector3f &pos, bool &isWithinLineSegment)
Definition MathUtil.cpp:246
vector3d RandomPointInCircle(double minRadius, double maxRadius)
Definition MathUtil.cpp:20
matrix4x4f InverseSlow(const matrix4x4f &cell)
Definition MathUtil.cpp:54
vector3d RandomPointOnCircle(double radius)
Definition MathUtil.h:19
float DistanceFromLine(const vector3f &start, const vector3f &end, const vector3f &pos)
Definition MathUtil.cpp:265
matrix4x4f Transpose(const matrix4x4f &cell)
Definition MathUtil.cpp:183