Re: [Boost-bugs] [Boost C++ Libraries] #5954: distance_pythagoras skips sqrt() step

Subject: Re: [Boost-bugs] [Boost C++ Libraries] #5954: distance_pythagoras skips sqrt() step
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2011-09-30 11:56:29


#5954: distance_pythagoras skips sqrt() step
--------------------------------------+-------------------------------------
  Reporter: mloskot | Owner: barendgehrels
      Type: Bugs | Status: new
 Milestone: To Be Determined | Component: geometry
   Version: Boost Development Trunk | Severity: Problem
Resolution: | Keywords: pythagoras,geos,douglas,peucker,algorithm
--------------------------------------+-------------------------------------

Comment (by mloskot):

 The patch has been proven incorrect as the sqrt is supposed to not to be
 called where I added it. However, I'm still confused why different
 strategy is selected in the simplify.

 Here is illustration of what strategy is actually called:

 {{{
 #include <boost/geometry.hpp>
 #include <boost/geometry/strategies/cartesian/distance_pythagoras.hpp>
 #include <boost/geometry/geometries/point_xy.hpp>
 using namespace boost::geometry;

 int main()
 {
   typedef model::d2::point_xy<double> point_xy;

   point_xy p1(0.0, 0.0);
   point_xy p2(5.0, 0.0);

   // 1) This is direct call to Pythagoras algo
   typedef strategy::distance::pythagoras<point_xy, point_xy, double>
 strategy1_type;
   strategy1_type strategy1;
   strategy1_type ::calculation_type d1 = strategy1.apply(p1, p2);

   // 2) This is what is effectively called by simplify
   typedef strategy::distance::comparable::pythagoras<point_xy, point_xy,
 double> strategy2_type;
   strategy2_type strategy2;
   strategy2_type::calculation_type d2 = strategy2.apply(p1, p2);

   return 0;
 }
 }}}

 The 1) calls all expected implementation stages:

 {{{
 1 ->
 boost::geometry::strategy::distance::comparable::pythagoras<...>::apply()
 [Note 1]
 2 ->-> boost::geometry::strategy::distance::pythagoras<...>::apply()
 3 ->->-> sqrt()
 }}}

 '''Note 1''', with the patch attached above, this will lead to double call
 to sqrt(), because the step 2 squares the result of step 1 (as expected),
 so the sqrt in step 1 is redundant and incorrect.

 The 2) execution path seems to be missing the step 2 from the call stack
 above leading to no call to sqrt() at all what yields partial/incorrect
 result:

 {{{
 1 ->
 boost::geometry::strategy::distance::comparable::pythagoras<...>::apply()
 [Note 2]
 3 ->->-> sqrt()
 }}}

 '''Note 2''', that's why I hardcoded sqrt call in this function directly,
 but it is not correct in big picture, as explained in '''Note 1''' above.

 I'm having troubles with following the selection of pythagoras vs
 comparable. I may not understand well what's happening in
 boost::geometry::strategy::distance::services. So, all my hope in Barend!

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/5954#comment:3>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:50:07 UTC