contrib/mul/mmn/mmn_order_cost.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 // \brief Functions to re-arrange cost data to match index rank
00004 // \author Tim Cootes
00005 
00006 #include "mmn_order_cost.h"
00007 #include <vcl_vector.h>
00008 
00009 //: (i0,i1,i2) gives ordering of v0,v1,v2
00010 //  Each i is {0,1,2}, indicating rank of equivalent v
00011 //  ie if i0=1, then v0 is the middle value
00012 //  if i1=2, then v1 is the largest value
00013 void mmn_get_order(unsigned v0, unsigned v1, unsigned v2,
00014                    unsigned& i0, unsigned& i1, unsigned& i2)
00015 {
00016   if (v0<v1)
00017   {
00018     if (v1<v2) { i0=0; i1=1; i2=2; return; }  // 0-1-2
00019     if (v2<v0) { i0=1; i1=2; i2=0; return; }  // 2-0-1
00020     i0=0; i1=2; i2=1; return;  // 0-2-1
00021   }
00022   if (v0<v2) { i0=1; i1=0; i2=2; return; }  // 1-0-2
00023   if (v2<v1) { i0=2; i1=1; i2=0; return; }  // 2-1-0
00024   i0=2; i1=0; i2=1; return;  // 1-2-0
00025 }
00026 
00027 //: (i0,i1,i2) gives rank of v0,v1,v2
00028 // I.e. v[i0] is smallest, v[i2] is largest
00029 void mmn_get_rank(unsigned v0, unsigned v1, unsigned v2,
00030                   unsigned& i0, unsigned& i1, unsigned& i2)
00031 {
00032   if (v0<v1)
00033   {
00034     if (v1<v2) { i0=0; i1=1; i2=2; return; }  // 0-1-2
00035     if (v2<v0) { i0=2; i1=0; i2=1; return; }  // 2-0-1
00036     i0=0; i1=2; i2=1; return;  // 0-2-1
00037   }
00038   if (v0<v2) { i0=1; i1=0; i2=2; return; }  // 1-0-2
00039   if (v2<v1) { i0=2; i1=1; i2=0; return; }  // 2-1-0
00040   i0=1; i1=2; i2=0; return;  // 1-2-0
00041 }
00042 
00043 //: Create new view of data, with axes re-arranged
00044 //  \a c(i1,i2,i3) is the value associated with i1 on node v1,
00045 //  i2 on node v2 etc.
00046 //  The i-axis of the returned image is associated with the node
00047 //  min(v1,v2,v3), the j axis with mid(v1,v2,v3) and the k with
00048 //  max(v1,v2,v3)
00049 //  The inverse operation is given by mmn_unorder_cost
00050 vil_image_view<double> mmn_order_cost(const vil_image_view<double>& c,
00051                                       unsigned v1, unsigned v2, unsigned v3)
00052 {
00053   unsigned i1,i2,i3;
00054   mmn_get_rank(v1,v2,v3, i1,i2,i3);
00055   vcl_vector<unsigned> n(3);
00056   n[0]=c.ni(); n[1]=c.nj(); n[2]=c.nplanes();
00057   vcl_vector<vcl_ptrdiff_t> step(3);
00058   step[0]=c.istep(); step[1]=c.jstep(); step[2]=c.planestep();
00059 
00060   return vil_image_view<double>(c.top_left_ptr(),
00061                                 n[i1],n[i2],n[i3],
00062                                 step[i1],step[i2],step[i3]);
00063 }
00064 
00065 //: Make new view of data, with axes re-arranged. Inverse of mmn_order_data
00066 //  The i-axis of c is associated with the node min(v1,v2,v3),
00067 //  the j axis with mid(v1,v2,v3) and the k with max(v1,v2,v3)
00068 //  Returns image r, where r(i1,i2,i3) is the value associated
00069 //  with i1 on node v1, i2 on node v2 etc.
00070 vil_image_view<double> mmn_unorder_cost(const vil_image_view<double>& c,
00071                                         unsigned v1, unsigned v2, unsigned v3)
00072 {
00073   unsigned i1,i2,i3;
00074   mmn_get_order(v1,v2,v3, i1,i2,i3);
00075   vcl_vector<unsigned> n(3);
00076   n[0]=c.ni(); n[1]=c.nj(); n[2]=c.nplanes();
00077   vcl_vector<vcl_ptrdiff_t> step(3);
00078   step[0]=c.istep(); step[1]=c.jstep(); step[2]=c.planestep();
00079 
00080   return vil_image_view<double>(c.top_left_ptr(),
00081                                 n[i1],n[i2],n[i3],
00082                                 step[i1],step[i2],step[i3]);
00083 }