-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathUtils.h
More file actions
96 lines (83 loc) · 1.79 KB
/
Copy pathMathUtils.h
File metadata and controls
96 lines (83 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#ifndef _MATHUTILS_H_
#define _MATHUTILS_H_
#include "Vec3D.h"
#include "math.h"
inline double max (
const float& iA,
const float& iB
) {
return ( ( iA > iB ) ? iA : iB );
}
inline double min (
const float& iA,
const float& iB
) {
return ( ( iA < iB ) ? iA : iB );
}
template <typename T>
inline T max (
const T& iA,
const T& iB
) {
return ( ( iA > iB ) ? iA : iB );
}
template <typename T>
inline T min (
const T& iA,
const T& iB
) {
return ( ( iA < iB ) ? iA : iB );
}
inline int clamp (
const float& f,
const int& inf,
const int& sup
) {
int v = static_cast<int> (f);
return (v < inf ? inf : (v > sup ? sup : v));
}
inline float fclamp (
const float& iVal,
const float& iInf,
const float& iSup
) {
return ( iVal < iInf ) ? iInf : ( iVal > iSup ) ? iSup : iVal;
}
template <typename T>
inline T tclamp (
const T& iVal,
const T& iInf,
const T& iSup
) {
return ( iVal < iInf ) ? iInf : ( iVal > iSup ) ? iSup : iVal;
}
//! Generates a random vector using a cosine weighted distribution
/*! Objective: creates a disk (polar coordinate)
and project it onto a hemisphere of unit size
\param R a random radius value
\param iTheta a random iTheta value
*/
inline Vec3Df CosineWeightedDistribution (
const float& iR,
const float& iTheta
) {
/* Projecting my disk onto a unit hemisphere
_
( )
( /) | Z
(__/__) |
R
1 = R*R + Z*Z
Z = sqrt(1 - R*R)
*/
/* Converting between polar and Cartesian coordinates*/
const float z = sqrt(1.0f - iR * iR);
const float x = iR * cos ( iTheta );
const float y = iR * sin ( iTheta );
return Vec3Df (
x,
y,
z
);
}
#endif // _MATHUTILS_H_