Typing shape of geom util (#4524)

* Fix static copy method of sphere and typing sphere

* typing aabb and obb
This commit is contained in:
jiaxin.lai
2019-05-30 14:54:15 +08:00
committed by pandamicro
parent 4ab9b0dee0
commit 2f826a35eb
3 changed files with 18 additions and 16 deletions
+4 -4
View File
@@ -1,4 +1,4 @@
import { mat3, mat4, vec3 } from '../../core/vmath';
import { mat3, mat4, quat, vec3 } from '../../core/vmath';
import enums from './enums';
const _v3_tmp = vec3.create();
@@ -55,7 +55,7 @@ export default class aabb {
* @param {aabb} a the source aabb
* @return {aabb}
*/
public static copy (out, a) {
public static copy (out: aabb, a: aabb): aabb {
vec3.copy(out.center, a.center);
vec3.copy(out.halfExtents, a.halfExtents);
@@ -89,7 +89,7 @@ export default class aabb {
* @return {aabb} out
* @function
*/
public static set (out, px, py, pz, w, h, l) {
public static set (out: aabb, px: number, py: number, pz: number, w: number, h: number, l: number): aabb {
vec3.set(out.center, px, py, pz);
vec3.set(out.halfExtents, w, h, l);
return out;
@@ -134,7 +134,7 @@ export default class aabb {
* @param m - the transform matrix
* @param out - the target shape
*/
public transform (m, pos, rot, scale, out) {
public transform (m: mat4, pos: vec3 | null, rot: quat | null, scale: vec3 | null, out: aabb){
if (!out) { out = this; }
vec3.transformMat4(out.center, this.center, m);
transform_extent_m4(out.halfExtents, this.halfExtents, m);
+2 -1
View File
@@ -13,6 +13,7 @@ const transform_extent_m3 = (out: vec3, extent: vec3, m3: mat3) => {
vec3.transformMat3(out, extent, _m3_tmp);
};
// tslint:disable-next-line:class-name
export default class obb {
/**
@@ -157,7 +158,7 @@ export default class obb {
* @param scale the scale part of the transform
* @param [out] the target shape
*/
public transform (m: mat4, pos: vec3, rot: quat, scale: vec3, out?: obb) {
public transform (m: mat4, pos: vec3, rot: quat, scale: vec3, out: obb) {
if (!out) { out = this; }
vec3.transformMat4(out.center, this.center, m);
// parent shape doesn't contain rotations for now
+12 -11
View File
@@ -1,17 +1,18 @@
import { vec3 } from '../../core/vmath';
import { mat4, quat, vec3 } from '../../core/vmath';
import enums from './enums';
const _v3_tmp = vec3.create();
function maxComponent (v) { return Math.max(Math.max(v.x, v.y), v.z); }
function maxComponent (v: vec3) { return Math.max(Math.max(v.x, v.y), v.z); }
// tslint:disable-next-line: class-name
export default class sphere {
/**
* create a new sphere
*
* @return {plane}
* @return {sphere}
*/
public static create (cx, cy, cz, r) {
public static create (cx: number, cy: number, cz: number, r: number): sphere {
return new sphere(cx, cy, cz, r);
}
@@ -21,7 +22,7 @@ export default class sphere {
* @param {sphere} p the source sphere
* @return {sphere}
*/
public static clone (p) {
public static clone (p: sphere): sphere {
return new sphere(p.c.x, p.c.y, p.c.z, p.r);
}
@@ -32,8 +33,8 @@ export default class sphere {
* @param {sphere} p the source sphere
* @return {sphere}
*/
public static copy (out, p) {
vec3.copy(out, p.c);
public static copy (out: sphere, p: sphere): sphere {
vec3.copy(out.c, p.c);
out.r = p.r;
return out;
@@ -47,7 +48,7 @@ export default class sphere {
* @param {vec3} maxPos upper corner of the original shape
* @return {sphere}
*/
public static fromPoints (out, minPos, maxPos) {
public static fromPoints (out: sphere, minPos: vec3, maxPos: vec3): sphere {
vec3.scale(out.c, vec3.add(_v3_tmp, minPos, maxPos), 0.5);
out.r = vec3.mag(vec3.sub(_v3_tmp, maxPos, minPos)) * 0.5;
return out;
@@ -64,7 +65,7 @@ export default class sphere {
* @return {sphere} out
* @function
*/
public static set (out, cx, cy, cz, r) {
public static set (out: sphere, cx: number, cy: number, cz: number, r: number): sphere {
out.c.x = cx;
out.c.y = cy;
out.c.z = cz;
@@ -97,7 +98,7 @@ export default class sphere {
* @param {vec3} minPos
* @param {vec3} maxPos
*/
public getBoundary (minPos, maxPos) {
public getBoundary (minPos: vec3, maxPos: vec3) {
vec3.set(minPos, this.c.x - this.r, this.c.y - this.r, this.c.z - this.r);
vec3.set(maxPos, this.c.x + this.r, this.c.y + this.r, this.c.z + this.r);
}
@@ -110,7 +111,7 @@ export default class sphere {
* @param {vec3} scale the scale part of the transform
* @param {sphere} [out] the target shape
*/
public transform (m, pos, rot, scale, out) {
public transform (m: mat4, pos: vec3, rot: quat, scale: vec3, out: sphere) {
if (!out) { out = this; }
vec3.transformMat4(out.c, this.c, m);
out.r = this.r * maxComponent(scale);