Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

# Tools/PotentialFields/RandomMotionGenerator.cpp

Go to the documentation of this file.

    00001 /**
    00002 * @file RandomMotionGenerator.cpp
    00003 *
    00004 * Implementation of class RandomMotionGenerator
    00005 *
    00006 * @author <a href="mailto:timlaue@informatik.uni-bremen.de">Tim Laue</a>
    00007 */
    00008
    00009 #include <cstdlib>
    00010 #include "RandomMotionGenerator.h"
    00011 #include "PfieldConfig.h"
    00012
    00013
    00014
    00015 RandomMotionGenerator::RandomMotionGenerator
    00016                        (double minValue, double maxValue, double valueDx,
    00017                         double directionDx, ChangeType changeType, unsigned long n)
    00018 {
    00019   this->minValue = minValue;
    00020   this->maxValue = maxValue;
    00021   this->valueDx = valueDx;
    00022   this->directionDx = directionDx;
    00023   this->changeType = changeType;
    00024   this->n = n;
    00025   // Initialize random generator
    00026   srand (getSystemTime());
    00027   // Set initial vector
    00028   lastVec.x = computeVecLength(minValue + ((maxValue-minValue) / 2.0));
    00029   lastVec.y = 0.0;
    00030   lastDirection = getRandomNumberBetween(-pi,pi);
    00031   lastVec.rotate(lastDirection);
    00032   pointOfGenerationTime = getSystemTime();
    00033 }
    00034
    00035
    00036 PfVec RandomMotionGenerator::getMotionVector()
    00037 {
    00038   calls++;
    00039   if((changeType == CALLS) && (calls < n))
    00040   {
    00041     return lastVec;
    00042   }
    00043   else if((changeType == MILLISECONDS) &&
    00044           ((getSystemTime() - pointOfGenerationTime) < n))
    00045   {
    00046     return lastVec;
    00047   }
    00048   else
    00049   {
    00050     PfVec result(computeVecLength(lastVec.length()),0.0);
    00051     lastDirection = computeDirection(lastDirection);
    00052     result.rotate(lastDirection);
    00053     lastVec = result;
    00054     pointOfGenerationTime = getSystemTime();
    00055     calls = 0;
    00056     return result;
    00057   }
    00058 }
    00059
    00060
    00061 inline double RandomMotionGenerator::computeDirection(double previousDirection) const
    00062 {
    00063   double maxVal(previousDirection + directionDx);
    00064   double minVal(previousDirection - directionDx);
    00065   double newDirection(getRandomNumberBetween(minVal,maxVal));
    00066   while(newDirection > pi) newDirection-=pi2;
    00067   while(newDirection < -pi) newDirection+=pi2;
    00068   return newDirection;
    00069 }
    00070
    00071
    00072 inline double RandomMotionGenerator::computeVecLength(double previousLength) const
    00073 {
    00074   if(minValue == maxValue)
    00075   {
    00076     return previousLength;
    00077   }
    00078   else
    00079   {
    00080     double minVal(previousLength - valueDx);
    00081     double maxVal(previousLength + valueDx);
    00082     if(minVal < minValue) minVal = minValue;
    00083     if(maxVal > maxValue) maxVal = maxValue;
    00084     return getRandomNumberBetween(minVal, maxVal);
    00085   }
    00086 }
    00087
    00088
    00089 inline double RandomMotionGenerator::getRandomNumberBetween(double min, double max) const
    00090 {
    00091   double range(max - min);
    00092   double scalar(((double)rand()) / ((double)RAND_MAX));
    00093   return (min + scalar*range);
    00094 }