Explorar el Código

Math.isqrt -> invSqrt + added Math functions

ncannasse hace 12 años
padre
commit
b951d176dd
Se han modificado 8 ficheros con 76 adiciones y 11 borrados
  1. 2 2
      h2d/Graphics.hx
  2. 1 1
      h2d/col/Point.hx
  3. 1 1
      h3d/Matrix.hx
  4. 2 2
      h3d/Quat.hx
  5. 5 1
      h3d/Vector.hx
  6. 1 1
      h3d/col/Point.hx
  7. 1 1
      h3d/col/Ray.hx
  8. 63 2
      hxd/Math.hx

+ 2 - 2
h2d/Graphics.hx

@@ -152,10 +152,10 @@ class Graphics extends Drawable {
 			var next = linePts[(i + 1) % linePts.length];
 			var nx1 = prev.y - p.y;
 			var ny1 = p.x - prev.x;
-			var ns1 = Math.isqrt(nx1 * nx1 + ny1 * ny1);
+			var ns1 = Math.invSqrt(nx1 * nx1 + ny1 * ny1);
 			var nx2 = p.y - next.y;
 			var ny2 = next.x - p.x;
-			var ns2 = Math.isqrt(nx2 * nx2 + ny2 * ny2);
+			var ns2 = Math.invSqrt(nx2 * nx2 + ny2 * ny2);
 			
 			var nx = (nx1 * ns1 + nx2 * ns2) * lineSize * 0.5;
 			var ny = (ny1 * ns1 + ny2 * ns2) * lineSize * 0.5;

+ 1 - 1
h2d/col/Point.hx

@@ -47,7 +47,7 @@ class Point {
 
 	public function normalize() {
 		var k = lengthSq();
-		if( k < Math.EPSILON ) k = 0 else k = Math.isqrt(k);
+		if( k < Math.EPSILON ) k = 0 else k = Math.invSqrt(k);
 		x *= k;
 		y *= k;
 	}

+ 1 - 1
h3d/Matrix.hx

@@ -85,7 +85,7 @@ class Matrix {
 		var cos1 = 1 - cos;
 		var x = -axis.x, y = -axis.y, z = -axis.z;
 		var xx = x * x, yy = y * y, zz = z * z;
-		var len = Math.isqrt(xx + yy + zz);
+		var len = Math.invSqrt(xx + yy + zz);
 		x *= len;
 		y *= len;
 		z *= len;

+ 2 - 2
h3d/Quat.hx

@@ -88,7 +88,7 @@ class Quat {
 			x = y = z = 0;
 			w = 1;
 		} else {
-			var m = len.isqrt();
+			var m = len.invSqrt();
 			x *= m;
 			y *= m;
 			z *= m;
@@ -162,7 +162,7 @@ class Quat {
 			return;
 		}
 		var halfTheta = cosHalfTheta.acos();
-		var invSinHalfTheta = (1 - cosHalfTheta * cosHalfTheta).isqrt();
+		var invSinHalfTheta = (1 - cosHalfTheta * cosHalfTheta).invSqrt();
 		if( invSinHalfTheta.abs() > 1e3 ) {
 			this.lerp(q1, q2, 0.5, true);
 			return;

+ 5 - 1
h3d/Vector.hx

@@ -56,7 +56,7 @@ class Vector {
 
 	public function normalize() {
 		var k = lengthSq();
-		if( k < hxd.Math.EPSILON ) k = 0 else k = k.isqrt();
+		if( k < hxd.Math.EPSILON ) k = 0 else k = k.invSqrt();
 		x *= k;
 		y *= k;
 		z *= k;
@@ -114,6 +114,10 @@ class Vector {
 		w = (c >>> 24) * s;
 	}
 	
+	public inline function toPoint() {
+		return new h3d.col.Point(x, y, z);
+	}
+	
 	public inline function toColor() {
 		return (Std.int(w.clamp() * 255) << 24) | (Std.int(x.clamp() * 255) << 16) | (Std.int(y.clamp() * 255) << 8) | Std.int(z.clamp() * 255);
 	}

+ 1 - 1
h3d/col/Point.hx

@@ -57,7 +57,7 @@ class Point {
 	
 	public function normalize() {
 		var k = x * x + y * y + z * z;
-		if( k < hxd.Math.EPSILON ) k = 0 else k = k.isqrt();
+		if( k < hxd.Math.EPSILON ) k = 0 else k = k.invSqrt();
 		x *= k;
 		y *= k;
 		z *= k;

+ 1 - 1
h3d/col/Ray.hx

@@ -16,7 +16,7 @@ class Ray {
 	
 	public function normalize() {
 		var l = lx * lx + ly * ly + lz * lz;
-		if( l < Math.EPSILON ) l = 0 else l = Math.isqrt(l);
+		if( l < Math.EPSILON ) l = 0 else l = Math.invSqrt(l);
 		lx *= l;
 		ly *= l;
 		lz *= l;

+ 63 - 2
hxd/Math.hx

@@ -16,7 +16,7 @@ class Math {
 		return std.Math.NEGATIVE_INFINITY;
 	}
 	
-	// round to 4 significant digits, eliminates <1e-10
+	// round to 4 significant digits, eliminates < 1e-10
 	public static function fmt( v : Float ) {
 		var neg;
 		if( v < 0 ) {
@@ -83,7 +83,7 @@ class Math {
 		return std.Math.sqrt(f);
 	}
 
-	public static inline function isqrt( f : Float ) {
+	public static inline function invSqrt( f : Float ) {
 		return 1. / sqrt(f);
 	}
 	
@@ -118,5 +118,66 @@ class Math {
 	public static inline function iclamp( v : Int, min : Int, max : Int ) {
 		return v < min ? min : (v > max ? max : v);
 	}
+
+	/**
+		Linear interpolation between two values. When k is 0 a is returned, when it's 1, b is returned.
+	**/
+	public inline static function lerp(a:Float, b:Float, k:Float) {
+		return a * (1 - k) + b * k;
+	}
+	
+	public inline static function bitCount(v:Int) {
+		var k = 0;
+		while( v != 0 ) {
+			k += v & 1;
+			v >>>= 1;
+		}
+		return k;
+	}
+	
+	public static inline function distanceSq( dx : Float, dy : Float, dz = 0. ) {
+		return dx * dx + dy * dy + dz * dz;
+	}
+	
+	public static inline function distance( dx : Float, dy : Float, dz = 0. ) {
+		return sqrt(distanceSq(dx,dy,dz));
+	}
+	
+	/**
+		Linear interpolation between two colors (ARGB).
+	**/
+	public static function colorLerp( c1 : Int, c2 : Int, k : Float ) {
+		var a1 = c1 >>> 24;
+		var r1 = (c1 >> 16) & 0xFF;
+		var g1 = (c1 >> 8) & 0xFF;
+		var b1 = c1 & 0xFF;
+		var a2 = c2 >>> 24;
+		var r2 = (c2 >> 16) & 0xFF;
+		var g2 = (c2 >> 8) & 0xFF;
+		var b2 = c2 & 0xFF;
+		var a = Std.int(a1 * (1-k) + a2 * k);
+		var r = Std.int(r1 * (1-k) + r2 * k);
+		var g = Std.int(g1 * (1-k) + g2 * k);
+		var b = Std.int(b1 * (1 - k) + b2 * k);
+		return (a << 24) | (r << 16) | (g << 8) | b;
+	}
+	
+	/*
+		Clamp an angle into the [-PI,+PI[ range. Can be used to measure the direction between two angles : if Math.angle(A-B) < 0 go left else go right.
+	*/
+	public static inline function angle( da : Float ) {
+		da %= PI * 2;
+		if( da > PI ) da -= 2 * PI else if( da <= -PI ) da += 2 * PI;
+		return da;
+	}
+	
+	/**
+		Move a towards b with a max increment. Return the new angle.
+	**/
+	public static inline function angleMove( a : Float, b : Float, max : Float ) {
+		var da = (b - a) % (PI * 2);
+		if( da > PI ) da -= 2 * PI else if( da <= -PI ) da += 2 * PI;
+		return if( da > -max && da < max ) b else a + (da < 0 ? -max : max);
+	}
 	
 }