contrib/brl/bbas/bvgl/bvgl_triangle_interpolation_iterator.txx
Go to the documentation of this file.
00001 #ifndef bvgl_triangle_interpolation_iterator_txx_
00002 #define bvgl_triangle_interpolation_iterator_txx_
00003 //:
00004 // \file
00005 
00006 #include "bvgl_triangle_interpolation_iterator.h"
00007 
00008 #include <vnl/algo/vnl_determinant.h>
00009 #include <vcl_vector.h>
00010 #include <vcl_cmath.h>
00011 #include <vcl_algorithm.h>
00012 #include <vcl_iostream.h>
00013 
00014 
00015 //: constructor
00016 template<class T>
00017 bvgl_triangle_interpolation_iterator<T>::bvgl_triangle_interpolation_iterator(double *verts_x, double *verts_y, T *vals, unsigned int v0, unsigned int v1, unsigned int v2)
00018 : tri_it_()
00019 {
00020   tri_it_.a.x = verts_x[v0]  - 0.5;
00021   tri_it_.a.y = verts_y[v0]  - 0.5;
00022   tri_it_.b.x = verts_x[v1]  - 0.5;
00023   tri_it_.b.y = verts_y[v1]  - 0.5;
00024   tri_it_.c.x = verts_x[v2]  - 0.5;
00025   tri_it_.c.y = verts_y[v2]  - 0.5;
00026 
00027 
00028   // compute s0, s1, s2 such that  val = s0*x + s1*y + s2 for any point within the triangle
00029   double Acol0[] = {tri_it_.a.x + 0.5, tri_it_.b.x + 0.5, tri_it_.c.x + 0.5};
00030   double Acol1[] = {tri_it_.a.y + 0.5, tri_it_.b.y + 0.5, tri_it_.c.y + 0.5};
00031   double Acol2[] = {1.0, 1.0, 1.0};
00032 
00033   double Z[] = {vals[v0], vals[v1], vals[v2]};
00034 
00035   double detA = vnl_determinant(Acol0, Acol1, Acol2);
00036   s0_ = vnl_determinant(Z, Acol1, Acol2) / detA;
00037   s1_ = vnl_determinant(Acol0, Z, Acol2) / detA;
00038   s2_ = vnl_determinant(Acol0, Acol1, Z) / detA;
00039 
00040   //vcl_cout << "s0 = " << s0_ <<"  s1 = " << s1_ << " s2 = " << s2_ << vcl_endl;
00041 }
00042 
00043 
00044 //: Resets the scan iterator to before the first scan line
00045 //  After calling this function, next() needs to be called before
00046 //  startx() and endx() form a valid scan line.
00047 template<class T>
00048 void bvgl_triangle_interpolation_iterator<T>::reset()
00049 {
00050   tri_it_.reset();
00051 }
00052 
00053 //: Tries to move to the next scan line.
00054 //  Returns false if there are no more scan lines.
00055 template<class T>
00056 bool bvgl_triangle_interpolation_iterator<T>::next()
00057 {
00058   return tri_it_.next();
00059 }
00060 
00061 //: y-coordinate of the current scan line.
00062 template<class T>
00063 int bvgl_triangle_interpolation_iterator<T>::scany() const
00064 {
00065   return tri_it_.scany();
00066 }
00067 
00068 //: Returns starting x-value of the current scan line.
00069 //  startx() should be smaller than endx(), unless the scan line is empty
00070 template<class T>
00071 int bvgl_triangle_interpolation_iterator<T>::startx() const
00072 {
00073   return tri_it_.startx();
00074 }
00075 
00076 //: Returns ending x-value of the current scan line.
00077 //  endx() should be larger than startx(), unless the scan line is empty
00078 template<class T>
00079 int  bvgl_triangle_interpolation_iterator<T>::endx() const
00080 {
00081   return tri_it_.endx() + 1;
00082 }
00083 
00084 
00085 //: returns the interpolated value at location x in the current scanline
00086 template<class T>
00087 T bvgl_triangle_interpolation_iterator<T>::value_at(int x)
00088 {
00089   return (T)((s0_*(x+0.5) + s1_*(tri_it_.scany()+0.5) + s2_));
00090 }
00091 
00092 
00093 #define BVGL_TRIANGLE_INTERPOLATION_ITERATOR_INSTANTIATE(T) \
00094   template class bvgl_triangle_interpolation_iterator<T >
00095 
00096 
00097 #endif