VTK  9.7.20261123
vtkWebGPUVolumeTestHelpers.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2// SPDX-License-Identifier: BSD-3-Clause
8
9#ifndef vtkWebGPUVolumeTestHelpers_h
10#define vtkWebGPUVolumeTestHelpers_h
11
13#include "vtkImageData.h"
14#include "vtkNew.h"
16#include "vtkPointData.h"
17#include "vtkRenderWindow.h"
18#include "vtkSmartPointer.h"
20#include "vtkVolumeProperty.h"
21
22#include <algorithm>
23#include <cstdlib>
24#include <iostream>
25#include <string>
26#include <vector>
27
29{
30
35inline bool CheckClass(vtkObject* object, const char* expected)
36{
37 if (object == nullptr || std::string(object->GetClassName()) != expected)
38 {
39 std::cerr << "Expected a " << expected << " but got "
40 << (object != nullptr ? object->GetClassName() : "nothing") << ".\n";
41 return false;
42 }
43 return true;
44}
45
49struct Frame
50{
51 std::vector<unsigned char> Pixels;
52 int Width = 0;
53 int Height = 0;
54
55 const unsigned char* At(int x, int y) const
56 {
57 return &this->Pixels[3 * (static_cast<std::size_t>(y) * this->Width + x)];
58 }
59
61 std::size_t CountLit(const double background[3], int tolerance = 12) const
62 {
63 std::size_t count = 0;
64 for (std::size_t i = 0; i + 2 < this->Pixels.size(); i += 3)
65 {
66 bool lit = false;
67 for (int c = 0; c < 3; ++c)
68 {
69 const int reference = static_cast<int>(background[c] * 255.0 + 0.5);
70 lit = lit || std::abs(static_cast<int>(this->Pixels[i + c]) - reference) > tolerance;
71 }
72 count += lit ? 1 : 0;
73 }
74 return count;
75 }
76};
77
79{
80 Frame frame;
81 const int* size = window->GetSize();
82 frame.Width = size[0];
83 frame.Height = size[1];
84
86 window->GetPixelData(0, 0, frame.Width - 1, frame.Height - 1, /*front=*/1, pixels);
87 frame.Pixels.assign(pixels->GetPointer(0),
88 pixels->GetPointer(0) + static_cast<std::size_t>(pixels->GetNumberOfValues()));
89 return frame;
90}
91
95inline int LargestDifference(const Frame& left, const Frame& right)
96{
97 if (left.Pixels.size() != right.Pixels.size())
98 {
99 return 255;
100 }
101 int largest = 0;
102 for (std::size_t i = 0; i < left.Pixels.size(); ++i)
103 {
104 largest = std::max(
105 largest, std::abs(static_cast<int>(left.Pixels[i]) - static_cast<int>(right.Pixels[i])));
106 }
107 return largest;
108}
109
113inline double MeanDifference(const Frame& left, const Frame& right)
114{
115 if (left.Pixels.size() != right.Pixels.size() || left.Pixels.empty())
116 {
117 return 255.0;
118 }
119 double total = 0.0;
120 for (std::size_t i = 0; i < left.Pixels.size(); ++i)
121 {
122 total += std::abs(static_cast<int>(left.Pixels[i]) - static_cast<int>(right.Pixels[i]));
123 }
124 return total / left.Pixels.size();
125}
126
131 const double range[2], const double rgb[3] = nullptr)
132{
134 if (rgb != nullptr)
135 {
136 color->AddRGBPoint(range[0], 0.0, 0.0, 0.0);
137 color->AddRGBPoint(range[1], rgb[0], rgb[1], rgb[2]);
138 }
139 else
140 {
141 color->AddRGBPoint(range[0], 0.0, 0.1, 0.6);
142 color->AddRGBPoint(0.5 * (range[0] + range[1]), 0.1, 0.9, 0.3);
143 color->AddRGBPoint(range[1], 1.0, 0.8, 0.1);
144 }
145
147 opacity->AddPoint(range[0], 0.0);
148 opacity->AddPoint(0.5 * (range[0] + range[1]), 0.05);
149 opacity->AddPoint(range[1], 0.6);
150
152 property->SetColor(color);
153 property->SetScalarOpacity(opacity);
154 property->SetScalarOpacityUnitDistance(2.0);
155 property->SetInterpolationTypeToLinear();
156 return property;
157}
158
159} // namespace vtkWebGPUVolumeTest
160
161#endif
162// VTK-HeaderTest-Exclude: vtkWebGPUVolumeTestHelpers.h
int count
Definition PyrGnBasis.h:1
Allocate and hold a VTK object.
Definition vtkNew.h:168
T * GetPointer() const noexcept
Get a raw pointer to the contained object.
Definition vtkNew.h:257
const char * GetClassName() const
Return the class name as a string.
abstract base class for most VTK objects
Definition vtkObject.h:162
create a window for renderers to draw into
Hold a reference to a vtkObjectBase instance.
static vtkSmartPointer< T > New()
Create an instance of a VTK object.
virtual unsigned char * GetPixelData(int, int, int, int, int, int=0)
Get the pixel data of an image, transmitted as RGBRGBRGB.
Definition vtkWindow.h:238
virtual int * GetSize()
Get the size (width and height) of the rendering window in screen coordinates (in pixels).
Shared scaffolding for the WebGPU volume tests: it pins the rendering backend, builds the transfer fu...
Frame Capture(vtkRenderWindow *window)
vtkSmartPointer< vtkVolumeProperty > MakeProperty(const double range[2], const double rgb[3]=nullptr)
A property that maps range onto a blue to yellow ramp of growing opacity.
int LargestDifference(const Frame &left, const Frame &right)
Largest per channel difference between two frames of the same size.
double MeanDifference(const Frame &left, const Frame &right)
Mean per channel difference between two frames of the same size.
bool CheckClass(vtkObject *object, const char *expected)
Fail loudly when the factory handed back something other than the class under test,...
A frame, as RGB triples ordered row by row.
std::size_t CountLit(const double background[3], int tolerance=12) const
Pixels that differ from the background by more than tolerance per channel.
const unsigned char * At(int x, int y) const
std::vector< unsigned char > Pixels