11import macro from 'vtk.js/Sources/macros' ;
22import vtk from 'vtk.js/Sources/vtk' ;
33import vtkDataSetAttributes from 'vtk.js/Sources/Common/DataModel/DataSetAttributes' ;
4+ import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox' ;
5+ import vtkMath from 'vtk.js/Sources/Common/Core/Math' ;
46import Constants from 'vtk.js/Sources/Common/DataModel/DataSet/Constants' ;
57
6- // import vtkBoundingBox from '../BoundingBox';
7- // import * as vtkMath from '../../Core/Math';
8- //
9- // function getBounds(dataset) {
10- // if (dataset.bounds) {
11- // return dataset.bounds;
12- // }
13- // if (dataset.type && dataset[dataset.type]) {
14- // const ds = dataset[dataset.type];
15- // if (ds.bounds) {
16- // return ds.bounds;
17- // }
18- // if (ds.Points && ds.Points.bounds) {
19- // return ds.Points.bounds;
20- // }
21-
22- // if (ds.Points && ds.Points.values) {
23- // const array = ds.Points.values;
24- // const bbox = [...vtkBoundingBox.INIT_BOUNDS];
25- // const size = array.length;
26- // const delta = ds.Points.numberOfComponents ? ds.Points.numberOfComponents : 3;
27- // for (let idx = 0; idx < size; idx += delta) {
28- // vtkBoundingBox.addPoint(bbox, array[idx * delta], array[(idx * delta) + 1], array[(idx * delta) + 2]);
29- // }
30- // ds.Points.bounds = bbox;
31- // return ds.Points.bounds;
32- // }
33- // }
34- // return vtkMath.createUninitializedBounds();
35- // }
36-
378// ----------------------------------------------------------------------------
389// Global methods
3910// ----------------------------------------------------------------------------
@@ -57,6 +28,87 @@ function vtkDataSet(publicAPI, model) {
5728 }
5829 } ) ;
5930
31+ //------------------------------------------------------------------------------
32+ // Compute the data bounding box from data points.
33+ publicAPI . computeBounds = ( ) => {
34+ if (
35+ ( model . modifiedTime &&
36+ model . computeTime &&
37+ model . modifiedTime > model . computeTime ) ||
38+ ! model . computeTime
39+ ) {
40+ const points = publicAPI . getPoints ( ) ;
41+ if ( points ?. getNumberOfPoints ( ) ) {
42+ // Compute bounds from points
43+ const bounds = vtkBoundingBox . INIT_BOUNDS . slice ( ) ;
44+ const numberOfPoints = points . getNumberOfPoints ( ) ;
45+ for ( let i = 0 ; i < numberOfPoints ; i ++ ) {
46+ const pt = points . getPoint ( i ) ;
47+ vtkBoundingBox . addPoint ( bounds , pt [ 0 ] , pt [ 1 ] , pt [ 2 ] ) ;
48+ }
49+ model . bounds = bounds ;
50+ } else {
51+ model . bounds = vtkMath . createUninitializedBounds ( ) ;
52+ }
53+ // Update computeTime
54+ model . computeTime = Date . now ( ) ;
55+ }
56+ } ;
57+
58+ /**
59+ * Returns the bounds as [xmin, xmax, ymin, ymax, zmin, zmax]
60+ */
61+ publicAPI . getBounds = ( ) => {
62+ if ( model . bounds && model . bounds . length === 6 ) {
63+ return model . bounds ;
64+ }
65+ // fallback: try to get bounds from pointData if available
66+ if ( publicAPI . getPoints && publicAPI . getPoints ( ) ) {
67+ return publicAPI . getPoints ( ) . getBounds ( ) ;
68+ }
69+ return vtkMath . createUninitializedBounds ( ) ;
70+ } ;
71+
72+ /**
73+ * Returns the squared length of the diagonal of the bounding box
74+ */
75+ publicAPI . getLength2 = ( ) => {
76+ const bounds = publicAPI . getBounds ( ) ;
77+ if ( ! bounds || bounds . length !== 6 ) return 0 ;
78+ return vtkBoundingBox . getDiagonalLength2 ( bounds ) ;
79+ } ;
80+
81+ /**
82+ * Returns the length of the diagonal of the bounding box
83+ */
84+ publicAPI . getLength = ( ) => Math . sqrt ( publicAPI . getLength2 ( ) ) ;
85+
86+ /**
87+ * Returns the center of the bounding box as [x, y, z]
88+ */
89+ publicAPI . getCenter = ( ) => {
90+ const bounds = publicAPI . getBounds ( ) ;
91+ if ( ! bounds || bounds . length !== 6 ) return [ 0 , 0 , 0 ] ;
92+ return [
93+ ( bounds [ 0 ] + bounds [ 1 ] ) / 2 ,
94+ ( bounds [ 2 ] + bounds [ 3 ] ) / 2 ,
95+ ( bounds [ 4 ] + bounds [ 5 ] ) / 2 ,
96+ ] ;
97+ } ;
98+
99+ /**
100+ * Get the bounding box of a cell with the given cellId
101+ * @param {Number } cellId - The id of the cell
102+ * @returns {Number[] } - The bounds as [xmin, xmax, ymin, ymax, zmin, zmax]
103+ */
104+ publicAPI . getCellBounds = ( cellId ) => {
105+ const cell = publicAPI . getCell ( cellId ) ;
106+ if ( cell ) {
107+ return cell . getBounds ( ) ;
108+ }
109+ return vtkMath . createUninitializedBounds ( ) ;
110+ } ;
111+
60112 const superShallowCopy = publicAPI . shallowCopy ;
61113 publicAPI . shallowCopy = ( other , debug = false ) => {
62114 superShallowCopy ( other , debug ) ;
0 commit comments