Skip to content
New issue

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

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

Already on GitHub? # to your account

chore: attempt at fixing MathUtils issue #4182

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright 2024 Cognite AS
*/

import { MathUtils } from 'three/src/math/MathUtils';
import { MathUtils } from 'three';
import { FlexibleControlsType } from './FlexibleControlsType';
import { FlexibleMouseActionType } from './FlexibleMouseActionType';
import { FlexibleWheelZoomType } from './FlexibleWheelZoomType';
Expand Down
18 changes: 9 additions & 9 deletions viewer/packages/tools/src/HtmlOverlay/BucketGrid2D.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/*!
* Copyright 2021 Cognite AS
*/
import * as THREE from 'three';
import range from 'lodash/range';

import { HtmlOverlayTool } from './HtmlOverlayTool';
import { Box2, MathUtils } from 'three';

type SimpleGrid2DElement<T> = {
bounds: THREE.Box2;
bounds: Box2;
element: T;
};

Expand All @@ -22,21 +22,21 @@ type SimpleGrid2DElement<T> = {
*/
export class BucketGrid2D<T> {
private readonly _dimensions: [width: number, height: number];
private readonly _bounds: THREE.Box2;
private readonly _bounds: Box2;
private readonly _cells: SimpleGrid2DElement<T>[][];
/**
* Holds elements that has been removed from the collection using {@link BucketGrid2D.removeOverlappingElements}.
* This is used to avoid expensive re-allocations of cells when removing elements.
*/
private readonly _removedElements = new Set<T>();

constructor(bounds: THREE.Box2, dimensions: [width: number, height: number]) {
constructor(bounds: Box2, dimensions: [width: number, height: number]) {
this._dimensions = dimensions;
this._cells = range(0, dimensions[0] * dimensions[1]).map(() => new Array<SimpleGrid2DElement<T>>());
this._bounds = bounds;
}

insert(bounds: THREE.Box2, element: T): void {
insert(bounds: Box2, element: T): void {
if (!this._bounds.intersectsBox(bounds)) {
throw new Error('Element to be added must be partially inside grid');
}
Expand All @@ -49,7 +49,7 @@ export class BucketGrid2D<T> {
}
}

*overlappingElements(bounds: THREE.Box2): Generator<T> {
*overlappingElements(bounds: Box2): Generator<T> {
if (!this._bounds.intersectsBox(bounds)) {
return;
}
Expand All @@ -69,7 +69,7 @@ export class BucketGrid2D<T> {
}
}

*removeOverlappingElements(bounds: THREE.Box2): Generator<T> {
*removeOverlappingElements(bounds: Box2): Generator<T> {
if (!this._bounds.intersectsBox(bounds)) {
return;
}
Expand All @@ -85,7 +85,7 @@ export class BucketGrid2D<T> {
}
}

private *cellsIntersecting(bounds: THREE.Box2): Generator<SimpleGrid2DElement<T>[]> {
private *cellsIntersecting(bounds: Box2): Generator<SimpleGrid2DElement<T>[]> {
const { min, max } = this._bounds;
const dimX = this._dimensions[0];
const dimY = this._dimensions[1];
Expand All @@ -94,7 +94,7 @@ export class BucketGrid2D<T> {
const relativeBoundsMinY = (bounds.min.y - min.y) / (max.y - min.y);
const relativeBoundsMaxY = (bounds.max.y - min.y) / (max.y - min.y);

const clamp = THREE.MathUtils.clamp;
const clamp = MathUtils.clamp;
const minI = clamp(Math.floor(dimX * relativeBoundsMinX), 0, dimX - 1);
const maxI = clamp(Math.floor(dimX * relativeBoundsMaxX), 0, dimX - 1);
const minJ = clamp(Math.floor(dimY * relativeBoundsMinY), 0, dimY - 1);
Expand Down
Loading