Skip to content

Commit 9c8e24b

Browse files
committed
Add math package and move some files from physics into it. Refactor dependencies.
1 parent 9df7efe commit 9c8e24b

File tree

24 files changed

+129
-61
lines changed

24 files changed

+129
-61
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ else()
2323
testing
2424
meta
2525
memory
26+
math
2627
hid
2728
audio
2829
nodegraph

packages/audio/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ set(deps
44
logging
55
testing
66
memory
7+
math
78
)
89

910
bs_generate_package(audio "tier1" "${deps}" "PortAudio")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
#pragma once
22

3+
#include <functional>
4+
35
namespace l::audio {
46
float GetFrequencyFromNote(float note);
7+
float BatchUpdate(float updateSamples, float samplesLeft, int32_t start, int32_t end, std::function<void(int32_t, int32_t, bool)> process, std::function<void(int32_t)> update);
58
}

packages/audio/source/common/AudioUtils.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "audio/AudioUtils.h"
22

33
#include "logging/LoggingAll.h"
4+
#include "math/MathAll.h"
45

56
#include <math.h>
67

@@ -9,4 +10,32 @@ namespace l::audio {
910
float GetFrequencyFromNote(float note) {
1011
return 440.0f * powf(2.0f, (note - 49.0f) / 12.0f);
1112
}
13+
14+
float BatchUpdate(float updateSamples, float samplesLeft, int32_t start, int32_t end, std::function<void(int32_t, int32_t, bool)> process, std::function<void(float)> update) {
15+
float startNum = static_cast<float>(start);
16+
while (startNum < static_cast<float>(end)) {
17+
bool updated = false;
18+
if (samplesLeft < 1.0f) {
19+
samplesLeft += updateSamples;
20+
if (update != nullptr) {
21+
update(startNum);
22+
}
23+
updated = true;
24+
}
25+
26+
// Update stuff
27+
// process samples until
28+
// * batch max has been reached
29+
// * end has been reached
30+
31+
float samples = l::math::functions::min(static_cast<float>(end) - startNum, samplesLeft);
32+
if (process != nullptr) {
33+
process(static_cast<int32_t>(startNum), static_cast<int32_t>(startNum + samples), updated);
34+
}
35+
startNum += samples;
36+
samplesLeft -= samples;
37+
}
38+
return samplesLeft;
39+
}
40+
1241
}

packages/math/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
project (math)
2+
3+
set(deps
4+
logging
5+
testing
6+
memory
7+
)
8+
9+
bs_generate_package(math "tier1" "${deps}" "")

packages/physics/include/physics/Algorithm.h renamed to packages/math/include/math/MathAlgorithm.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,23 @@
1212
#include "logging/LoggingAll.h"
1313
#include <math.h>
1414

15-
#include "physics/MathFunc.h"
15+
#include "math/MathFunc.h"
1616

17-
namespace l {
18-
namespace algorithm {
17+
namespace l::math::algorithm {
1918

2019
uint64_t pairIndex32(uint32_t i, uint32_t j);
2120
uint32_t pairIndex16(uint16_t i, uint16_t j);
2221
uint32_t encodeFloat(const float newPos);
2322
float decodeFloat(uint32_t ir);
2423

24+
template<class T, class U>
25+
T convert(U data) {
26+
const char* srcPtr = reinterpret_cast<const char*>(&data);
27+
T dst;
28+
memcpy(&dst, srcPtr, sizeof(T));
29+
return dst;
30+
}
31+
2532
template <class T>
2633
bool samesign(T a, T b) {
2734
return a * b >= 0.0;
@@ -151,4 +158,3 @@ namespace algorithm {
151158

152159
}
153160
}
154-
}

packages/math/include/math/MathAll.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#pragma once
2+
3+
#include "math/MathFunc.h"
4+
#include "math/MathSmooth.h"
5+
#include "math/MathConstants.h"
6+
#include "math/MathAlgorithm.h"

packages/physics/include/physics/Constants.h renamed to packages/math/include/math/MathConstants.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,27 @@
22

33
#include <cmath>
44

5-
namespace l {
6-
namespace constants {
5+
namespace l::math::constants {
76
// Newtonian constant of gravitation 6.67430(15)x10-11 m3 kg-1 s-2
87
const double G = 6.674 * std::pow(10.0, -11.0);
98

10-
// speed of light in vacuum 299792458 m s-1
9+
// Speed of light in vacuum 299792458 m s-1
1110
const double c = 299792458;
1211

13-
// Planck constant 6.62607015x10-34 J Hz-1
12+
// Planck's constant 6.62607015x10-34 J Hz-1
1413
const double h = 6.62607015 * std::pow(10.0, -34.0);
1514

16-
const double pi = 3.14159265359;
17-
const float pi_f = 3.14159265359f;
15+
// PI
16+
const double PI = 3.141592653589793;
17+
const float PI_f = 3.141592653589793f;
18+
19+
// euler's number e
20+
const double E = 2.718281828459045;
21+
const float E_f = 2.718281828459045f;
22+
23+
// phi
24+
const double PHI = 1.618033988749895;
25+
const double PHI_f = 1.618033988749895f;
1826

1927
// Minkowski space time distance https://www.youtube.com/watch?v=fvqXshyuvOg
2028
// Basically objects are 5 dimensional and the distance between objects are
@@ -27,4 +35,3 @@ namespace constants {
2735
}
2836

2937
}
30-
}

packages/physics/include/physics/MathFunc.h renamed to packages/math/include/math/MathFunc.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,24 @@
77
#include <string>
88
#include <algorithm>
99

10-
#include "physics/Constants.h"
10+
#include "math/MathConstants.h"
1111

12-
namespace l {
13-
namespace algorithm {
12+
namespace l::math::functions {
13+
14+
template<class T>
15+
T min(T val1, T val2) {
16+
return val1 < val2 ? val1 : val2;
17+
}
18+
19+
template<class T>
20+
T max(T val1, T val2) {
21+
return val1 > val2 ? val1 : val2;
22+
}
23+
24+
template<class T>
25+
T clamp(T val, T min, T max) {
26+
return val < min ? min : val > max ? max : val;
27+
}
1428

1529
template<class T>
1630
T abs(T val) {
@@ -87,14 +101,6 @@ namespace algorithm {
87101
return static_cast<T>(0);
88102
}
89103

90-
template<class T, class U>
91-
T convert(U data) {
92-
const char* srcPtr = reinterpret_cast<const char*>(&data);
93-
T dst;
94-
memcpy(&dst, srcPtr, sizeof(T));
95-
return dst;
96-
}
97-
98104
// Almost Identity(I)
99105
// Imagine you don't want to modify a signal unless it's drops to zero or close to it, in which case you want to replace the value with a small possitive constant.Then, rather than clamping the valueand introduce a discontinuity, you can smoothly blend the signal into the desired clipped value.So, let m be the threshold(anything above m stays unchanged), and n the value things will take when the signal is zero.Then, the following function does the soft clipping(in a cubic fashion):
100106
template<class T>
@@ -217,9 +223,8 @@ namespace algorithm {
217223
template<class T>
218224
T sinc(T x, T k)
219225
{
220-
const T a = l::constants::pi * (k * x - 1.0);
226+
const T a = math::constants::PI * (k * x - 1.0);
221227
return sin(a) / a;
222228
}
223229

224230
}
225-
}

packages/physics/include/physics/MathSmooth.h renamed to packages/math/include/math/MathSmooth.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
#include <optional>
77
#include <string>
88

9-
namespace l {
10-
namespace algorithm {
9+
namespace l::math::smooth {
1110

1211
// Quintic Polynomial - C2 continuity
1312
template <class V>
@@ -50,4 +49,3 @@ namespace algorithm {
5049
}
5150

5251
}
53-
}

packages/physics/source/common/Algorithm.cpp renamed to packages/math/source/common/MathAlgorithm.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11

2-
#include "physics/Algorithm.h"
2+
#include "math/MathAlgorithm.h"
3+
#include "math/MathFunc.h"
34

4-
namespace l {
5-
namespace algorithm {
5+
namespace l::math::algorithm {
66

77
uint64_t pairIndex32(uint32_t i, uint32_t j) {
88
/// Make sure index is order invariant
@@ -46,4 +46,3 @@ namespace algorithm {
4646
return convert<float>(rv);
4747
}
4848
}
49-
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "testing/Test.h"
2+
3+
int main(int, char* argw[]) {
4+
TEST_RUN(argw[0]);
5+
6+
return 0;
7+
}

packages/physics/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ set(deps
55

66
logging
77
testing
8+
math
89
tools
910
)
1011

packages/physics/include/physics/CurveBezier.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
#include <cmath>
44
#include "VecX.h"
5-
#include "MathSmooth.h"
6-
75

86
namespace l {
97
namespace curves {

packages/physics/include/physics/GridMap.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
#include "logging/LoggingAll.h"
1313
#include "physics/VecX.h"
14-
#include "physics/Constants.h"
14+
15+
#include "math/MathConstants.h"
1516

1617
namespace l::physics {
1718

1819
template<class T>
1920
auto DistanceFromDegrees(T degree) {
20-
auto x = cos((degree / 360.0) * 2.0 * constants::pi);
21-
auto y = sin((degree / 360.0) * 2.0 * constants::pi);
21+
auto x = cos((degree / 360.0) * 2.0 * math::constants::PI);
22+
auto y = sin((degree / 360.0) * 2.0 * math::constants::PI);
2223

2324
return static_cast<T>(sqrt((1.0 - x) * (1.0 - x) + y * y));
2425
}

packages/physics/include/physics/Integration.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#pragma once
22

33
#include "logging/Log.h"
4-
#include "Constants.h"
5-
#include "Algorithm.h"
4+
5+
#include "math/MathAlgorithm.h"
66

77
namespace l {
88
namespace physics {
@@ -167,10 +167,10 @@ namespace physics {
167167
V freq = 2.25;
168168

169169
V gravity = - 1.0 / (1 + pow(r * freq, power));
170-
V modulation = (cos(l::constants::pi * (1 + 2 * r * 0.1 * freq)) * 0.25 + 0.75);
170+
V modulation = (cos(l::math::constants::PI * (1 + 2 * r * 0.1 * freq)) * 0.25 + 0.75);
171171
V cutoff = (1.0 - exp(-r));
172172

173-
//V mod = cos(l::constants::pi * (1.0 + 2 * distance * freq)) * 0.5 + 0.5;
173+
//V mod = cos(l::constants::PI * (1.0 + 2 * distance * freq)) * 0.5 + 0.5;
174174
//return lambda * mod;
175175

176176
return gravity * modulation * cutoff;
@@ -180,7 +180,7 @@ namespace physics {
180180
V computeForceLambda2(V r, V forceConstant, V power) {
181181
V freq = 1.0;
182182
V gravity = -power * forceConstant / (pow(1 + r, power));
183-
V modulation = (cos(l::constants::pi * (1 + 2 * r * freq * power)) * 0.25 + 0.75);
183+
V modulation = (cos(l::math::constants::PI * (1 + 2 * r * freq * power)) * 0.25 + 0.75);
184184
V cutoff = (1.0 - exp(-r));
185185

186186
return gravity * modulation * cutoff;
@@ -231,12 +231,12 @@ namespace physics {
231231
V f = abs(computeForceLambda(r, rDesired, forceConstant, type));
232232

233233
if (f >= 0.00001) {
234-
auto x1 = l::algorithm::solve_quad_eq(m1 * 2.0 * v1 / f, -2.0 * m1 * s / f);
234+
auto x1 = l::math::algorithm::solve_quad_eq(m1 * 2.0 * v1 / f, -2.0 * m1 * s / f);
235235
if (x1) {
236236
auto dt = x1->first > 0.0 ? x1->first : x1->second;
237237
v1 += f * dt / m1;
238238
}
239-
auto x2 = l::algorithm::solve_quad_eq(m2 * 2.0 * v2 / f, -2.0 * m2 * s / f);
239+
auto x2 = l::math::algorithm::solve_quad_eq(m2 * 2.0 * v2 / f, -2.0 * m2 * s / f);
240240
if (x2) {
241241
auto dt = x2->first > 0.0 ? x2->first : x2->second;
242242
v2 += f * dt / m2;

packages/physics/include/physics/LinkedList.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <vector>
77
#include <unordered_map>
88

9-
#include "Algorithm.h"
9+
#include "math/MathAlgorithm.h"
1010

1111
namespace l::physics {
1212

@@ -96,7 +96,7 @@ class LinkedElements {
9696
LinkedElement<T> e;
9797
e.mData = id;
9898

99-
uint32_t index = algorithm::binary_search(mElements, e, fromIndex - 10, fromIndex + 10);
99+
uint32_t index = math::algorithm::binary_search(mElements, e, fromIndex - 10, fromIndex + 10);
100100

101101
while (fromIndex < mElementsSize && mElements.at(fromIndex).mId != id) {
102102
fromIndex++;
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
#pragma once
22

3-
#include "Algorithm.h"
43
#include "BoundaryList.h"
54
#include "BoxSweep.h"
6-
#include "Constants.h"
75
#include "CurveBezier.h"
86
#include "CurveNurbs.h"
97
#include "Integration.h"
10-
#include "MathFunc.h"
11-
#include "MathSmooth.h"
128
#include "Octree.h"
139
#include "VecX.h"

packages/physics/source/common/BoxSweep.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#include "physics/BoxSweep.h"
44
#include "physics/VecX.h"
5+
#include "math/MathAlgorithm.h"
56

67
#include <algorithm>
78

@@ -111,7 +112,7 @@ void BoxSweep::update() {
111112
};
112113

113114
auto updatePair = [&](uint32_t id0, uint32_t id1, bool add) {
114-
auto id = l::algorithm::pairIndex32(id0, id1);
115+
auto id = l::math::algorithm::pairIndex32(id0, id1);
115116
if (add) {
116117
if (IsOverlapping(CO_AXIS_Y, mBoxes.at(id0), mBoxes.at(id1))) {
117118
mOverlapPairs.emplace(id);

packages/physics/tests/common/IntegrationTest.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#include "physics/Integration.h"
22

33
#include "physics/VecX.h"
4-
#include "physics/Algorithm.h"
4+
5+
#include "math/MathAlgorithm.h"
56

67
#include "testing/Test.h"
78

@@ -11,7 +12,7 @@ using namespace l;
1112

1213
TEST(Integration, Bisect) {
1314

14-
double x = algorithm::bisect<double>(0.2, 5.0, EPSILON, 10, [](double x) -> double {
15+
double x = math::algorithm::bisect<double>(0.2, 5.0, EPSILON, 10, [](double x) -> double {
1516
return -1.0 + (x - 1.0) * (x - 1.0);
1617
});
1718

0 commit comments

Comments
 (0)