Skip to content

Commit 59cf6ba

Browse files
Add twoPi and halfPi constants
1 parent fd72a70 commit 59cf6ba

8 files changed

Lines changed: 60 additions & 54 deletions

File tree

src/ammonite/camera/camera.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ namespace ammonite {
6060

6161
//Right vector, relative to the camera
6262
const ammonite::Vec<float, 3> right = {
63-
std::sin((float)activeCamera->horizontalAngle - (ammonite::pi<float> / 2.0f)),
63+
std::sin((float)activeCamera->horizontalAngle - ammonite::halfPi<float>),
6464
0.0f,
65-
std::cos((float)activeCamera->horizontalAngle - (ammonite::pi<float> / 2.0f))
65+
std::cos((float)activeCamera->horizontalAngle - ammonite::halfPi<float>)
6666
};
6767

6868
//Up vector, relative to the camera

src/ammonite/input/controls.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ namespace ammonite {
3737
} multipliers;
3838

3939
//Default to a 120 degree field of view limit
40-
float fovLimit = (2.0f * ammonite::pi<float>) / 3.0f;
40+
float fovLimit = ammonite::twoPi<float> / 3.0f;
4141

4242
//Final sensitivities
4343
float movementSpeed = baseSettings.movementSpeed;
@@ -159,7 +159,7 @@ namespace ammonite {
159159
};
160160

161161
//Right vector, relative to the camera
162-
const float angleRight = horizontalAngle - (ammonite::pi<float> / 2.0f);
162+
const float angleRight = horizontalAngle - ammonite::halfPi<float>;
163163
const ammonite::Vec<float, 3> right = {
164164
std::sin(angleRight), 0.0f, std::cos(angleRight)
165165
};

src/demoLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,7 @@ int main(int argc, char** argv) noexcept(false) {
334334

335335
//Calculate the node's coordinates
336336
const double progress = (double)(i) / (double)(nodeCount - 1);
337-
const float angle = (float)progress * 2.0f * ammonite::pi<float>;
337+
const float angle = (float)progress * ammonite::twoPi<float>;
338338
position[0] = pathRadius * std::sin(angle);
339339
position[1] = pathHeight;
340340
position[2] = pathRadius * std::cos(angle);

src/demos/object-field.cpp

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ namespace objectFieldDemo {
6363

6464
//Rotation
6565
ammonite::set(objectData[(i * 3) + 1],
66-
(float)ammonite::utils::randomDouble(0.0, ammonite::pi<float> * 2.0f),
67-
(float)ammonite::utils::randomDouble(0.0, ammonite::pi<float> * 2.0f),
68-
(float)ammonite::utils::randomDouble(0.0, ammonite::pi<float> * 2.0f));
66+
(float)ammonite::utils::randomDouble(0.0, ammonite::twoPi<float>),
67+
(float)ammonite::utils::randomDouble(0.0, ammonite::twoPi<float>),
68+
(float)ammonite::utils::randomDouble(0.0, ammonite::twoPi<float>));
6969

7070
//Scale
7171
ammonite::set(objectData[(i * 3) + 2], (float)ammonite::utils::randomDouble(0.0, 1.2));
@@ -126,10 +126,10 @@ namespace objectFieldDemo {
126126
//Return the position of an orbit's centre, so that each orbit in a shape meets the next
127127
constexpr void calculateOrbitPosition(unsigned int orbitCount, unsigned int orbitIndex,
128128
float radius, ammonite::Vec<float, 2>& dest) {
129-
const float nucleusAngle = (ammonite::pi<float> * 2.0f * (float)orbitIndex) / (float)orbitCount;
129+
const float nucleusAngle = (ammonite::twoPi<float> * (float)orbitIndex) / (float)orbitCount;
130130

131131
//Correct for overlapping orbits
132-
const float indexOffsetAngle = (ammonite::pi<float> / 2.0f) - (ammonite::pi<float> / (float)orbitCount);
132+
const float indexOffsetAngle = ammonite::halfPi<float> - (ammonite::pi<float> / (float)orbitCount);
133133
const float opp = ammonite::pi<float> - (2.0f * indexOffsetAngle);
134134
const float nucleusDistance = radius * 2.0f * std::sin(indexOffsetAngle) / std::sin(opp);
135135

@@ -145,8 +145,8 @@ namespace objectFieldDemo {
145145
- The second index has the angle to the next index
146146
*/
147147
float* calculateSwapAngles(unsigned int orbitCount) {
148-
const float down = ammonite::pi<float> / 2.0f;
149-
const float indexOffsetAngle = (ammonite::pi<float> / 2.0f) - (ammonite::pi<float> / (float)orbitCount);
148+
const float down = ammonite::halfPi<float>;
149+
const float indexOffsetAngle = ammonite::halfPi<float> - (ammonite::pi<float> / (float)orbitCount);
150150

151151
float* const swapAngles = new float[(std::size_t)(orbitCount) * 2];
152152
for (unsigned int orbit = 0; orbit < orbitCount; orbit++) {
@@ -158,11 +158,11 @@ namespace objectFieldDemo {
158158
swapAngles[writeIndex] = down - (indexOffsetAngle * (float)sign);
159159

160160
//Rotate the angle to match index position
161-
swapAngles[writeIndex] += ((float)orbit / (float)orbitCount) * ammonite::pi<float> * 2.0f;
162-
if (swapAngles[writeIndex] >= ammonite::pi<float> * 2.0f) {
163-
swapAngles[writeIndex] -= ammonite::pi<float> * 2.0f;
161+
swapAngles[writeIndex] += ((float)orbit / (float)orbitCount) * ammonite::twoPi<float>;
162+
if (swapAngles[writeIndex] >= ammonite::twoPi<float>) {
163+
swapAngles[writeIndex] -= ammonite::twoPi<float>;
164164
} else if (swapAngles[writeIndex] <= 0.0f) {
165-
swapAngles[writeIndex] += ammonite::pi<float> * 2.0f;
165+
swapAngles[writeIndex] += ammonite::twoPi<float>;
166166
}
167167

168168
writeIndex++;
@@ -367,7 +367,7 @@ namespace objectFieldDemo {
367367
}
368368

369369
//Decide where the light source's angle, in radians
370-
const float targetAngle = (orbitTime / lightData[i].orbitPeriod) * ammonite::pi<float> * 2.0f;
370+
const float targetAngle = (orbitTime / lightData[i].orbitPeriod) * ammonite::twoPi<float>;
371371

372372
//Decide if the light is within the region to swap orbits
373373
unsigned int swapTarget = 0;
@@ -394,10 +394,10 @@ namespace objectFieldDemo {
394394
//Set timer for new angle
395395
float newAngle = orbitSwapAngles[swapTarget][1 - swapDirection];
396396
if (!lightData[i].isOrbitClockwise) {
397-
newAngle = (ammonite::pi<float> * 2.0f) - newAngle;
397+
newAngle = ammonite::twoPi<float> - newAngle;
398398
}
399-
lightData[i].orbitTimer.setTime((newAngle / (ammonite::pi<float> * 2.0f)) * \
400-
lightData[i].orbitPeriod);
399+
lightData[i].orbitTimer.setTime((newAngle / ammonite::twoPi<float>) * \
400+
lightData[i].orbitPeriod);
401401

402402
//Set new orbit and flip direction
403403
lightData[i].orbitIndex = swapTarget;

src/helper/torusGenerator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ namespace torus {
6060

6161
//Generate the mesh vertices and normals
6262
for (unsigned int widthNode = 0; widthNode < widthNodes; widthNode++) {
63-
const float ringRadians = ((float)widthNode / (float)widthNodes) * 2.0f * ammonite::pi<float>;
63+
const float ringRadians = ((float)widthNode / (float)widthNodes) * ammonite::twoPi<float>;
6464

6565
//Calculate the origin for the current ring
6666
ammonite::Vec<float, 3> ringOrigin = {
@@ -70,7 +70,7 @@ namespace torus {
7070

7171
//Calculate the vertices and normals of the current ring
7272
for (unsigned int heightNode = 0; heightNode < heightNodes; heightNode++) {
73-
const float volumeRadians = ((float)heightNode / (float)heightNodes) * 2.0f * ammonite::pi<float>;
73+
const float volumeRadians = ((float)heightNode / (float)heightNodes) * ammonite::twoPi<float>;
7474
const float localRadius = std::sin(volumeRadians) * volumeDiameter;
7575

7676
//Calculate the vertex position

src/include/ammonite/maths/angle.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ namespace AMMONITE_INTERNAL ammonite {
1111
template <typename T> requires std::is_floating_point_v<T>
1212
constexpr T pi = std::numbers::pi_v<T>;
1313

14+
template <typename T> requires std::is_floating_point_v<T>
15+
constexpr T twoPi = (std::numbers::pi_v<T> * (T)2.0);
16+
17+
template <typename T> requires std::is_floating_point_v<T>
18+
constexpr T halfPi = (std::numbers::pi_v<T> / (T)2.0);
19+
1420
template <typename T> requires std::is_floating_point_v<T>
1521
constexpr T radians(T angle) {
1622
return (angle / (T)180.0) * ammonite::pi<T>;
@@ -25,9 +31,9 @@ namespace AMMONITE_INTERNAL ammonite {
2531
constexpr T smallestAngleDelta(T angleA, T angleB) {
2632
T angleDelta = angleA - angleB;
2733
if (angleDelta < -ammonite::pi<T>) {
28-
angleDelta += ammonite::pi<T> * (T)2.0;
34+
angleDelta += ammonite::twoPi<T>;
2935
} else if (angleDelta > ammonite::pi<T>) {
30-
angleDelta -= ammonite::pi<T> * (T)2.0;
36+
angleDelta -= ammonite::twoPi<T>;
3137
}
3238

3339
return angleDelta;

src/tests/matrix/matrixTestTemplates.hpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -748,23 +748,23 @@ namespace {
748748

749749
const TestData tests[11] = {
750750
//Convert between axes
751-
{y, x, z, -ammonite::pi<T> / 2},
752-
{z, y, x, -ammonite::pi<T> / 2},
753-
{x, z, y, -ammonite::pi<T> / 2},
751+
{y, x, z, -ammonite::halfPi<T>},
752+
{z, y, x, -ammonite::halfPi<T>},
753+
{x, z, y, -ammonite::halfPi<T>},
754754

755755
//Reflect axes
756756
{xz, x, z, ammonite::pi<T>},
757757
{xz, z, x, ammonite::pi<T>},
758758
{xz, y, negY, ammonite::pi<T>},
759759

760760
//Roll axes
761-
{xyz, y, z, (ammonite::pi<T> * 2) / 3},
762-
{xyz, z, x, (ammonite::pi<T> * 2) / 3},
763-
{xyz, x, y, (ammonite::pi<T> * 2) / 3},
761+
{xyz, y, z, ammonite::twoPi<T> / (T)3.0},
762+
{xyz, z, x, ammonite::twoPi<T> / (T)3.0},
763+
{xyz, x, y, ammonite::twoPi<T> / (T)3.0},
764764

765765
//Recover x and z from xz
766-
{y, xz, x, ammonite::pi<T> / 4},
767-
{y, xz, z, -ammonite::pi<T> / 4}
766+
{y, xz, x, ammonite::pi<T> / (T)4.0},
767+
{y, xz, z, -ammonite::pi<T> / (T)4.0}
768768
};
769769

770770
const int totalTests = sizeof(tests) / sizeof(TestData);

src/tests/quaternion/quaternionTestTemplates.hpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,10 @@ namespace {
9292
{(T)0.0, (T)0.0, (T)0.0, {{(T)0.0, (T)0.0, (T)0.0, (T)1.0}}},
9393

9494
//Complete rotation in each axis
95-
{(T)2.0 * ammonite::pi<T>, (T)0.0, (T)0.0, {{(T)0.0, (T)0.0, (T)0.0, (T)-1.0}}},
96-
{(T)0.0, (T)2.0 * ammonite::pi<T>, (T)0.0, {{(T)0.0, (T)0.0, (T)0.0, (T)-1.0}}},
97-
{(T)0.0, (T)0.0, (T)2.0 * ammonite::pi<T>, {{(T)0.0, (T)0.0, (T)0.0, (T)-1.0}}},
98-
{(T)2.0 * ammonite::pi<T>, (T)2.0 * ammonite::pi<T>, (T)2.0 * ammonite::pi<T>, {{(T)0.0, (T)0.0, (T)0.0, (T)-1.0}}},
95+
{ammonite::twoPi<T>, (T)0.0, (T)0.0, {{(T)0.0, (T)0.0, (T)0.0, (T)-1.0}}},
96+
{(T)0.0, ammonite::twoPi<T>, (T)0.0, {{(T)0.0, (T)0.0, (T)0.0, (T)-1.0}}},
97+
{(T)0.0, (T)0.0, ammonite::twoPi<T>, {{(T)0.0, (T)0.0, (T)0.0, (T)-1.0}}},
98+
{ammonite::twoPi<T>, ammonite::twoPi<T>, ammonite::twoPi<T>, {{(T)0.0, (T)0.0, (T)0.0, (T)-1.0}}},
9999

100100
//Half rotation in each axis
101101
{ammonite::pi<T>, (T)0.0, (T)0.0, {{(T)1.0, (T)0.0, (T)0.0, (T)0.0}}},
@@ -152,7 +152,7 @@ namespace {
152152
ammonite::Vec<T, 3> angleVec = {0};
153153

154154
//Initialise a random angle vector and corresponding quaternion
155-
ammonite::fromEuler(aQuat, randomFillVector(angleVec, (T)2.0 * ammonite::pi<T>));
155+
ammonite::fromEuler(aQuat, randomFillVector(angleVec, ammonite::twoPi<T>));
156156

157157
//Get angles out of the quaternion
158158
ammonite::toEuler(aQuat, aVec);
@@ -185,8 +185,8 @@ namespace {
185185
ammonite::Vec<T, 3> angleVecB = {0};
186186

187187
//Initialise two pairs of random angle vectors and corresponding quaternions
188-
ammonite::fromEuler(aQuat, randomFillVector(angleVecA, (T)2.0 * ammonite::pi<T>));
189-
ammonite::fromEuler(bQuat, randomFillVector(angleVecB, (T)2.0 * ammonite::pi<T>));
188+
ammonite::fromEuler(aQuat, randomFillVector(angleVecA, ammonite::twoPi<T>));
189+
ammonite::fromEuler(bQuat, randomFillVector(angleVecB, ammonite::twoPi<T>));
190190

191191
T sum = (T)0;
192192
for (int i = 0; i < 4; i++) {
@@ -212,7 +212,7 @@ namespace {
212212
ammonite::Quat<T> bQuat = {{0}};
213213
ammonite::Quat<T> cQuat = {{0}};
214214
ammonite::Vec<T, 3> angleVec = {0};
215-
ammonite::fromEuler(aQuat, randomFillVector(angleVec, (T)2.0 * ammonite::pi<T>));
215+
ammonite::fromEuler(aQuat, randomFillVector(angleVec, ammonite::twoPi<T>));
216216

217217
//Test conjugate calculation
218218
ammonite::conjugate(aQuat, bQuat);
@@ -253,7 +253,7 @@ namespace {
253253
bool testLength() {
254254
ammonite::Quat<T> aQuat = {{0}};
255255
ammonite::Vec<T, 3> angleVec = {0};
256-
ammonite::fromEuler(aQuat, randomFillVector(angleVec, (T)2.0 * ammonite::pi<T>));
256+
ammonite::fromEuler(aQuat, randomFillVector(angleVec, ammonite::twoPi<T>));
257257

258258
T sum = (T)0;
259259
for (int i = 0; i < 4; i++) {
@@ -279,7 +279,7 @@ namespace {
279279
ammonite::Quat<T> aQuat = {{0}};
280280
ammonite::Quat<T> bQuat = {{0}};
281281
ammonite::Vec<T, 3> angleVec = {0};
282-
ammonite::fromEuler(aQuat, randomFillVector(angleVec, (T)2.0 * ammonite::pi<T>));
282+
ammonite::fromEuler(aQuat, randomFillVector(angleVec, ammonite::twoPi<T>));
283283

284284
//Skip (effectively) zero length quaternions
285285
T length = ammonite::length(aQuat);
@@ -326,8 +326,8 @@ namespace {
326326
ammonite::Vec<T, 3> angleVecA = {0};
327327
ammonite::Vec<T, 3> angleVecB = {0};
328328

329-
ammonite::fromEuler(aQuat, randomFillVector(angleVecA, (T)2.0 * ammonite::pi<T>));
330-
ammonite::fromEuler(bQuat, randomFillVector(angleVecB, (T)2.0 * ammonite::pi<T>));
329+
ammonite::fromEuler(aQuat, randomFillVector(angleVecA, ammonite::twoPi<T>));
330+
ammonite::fromEuler(bQuat, randomFillVector(angleVecB, ammonite::twoPi<T>));
331331

332332
ammonite::inverse(aQuat, cQuat);
333333

@@ -370,7 +370,7 @@ namespace {
370370
ammonite::Quat<T> eQuat = {{0}};
371371
ammonite::Quat<T> fQuat = {{0}};
372372
ammonite::Vec<T, 3> angleVec = {0};
373-
randomFillVector(angleVec, (T)2.0 * ammonite::pi<T>);
373+
randomFillVector(angleVec, ammonite::twoPi<T>);
374374

375375
//Initialise quaternions using the angles
376376
ammonite::fromEuler(aQuat, angleVec[0], (T)0.0, (T)0.0);
@@ -417,39 +417,39 @@ namespace {
417417

418418
TestData tests[8] = {
419419
{ //Convert between axes
420-
{-ammonite::pi<T> / 2, (T)0.0, (T)0.0},
420+
{-ammonite::halfPi<T>, (T)0.0, (T)0.0},
421421
{(T)0.0, (T)0.0, (T)1.0},
422422
{(T)0.0, (T)1.0, (T)0.0}
423423
}, {
424-
{(T)0.0, -ammonite::pi<T> / 2, (T)0.0},
424+
{(T)0.0, -ammonite::halfPi<T>, (T)0.0},
425425
{(T)1.0, (T)0.0, (T)0.0},
426426
{(T)0.0, (T)0.0, (T)1.0}
427427
}, {
428-
{(T)0.0, (T)0.0, -ammonite::pi<T> / 2},
428+
{(T)0.0, (T)0.0, -ammonite::halfPi<T>},
429429
{(T)0.0, (T)1.0, (T)0.0},
430430
{(T)1.0, (T)0.0, (T)0.0}
431431
},
432432

433433
{ //Complete turns
434-
{ammonite::pi<T> * 2, ammonite::pi<T> * 2, ammonite::pi<T> * 2},
434+
{ammonite::twoPi<T>, ammonite::twoPi<T>, ammonite::twoPi<T>},
435435
{(T)1.0, (T)0.0, (T)0.0},
436436
{(T)1.0, (T)0.0, (T)0.0}
437437
}, {
438-
{ammonite::pi<T> * 2, ammonite::pi<T> * 2, ammonite::pi<T> * 2},
438+
{ammonite::twoPi<T>, ammonite::twoPi<T>, ammonite::twoPi<T>},
439439
{(T)0.0, (T)1.0, (T)0.0},
440440
{(T)0.0, (T)1.0, (T)0.0}
441441
}, {
442-
{ammonite::pi<T> * 2, ammonite::pi<T> * 2, ammonite::pi<T> * 2},
442+
{ammonite::twoPi<T>, ammonite::twoPi<T>, ammonite::twoPi<T>},
443443
{(T)0.0, (T)0.0, (T)1.0},
444444
{(T)0.0, (T)0.0, (T)1.0}
445445
},
446446

447447
{ //Recover x and z from xz
448-
{(T)0.0, ammonite::pi<T> / 4, (T)0.0},
448+
{(T)0.0, ammonite::pi<T> / (T)4.0, (T)0.0},
449449
{(T)1.0, (T)0.0, (T)1.0},
450450
{(T)1.0, (T)0.0, (T)0.0}
451451
}, {
452-
{(T)0.0, -ammonite::pi<T> / 4, (T)0.0},
452+
{(T)0.0, -ammonite::pi<T> / (T)4.0, (T)0.0},
453453
{(T)1.0, (T)0.0, (T)1.0},
454454
{(T)0.0, (T)0.0, (T)1.0}
455455
}
@@ -539,7 +539,7 @@ namespace {
539539
ammonite::Vec<T, 4> cVec = {0};
540540
ammonite::Vec<T, 3> angleVec = {0};
541541
randomFillVector(aVec);
542-
ammonite::fromEuler(aQuat, randomFillVector(angleVec, (T)2.0 * ammonite::pi<T>));
542+
ammonite::fromEuler(aQuat, randomFillVector(angleVec, ammonite::twoPi<T>));
543543

544544
//Apply the quaternion to a point
545545
ammonite::multiply(aQuat, aVec, bVec);

0 commit comments

Comments
 (0)