From 9966161704ee76bdd96436c4169bc3de2c6884e1 Mon Sep 17 00:00:00 2001
From: abv <abv@opencascade.com>
Date: Sun, 6 Sep 2015 16:46:08 +0300
Subject: [PATCH] 0026647: BRepTools::UVBounds() computes zero range by V

Calculation of bounding box on conic 2d curves corrected to work properly if specified parametric range is in negative values.

Test case added
---
 src/BndLib/BndLib_Add2dCurve.cxx | 45 +++++++-------------------------
 tests/bugs/modalg_6/bug26647     | 17 ++++++++++++
 2 files changed, 27 insertions(+), 35 deletions(-)
 create mode 100644 tests/bugs/modalg_6/bug26647

diff --git a/src/BndLib/BndLib_Add2dCurve.cxx b/src/BndLib/BndLib_Add2dCurve.cxx
index e6092da883..eeb9025536 100644
--- a/src/BndLib/BndLib_Add2dCurve.cxx
+++ b/src/BndLib/BndLib_Add2dCurve.cxx
@@ -580,7 +580,7 @@ void BndLib_Box2dCurve::Compute(const Handle(Geom2d_Conic)& aConic2D,
 				Bnd_Box2d& aBox2D)
 {
   Standard_Integer i, aNbT;
-  Standard_Real pT[10], aT, aTwoPI, aT1x, aT2x, dT, aT1z, aT2z, aEps;
+  Standard_Real pT[10], aT, aTwoPI, dT, aEps;
   gp_Pnt2d aP2D;
   //
   aNbT=Compute(aConic2D, aType, pT);
@@ -601,53 +601,28 @@ void BndLib_Box2dCurve::Compute(const Handle(Geom2d_Conic)& aConic2D,
   aTwoPI=2.*M_PI;
   dT=aT2-aT1;
   //
-  aT1z=aT1;
-  aT2z=aT2;
-  if (aT1z>=aTwoPI) {
-    aT1z=AdjustToPeriod(aT1z, aTwoPI);
-    aT2z=aT1z+dT;
-  }
+  Standard_Real aT1z = AdjustToPeriod (aT1, aTwoPI);
   if (fabs(aT1z)<aEps) {
     aT1z=0.;
   }
   //
+  Standard_Real aT2z = aT1z + dT;
   if (fabs(aT2z-aTwoPI)<aEps) {
     aT2z=aTwoPI;
   }
-    //
+  //
   for (i=0; i<aNbT; ++i) {
-    aT=pT[i];
-    if (aT>=aT1z && aT<=aT2z) {
+    aT = pT[i];
+    // adjust aT to range [aT1z, aT1z + 2*PI]; note that pT[i] and aT1z
+    // are adjusted to range [0, 2*PI], but aT2z can be greater than 2*PI
+    aT = (aT < aT1z ? aT + aTwoPI : aT);
+    if (aT <= aT2z) {
       D0(aT, aP2D);
       aBox2D.Add(aP2D);
     }
   }
-  //
-  aT1x=AdjustToPeriod(aT1, aTwoPI);
-  aT2x=aT1x+dT;
-  //
-  if (aT1x < aTwoPI && aT2x > aTwoPI) {
-    aT1z=aT1x;
-    aT2z=aTwoPI;
-    for (i=0; i<aNbT; ++i) {
-      aT=pT[i];
-      if (aT>=aT1z && aT<=aT2z) {
-	D0(aT, aP2D);
-	aBox2D.Add(aP2D);
-      }
-    }
-    //
-    aT1z=0.;
-    aT2z=aT2x-aTwoPI;
-    for (i=0; i<aNbT; ++i) {
-      aT=pT[i];
-      if (aT>=aT1z && aT<=aT2z) {
-	D0(aT, aP2D);
-	aBox2D.Add(aP2D);
-      }
-    }
-  }
 }
+
 //=======================================================================
 //function : Compute
 //purpose  : 
diff --git a/tests/bugs/modalg_6/bug26647 b/tests/bugs/modalg_6/bug26647
new file mode 100644
index 0000000000..3661aed7c9
--- /dev/null
+++ b/tests/bugs/modalg_6/bug26647
@@ -0,0 +1,17 @@
+puts "##################################################"
+puts "0026647: BRepTools::UVBounds() computes zero range by V"
+puts "##################################################"
+
+# load and check faces
+restore [locate_data_file bug26627_face1.brep] f
+tolerance f
+checkshape f
+
+# get UV bounds
+set bounds [xbounds f]
+
+# check for expected result
+set expected [list {UMin -9.} {UMax 9.} {VMin -9.} {VMax 0.}]
+for {set i 0} {$i < 4} {incr i} {
+  checkreal "[lindex $expected $i 0]" [lindex $bounds $i] [lindex $expected $i 1] 1e-9 0.
+}