Skip to content

Commit be68190

Browse files
committed
Make zoom to fit, update version
1 parent 4e4a014 commit be68190

File tree

11 files changed

+48
-18
lines changed

11 files changed

+48
-18
lines changed

examples/simple-angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"three": "^0.129.0",
2727
"tslib": "^2.1.0",
2828
"web-ifc": "^0.0.20",
29-
"web-ifc-viewer": "^1.0.11",
29+
"web-ifc-viewer": "1.0.14",
3030
"zone.js": "~0.11.4"
3131
},
3232
"devDependencies": {

examples/simple-angular/src/app/services/ifc.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export class IfcService {
4747
}
4848

4949
loadIfc(file: File) {
50-
this.ifcViewer?.loadIfc(file);
50+
this.ifcViewer?.loadIfc(file, true);
5151
}
5252

5353
select(modelID: number, expressID: number, pick = true) {

examples/simple-angular/src/app/spatial-tree/spatial-tree.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { IfcService } from '../services/ifc.service';
22
import { NestedTreeControl } from '@angular/cdk/tree';
3-
import { ChangeDetectionStrategy, Component, Output, EventEmitter } from '@angular/core';
3+
import { ChangeDetectionStrategy, Component } from '@angular/core';
44
import { MatTreeNestedDataSource } from '@angular/material/tree';
55

66
/**

examples/simple-html/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "web-ifc-viewer-basic-example",
33
"private": true,
44
"type": "module",
5-
"version": "1.0.11",
5+
"version": "1.0.12",
66
"description": "A basic html example for web-ifc-viewer",
77
"main": "index.html",
88
"scripts": {
@@ -21,6 +21,6 @@
2121
},
2222
"dependencies": {
2323
"stats.js": "^0.17.0",
24-
"web-ifc-viewer": "^1.0.11"
24+
"web-ifc-viewer": "1.0.14"
2525
}
2626
}

examples/simple-react/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web-ifc-viewer-simple-react",
3-
"version": "1.0.11",
3+
"version": "1.0.12",
44
"private": true,
55
"dependencies": {
66
"@material-ui/core": "^4.11.4",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "web-ifc-viewer-root",
33
"description": "IFC viewer",
4-
"version": "1.0.11",
4+
"version": "1.0.14",
55
"private": true,
66
"author": "agviegas",
77
"license": "MIT",

viewer/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "web-ifc-viewer",
3-
"version": "1.0.11",
3+
"version": "1.0.14",
44
"description": "IFC viewer",
55
"main": "dist/index.js",
66
"scripts": {
@@ -31,7 +31,7 @@
3131
"@types/stats.js": "^0.17.0",
3232
"stats.js": "^0.17.0",
3333
"three": "^0.128.0",
34-
"web-ifc-three": "^0.0.30"
34+
"web-ifc-three": "0.0.31"
3535
},
3636
"devDependencies": {
3737
"@types/jest": "^26.0.23",

viewer/src/components/context/camera.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { PerspectiveCamera, Vector3, MOUSE } from 'three';
1+
import { PerspectiveCamera, Vector3, MOUSE, Box3, MathUtils } from 'three';
22
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';
33
import { IfcComponent, Context } from '../../base-types';
44

@@ -33,6 +33,30 @@ export class IfcCamera extends IfcComponent {
3333
this.controls.enabled = active;
3434
}
3535

36+
fitModelToFrame() {
37+
const scene = this.context.getScene();
38+
const box = new Box3().setFromObject(scene.children[scene.children.length - 1]);
39+
const boxSize = box.getSize(new Vector3()).length();
40+
const boxCenter = box.getCenter(new Vector3());
41+
42+
const halfSizeToFitOnScreen = boxSize * 0.5;
43+
const halfFovY = MathUtils.degToRad(this.camera.fov * 0.5);
44+
const distance = halfSizeToFitOnScreen / Math.tan(halfFovY);
45+
46+
const direction = new Vector3()
47+
.subVectors(this.camera.position, boxCenter)
48+
.multiply(new Vector3(1, 0, 1))
49+
.normalize();
50+
51+
this.camera.position.copy(direction.multiplyScalar(distance).add(boxCenter));
52+
this.camera.updateProjectionMatrix();
53+
this.camera.lookAt(boxCenter.x, boxCenter.y, boxCenter.z);
54+
55+
// set target to newest loaded model
56+
this.controls.target.copy(boxCenter);
57+
this.controls.update();
58+
}
59+
3660
private setupCamera() {
3761
// camera.up = new Vector3(0, 0, 1);
3862
this.camera.position.z = 8;

viewer/src/components/context/context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ export class IfcContext implements Context {
8181
return this.ifcCaster.castRayIfc();
8282
}
8383

84+
fitToFrame() {
85+
this.ifcCamera.fitModelToFrame();
86+
}
87+
8488
private render = () => {
8589
requestAnimationFrame(this.render);
8690
this.updateAllComponents();

viewer/src/ifc-viewer-api.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,14 @@ export class IfcViewerAPI {
6767
// if(!this.edges) this.edges
6868
// }
6969

70-
loadIfc = async (file: File) => {
71-
this.ifcManager.loadIfc(file);
70+
loadIfc = async (file: File, fitToFrame = false) => {
71+
await this.ifcManager.loadIfc(file);
72+
if (fitToFrame) this.context.fitToFrame();
7273
};
7374

74-
loadIfcUrl = async (fileUrl: string) => {
75-
this.ifcManager.loadIfcUrl(fileUrl);
75+
loadIfcUrl = async (fileUrl: string, fitToFrame = false) => {
76+
await this.ifcManager.loadIfcUrl(fileUrl);
77+
if (fitToFrame) this.context.fitToFrame();
7678
};
7779

7880
setWasmPath(path: string) {

0 commit comments

Comments
 (0)