Skip to content

Commit

Permalink
[functions][core] bounds -> getBounds (#774)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Jan 8, 2023
1 parent d38e96d commit c296525
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 60 deletions.
1 change: 1 addition & 0 deletions packages/core/src/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export {
Logger,
MathUtils,
Verbosity,
getBounds,
bounds,
uuid,
} from './utils';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import type { Mesh, Node, Scene } from '../properties';
* Example:
*
* ```ts
* const {min, max} = bounds(scene);
* const {min, max} = getBounds(scene);
* ```
*/
export function bounds(node: Node | Scene): bbox {
export function getBounds(node: Node | Scene): bbox {
const resultBounds = createBounds();
const parents = node.propertyType === PropertyType.NODE ? [node] : node.listChildren();

Expand All @@ -30,6 +30,12 @@ export function bounds(node: Node | Scene): bbox {
return resultBounds;
}

/**
* @deprecated Renamed to {@link getBounds}.
* @hidden
*/
export const bounds = getBounds;

/** Computes mesh bounds in local space. */
function getMeshBounds(mesh: Mesh, worldMatrix: mat4): bbox {
const meshBounds = createBounds();
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export * from './bounds';
export * from './get-bounds';
export * from './buffer-utils';
export * from './color-utils';
export * from './file-utils';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import test from 'tape';
import { Accessor, Document } from '@gltf-transform/core';
import { bounds } from '@gltf-transform/core';
import { getBounds } from '@gltf-transform/core';

test('@gltf-transform/functions::bounds', (t) => {
test('@gltf-transform/functions::getBounds', (t) => {
const doc = new Document();
const position = doc
.createAccessor()
Expand All @@ -14,7 +14,7 @@ test('@gltf-transform/functions::bounds', (t) => {
const scene = doc.createScene().addChild(node);

t.deepEquals(
bounds(scene),
getBounds(scene),
{
min: [100, 100, 100],
max: [105, 105, 105],
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
Accessor,
bbox,
bounds,
getBounds,
BufferUtils,
Document,
Extension,
Expand Down Expand Up @@ -244,7 +244,7 @@ export class DracoMeshCompression extends Extension {
if (this.document.getRoot().listScenes().length !== 1) {
logger.warn(`[${NAME}]: quantizationVolume=scene requires exactly 1 scene.`);
} else {
quantizationVolume = bounds(this.document.getRoot().listScenes().pop()!);
quantizationVolume = getBounds(this.document.getRoot().listScenes().pop()!);
}
}

Expand Down
7 changes: 3 additions & 4 deletions packages/extensions/test/draco-mesh-compression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ require('source-map-support').install();
import path from 'path';
import { createDecoderModule, createEncoderModule } from 'draco3dgltf';
import test from 'tape';
import { Accessor, Buffer, Document, Format, NodeIO, Primitive } from '@gltf-transform/core';
import { bounds } from '@gltf-transform/functions';
import { Accessor, Buffer, Document, Format, NodeIO, Primitive, getBounds } from '@gltf-transform/core';
import { DracoMeshCompression } from '../';

const throwsAsync = async (t: test.Test, fn: () => Promise<unknown>, re: RegExp, msg: string): Promise<void> => {
Expand All @@ -19,7 +18,7 @@ const throwsAsync = async (t: test.Test, fn: () => Promise<unknown>, re: RegExp,
test('@gltf-transform/extensions::draco-mesh-compression | decoding', async (t) => {
const io = await createDecoderIO();
const doc = await io.read(path.join(__dirname, 'in', 'BoxDraco.gltf'));
const bbox = bounds(doc.getRoot().listScenes()[0]);
const bbox = getBounds(doc.getRoot().listScenes()[0]);
t.deepEquals(
bbox.min.map((v) => +v.toFixed(3)),
[-0.5, -0.5, -0.5],
Expand Down Expand Up @@ -123,7 +122,7 @@ test('@gltf-transform/extensions::draco-mesh-compression | encoding complete', a
io = await createDecoderIO();
const roundtripDoc = await io.readJSON(jsonDoc);
const roundtripNode = roundtripDoc.getRoot().listNodes()[0];
const bbox = bounds(roundtripNode);
const bbox = getBounds(roundtripNode);
t.deepEquals(
bbox.min.map((v) => +v.toFixed(3)),
[0, -1, -1],
Expand Down
7 changes: 3 additions & 4 deletions packages/extensions/test/meshopt-compression.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ require('source-map-support').install();

import path from 'path';
import test from 'tape';
import { NodeIO } from '@gltf-transform/core';
import { bounds } from '@gltf-transform/functions';
import { NodeIO, getBounds } from '@gltf-transform/core';
import { MeshoptCompression, MeshQuantization } from '../';
import { MeshoptDecoder, MeshoptEncoder } from 'meshoptimizer';

Expand All @@ -18,7 +17,7 @@ test('@gltf-transform/extensions::draco-mesh-compression | decoding', async (t)

for (const input of INPUTS) {
const doc = await io.read(path.join(__dirname, 'in', input));
const bbox = bounds(doc.getRoot().listScenes()[0]);
const bbox = getBounds(doc.getRoot().listScenes()[0]);
t.deepEquals(
bbox.min.map((v) => +v.toFixed(3)),
[-0.5, -0.5, -0.5],
Expand Down Expand Up @@ -50,7 +49,7 @@ test('@gltf-transform/extensions::draco-mesh-compression | encoding', async (t)
.getRoot()
.listExtensionsRequired()
.map((ext) => ext.extensionName);
const bbox = bounds(doc.getRoot().listScenes()[0]);
const bbox = getBounds(doc.getRoot().listScenes()[0]);

t.ok(extensionsRequired.includes('EXT_meshopt_compression'), 'retains EXT_meshopt_compression');
t.deepEquals(
Expand Down
4 changes: 2 additions & 2 deletions packages/functions/src/center.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Document, Transform, vec3 } from '@gltf-transform/core';
import { bounds } from '@gltf-transform/core';
import { getBounds } from '@gltf-transform/core';
import { createTransform } from './utils';

const NAME = 'center';
Expand Down Expand Up @@ -37,7 +37,7 @@ export function center(_options: CenterOptions = CENTER_DEFAULTS): Transform {

let pivot: vec3;
if (typeof options.pivot === 'string') {
const bbox = bounds(scene);
const bbox = getBounds(scene);
pivot = [
(bbox.max[0] - bbox.min[0]) / 2 + bbox.min[0],
(bbox.max[1] - bbox.min[1]) / 2 + bbox.min[1],
Expand Down
1 change: 0 additions & 1 deletion packages/functions/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
* @module functions
*/

export { bounds } from '@gltf-transform/core'; // backwards compatibility, remove in v0.12
export * from './center';
export * from './colorspace';
export * from './dedup';
Expand Down
4 changes: 2 additions & 2 deletions packages/functions/src/inspect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
GLTF,
ImageUtils,
Texture,
bounds,
getBounds,
PropertyType,
} from '@gltf-transform/core';
import { getGLPrimitiveCount } from './utils';
Expand All @@ -29,7 +29,7 @@ function listScenes(doc: Document): InspectPropertyReport<InspectSceneReport> {
.listScenes()
.map((scene) => {
const root = scene.listChildren()[0];
const sceneBounds = bounds(scene);
const sceneBounds = getBounds(scene);
return {
name: scene.getName(),
rootName: root ? root.getName() : '',
Expand Down
10 changes: 5 additions & 5 deletions packages/functions/test/center.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
require('source-map-support').install();

import test from 'tape';
import { Accessor, Document } from '@gltf-transform/core';
import { bounds, center } from '../';
import { Accessor, Document, getBounds } from '@gltf-transform/core';
import { center } from '../';

test('@gltf-transform/functions::center', async (t) => {
const doc = new Document();
Expand All @@ -18,7 +18,7 @@ test('@gltf-transform/functions::center', async (t) => {
await doc.transform(center({ pivot: 'center' }));

t.deepEquals(
bounds(scene),
getBounds(scene),
{
min: [-2.5, -2.5, -2.5],
max: [2.5, 2.5, 2.5],
Expand All @@ -29,7 +29,7 @@ test('@gltf-transform/functions::center', async (t) => {
await doc.transform(center({ pivot: 'above' }));

t.deepEquals(
bounds(scene),
getBounds(scene),
{
min: [-2.5, -5.0, -2.5],
max: [2.5, 0.0, 2.5],
Expand All @@ -40,7 +40,7 @@ test('@gltf-transform/functions::center', async (t) => {
await doc.transform(center({ pivot: 'below' }));

t.deepEquals(
bounds(scene),
getBounds(scene),
{
min: [-2.5, 0.0, -2.5],
max: [2.5, 5.0, 2.5],
Expand Down
10 changes: 5 additions & 5 deletions packages/functions/test/dequantize.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require('source-map-support').install();

import test from 'tape';
import { bbox, bounds, Document, Logger, Primitive, PrimitiveTarget, Scene, vec3 } from '@gltf-transform/core';
import { bbox, getBounds, Document, Logger, Primitive, PrimitiveTarget, Scene, vec3 } from '@gltf-transform/core';
import { dequantize } from '../';

const logger = new Logger(Logger.Verbosity.WARN);
Expand All @@ -12,8 +12,8 @@ test('@gltf-transform/functions::dequantize', async (t) => {
const node = doc.getRoot().listNodes()[0];
const prim = doc.getRoot().listMeshes()[0].listPrimitives()[0];

const bboxScenePrev = bounds(scene);
const bboxNodePrev = bounds(node);
const bboxScenePrev = getBounds(scene);
const bboxNodePrev = getBounds(node);
const bboxMeshPrev = primBounds(prim);

t.deepEquals(bboxScenePrev, { min: [0, 0, 0], max: [50, 50, 50] }, 'original bbox - scene');
Expand All @@ -22,8 +22,8 @@ test('@gltf-transform/functions::dequantize', async (t) => {

await doc.transform(dequantize());

const bboxScene = bounds(scene);
const bboxNode = bounds(node);
const bboxScene = getBounds(scene);
const bboxNode = getBounds(node);
const bboxMesh = primBounds(prim);

t.ok(prim.getAttribute('POSITION').getArray() instanceof Float32Array, 'position - float32');
Expand Down
44 changes: 22 additions & 22 deletions packages/functions/test/quantize.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
Accessor,
AnimationChannel,
bbox,
bounds,
getBounds,
Document,
Logger,
Mesh,
Expand Down Expand Up @@ -45,9 +45,9 @@ test('@gltf-transform/functions::quantize | mesh volume', async (t) => {
const primA = doc.getRoot().listMeshes()[0].listPrimitives()[0];
const primB = doc.getRoot().listMeshes()[1].listPrimitives()[0];

const bboxSceneCopy = bounds(scene);
const bboxNodeACopy = bounds(nodeA);
const bboxNodeBCopy = bounds(nodeB);
const bboxSceneCopy = getBounds(scene);
const bboxNodeACopy = getBounds(nodeA);
const bboxNodeBCopy = getBounds(nodeB);
const bboxMeshACopy = primBounds(primA);
const bboxMeshBCopy = primBounds(primB);

Expand All @@ -59,9 +59,9 @@ test('@gltf-transform/functions::quantize | mesh volume', async (t) => {

await doc.transform(quantize({ quantizePosition: 14 }));

const bboxScene = bounds(scene);
const bboxNodeA = bounds(nodeA);
const bboxNodeB = bounds(nodeB);
const bboxScene = getBounds(scene);
const bboxNodeA = getBounds(nodeA);
const bboxNodeB = getBounds(nodeB);
const bboxMeshA = primBounds(primA);
const bboxMeshB = primBounds(primB);

Expand All @@ -84,9 +84,9 @@ test('@gltf-transform/functions::quantize | scene volume', async (t) => {
const primA = doc.getRoot().listMeshes()[0].listPrimitives()[0];
const primB = doc.getRoot().listMeshes()[1].listPrimitives()[0];

const bboxSceneCopy = bounds(scene);
const bboxNodeACopy = bounds(nodeA);
const bboxNodeBCopy = bounds(nodeB);
const bboxSceneCopy = getBounds(scene);
const bboxNodeACopy = getBounds(nodeA);
const bboxNodeBCopy = getBounds(nodeB);
const bboxMeshACopy = primBounds(primA);
const bboxMeshBCopy = primBounds(primB);

Expand All @@ -103,9 +103,9 @@ test('@gltf-transform/functions::quantize | scene volume', async (t) => {
})
);

const bboxScene = roundBbox(bounds(scene), 3);
const bboxNodeA = roundBbox(bounds(nodeA), 2);
const bboxNodeB = roundBbox(bounds(nodeB), 3);
const bboxScene = roundBbox(getBounds(scene), 3);
const bboxNodeA = roundBbox(getBounds(nodeA), 2);
const bboxNodeB = roundBbox(getBounds(nodeB), 3);
const bboxMeshA = roundBbox(primBounds(primA), 3);
const bboxMeshB = roundBbox(primBounds(primB), 3);

Expand All @@ -128,9 +128,9 @@ test('@gltf-transform/functions::quantize | skinned mesh', async (t) => {
const primA = doc.getRoot().listMeshes()[0].listPrimitives()[0];
const primB = doc.getRoot().listMeshes()[1].listPrimitives()[0];

const bboxSceneCopy = bounds(scene);
const bboxNodeACopy = bounds(nodeA);
const bboxNodeBCopy = bounds(nodeB);
const bboxSceneCopy = getBounds(scene);
const bboxNodeACopy = getBounds(nodeA);
const bboxNodeBCopy = getBounds(nodeB);
const bboxMeshACopy = primBounds(primA);
const bboxMeshBCopy = primBounds(primB);

Expand All @@ -152,13 +152,13 @@ test('@gltf-transform/functions::quantize | skinned mesh', async (t) => {

await doc.transform(quantize({ quantizePosition: 14 }));

const bboxScene = bounds(scene);
const bboxNodeA = bounds(nodeA);
const bboxNodeB = bounds(nodeB);
const bboxScene = getBounds(scene);
const bboxNodeA = getBounds(nodeA);
const bboxNodeB = getBounds(nodeB);
const bboxMeshA = primBounds(primA);
const bboxMeshB = primBounds(primB);

// NodeA now affects scene bounds, because bounds() does not check IBMs.
// NodeA now affects scene bounds, because getBounds() does not check IBMs.
t.deepEquals(bboxScene, { min: [-0.5, -0.5, -0.5], max: [25, 15, 0.5] }, 'bbox - scene');
t.deepEquals(bboxNodeA, { min: [20, 10, 0], max: [25, 15, 0] }, 'bbox - nodeA');
t.deepEquals(bboxNodeB, { min: [-0.5, -0.5, -0.5], max: [0.5, 0.5, 0.5] }, 'bbox - nodeB');
Expand Down Expand Up @@ -192,7 +192,7 @@ test('@gltf-transform/functions::quantize | morph targets', async (t) => {

const scene = doc.getRoot().listScenes().pop();

const bboxSceneCopy = bounds(scene);
const bboxSceneCopy = getBounds(scene);
const bboxMeshCopy = primBounds(prim);
const bboxTargetCopy = primBounds(target);

Expand All @@ -202,7 +202,7 @@ test('@gltf-transform/functions::quantize | morph targets', async (t) => {

await doc.transform(quantize({ quantizePosition: 14 }));

const bboxScene = roundBbox(bounds(scene), 2);
const bboxScene = roundBbox(getBounds(scene), 2);
const bboxMesh = roundBbox(primBounds(prim), 3);
const bboxTarget = roundBbox(primBounds(target), 3);

Expand Down
Loading

0 comments on commit c296525

Please sign in to comment.