Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/jolt_physics/Jolt/Core/LinearCurve.h
9906 views
1
// Jolt Physics Library (https://github.com/jrouwe/JoltPhysics)
2
// SPDX-FileCopyrightText: 2021 Jorrit Rouwe
3
// SPDX-License-Identifier: MIT
4
5
#pragma once
6
7
#include <Jolt/ObjectStream/SerializableObject.h>
8
#include <Jolt/Core/QuickSort.h>
9
10
JPH_NAMESPACE_BEGIN
11
12
class StreamOut;
13
class StreamIn;
14
15
// A set of points (x, y) that form a linear curve
16
class JPH_EXPORT LinearCurve
17
{
18
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, LinearCurve)
19
20
public:
21
/// A point on the curve
22
class Point
23
{
24
JPH_DECLARE_SERIALIZABLE_NON_VIRTUAL(JPH_EXPORT, Point)
25
26
public:
27
float mX = 0.0f;
28
float mY = 0.0f;
29
};
30
31
/// Remove all points
32
void Clear() { mPoints.clear(); }
33
34
/// Reserve memory for inNumPoints points
35
void Reserve(uint inNumPoints) { mPoints.reserve(inNumPoints); }
36
37
/// Add a point to the curve. Points must be inserted in ascending X or Sort() needs to be called when all points have been added.
38
/// @param inX X value
39
/// @param inY Y value
40
void AddPoint(float inX, float inY) { mPoints.push_back({ inX, inY }); }
41
42
/// Sort the points on X ascending
43
void Sort() { QuickSort(mPoints.begin(), mPoints.end(), [](const Point &inLHS, const Point &inRHS) { return inLHS.mX < inRHS.mX; }); }
44
45
/// Get the lowest X value
46
float GetMinX() const { return mPoints.empty()? 0.0f : mPoints.front().mX; }
47
48
/// Get the highest X value
49
float GetMaxX() const { return mPoints.empty()? 0.0f : mPoints.back().mX; }
50
51
/// Sample value on the curve
52
/// @param inX X value to sample at
53
/// @return Interpolated Y value
54
float GetValue(float inX) const;
55
56
/// Saves the state of this object in binary form to inStream.
57
void SaveBinaryState(StreamOut &inStream) const;
58
59
/// Restore the state of this object from inStream.
60
void RestoreBinaryState(StreamIn &inStream);
61
62
/// The points on the curve, should be sorted ascending by x
63
using Points = Array<Point>;
64
Points mPoints;
65
};
66
67
JPH_NAMESPACE_END
68
69