Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 3320 in orxonox.OLD for orxonox/branches/parenting/src/curve.cc


Ignore:
Timestamp:
Jan 3, 2005, 12:23:41 PM (21 years ago)
Author:
bensch
Message:

orxonox/branches/parenting: curve-optimizations. Now calculation should be about 5 times faster. especially for long tracks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • orxonox/branches/parenting/src/curve.cc

    r3319 r3320  
    2424#include "curve.h"
    2525
    26 
     26#include <math.h>
     27#include <stdio.h>
    2728
    2829/**
     
    3940  currentNode->next = 0; // not sure if this really points to NULL!!
    4041  currentNode->number = (++nodeCount);
     42  this->rebuild();
    4143  return;
    4244}
     
    7779
    7880/**
     81   \brief Rebuilds a Curve
     82*/
     83void BezierCurve::rebuild(void)
     84{
     85  PathNode* tmpNode = firstNode;
     86
     87  int k=0;
     88  int binCoef = 1;
     89  while(tmpNode)
     90    {
     91      if (k+1 < nodeCount-k)
     92        binCoef *=(nodeCount-k)/(k+1);
     93      else
     94        binCoef /= (k+1)/(nodeCount-k);
     95      ++k;
     96      tmpNode->factor = binCoef;
     97      tmpNode = tmpNode->next;
     98    }
     99}
     100
     101/**
    79102   \brief calculates the Position on the curve
    80103   \param t The position on the Curve (0<=t<=1)
     
    85108  if (nodeCount <=4)
    86109    {
    87       //      if (verbose >= 1)
     110      //    if (verbose >= 1)
    88111      //      printf ("Please define at least 4 nodes, until now you have only defined %i.\n", nodeCount);
    89112      return Vector(0,0,0);
    90113    }
    91114  PathNode* tmpNode = firstNode;
    92    
    93115  Vector ret = Vector(0.0,0.0,0.0);
    94   double factor;
    95   int k=0;
    96   while(tmpNode!=0)
     116  float factor = 1.0*pow(1.0-t,nodeCount);
     117  while(tmpNode)
    97118    {
    98       k++;
    99       factor = ncr (nodeCount, k);
    100      
    101       for (int j=0; j<nodeCount-k; j++)
    102         factor*=(1-t);
    103       for (int j=0; j<k; j++)
    104         factor*=t;
    105       ret.x += factor * tmpNode->position.x;
    106       ret.y += factor * tmpNode->position.y;
    107       ret.z += factor * tmpNode->position.z;
    108      
     119      factor *= t/(1.0-t); // same as pow but much faster.
     120      ret.x += tmpNode->factor * factor * tmpNode->position.x;
     121      ret.y += tmpNode->factor * factor * tmpNode->position.y;
     122      ret.z += tmpNode->factor * factor * tmpNode->position.z;
     123
    109124      tmpNode = tmpNode->next;
    110 
    111125    }
    112126  return ret;
     
    160174  return curvePoint;
    161175}
    162 
    163 /**
    164    \brief ncr-calculator, did not find an other method
    165    \todo a c++ variante to do this
    166 */
    167 int ncr(int n, int i)
    168 {
    169   int ret = 1;
    170   for (int k=1; k<=n; k++)
    171     ret*=k;
    172   for (int k=1; k<=i; k++)
    173     ret/=k;
    174   for (int k=1; k<=n-i; k++)
    175     ret/=k;
    176 
    177   return ret;
    178 }
    179 
Note: See TracChangeset for help on using the changeset viewer.