Skip to content

Commit a815b73

Browse files
committed
Fix null reference exception when drawing empty geometries (null Box).
1 parent ff1bc1e commit a815b73

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

Visual_Studio_2017/GraphicalDebugging/ExpressionDrawer.cs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,12 @@ private static Geometry.Interval RelativeEnvelopeLon(Geometry.IRandomAccessRange
7373

7474
if (points.Count < 1)
7575
{
76-
result = new Geometry.Interval();
77-
Geometry.AssignInverse(result);
76+
result = Geometry.InversedInterval();
7877
return result;
7978
}
8079

8180
double x0 = points[0][0];
82-
result = new Geometry.Interval(x0);
81+
result = new Geometry.Interval(x0, x0);
8382

8483
int count = points.Count + (closed ? 1 : 0);
8584
for (int ii = 1; ii < count; ++ii)
@@ -582,7 +581,7 @@ public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
582581
}
583582

584583
if (box == null)
585-
Geometry.AssignInverse(box);
584+
box = Geometry.InversedBox();
586585

587586
return box;
588587
}
@@ -623,7 +622,7 @@ public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
623622
}
624623

625624
if (box == null)
626-
Geometry.AssignInverse(box);
625+
box = Geometry.InversedBox();
627626

628627
return box;
629628
}
@@ -664,7 +663,7 @@ public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
664663
}
665664

666665
if (box == null)
667-
Geometry.AssignInverse(box);
666+
box = Geometry.InversedBox();
668667

669668
return box;
670669
}
@@ -706,7 +705,7 @@ public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
706705
}
707706

708707
if (result == null)
709-
Geometry.AssignInverse(result);
708+
result = Geometry.InversedBox();
710709

711710
return result;
712711
}
@@ -854,8 +853,7 @@ public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
854853
{
855854
// NOTE: traits == null
856855

857-
Geometry.Box box = new Geometry.Box();
858-
Geometry.AssignInverse(box);
856+
Geometry.Box box = Geometry.InversedBox();
859857

860858
if (values.Count > 0)
861859
Geometry.Expand(box, new Point(0.0, 0.0));
@@ -955,8 +953,7 @@ public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
955953
// NOTE: traits == null
956954
// so don't return points.Aabb(traits, calculateEnvelope)
957955

958-
Geometry.Box box = new Geometry.Box();
959-
Geometry.AssignInverse(box);
956+
Geometry.Box box = Geometry.InversedBox();
960957

961958
for (int i = 0; i < points.Count; ++i)
962959
Geometry.Expand(box, points[i]);
@@ -1075,8 +1072,7 @@ public void Draw(Geometry.Box box, Graphics graphics, Settings settings, Geometr
10751072

10761073
public Geometry.Box Aabb(Geometry.Traits traits, bool calculateEnvelope)
10771074
{
1078-
Geometry.Box box = new Geometry.Box();
1079-
Geometry.AssignInverse(box);
1075+
Geometry.Box box = Geometry.InversedBox();
10801076

10811077
for (int i = 0; i < turns.Count; ++i)
10821078
{
@@ -1210,8 +1206,7 @@ static Geometry.Box Draw(Graphics graphics, bool ignoreTraits,
12101206
if (drawables.Length != traits.Length || drawables.Length != settings.Length)
12111207
throw new ArgumentOutOfRangeException("drawables.Length, traits.Length, settings.Length");
12121208

1213-
Geometry.Box box = new Geometry.Box();
1214-
Geometry.AssignInverse(box);
1209+
Geometry.Box box = Geometry.InversedBox();
12151210

12161211
int drawnCount = 0;
12171212
int count = drawables.Length;

Visual_Studio_2017/GraphicalDebugging/Geometry.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,6 @@ public class Interval
112112
public Interval()
113113
{ }
114114

115-
public Interval(double v)
116-
{
117-
Min = v;
118-
Max = v;
119-
}
120-
121115
public Interval(double mi, double ma)
122116
{
123117
Min = mi;
@@ -302,6 +296,20 @@ public static void AssignInverse(Interval i)
302296
i.Max = double.MinValue;
303297
}
304298

299+
public static Box InversedBox()
300+
{
301+
Box result = new Box();
302+
AssignInverse(result);
303+
return result;
304+
}
305+
306+
public static Interval InversedInterval()
307+
{
308+
Interval result = new Interval();
309+
AssignInverse(result);
310+
return result;
311+
}
312+
305313
public static Box Aabb(Point p1, Point p2, Unit unit)
306314
{
307315
if (unit == Unit.None)
@@ -327,24 +335,20 @@ public static Box AabbAngle(Point p1, Point p2, Unit unit)
327335

328336
public static Box Aabb(IRandomAccessRange<Point> points, bool closed, Unit unit)
329337
{
330-
Box result = new Box();
331-
332338
if (points.Count < 1)
333339
{
334-
AssignInverse(result);
335-
return result;
340+
return InversedBox();
336341
}
337342

338343
Point p1 = points[0];
339344
if (points.Count < 2)
340345
{
341346
// NOTE: unsafe, if this Box is modified then the original points will be modified as well
342-
result = new Box(p1, p1);
343-
return result;
347+
return new Box(p1, p1);
344348
}
345349
Point p2 = points[1];
346350

347-
result = Aabb(p1, p2, unit);
351+
Box result = Aabb(p1, p2, unit);
348352
int count = points.Count + (closed ? 1 : 0);
349353
for (int i = 2; i < count; ++i)
350354
{
@@ -535,9 +539,7 @@ public static Box Envelope(IRandomAccessRange<Point> range, bool closed, Traits
535539
{
536540
if (range.Count <= 0)
537541
{
538-
Box result = new Box();
539-
AssignInverse(result);
540-
return result;
542+
return InversedBox();
541543
}
542544
else if (range.Count <= 1)
543545
{

Visual_Studio_2017/GraphicalDebugging/GeometryWatchControl.xaml.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,13 @@ private void UpdateItems(bool load, int modified_index = -1)
313313
{
314314
try
315315
{
316+
// TODO: Unify the loading of empty geometries.
317+
// For empty linestring the coordinate system name is drawn
318+
// however for empty multilinestring it is not. The reason
319+
// is that in the latter case null drawable and traits are
320+
// returned becasue traits are loaded from an element
321+
// (linestring) but there are no elements since the
322+
// multi-geometry is empty.
316323
ExpressionDrawer.IDrawable d = null;
317324
Geometry.Traits t = null;
318325
ExpressionLoader.Load(names[i], ExpressionLoader.OnlyGeometries, out t, out d);

0 commit comments

Comments
 (0)