core/vgl/vgl_triangle_test.txx
Go to the documentation of this file.
00001 // This is core/vgl/vgl_triangle_test.txx
00002 #ifndef vgl_triangle_test_txx_
00003 #define vgl_triangle_test_txx_
00004 //:
00005 // \file
00006 // \author fsm
00007 
00008 #include "vgl_triangle_test.h"
00009 
00010 template <class T>
00011 T vgl_triangle_test_discriminant(T x1, T y1,
00012                                  T x2, T y2,
00013                                  T x3, T y3)
00014 {
00015   return x1*(y2-y3) - x2*(y1-y3) + x3*(y1-y2);
00016 }
00017 
00018 template <class T>
00019 bool vgl_triangle_test_inside(T x1, T y1,
00020                               T x2, T y2,
00021                               T x3, T y3,
00022                               T x , T y )
00023 {
00024   T det123 = vgl_triangle_test_discriminant(x1, y1,   x2, y2,   x3, y3);
00025   T detA23 = vgl_triangle_test_discriminant( x,  y,   x2, y2,   x3, y3);
00026   T det1A3 = vgl_triangle_test_discriminant(x1, y1,    x,  y,   x3, y3);
00027   T det12A = vgl_triangle_test_discriminant(x1, y1,   x2, y2,    x,  y);
00028 
00029   if (det123 > 0)
00030     return detA23>=0 && det1A3>=0 && det12A>=0;
00031 
00032   else if (det123 < 0)
00033     return detA23<=0 && det1A3<=0 && det12A<=0;
00034 
00035   else // det123 == 0 or NaN
00036     // degenerate triangle. ignore for now.
00037     return false;
00038 }
00039 
00040 //----------------------------------------
00041 
00042 #undef VGL_TRIANGLE_TEST_INSTANTIATE
00043 #define VGL_TRIANGLE_TEST_INSTANTIATE(T) \
00044 template T    vgl_triangle_test_discriminant(T, T, T, T, T, T); \
00045 template bool vgl_triangle_test_inside(T, T, T, T, T, T, T, T)
00046 
00047 #endif // vgl_triangle_test_txx_