contrib/brl/bbas/bdgl/bdgl_peano_curve.cxx
Go to the documentation of this file.
00001 #include "bdgl_peano_curve.h"
00002 //:
00003 // \file
00004 
00005 #include <vcl_cmath.h>
00006 #include <vnl/vnl_math.h>
00007 
00008 vcl_vector<vgl_point_2d<double> > rotate_2d(vcl_vector<vgl_point_2d<double> > in, double angle)
00009 {
00010   vcl_vector< vgl_point_2d<double> >  pnew;
00011   for (unsigned i=0;i<in.size();i++)
00012   {
00013     double x=in[i].x(); double y=in[i].y();
00014     double xnew=vcl_cos(angle)*x+vcl_sin(angle)*y;
00015     double ynew=-vcl_sin(angle)*x+vcl_cos(angle)*y;
00016     pnew.push_back(vgl_point_2d<double>(xnew,ynew));
00017   }
00018   return pnew;
00019 }
00020 
00021 vcl_vector<vgl_point_2d<double> > trans_2d(vcl_vector<vgl_point_2d<double> > in, double tx, double ty)
00022 {
00023   vcl_vector< vgl_point_2d<double> >  pnew;
00024   for (unsigned i=0;i<in.size();i++)
00025   {
00026     double x=in[i].x();
00027     double y=in[i].y();
00028     double xnew=x+tx;
00029     double ynew=y+ty;
00030     pnew.push_back(vgl_point_2d<double>(xnew,ynew));
00031   }
00032   return pnew;
00033 }
00034 
00035 vcl_vector<vgl_point_2d<double> > scale_2d(vcl_vector<vgl_point_2d<double> > in, double s)
00036 {
00037   vcl_vector< vgl_point_2d<double> >  pnew;
00038   for (unsigned i=0;i<in.size();i++)
00039   {
00040     double x=in[i].x();
00041     double y=in[i].y();
00042     double xnew=x*s;
00043     double ynew=y*s;
00044     pnew.push_back(vgl_point_2d<double>(xnew,ynew));
00045   }
00046   return pnew;
00047 }
00048 
00049 #if 0 // commented out
00050 vcl_vector<vgl_point_2d<double> > flip_y(vcl_vector<vgl_point_2d<double> > in)
00051 {
00052   vcl_vector< vgl_point_2d<double> >  pnew;
00053   for (unsigned i=0;i<in.size();i++)
00054   {
00055     double x=in[i].x();
00056     double y=in[i].y();
00057     double xnew=-x;
00058     double ynew=y;
00059     pnew.push_back(vgl_point_2d<double>(xnew,ynew));
00060   }
00061   return pnew;
00062 }
00063 
00064 vcl_vector<vgl_point_2d<double> > flip_x(vcl_vector<vgl_point_2d<double> > in)
00065 {
00066   vcl_vector< vgl_point_2d<double> >  pnew;
00067   for (unsigned i=0;i<in.size();i++)
00068   {
00069     double x=in[i].x();
00070     double y=in[i].y();
00071     double xnew=x;
00072     double ynew=-y;
00073     pnew.push_back(vgl_point_2d<double>(xnew,ynew));
00074   }
00075   return pnew;
00076 }
00077 #endif // 0
00078 
00079 vcl_vector<vgl_point_2d<double> >  recurse_peano_curve(unsigned level)
00080 {
00081   vcl_vector<vgl_point_2d<double> >  p;
00082   if (level==0)
00083   {
00084     p.push_back(vgl_point_2d<double>(0,0));
00085     return p;
00086   }
00087   vcl_vector<vgl_point_2d<double> >  p1,p2;
00088   p1=recurse_peano_curve(level-1);
00089   vcl_reverse(p1.begin(),p1.end());
00090   p2=trans_2d(rotate_2d(p1,vnl_math::pi_over_2),-1,-1);
00091 
00092   p.insert(p.end(),p2.begin(),p2.end());
00093 
00094   p1=recurse_peano_curve(level-1);
00095   p2=trans_2d(p1,-1,1);
00096   p.insert(p.end(),p2.begin(),p2.end());
00097 
00098   p1=recurse_peano_curve(level-1);
00099   p2=trans_2d(p1,1,1);
00100   p.insert(p.end(),p2.begin(),p2.end());
00101 
00102   p1=recurse_peano_curve(level-1);
00103   vcl_reverse(p1.begin(),p1.end());
00104   p2=trans_2d(rotate_2d(p1,-vnl_math::pi_over_2),1,-1);
00105   p.insert(p.end(),p2.begin(),p2.end());
00106 
00107   return scale_2d(p, 0.5);
00108 }
00109 
00110 //: peano_curve on cube of length 2
00111 vcl_vector<vgl_point_3d<double> >  peano_curve_on_cube(unsigned level)
00112 {
00113   vcl_vector<vgl_point_2d<double> > local_peano=recurse_peano_curve(level);
00114 
00115   // 6 faces
00116   vcl_vector<vgl_point_3d<double> > pc;
00117 
00118   // high y face
00119   vcl_vector<vgl_point_3d<double> > high_y;
00120   for (unsigned i=0;i<local_peano.size();i++)
00121     high_y.push_back(vgl_point_3d<double>(local_peano[i].x(),1,local_peano[i].y()));
00122   // high x face
00123   vcl_vector<vgl_point_3d<double> > high_x;
00124   for (unsigned i=0;i<local_peano.size();i++)
00125      high_x.push_back(vgl_point_3d<double>(1,-local_peano[i].x(),local_peano[i].y()));
00126   // low z face
00127   vcl_vector<vgl_point_3d<double> > low_z;
00128   for (int i=local_peano.size()-1;i>=0;i--)
00129      low_z.push_back(vgl_point_3d<double>(local_peano[i].x(),local_peano[i].y(),-1));
00130   // low x face
00131   vcl_vector<vgl_point_3d<double> > low_x;
00132   for (int i=local_peano.size()-1;i>=0;i--)
00133      low_x.push_back(vgl_point_3d<double>(-1,local_peano[i].y(),-local_peano[i].x()));
00134   // High z face
00135   vcl_vector<vgl_point_3d<double> > high_z;
00136   for (int i=local_peano.size()-1;i>=0;i--)
00137     high_z.push_back(vgl_point_3d<double>(-local_peano[i].x(),local_peano[i].y(),1));
00138   // Low y face
00139   vcl_vector<vgl_point_3d<double> > low_y;
00140   for (int i=local_peano.size()-1;i>=0;i--)
00141     low_y.push_back(vgl_point_3d<double>(local_peano[i].x(),-1,-local_peano[i].y()));
00142 
00143   pc.insert(pc.end(),high_y.begin(),high_y.end());
00144   pc.insert(pc.end(),high_x.begin(),high_x.end());
00145   pc.insert(pc.end(),low_z.begin(),low_z.end());
00146   pc.insert(pc.end(),low_x.begin(),low_x.end());
00147   pc.insert(pc.end(),high_z.begin(),high_z.end());
00148   pc.insert(pc.end(),low_y.begin(),low_y.end());
00149 
00150   return pc;
00151 }