1
2
3
4
5
6
7#ifndef pvec_h
8#define pvec_h
9
10#include <cmath>
11#include <iostream>
12
13#ifndef M_PI
14#define M_PI 3.1415926535897932384626433f
15#endif
16
17#ifdef WIN32
18#define PINLINE __forceinline
19#else
21#endif
22
24const float P_SQRT2PI = 2.506628274631000502415765284811045253006f;
26
28
29#ifdef unix
32#else
36#endif
37
39
40
42{
43 float x, y, r2;
44 do {
47 r2 = x * x + y * y;
48 } while (r2 > 1.0f || r2 == 0.0f);
49
50 float m = sqrtf(-2.0f * logf(r2) / r2);
51
52 float px = x * m * sigma;
53
54
55 return px;
56}
57
58
59
60
61
62
64 float vx, vy, vz;
65
66public:
67 PINLINE pVec(
float ax,
float ay,
float az) : vx(ax), vy(ay), vz(az) {}
71
72 const float&
x()
const {
return vx; }
73 const float&
y()
const {
return vy; }
74 const float&
z()
const {
return vz; }
75
76 float&
x() {
return vx; }
77 float&
y() {
return vy; }
78 float&
z() {
return vz; }
79
80 PINLINE float length()
const {
return sqrtf(vx * vx + vy * vy + vz * vz); }
81
83
85 {
86 float onel = toLen / sqrtf(vx * vx + vy * vy + vz * vz);
87 vx *= onel;
88 vy *= onel;
89 vz *= onel;
90
91 return onel;
92 }
93
94
96
97
99
101 {
102 float invs = 1.0f / s;
103 return pVec(vx * invs
, vy * invs
, vz * invs
);
104 }
105
107
109
111
113 {
114 vx = -vx;
115 vy = -vy;
116 vz = -vz;
117 return *this;
118 }
119
121 {
125 return *this;
126 }
127
129 {
133 return *this;
134 }
135
137 {
138 vx *= a;
139 vy *= a;
140 vz *= a;
141 return *this;
142 }
143
145 {
146 float b = 1.0f / a;
147 vx *= b;
148 vy *= b;
149 vz *= b;
150 return *this;
151 }
152
154
155
157
158
160
162 {
164 }
165
166 friend PINLINE std::ostream& operator<<(std::ostream& os,
const pVec& v)
167 {
168 os << &v <<
'[' << v
.x() <<
", " << v
.y() <<
", " << v
.z() <<
']';
169
170 return os;
171 }
172};
173
174
176
178
180{
181 float x, y, r2;
182 do {
185 r2 = x * x + y * y;
186 } while (r2 > 1.0f || r2 == 0.0f);
187
188 float m = sqrtf(-2.0f * logf(r2) / r2);
189
190 float px = x * m * sigma;
191 float py = y * m * sigma;
193}
194};
195
196#endif
A single-precision floating point three-vector.
Definition: pVec.h:63
friend pVec Abs(const pVec &a)
Definition: pVec.h:156
friend pVec CompMult(const pVec &a, const pVec &b)
Definition: pVec.h:159
float normalize(const float toLen=1.f)
Definition: pVec.h:84
pVec(float ax, float ay, float az)
Definition: pVec.h:67
pVec operator+(const pVec &a) const
Definition: pVec.h:106
pVec operator*(const float s) const
Definition: pVec.h:98
bool isNan()
Definition: pVec.h:153
friend float dot(const pVec &a, const pVec &b)
Definition: pVec.h:95
const float & x() const
Definition: pVec.h:72
float & y()
Definition: pVec.h:77
pVec operator-(const pVec &a) const
Definition: pVec.h:108
pVec & operator/=(const float a)
Definition: pVec.h:144
bool operator==(const pVec &a) const
Definition: pVec.h:110
pVec & operator*=(const float a)
Definition: pVec.h:136
pVec operator-()
Definition: pVec.h:112
pVec & operator-=(const pVec &a)
Definition: pVec.h:128
float & x()
Definition: pVec.h:76
float length() const
Definition: pVec.h:80
const float & y() const
Definition: pVec.h:73
float lenSqr() const
Definition: pVec.h:82
pVec & operator+=(const pVec &a)
Definition: pVec.h:120
pVec operator/(const float s) const
Definition: pVec.h:100
pVec(float a)
Definition: pVec.h:68
const float & z() const
Definition: pVec.h:74
pVec(float *v)
Definition: pVec.h:70
float & z()
Definition: pVec.h:78
friend pVec Cross(const pVec &a, const pVec &b)
Definition: pVec.h:161
PAPIClasses.h.
Definition: pAPIContext.h:16
float pRandf()
Definition: pVec.h:34
pVec pNRandVec(float sigma)
Definition: pVec.h:179
bool pSameSign(const float &a, const float &b)
Definition: pVec.h:38
const float P_ONEOVER_RAND_MAX
Definition: pVec.h:33
const float P_ONEOVERSQRT2PI
Definition: pVec.h:25
const float P_SQRT2PI
Definition: pVec.h:24
void pSRandf(int x)
Definition: pVec.h:35
float fsqr(float f)
Definition: pVec.h:27
pVec pRandVec()
Definition: pVec.h:177
float pNRandf(float sigma=1.0f)
Return a random number with a normal distribution.
Definition: pVec.h:41
#define PINLINE
Definition: pVec.h:20