@@ -241,7 +241,7 @@ function vtkDataArray(publicAPI, model) {
241241 publicAPI . getNumberOfComponents = ( ) => model . numberOfComponents ;
242242 publicAPI . getNumberOfValues = ( ) => model . values . length ;
243243 publicAPI . getNumberOfTuples = ( ) =>
244- model . values . length / model . numberOfComponents ;
244+ model . values ? model . values . length / model . numberOfComponents : 0 ;
245245 publicAPI . getDataType = ( ) => model . dataType ;
246246 /* eslint-disable no-use-before-define */
247247 publicAPI . newClone = ( ) =>
@@ -262,9 +262,14 @@ function vtkDataArray(publicAPI, model) {
262262 } ;
263263
264264 publicAPI . setData = ( typedArray , numberOfComponents ) => {
265- model . values = typedArray ;
266- model . size = typedArray . length ;
267- model . dataType = getDataType ( typedArray ) ;
265+ if ( typedArray ) {
266+ model . values = typedArray ;
267+ model . size = typedArray . length ;
268+ model . empty = model . size === 0 ;
269+ const type = getDataType ( typedArray ) ;
270+ model . dataType = type === 'Array' ? model . dataType : type ;
271+ }
272+
268273 if ( numberOfComponents ) {
269274 model . numberOfComponents = numberOfComponents ;
270275 }
@@ -313,44 +318,38 @@ function vtkDataArray(publicAPI, model) {
313318// Object factory
314319// ----------------------------------------------------------------------------
315320
316- const DEFAULT_VALUES = {
317- name : '' ,
318- numberOfComponents : 1 ,
319- size : 0 ,
320- dataType : DefaultDataType ,
321- rangeTuple : [ 0 , 0 ] ,
322- // values: null,
323- // ranges: null,
324- } ;
321+ function defaultValues ( initialValues ) {
322+ return {
323+ name : '' ,
324+ numberOfComponents : 1 ,
325+ size : 0 ,
326+ dataType : DefaultDataType ,
327+ rangeTuple : [ 0 , 0 ] ,
328+ // values: null,
329+ // ranges: null,
330+ ...initialValues ,
331+ } ;
332+ }
325333
326334// ----------------------------------------------------------------------------
327335
328336export function extend ( publicAPI , model , initialValues = { } ) {
329- Object . assign ( model , DEFAULT_VALUES , initialValues ) ;
330-
331- if ( ! model . empty && ! model . values && ! model . size ) {
332- throw new TypeError (
333- 'Cannot create vtkDataArray object without: size > 0, values'
334- ) ;
335- }
336-
337- if ( ! model . values ) {
338- model . values = macro . newTypedArray ( model . dataType , model . size ) ;
339- } else if ( Array . isArray ( model . values ) ) {
340- model . values = macro . newTypedArrayFrom ( model . dataType , model . values ) ;
341- }
342-
343- if ( model . values ) {
344- model . size = model . values . length ;
345- model . dataType = getDataType ( model . values ) ;
346- }
337+ Object . assign ( model , defaultValues ( model ) ) ;
347338
348339 // Object methods
349340 macro . obj ( publicAPI , model ) ;
350341 macro . set ( publicAPI , model , [ 'name' , 'numberOfComponents' ] ) ;
351342
343+ const type = initialValues . dataType ? initialValues . dataType : model . dataType ;
344+ if ( ! initialValues . values ) {
345+ initialValues . values = macro . newTypedArray ( type , model . size ) ;
346+ } else if ( Array . isArray ( initialValues . values ) ) {
347+ initialValues . values = macro . newTypedArrayFrom ( type , initialValues . values ) ;
348+ }
349+
352350 // Object specific methods
353351 vtkDataArray ( publicAPI , model ) ;
352+ publicAPI . setData ( initialValues . values , initialValues . numberOfComponents ) ;
354353}
355354
356355// ----------------------------------------------------------------------------
0 commit comments