contrib/mul/msm/msm_pose_maker.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \author Tim Cootes
00004 // \brief Compute a direction at each point, usually normal to curve
00005 
00006 #include "msm_pose_maker.h"
00007 #include <vsl/vsl_binary_loader.h>
00008 #include <vsl/vsl_vector_io.h>
00009 
00010 //=======================================================================
00011 
00012 //: Set up definitions of directions from the curves.
00013 void msm_pose_maker::set_from_curves(unsigned n_points,
00014                                      const msm_curves& curves)
00015 {
00016   end0_.resize(n_points,0);
00017   end1_.resize(n_points,0);
00018 
00019   for (unsigned j=0;j<curves.size();++j)
00020   {
00021     const msm_curve& curve=curves[j];
00022     unsigned nc=curve.size();
00023     // Tangent at point defined by previous and next points
00024     for (unsigned i=0;i<nc;++i)
00025     {
00026       if (defined(curve[i])) continue;
00027       end0_[curve[i]]=curve[(i+nc-1)%nc];
00028       end1_[curve[i]]=curve[(i+nc+1)%nc];
00029     }
00030     // For open curves, first point tangent is p0-p1,
00031     // last is p[nc-2],p[nc-1]
00032     if (curve.open())
00033     {
00034       if (defined(curve[0]))
00035         end0_[curve[0]]=curve[0];  // First point is p[0]-p[1]
00036       if (defined(curve[0]))
00037         end1_[curve[nc-1]]=curve[nc-1];
00038     }
00039   }
00040 }
00041 
00042 //: Compute a direction at each point, usually normal to curve.
00043 void msm_pose_maker::create_vectors(const msm_points& points,
00044                                     vcl_vector<vgl_vector_2d<double> >& dir) const
00045 {
00046   if (end0_.size()==0)
00047   {
00048     dir.resize(points.size());
00049     for (unsigned i=0;i<dir.size();++i)
00050       dir[i]=vgl_vector_2d<double>(1,0);
00051     return;
00052   }
00053 
00054   unsigned n_points=points.size();
00055   dir.resize(n_points);
00056   for (unsigned i=0;i<n_points;++i)
00057   {
00058     vgl_point_2d<double> p=points[i];
00059     if (end0_[i]==end1_[i])
00060     {
00061       dir[i]=vgl_vector_2d<double>(1,0);
00062     }
00063     else
00064     {
00065       vgl_vector_2d<double> t = points[end0_[i]]
00066                                  - points[end1_[i]];
00067       double L=t.length();
00068       if (L<=1e-8)
00069         dir[i]=vgl_vector_2d<double>(1,0);
00070       else
00071       {
00072         // Normal is (-t.y(),t.x())/L
00073         dir[i]=vgl_vector_2d<double>(-t.y()/L,t.x()/L);
00074       }
00075     }
00076   }
00077 }
00078 
00079 //=======================================================================
00080 
00081 void msm_pose_maker::print_summary(vcl_ostream& os) const
00082 {
00083   os<<" n_points: "<<end0_.size();
00084 }
00085 
00086 const static short version_no = 1;
00087 
00088 //: Save class to binary file stream
00089 void msm_pose_maker::b_write(vsl_b_ostream& bfs) const
00090 {
00091   vsl_b_write(bfs,version_no);
00092   vsl_b_write(bfs,end0_);
00093   vsl_b_write(bfs,end1_);
00094 }
00095 
00096 
00097 //: Load class from binary file stream
00098 void msm_pose_maker::b_read(vsl_b_istream& bfs)
00099 {
00100   short version;
00101   vsl_b_read(bfs,version);
00102   switch (version)
00103   {
00104     case (1):
00105       vsl_b_read(bfs,end0_);
00106       vsl_b_read(bfs,end1_);
00107       break;
00108     default:
00109       vcl_cerr << "msm_pose_maker::b_read() :\n"
00110                << "Unexpected version number " << version << vcl_endl;
00111       bfs.is().clear(vcl_ios::badbit); // Set an unrecoverable IO error on stream
00112       return;
00113   }
00114 }
00115 
00116 
00117 //: Binary file stream output operator for class reference
00118 void vsl_b_write(vsl_b_ostream& bfs, const msm_pose_maker& b)
00119 { b.b_write(bfs); }
00120 
00121 //: Binary file stream input operator for class reference
00122 void vsl_b_read(vsl_b_istream& bfs, msm_pose_maker& b)
00123 { b.b_read(bfs); }
00124 
00125 //: Stream output operator for class reference
00126 vcl_ostream& operator<<(vcl_ostream& os,const msm_pose_maker& b)
00127 {
00128   b.print_summary(os);
00129   return os;
00130 }
00131 
00132