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

Commit

Permalink
Merge pull request #251 from vimaec/sroberge/2_0_10
Browse files Browse the repository at this point in the history
2.0.10 - Section Box Fix
  • Loading branch information
vim-sroberge authored Oct 28, 2024
2 parents 46c57f0 + 9d9e7f8 commit 04e8f89
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 54 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vim-webgl-viewer",
"version": "2.0.9",
"version": "2.0.10",
"description": "A high-performance 3D viewer and VIM file loader built on top of Three.JS.",
"files": [
"dist"
Expand Down Expand Up @@ -57,7 +57,7 @@
"ste-signals": "^3.0.9",
"ste-simple-events": "^3.0.7",
"three": "0.143.0",
"vim-format": "1.0.6"
"vim-format": "1.0.14"
},
"keywords": [
"3d",
Expand Down
3 changes: 3 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
// Links files to generate package type exports
import './style.css'
import { BFastSource } from 'vim-format'
export * as THREE from 'three'

export type VimSource = BFastSource
export { IProgressLogs } from 'vim-format'
export * from './vim-loader/progressive/open'
export * from './vim-loader/progressive/vimRequest'
export * from './vim-loader/progressive/vimx'
export * from './vim-webgl-viewer/viewer'
export * from './vim-loader/geometry'
Expand Down
37 changes: 21 additions & 16 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,45 @@
import { Viewer, open, THREE, getViewerSettingsFromUrl } from '.'
import { Viewer, request, THREE, getViewerSettingsFromUrl } from '.'

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

let time: number

const viewer = new Viewer({
...getViewerSettingsFromUrl(window.location.search)
})

load(url ?? 'https://vim02.azureedge.net/samples/residence.v1.2.75.vim')
// load(url ?? './F_A_X_X_0_001.vim')
// load(url ?? "https://vimdevelopment01storage.blob.core.windows.net/samples/TowerS-ARCHITECTURE-ALL.v1.2.50.vimx")
// load(url ?? "https://vimdevelopment01storage.blob.core.windows.net/samples/BIM1-AUTOP_ARC_2023.vimx")
// load('https://vim.azureedge.net/samples/TowerS-ARCHITECTURE.1.2.88-ALL.vim')

addLoadButton()

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

const vim = await open(url,
{
rotation: new THREE.Vector3(270, 0, 0)
}, (p) => console.log(`Downloading Vim (${(p.loaded / 1000).toFixed(0)} kb)`)
)
const r = request({
url: 'https://vimdevelopment01storage.blob.core.windows.net/samples/Wolford_Residence.r2025.vim'
},
{
rotation: new THREE.Vector3(270, 0, 0)
})

for await (const progress of r.getProgress()) {
console.log(`Downloading Vim (${(progress.loaded / 1000).toFixed(0)} kb)`)
}

const result = await r.getResult()
viewer.gizmos.loading.visible = false
if (result.isError()) {
console.error(result.error)
return
}

const vim = result.result

viewer.add(vim)
await vim.loadAll()
viewer.add(vim)
viewer.camera.snap(true).frame(vim)
viewer.camera.save()
viewer.gizmos.loading.visible = false

// Useful for debuging in console.
globalThis.THREE = THREE
Expand Down
29 changes: 29 additions & 0 deletions src/utils/deferredPromise.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export class DeferredPromise<T> extends Promise<T> {
resolve: (value: T | PromiseLike<T>) => void
reject: (reason: T | Error) => void

initialCallStack: Error['stack']

constructor (executor: ConstructorParameters<typeof Promise<T>>[0] = () => {}) {
let resolver: (value: T | PromiseLike<T>) => void
let rejector: (reason: T | Error) => void

super((resolve, reject) => {
resolver = resolve
rejector = reject
return executor(resolve, reject) // Promise magic: this line is unexplicably essential
})

this.resolve = resolver!
this.reject = rejector!

// store call stack for location where instance is created
this.initialCallStack = Error().stack?.split('\n').slice(2).join('\n')
}

/** @throws error with amended call stack */
rejectWithError (error: Error) {
error.stack = [error.stack?.split('\n')[0], this.initialCallStack].join('\n')
this.reject(error)
}
}
34 changes: 34 additions & 0 deletions src/utils/requestResult.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@

export class SuccessResult<T> {
result: T

constructor (result: T) {
this.result = result
}

isSuccess (): this is SuccessResult<T> {
return true
}

isError (): false {
return false
}
}

export class ErrorResult {
error: string

constructor (error: string) {
this.error = error
}

isSuccess (): false {
return false
}

isError (): this is ErrorResult {
return true
}
}

export type RequestResult<T> = SuccessResult<T> | ErrorResult
4 changes: 2 additions & 2 deletions src/vim-loader/progressive/g3dSubset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* @module vim-loader
*/

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

Expand Down Expand Up @@ -261,7 +261,7 @@ export class G3dSubset {
}

private _filter (
mode: FilterModeNext,
mode: FilterMode,
filter: number[] | Set<number>,
has: boolean
): G3dSubset {
Expand Down
50 changes: 24 additions & 26 deletions src/vim-loader/progressive/open.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { Vim } from '../vim'
import { Scene } from '../scene'
import { Vimx } from './vimx'

import { VimSource } from '../../index'
import { ElementMapping, ElementMapping2 } from '../elementMapping'
import {
BFast,
Expand All @@ -26,71 +27,68 @@ import { DefaultLog } from 'vim-format/dist/logging'

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

if (type === 'vim') {
return loadFromVim(source, fullSettings, onProgress)
return loadFromVim(bfast, fullSettings, onProgress)
}

if (type === 'vimx') {
return loadFromVimX(source, fullSettings, onProgress)
return loadFromVimX(bfast, fullSettings, onProgress)
}

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

async function determineFileType (
vimPath: string | ArrayBuffer,
bfast: BFast,
settings: VimSettings
) {
if (settings?.fileType === 'vim') return 'vim'
if (settings?.fileType === 'vimx') return 'vimx'
return requestFileType(vimPath)
return requestFileType(bfast)
}

async function requestFileType (vimPath: string | ArrayBuffer) {
if (typeof vimPath === 'string') {
if (vimPath.endsWith('vim')) return 'vim'
if (vimPath.endsWith('vimx')) return 'vimx'
async function requestFileType (bfast: BFast) {
if (bfast.url) {
if (bfast.url.endsWith('vim')) return 'vim'
if (bfast.url.endsWith('vimx')) return 'vimx'
}

const bfast = new BFast(vimPath)
const header = await requestHeader(bfast)

if (header.vim !== undefined) return 'vim'
if (header.vimx !== undefined) return 'vimx'

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

/**
* Loads a Vimx file from source
*/
* Loads a Vimx file from source
*/
async function loadFromVimX (
source: string | ArrayBuffer,
bfast: BFast,
settings: VimSettings,
onProgress: (p: IProgressLogs) => void
) {
// Fetch geometry data
const remoteVimx = new RemoteVimx(source)
const remoteVimx = new RemoteVimx(bfast)
if (remoteVimx.bfast.source instanceof RemoteBuffer) {
remoteVimx.bfast.source.onProgress = onProgress
}

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

// Create scene
const scene = new Scene(settings.matrix)
Expand All @@ -109,7 +107,7 @@ async function loadFromVimX (
settings,
mapping,
builder,
typeof source === 'string' ? source : undefined,
bfast.url,
'vimx'
)

Expand All @@ -121,15 +119,15 @@ async function loadFromVimX (
}

/**
* Loads a Vim file from source
*/
* Loads a Vim file from source
*/
async function loadFromVim (
source: string | ArrayBuffer,
bfast: BFast,
settings: VimSettings,
onProgress?: (p: IProgressLogs) => void
) {
const fullSettings = getFullSettings(settings)
const bfast = new BFast(source)

if (bfast.source instanceof RemoteBuffer) {
bfast.source.onProgress = onProgress
if (settings.verboseHttp) {
Expand All @@ -147,7 +145,7 @@ async function loadFromVim (
const factory = new VimMeshFactory(g3d, materials, scene)

// Create legacy mapping
const doc = await VimDocument.createFromBfast(bfast, true)
const doc = await VimDocument.createFromBfast(bfast)
const mapping = await ElementMapping.fromG3d(g3d, doc)
const header = await requestHeader(bfast)

Expand All @@ -161,7 +159,7 @@ async function loadFromVim (
fullSettings,
mapping,
builder,
typeof source === 'string' ? source : undefined,
bfast.url,
'vim'
)

Expand Down
Loading

0 comments on commit 04e8f89

Please sign in to comment.