Skip to content

Commit 8703bad

Browse files
committed
Fix calculation of bounding boxes of non-cartesian geometries.
1 parent fbdf156 commit 8703bad

File tree

2 files changed

+21
-21
lines changed

2 files changed

+21
-21
lines changed

Visual_Studio_2015/GraphicalDebugging/ExpressionDrawer.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -341,11 +341,11 @@ private static void DrawLinestring(Geometry.Linestring linestring, Geometry.Box
341341
}
342342
}
343343

344-
private static Geometry.Box AabbRange(Geometry.IRandomAccessRange<Geometry.Point> rng, Geometry.Traits traits, bool calculateEnvelope)
344+
private static Geometry.Box AabbRange(Geometry.IRandomAccessRange<Geometry.Point> rng, bool closed, Geometry.Traits traits, bool calculateEnvelope)
345345
{
346346
return calculateEnvelope
347-
? Geometry.Envelope(rng, traits)
348-
: Geometry.Aabb(rng, traits.Unit);
347+
? Geometry.Envelope(rng, closed, traits)
348+
: Geometry.Aabb(rng, closed, traits.Unit);
349349
}
350350

351351
public class Linestring : Geometry.Linestring, IDrawable
@@ -357,7 +357,7 @@ public void Draw(Geometry.Box box, Graphics graphics, Settings settings, Geometr
357357

358358
public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
359359
{
360-
return AabbRange(this, traits, calculateEnvelope);
360+
return AabbRange(this, false, traits, calculateEnvelope);
361361
}
362362
}
363363

@@ -397,7 +397,7 @@ public void Draw(Geometry.Box box, Graphics graphics, Settings settings, Geometr
397397

398398
public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
399399
{
400-
return AabbRange(this, traits, calculateEnvelope);
400+
return AabbRange(this, true, traits, calculateEnvelope);
401401
}
402402
}
403403

@@ -459,11 +459,11 @@ private static void DrawPolygon(Geometry.Polygon polygon, Geometry.Box box, Grap
459459

460460
private static Geometry.Box AabbPolygon(Geometry.Polygon poly, Geometry.Traits traits, bool calculateEnvelope)
461461
{
462-
Geometry.Box result = AabbRange(poly.Outer, traits, calculateEnvelope);
462+
Geometry.Box result = AabbRange(poly.Outer, true, traits, calculateEnvelope);
463463

464464
foreach (Geometry.Ring inner in poly.Inners)
465465
{
466-
Geometry.Box aabb = AabbRange(inner, traits, calculateEnvelope);
466+
Geometry.Box aabb = AabbRange(inner, true, traits, calculateEnvelope);
467467
if (calculateEnvelope)
468468
Geometry.Expand(result, aabb, traits);
469469
else
@@ -556,7 +556,7 @@ public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
556556

557557
for (int i = 0; i < this.Count; ++i)
558558
{
559-
Geometry.Box ls_box = AabbRange(this[i], traits, calculateEnvelope);
559+
Geometry.Box ls_box = AabbRange(this[i], false, traits, calculateEnvelope);
560560

561561
if (box == null)
562562
box = ls_box;

Visual_Studio_2015/GraphicalDebugging/Geometry.cs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ public static Box AabbAngle(Point p1, Point p2, Unit unit)
316316
return result;
317317
}
318318

319-
public static Box Aabb(IRandomAccessRange<Point> points, Unit unit)
319+
public static Box Aabb(IRandomAccessRange<Point> points, bool closed, Unit unit)
320320
{
321321
Box result = new Box();
322322

@@ -335,13 +335,12 @@ public static Box Aabb(IRandomAccessRange<Point> points, Unit unit)
335335
}
336336
Point p2 = points[1];
337337

338-
// NOTE: This does not take into account the closing segment
339-
340338
result = Aabb(p1, p2, unit);
341-
for (int i = 2; i < points.Count; ++i)
339+
int count = points.Count + (closed ? 1 : 0);
340+
for (int i = 2; i < count; ++i)
342341
{
343342
p1 = p2;
344-
p2 = points[i];
343+
p2 = points[i % points.Count];
345344
Box b = Aabb(p1, p2, unit);
346345
Expand(result, b);
347346
}
@@ -365,7 +364,7 @@ public static Box Aabb(Segment seg, Traits traits)
365364
}
366365
public static Box Aabb(Linestring linestring, Traits traits)
367366
{
368-
return Aabb(linestring, traits.Unit);
367+
return Aabb(linestring, false, traits.Unit);
369368
}
370369

371370
public static bool IntersectsX(Box box, double x)
@@ -523,7 +522,7 @@ public static void Expand<CS>(Box<CS, Degree> box, Point<CS, Degree> p)
523522
if (p[1] > box.Max[1]) box.Max[1] = p[1];
524523
}*/
525524

526-
public static Box Envelope(IRandomAccessRange<Point> range, Traits traits)
525+
public static Box Envelope(IRandomAccessRange<Point> range, bool closed, Traits traits)
527526
{
528527
if (range.Count <= 0)
529528
{
@@ -537,10 +536,11 @@ public static Box Envelope(IRandomAccessRange<Point> range, Traits traits)
537536
}
538537
else
539538
{
539+
int count = range.Count + (closed ? 1 : 0);
540540
Box result = Envelope(range[0], range[1], traits);
541-
for (int i = 2; i < range.Count; ++i)
541+
for (int i = 2; i < count; ++i)
542542
{
543-
Box b = Envelope(range[i], range[i - 1], traits);
543+
Box b = Envelope(range[i % range.Count], range[i - 1], traits);
544544
Expand(result, b, traits);
545545
}
546546
return result;
@@ -578,14 +578,14 @@ public static void ExpandAngle(Box box, Box b, Unit unit)
578578
if (left_dist >= 0 && right_dist >= 0)
579579
{
580580
if (left_dist < right_dist)
581-
box.Min[0] = xmin2;
581+
box.Min[0] -= left_dist;
582582
else
583-
box.Max[0] = xmax2;
583+
box.Max[0] += right_dist;
584584
}
585585
else if (left_dist >= 0)
586-
box.Min[0] = xmin2;
586+
box.Min[0] -= left_dist;
587587
else if (right_dist >= 0)
588-
box.Max[0] = xmax2;
588+
box.Max[0] += right_dist;
589589

590590
if (box.Max[0] < box.Min[0])
591591
box.Max[0] += twoPi;

0 commit comments

Comments
 (0)