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 <cassert> |
---|
9 | |
---|
10 | #include <vtkDataSet.h> |
---|
11 | #include <vtkPointData.h> |
---|
12 | #include <vtkDataSetMapper.h> |
---|
13 | #include <vtkUnstructuredGrid.h> |
---|
14 | #include <vtkProperty.h> |
---|
15 | #include <vtkImageData.h> |
---|
16 | #include <vtkLookupTable.h> |
---|
17 | #include <vtkDelaunay2D.h> |
---|
18 | #include <vtkDelaunay3D.h> |
---|
19 | #include <vtkGaussianSplatter.h> |
---|
20 | #include <vtkExtractVOI.h> |
---|
21 | #include <vtkDataSetSurfaceFilter.h> |
---|
22 | |
---|
23 | #include "RpPseudoColor.h" |
---|
24 | #include "Trace.h" |
---|
25 | |
---|
26 | #define MESH_POINT_CLOUDS |
---|
27 | |
---|
28 | using namespace Rappture::VtkVis; |
---|
29 | |
---|
30 | PseudoColor::PseudoColor() : |
---|
31 | VtkGraphicsObject() |
---|
32 | { |
---|
33 | } |
---|
34 | |
---|
35 | PseudoColor::~PseudoColor() |
---|
36 | { |
---|
37 | #ifdef WANT_TRACE |
---|
38 | if (_dataSet != NULL) |
---|
39 | TRACE("Deleting PseudoColor for %s", _dataSet->getName().c_str()); |
---|
40 | else |
---|
41 | TRACE("Deleting PseudoColor with NULL DataSet"); |
---|
42 | #endif |
---|
43 | } |
---|
44 | |
---|
45 | /** |
---|
46 | * \brief Create and initialize a VTK Prop to render the colormapped dataset |
---|
47 | */ |
---|
48 | void PseudoColor::initProp() |
---|
49 | { |
---|
50 | if (_prop == NULL) { |
---|
51 | _prop = vtkSmartPointer<vtkActor>::New(); |
---|
52 | vtkProperty *property = getActor()->GetProperty(); |
---|
53 | property->SetOpacity(_opacity); |
---|
54 | property->SetEdgeColor(_edgeColor[0], _edgeColor[1], _edgeColor[2]); |
---|
55 | property->SetLineWidth(_edgeWidth); |
---|
56 | property->EdgeVisibilityOff(); |
---|
57 | property->SetAmbient(.2); |
---|
58 | if (!_lighting) |
---|
59 | property->LightingOff(); |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * \brief Internal method to set up pipeline after a state change |
---|
65 | */ |
---|
66 | void PseudoColor::update() |
---|
67 | { |
---|
68 | if (_dataSet == NULL) |
---|
69 | return; |
---|
70 | |
---|
71 | vtkDataSet *ds = _dataSet->getVtkDataSet(); |
---|
72 | |
---|
73 | double dataRange[2]; |
---|
74 | _dataSet->getDataRange(dataRange); |
---|
75 | |
---|
76 | // Mapper, actor to render color-mapped data set |
---|
77 | if (_dsMapper == NULL) { |
---|
78 | _dsMapper = vtkSmartPointer<vtkDataSetMapper>::New(); |
---|
79 | } |
---|
80 | |
---|
81 | vtkPolyData *pd = vtkPolyData::SafeDownCast(ds); |
---|
82 | if (pd) { |
---|
83 | // DataSet is a vtkPolyData |
---|
84 | if (pd->GetNumberOfLines() == 0 && |
---|
85 | pd->GetNumberOfPolys() == 0 && |
---|
86 | pd->GetNumberOfStrips() == 0) { |
---|
87 | // DataSet is a point cloud |
---|
88 | if (_dataSet->is2D()) { |
---|
89 | #ifdef MESH_POINT_CLOUDS |
---|
90 | vtkSmartPointer<vtkDelaunay2D> mesher = vtkSmartPointer<vtkDelaunay2D>::New(); |
---|
91 | mesher->SetInput(pd); |
---|
92 | vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); |
---|
93 | gf->SetInputConnection(mesher->GetOutputPort()); |
---|
94 | _dsMapper->SetInputConnection(gf->GetOutputPort()); |
---|
95 | #else |
---|
96 | vtkSmartPointer<vtkGaussianSplatter> splatter = vtkSmartPointer<vtkGaussianSplatter>::New(); |
---|
97 | splatter->SetInput(pd); |
---|
98 | int dims[3]; |
---|
99 | splatter->GetSampleDimensions(dims); |
---|
100 | TRACE("Sample dims: %d %d %d", dims[0], dims[1], dims[2]); |
---|
101 | dims[2] = 3; |
---|
102 | splatter->SetSampleDimensions(dims); |
---|
103 | double bounds[6]; |
---|
104 | splatter->Update(); |
---|
105 | splatter->GetModelBounds(bounds); |
---|
106 | TRACE("Model bounds: %g %g %g %g %g %g", |
---|
107 | bounds[0], bounds[1], |
---|
108 | bounds[2], bounds[3], |
---|
109 | bounds[4], bounds[5]); |
---|
110 | vtkSmartPointer<vtkExtractVOI> slicer = vtkSmartPointer<vtkExtractVOI>::New(); |
---|
111 | slicer->SetInputConnection(splatter->GetOutputPort()); |
---|
112 | slicer->SetVOI(0, dims[0]-1, 0, dims[1]-1, 1, 1); |
---|
113 | slicer->SetSampleRate(1, 1, 1); |
---|
114 | vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); |
---|
115 | gf->UseStripsOn(); |
---|
116 | gf->SetInputConnection(slicer->GetOutputPort()); |
---|
117 | _dsMapper->SetInputConnection(gf->GetOutputPort()); |
---|
118 | #endif |
---|
119 | } else { |
---|
120 | vtkSmartPointer<vtkDelaunay3D> mesher = vtkSmartPointer<vtkDelaunay3D>::New(); |
---|
121 | mesher->SetInput(pd); |
---|
122 | vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); |
---|
123 | gf->SetInputConnection(mesher->GetOutputPort()); |
---|
124 | _dsMapper->SetInputConnection(gf->GetOutputPort()); |
---|
125 | } |
---|
126 | } else { |
---|
127 | // DataSet is a vtkPolyData with lines and/or polygons |
---|
128 | _dsMapper->SetInput(ds); |
---|
129 | } |
---|
130 | } else { |
---|
131 | TRACE("Generating surface for data set"); |
---|
132 | // DataSet is NOT a vtkPolyData |
---|
133 | vtkSmartPointer<vtkDataSetSurfaceFilter> gf = vtkSmartPointer<vtkDataSetSurfaceFilter>::New(); |
---|
134 | gf->SetInput(ds); |
---|
135 | _dsMapper->SetInputConnection(gf->GetOutputPort()); |
---|
136 | } |
---|
137 | |
---|
138 | if (ds->GetPointData() == NULL || |
---|
139 | ds->GetPointData()->GetScalars() == NULL) { |
---|
140 | WARN("No scalar point data in dataset %s", _dataSet->getName().c_str()); |
---|
141 | if (_lut == NULL) { |
---|
142 | _lut = vtkSmartPointer<vtkLookupTable>::New(); |
---|
143 | } |
---|
144 | } else { |
---|
145 | vtkLookupTable *lut = ds->GetPointData()->GetScalars()->GetLookupTable(); |
---|
146 | TRACE("Data set scalars lookup table: %p\n", lut); |
---|
147 | if (_lut == NULL) { |
---|
148 | if (lut) |
---|
149 | _lut = lut; |
---|
150 | else |
---|
151 | _lut = vtkSmartPointer<vtkLookupTable>::New(); |
---|
152 | } |
---|
153 | } |
---|
154 | |
---|
155 | _lut->SetRange(dataRange); |
---|
156 | |
---|
157 | _dsMapper->SetColorModeToMapScalars(); |
---|
158 | _dsMapper->UseLookupTableScalarRangeOn(); |
---|
159 | _dsMapper->SetLookupTable(_lut); |
---|
160 | //_dsMapper->InterpolateScalarsBeforeMappingOn(); |
---|
161 | |
---|
162 | initProp(); |
---|
163 | getActor()->SetMapper(_dsMapper); |
---|
164 | _dsMapper->Update(); |
---|
165 | } |
---|
166 | |
---|
167 | /** |
---|
168 | * \brief Get the VTK colormap lookup table in use |
---|
169 | */ |
---|
170 | vtkLookupTable *PseudoColor::getLookupTable() |
---|
171 | { |
---|
172 | return _lut; |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * \brief Associate a colormap lookup table with the DataSet |
---|
177 | */ |
---|
178 | void PseudoColor::setLookupTable(vtkLookupTable *lut) |
---|
179 | { |
---|
180 | if (lut == NULL) { |
---|
181 | _lut = vtkSmartPointer<vtkLookupTable>::New(); |
---|
182 | } else { |
---|
183 | _lut = lut; |
---|
184 | } |
---|
185 | |
---|
186 | if (_dsMapper != NULL) { |
---|
187 | _dsMapper->UseLookupTableScalarRangeOn(); |
---|
188 | _dsMapper->SetLookupTable(_lut); |
---|
189 | } |
---|
190 | } |
---|
191 | |
---|
192 | /** |
---|
193 | * \brief Set a group of world coordinate planes to clip rendering |
---|
194 | * |
---|
195 | * Passing NULL for planes will remove all cliping planes |
---|
196 | */ |
---|
197 | void PseudoColor::setClippingPlanes(vtkPlaneCollection *planes) |
---|
198 | { |
---|
199 | if (_dsMapper != NULL) { |
---|
200 | _dsMapper->SetClippingPlanes(planes); |
---|
201 | } |
---|
202 | } |
---|