Skip to content

Commit

Permalink
feat(Polygons): move bounding box related methods from ArxMap to Poly…
Browse files Browse the repository at this point in the history
…gons
  • Loading branch information
meszaros-lajos-gyorgy committed Jun 2, 2024
1 parent fa1faac commit 6fb2f5b
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 55 deletions.
58 changes: 4 additions & 54 deletions src/ArxMap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
ArxVertex,
} from 'arx-convert/types'
import { getCellCoords, MAP_DEPTH_IN_CELLS, MAP_WIDTH_IN_CELLS, QuadrupleOf } from 'arx-convert/utils'
import { Box3, Object3D } from 'three'
import { Object3D } from 'three'
import { Audio } from '@src/Audio.js'
import { Entities } from '@src/Entities.js'
import { Entity } from '@src/Entity.js'
Expand Down Expand Up @@ -108,14 +108,6 @@ export class ArxMap {
],
}

cashedBBox: {
numberOfPolygons: number
value: Box3
} = {
numberOfPolygons: 0,
value: new Box3(),
}

constructor(dlf?: ArxDLF, fts?: ArxFTS, llf?: ArxLLF, areNormalsCalculated = false) {
if (typeof dlf === 'undefined' || typeof fts === 'undefined' || typeof llf === 'undefined') {
return
Expand Down Expand Up @@ -244,11 +236,11 @@ export class ArxMap {
throw new MapFinalizedError()
}

const numberOfRemovedPolygons = $(this.polygons).clearSelection().selectOutOfBounds().delete()
const removedPolygons = $(this.polygons).clearSelection().selectOutOfBounds().delete()

if (numberOfRemovedPolygons > 0) {
if (removedPolygons.length > 0) {
console.warn(
`[warning] ArxMap: Removed ${numberOfRemovedPolygons} polygons what are outside the 0..16000 boundary on the X or Z axis`,
`[warning] ArxMap: Removed ${removedPolygons.length} polygons what are outside the 0..16000 boundary on the X or Z axis`,
)
}

Expand Down Expand Up @@ -616,46 +608,4 @@ export class ArxMap {
// TODO: adjust fts anchor linked anchor indices
// TODO: adjust fts polygon texture container ids
}

getBoundingBox() {
// TODO: this isn't ideal when only a vertex gets changed, but not the number of polygons
if (this.cashedBBox.numberOfPolygons === this.polygons.length) {
return this.cashedBBox.value
}

const box = new Box3()

for (const polygon of this.polygons) {
for (let i = 0; i < (polygon.isQuad() ? 4 : 3); i++) {
box.expandByPoint(polygon.vertices[i])
}
}

this.cashedBBox.numberOfPolygons = this.polygons.length
this.cashedBBox.value = box

return box
}

getCenter() {
const bb = this.getBoundingBox()
const center = new Vector3()
bb.getCenter(center)
return center
}

getHeight() {
const { max, min } = this.getBoundingBox()
return max.y - min.y
}

getWidth() {
const { max, min } = this.getBoundingBox()
return max.x - min.x
}

getDepth() {
const { max, min } = this.getBoundingBox()
return max.z - min.z
}
}
52 changes: 51 additions & 1 deletion src/Polygons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
TripleOf,
isTiled,
} from 'arx-convert/utils'
import { Mesh, MeshBasicMaterial, Object3D, BufferAttribute } from 'three'
import { Mesh, MeshBasicMaterial, Object3D, BufferAttribute, Box3 } from 'three'
import { Color } from '@src/Color.js'
import { Material } from '@src/Material.js'
import { Polygon, TransparencyType } from '@src/Polygon.js'
Expand Down Expand Up @@ -43,6 +43,14 @@ type VertexWithMaterialIndex = {
}

export class Polygons extends Array<Polygon> {
cashedBBox: {
numberOfPolygons: number
value: Box3
} = {
numberOfPolygons: 0,
value: new Box3(),
}

async exportTextures(settings: Settings) {
const texturesToExport: {
tileable: Record<string, Texture>
Expand Down Expand Up @@ -409,4 +417,46 @@ export class Polygons extends Array<Polygon> {

return colors
}

getBoundingBox() {
// TODO: this isn't ideal when only a vertex gets changed, but not the number of polygons
if (this.cashedBBox.numberOfPolygons === this.length) {
return this.cashedBBox.value
}

const bbox = new Box3()

for (const polygon of this) {
const { min, max } = polygon.getBoundingBox()
bbox.expandByPoint(min)
bbox.expandByPoint(max)
}

this.cashedBBox.numberOfPolygons = this.length
this.cashedBBox.value = bbox

return bbox
}

getCenter() {
const bb = this.getBoundingBox()
const center = new Vector3()
bb.getCenter(center)
return center
}

getHeight() {
const { max, min } = this.getBoundingBox()
return max.y - min.y
}

getWidth() {
const { max, min } = this.getBoundingBox()
return max.x - min.x
}

getDepth() {
const { max, min } = this.getBoundingBox()
return max.z - min.z
}
}

0 comments on commit 6fb2f5b

Please sign in to comment.