00001 #include <assert.h>
00002 #include "CollisionDetectors\CD_BasicStyle.h"
00003 #include "SM_PathSmoothingRandomNodePair.h"
00004
00005
00006
00007
00008
00009
00010 SM_PathSmoothingRandomNodePair::SM_PathSmoothingRandomNodePair()
00011 :
00012 SmootherBase()
00013 {
00014 }
00015
00016
00017
00018
00019
00020
00021 SM_PathSmoothingRandomNodePair::~SM_PathSmoothingRandomNodePair()
00022 {
00023 }
00024
00025
00026
00027
00028
00029
00030 void SM_PathSmoothingRandomNodePair::Smooth()
00031 {
00032 bool pathInterfering = true;
00033 int t1 = 0, t2 = 0, temp = 0, size = 0;
00034 double old_dist = 0.0, new_dist = 0.0;
00035 Configuration c1;
00036 Configuration c2;
00037
00038 size = this->m_PathToSmooth->Size();
00039
00040 if (size == 2)
00041 {
00042 this->m_SmoothedPath.Clear();
00043 this->m_SmoothedPath.AppendPoint(this->m_PathToSmooth->GetPoint(0));
00044 this->m_SmoothedPath.AppendPoint(this->m_PathToSmooth->GetPoint(1));
00045 return;
00046 }
00047
00048 this->m_Points.clear();
00049 for (int i = 0; i < size; i++)
00050 {
00051 this->m_Points.push_back(this->m_PathToSmooth->GetPoint(i));
00052 }
00053
00054
00055 for (int n = 0; n < 1000; n++)
00056 {
00057 t1 = Random(0,this->m_Points.size());
00058 t2 = Random(0,this->m_Points.size());
00059
00060
00061 if (t1 > t2)
00062 {
00063 temp = t1;
00064 t1 = t2;
00065 t2 = temp;
00066 }
00067 assert(t1 <= t2);
00068
00069 c1 = this->m_Points[t1];
00070 c2 = this->m_Points[t2];
00071 pathInterfering = this->m_CollisionDetector->IsInterferingLinear(c1,c2);
00072
00073 if (pathInterfering)
00074 continue;
00075 else
00076 {
00077 new_dist = Distance(c1,c2);
00078 old_dist = 0.0;
00079 for (int j = t1; j < t2; j++)
00080 {
00081 old_dist += Distance(this->m_Points[j],this->m_Points[j+1]);
00082 }
00083 if (new_dist < old_dist)
00084 UpdatePoints(t1,t2);
00085 }
00086 }
00087
00088 this->m_SmoothedPath.Clear();
00089 for (int k = 0; k < this->m_Points.size(); k++)
00090 {
00091 this->m_SmoothedPath.AppendPoint(this->m_Points[k]);
00092 }
00093 }
00094
00095 double SM_PathSmoothingRandomNodePair::Distance(const Configuration& conf1, const Configuration& conf2) const
00096 {
00097 return m_CollisionDetector->DistanceBetween( conf1, conf2 ) ;
00098 }
00099
00100 int SM_PathSmoothingRandomNodePair::Random(const int min, const int max) const
00101 {
00102 return ( int(min + rand() % max) );
00103 }
00104
00105 void SM_PathSmoothingRandomNodePair::UpdatePoints(const int index1, const int index2)
00106 {
00107 this->m_Points.erase(this->m_Points.begin()+index1+1, this->m_Points.begin()+index2 );
00108 }