contrib/mul/vimt/vimt_transform_util_2d.cxx
Go to the documentation of this file.
00001 #include "vimt_transform_util_2d.h"
00002 //:
00003 // \file
00004 
00005 #include <vcl_cassert.h>
00006 
00007 //: Compute similarity transform which maps the p1,p2 to q1,q2
00008 void vimt_calc_transform_2d( vimt_transform_2d& trans,
00009                              const vgl_point_2d<double>& p1,
00010                              const vgl_point_2d<double>& p2,
00011                              const vgl_point_2d<double>& q1,
00012                              const vgl_point_2d<double>& q2)
00013 {
00014   vgl_point_2d<double> pc = centre(p1,p2);
00015   vgl_point_2d<double> qc = centre(q1,q2);
00016   vgl_vector_2d<double> dp = p2-p1;
00017   vgl_vector_2d<double> dq = q2-q1;
00018 
00019   double dp2 = dp.x()*dp.x() + dp.y()*dp.y();
00020   assert(dp2>0);
00021 
00022   double a = (dp.x()*dq.x() + dp.y()*dq.y())/dp2;
00023   double b = (dp.x()*dq.y() - dp.y()*dq.x())/dp2;
00024 
00025   double tx = qc.x() - (a*pc.x()-b*pc.y());
00026   double ty = qc.y() - (b*pc.x()+a*pc.y());
00027 
00028   trans.set_similarity(vgl_point_2d<double>(a,b), vgl_point_2d<double>(tx,ty));
00029 }
00030 
00031 
00032 //: Transform 2d box
00033 void vimt_transform_util_2d( vgl_box_2d<double>& dest_box,
00034                              const vgl_box_2d<double>& src_box,
00035                              const vimt_transform_2d& trans )
00036 {
00037   vgl_point_2d<double> tl( src_box.min_x(), src_box.min_y() );
00038   vgl_point_2d<double> tl_pt= trans ( tl );
00039 
00040   vgl_point_2d<double> br( src_box.max_x(), src_box.max_y() );
00041   vgl_point_2d<double> br_pt= trans ( br );
00042 
00043   dest_box.set_min_point ( tl_pt );
00044   dest_box.set_max_point ( br_pt );
00045 }
00046 
00047 //: Transform vector of 2d pts
00048 void vimt_transform_util_2d( vcl_vector< vgl_point_2d<double> >& dest_pt_vec,
00049                              const vcl_vector< vgl_point_2d<double> >& src_pt_vec,
00050                              const vimt_transform_2d& trans )
00051 {
00052   int n= src_pt_vec.size();
00053   dest_pt_vec.resize(n);
00054   for (int i=0; i<n; ++i)
00055   {
00056     dest_pt_vec[i] = trans ( src_pt_vec[i] );
00057   }
00058 }