source: vtkvis/trunk/DataSet.cpp @ 5049

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

remove unused method

  • Property svn:eol-style set to native
File size: 30.7 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2004-2012  HUBzero Foundation, LLC
4 *
5 * Author: Leif Delgass <ldelgass@purdue.edu>
6 */
7
8#include <cassert>
9#include <cstring>
10#include <cfloat>
11#include <cmath>
12
13#include <vtkCharArray.h>
14#include <vtkDataSetReader.h>
15#include <vtkDataSetWriter.h>
16#include <vtkPolyData.h>
17#include <vtkStructuredPoints.h>
18#include <vtkStructuredGrid.h>
19#include <vtkRectilinearGrid.h>
20#include <vtkUnstructuredGrid.h>
21#include <vtkProperty.h>
22#include <vtkPointData.h>
23#include <vtkCellData.h>
24#include <vtkCell.h>
25#include <vtkLookupTable.h>
26#include <vtkExtractUnstructuredGrid.h>
27
28#include "DataSet.h"
29#include "Trace.h"
30
31using namespace VtkVis;
32
33DataSet::DataSet(const std::string& name) :
34    _name(name),
35    _visible(true),
36    _opacity(1),
37    _cellSizeAverage(0)
38{
39    _cellSizeRange[0] = -1;
40    _cellSizeRange[1] = -1;
41}
42
43DataSet::~DataSet()
44{
45}
46
47/**
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/**
60 * \brief Set visibility flag in DataSet
61 *
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.
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
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);
90#ifdef USE_VTK6
91    writer->SetInputData(_dataSet);
92#else
93    writer->SetInput(_dataSet);
94#endif
95    writer->Write();
96}
97
98/**
99 * \brief Read a VTK data file
100 */
101bool DataSet::setDataFile(const char *filename)
102{
103    vtkSmartPointer<vtkDataSetReader> reader = vtkSmartPointer<vtkDataSetReader>::New();
104
105#if defined(WANT_TRACE) && defined(DEBUG)
106    reader->DebugOn();
107#endif
108
109    reader->SetFileName(filename);
110    reader->ReadAllNormalsOn();
111    reader->ReadAllTCoordsOn();
112    reader->ReadAllScalarsOn();
113    reader->ReadAllColorScalarsOn();
114    reader->ReadAllVectorsOn();
115    reader->ReadAllTensorsOn();
116    reader->ReadAllFieldsOn();
117
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
129#if defined(WANT_TRACE) && defined(DEBUG)
130    reader->DebugOn();
131    dataSetString->DebugOn();
132#endif
133
134    dataSetString->SetArray(data, nbytes, 1);
135    reader->SetInputArray(dataSetString);
136    reader->ReadFromInputStringOn();
137    reader->ReadAllNormalsOn();
138    reader->ReadAllTCoordsOn();
139    reader->ReadAllScalarsOn();
140    reader->ReadAllColorScalarsOn();
141    reader->ReadAllVectorsOn();
142    reader->ReadAllTensorsOn();
143    reader->ReadAllFieldsOn();
144
145    return setData(reader);
146}
147
148/**
149 * \brief Read dataset using supplied reader
150 *
151 * Pipeline information is removed from the resulting
152 * vtkDataSet, so that the reader and its data can be
153 * released
154 */
155bool DataSet::setData(vtkDataSetReader *reader)
156{
157    TRACE("Enter");
158    // Force reading data set
159    reader->SetLookupTableName("");
160    reader->Update();
161
162    _dataSet = reader->GetOutput();
163    if (_dataSet == NULL)
164        return false;
165
166    if (vtkUnstructuredGrid::SafeDownCast(_dataSet) != NULL && !isCloud()) {
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
179#ifndef USE_VTK6
180    _dataSet->SetPipelineInformation(NULL);
181#endif
182    if (_dataSet->GetPointData() != NULL &&
183        _dataSet->GetPointData()->GetScalars() != NULL &&
184        _dataSet->GetPointData()->GetScalars()->GetLookupTable() != NULL) {
185        USER_ERROR("No lookup table should be specified in VTK data sets");
186    }
187
188    setDefaultArrays();
189
190#ifdef WANT_TRACE
191    print();
192#endif
193    TRACE("Leave");
194    return true;
195}
196
197/**
198 * \brief Set DataSet from existing vtkDataSet object
199 *
200 * Pipeline information is removed from the supplied vtkDataSet
201 */
202bool DataSet::setData(vtkDataSet *ds)
203{
204    _dataSet = ds;
205#ifndef USE_VTK6
206    _dataSet->SetPipelineInformation(NULL);
207#endif
208
209    if (_dataSet->GetPointData() != NULL &&
210        _dataSet->GetPointData()->GetScalars() != NULL &&
211        _dataSet->GetPointData()->GetScalars()->GetLookupTable() != NULL) {
212        USER_ERROR("No lookup table should be specified in VTK data sets");
213    }
214
215    setDefaultArrays();
216
217#ifdef WANT_TRACE
218    print();
219#endif
220    return true;
221}
222
223/**
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
252#ifdef WANT_TRACE
253    print();
254#endif   
255    return _dataSet;
256}
257
258/**
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 */
280bool DataSet::is2D(PrincipalPlane *plane, double *offset) const
281{
282    double bounds[6];
283    getBounds(bounds);
284    if (bounds[4] == bounds[5]) {
285        // Zmin = Zmax, XY plane
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]) {
293        // Xmin = Xmax, ZY plane
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]) {
301        // Ymin = Ymax, XZ plane
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 */
316PrincipalPlane DataSet::principalPlane() const
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/**
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/**
354 * \brief Get the name/id of this dataset
355 */
356const std::string& DataSet::getName() const
357{
358    return _name;
359}
360
361/**
362 * \brief Get the underlying VTK DataSet object
363 */
364vtkDataSet *DataSet::getVtkDataSet()
365{
366    return _dataSet;
367}
368
369/**
370 * \brief Get the underlying VTK DataSet subclass class name
371 */
372const char *DataSet::getVtkType() const
373{
374    return _dataSet->GetClassName();
375}
376
377/**
378 * \brief Set the ative scalar array to the named field
379 */
380bool DataSet::setActiveScalars(const char *name)
381{
382    bool found = false;
383    if (_dataSet != NULL) {
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                }
392            }
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                }
398            }
399        }
400    }
401#ifdef WANT_TRACE
402    if (_dataSet->GetPointData() != NULL) {
403        if (_dataSet->GetPointData()->GetScalars() != NULL) {
404            TRACE("Point data scalars for %s: %s", getName().c_str(), _dataSet->GetPointData()->GetScalars()->GetName());
405        } else {
406            TRACE("NULL point data scalars for %s", getName().c_str());
407        }
408    }
409    if (_dataSet->GetCellData() != NULL) {
410        if (_dataSet->GetCellData()->GetScalars() != NULL) {
411            TRACE("Cell data scalars for %s: %s", getName().c_str(), _dataSet->GetCellData()->GetScalars()->GetName());
412        } else {
413            TRACE("NULL cell data scalars for %s", getName().c_str());
414        }
415    }
416#endif
417    return found;
418}
419
420/**
421 * \brief Get the active scalar array field name
422 */
423const char *DataSet::getActiveScalarsName() const
424{
425    if (_dataSet != NULL) {
426         if (_dataSet->GetPointData() != NULL &&
427             _dataSet->GetPointData()->GetScalars() != NULL) {
428            return _dataSet->GetPointData()->GetScalars()->GetName();
429        }
430#ifdef DEBUG
431        TRACE("No point scalars");
432#endif
433        if (_dataSet->GetCellData() != NULL &&
434            _dataSet->GetCellData()->GetScalars() != NULL) {
435            return _dataSet->GetCellData()->GetScalars()->GetName();
436        }
437#ifdef DEBUG
438        TRACE("No cell scalars");
439#endif
440    }
441    return NULL;
442}
443
444/**
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        }
454#ifdef DEBUG
455        TRACE("No point scalars");
456#endif
457        if (_dataSet->GetCellData() != NULL &&
458            _dataSet->GetCellData()->GetScalars() != NULL) {
459            return CELL_DATA;
460        }
461#ifdef DEBUG
462        TRACE("No cell scalars");
463#endif
464    }
465    return POINT_DATA;
466}
467
468/**
469 * \brief Set the ative vector array to the named field
470 */
471bool DataSet::setActiveVectors(const char *name)
472{
473    bool found = false;
474    if (_dataSet != NULL) {
475        if (_dataSet->GetPointData() != NULL) {
476            if (_dataSet->GetPointData()->SetActiveVectors(name) >= 0) {
477                TRACE("Set active point data vectors to %s", name);
478                found = true;
479            }
480        }
481        if (_dataSet->GetCellData() != NULL) {
482            if (_dataSet->GetCellData()->SetActiveVectors(name) >= 0) {
483                TRACE("Set active cell data vectors to %s", name);
484                found = true;
485            }
486        }
487    }
488
489    return found;
490}
491
492/**
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        }
502#ifdef DEBUG
503        TRACE("No point vectors");
504#endif
505        if (_dataSet->GetCellData() != NULL &&
506            _dataSet->GetCellData()->GetVectors() != NULL) {
507            return CELL_DATA;
508        }
509#ifdef DEBUG
510        TRACE("No cell vectors");
511#endif
512    }
513    return POINT_DATA;
514}
515
516/**
517 * \brief Get the active vector array field name
518 */
519const char *DataSet::getActiveVectorsName() const
520{
521    if (_dataSet != NULL) {
522        if (_dataSet->GetPointData() != NULL &&
523            _dataSet->GetPointData()->GetVectors() != NULL) {
524            return _dataSet->GetPointData()->GetVectors()->GetName();
525        }
526#ifdef DEBUG
527        TRACE("No point vectors");
528#endif
529        if (_dataSet->GetCellData() != NULL &&
530            _dataSet->GetCellData()->GetVectors() != NULL) {
531            return _dataSet->GetCellData()->GetVectors()->GetName();
532        }
533#ifdef DEBUG
534        TRACE("No cell vectors");
535#endif
536    }
537    return NULL;
538}
539
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
592bool DataSet::getFieldInfo(const char *fieldName,
593                           DataAttributeType *type,
594                           int *numComponents) const
595{
596    if (_dataSet->GetPointData() != NULL &&
597        _dataSet->GetPointData()->GetAbstractArray(fieldName) != NULL) {
598        if (type != NULL)
599            *type = POINT_DATA;
600        if (numComponents != NULL)
601            *numComponents = _dataSet->GetPointData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
602        return true;
603    } else if (_dataSet->GetCellData() != NULL &&
604               _dataSet->GetCellData()->GetAbstractArray(fieldName) != NULL) {
605        if (type != NULL)
606            *type = CELL_DATA;
607        if (numComponents != NULL)
608            *numComponents = _dataSet->GetCellData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
609        return true;
610    } else if (_dataSet->GetFieldData() != NULL &&
611               _dataSet->GetFieldData()->GetAbstractArray(fieldName) != NULL) {
612        if (type != NULL)
613            *type = FIELD_DATA;
614        if (numComponents != NULL)
615            *numComponents = _dataSet->GetFieldData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
616        return true;
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 &&
628            _dataSet->GetPointData()->GetAbstractArray(fieldName) != NULL) {
629            if (numComponents != NULL)
630                *numComponents = _dataSet->GetPointData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
631            return true;
632        } else
633            return false;
634        break;
635    case CELL_DATA:
636        if (_dataSet->GetCellData() != NULL &&
637            _dataSet->GetCellData()->GetAbstractArray(fieldName) != NULL) {
638            if (numComponents != NULL)
639                *numComponents = _dataSet->GetCellData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
640            return true;
641        } else
642            return false;
643        break;
644    case FIELD_DATA:
645        if (_dataSet->GetFieldData() != NULL &&
646            _dataSet->GetFieldData()->GetAbstractArray(fieldName) != NULL) {
647            if (numComponents != NULL)
648                *numComponents = _dataSet->GetFieldData()->GetAbstractArray(fieldName)->GetNumberOfComponents();
649            return true;
650        } else
651            return false;
652        break;
653    default:
654        ;
655    }
656    return false;
657}
658
659/**
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 ||
677                    (_dataSet->GetPointData()->GetAbstractArray(i) != NULL &&
678                     _dataSet->GetPointData()->GetAbstractArray(i)->GetNumberOfComponents() == numComponents)) {
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 ||
688                    (_dataSet->GetCellData()->GetAbstractArray(i) != NULL &&
689                     _dataSet->GetCellData()->GetAbstractArray(i)->GetNumberOfComponents() == numComponents)) {
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 ||
699                    (_dataSet->GetFieldData()->GetAbstractArray(i) != NULL &&
700                     _dataSet->GetFieldData()->GetAbstractArray(i)->GetNumberOfComponents() == numComponents)) {
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/**
712 * \brief Get the range of scalar values in the DataSet
713 */
714void DataSet::getScalarRange(double minmax[2]) const
715{
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    }
725}
726
727/**
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
732 */
733void DataSet::getVectorRange(double minmax[2], int component) const
734{
735    if (_dataSet == NULL)
736        return;
737    if (_dataSet->GetPointData() != NULL &&
738        _dataSet->GetPointData()->GetVectors() != NULL) {
739        _dataSet->GetPointData()->GetVectors()->GetRange(minmax, component);
740    } else if (_dataSet->GetCellData() != NULL &&
741               _dataSet->GetCellData()->GetVectors() != NULL) {
742        _dataSet->GetCellData()->GetVectors()->GetRange(minmax, component);
743    }
744}
745
746/**
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
751 * \param[in] type The DataAttributeType
752 * \param[in] component The field component, -1 means magnitude
753 * \return boolean indicating if field was found
754 */
755bool DataSet::getDataRange(double minmax[2], const char *fieldName,
756                           DataAttributeType type, int component) const
757{
758    if (_dataSet == NULL)
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;
791    }
792    return false;
793}
794
795/**
796 * \brief Get the bounds the DataSet
797 */
798void DataSet::getBounds(double bounds[6]) const
799{
800    _dataSet->GetBounds(bounds);
801}
802
803/**
804 * \brief Get the range of cell AABB diagonal lengths in the DataSet
805 */
806void DataSet::getCellSizeRange(double minmax[2], double *average)
807{
808    // Check for cached values
809    if (_cellSizeRange[0] >= 0.0) {
810        minmax[0] = _cellSizeRange[0];
811        minmax[1] = _cellSizeRange[1];
812        *average = _cellSizeAverage;
813        return;
814    }
815
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
837    minmax[0] = DBL_MAX;
838    minmax[1] = -DBL_MAX;
839
840    *average = 0;
841    for (int i = 0; i < dataSet->GetNumberOfCells(); i++) {
842        double length2 = dataSet->GetCell(i)->GetLength2();
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]);
856    *average = sqrt(*average/((double)dataSet->GetNumberOfCells()));
857}
858
859/**
860 * \brief Get nearest data value given world coordinates x,y,z
861 *
862 * Note: no interpolation is performed on data
863 *
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
869 */
870bool DataSet::getScalarValue(double x, double y, double z, double *value) const
871{
872    if (_dataSet == NULL)
873        return false;
874    if (_dataSet->GetPointData() == NULL ||
875        _dataSet->GetPointData()->GetScalars() == NULL) {
876        return false;
877    }
878    vtkIdType pt = _dataSet->FindPoint(x, y, z);
879    if (pt < 0)
880        return false;
881    *value = _dataSet->GetPointData()->GetScalars()->GetComponent(pt, 0);
882    return true;
883}
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}
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.