From caad1edabf755c2ef8e00f94f39a8b4c524012b4 Mon Sep 17 00:00:00 2001
From: Robert Adams
Date: Thu, 17 Jan 2013 14:44:54 -0800
Subject: Add utility function to clamp a vector to a maximum magnitude.

---
 OpenSim/Framework/Util.cs | 12 ++++++++++++
 1 file changed, 12 insertions(+)

(limited to 'OpenSim')

diff --git a/OpenSim/Framework/Util.cs b/OpenSim/Framework/Util.cs
index f511494..f6c9d15 100644
--- a/OpenSim/Framework/Util.cs
+++ b/OpenSim/Framework/Util.cs
@@ -299,6 +299,18 @@ namespace OpenSim.Framework
                 x;
         }
 
+        // Clamp the maximum magnitude of a vector
+        public static Vector3 ClampV(Vector3 x, float max)
+        {
+            Vector3 ret = x;
+            float lenSq = x.LengthSquared();
+            if (lenSq > (max * max))
+            {
+                x = x / x.Length() * max;
+            }
+            return x;
+        }
+
         // Inclusive, within range test (true if equal to the endpoints)
         public static bool InRange<T>(T x, T min, T max)
             where T : IComparable<T>
-- 
cgit v1.1