Skip to content
This repository was archived by the owner on Dec 17, 2024. It is now read-only.

Commit 04e8f89

Browse files
authored
Merge pull request #251 from vimaec/sroberge/2_0_10
2.0.10 - Section Box Fix
2 parents 46c57f0 + 9d9e7f8 commit 04e8f89

File tree

10 files changed

+245
-54
lines changed

10 files changed

+245
-54
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "vim-webgl-viewer",
3-
"version": "2.0.9",
3+
"version": "2.0.10",
44
"description": "A high-performance 3D viewer and VIM file loader built on top of Three.JS.",
55
"files": [
66
"dist"
@@ -57,7 +57,7 @@
5757
"ste-signals": "^3.0.9",
5858
"ste-simple-events": "^3.0.7",
5959
"three": "0.143.0",
60-
"vim-format": "1.0.6"
60+
"vim-format": "1.0.14"
6161
},
6262
"keywords": [
6363
"3d",

src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
// Links files to generate package type exports
22
import './style.css'
3+
import { BFastSource } from 'vim-format'
34
export * as THREE from 'three'
45

6+
export type VimSource = BFastSource
57
export { IProgressLogs } from 'vim-format'
68
export * from './vim-loader/progressive/open'
9+
export * from './vim-loader/progressive/vimRequest'
710
export * from './vim-loader/progressive/vimx'
811
export * from './vim-webgl-viewer/viewer'
912
export * from './vim-loader/geometry'

src/main.ts

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,45 @@
1-
import { Viewer, open, THREE, getViewerSettingsFromUrl } from '.'
1+
import { Viewer, request, THREE, getViewerSettingsFromUrl } from '.'
22

33
// Parse URL for source file
44
const params = new URLSearchParams(window.location.search)
55
const url = params.has('vim')
66
? params.get('vim')
77
: null
88

9-
let time: number
10-
119
const viewer = new Viewer({
1210
...getViewerSettingsFromUrl(window.location.search)
1311
})
1412

1513
load(url ?? 'https://vim02.azureedge.net/samples/residence.v1.2.75.vim')
16-
// load(url ?? './F_A_X_X_0_001.vim')
17-
// load(url ?? "https://vimdevelopment01storage.blob.core.windows.net/samples/TowerS-ARCHITECTURE-ALL.v1.2.50.vimx")
18-
// load(url ?? "https://vimdevelopment01storage.blob.core.windows.net/samples/BIM1-AUTOP_ARC_2023.vimx")
19-
// load('https://vim.azureedge.net/samples/TowerS-ARCHITECTURE.1.2.88-ALL.vim')
20-
2114
addLoadButton()
2215

2316
async function load (url: string | ArrayBuffer) {
24-
time = Date.now()
2517
viewer.gizmos.loading.visible = true
2618

27-
const vim = await open(url,
28-
{
29-
rotation: new THREE.Vector3(270, 0, 0)
30-
}, (p) => console.log(`Downloading Vim (${(p.loaded / 1000).toFixed(0)} kb)`)
31-
)
19+
const r = request({
20+
url: 'https://vimdevelopment01storage.blob.core.windows.net/samples/Wolford_Residence.r2025.vim'
21+
},
22+
{
23+
rotation: new THREE.Vector3(270, 0, 0)
24+
})
25+
26+
for await (const progress of r.getProgress()) {
27+
console.log(`Downloading Vim (${(progress.loaded / 1000).toFixed(0)} kb)`)
28+
}
29+
30+
const result = await r.getResult()
31+
viewer.gizmos.loading.visible = false
32+
if (result.isError()) {
33+
console.error(result.error)
34+
return
35+
}
36+
37+
const vim = result.result
3238

33-
viewer.add(vim)
3439
await vim.loadAll()
40+
viewer.add(vim)
3541
viewer.camera.snap(true).frame(vim)
3642
viewer.camera.save()
37-
viewer.gizmos.loading.visible = false
3843

3944
// Useful for debuging in console.
4045
globalThis.THREE = THREE

src/utils/deferredPromise.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
export class DeferredPromise<T> extends Promise<T> {
2+
resolve: (value: T | PromiseLike<T>) => void
3+
reject: (reason: T | Error) => void
4+
5+
initialCallStack: Error['stack']
6+
7+
constructor (executor: ConstructorParameters<typeof Promise<T>>[0] = () => {}) {
8+
let resolver: (value: T | PromiseLike<T>) => void
9+
let rejector: (reason: T | Error) => void
10+
11+
super((resolve, reject) => {
12+
resolver = resolve
13+
rejector = reject
14+
return executor(resolve, reject) // Promise magic: this line is unexplicably essential
15+
})
16+
17+
this.resolve = resolver!
18+
this.reject = rejector!
19+
20+
// store call stack for location where instance is created
21+
this.initialCallStack = Error().stack?.split('\n').slice(2).join('\n')
22+
}
23+
24+
/** @throws error with amended call stack */
25+
rejectWithError (error: Error) {
26+
error.stack = [error.stack?.split('\n')[0], this.initialCallStack].join('\n')
27+
this.reject(error)
28+
}
29+
}

src/utils/requestResult.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
export class SuccessResult<T> {
3+
result: T
4+
5+
constructor (result: T) {
6+
this.result = result
7+
}
8+
9+
isSuccess (): this is SuccessResult<T> {
10+
return true
11+
}
12+
13+
isError (): false {
14+
return false
15+
}
16+
}
17+
18+
export class ErrorResult {
19+
error: string
20+
21+
constructor (error: string) {
22+
this.error = error
23+
}
24+
25+
isSuccess (): false {
26+
return false
27+
}
28+
29+
isError (): this is ErrorResult {
30+
return true
31+
}
32+
}
33+
34+
export type RequestResult<T> = SuccessResult<T> | ErrorResult

src/vim-loader/progressive/g3dSubset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* @module vim-loader
33
*/
44

5-
import { G3d, MeshSection, G3dScene, FilterModeNext, FilterMode } from 'vim-format'
5+
import { G3d, MeshSection, G3dScene, FilterMode } from 'vim-format'
66
import { G3dMeshOffsets, G3dMeshCounts } from './g3dOffsets'
77
import * as THREE from 'three'
88

@@ -261,7 +261,7 @@ export class G3dSubset {
261261
}
262262

263263
private _filter (
264-
mode: FilterModeNext,
264+
mode: FilterMode,
265265
filter: number[] | Set<number>,
266266
has: boolean
267267
): G3dSubset {

src/vim-loader/progressive/open.ts

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { Vim } from '../vim'
99
import { Scene } from '../scene'
1010
import { Vimx } from './vimx'
1111

12+
import { VimSource } from '../../index'
1213
import { ElementMapping, ElementMapping2 } from '../elementMapping'
1314
import {
1415
BFast,
@@ -26,71 +27,68 @@ import { DefaultLog } from 'vim-format/dist/logging'
2627

2728
/**
2829
* Asynchronously opens a vim object from a given source with the provided settings.
29-
* @param {string | ArrayBuffer} source - The source of the vim object, either a string or an ArrayBuffer.
30+
* @param {string | BFast} source - The source of the vim object, either a string or a BFast.
3031
* @param {VimPartialSettings} settings - The settings to configure the behavior of the vim object.
3132
* @param {(p: IProgressLogs) => void} [onProgress] - Optional callback function to track progress logs.
3233
* @returns {Promise<void>} A Promise that resolves when the vim object is successfully opened.
3334
*/
3435
export async function open (
35-
source: string | ArrayBuffer,
36+
source: VimSource | BFast,
3637
settings: VimPartialSettings,
3738
onProgress?: (p: IProgressLogs) => void
3839
) {
40+
const bfast = source instanceof BFast ? source : new BFast(source)
3941
const fullSettings = getFullSettings(settings)
40-
const type = await determineFileType(source, fullSettings)!
42+
const type = await determineFileType(bfast, fullSettings)!
4143

4244
if (type === 'vim') {
43-
return loadFromVim(source, fullSettings, onProgress)
45+
return loadFromVim(bfast, fullSettings, onProgress)
4446
}
4547

4648
if (type === 'vimx') {
47-
return loadFromVimX(source, fullSettings, onProgress)
49+
return loadFromVimX(bfast, fullSettings, onProgress)
4850
}
4951

5052
throw new Error('Cannot determine the appropriate loading strategy.')
5153
}
5254

5355
async function determineFileType (
54-
vimPath: string | ArrayBuffer,
56+
bfast: BFast,
5557
settings: VimSettings
5658
) {
5759
if (settings?.fileType === 'vim') return 'vim'
5860
if (settings?.fileType === 'vimx') return 'vimx'
59-
return requestFileType(vimPath)
61+
return requestFileType(bfast)
6062
}
6163

62-
async function requestFileType (vimPath: string | ArrayBuffer) {
63-
if (typeof vimPath === 'string') {
64-
if (vimPath.endsWith('vim')) return 'vim'
65-
if (vimPath.endsWith('vimx')) return 'vimx'
64+
async function requestFileType (bfast: BFast) {
65+
if (bfast.url) {
66+
if (bfast.url.endsWith('vim')) return 'vim'
67+
if (bfast.url.endsWith('vimx')) return 'vimx'
6668
}
6769

68-
const bfast = new BFast(vimPath)
6970
const header = await requestHeader(bfast)
70-
7171
if (header.vim !== undefined) return 'vim'
7272
if (header.vimx !== undefined) return 'vimx'
7373

7474
throw new Error('Cannot determine file type from header.')
7575
}
7676

7777
/**
78-
* Loads a Vimx file from source
79-
*/
78+
* Loads a Vimx file from source
79+
*/
8080
async function loadFromVimX (
81-
source: string | ArrayBuffer,
81+
bfast: BFast,
8282
settings: VimSettings,
8383
onProgress: (p: IProgressLogs) => void
8484
) {
8585
// Fetch geometry data
86-
const remoteVimx = new RemoteVimx(source)
86+
const remoteVimx = new RemoteVimx(bfast)
8787
if (remoteVimx.bfast.source instanceof RemoteBuffer) {
8888
remoteVimx.bfast.source.onProgress = onProgress
8989
}
9090

91-
console.log('Downloading Scene Index..')
9291
const vimx = await Vimx.fromRemote(remoteVimx, !settings.progressive)
93-
console.log('Scene Index Downloaded.')
9492

9593
// Create scene
9694
const scene = new Scene(settings.matrix)
@@ -109,7 +107,7 @@ async function loadFromVimX (
109107
settings,
110108
mapping,
111109
builder,
112-
typeof source === 'string' ? source : undefined,
110+
bfast.url,
113111
'vimx'
114112
)
115113

@@ -121,15 +119,15 @@ async function loadFromVimX (
121119
}
122120

123121
/**
124-
* Loads a Vim file from source
125-
*/
122+
* Loads a Vim file from source
123+
*/
126124
async function loadFromVim (
127-
source: string | ArrayBuffer,
125+
bfast: BFast,
128126
settings: VimSettings,
129127
onProgress?: (p: IProgressLogs) => void
130128
) {
131129
const fullSettings = getFullSettings(settings)
132-
const bfast = new BFast(source)
130+
133131
if (bfast.source instanceof RemoteBuffer) {
134132
bfast.source.onProgress = onProgress
135133
if (settings.verboseHttp) {
@@ -147,7 +145,7 @@ async function loadFromVim (
147145
const factory = new VimMeshFactory(g3d, materials, scene)
148146

149147
// Create legacy mapping
150-
const doc = await VimDocument.createFromBfast(bfast, true)
148+
const doc = await VimDocument.createFromBfast(bfast)
151149
const mapping = await ElementMapping.fromG3d(g3d, doc)
152150
const header = await requestHeader(bfast)
153151

@@ -161,7 +159,7 @@ async function loadFromVim (
161159
fullSettings,
162160
mapping,
163161
builder,
164-
typeof source === 'string' ? source : undefined,
162+
bfast.url,
165163
'vim'
166164
)
167165

0 commit comments

Comments
 (0)