00001
00002
00003
00004
00005 #pragma once
00006 #include "gNode.h"
00007 #include "Point.h"
00008 #include "PointList.h"
00009 #include "Step.h"
00010 #include <deque>
00011 #include <algorithm>
00012 #include <math.h>
00013 using namespace std;
00014 namespace Recognition{
00018 class Grid{
00020 gNode *origin;
00022 int rowSize;
00024 int colSize;
00026 void orient();
00028 void check();
00030 double dist(gNode *one,gNode *two){
00031 double dx=one->x-two->x;
00032 double dy=one->y-two->y;
00033 return hypot(dx,dy);
00034 }
00035 public:
00036
00042 Point extendTop(int i,int &t){
00043 double x=top[i]->x;
00044 double y=top[i]->y;
00045 double x2=top[i]->b->x;
00046 double y2=top[i]->b->y;
00047 double x3=top[i]->b->b->x;
00048 double y3=top[i]->b->b->y;
00049
00050 double dx=x-x2;
00051 double dy=y-y2;
00052 double d1=hypot(dx,dy);
00053 double d2=hypot(x2-x3,y2-y3);
00054 dx*= d1/d2;
00055 dy*= d1/d2;
00056 t=hypot(dx,dy)/2;
00057
00058 return Point(x + dx, y + dy );
00059 }
00065 Point extendBottom(int i,int &t){
00066 double x=bot[i]->x;
00067 double y=bot[i]->y;
00068 double x2=bot[i]->t->x;
00069 double y2=bot[i]->t->y;
00070 double x3=bot[i]->t->t->x;
00071 double y3=bot[i]->t->t->y;
00072
00073 double dx=x-x2;
00074 double dy=y-y2;
00075 double d1=hypot(dx,dy);
00076 double d2=hypot(x2-x3,y2-y3);
00077 dx*= d1/d2;
00078 dy*= d1/d2;
00079 t=hypot(dx,dy)/2;
00080 return Point(x + dx, y + dy );
00081 }
00087 Point extendLeft(int i,int &t){
00088 double x=left[i]->x;
00089 double y=left[i]->y;
00090 double x2=left[i]->r->x;
00091 double y2=left[i]->r->y;
00092 double x3=left[i]->r->r->x;
00093 double y3=left[i]->r->r->y;
00094
00095 double dx=x-x2;
00096 double dy=y-y2;
00097 double d1=hypot(dx,dy);
00098 double d2=hypot(x2-x3,y2-y3);
00099 dx*= d1/d2;
00100 dy*= d1/d2;
00101 t=hypot(dx,dy)/2;
00102 return Point(x + dx, y + dy );
00103 }
00109 Point extendRight(int i,int &t){
00110 double x=righ[i]->x;
00111 double y=righ[i]->y;
00112 double x2=righ[i]->l->x;
00113 double y2=righ[i]->l->y;
00114 double x3=righ[i]->l->l->x;
00115 double y3=righ[i]->l->l->y;
00116
00117 double dx=x-x2;
00118 double dy=y-y2;
00119 double d1=hypot(dx,dy);
00120 double d2=hypot(x2-x3,y2-y3);
00121 dx*= d1/d2;
00122 dy*= d1/d2;
00123 t=hypot(dx,dy)/2;
00124 return Point(x + dx, y + dy );
00125 }
00126
00127
00128
00130 deque <gNode *>top;
00132 deque <gNode *>bot;
00134 deque <gNode *>left;
00136 deque <gNode *>righ;
00138 void switchXandY();
00139 Grid(Point &);
00140 ~Grid();
00142 void addTrow(PointList &);
00143 void addBrow(PointList &);
00144 void addLcol(PointList &);
00145 void addRcol(PointList &);
00146
00147 int getRSize(){return rowSize;}
00148 int getCSize(){return colSize;}
00153 Step getStep(char side,int index){
00154 Step temp;
00155 Step temp2;
00156 switch(side){
00157 case 't':
00158 temp.rise=top[index]->y-top[index]->b->y;
00159 temp.run =top[index]->x-top[index]->b->x;
00160 temp2.rise=top[index]->b->y-top[index]->b->b->y;
00161 temp2.run =top[index]->b->x-top[index]->b->b->x;
00162 break;
00163 case 'b':
00164 temp.rise=bot[index]->y-bot[index]->t->y;
00165 temp.run =bot[index]->x-bot[index]->t->x;
00166 temp2.rise=bot[index]->t->y-bot[index]->t->t->y;
00167 temp2.run =bot[index]->t->x-bot[index]->t->t->x;
00168 break;
00169 case 'l':
00170 temp.rise=left[index]->y-left[index]->r->y;
00171 temp.run =left[index]->x-left[index]->r->x;
00172 temp2.rise=left[index]->r->y-left[index]->r->r->y;
00173 temp2.run =left[index]->r->x-left[index]->r->r->x;
00174 break;
00175 case 'r':
00176 temp.rise=righ[index]->y-righ[index]->l->y;
00177 temp.run =righ[index]->x-righ[index]->l->x;
00178 temp2.rise=righ[index]->l->y-righ[index]->l->l->y;
00179 temp2.run =righ[index]->l->x-righ[index]->l->l->x;
00180 break;
00181 default:
00182 cout<<"error in getStep--wrong valued parameter\n";
00183 }
00184 temp.rise+= temp.rise -temp2.rise;
00185 temp.run += temp.run -temp2.run;
00186 return temp;
00187 }
00189 void toList(PointList &);
00191 void print(){
00192 gNode *temp=origin;
00193 gNode *tempR=origin;
00194 while( temp!=NULL){
00195 while(temp!=NULL){
00196 cout<<"["<<temp->x<<","<<temp->y<<"] ";
00197 temp=temp->r;
00198 }
00199 tempR=tempR->b;
00200 temp=tempR;
00201 cout<<endl;
00202 }
00203 }
00204 };
00205 }