LCOV - code coverage report
Current view: top level - source/maths - NUSpline.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 94 147 63.9 %
Date: 2026-12-03 01:19:39 Functions: 16 24 66.7 %

          Line data    Source code
       1             : /* Copyright (C) 2026 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       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.
       8             :  *
       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.
      13             :  *
      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/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "NUSpline.h"
      21             : 
      22             : #include "lib/debug.h"
      23             : #include "maths/FixedVector3D.h"
      24             : #include "maths/Matrix3D.h"
      25             : #include "maths/Vector3D.h"
      26             : 
      27             : #include <utility>
      28             : 
      29             : 
      30             : //Note: column major order!  Each set of 4 constitutes a column.
      31             : CMatrix3D HermiteSpline(2.f, -3.f, 0.f, 1.f,
      32             :                         -2.f, 3.f, 0.f, 0.f,
      33             :                         1.f, -2.f, 1.f, 0.f,
      34             :                         1.f, -1.f, 0.f, 0.f);  // Matrix H in article
      35             : 
      36             : 
      37             : // cubic curve defined by 2 positions and 2 velocities
      38          30 : CVector3D GetPositionOnCubic(const CVector3D& startPos, const CVector3D& startVel, const CVector3D& endPos, const CVector3D& endVel, float time)
      39             : {
      40          30 :     CMatrix3D m(startPos.X, endPos.X, startVel.X, endVel.X,
      41          30 :                 startPos.Y, endPos.Y, startVel.Y, endVel.Y,
      42          30 :                 startPos.Z, endPos.Z, startVel.Z, endVel.Z,
      43          30 :                 0.0f, 0.0f, 0.0f, 1.0f);
      44             : 
      45          30 :     m = m * HermiteSpline; // multiply by the mixer
      46             : 
      47          30 :     CVector3D TimeVector(time*time*time, time*time, time);
      48          30 :     CVector3D Result;
      49          30 :     m.Transform(TimeVector, Result);
      50          30 :     return Result;
      51             : }
      52             : 
      53             : /*********************************** R N S **************************************************/
      54             : 
      55          29 : RNSpline::RNSpline() = default;
      56             : 
      57          59 : RNSpline::~RNSpline() = default;
      58             : 
      59             : // adds node and updates segment length
      60           0 : void RNSpline::AddNode(const CFixedVector3D& pos)
      61             : {
      62           0 :     if (m_NodeCount >= MAX_SPLINE_NODES)
      63           0 :         return;
      64           0 :     if (m_NodeCount == 0)
      65           0 :         m_MaxDistance = fixed::Zero();
      66             :     else
      67             :     {
      68           0 :         m_Nodes[m_NodeCount-1].Distance = (m_Nodes[m_NodeCount-1].Position - pos).Length();
      69           0 :         m_MaxDistance += m_Nodes[m_NodeCount-1].Distance;
      70             :     }
      71           0 :     SplineData temp;
      72           0 :     temp.Position = pos;
      73           0 :     m_Nodes.push_back(temp);
      74           0 :     ++m_NodeCount;
      75             : }
      76             : 
      77             : 
      78             : // called after all nodes added. This function calculates the node velocities
      79          14 : void RNSpline::BuildSpline()
      80             : {
      81          14 :     if (m_NodeCount == 2)
      82             :     {
      83          14 :         m_Nodes[0].Velocity = GetStartVelocity(0);
      84          14 :         m_Nodes[m_NodeCount-1].Velocity = GetEndVelocity(m_NodeCount-1);
      85          14 :         return;
      86             :     }
      87           0 :     else if (m_NodeCount < 2)
      88             :         return;
      89             : 
      90           0 :     for (int i = 1; i < m_NodeCount-1; ++i)
      91             :     {
      92           0 :         CVector3D Next = m_Nodes[i+1].Position - m_Nodes[i].Position;
      93           0 :         CVector3D Previous = m_Nodes[i-1].Position - m_Nodes[i].Position;
      94           0 :         Next.Normalize();
      95           0 :         Previous.Normalize();
      96             : 
      97             :         // split the angle (figure 4)
      98           0 :         m_Nodes[i].Velocity = Next - Previous;
      99           0 :         m_Nodes[i].Velocity.Normalize();
     100             :     }
     101             :     // calculate start and end velocities
     102           0 :     m_Nodes[0].Velocity = GetStartVelocity(0);
     103           0 :     m_Nodes[m_NodeCount-1].Velocity = GetEndVelocity(m_NodeCount-1);
     104             : }
     105             : 
     106             : // spline access function. time is 0 -> 1
     107          30 : CVector3D RNSpline::GetPosition(float time) const
     108             : {
     109          30 :     if (m_NodeCount < 2)
     110           0 :         return CVector3D(0.0f, 0.0f, 0.0f);
     111          30 :     if (time < 0.0f)
     112           0 :         time = 0.0f;
     113          30 :     if (time > 1.0f)
     114           0 :         time = 1.0f;
     115          30 :     float Distance = time * m_MaxDistance.ToFloat();
     116          30 :     float CurrentDistance = 0.f;
     117          30 :     int i = 0;
     118             : 
     119             :     // Find which node we're on
     120          36 :     while (CurrentDistance + m_Nodes[i].Distance.ToFloat() < Distance && i < m_NodeCount - 2)
     121             :     {
     122           6 :         CurrentDistance += m_Nodes[i].Distance.ToFloat();
     123           6 :         ++i;
     124             :     }
     125          30 :     ENSURE(i < m_NodeCount - 1);
     126          30 :     float t = Distance - CurrentDistance;
     127             :     // TODO: reimplement CVector3D comparator (float comparing is bad without EPS)
     128          30 :     if (m_Nodes[i].Position == m_Nodes[i+1].Position || m_Nodes[i].Distance.ToFloat() < 1e-7) // distance too small or zero
     129             :     {
     130           0 :         return m_Nodes[i+1].Position;
     131             :     }
     132          30 :     t /= m_Nodes[i].Distance.ToFloat(); // scale t in range 0 - 1
     133          30 :     CVector3D startVel = m_Nodes[i].Velocity * m_Nodes[i].Distance.ToFloat();
     134          30 :     CVector3D endVel = m_Nodes[i+1].Velocity * m_Nodes[i].Distance.ToFloat();
     135          30 :     return GetPositionOnCubic(m_Nodes[i].Position, startVel,
     136          30 :         m_Nodes[i+1].Position, endVel, t);
     137             : }
     138             : 
     139          61 : const std::vector<SplineData>& RNSpline::GetAllNodes() const
     140             : {
     141          61 :     return m_Nodes;
     142             : }
     143             : 
     144             : // internal. Based on Equation 14
     145          14 : CVector3D RNSpline::GetStartVelocity(int index)
     146             : {
     147          14 :     if (index >= m_NodeCount - 1 || index < 0)
     148           0 :         return CVector3D(0.0f, 0.0f, 0.0f);
     149          14 :     CVector3D temp = CVector3D(m_Nodes[index+1].Position - m_Nodes[index].Position) * 3.0f * (1.0f / m_Nodes[index].Distance.ToFloat());
     150          14 :     return (temp - m_Nodes[index+1].Velocity)*0.5f;
     151             : }
     152             : 
     153             : // internal. Based on Equation 15
     154          14 : CVector3D RNSpline::GetEndVelocity(int index)
     155             : {
     156          14 :     if (index >= m_NodeCount || index < 1)
     157           0 :         return CVector3D(0.0f, 0.0f, 0.0f);
     158          14 :     CVector3D temp = CVector3D(m_Nodes[index].Position - m_Nodes[index-1].Position) * 3.0f * (1.0f / m_Nodes[index-1].Distance.ToFloat());
     159          14 :     return (temp - m_Nodes[index-1].Velocity) * 0.5f;
     160             : }
     161             : 
     162             : /*********************************** S N S **************************************************/
     163             : 
     164          59 : SNSpline::~SNSpline() = default;
     165             : 
     166           0 : void SNSpline::BuildSpline()
     167             : {
     168           0 :     RNSpline::BuildSpline();
     169           0 :     for (int i = 0; i < 3; ++i)
     170           0 :         Smooth();
     171           0 : }
     172             : 
     173             : // smoothing filter.
     174         126 : void SNSpline::Smooth()
     175             : {
     176         126 :     if (m_NodeCount < 3)
     177         126 :         return;
     178             : 
     179           0 :     CVector3D newVel;
     180           0 :     CVector3D oldVel = GetStartVelocity(0);
     181           0 :     for (int i = 1; i < m_NodeCount-1; ++i)
     182             :     {
     183             :         // Equation 12
     184           0 :         newVel = GetEndVelocity(i) * m_Nodes[i].Distance.ToFloat() + GetStartVelocity(i) * m_Nodes[i-1].Distance.ToFloat();
     185           0 :         newVel = newVel * (1 / (m_Nodes[i-1].Distance + m_Nodes[i].Distance).ToFloat());
     186           0 :         m_Nodes[i-1].Velocity = oldVel;
     187           0 :         oldVel = newVel;
     188             :     }
     189           0 :     m_Nodes[m_NodeCount-1].Velocity = GetEndVelocity(m_NodeCount-1);
     190           0 :     m_Nodes[m_NodeCount-2].Velocity = oldVel;
     191             : }
     192             : 
     193             : /*********************************** T N S **************************************************/
     194             : 
     195          59 : TNSpline::~TNSpline() = default;
     196             : 
     197             : // as with RNSpline but use timePeriod in place of actual node spacing
     198             : // ie time period is time from last node to this node
     199          35 : void TNSpline::AddNode(const CFixedVector3D& pos, const CFixedVector3D& rotation, fixed timePeriod)
     200             : {
     201          35 :     if (m_NodeCount >= MAX_SPLINE_NODES)
     202           0 :         return;
     203             : 
     204          35 :     if (m_NodeCount == 0)
     205          16 :         m_MaxDistance = fixed::Zero();
     206             :     else
     207             :     {
     208          19 :         m_Nodes[m_NodeCount-1].Distance = timePeriod;
     209          19 :         m_MaxDistance += m_Nodes[m_NodeCount-1].Distance;
     210             :     }
     211             : 
     212          35 :     SplineData temp;
     213          35 :     temp.Position = pos;
     214             : 
     215             :     //make sure we don't end up using undefined numbers...
     216          35 :     temp.Distance = fixed::Zero();
     217          35 :     temp.Velocity = CVector3D(0.0f, 0.0f, 0.0f);
     218          35 :     temp.Rotation = rotation;
     219          35 :     m_Nodes.push_back(temp);
     220          35 :     ++m_NodeCount;
     221             : }
     222             : 
     223             : //Inserts node before position
     224           3 : void TNSpline::InsertNode(const int index, const CFixedVector3D& pos, const CFixedVector3D& /*rotation*/,
     225             :     fixed timePeriod)
     226             : {
     227           3 :     if (m_NodeCount >= MAX_SPLINE_NODES || index < 0 || index > m_NodeCount)
     228           0 :         return;
     229             : 
     230           3 :     if (m_NodeCount == 0)
     231           1 :         m_MaxDistance = fixed::Zero();
     232             :     else
     233           2 :         m_MaxDistance += timePeriod;
     234             : 
     235           3 :     SplineData temp;
     236           3 :     temp.Position = pos;
     237           3 :     temp.Distance = timePeriod;
     238           3 :     m_Nodes.insert(m_Nodes.begin() + index, temp);
     239           3 :     if (index > 0)
     240           1 :         std::swap(m_Nodes[index].Distance, m_Nodes[index - 1].Distance);
     241           3 :     ++m_NodeCount;
     242             : }
     243             : 
     244             : //Removes node at index
     245           2 : void TNSpline::RemoveNode(const int index)
     246             : {
     247           2 :     if (m_NodeCount == 0 || index > m_NodeCount - 1)
     248             :         return;
     249             : 
     250           2 :     if (index > 0)
     251           1 :         std::swap(m_Nodes[index].Distance, m_Nodes[index - 1].Distance);
     252           2 :     m_MaxDistance -= m_Nodes[index].Distance;
     253           2 :     m_Nodes.erase(m_Nodes.begin() + index);
     254           2 :     --m_NodeCount;
     255             : }
     256             : 
     257           0 : void TNSpline::UpdateNodeTime(const int index, fixed time)
     258             : {
     259           0 :     if (m_NodeCount == 0 || index > m_NodeCount - 1)
     260             :         return;
     261             : 
     262           0 :     m_Nodes[index].Distance = time;
     263             : }
     264             : 
     265           0 : void TNSpline::UpdateNodePos(const int index, const CFixedVector3D& pos)
     266             : {
     267           0 :     if (m_NodeCount == 0 || index > m_NodeCount - 1)
     268             :         return;
     269             : 
     270           0 :     m_Nodes[index].Position = pos;
     271             : }
     272             : 
     273          14 : void TNSpline::BuildSpline()
     274             : {
     275          14 :     RNSpline::BuildSpline();
     276          56 :     for (int i = 0; i < 3; ++i)
     277          42 :         Smooth();
     278          14 : }
     279             : 
     280          42 : void TNSpline::Smooth()
     281             : {
     282         168 :     for (int i = 0; i < 3; ++i)
     283             :     {
     284         126 :         SNSpline::Smooth();
     285         126 :         Constrain();
     286             :     }
     287          42 : }
     288             : 
     289         126 : void TNSpline::Constrain()
     290             : {
     291         126 :     if (m_NodeCount < 3)
     292             :         return;
     293             : 
     294           0 :     for (int i = 1; i < m_NodeCount-1; ++i)
     295             :     {
     296             :         // Equation 13
     297           0 :         float r0 = (m_Nodes[i].Position - m_Nodes[i - 1].Position).Length().ToFloat() / m_Nodes[i-1].Distance.ToFloat();
     298           0 :         float r1 = (m_Nodes[i+1].Position - m_Nodes[i].Position).Length().ToFloat() / m_Nodes[i].Distance.ToFloat();
     299           0 :         m_Nodes[i].Velocity *= 4.0f*r0*r1 / ((r0 + r1)*(r0 + r1));
     300             :     }
     301             : }
     302             : 

Generated by: LCOV version 1.16