File: vtkOpenGLVolumeLookupTable.cxx

package info (click to toggle)
paraview 5.11.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 497,236 kB
  • sloc: cpp: 3,171,290; ansic: 1,315,072; python: 134,290; xml: 103,324; sql: 65,887; sh: 5,286; javascript: 4,901; yacc: 4,383; java: 3,977; perl: 2,363; lex: 1,909; f90: 1,255; objc: 143; makefile: 119; tcl: 59; pascal: 50; fortran: 29
file content (234 lines) | stat: -rw-r--r-- 7,123 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkOpenGLVolumeLookupTable.cxx

  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
  All rights reserved.
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.

  This software is distributed WITHOUT ANY WARRANTY; without even
  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  PURPOSE.  See the above copyright notice for more information.

=========================================================================*/

#include "vtkOpenGLVolumeLookupTable.h"

#include "vtkColorTransferFunction.h"
#include "vtkImageData.h"
#include "vtkMath.h"
#include "vtkObjectFactory.h"
#include "vtkPiecewiseFunction.h"
#include "vtkTextureObject.h"

// vtkStandardNewMacro(vtkOpenGLVolumeLookupTable);

//------------------------------------------------------------------------------
VTK_ABI_NAMESPACE_BEGIN
vtkOpenGLVolumeLookupTable::~vtkOpenGLVolumeLookupTable()
{
  if (this->TextureObject)
  {
    this->TextureObject->Delete();
    this->TextureObject = nullptr;
  }

  delete[] this->Table;
}

//------------------------------------------------------------------------------
void vtkOpenGLVolumeLookupTable::Activate()
{
  if (!this->TextureObject)
  {
    return;
  }
  this->TextureObject->Activate();
}

//------------------------------------------------------------------------------
void vtkOpenGLVolumeLookupTable::Deactivate()
{
  if (!this->TextureObject)
  {
    return;
  }
  this->TextureObject->Deactivate();
}

//------------------------------------------------------------------------------
inline int vtkOpenGLVolumeLookupTable::GetMaximumSupportedTextureWidth(
  vtkOpenGLRenderWindow* renWin, int idealWidth)
{
  if (!this->TextureObject)
  {
    vtkErrorMacro("vtkTextureObject not initialized!");
    return -1;
  }

  // Try to match the next power of two.
  idealWidth = vtkMath::NearestPowerOfTwo(idealWidth);
  int const maxWidth = vtkTextureObject::GetMaximumTextureSize(renWin);
  if (maxWidth < 0)
  {
    vtkErrorMacro("Failed to query max texture size! using default 1024.");
    return 1024;
  }

  if (maxWidth >= idealWidth)
  {
    idealWidth = vtkMath::Max(1024, idealWidth);
    return idealWidth;
  }

  vtkWarningMacro("This OpenGL implementation does not support the required "
                  "texture size of "
    << idealWidth << ", falling back to maximum allowed, " << maxWidth << "."
    << "This may cause an incorrect lookup table mapping.");

  return maxWidth;
}

//------------------------------------------------------------------------------
int vtkOpenGLVolumeLookupTable::GetTextureUnit()
{
  if (!this->TextureObject)
  {
    return -1;
  }
  return this->TextureObject->GetTextureUnit();
}

//------------------------------------------------------------------------------
void vtkOpenGLVolumeLookupTable::ReleaseGraphicsResources(vtkWindow* window)
{
  if (this->TextureObject)
  {
    this->TextureObject->ReleaseGraphicsResources(window);
    this->TextureObject->Delete();
    this->TextureObject = nullptr;
  }
}

//------------------------------------------------------------------------------
void vtkOpenGLVolumeLookupTable::ComputeIdealTextureSize(
  vtkObject* func, int& width, int& height, vtkOpenGLRenderWindow* renWin)
{
  vtkColorTransferFunction* scalarRGB = vtkColorTransferFunction::SafeDownCast(func);
  if (scalarRGB)
  {
    width = scalarRGB->EstimateMinNumberOfSamples(this->LastRange[0], this->LastRange[1]);
    height = 1;
  }
  vtkPiecewiseFunction* scalarOp = vtkPiecewiseFunction::SafeDownCast(func);
  if (scalarOp)
  {
    width = scalarOp->EstimateMinNumberOfSamples(this->LastRange[0], this->LastRange[1]);
    height = 1;
  }
  vtkImageData* transfer2D = vtkImageData::SafeDownCast(func);
  if (transfer2D)
  {
    int* dims = transfer2D->GetDimensions();
    width = dims[0];
    height = dims[1];
  }
  height = height > 1 ? this->GetMaximumSupportedTextureWidth(renWin, height) : 1;
}

//------------------------------------------------------------------------------
void vtkOpenGLVolumeLookupTable::Update(vtkObject* func, double scalarRange[2], int blendMode,
  double sampleDistance, double unitDistance, int filterValue, vtkOpenGLRenderWindow* renWin)
{
  if (!func || !renWin)
  {
    return;
  }

  if (!this->TextureObject)
  {
    this->TextureObject = vtkTextureObject::New();
  }

  this->TextureObject->SetContext(renWin);

  if (this->NeedsUpdate(func, scalarRange, blendMode, sampleDistance))
  {
    int idealW = 1024;
    int newHeight = 1;
    this->ComputeIdealTextureSize(func, idealW, newHeight, renWin);
    int const newWidth = this->GetMaximumSupportedTextureWidth(renWin, idealW);
    if (!this->Table || this->TextureWidth != newWidth || this->TextureHeight != newHeight)
    {
      this->TextureWidth = newWidth;
      this->TextureHeight = newHeight;
      this->AllocateTable();
    }

    this->InternalUpdate(func, blendMode, sampleDistance, unitDistance, filterValue);
    this->LastInterpolation = filterValue;
    this->BuildTime.Modified();
  }

  if (this->LastInterpolation != filterValue)
  {
    this->LastInterpolation = filterValue;
    this->TextureObject->SetMagnificationFilter(filterValue);
    this->TextureObject->SetMinificationFilter(filterValue);
  }
}

//------------------------------------------------------------------------------
void vtkOpenGLVolumeLookupTable::AllocateTable()
{
  delete[] this->Table;
  this->Table = new float[this->TextureWidth * this->TextureHeight * this->NumberOfColorComponents];
}

//------------------------------------------------------------------------------
bool vtkOpenGLVolumeLookupTable::NeedsUpdate(vtkObject* func, double scalarRange[2],
  int vtkNotUsed(blendMode), double vtkNotUsed(sampleDistance))
{
  if (!func)
  {
    return false;
  }
  if (scalarRange[0] != this->LastRange[0] || scalarRange[1] != this->LastRange[1] ||
    func->GetMTime() > this->BuildTime || this->TextureObject->GetMTime() > this->BuildTime ||
    !this->TextureObject->GetHandle())
  {
    this->LastRange[0] = scalarRange[0];
    this->LastRange[1] = scalarRange[1];
    return true;
  }
  return false;
}

//------------------------------------------------------------------------------
void vtkOpenGLVolumeLookupTable::InternalUpdate(vtkObject* vtkNotUsed(func),
  int vtkNotUsed(blendMode), double vtkNotUsed(sampleDistance), double vtkNotUsed(unitDistance),
  int vtkNotUsed(filterValue))
{
}

//------------------------------------------------------------------------------
void vtkOpenGLVolumeLookupTable::PrintSelf(ostream& os, vtkIndent indent)
{
  this->Superclass::PrintSelf(os, indent);

  os << indent << "TextureObject:";
  if (this->TextureObject)
  {
    this->TextureObject->PrintSelf(os << endl, indent.GetNextIndent());
  }
  else
  {
    os << "(none)\n";
  }

  os << indent << "Last Interpolation: " << this->LastInterpolation << endl;
  os << indent << "Last Range: (" << this->LastRange[0] << ", " << this->LastRange[1] << ")"
     << endl;
}
VTK_ABI_NAMESPACE_END