Boost C++ Libraries: Ticket #5954: distance_pythagoras skips sqrt() step https://svn.boost.org/trac10/ticket/5954 <p> A problem with <a class="ext-link" href="http://lists.osgeo.org/pipermail/ggl/2011-September/001533.html"><span class="icon">​</span>Polygon Douglas–Peucker simplification</a> results have been reported. </p> <p> There seems to be a bug in calculation of distance using Pythagoras Theorem. The problem is that the distance calculation is somehow omitting call to sqrt() leading to incomplete results. </p> <p> It may be a problem with dispatching and execution execution paths in distance_pythagoras.hpp, so this part needs to be thoroughly reviewed. </p> <p> Meanwhile, the patch attached provides a quick fix. The fix solves the reported problem, so the Boost.Geometry generates expected results. Also, the posted test case has been checked using GEOS and the patched distance_pythagoras generates results consistent with those obtained from GEOS. Means, the fix seems to be correct. </p> <p> But, it causes number of unit test failures, so I decided to not to commit it this fix. </p> <p> Barend, please get in touch when you will have a moment. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5954 Trac 1.4.3 Mateusz Loskot Wed, 28 Sep 2011 17:02:37 GMT attachment set https://svn.boost.org/trac10/ticket/5954 https://svn.boost.org/trac10/ticket/5954 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">distance_pythagoras-sqrt-fix.patch</span> </li> </ul> <p> Patch fixing missing sqrt() callin distance_pythagoras.hpp. Also fixes oordinates/points mismatch, trivial. </p> Ticket Mateusz Loskot Wed, 28 Sep 2011 17:05:57 GMT <link>https://svn.boost.org/trac10/ticket/5954#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5954#comment:1</guid> <description> <p> Also, please check FIXME comment <em>strategies/agnostic/simplify_douglas_peucker.hpp:148</em> (<a class="changeset" href="https://svn.boost.org/trac10/changeset/74600" title="[geometry] Added FIXME comment related to ticket #5954">r74600</a>) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Mateusz Loskot</dc:creator> <pubDate>Wed, 28 Sep 2011 17:18:23 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5954#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5954#comment:2</guid> <description> <p> The test program is now in the trunk, as <em>l01_multipolygon_simplify.cpp</em> example (<a class="changeset" href="https://svn.boost.org/trac10/changeset/74598" title="[geometry] Added quick example of DP simplifier fired against ...">r74598</a>) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Mateusz Loskot</dc:creator> <pubDate>Wed, 28 Sep 2011 17:19:13 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/5954 https://svn.boost.org/trac10/ticket/5954 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">l01_multipolygon_simplify.cpp.log</span> </li> </ul> <p> l01_multipolygon_simplify.cpp test run log </p> Ticket Mateusz Loskot Fri, 30 Sep 2011 11:56:28 GMT <link>https://svn.boost.org/trac10/ticket/5954#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5954#comment:3</guid> <description> <p> 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. </p> <p> Here is illustration of what strategy is actually called: </p> <pre class="wiki">#include &lt;boost/geometry.hpp&gt; #include &lt;boost/geometry/strategies/cartesian/distance_pythagoras.hpp&gt; #include &lt;boost/geometry/geometries/point_xy.hpp&gt; using namespace boost::geometry; int main() { typedef model::d2::point_xy&lt;double&gt; 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&lt;point_xy, point_xy, double&gt; 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&lt;point_xy, point_xy, double&gt; strategy2_type; strategy2_type strategy2; strategy2_type::calculation_type d2 = strategy2.apply(p1, p2); return 0; } </pre><p> The 1) calls all expected implementation stages: </p> <pre class="wiki">1 -&gt; boost::geometry::strategy::distance::comparable::pythagoras&lt;...&gt;::apply() [Note 1] 2 -&gt;-&gt; boost::geometry::strategy::distance::pythagoras&lt;...&gt;::apply() 3 -&gt;-&gt;-&gt; sqrt() </pre><p> <strong>Note 1</strong>, 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. </p> <p> 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: </p> <pre class="wiki">1 -&gt; boost::geometry::strategy::distance::comparable::pythagoras&lt;...&gt;::apply() [Note 2] 3 -&gt;-&gt;-&gt; sqrt() </pre><p> <strong>Note 2</strong>, that's why I hardcoded sqrt call in this function directly, but it is not correct in big picture, as explained in <strong>Note 1</strong> above. </p> <p> 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! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>anonymous</dc:creator> <pubDate>Thu, 06 Oct 2011 16:44:32 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5954#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5954#comment:4</guid> <description> <p> Is this related <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5730" title="#5730: Bugs: strategy selection error in the distance function (closed: fixed)">#5730</a> ? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Barend Gehrels</dc:creator> <pubDate>Thu, 06 Oct 2011 17:06:32 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5954#comment:5 https://svn.boost.org/trac10/ticket/5954#comment:5 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/74761" title="Fix ticket 5954, use strategy directly, not the comparable strategy ...">[74761]</a>) Fix ticket 5954, use strategy directly, not the comparable strategy (unless fixed otherwise) </p> Ticket