From 1a8fd626803635c7edf0647fbdb4be30d32572aa Mon Sep 17 00:00:00 2001 From: Tim Moore Date: Mon, 19 Jan 2009 00:04:59 +0100 Subject: [PATCH] Protect against division by zero in QuadTreeBuilder This could only happen when there's one leaf in the tree, or all the objects happen to have the same position. Noticed by Csaba Halaz --- simgear/scene/util/QuadTreeBuilder.hxx | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/simgear/scene/util/QuadTreeBuilder.hxx b/simgear/scene/util/QuadTreeBuilder.hxx index 2c5c4bf5..62734e3b 100644 --- a/simgear/scene/util/QuadTreeBuilder.hxx +++ b/simgear/scene/util/QuadTreeBuilder.hxx @@ -80,10 +80,14 @@ public: { using namespace osg; const Vec3 center(_getLocalCoords(obj)); - int x = (int)(_dimension * (center.x() - _min.x()) + int x = 0; + if (_max.x() != _min.x()) + x = (int)(_dimension * (center.x() - _min.x()) / (_max.x() - _min.x())); x = clampTo(x, 0, (_dimension - 1)); - int y = (int)(_dimension * (center.y() - _min.y()) + int y = 0; + if (_max.y() != _min.y()) + y = (int)(_dimension * (center.y() - _min.y()) / (_max.y() - _min.y())); y = clampTo(y, 0, (_dimension -1)); _addLeafObject(_leaves(y, x), obj);