Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate oriented bounding box from bounding region for 3D tiles of Cesium's OSM buildings #40

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions modules/core/src/classes/matrix3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import {NumericArray, NumericArray9} from '@math.gl/types';
import {Matrix} from './base/matrix';
import {Vector3} from './vector3';
import {checkVector} from '../lib/validators';

import {vec4_transformMat3} from '../lib/gl-matrix-extras';
Expand Down Expand Up @@ -202,6 +203,56 @@ export class Matrix3 extends Matrix {
return this.check();
}

/**
* Computes the product of this matrix and a column vector.
*
* @param {Vector3} cartesian The column.
* @param {Vector3} result The object onto which to store the result.
* @returns {Vector3} The modified result parameter.
*/
multiplyByVector(cartesian: Vector3, result?: Vector3): Vector3 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same as m3.transform(v3) below? The name comes from viewing the matrix as a transform on vectors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's indeed the same. I removed the method 2cb1e6b

if (!result)
result = new Vector3()

const vX = cartesian.x;
const vY = cartesian.y;
const vZ = cartesian.z;

const x = this[0] * vX + this[3] * vY + this[6] * vZ;
const y = this[1] * vX + this[4] * vY + this[7] * vZ;
const z = this[2] * vX + this[5] * vY + this[8] * vZ;

result.x = x;
result.y = y;
result.z = z;

return result;
}

/**
* Computes the product of this matrix times a (non-uniform) scale, as if the scale were a scale matrix.
*
* @param {Vector3} scale The non-uniform scale on the right-hand side.
* @param {Matrix3} result The object onto which to store the result.
* @returns {Matrix3} The modified result parameter.
*/
multiplyByScale(scale: Vector3, result?: Matrix3): Matrix3 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method is inherited from Matrix3 < Matrix < MathArray. Called m3.scale(v3)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I now reused the parent scale method, see 801904f The overridden scale method in Matrix3 multiplies it with a Vector2. There I'm not sure if adding another check whether the array length is 2 or 3 impacts the performance in case the method is called many times.

if (!result)
result = new Matrix3()

result[0] = this[0] * scale.x;
result[1] = this[1] * scale.x;
result[2] = this[2] * scale.x;
result[3] = this[3] * scale.y;
result[4] = this[4] * scale.y;
result[5] = this[5] * scale.y;
result[6] = this[6] * scale.z;
result[7] = this[7] * scale.z;
result[8] = this[8] * scale.z;

return result;
}

rotate(radians: number): this {
mat3_rotate(this, this, radians);
return this.check();
Expand Down
3 changes: 3 additions & 0 deletions modules/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ export {
// math.gl "GLSL"-style functions
radians,
degrees,
mod,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least a trivial, minimal test case for every global export is normally expected.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. See 0f89190

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In bounding-box-from-region.spec.ts, however, I added only two tests since there are too many to port from Cesium. Moreover, these tests are based on a unit sphere and math.gl supports only a WGS84 ellipsoid currently.

zeroToTwoPi,
negativePiToPi,
sin,
cos,
tan,
Expand Down
50 changes: 50 additions & 0 deletions modules/core/src/lib/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import type {NumericArray} from '@math.gl/types';

import type {MathArray} from '../classes/base/math-array';
import {EPSILON14, TWO_PI, PI} from './math-utils';

const RADIANS_TO_DEGREES = (1 / Math.PI) * 180;
const DEGREES_TO_RADIANS = (1 / 180) * Math.PI;
Expand Down Expand Up @@ -122,6 +123,55 @@ export function degrees(
return map(radians, (radians) => radians * RADIANS_TO_DEGREES, result);
}


/**
* The modulo operation that also works for negative dividends.
*
* @param {number} m The dividend.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: Avoid adding types in the TSDoc, they are inferred from the typescript types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. See 2222cc5

* @param {number} n The divisor.
* @returns {number} The remainder.
*/
export function mod(m: number, n: number): number {
if (Math.sign(m) === Math.sign(n) && Math.abs(m) < Math.abs(n)) {
return m;
}

return ((m % n) + n) % n;
}

/**
* Produces an angle in the range 0 <= angle <= 2Pi which is equivalent to the provided angle.
*
* @param {number} angle in radians
* @returns {number} The angle in the range [0, <code>TWO_PI</code>].
*/
export function zeroToTwoPi(angle: number): number {
if (angle >= 0 && angle <= TWO_PI) {
return angle;
}
const remainder = mod(angle, TWO_PI);
if (
Math.abs(remainder) < EPSILON14 &&
Math.abs(angle) > EPSILON14
) {
return TWO_PI;
}
return remainder;
}

/**
* Produces an angle in the range -Pi <= angle <= Pi which is equivalent to the provided angle.
*
* @param {number} angle in radians
* @returns {number} The angle in the range [<code>-PI</code>, <code>PI</code>].
*/
export function negativePiToPi(angle: number): number {
if (angle >= -PI && angle <= PI) {
return angle;
}
return zeroToTwoPi(angle + PI) - PI;
}

/**
* "GLSL equivalent" of `Math.sin`: Works on single values and vectors
* @deprecated
Expand Down
1 change: 1 addition & 0 deletions modules/core/src/lib/math-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ export const PI_OVER_TWO = Math.PI / 2;
export const PI_OVER_FOUR = Math.PI / 4;
export const PI_OVER_SIX = Math.PI / 6;

export const PI = Math.PI;
export const TWO_PI = Math.PI * 2;
3 changes: 2 additions & 1 deletion modules/culling/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
],
"dependencies": {
"@math.gl/core": "4.1.0-alpha.9",
"@math.gl/types": "4.1.0-alpha.9"
"@math.gl/types": "4.1.0-alpha.9",
"@math.gl/geospatial": "4.1.0-alpha.9"
},
"gitHead": "e1a95300cb225a90da6e90333d4adf290f7ba501"
}
2 changes: 2 additions & 0 deletions modules/culling/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export {BoundingSphere} from './lib/bounding-volumes/bounding-sphere';
export {OrientedBoundingBox} from './lib/bounding-volumes/oriented-bounding-box';
export {CullingVolume} from './lib/culling-volume';
export {Plane} from './lib/plane';
export {Ray} from './lib/ray';
export {EllipsoidTangentPlane} from './lib/ellipsoid-tangent-plane';

export {PerspectiveOffCenterFrustum as _PerspectiveOffCenterFrustum} from './lib/perspective-off-center-frustum';
export {PerspectiveFrustum as _PerspectiveFrustum} from './lib/perspective-frustum';
Expand Down
Loading
Loading