Advertisement
agmike

Вычисление кривизны пути

Dec 6th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. void UpdateCurveCalculations()
  2. {
  3.     WorldCoordinate rightPosition = null;
  4.     Orientation rightOrient = null;
  5.     float rightCurvature;
  6.     if (Count > 0) {
  7.         rightPosition = VD[0].Veh.GetMapObjectPosition();
  8.         rightOrient = VD[0].Veh.GetMapObjectOrientation();
  9.     }
  10.    
  11.     int j;
  12.     for (j = 0; j < Count; ++j) {
  13.         WorldCoordinate currentPosition = rightPosition;
  14.         Orientation currentOrient = rightOrient;
  15.         rightPosition = null;
  16.         rightOrient = null;
  17.         float leftCurvature = rightCurvature;
  18.         float curvature;
  19.         if (j < Count - 1) {
  20.             rightPosition = VD[j + 1].Veh.GetMapObjectPosition();
  21.             rightOrient = VD[j + 1].Veh.GetMapObjectOrientation();
  22.             rightCurvature = GetCurvature(currentPosition, currentOrient, rightPosition, rightOrient);
  23.             if (j > 0)
  24.                 curvature = 0.5f * (leftCurvature + rightCurvature);
  25.             else
  26.                 curvature = rightCurvature;
  27.         }
  28.         else {
  29.             curvature = leftCurvature;
  30.             rightCurvature = 0.0f;
  31.         }
  32.         VD[j].Curvature = curvature;
  33.     }
  34. }
  35.  
  36.  
  37. // Experimental stuff
  38.  
  39. define float Pi = 3.1415927;
  40. define float PiOverTwo = 3.1415927 / 2;
  41. define float PiSquared = 3.1415927 * 3.1415927;
  42.    
  43. define float sinB = 4.0 / Pi;
  44. define float sinC = -4.0 / PiSquared;
  45. define float sinQ = 0.775;
  46. define float sinP = 0.225;
  47. define float sinApproxRangeStart = 0.186842089;
  48.  
  49. final float Sin2(float x)
  50. {
  51.     if (x < sinApproxRangeStart)
  52.         return x;
  53.     float y = sinB * x + sinC * x * Math.Fabs(x);
  54.     y = sinP * (y * Math.Fabs(y) - y) + y;
  55.     return y;
  56. }
  57.  
  58. final float Sin(float x)
  59. {
  60.     int periods = (int) (x / Pi);
  61.     float sign = 1.0f;
  62.     if (x < 0 != (periods % 2 != 0))
  63.         sign = -1.0f;
  64.     float xNorm = x - Pi * periods;
  65.     if (xNorm < 0.0)
  66.         xNorm = -xNorm;
  67.     if (0.0 <= xNorm and xNorm <= PiOverTwo)
  68.         return sign * Sin2(xNorm);
  69.     return sign * Sin2(Pi - xNorm);
  70. }
  71.  
  72. public float Cos(float x)
  73. {
  74.     int periods = (int) (x / Pi);
  75.     float sign = 1.0f;
  76.     if (periods % 2 != 0)
  77.         sign = -1.0f;
  78.     float xNorm = x - Pi * periods;
  79.     if (xNorm < 0.0)
  80.         xNorm = -xNorm;
  81.     if (0.0 <= xNorm and xNorm <= PiOverTwo)
  82.         return sign * Sin2(PiOverTwo - xNorm);
  83.     return sign * -Sin2(xNorm - PiOverTwo);
  84. }
  85.  
  86. // This mathematical menace was created by TRam_
  87. float GetCurvature(WorldCoordinate p1, Orientation o1, WorldCoordinate p2, Orientation o2)
  88. {
  89.     float baseDeltaX = (p2.baseboardX - p1.baseboardX) * 720.0f;
  90.     float baseDeltaY = (p2.baseboardY - p1.baseboardY) * 720.0f;
  91.     float dx = baseDeltaX + p2.x - p1.x;
  92.     float dy = baseDeltaY + p2.y - p1.y;
  93.    
  94.     float baseRot = o1.rz;
  95.     float baseSin = Sin(baseRot);
  96.     float baseCos = Cos(baseRot);
  97.    
  98.     float baseX = dx * baseCos + dy * baseSin;
  99.     float baseY = -dx * baseSin + dy * baseCos;
  100.    
  101.     return Math.Fabs(2.0f * baseX / (baseX * baseX + baseY * baseY));
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement