source: vtkvis/trunk/DataSet.cpp @ 4843

Last change on this file since 4843 was 4739, checked in by ldelgass, 9 years ago

remove unused method

  • Property svn:eol-style set to native
File size: 30.7 KB
RevLine 
[2100]1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
[3177]3 * Copyright (C) 2004-2012  HUBzero Foundation, LLC
[2100]4 *
5 * Author: Leif Delgass <ldelgass@purdue.edu>
6 */
7
[2423]8#include <cassert>
[2317]9#include <cstring>
[2402]10#include <cfloat>
11#include <cmath>
[2317]12
[2100]13#include <vtkCharArray.h>
14#include <vtkDataSetReader.h>
[3136]15#include <vtkDataSetWriter.h>
[2270]16#include <vtkPolyData.h>
17#include <vtkStructuredPoints.h>
18#include <vtkStructuredGrid.h>
19#include <vtkRectilinearGrid.h>
20#include <vtkUnstructuredGrid.h>
[2100]21#include <vtkProperty.h>
22#include <vtkPointData.h>
[2332]23#include <vtkCellData.h>
[2402]24#include <vtkCell.h>
[2100]25#include <vtkLookupTable.h>
[3818]26#include <vtkExtractUnstructuredGrid.h>
[2100]27
[3621]28#include "DataSet.h"
[2100]29#include "Trace.h"
30
[3615]31using namespace VtkVis;
[2100]32
[2194]33DataSet::DataSet(const std::string& name) :
34    _name(name),
[2454]35    _visible(true),
[2455]36    _opacity(1),
[2454]37    _cellSizeAverage(0)
[2100]38{
[2454]39    _cellSizeRange[0] = -1;
40    _cellSizeRange[1] = -1;
[2100]41}
42
43DataSet::~DataSet()
44{
45}
46
47/**
[2455]48 * \brief Set opacity of DataSet outline
49 *
50 * This method is used for record-keeping and opacity of the
51 * DataSet bounds outline.  The renderer controls opacity
52 * of other related graphics objects.
53 */
54void DataSet::setOpacity(double opacity)
55{
56    _opacity = opacity;
57}
58
59/**
[2194]60 * \brief Set visibility flag in DataSet
61 *
[2455]62 * This method is used for record-keeping and visibility of the
63 * DataSet bounds outline.  The renderer controls visibility
64 * of other related graphics objects.
[2194]65 */
66void DataSet::setVisibility(bool state)
67{
68    _visible = state;
69}
70
71/**
72 * \brief Get visibility flag in DataSet
73 *
74 * This method is used for record-keeping.  The renderer controls
75 * the visibility of related graphics objects.
76 */
77bool DataSet::getVisibility() const
78{
79    return _visible;
80}
81
[3136]82void DataSet::writeDataFile(const char *filename)
83{
84    if (_dataSet == NULL)
85        return;
86   
87    vtkSmartPointer<vtkDataSetWriter> writer = vtkSmartPointer<vtkDataSetWriter>::New();
88
89    writer->SetFileName(filename);
[3189]90#ifdef USE_VTK6
91    writer->SetInputData(_dataSet);
92#else
[3136]93    writer->SetInput(_dataSet);
[3189]94#endif
[3136]95    writer->Write();
96}
97
[2194]98/**
[2100]99 * \brief Read a VTK data file
100 */
101bool DataSet::setDataFile(const char *filename)
102{
103    vtkSmartPointer<vtkDataSetReader> reader = vtkSmartPointer<vtkDataSetReader>::New();
[2270]104
105#if defined(WANT_TRACE) && defined(DEBUG)
106    reader->DebugOn();
107#endif
108
[2100]109    reader->SetFileName(filename);
[2402]110    reader->ReadAllNormalsOn();
[3704]111    reader->ReadAllTCoordsOn();
[2270]112    reader->ReadAllScalarsOn();
[3704]113    reader->ReadAllColorScalarsOn();
[2270]114    reader->ReadAllVectorsOn();
[3704]115    reader->ReadAllTensorsOn();
[2320]116    reader->ReadAllFieldsOn();
[2454]117
[2100]118    return setData(reader);
119}
120
121/**
122 * \brief Read a VTK data file from a memory buffer
123 */
124bool DataSet::setData(char *data, int nbytes)
125{
126    vtkSmartPointer<vtkDataSetReader> reader = vtkSmartPointer<vtkDataSetReader>::New();
127    vtkSmartPointer<vtkCharArray> dataSetString = vtkSmartPointer<vtkCharArray>::New();
128
[2270]129#if defined(WANT_TRACE) && defined(DEBUG)
130    reader->DebugOn();
131    dataSetString->DebugOn();
132#endif
133
[2100]134    dataSetString->SetArray(data, nbytes, 1);
135    reader->SetInputArray(dataSetString);
136    reader->ReadFromInputStringOn();
[2402]137    reader->ReadAllNormalsOn();
[3704]138    reader->ReadAllTCoordsOn();
[2270]139    reader->ReadAllScalarsOn();
[3704]140    reader->ReadAllColorScalarsOn();
[2270]141    reader->ReadAllVectorsOn();
[3704]142    reader->ReadAllTensorsOn();
[2320]143    reader->ReadAllFieldsOn();
[2270]144
[2454]145    return setData(reader);
[2100]146}
147
[2112]148/**
149 * \brief Read dataset using supplied reader
[2270]150 *
151 * Pipeline information is removed from the resulting
152 * vtkDataSet, so that the reader and its data can be
153 * released
[2112]154 */
[2100]155bool DataSet::setData(vtkDataSetReader *reader)
156{
[2454]157    TRACE("Enter");
[2100]158    // Force reading data set
159    reader->SetLookupTableName("");
[2270]160    reader->Update();
161
[2100]162    _dataSet = reader->GetOutput();
[3360]163    if (_dataSet == NULL)
164        return false;
[3818]165
[3873]166    if (vtkUnstructuredGrid::SafeDownCast(_dataSet) != NULL && !isCloud()) {
[3818]167        vtkSmartPointer<vtkExtractUnstructuredGrid> filter = vtkSmartPointer<vtkExtractUnstructuredGrid>::New();
168#ifdef USE_VTK6
169        filter->SetInputData(_dataSet);
170#else
171        filter->SetInput(_dataSet);
172#endif
173        filter->MergingOn();
174        filter->ReleaseDataFlagOn();
175        filter->Update();
176        _dataSet = filter->GetOutput();
177    }
178
[3189]179#ifndef USE_VTK6
[2270]180    _dataSet->SetPipelineInformation(NULL);
[3189]181#endif
[2402]182    if (_dataSet->GetPointData() != NULL &&
183        _dataSet->GetPointData()->GetScalars() != NULL &&
184        _dataSet->GetPointData()->GetScalars()->GetLookupTable() != NULL) {
[3360]185        USER_ERROR("No lookup table should be specified in VTK data sets");
[2402]186    }
187
[2423]188    setDefaultArrays();
189
[2332]190#ifdef WANT_TRACE
[2402]191    print();
[2332]192#endif
[2454]193    TRACE("Leave");
[2100]194    return true;
195}
196
197/**
[2270]198 * \brief Set DataSet from existing vtkDataSet object
199 *
200 * Pipeline information is removed from the supplied vtkDataSet
[2121]201 */
202bool DataSet::setData(vtkDataSet *ds)
203{
204    _dataSet = ds;
[3189]205#ifndef USE_VTK6
[2270]206    _dataSet->SetPipelineInformation(NULL);
[3189]207#endif
[2121]208
[2402]209    if (_dataSet->GetPointData() != NULL &&
210        _dataSet->GetPointData()->GetScalars() != NULL &&
211        _dataSet->GetPointData()->GetScalars()->GetLookupTable() != NULL) {
[3360]212        USER_ERROR("No lookup table should be specified in VTK data sets");
[2402]213    }
214
[2423]215    setDefaultArrays();
216
[2332]217#ifdef WANT_TRACE
[2402]218    print();
[2332]219#endif
[2121]220    return true;
221}
222
223/**
[2270]224 * \brief Copy an existing vtkDataSet object
225 *
226 * Pipeline information is not copied from the supplied vtkDataSet
227 * into this DataSet, but pipeline information in the supplied
228 * vtkDataSet is not removed
229 */
230vtkDataSet *DataSet::copyData(vtkDataSet *ds)
231{
232    if (vtkPolyData::SafeDownCast(ds) != NULL) {
233        _dataSet = vtkSmartPointer<vtkPolyData>::New();
234        _dataSet->DeepCopy(vtkPolyData::SafeDownCast(ds));
235    } else if (vtkStructuredPoints::SafeDownCast(ds) != NULL) {
236        _dataSet = vtkSmartPointer<vtkStructuredPoints>::New();
237        _dataSet->DeepCopy(vtkStructuredPoints::SafeDownCast(ds));
238    } else if (vtkStructuredGrid::SafeDownCast(ds) != NULL) {
239        _dataSet = vtkSmartPointer<vtkStructuredGrid>::New();
240        _dataSet->DeepCopy(vtkStructuredGrid::SafeDownCast(ds));
241    } else if (vtkRectilinearGrid::SafeDownCast(ds) != NULL) {
242        _dataSet = vtkSmartPointer<vtkRectilinearGrid>::New();
243        _dataSet->DeepCopy(vtkRectilinearGrid::SafeDownCast(ds));
244    } else if (vtkUnstructuredGrid::SafeDownCast(ds) != NULL) {
245        _dataSet = vtkSmartPointer<vtkUnstructuredGrid>::New();
246        _dataSet->DeepCopy(vtkUnstructuredGrid::SafeDownCast(ds));
247    } else {
248        ERROR("Unknown data type");
249        return NULL;
250    }
251
[2332]252#ifdef WANT_TRACE
[2402]253    print();
[2332]254#endif   
[2270]255    return _dataSet;
256}
257
258/**
[2423]259 * \brief Returns the dimensionality of the AABB
260 */
261int DataSet::numDimensions() const
262{
263    double bounds[6];
264    getBounds(bounds);
265    int numDims = 0;
266    if (bounds[0] != bounds[1])
267        numDims++;
268    if (bounds[2] != bounds[3])
269        numDims++;
270    if (bounds[4] != bounds[5])
271        numDims++;
272
273    return numDims;
274}
275
276/**
277 * \brief Determines if DataSet lies in a principal axis plane
278 * and if so, returns the plane normal and offset from origin
279 */
[2612]280bool DataSet::is2D(PrincipalPlane *plane, double *offset) const
[2423]281{
282    double bounds[6];
283    getBounds(bounds);
284    if (bounds[4] == bounds[5]) {
[3682]285        // Zmin = Zmax, XY plane
[2423]286        if (plane != NULL) {
287            *plane = PLANE_XY;
288        }
289        if (offset != NULL)
290            *offset = bounds[4];
291        return true;
292    } else if (bounds[0] == bounds[1]) {
[3682]293        // Xmin = Xmax, ZY plane
[2423]294        if (plane != NULL) {
295            *plane = PLANE_ZY;
296        }
297        if (offset != NULL)
298            *offset = bounds[0];
299        return true;
300    } else if (bounds[2] == bounds[3]) {
[3682]301        // Ymin = Ymax, XZ plane
[2423]302        if (plane != NULL) {
303            *plane = PLANE_XZ;
304         }
305        if (offset != NULL)
306            *offset = bounds[2];
307        return true;
308    }
309    return false;
310}
311
312/**
313 * \brief Determines a principal plane with the
314 * largest two dimensions of the AABB
315 */
[2612]316PrincipalPlane DataSet::principalPlane() const
[2423]317{
318    double bounds[6];
319    getBounds(bounds);
320    double xlen = bounds[1] - bounds[0];
321    double ylen = bounds[3] - bounds[2];
322    double zlen = bounds[5] - bounds[4];
323    if (zlen <= xlen && zlen <= ylen) {
324        return PLANE_XY;
325    } else if (xlen <= ylen && xlen <= zlen) {
326        return PLANE_ZY;
327    } else {
328        return PLANE_XZ;
329    }
330}
331
332/**
[3680]333 * \brief Determines if mesh is a point cloud (has no cells)
334 */
335bool DataSet::isCloud() const
336{
337    vtkPolyData *pd = vtkPolyData::SafeDownCast(_dataSet);
338    if (pd) {
339        // If PolyData, is a cloud if there are no cells other than vertices
340        if (pd->GetNumberOfLines() == 0 &&
341            pd->GetNumberOfPolys() == 0 &&
342            pd->GetNumberOfStrips() == 0) {
343            return true;
344        } else {
345            // Has cells
346            return false;
347        }
348    } else {
349        return (_dataSet->GetNumberOfCells() == 0);
350    }
351}
352
353/**
[2112]354 * \brief Get the name/id of this dataset
355 */
[2194]356const std::string& DataSet::getName() const
[2112]357{
358    return _name;
359}
360
361/**
[2100]362 * \brief Get the underlying VTK DataSet object
363 */
364vtkDataSet *DataSet::getVtkDataSet()
365{
366    return _dataSet;
367}
368
369/**
[2260]370 * \brief Get the underlying VTK DataSet subclass class name
371 */
[2332]372const char *DataSet::getVtkType() const
[2260]373{
374    return _dataSet->GetClassName();
375}
376
377/**
[2332]378 * \brief Set the ative scalar array to the named field
379 */
[2393]380bool DataSet::setActiveScalars(const char *name)
[2332]381{
382    bool found = false;
383    if (_dataSet != NULL) {
[3961]384        if (!hasField(name)) {
385            ERROR("No field named %s in %s", name, getName().c_str());
386        } else {
387            if (_dataSet->GetPointData() != NULL) {
388                if (_dataSet->GetPointData()->SetActiveScalars(name) >= 0) {
389                    TRACE("Set active point data scalars for %s to %s", getName().c_str(), name);
390                    found = true;
391                }
[2457]392            }
[3961]393            if (_dataSet->GetCellData() != NULL) {
394                if (_dataSet->GetCellData()->SetActiveScalars(name) >= 0) {
395                    TRACE("Set active cell data scalars for %s to %s", getName().c_str(), name);
396                    found = true;
397                }
[2457]398            }
[2332]399        }
400    }
[2457]401#ifdef WANT_TRACE
402    if (_dataSet->GetPointData() != NULL) {
403        if (_dataSet->GetPointData()->GetScalars() != NULL) {
[3961]404            TRACE("Point data scalars for %s: %s", getName().c_str(), _dataSet->GetPointData()->GetScalars()->GetName());
[2457]405        } else {
[3961]406            TRACE("NULL point data scalars for %s", getName().c_str());
[2457]407        }
408    }
409    if (_dataSet->GetCellData() != NULL) {
410        if (_dataSet->GetCellData()->GetScalars() != NULL) {
[3961]411            TRACE("Cell data scalars for %s: %s", getName().c_str(), _dataSet->GetCellData()->GetScalars()->GetName());
[2457]412        } else {
[3961]413            TRACE("NULL cell data scalars for %s", getName().c_str());
[2457]414        }
415    }
416#endif
[2332]417    return found;
418}
419
420/**
[2471]421 * \brief Get the active scalar array field name
422 */
[2610]423const char *DataSet::getActiveScalarsName() const
[2471]424{
425    if (_dataSet != NULL) {
426         if (_dataSet->GetPointData() != NULL &&
427             _dataSet->GetPointData()->GetScalars() != NULL) {
428            return _dataSet->GetPointData()->GetScalars()->GetName();
429        }
[3842]430#ifdef DEBUG
[2471]431        TRACE("No point scalars");
[3842]432#endif
[2471]433        if (_dataSet->GetCellData() != NULL &&
434            _dataSet->GetCellData()->GetScalars() != NULL) {
435            return _dataSet->GetCellData()->GetScalars()->GetName();
436        }
[3842]437#ifdef DEBUG
[2471]438        TRACE("No cell scalars");
[3842]439#endif
[2471]440    }
441    return NULL;
442}
443
444/**
[2612]445 * \brief Get the active scalar array default attribute type
446 */
447DataSet::DataAttributeType DataSet::getActiveScalarsType() const
448{
449    if (_dataSet != NULL) {
450         if (_dataSet->GetPointData() != NULL &&
451             _dataSet->GetPointData()->GetScalars() != NULL) {
452            return POINT_DATA;
453        }
[3842]454#ifdef DEBUG
[2612]455        TRACE("No point scalars");
[3842]456#endif
[2612]457        if (_dataSet->GetCellData() != NULL &&
458            _dataSet->GetCellData()->GetScalars() != NULL) {
459            return CELL_DATA;
460        }
[3842]461#ifdef DEBUG
[2612]462        TRACE("No cell scalars");
[3842]463#endif
[2612]464    }
465    return POINT_DATA;
466}
467
468/**
[2332]469 * \brief Set the ative vector array to the named field
470 */
[2393]471bool DataSet::setActiveVectors(const char *name)
[2332]472{
473    bool found = false;
474    if (_dataSet != NULL) {
475        if (_dataSet->GetPointData() != NULL) {
[2457]476            if (_dataSet->GetPointData()->SetActiveVectors(name) >= 0) {
477                TRACE("Set active point data vectors to %s", name);
[2332]478                found = true;
[2457]479            }
[2332]480        }
481        if (_dataSet->GetCellData() != NULL) {
[2457]482            if (_dataSet->GetCellData()->SetActiveVectors(name) >= 0) {
483                TRACE("Set active cell data vectors to %s", name);
[2332]484                found = true;
[2457]485            }
[2332]486        }
487    }
[2457]488
[2332]489    return found;
490}
491
492/**
[2612]493 * \brief Get the active vector array default attribute type
494 */
495DataSet::DataAttributeType DataSet::getActiveVectorsType() const
496{
497    if (_dataSet != NULL) {
498         if (_dataSet->GetPointData() != NULL &&
499             _dataSet->GetPointData()->GetVectors() != NULL) {
500            return POINT_DATA;
501        }
[3842]502#ifdef DEBUG
[2612]503        TRACE("No point vectors");
[3842]504#endif
[2612]505        if (_dataSet->GetCellData() != NULL &&
506            _dataSet->GetCellData()->GetVectors() != NULL) {
507            return CELL_DATA;
508        }
[3842]509#ifdef DEBUG
[2612]510        TRACE("No cell vectors");
[3842]511#endif
[2612]512    }
513    return POINT_DATA;
514}
515
516/**
[2471]517 * \brief Get the active vector array field name
518 */
[2610]519const char *DataSet::getActiveVectorsName() const
[2471]520{
521    if (_dataSet != NULL) {
522        if (_dataSet->GetPointData() != NULL &&
523            _dataSet->GetPointData()->GetVectors() != NULL) {
524            return _dataSet->GetPointData()->GetVectors()->GetName();
525        }
[3842]526#ifdef DEBUG
[2471]527        TRACE("No point vectors");
[3842]528#endif
[2471]529        if (_dataSet->GetCellData() != NULL &&
530            _dataSet->GetCellData()->GetVectors() != NULL) {
531            return _dataSet->GetCellData()->GetVectors()->GetName();
532        }
[3842]533#ifdef DEBUG
[2471]534        TRACE("No cell vectors");
[3842]535#endif
[2471]536    }
537    return NULL;
538}
539
[3818]540void DataSet::setDefaultArrays()
541{
542    if (_dataSet->GetPointData() != NULL &&
543        _dataSet->GetPointData()->GetScalars() == NULL &&
544        _dataSet->GetPointData()->GetNumberOfArrays() > 0) {
545        for (int i = 0; i < _dataSet->GetPointData()->GetNumberOfArrays(); i++) {
546            if (_dataSet->GetPointData()->GetArray(i) != NULL &&
547                _dataSet->GetPointData()->GetArray(i)->GetNumberOfComponents() == 1) {
548                TRACE("Setting point scalars to '%s'", _dataSet->GetPointData()->GetArrayName(i));
549                _dataSet->GetPointData()->SetActiveScalars(_dataSet->GetPointData()->GetArrayName(i));
550                break;
551            }
552        }
553    }
554    if (_dataSet->GetPointData() != NULL &&
555        _dataSet->GetPointData()->GetVectors() == NULL &&
556        _dataSet->GetPointData()->GetNumberOfArrays() > 0) {
557        for (int i = 0; i < _dataSet->GetPointData()->GetNumberOfArrays(); i++) {
558            if (_dataSet->GetPointData()->GetArray(i) != NULL &&
559                _dataSet->GetPointData()->GetArray(i)->GetNumberOfComponents() == 3) {
560                TRACE("Setting point vectors to '%s'", _dataSet->GetPointData()->GetArrayName(i));
561                _dataSet->GetPointData()->SetActiveVectors(_dataSet->GetPointData()->GetArrayName(i));
562                break;
563            }
564        }
565    }
566    if (_dataSet->GetCellData() != NULL &&
567        _dataSet->GetCellData()->GetScalars() == NULL &&
568        _dataSet->GetCellData()->GetNumberOfArrays() > 0) {
569        for (int i = 0; i < _dataSet->GetCellData()->GetNumberOfArrays(); i++) {
570            if (_dataSet->GetCellData()->GetArray(i) != NULL &&
571                _dataSet->GetCellData()->GetArray(i)->GetNumberOfComponents() == 1) {
572                TRACE("Setting cell scalars to '%s'", _dataSet->GetCellData()->GetArrayName(i));
573                _dataSet->GetCellData()->SetActiveScalars(_dataSet->GetCellData()->GetArrayName(i));
574                break;
575            }
576        }
577    }
578    if (_dataSet->GetCellData() != NULL &&
579        _dataSet->GetCellData()->GetVectors() == NULL &&
580        _dataSet->GetCellData()->GetNumberOfArrays() > 0) {
581        for (int i = 0; i < _dataSet->GetCellData()->GetNumberOfArrays(); i++) {
582            if (_dataSet->GetCellData()->GetArray(i) != NULL &&
583                _dataSet->GetCellData()->GetArray(i)->GetNumberOfComponents() == 3) {
584                TRACE("Setting cell vectors to '%s'", _dataSet->GetCellData()->GetArrayName(i));
585                _dataSet->GetCellData()->SetActiveVectors(_dataSet->GetCellData()->GetArrayName(i));
586                break;
587            }
588        }
589    }
590}
591
[2612]592bool DataSet::getFieldInfo(const char *fieldName,
593                           DataAttributeType *type,
594                           int *numComponents) const
595{
596    if (_dataSet->GetPointData() != NULL &&
[3136]597        _dataSet->GetPointData()->GetAbstractArray(fieldName) != NULL) {
[2639]598        if (type != NULL)
599            *type = POINT_DATA;
600        if (numComponents != NULL)
[3136]601            *numComponents = _dataSet->GetPointData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
[2612]602        return true;
603    } else if (_dataSet->GetCellData() != NULL &&
[3136]604               _dataSet->GetCellData()->GetAbstractArray(fieldName) != NULL) {
[2639]605        if (type != NULL)
606            *type = CELL_DATA;
607        if (numComponents != NULL)
[3136]608            *numComponents = _dataSet->GetCellData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
[2612]609        return true;
610    } else if (_dataSet->GetFieldData() != NULL &&
[3136]611               _dataSet->GetFieldData()->GetAbstractArray(fieldName) != NULL) {
[2639]612        if (type != NULL)
613            *type = FIELD_DATA;
614        if (numComponents != NULL)
[3136]615            *numComponents = _dataSet->GetFieldData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
[2639]616        return true;
[2612]617    }
618    return false;
619}
620
621bool DataSet::getFieldInfo(const char *fieldName,
622                           DataAttributeType type,
623                           int *numComponents) const
624{
625    switch (type) {
626    case POINT_DATA:
627        if (_dataSet->GetPointData() != NULL &&
[3136]628            _dataSet->GetPointData()->GetAbstractArray(fieldName) != NULL) {
[2639]629            if (numComponents != NULL)
[3136]630                *numComponents = _dataSet->GetPointData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
[2612]631            return true;
632        } else
633            return false;
634        break;
635    case CELL_DATA:
636        if (_dataSet->GetCellData() != NULL &&
[3136]637            _dataSet->GetCellData()->GetAbstractArray(fieldName) != NULL) {
[2639]638            if (numComponents != NULL)
[3136]639                *numComponents = _dataSet->GetCellData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
[2612]640            return true;
641        } else
642            return false;
643        break;
644    case FIELD_DATA:
645        if (_dataSet->GetFieldData() != NULL &&
[3136]646            _dataSet->GetFieldData()->GetAbstractArray(fieldName) != NULL) {
[2639]647            if (numComponents != NULL)
[3136]648                *numComponents = _dataSet->GetFieldData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
[2612]649            return true;
650        } else
651            return false;
652        break;
653    default:
654        ;
655    }
656    return false;
657}
658
[2471]659/**
[2610]660 * \brief Get the list of field names in the DataSet
661 *
662 * \param[in,out] names The field names will be appended to this list
663 * \param[in] type The DataAttributeType: e.g. POINT_DATA, CELL_DATA
664 * \param[in] numComponents Filter list by number of components, -1 means to
665 * return all fields regardless of dimension
666 */
667void DataSet::getFieldNames(std::vector<std::string>& names,
668                            DataAttributeType type, int numComponents) const
669{
670    if (_dataSet == NULL)
671        return;
672    switch (type) {
673    case POINT_DATA:
674        if (_dataSet->GetPointData() != NULL) {
675            for (int i = 0; i < _dataSet->GetPointData()->GetNumberOfArrays(); i++) {
676                if (numComponents == -1 ||
[3136]677                    (_dataSet->GetPointData()->GetAbstractArray(i) != NULL &&
678                     _dataSet->GetPointData()->GetAbstractArray(i)->GetNumberOfComponents() == numComponents)) {
[2610]679                    names.push_back(_dataSet->GetPointData()->GetArrayName(i));
680                }
681            }
682        }
683        break;
684    case CELL_DATA:
685        if (_dataSet->GetCellData() != NULL) {
686            for (int i = 0; i < _dataSet->GetCellData()->GetNumberOfArrays(); i++) {
687                if (numComponents == -1 ||
[3136]688                    (_dataSet->GetCellData()->GetAbstractArray(i) != NULL &&
689                     _dataSet->GetCellData()->GetAbstractArray(i)->GetNumberOfComponents() == numComponents)) {
[2610]690                    names.push_back(_dataSet->GetCellData()->GetArrayName(i));
691                }
692            }
693        }
694        break;
695    case FIELD_DATA:
696        if (_dataSet->GetFieldData() != NULL) {
697            for (int i = 0; i < _dataSet->GetFieldData()->GetNumberOfArrays(); i++) {
698                if (numComponents == -1 ||
[3136]699                    (_dataSet->GetFieldData()->GetAbstractArray(i) != NULL &&
700                     _dataSet->GetFieldData()->GetAbstractArray(i)->GetNumberOfComponents() == numComponents)) {
[2610]701                    names.push_back(_dataSet->GetFieldData()->GetArrayName(i));
702                }
703            }
704        }
705        break;
706    default:
707        ERROR("Unknown DataAttributeType %d", type);
708    }
709}
710
711/**
[2100]712 * \brief Get the range of scalar values in the DataSet
713 */
[2402]714void DataSet::getScalarRange(double minmax[2]) const
[2100]715{
[2423]716    if (_dataSet == NULL)
717        return;
718    if (_dataSet->GetPointData() != NULL &&
719        _dataSet->GetPointData()->GetScalars() != NULL) {
720        _dataSet->GetPointData()->GetScalars()->GetRange(minmax, -1);
721    } else if (_dataSet->GetCellData() != NULL &&
722               _dataSet->GetCellData()->GetScalars() != NULL) {
723        _dataSet->GetCellData()->GetScalars()->GetRange(minmax, -1);
724    }
[2100]725}
726
727/**
[2402]728 * \brief Get the range of a vector component in the DataSet
729 *
730 * \param[out] minmax The data range
731 * \param[in] component The field component, -1 means magnitude
[2332]732 */
[2402]733void DataSet::getVectorRange(double minmax[2], int component) const
[2332]734{
[2402]735    if (_dataSet == NULL)
[2332]736        return;
737    if (_dataSet->GetPointData() != NULL &&
738        _dataSet->GetPointData()->GetVectors() != NULL) {
[2402]739        _dataSet->GetPointData()->GetVectors()->GetRange(minmax, component);
[2332]740    } else if (_dataSet->GetCellData() != NULL &&
741               _dataSet->GetCellData()->GetVectors() != NULL) {
[2402]742        _dataSet->GetCellData()->GetVectors()->GetRange(minmax, component);
[2332]743    }
744}
745
746/**
[2402]747 * \brief Get the range of values for the named field in the DataSet
748 *
749 * \param[out] minmax The data range
750 * \param[in] fieldName The array name
[2610]751 * \param[in] type The DataAttributeType
[2402]752 * \param[in] component The field component, -1 means magnitude
[2610]753 * \return boolean indicating if field was found
[2332]754 */
[2610]755bool DataSet::getDataRange(double minmax[2], const char *fieldName,
756                           DataAttributeType type, int component) const
[2332]757{
758    if (_dataSet == NULL)
[2610]759        return false;
760    switch (type) {
761    case POINT_DATA:
762        if (_dataSet->GetPointData() != NULL &&
763            _dataSet->GetPointData()->GetArray(fieldName) != NULL) {
764            _dataSet->GetPointData()->GetArray(fieldName)->GetRange(minmax, component);
765            return true;
766        } else {
767            return false;
768        }
769        break;
770    case CELL_DATA:
771        if (_dataSet->GetCellData() != NULL &&
772            _dataSet->GetCellData()->GetArray(fieldName) != NULL) {
773            _dataSet->GetCellData()->GetArray(fieldName)->GetRange(minmax, component);
774            return true;
775        } else {
776            return false;
777        }
778        break;
779    case FIELD_DATA:
780        if (_dataSet->GetFieldData() != NULL &&
781            _dataSet->GetFieldData()->GetArray(fieldName) != NULL) {
782            _dataSet->GetFieldData()->GetArray(fieldName)->GetRange(minmax, component);
783            return true;
784        } else {
785            return false;
786        }
787        break;
788    default:
789        ERROR("Unknown DataAttributeType %d", type);
790        break;
[2332]791    }
[2610]792    return false;
[2332]793}
794
795/**
[2290]796 * \brief Get the bounds the DataSet
797 */
[2332]798void DataSet::getBounds(double bounds[6]) const
[2290]799{
[2332]800    _dataSet->GetBounds(bounds);
[2290]801}
802
803/**
[2404]804 * \brief Get the range of cell AABB diagonal lengths in the DataSet
[2402]805 */
[2454]806void DataSet::getCellSizeRange(double minmax[2], double *average)
[2402]807{
[3680]808    // Check for cached values
[2454]809    if (_cellSizeRange[0] >= 0.0) {
810        minmax[0] = _cellSizeRange[0];
811        minmax[1] = _cellSizeRange[1];
812        *average = _cellSizeAverage;
813        return;
814    }
815
[3680]816    getCellSizeRange(_dataSet, minmax, average);
817
818    // Save values in cache
819    _cellSizeRange[0] = minmax[0];
820    _cellSizeRange[1] = minmax[1];
821    _cellSizeAverage = *average;
822}
823
824/**
825 * \brief Get the range of cell AABB diagonal lengths in the DataSet
826 */
827void DataSet::getCellSizeRange(vtkDataSet *dataSet, double minmax[2], double *average)
828{
829    if (dataSet == NULL ||
830        dataSet->GetNumberOfCells() < 1) {
831        minmax[0] = 1;
832        minmax[1] = 1;
833        *average = 1;
834        return;
835    }
836
[2402]837    minmax[0] = DBL_MAX;
838    minmax[1] = -DBL_MAX;
839
840    *average = 0;
[3680]841    for (int i = 0; i < dataSet->GetNumberOfCells(); i++) {
842        double length2 = dataSet->GetCell(i)->GetLength2();
[2402]843        if (length2 < minmax[0])
844            minmax[0] = length2;
845        if (length2 > minmax[1])
846            minmax[1] = length2;
847        *average += length2;
848    }
849    if (minmax[0] == DBL_MAX)
850        minmax[0] = 1;
851    if (minmax[1] == -DBL_MAX)
852        minmax[1] = 1;
853
854    minmax[0] = sqrt(minmax[0]);
855    minmax[1] = sqrt(minmax[1]);
[3680]856    *average = sqrt(*average/((double)dataSet->GetNumberOfCells()));
[2402]857}
858
859/**
[2100]860 * \brief Get nearest data value given world coordinates x,y,z
861 *
862 * Note: no interpolation is performed on data
[2121]863 *
[2610]864 * \param[in] x World x coordinate to probe
865 * \param[in] y World y coordinate to probe
866 * \param[in] z World z coordinate to probe
867 * \param[out] value On success, contains the data value
868 * \return boolean indicating success or failure
[2100]869 */
[2423]870bool DataSet::getScalarValue(double x, double y, double z, double *value) const
[2100]871{
872    if (_dataSet == NULL)
[2423]873        return false;
[2121]874    if (_dataSet->GetPointData() == NULL ||
875        _dataSet->GetPointData()->GetScalars() == NULL) {
[2423]876        return false;
[2121]877    }
[2100]878    vtkIdType pt = _dataSet->FindPoint(x, y, z);
[2423]879    if (pt < 0)
880        return false;
881    *value = _dataSet->GetPointData()->GetScalars()->GetComponent(pt, 0);
882    return true;
[2100]883}
[2423]884
885/**
886 * \brief Get nearest vector data value given world coordinates x,y,z
887 *
888 * Note: no interpolation is performed on data
889 *
890 * \param[in] x World x coordinate to probe
891 * \param[in] y World y coordinate to probe
892 * \param[in] z World z coordinate to probe
893 * \param[out] vector On success, contains the data values
894 * \return boolean indicating success or failure
895 */
896bool DataSet::getVectorValue(double x, double y, double z, double vector[3]) const
897{
898    if (_dataSet == NULL)
899        return false;
900    if (_dataSet->GetPointData() == NULL ||
901        _dataSet->GetPointData()->GetVectors() == NULL) {
902        return false;
903    }
904    vtkIdType pt = _dataSet->FindPoint(x, y, z);
905    if (pt < 0)
906        return false;
907    assert(_dataSet->GetPointData()->GetVectors()->GetNumberOfComponents() == 3);
908    _dataSet->GetPointData()->GetVectors()->GetTuple(pt, vector);
909    return true;
910}
[3818]911
912void DataSet::print() const
913{
914    print(_dataSet);
915}
916
917void DataSet::print(vtkDataSet *ds)
918{
919    if (ds == NULL)
920        return;
921
922    TRACE("DataSet class: %s", ds->GetClassName());
923
924    TRACE("DataSet memory: %g MiB", ds->GetActualMemorySize()/1024.);
925
926    double bounds[6];
927    ds->GetBounds(bounds);
928
929    // Topology
930    TRACE("DataSet bounds: %g %g %g %g %g %g",
931          bounds[0], bounds[1],
932          bounds[2], bounds[3],
933          bounds[4], bounds[5]);
934    TRACE("Points: %d Cells: %d", ds->GetNumberOfPoints(), ds->GetNumberOfCells());
935
936    double dataRange[2];
937    if (ds->GetPointData() != NULL) {
938        TRACE("PointData arrays: %d", ds->GetPointData()->GetNumberOfArrays());
939        for (int i = 0; i < ds->GetPointData()->GetNumberOfArrays(); i++) {
940            if (ds->GetPointData()->GetArray(i) != NULL) {
941                ds->GetPointData()->GetArray(i)->GetRange(dataRange, -1);
942                TRACE("PointData[%d]: '%s' comp:%d, (%g,%g)", i,
943                      ds->GetPointData()->GetArrayName(i),
944                      ds->GetPointData()->GetAbstractArray(i)->GetNumberOfComponents(),
945                      dataRange[0], dataRange[1]);
946            } else {
947                TRACE("PointData[%d]: '%s' comp:%d", i,
948                      ds->GetPointData()->GetArrayName(i),
949                      ds->GetPointData()->GetAbstractArray(i)->GetNumberOfComponents());
950            }
951        }
952        if (ds->GetPointData()->GetScalars() != NULL) {
953            TRACE("Active point scalars: %s", ds->GetPointData()->GetScalars()->GetName());
954        }
955        if (ds->GetPointData()->GetVectors() != NULL) {
956            TRACE("Active point vectors: %s", ds->GetPointData()->GetVectors()->GetName());
957        }
958    }
959    if (ds->GetCellData() != NULL) {
960        TRACE("CellData arrays: %d", ds->GetCellData()->GetNumberOfArrays());
961        for (int i = 0; i < ds->GetCellData()->GetNumberOfArrays(); i++) {
962            if (ds->GetCellData()->GetArray(i) != NULL) {
963                ds->GetCellData()->GetArray(i)->GetRange(dataRange, -1);
964                TRACE("CellData[%d]: '%s' comp:%d, (%g,%g)", i,
965                      ds->GetCellData()->GetArrayName(i),
966                      ds->GetCellData()->GetAbstractArray(i)->GetNumberOfComponents(),
967                      dataRange[0], dataRange[1]);
968            } else {
969                TRACE("CellData[%d]: '%s' comp:%d", i,
970                      ds->GetCellData()->GetArrayName(i),
971                      ds->GetCellData()->GetAbstractArray(i)->GetNumberOfComponents());
972            }
973        }
974        if (ds->GetCellData()->GetScalars() != NULL) {
975            TRACE("Active cell scalars: %s", ds->GetCellData()->GetScalars()->GetName());
976        }
977        if (ds->GetCellData()->GetVectors() != NULL) {
978            TRACE("Active cell vectors: %s", ds->GetCellData()->GetVectors()->GetName());
979        }
980    }
981    if (ds->GetFieldData() != NULL) {
982        TRACE("FieldData arrays: %d", ds->GetFieldData()->GetNumberOfArrays());
983        for (int i = 0; i < ds->GetFieldData()->GetNumberOfArrays(); i++) {
984            if (ds->GetFieldData()->GetArray(i) != NULL) {
985                ds->GetFieldData()->GetArray(i)->GetRange(dataRange, -1);
986                TRACE("FieldData[%d]: '%s' comp:%d, tuples:%d (%g,%g)", i,
987                      ds->GetFieldData()->GetArrayName(i),
988                      ds->GetFieldData()->GetAbstractArray(i)->GetNumberOfComponents(),
989                      ds->GetFieldData()->GetAbstractArray(i)->GetNumberOfTuples(),
990                      dataRange[0], dataRange[1]);
991            } else {
992                TRACE("FieldData[%d]: '%s' comp:%d, tuples:%d", i,
993                      ds->GetFieldData()->GetArrayName(i),
994                      ds->GetFieldData()->GetAbstractArray(i)->GetNumberOfComponents(),
995                      ds->GetFieldData()->GetAbstractArray(i)->GetNumberOfTuples());
996            }
997        }
998    }
999}
1000
Note: See TracBrowser for help on using the repository browser.