]> git.mxchange.org Git - simgear.git/blobdiff - simgear/math/SGMisc.hxx
Canvas: fix element mouse hit detection with OSG 3.3.2.
[simgear.git] / simgear / math / SGMisc.hxx
index 0f57edf17713d8cef1a9fb9c181acb5591366b48..abf162d819e5ab285c397a2d8330077ec4097802 100644 (file)
@@ -41,6 +41,30 @@ public:
   static T clip(const T& a, const T& _min, const T& _max)
   { return max(_min, min(_max, a)); }
 
+
+  /// Add two (integer) values taking care of overflows.
+  static T addClipOverflow(T a, T b)
+  {
+    if( b > 0 )
+    {
+      if( SGLimits<T>::max() - b < a )
+        return SGLimits<T>::max();
+    }
+    else
+    {
+      if( SGLimits<T>::min() - b > a )
+        return SGLimits<T>::min();
+    }
+
+    return a + b;
+  }
+
+  /// Add two (integer) values in place, taking care of overflows.
+  static T& addClipOverflowInplace(T& a, T b)
+  {
+    return a = addClipOverflow(a, b);
+  }
+
   /**
    * Seek a variable towards a target value with given rate and timestep
    *