Merge 'remotes/trunk'
[0ad.git] / source / maths / NUSpline.h
blob4e324c2b5809d04379476529135589da41c0943d
1 /* Copyright (C) 2020 Wildfire Games.
2 * This file is part of 0 A.D.
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
18 /**
19 * Contains classes for smooth splines
20 * Borrowed from Game Programming Gems 4. (Slightly changed to better suit our purposes
21 * and compatability. Any references to external material can be found there.
24 #ifndef INCLUDED_NUSPLINE
25 #define INCLUDED_NUSPLINE
27 #define MAX_SPLINE_NODES 128
29 #include "FixedVector3D.h"
30 #include "Vector3D.h"
32 #include <vector>
34 /**
35 * Describes a node of the spline
37 struct SplineData
39 // Should be fixed, because used in the simulation
40 CFixedVector3D Position;
41 CVector3D Velocity;
42 // TODO: make rotation as other spline
43 CFixedVector3D Rotation;
44 // Time interval to the previous node, should be 0 for the first node
45 fixed Distance;
49 /**
50 * Rounded Nonuniform Spline for describing spatial curves or paths with constant speed
52 class RNSpline
54 public:
56 RNSpline();
57 virtual ~RNSpline();
59 void AddNode(const CFixedVector3D& pos);
60 void BuildSpline();
61 CVector3D GetPosition(float time) const;
62 CVector3D GetRotation(float time) const;
63 const std::vector<SplineData>& GetAllNodes() const;
65 fixed MaxDistance;
66 int NodeCount;
68 protected:
70 std::vector<SplineData> Node;
71 CVector3D GetStartVelocity(int index);
72 CVector3D GetEndVelocity(int index);
76 /**
77 * Smooth Nonuniform Spline for describing paths with smooth acceleration and deceleration,
78 * but without turning
80 class SNSpline : public RNSpline
82 public:
83 virtual ~SNSpline();
85 void BuildSpline();
86 void Smooth();
90 /**
91 * Timed Nonuniform Spline for paths with different time intervals between nodes
93 class TNSpline : public SNSpline
95 public:
96 virtual ~TNSpline();
98 void AddNode(const CFixedVector3D& pos, const CFixedVector3D& rotation, fixed timePeriod);
99 void InsertNode(const int index, const CFixedVector3D& pos, const CFixedVector3D& rotation, fixed timePeriod);
100 void RemoveNode(const int index);
101 void UpdateNodePos(const int index, const CFixedVector3D& pos);
102 void UpdateNodeTime(const int index, fixed time);
104 void BuildSpline();
105 void Smooth();
106 void Constrain();
109 #endif // INCLUDED_NUSPLINE