source: branches/blt4/packages/vizservers/vtkvis/RpGlyphs.cpp @ 2322

Last change on this file since 2322 was 2322, checked in by gah, 13 years ago

update from trunk

File size: 7.6 KB
Line 
1/* -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 * Copyright (C) 2011, Purdue Research Foundation
4 *
5 * Author: Leif Delgass <ldelgass@purdue.edu>
6 */
7
8#include <vtkDataSet.h>
9#include <vtkPointData.h>
10#include <vtkCellData.h>
11#include <vtkCellDataToPointData.h>
12#include <vtkProp.h>
13#include <vtkActor.h>
14#include <vtkProperty.h>
15#include <vtkGlyph3D.h>
16#include <vtkArrowSource.h>
17#include <vtkConeSource.h>
18#include <vtkCylinderSource.h>
19#include <vtkPlatonicSolidSource.h>
20#include <vtkSphereSource.h>
21#include <vtkPolyDataMapper.h>
22
23#include "RpGlyphs.h"
24#include "Trace.h"
25
26using namespace Rappture::VtkVis;
27
28Glyphs::Glyphs() :
29    _dataSet(NULL),
30    _opacity(1.0),
31    _lighting(true),
32    _glyphShape(ARROW),
33    _scaleFactor(1.0)
34{
35}
36
37Glyphs::~Glyphs()
38{
39}
40
41/**
42 * \brief Get the VTK Prop for the Glyphs
43 */
44vtkProp *Glyphs::getProp()
45{
46    return _prop;
47}
48
49/**
50 * \brief Create and initialize a VTK Prop to render Glyphs
51 */
52void Glyphs::initProp()
53{
54    if (_prop == NULL) {
55        _prop = vtkSmartPointer<vtkActor>::New();
56        _prop->GetProperty()->EdgeVisibilityOff();
57        _prop->GetProperty()->SetOpacity(_opacity);
58        _prop->GetProperty()->SetAmbient(.2);
59        if (!_lighting)
60            _prop->GetProperty()->LightingOff();
61    }
62}
63
64/**
65 * \brief Specify input DataSet
66 *
67 * The DataSet must be a PolyData point set
68 * with vectors and/or scalars
69 */
70void Glyphs::setDataSet(DataSet *dataSet)
71{
72    if (_dataSet != dataSet) {
73        _dataSet = dataSet;
74        update();
75    }
76}
77
78/**
79 * \brief Returns the DataSet this Glyphs renders
80 */
81DataSet *Glyphs::getDataSet()
82{
83    return _dataSet;
84}
85
86/**
87 * \brief Set the shape of the glyphs
88 */
89void Glyphs::setGlyphShape(GlyphShape shape)
90{
91    _glyphShape = shape;
92    switch (_glyphShape) {
93    case ARROW:
94        _glyphSource = vtkSmartPointer<vtkArrowSource>::New();
95        break;
96    case CONE:
97        _glyphSource = vtkSmartPointer<vtkConeSource>::New();
98        break;
99    case CUBE:
100        _glyphSource = vtkSmartPointer<vtkPlatonicSolidSource>::New();
101        vtkPlatonicSolidSource::SafeDownCast(_glyphSource)->SetSolidTypeToCube();
102        break;
103    case CYLINDER:
104        _glyphSource = vtkSmartPointer<vtkCylinderSource>::New();
105        break;
106    case DODECAHEDRON:
107        _glyphSource = vtkSmartPointer<vtkPlatonicSolidSource>::New();
108        vtkPlatonicSolidSource::SafeDownCast(_glyphSource)->SetSolidTypeToDodecahedron();
109        break;
110    case ICOSAHEDRON:
111        _glyphSource = vtkSmartPointer<vtkPlatonicSolidSource>::New();
112        vtkPlatonicSolidSource::SafeDownCast(_glyphSource)->SetSolidTypeToIcosahedron();
113        break;
114    case OCTAHEDRON:
115        _glyphSource = vtkSmartPointer<vtkPlatonicSolidSource>::New();
116        vtkPlatonicSolidSource::SafeDownCast(_glyphSource)->SetSolidTypeToOctahedron();
117        break;
118    case SPHERE:
119        _glyphSource = vtkSmartPointer<vtkSphereSource>::New();
120        break;
121    case TETRAHEDRON:
122        _glyphSource = vtkSmartPointer<vtkPlatonicSolidSource>::New();
123        vtkPlatonicSolidSource::SafeDownCast(_glyphSource)->SetSolidTypeToTetrahedron();
124        break;
125    default:
126        ERROR("Unknown glyph shape: %d", _glyphShape);
127        return;
128    }
129    if (_glyphGenerator != NULL) {
130        _glyphGenerator->SetSourceConnection(_glyphSource->GetOutputPort());
131    }
132}
133
134/**
135 * \brief Internal method to set up pipeline after a state change
136 */
137void Glyphs::update()
138{
139    if (_dataSet == NULL) {
140        return;
141    }
142
143    vtkDataSet *ds = _dataSet->getVtkDataSet();
144    double dataRange[2];
145    _dataSet->getDataRange(dataRange);
146
147    if (_glyphGenerator == NULL) {
148        _glyphGenerator = vtkSmartPointer<vtkGlyph3D>::New();
149    }
150
151    setGlyphShape(_glyphShape);
152
153    vtkSmartPointer<vtkCellDataToPointData> cellToPtData;
154
155    if (ds->GetPointData() == NULL ||
156        ds->GetPointData()->GetScalars() == NULL) {
157        WARN("No scalar point data in dataset %s", _dataSet->getName().c_str());
158        if (ds->GetCellData() != NULL &&
159            ds->GetCellData()->GetScalars() != NULL) {
160            cellToPtData =
161                vtkSmartPointer<vtkCellDataToPointData>::New();
162            cellToPtData->SetInput(ds);
163            //cellToPtData->PassCellDataOn();
164            cellToPtData->Update();
165            ds = cellToPtData->GetOutput();
166        } else {
167            ERROR("No scalar cell data in dataset %s", _dataSet->getName().c_str());
168        }
169    }
170
171    _glyphGenerator->SetInput(ds);
172
173    if (ds->GetPointData()->GetVectors() != NULL) {
174        _glyphGenerator->SetScaleModeToScaleByVector();
175    } else {
176        _glyphGenerator->SetScaleModeToScaleByScalar();
177    }
178    _glyphGenerator->SetScaleFactor(_scaleFactor);
179    _glyphGenerator->ScalingOn();
180
181    if (ds->GetPointData()->GetScalars() == NULL) {
182        _glyphGenerator->SetColorModeToColorByVector();
183    } else {
184        _glyphGenerator->SetColorModeToColorByScalar();
185     }
186    if (_glyphShape == SPHERE) {
187        _glyphGenerator->OrientOff();
188    }
189
190    if (_pdMapper == NULL) {
191        _pdMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
192        _pdMapper->SetResolveCoincidentTopologyToPolygonOffset();
193        _pdMapper->ScalarVisibilityOn();
194    }
195    _pdMapper->SetInputConnection(_glyphGenerator->GetOutputPort());
196
197    if (ds->GetPointData() == NULL ||
198        ds->GetPointData()->GetScalars() == NULL) {
199        if (_lut == NULL) {
200            _lut = vtkSmartPointer<vtkLookupTable>::New();
201        }
202    } else {
203        vtkLookupTable *lut = ds->GetPointData()->GetScalars()->GetLookupTable();
204        TRACE("Data set scalars lookup table: %p\n", lut);
205        if (_lut == NULL) {
206            if (lut)
207                _lut = lut;
208            else
209                _lut = vtkSmartPointer<vtkLookupTable>::New();
210        }
211    }
212
213    if (ds->GetPointData()->GetScalars() == NULL) {
214        _pdMapper->UseLookupTableScalarRangeOff();
215    } else {
216        _lut->SetRange(dataRange);
217        _pdMapper->UseLookupTableScalarRangeOn();
218    }
219
220    _pdMapper->SetLookupTable(_lut);
221
222    initProp();
223
224    _prop->SetMapper(_pdMapper);
225    _pdMapper->Update();
226}
227
228/**
229 * \brief Controls relative scaling of glyphs
230 */
231void Glyphs::setScaleFactor(double scale)
232{
233    _scaleFactor = scale;
234    if (_glyphGenerator != NULL) {
235        _glyphGenerator->SetScaleFactor(scale);
236        _pdMapper->Update();
237    }
238}
239
240/**
241 * \brief Get the VTK colormap lookup table in use
242 */
243vtkLookupTable *Glyphs::getLookupTable()
244{
245    return _lut;
246}
247
248/**
249 * \brief Associate a colormap lookup table with the DataSet
250 */
251void Glyphs::setLookupTable(vtkLookupTable *lut)
252{
253    if (lut == NULL) {
254        _lut = vtkSmartPointer<vtkLookupTable>::New();
255    } else {
256        _lut = lut;
257    }
258
259    if (_pdMapper != NULL) {
260        _pdMapper->SetLookupTable(_lut);
261    }
262}
263
264/**
265 * \brief Turn on/off rendering of this Glyphs
266 */
267void Glyphs::setVisibility(bool state)
268{
269    if (_prop != NULL) {
270        _prop->SetVisibility((state ? 1 : 0));
271    }
272}
273
274/**
275 * \brief Get visibility state of the Glyphs
276 *
277 * \return Are the glyphs visible?
278 */
279bool Glyphs::getVisibility()
280{
281    if (_prop == NULL) {
282        return false;
283    } else {
284        return (_prop->GetVisibility() != 0);
285    }
286}
287
288/**
289 * \brief Set opacity used to render the Glyphs
290 */
291void Glyphs::setOpacity(double opacity)
292{
293    _opacity = opacity;
294    if (_prop != NULL)
295        _prop->GetProperty()->SetOpacity(opacity);
296}
297
298/**
299 * \brief Get opacity used to render the Glyphs
300 */
301double Glyphs::getOpacity()
302{
303    return _opacity;
304}
305
306/**
307 * \brief Set a group of world coordinate planes to clip rendering
308 *
309 * Passing NULL for planes will remove all cliping planes
310 */
311void Glyphs::setClippingPlanes(vtkPlaneCollection *planes)
312{
313    if (_pdMapper != NULL) {
314        _pdMapper->SetClippingPlanes(planes);
315    }
316}
317
318/**
319 * \brief Turn on/off lighting of this object
320 */
321void Glyphs::setLighting(bool state)
322{
323    _lighting = state;
324    if (_prop != NULL)
325        _prop->GetProperty()->SetLighting((state ? 1 : 0));
326}
Note: See TracBrowser for help on using the repository browser.