| 1 | // Jolt Physics Library (https://github.com/jrouwe/JoltPhysics) |
| 2 | // SPDX-FileCopyrightText: 2021 Jorrit Rouwe |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <Jolt/Jolt.h> |
| 6 | |
| 7 | #include <Jolt/Core/LinearCurve.h> |
| 8 | #include <Jolt/Core/StreamIn.h> |
| 9 | #include <Jolt/Core/StreamOut.h> |
| 10 | #include <Jolt/ObjectStream/TypeDeclarations.h> |
| 11 | |
| 12 | JPH_NAMESPACE_BEGIN |
| 13 | |
| 14 | JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(LinearCurve::Point) |
| 15 | { |
| 16 | JPH_ADD_ATTRIBUTE(Point, mX) |
| 17 | JPH_ADD_ATTRIBUTE(Point, mY) |
| 18 | } |
| 19 | |
| 20 | JPH_IMPLEMENT_SERIALIZABLE_NON_VIRTUAL(LinearCurve) |
| 21 | { |
| 22 | JPH_ADD_ATTRIBUTE(LinearCurve, mPoints) |
| 23 | } |
| 24 | |
| 25 | float LinearCurve::GetValue(float inX) const |
| 26 | { |
| 27 | if (mPoints.empty()) |
| 28 | return 0.0f; |
| 29 | |
| 30 | Points::const_iterator i2 = std::lower_bound(mPoints.begin(), mPoints.end(), inX, [](const Point &inPoint, float inValue) { return inPoint.mX < inValue; }); |
| 31 | |
| 32 | if (i2 == mPoints.begin()) |
| 33 | return mPoints.front().mY; |
| 34 | else if (i2 == mPoints.end()) |
| 35 | return mPoints.back().mY; |
| 36 | |
| 37 | Points::const_iterator i1 = i2 - 1; |
| 38 | return i1->mY + (inX - i1->mX) * (i2->mY - i1->mY) / (i2->mX - i1->mX); |
| 39 | } |
| 40 | |
| 41 | void LinearCurve::SaveBinaryState(StreamOut &inStream) const |
| 42 | { |
| 43 | inStream.Write(mPoints); |
| 44 | } |
| 45 | |
| 46 | void LinearCurve::RestoreBinaryState(StreamIn &inStream) |
| 47 | { |
| 48 | inStream.Read(mPoints); |
| 49 | } |
| 50 | |
| 51 | JPH_NAMESPACE_END |