11import macro from 'vtk.js/Sources/macros' ;
22import vtk from 'vtk.js/Sources/vtk' ;
3+ import vtkBoundingBox from 'vtk.js/Sources/Common/DataModel/BoundingBox' ;
34import vtkDataSetAttributes from 'vtk.js/Sources/Common/DataModel/DataSetAttributes' ;
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,73 @@ 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+ vtkBoundingBox . setBounds ( model . bounds , points . getBoundsByReference ( ) ) ;
44+ } else {
45+ model . bounds = vtkMath . createUninitializedBounds ( ) ;
46+ }
47+ // Update computeTime
48+ model . computeTime = macro . getCurrentGlobalMTime ( ) ;
49+ }
50+ } ;
51+
52+ /**
53+ * Returns the squared length of the diagonal of the bounding box
54+ */
55+ publicAPI . getLength2 = ( ) => {
56+ const bounds = publicAPI . getBoundsByReference ( ) ;
57+ if ( ! bounds || bounds . length !== 6 ) return 0 ;
58+ return vtkBoundingBox . getDiagonalLength2 ( bounds ) ;
59+ } ;
60+
61+ /**
62+ * Returns the length of the diagonal of the bounding box
63+ */
64+ publicAPI . getLength = ( ) => Math . sqrt ( publicAPI . getLength2 ( ) ) ;
65+
66+ /**
67+ * Returns the center of the bounding box as [x, y, z]
68+ */
69+ publicAPI . getCenter = ( ) => {
70+ const bounds = publicAPI . getBoundsByReference ( ) ;
71+ if ( ! bounds || bounds . length !== 6 ) return [ 0 , 0 , 0 ] ;
72+ return vtkBoundingBox . getCenter ( bounds ) ;
73+ } ;
74+
75+ /**
76+ * Get the bounding box of a cell with the given cellId
77+ * @param {Number } cellId - The id of the cell
78+ * @returns {Number[] } - The bounds as [xmin, xmax, ymin, ymax, zmin, zmax]
79+ */
80+ publicAPI . getCellBounds = ( cellId ) => {
81+ const cell = publicAPI . getCell ( cellId ) ;
82+ if ( cell ) {
83+ return cell . getBounds ( ) ;
84+ }
85+ return vtkMath . createUninitializedBounds ( ) ;
86+ } ;
87+
88+ publicAPI . getBounds = macro . chain (
89+ ( ) => publicAPI . computeBounds ,
90+ publicAPI . getBounds
91+ ) ;
92+
93+ publicAPI . getBoundsByReference = macro . chain (
94+ ( ) => publicAPI . computeBounds ,
95+ publicAPI . getBoundsByReference
96+ ) ;
97+
6098 const superShallowCopy = publicAPI . shallowCopy ;
6199 publicAPI . shallowCopy = ( other , debug = false ) => {
62100 superShallowCopy ( other , debug ) ;
@@ -98,7 +136,7 @@ export function extend(publicAPI, model, initialValues = {}) {
98136 // Object methods
99137 macro . obj ( publicAPI , model ) ;
100138 macro . setGet ( publicAPI , model , DATASET_FIELDS ) ;
101-
139+ macro . getArray ( publicAPI , model , [ 'bounds' ] , 6 ) ;
102140 // Object specific methods
103141 vtkDataSet ( publicAPI , model ) ;
104142}
0 commit comments