Browse Source

add squared epsilon for squared values comparison (#1176)

* add squared epsilon for squared values comparison

* remove squared epsilon comparison for ray distance and intersect

---------

Co-authored-by: lviguier <aiv4aiTh>
LeoVgr 1 year ago
parent
commit
ca41959cb0
10 changed files with 16 additions and 15 deletions
  1. 2 2
      h2d/col/Line.hx
  2. 2 2
      h2d/col/Point.hx
  3. 1 1
      h2d/col/Ray.hx
  4. 1 1
      h2d/col/RoundRect.hx
  5. 1 1
      h3d/Matrix.hx
  6. 3 3
      h3d/Quat.hx
  7. 2 2
      h3d/Vector.hx
  8. 2 2
      h3d/col/Point.hx
  9. 1 1
      h3d/col/Ray.hx
  10. 1 0
      hxd/Math.hx

+ 2 - 2
h2d/col/Line.hx

@@ -46,7 +46,7 @@ class Line {
 	**/
 	**/
 	public inline function intersect( l : Line ) {
 	public inline function intersect( l : Line ) {
 		var d = (p1.x - p2.x) * (l.p1.y - l.p2.y) - (p1.y - p2.y) * (l.p1.x - l.p2.x);
 		var d = (p1.x - p2.x) * (l.p1.y - l.p2.y) - (p1.y - p2.y) * (l.p1.x - l.p2.x);
-		if( hxd.Math.abs(d) < hxd.Math.EPSILON )
+		if( hxd.Math.abs(d) < hxd.Math.EPSILON2 )
 			return null;
 			return null;
 		var a = p1.x*p2.y - p1.y * p2.x;
 		var a = p1.x*p2.y - p1.y * p2.x;
 		var b = l.p1.x*l.p2.y - l.p1.y*l.p2.x;
 		var b = l.p1.x*l.p2.y - l.p1.y*l.p2.x;
@@ -60,7 +60,7 @@ class Line {
 	**/
 	**/
 	public inline function intersectWith( l : Line, pt : Point ) {
 	public inline function intersectWith( l : Line, pt : Point ) {
 		var d = (p1.x - p2.x) * (l.p1.y - l.p2.y) - (p1.y - p2.y) * (l.p1.x - l.p2.x);
 		var d = (p1.x - p2.x) * (l.p1.y - l.p2.y) - (p1.y - p2.y) * (l.p1.x - l.p2.x);
-		if( hxd.Math.abs(d) < hxd.Math.EPSILON )
+		if( hxd.Math.abs(d) < hxd.Math.EPSILON2 )
 			return false;
 			return false;
 		var a = p1.x*p2.y - p1.y * p2.x;
 		var a = p1.x*p2.y - p1.y * p2.x;
 		var b = l.p1.x*l.p2.y - l.p1.y*l.p2.x;
 		var b = l.p1.x*l.p2.y - l.p1.y*l.p2.x;

+ 2 - 2
h2d/col/Point.hx

@@ -103,7 +103,7 @@ class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 	**/
 	**/
 	public inline function normalize() {
 	public inline function normalize() {
 		var k = lengthSq();
 		var k = lengthSq();
-		if( k < Math.EPSILON ) k = 0 else k = Math.invSqrt(k);
+		if( k < Math.EPSILON2 ) k = 0 else k = Math.invSqrt(k);
 		x *= k;
 		x *= k;
 		y *= k;
 		y *= k;
 	}
 	}
@@ -113,7 +113,7 @@ class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 	**/
 	**/
 	public inline function normalized() {
 	public inline function normalized() {
 		var k = lengthSq();
 		var k = lengthSq();
-		if( k < Math.EPSILON ) k = 0 else k = Math.invSqrt(k);
+		if( k < Math.EPSILON2 ) k = 0 else k = Math.invSqrt(k);
 		return new h2d.col.Point(x*k,y*k);
 		return new h2d.col.Point(x*k,y*k);
 	}
 	}
 
 

+ 1 - 1
h2d/col/Ray.hx

@@ -52,7 +52,7 @@ class Ray {
 	function normalize() {
 	function normalize() {
 		var l = lx * lx + ly * ly;
 		var l = lx * lx + ly * ly;
 		if( l == 1. ) return;
 		if( l == 1. ) return;
-		if( l < Math.EPSILON ) l = 0 else l = Math.invSqrt(l);
+		if( l < Math.EPSILON2 ) l = 0 else l = Math.invSqrt(l);
 		lx *= l;
 		lx *= l;
 		ly *= l;
 		ly *= l;
 	}
 	}

+ 1 - 1
h2d/col/RoundRect.hx

@@ -42,7 +42,7 @@ class RoundRect extends Collider {
 		this.dy = dy * 2;
 		this.dy = dy * 2;
 		this.ray = h * 0.5;
 		this.ray = h * 0.5;
 		lenSq = this.dx * this.dx + this.dy * this.dy;
 		lenSq = this.dx * this.dx + this.dy * this.dy;
-		invLenSq = lenSq < hxd.Math.EPSILON ? 0 : 1 / lenSq;
+		invLenSq = lenSq < hxd.Math.EPSILON2 ? 0 : 1 / lenSq;
 	}
 	}
 
 
 	// distance segment
 	// distance segment

+ 1 - 1
h3d/Matrix.hx

@@ -823,7 +823,7 @@ class Matrix {
 		if( m == null ) m = new Matrix();
 		if( m == null ) m = new Matrix();
 		var ax = dir.normalized();
 		var ax = dir.normalized();
 		var ay = up.cross(ax).normalized();
 		var ay = up.cross(ax).normalized();
-		if( ay.lengthSq() < Math.EPSILON ) {
+		if( ay.lengthSq() < Math.EPSILON2 ) {
 			ay.x = ax.y;
 			ay.x = ax.y;
 			ay.y = ax.z;
 			ay.y = ax.z;
 			ay.z = ax.x;
 			ay.z = ax.x;

+ 3 - 3
h3d/Quat.hx

@@ -65,7 +65,7 @@ class Quat {
 
 
 	public function initNormal( dir : h3d.col.Point ) {
 	public function initNormal( dir : h3d.col.Point ) {
 		var dir = dir.normalized();
 		var dir = dir.normalized();
-		if( dir.x*dir.x+dir.y*dir.y < Math.EPSILON )
+		if( dir.x*dir.x+dir.y*dir.y < Math.EPSILON2 )
 			initDirection(new h3d.Vector(1,0,0));
 			initDirection(new h3d.Vector(1,0,0));
 		else {
 		else {
 			var ay = new h3d.col.Point(dir.x, dir.y, 0).normalized();
 			var ay = new h3d.col.Point(dir.x, dir.y, 0).normalized();
@@ -81,7 +81,7 @@ class Quat {
 		if( up != null ) 
 		if( up != null ) 
 			ay.load(up.cross(ax));
 			ay.load(up.cross(ax));
 		ay.normalize();
 		ay.normalize();
-		if( ay.lengthSq() < Math.EPSILON ) {
+		if( ay.lengthSq() < Math.EPSILON2 ) {
 			ay.x = ax.y;
 			ay.x = ax.y;
 			ay.y = ax.z;
 			ay.y = ax.z;
 			ay.z = ax.x;
 			ay.z = ax.x;
@@ -164,7 +164,7 @@ class Quat {
 
 
 	public function normalize() {
 	public function normalize() {
 		var len = x * x + y * y + z * z + w * w;
 		var len = x * x + y * y + z * z + w * w;
-		if( len < hxd.Math.EPSILON ) {
+		if( len < hxd.Math.EPSILON2 ) {
 			x = y = z = 0;
 			x = y = z = 0;
 			w = 1;
 			w = 1;
 		} else {
 		} else {

+ 2 - 2
h3d/Vector.hx

@@ -67,7 +67,7 @@ class Vector #if apicheck implements h2d.impl.PointApi<Vector,Matrix> #end {
 
 
 	public inline function normalize() {
 	public inline function normalize() {
 		var k = lengthSq();
 		var k = lengthSq();
-		if( k < hxd.Math.EPSILON ) k = 0 else k = k.invSqrt();
+		if( k < hxd.Math.EPSILON2 ) k = 0 else k = k.invSqrt();
 		x *= k;
 		x *= k;
 		y *= k;
 		y *= k;
 		z *= k;
 		z *= k;
@@ -75,7 +75,7 @@ class Vector #if apicheck implements h2d.impl.PointApi<Vector,Matrix> #end {
 
 
 	public inline function normalized() {
 	public inline function normalized() {
 		var k = lengthSq();
 		var k = lengthSq();
-		if( k < hxd.Math.EPSILON ) k = 0 else k = k.invSqrt();
+		if( k < hxd.Math.EPSILON2 ) k = 0 else k = k.invSqrt();
 		return new Vector(x * k, y * k, z * k);
 		return new Vector(x * k, y * k, z * k);
 	}
 	}
 
 

+ 2 - 2
h3d/col/Point.hx

@@ -72,7 +72,7 @@ class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 
 
 	public inline function normalize() {
 	public inline function normalize() {
 		var k = x * x + y * y + z * z;
 		var k = x * x + y * y + z * z;
-		if( k < hxd.Math.EPSILON ) k = 0 else k = k.invSqrt();
+		if( k < hxd.Math.EPSILON2 ) k = 0 else k = k.invSqrt();
 		x *= k;
 		x *= k;
 		y *= k;
 		y *= k;
 		z *= k;
 		z *= k;
@@ -80,7 +80,7 @@ class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 
 
 	public inline function normalized() {
 	public inline function normalized() {
 		var k = x * x + y * y + z * z;
 		var k = x * x + y * y + z * z;
-		if( k < hxd.Math.EPSILON ) k = 0 else k = k.invSqrt();
+		if( k < hxd.Math.EPSILON2 ) k = 0 else k = k.invSqrt();
 		return new Point(x*k,y*k,z*k);
 		return new Point(x*k,y*k,z*k);
 	}
 	}
 
 

+ 1 - 1
h3d/col/Ray.hx

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

+ 1 - 0
hxd/Math.hx

@@ -4,6 +4,7 @@ class Math {
 
 
 	public static inline var PI = 3.14159265358979323;
 	public static inline var PI = 3.14159265358979323;
 	public static inline var EPSILON = 1e-10;
 	public static inline var EPSILON = 1e-10;
+	public static inline var EPSILON2 = 1e-20;
 
 
 	public static var POSITIVE_INFINITY(get, never) : Float;
 	public static var POSITIVE_INFINITY(get, never) : Float;
 	public static var NEGATIVE_INFINITY(get, never) : Float;
 	public static var NEGATIVE_INFINITY(get, never) : Float;