pihmLIBS/simplifyPolyline.h

Go to the documentation of this file.
00001 #include <iostream>
00002 #include <math.h>
00003 
00004 using namespace std;
00005 
00006 struct Point{
00007         double x, y;
00008 };
00009 
00010 double dPointToLine(Point L1, Point L2, Point P){
00011         double dist; 
00012         dist = (L1.y-L2.y)*P.x + (L2.x-L1.x)*P.y + (L1.x*L2.y - L2.x*L1.y);
00013         dist = dist / sqrt(pow((L2.x-L1.x),2) + pow((L2.y-L1.y),2));
00014         if(L1.x==L2.x && L1.y==L2.y)
00015                 dist = sqrt(pow((L1.x-P.x),2) + pow((L1.y-P.y),2));
00016         return (dist>0?dist:(-dist));
00017 }
00018 /*
00019 void simplify_Polyline(Point *poly, int start, int end, double tolerance, int *marker, int flag){ //
00020         // Flag accounts if the function is called for the first time.. 0=>first time; 1=>otherwise
00021         if (end < start+1)
00022                 return;
00023         cout<<start<<"\t"<<end<<"\n";
00024         static int count=0;
00025         if(flag == 0)
00026                 count = 0;
00027         int     maxIndex = start;      // index of vertex farthest from S
00028         double   maxDistance = 0;       // distance squared of farthest vertex
00029     
00030         for (int i=start+1; i<end; i++){
00031                 double dist = dPointToLine(poly[start], poly[end], poly[i]);
00032                 if(dist > maxDistance){
00033                         maxDistance = dist;
00034                         maxIndex = i;
00035                 }
00036         }
00037 
00038         if(maxDistance>tolerance){
00039                 marker[count] = maxIndex;
00040                 cout<<"count= "<<count<<"\n";
00041                 cout<<"marker="<<marker[count]<<"\n";
00042                 count++;
00043                 simplify_Polyline(poly, start, maxIndex, tolerance, marker, 1);
00044                 simplify_Polyline(poly, maxIndex, end, tolerance, marker, 1);
00045         }
00046         return;
00047 }
00048 */
00049 void simplifyPolyline(Point *poly, int start, int end, double tolerance, int *marker){ //, int flag){ //
00050         // Flag accounts if the function is called for the first time.. 0=>first time; 1=>otherwise
00051         if (end < start+1)
00052                 return;// 0;
00053         //cout<<start<<"\t"<<end<<"\n";
00054         /*static int count=1;
00055         if(flag == 0)
00056                 count = 1; */
00057         int     maxIndex = start;      // index of vertex farthest from S
00058         double   maxDistance = 0;       // distance squared of farthest vertex
00059     
00060         for (int i=start+1; i<end; i++){
00061                 double dist = dPointToLine(poly[start], poly[end], poly[i]);
00062                 if(dist > maxDistance){
00063                         maxDistance = dist;
00064                         maxIndex = i;
00065                 }
00066         }
00067 
00068         if(maxDistance>tolerance){
00069                 //marker[count] = maxIndex;
00070                 marker[maxIndex]=1;
00071                 //cout<<"count= "<<count<<"\n";
00072                 //cout<<"marker="<<marker[count]<<"\n";
00073                 //count++;
00074                 simplifyPolyline(poly, start, maxIndex, tolerance, marker);
00075                 simplifyPolyline(poly, maxIndex, end, tolerance, marker);
00076         }
00077         return;// count;
00078 }
00079         

Generated on Sun Aug 5 17:34:00 2007 for PIHMgis by  doxygen 1.5.2