Browse Source

added Point, Vector, Matrix abstracts with operator overloading, changed `.multiply(v) to `scaled`

Nicolas Cannasse 1 year ago
parent
commit
4a59a8234c
10 changed files with 100 additions and 36 deletions
  1. 1 1
      h2d/col/IPoint.hx
  2. 19 2
      h2d/col/Point.hx
  3. 1 1
      h2d/col/Polygon.hx
  4. 3 3
      h2d/col/Voronoi.hx
  5. 1 2
      h2d/impl/PointApi.hx
  6. 18 3
      h3d/Matrix.hx
  7. 32 17
      h3d/Vector.hx
  8. 1 1
      h3d/col/IPoint.hx
  9. 22 4
      h3d/col/Point.hx
  10. 2 2
      hxd/impl/Properties.hx

+ 1 - 1
h2d/col/IPoint.hx

@@ -47,7 +47,7 @@ class IPoint #if apicheck implements h2d.impl.PointApi.IPointApi<IPoint> #end {
 	/**
 		Returns a new IPoint with the position of this IPoint multiplied by a given scalar `v`.
 	**/
-	public inline function multiply( v : Int ) {
+	public inline function scaled( v : Int ) {
 		return new IPoint(x * v, y * v);
 	}
 

+ 19 - 2
h2d/col/Point.hx

@@ -5,7 +5,7 @@ import hxd.Math;
 	A simple 2D position/vector container.
 	@see `h2d.col.IPoint`
 **/
-class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
+class PointImpl #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 
 	/**
 		The horizontal position of the point.
@@ -66,7 +66,7 @@ class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 	/**
 		Returns a new Point with the position of this Point multiplied by a given scalar `v`.
 	**/
-	public inline function multiply( v : Float ) {
+	public inline function scaled( v : Float ) {
 		return new Point(x * v, y * v);
 	}
 
@@ -224,3 +224,20 @@ class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 	}
 
 }
+
+@:forward abstract Point(PointImpl) from PointImpl to PointImpl {
+
+	public inline function new(x=0.,y=0.) {
+		this = new PointImpl(x,y);
+	}
+
+	@:op(a - b) public inline function sub(p:Point) return this.sub(p);
+	@:op(a + b) public inline function add(p:Point) return this.add(p);
+	@:op(a *= b) public inline function transform(m:Matrix) this.transform(m);
+	@:op(a * b) public inline function transformed(m:Matrix) return this.transformed(m);
+
+	@:op(a *= b) public inline function scale(v:Float) this.scale(v);
+	@:op(a * b) public inline function scaled(v:Float) return this.scaled(v);
+	@:op(a * b) static inline function scaledInv( f : Float, p : Point ) return p.scaled(f);
+
+}

+ 1 - 1
h2d/col/Polygon.hx

@@ -38,7 +38,7 @@ abstract Polygon(Array<Point>) from Array<Point> to Array<Point> {
 		Returns the points indexes
 	**/
 	public function fastTriangulate() {
-		return new hxd.earcut.Earcut().triangulate(points);
+		return new hxd.earcut.Earcut().triangulate(cast points);
 	}
 
 	/**

+ 3 - 3
h2d/col/Voronoi.hx

@@ -1522,7 +1522,7 @@ class Voronoi {
 							if ( lastBorderSegment ) { break; }
 							va = vb;
 							// fall through
-						} 
+						}
 
 						if (this.equalWithepsilon(va.y,yb) && this.lessThanWithepsilon(va.x,xr)) {
 							lastBorderSegment = this.equalWithepsilon(vz.y,yb);
@@ -1534,7 +1534,7 @@ class Voronoi {
 							if ( lastBorderSegment ) { break; }
 							va = vb;
 							// fall through
-						} 
+						}
 
 						if (this.equalWithepsilon(va.x,xr) && this.greaterThanWithepsilon(va.y,yt)) {
 							lastBorderSegment = this.equalWithepsilon(vz.x,xr);
@@ -1546,7 +1546,7 @@ class Voronoi {
 							if ( lastBorderSegment ) { break; }
 							va = vb;
 							// fall through
-						} 
+						}
 
 						if (this.equalWithepsilon(va.y,yt) && this.greaterThanWithepsilon(va.x,xl)) {
 							lastBorderSegment = this.equalWithepsilon(vz.y,yt);

+ 1 - 2
h2d/impl/PointApi.hx

@@ -25,8 +25,7 @@ interface GenPointApi<Point,Unit> {
 	/**
 		Returns a new Point with the position of this Point multiplied by scalar `v`.
 	**/
-	function multiply( v : Unit ) : Point;
-
+	function scaled( v : Unit ) : Point;
 	/**
 		Multiplies position of this Point by scalar `v`.
 	**/

+ 18 - 3
h3d/Matrix.hx

@@ -9,8 +9,7 @@ typedef ColorAdjust = {
 	?gain : { color : Int, alpha : Float },
 };
 
-@:noDebug
-class Matrix {
+class MatrixImpl {
 
 	static var tmp = new Matrix();
 
@@ -783,6 +782,21 @@ class Matrix {
 		return m;
 	}
 
+}
+
+
+@:forward abstract Matrix(MatrixImpl) from MatrixImpl to MatrixImpl {
+
+	public inline function new() {
+		this = new MatrixImpl();
+	}
+
+	@:op(a * b) public inline function multiplied( m : Matrix ) {
+		var mout = new Matrix();
+		mout.multiply(this, m);
+		return mout;
+	}
+
 	// STATICS
 
 	public static function I() {
@@ -847,4 +861,5 @@ class Matrix {
 		m._44 = 1;
 		return m;
 	}
-}
+
+}

+ 32 - 17
h3d/Vector.hx

@@ -4,7 +4,7 @@ using hxd.Math;
 /**
 	A 4 floats vector. Everytime a Vector is returned, it means a copy is created.
 **/
-class Vector #if apicheck implements h2d.impl.PointApi<Vector,Matrix> #end {
+class VectorImpl #if apicheck implements h2d.impl.PointApi<Vector,Matrix> #end {
 
 	public var x : Float;
 	public var y : Float;
@@ -39,8 +39,8 @@ class Vector #if apicheck implements h2d.impl.PointApi<Vector,Matrix> #end {
 		return new Vector(x + v.x, y + v.y, z + v.z, w + v.w);
 	}
 
-	public inline function multiply( v : Float ) {
-		// multiply only affects length
+	public inline function scaled( v : Float ) {
+		// see scale
 		return new Vector(x * v, y * v, z * v, w);
 	}
 
@@ -94,6 +94,10 @@ class Vector #if apicheck implements h2d.impl.PointApi<Vector,Matrix> #end {
 	}
 
 	public inline function scale( f : Float ) {
+		/*
+			The scale of a vector represents its length and thus
+			only x/y/z should be affected by scaling
+		*/
 		x *= f;
 		y *= f;
 		z *= f;
@@ -276,6 +280,31 @@ class Vector #if apicheck implements h2d.impl.PointApi<Vector,Matrix> #end {
 		return new h3d.Vector(h, s, l, a);
 	}
 
+}
+
+
+
+/**
+	A 4 floats vector. Everytime a Vector is returned, it means a copy is created.
+	For function manipulating the length (length, normalize, dot, scale, etc.), the Vector
+	acts like a Point in the sense only the X/Y/Z components will be affected.
+**/
+@:forward abstract Vector(VectorImpl) from VectorImpl to VectorImpl {
+
+	public inline function new( x = 0., y = 0., z = 0., w = 1. ) {
+		this = new VectorImpl(x,y,z,w);
+	}
+
+	@:op(a - b) public inline function sub(v:Vector) return this.sub(v);
+	@:op(a + b) public inline function add(v:Vector) return this.add(v);
+	@:op(a *= b) public inline function transform(m:Matrix) this.transform(m);
+	@:op(a * b) public inline function transformed(m:Matrix) return this.transformed(m);
+	@:to public inline function toPoint() return this.toPoint();
+
+	@:op(a *= b) public inline function scale(v:Float) this.scale(v);
+	@:op(a * b) public inline function scaled(v:Float) return this.scaled(v);
+	@:op(a * b) static inline function scaledInv( f : Float, v : Vector ) return v.scaled(f);
+
 	public static inline function fromColor( c : Int, scale : Float = 1.0 ) {
 		var s = scale / 255;
 		return new Vector(((c>>16)&0xFF)*s,((c>>8)&0xFF)*s,(c&0xFF)*s,(c >>> 24)*s);
@@ -290,18 +319,4 @@ class Vector #if apicheck implements h2d.impl.PointApi<Vector,Matrix> #end {
 		return r;
 	}
 
-	// deprecated (but not yet warnings)
-
-	@:noCompletion public inline function scale3( v : Float ) {
-		scale(v);
-	}
-
-	@:noCompletion public inline function dot3( v : Vector ) {
-		return dot(v);
-	}
-
-	@:noCompletion public inline function normalizeFast() {
-		normalize();
-	}
-
 }

+ 1 - 1
h3d/col/IPoint.hx

@@ -19,7 +19,7 @@ class IPoint #if apicheck implements h2d.impl.PointApi.IPointApi<IPoint> #end {
 		return 'IPoint{$x,$y,$z}';
 	}
 
-	public inline function multiply( v : Int ) {
+	public inline function scaled( v : Int ) {
 		return new IPoint(x * v, y * v, z * v);
 	}
 

+ 22 - 4
h3d/col/Point.hx

@@ -1,7 +1,7 @@
 package h3d.col;
 using hxd.Math;
 
-class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
+class PointImpl #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 
 	public var x : Float;
 	public var y : Float;
@@ -35,7 +35,7 @@ class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 		return new Point(x + p.x, y + p.y, z + p.z);
 	}
 
-	public inline function multiply( v : Float ) {
+	public inline function scaled( v : Float ) {
 		return new Point(x * v, y * v, z * v);
 	}
 
@@ -142,7 +142,7 @@ class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 	// ----
 
 	public inline function inFrustum( f : Frustum, ?m : h3d.Matrix ) {
-		return f.hasPoint(this);
+		return f.hasPoint((this:Point));
 	}
 
 	public inline function toVector() {
@@ -150,4 +150,22 @@ class Point #if apicheck implements h2d.impl.PointApi<Point,Matrix> #end {
 	}
 
 
-}
+}
+
+@:forward abstract Point(PointImpl) from PointImpl to PointImpl {
+
+	public inline function new(x=0.,y=0.,z=0.) {
+		this = new PointImpl(x,y,z);
+	}
+
+	@:op(a - b) public inline function sub(p:Point) return this.sub(p);
+	@:op(a + b) public inline function add(p:Point) return this.add(p);
+	@:op(a *= b) public inline function transform(m:Matrix) this.transform(m);
+	@:op(a * b) public inline function transformed(m:Matrix) return this.transformed(m);
+	@:to public inline function toVector() return this.toVector();
+
+	@:op(a *= b) public inline function scale(v:Float) this.scale(v);
+	@:op(a * b) public inline function scaled(v:Float) return this.scaled(v);
+	@:op(a * b) static inline function scaledInv( f : Float, p : Point ) return p.scaled(f);
+
+}

+ 2 - 2
hxd/impl/Properties.hx

@@ -35,7 +35,7 @@ class Properties {
 				continue;
 			case TClass(Array):
 				switch( Type.typeof(vprev) ) {
-				case TClass(h3d.Vector):
+				case TClass(h3d.Vector.VectorImpl):
 					var v : Array<Float> = v;
 					var vprev : h3d.Vector = vprev;
 					vprev.set();
@@ -48,7 +48,7 @@ class Properties {
 				}
 			case TClass(String):
 				switch( Type.typeof(vprev) ) {
-				case TClass(h3d.Vector):
+				case TClass(h3d.Vector.VectorImpl):
 					var v : String = v;
 					var vprev : h3d.Vector = vprev;
 					if( v.charCodeAt(0) == '#'.code )  {