소스 검색

New features in Tools class and MathLib

Tools
 - getClassName() now also supports objects having a getClassName():string method.
 - adding hasCodeFromStream() to compute a hash Code on a sequence of objects.

MathLib
 - getClassName() and getHashCode() is implemented for the following types: Color3/4, Vector2/3/4, Size, Quaternion, Matrix, Plane
nockawa 9 년 전
부모
커밋
39b85686f2
2개의 변경된 파일156개의 추가작업 그리고 9개의 파일을 삭제
  1. 104 0
      src/Math/babylon.math.ts
  2. 52 9
      src/Tools/babylon.tools.ts

+ 104 - 0
src/Math/babylon.math.ts

@@ -48,6 +48,17 @@
             return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
         }
 
+        public getClassName(): string {
+            return "Color3";
+        }
+
+        public getHashCode(): number {
+            let hash = this.r || 0;
+            hash = (hash * 397) ^ (this.g || 0);
+            hash = (hash * 397) ^ (this.b || 0);
+            return hash;
+        }
+
         // Operators
         public toArray(array: number[], index?: number): Color3 {
             if (index === undefined) {
@@ -298,6 +309,18 @@
             return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
         }
 
+        public getClassName(): string {
+            return "Color4";
+        }
+
+        public getHashCode(): number {
+            let hash = this.r || 0;
+            hash = (hash * 397) ^ (this.g || 0);
+            hash = (hash * 397) ^ (this.b || 0);
+            hash = (hash * 397) ^ (this.a || 0);
+            return hash;
+        }
+
         public clone(): Color4 {
             return new Color4(this.r, this.g, this.b, this.a);
         }
@@ -385,6 +408,16 @@
             return "{X: " + this.x + " Y:" + this.y + "}";
         }
 
+        public getClassName(): string {
+            return "Vector2";
+        }
+
+        public getHashCode(): number {
+            let hash = this.x || 0;
+            hash = (hash * 397) ^ (this.y || 0);
+            return hash;
+        }
+
         // Operators
         public toArray(array: number[], index: number = 0): Vector2 {
             array[index] = this.x;
@@ -640,6 +673,17 @@
             return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
         }
 
+        public getClassName(): string {
+            return "Vector3";
+        }
+
+        public getHashCode(): number {
+            let hash = this.x || 0;
+            hash = (hash * 397) ^ (this.y || 0);
+            hash = (hash * 397) ^ (this.z || 0);
+            return hash;
+        }
+
         // Operators
         public asArray(): number[] {
             var result = [];
@@ -1261,6 +1305,18 @@
             return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "W:" + this.w + "}";
         }
 
+        public getClassName(): string {
+            return "Vector4";
+        }
+
+        public getHashCode(): number {
+            let hash = this.x || 0;
+            hash = (hash * 397) ^ (this.y || 0);
+            hash = (hash * 397) ^ (this.z || 0);
+            hash = (hash * 397) ^ (this.w || 0);
+            return hash;
+        }
+
         // Operators
         public asArray(): number[] {
             var result = [];
@@ -1579,6 +1635,20 @@
             this.height = height;
         }
 
+        public toString(): string {
+            return `{W: ${this.width}, H: ${this.height}}`;
+        }
+
+        public getClassName(): string {
+            return "Size";
+        }
+
+        public getHashCode(): number {
+            let hash = this.width || 0;
+            hash = (hash * 397) ^ (this.height || 0);
+            return hash;
+        }
+
         public clone(): Size {
             return new Size(this.width, this.height);
         }
@@ -1627,6 +1697,18 @@
             return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
         }
 
+        public getClassName(): string {
+            return "Quaternion";
+        }
+
+        public getHashCode(): number {
+            let hash = this.x || 0;
+            hash = (hash * 397) ^ (this.y || 0);
+            hash = (hash * 397) ^ (this.z || 0);
+            hash = (hash * 397) ^ (this.w || 0);
+            return hash;
+        }
+
         public asArray(): number[] {
             return [this.x, this.y, this.z, this.w];
         }
@@ -2216,6 +2298,18 @@
                 this.m[12], this.m[13], this.m[14], this.m[15]);
         }
 
+        public getClassName(): string {
+            return "Matrix";
+        }
+
+        public getHashCode(): number {
+            let hash = this.m[0] || 0;
+            for (let i = 1; i < 16; i++) {
+                hash = (hash * 397) ^ (this.m[i] || 0);
+            }
+            return hash;
+        }
+
         public decompose(scale: Vector3, rotation: Quaternion, translation: Vector3): boolean {
             translation.x = this.m[12];
             translation.y = this.m[13];
@@ -2838,6 +2932,16 @@
             return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
         }
 
+        public getClassName(): string {
+            return "Plane";
+        }
+
+        public getHashCode(): number {
+            let hash = this.normal.getHashCode();
+            hash = (hash * 397) ^ (this.d || 0);
+            return hash;
+        }
+
         public normalize(): Plane {
             var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
             var magnitude = 0;

+ 52 - 9
src/Tools/babylon.tools.ts

@@ -910,14 +910,18 @@
          */
         public static getClassName(object, isType: boolean = false): string {
             let name = null;
-            if (object instanceof Object) {
-                let classObj = isType ? object :  Object.getPrototypeOf(object);
-                name = classObj.constructor["__bjsclassName__"];
-            }
-            if (!name) {
-                name = typeof object;
-            }
 
+            if (!isType && object.getClassName) {
+                name = object.getClassName();
+            } else {
+                if (object instanceof Object) {
+                    let classObj = isType ? object : Object.getPrototypeOf(object);
+                    name = classObj.constructor["__bjsclassName__"];
+                }
+                if (!name) {
+                    name = typeof object;
+                }
+            }
             return name;
         }
 
@@ -929,6 +933,45 @@
             }
         }
 
+        /**
+         * This method can be used with hashCodeFromStream when your input is an array of values that are either: number, string, boolean or custom type implementing the getHashCode():number method.
+         * @param array
+         */
+        public static arrayOrStringFeeder(array: any): (i) => number {
+            return (index: number) => {
+                if (index >= array.length) {
+                    return null;
+                }
+
+                let val = array.charCodeAt ? array.charCodeAt(index) : array[index];
+                if (val && val.getHashCode) {
+                    val = val.getHashCode();
+                }
+                if (typeof val === "string") {
+                    return Tools.hashCodeFromStream(Tools.arrayOrStringFeeder(val));
+                }
+                return val;
+            };
+        }
+
+        /**
+         * Compute the hashCode of a stream of number
+         * To compute the HashCode on a string or an Array of data types implementing the getHashCode() method, use the arrayOrStringFeeder method.
+         * @param feeder a callback that will be called until it returns null, each valid returned values will be used to compute the hash code.
+         * @return the hash code computed
+         */
+        public static hashCodeFromStream(feeder: (index: number) => number): number {
+            // Based from here: http://stackoverflow.com/a/7616484/802124
+            let hash = 0;
+            let index = 0;
+            let chr = feeder(index++);
+            while (chr != null) {
+                hash = ((hash << 5) - hash) + chr;
+                hash |= 0;                          // Convert to 32bit integer
+                chr = feeder(index++);
+            }
+            return hash;
+        }
     }
 
     /**
@@ -944,8 +987,8 @@
     }
 
     /**
-     * An implementation of a loop for asynchronous functions.
-     */
+    * An implementation of a loop for asynchronous functions.
+    */
     export class AsyncLoop {
         public index: number;
         private _done: boolean;