00001 #ifndef OPENTISSUE_CORE_GEOMETRY_GEOMETRY_COMPUTE_PERPENDICULAR_LINE_POINT_H 00002 #define OPENTISSUE_CORE_GEOMETRY_GEOMETRY_COMPUTE_PERPENDICULAR_LINE_POINT_H 00003 // 00004 // OpenTissue Template Library 00005 // - A generic toolbox for physics-based modeling and simulation. 00006 // Copyright (C) 2008 Department of Computer Science, University of Copenhagen. 00007 // 00008 // OTTL is licensed under zlib: http://opensource.org/licenses/zlib-license.php 00009 // 00010 #include <OpenTissue/configuration.h> 00011 00012 #include <OpenTissue/core/math/math_value_traits.h> 00013 00014 00015 namespace OpenTissue 00016 { 00017 namespace geometry 00018 { 00019 00020 // 2007-07-24 kenny: Where is the unit-test for this new piece of code? 00021 // 2007-07-24 micky: Unit-test, are you serious? Ain't this a bit overkill? 00022 // 2007-07-24 kenny: Yes I am very serious... Too many times we have got into trouble by not making sure small simple pieces of code are not consistent and working (last time were when we re-factored vector3 class) 00023 // 2007-09-18 kenny: he he, just to add some salt to the wound, do you recall what happended with the ``clamp'' unit-test:-) 00024 // 2007-09-25 micky: I'll add the unit test ASAP. 00034 // 2007-07-23 kenny: why not drop second type argument and simply use typename vector3_type::value_type instead? 00035 // 2007-07-23 micky: can't really see how t will then be usable for fx. vector3<int>. 00036 // 2007-07-24 kenny: Right now you can actually have vector_type = vector3<double> and real_type = int. If the code is re-written to be 00037 // template<typename vector_type> 00038 // void compute_perpendicular_line_point( vector_type const & p0 00039 // , vector_type const & p1 00040 // , vector_type const & x 00041 // , typename vector_type::value_type & t 00042 // ) 00043 // then you are forcing the caller to use the same precision as whatever vector type he is calling. At least you will know that all the types inside your code are the same. 00044 // 00045 // 2007-07-24 micky: My last comment still stands, and frankly it's quite realistic. Besides if the caller uses int as a real type he's a victim of his own incompetence. 00046 // 2007-07-24 kenny: So why not make sure the compiler tells the caller that he is doing something stupid? 00047 // 2007-09-25 micky: Should we try to wrap up this one? I world like both template args to stay. They both are used implicitly from the caller. 00048 // 2007-07-26 kenny: Fine, but maybe you should add a note about this ``implicitly'' handling of types, either as source comments or as doxygen comments? Maybe this way it will be easy for others to track down future problems of implicit type conversions? 00049 template<typename vector_type, typename real_type> 00050 void compute_perpendicular_line_point( vector_type const & p0 00051 , vector_type const & p1 00052 , vector_type const & x 00053 , real_type & t 00054 ) 00055 { 00056 vector_type const v = p1-p0; 00057 real_type const vl2 = inner_prod(v, v); 00058 assert(vl2 > math::ValueTraits<real_type>::zero() || !"p0 and p1 does not define a line as they are coinciding!"); 00059 vector_type const d = p0-x; 00060 t = -inner_prod(d, v)/vl2; 00061 } 00062 00063 } // namespace geometry 00064 00065 } // namespace OpenTissue 00066 00067 // OPENTISSUE_CORE_GEOMETRY_GEOMETRY_COMPUTE_PERPENDICULAR_LINE_POINT_H 00068 #endif