source: trunk/packages/vizservers/vtkvis/RpPseudoColor.cpp @ 2137

Last change on this file since 2137 was 2137, checked in by ldelgass, 13 years ago

Finish up making dataset name optional (meaning "all") in vtkvis protocol.

  • Property svn:eol-style set to native
File size: 6.2 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 <vtkDataSetMapper.h>
10#include <vtkProperty.h>
11#include <vtkPointData.h>
12#include <vtkLookupTable.h>
13
14#include "RpPseudoColor.h"
15#include "Trace.h"
16
17using namespace Rappture::VtkVis;
18
19PseudoColor::PseudoColor() :
20    _dataSet(NULL),
21    _opacity(1.0),
22    _edgeWidth(1.0)
23{
24    _edgeColor[0] = 0.0;
25    _edgeColor[1] = 0.0;
26    _edgeColor[2] = 0.0;
27}
28
29PseudoColor::~PseudoColor()
30{
31#ifdef WANT_TRACE
32    if (_dataSet != NULL)
33        TRACE("Deleting PseudoColor for %s", _dataSet->getName().c_str());
34    else
35        TRACE("Deleting PseudoColor with NULL DataSet");
36#endif
37}
38
39/**
40 * \brief Specify input DataSet with scalars to colormap
41 *
42 * Currently the DataSet must be image data (2D uniform grid)
43 */
44void PseudoColor::setDataSet(DataSet *dataSet)
45{
46    _dataSet = dataSet;
47    update();
48}
49
50/**
51 * \brief Returns the DataSet this PseudoColor renders
52 */
53DataSet *PseudoColor::getDataSet()
54{
55    return _dataSet;
56}
57
58/**
59 * \brief Internal method to set up color mapper after a state change
60 */
61void PseudoColor::update()
62{
63    if (_dataSet == NULL)
64        return;
65
66    vtkDataSet *ds = _dataSet->getVtkDataSet();
67
68    double dataRange[2];
69    _dataSet->getDataRange(dataRange);
70
71    // Mapper, actor to render color-mapped data set
72    if (_dsMapper == NULL) {
73        _dsMapper = vtkSmartPointer<vtkDataSetMapper>::New();
74    }
75    _dsMapper->SetInput(ds);
76    _dsMapper->StaticOff();
77
78    if (ds->GetPointData() == NULL ||
79        ds->GetPointData()->GetScalars() == NULL) {
80        ERROR("No scalar point data in dataset %s", _dataSet->getName().c_str());
81        if (_lut == NULL) {
82            _lut = vtkSmartPointer<vtkLookupTable>::New();
83        }
84    } else {
85        vtkLookupTable *lut = ds->GetPointData()->GetScalars()->GetLookupTable();
86        TRACE("Data set scalars lookup table: %p\n", lut);
87        if (_lut == NULL) {
88            if (lut)
89                _lut = lut;
90            else
91                _lut = vtkSmartPointer<vtkLookupTable>::New();
92        }
93    }
94
95    _lut->SetRange(dataRange);
96
97    _dsMapper->SetLookupTable(_lut);
98    _dsMapper->SetScalarRange(dataRange);
99    //_dsMapper->GetLookupTable()->SetRange(dataRange);
100    //_dsMapper->InterpolateScalarsBeforeMappingOn();
101
102    initActor();
103    _dsActor->SetMapper(_dsMapper);
104    //_dsActor->GetProperty()->SetRepresentationToWireframe();
105}
106
107/**
108 * \brief Get the VTK Actor for the colormapped dataset
109 */
110vtkActor *PseudoColor::getActor()
111{
112    return _dsActor;
113}
114
115/**
116 * \brief Create and initialize a VTK actor to render the colormapped dataset
117 */
118void PseudoColor::initActor()
119{
120    if (_dsActor == NULL) {
121        _dsActor = vtkSmartPointer<vtkActor>::New();
122        _dsActor->GetProperty()->SetOpacity(_opacity);
123        _dsActor->GetProperty()->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]);
124        _dsActor->GetProperty()->SetLineWidth(_edgeWidth);
125        _dsActor->GetProperty()->EdgeVisibilityOff();
126    }
127}
128
129/**
130 * \brief Get the VTK colormap lookup table in use
131 */
132vtkLookupTable *PseudoColor::getLookupTable()
133{
134    return _lut;
135}
136
137/**
138 * \brief Associate a colormap lookup table with the DataSet
139 */
140void PseudoColor::setLookupTable(vtkLookupTable *lut)
141{
142    if (lut == NULL) {
143        _lut = vtkSmartPointer<vtkLookupTable>::New();
144    } else {
145        _lut = lut;
146    }
147
148    double dataRange[2];
149    if (_dataSet != NULL) {
150        _dataSet->getDataRange(dataRange);
151        _lut->SetRange(dataRange);
152#ifdef notdef
153        if (_dataSet->getVtkDataSet()->GetPointData() &&
154            _dataSet->getVtkDataSet()->GetPointData()->GetScalars() &&
155            _dataSet->getVtkDataSet()->GetPointData()->GetScalars()->GetLookupTable()) {
156            TRACE("Change scalar table: %p %p\n",
157                  _dataSet->getVtkDataSet()->GetPointData()->GetScalars()->GetLookupTable(),
158                  _lut.GetPointer());
159            _dataSet->getVtkDataSet()->GetPointData()->GetScalars()->SetLookupTable(_lut);
160            TRACE("Scalar Table: %p\n", _dataSet->getVtkDataSet()->GetPointData()->GetScalars()->GetLookupTable());
161        }
162#endif
163    }
164    if (_dsMapper != NULL) {
165        _dsMapper->UseLookupTableScalarRangeOn();
166        _dsMapper->SetLookupTable(_lut);
167    }
168}
169
170/**
171 * \brief Turn on/off rendering of this colormapped dataset
172 */
173void PseudoColor::setVisibility(bool state)
174{
175    if (_dsActor != NULL) {
176        _dsActor->SetVisibility((state ? 1 : 0));
177    }
178}
179
180/**
181 * \brief Get visibility state of the colormapped dataset
182 *
183 * \return Is PseudoColor visible?
184 */
185bool PseudoColor::getVisibility()
186{
187    if (_dsActor == NULL) {
188        return false;
189    } else {
190        return (_dsActor->GetVisibility() != 0);
191    }
192}
193
194/**
195 * \brief Set opacity used to render the colormapped dataset
196 */
197void PseudoColor::setOpacity(double opacity)
198{
199    _opacity = opacity;
200    if (_dsActor != NULL)
201        _dsActor->GetProperty()->SetOpacity(opacity);
202}
203
204/**
205 * \brief Turn on/off rendering of mesh edges
206 */
207void PseudoColor::setEdgeVisibility(bool state)
208{
209    if (_dsActor != NULL) {
210        _dsActor->GetProperty()->SetEdgeVisibility((state ? 1 : 0));
211    }
212}
213
214/**
215 * \brief Set RGB color of polygon edges
216 */
217void PseudoColor::setEdgeColor(float color[3])
218{
219    _edgeColor[0] = color[0];
220    _edgeColor[1] = color[1];
221    _edgeColor[2] = color[2];
222    if (_dsActor != NULL)
223        _dsActor->GetProperty()->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]);
224}
225
226/**
227 * \brief Set pixel width of polygon edges (may be a no-op)
228 */
229void PseudoColor::setEdgeWidth(float edgeWidth)
230{
231    _edgeWidth = edgeWidth;
232    if (_dsActor != NULL)
233        _dsActor->GetProperty()->SetLineWidth(_edgeWidth);
234}
235
236/**
237 * \brief Set a group of world coordinate planes to clip rendering
238 *
239 * Passing NULL for planes will remove all cliping planes
240 */
241void PseudoColor::setClippingPlanes(vtkPlaneCollection *planes)
242{
243    if (_dsMapper != NULL) {
244        _dsMapper->SetClippingPlanes(planes);
245    }
246}
247
248/**
249 * \brief Turn on/off lighting of this object
250 */
251void PseudoColor::setLighting(bool state)
252{
253    if (_dsActor != NULL)
254        _dsActor->GetProperty()->SetLighting((state ? 1 : 0));
255}
Note: See TracBrowser for help on using the repository browser.