| 1 |
/*========================================================================= |
| 2 |
* |
| 3 |
* Copyright NumFOCUS |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
* |
| 17 |
*=========================================================================*/ |
| 18 |
#ifndef QuickView_h |
| 19 |
#define QuickView_h |
| 20 |
|
| 21 |
#include <vector> |
| 22 |
#include <algorithm> |
| 23 |
#include <string> |
| 24 |
|
| 25 |
#include <itkImage.h> |
| 26 |
#include <itkRGBPixel.h> |
| 27 |
#include <itkIntTypes.h> |
| 28 |
|
| 29 |
#include "ITKVtkGlueExport.h" |
| 30 |
|
| 31 |
/** |
| 32 |
* \class ImageInfo |
| 33 |
* \brief A container for an image and its description |
| 34 |
* \ingroup ITKVtkGlue |
| 35 |
*/ |
| 36 |
class ITKVtkGlue_EXPORT ImageInfo |
| 37 |
{ |
| 38 |
public: |
| 39 |
using ImageType = itk::Image<unsigned char, 2>; |
| 40 |
|
| 41 |
ImageInfo(ImageType * image, std::string description = "") |
| 42 |
{ |
| 43 |
m_Image = image; |
| 44 |
m_Description = description; |
| 45 |
} |
| 46 |
|
| 47 |
ImageType::Pointer m_Image; |
| 48 |
std::string m_Description; |
| 49 |
}; |
| 50 |
|
| 51 |
/** |
| 52 |
* \class RGBImageInfo |
| 53 |
* \brief A container for an rgb image and its description |
| 54 |
* \ingroup ITKVtkGlue |
| 55 |
*/ |
| 56 |
class ITKVtkGlue_EXPORT RGBImageInfo |
| 57 |
{ |
| 58 |
public: |
| 59 |
using ImageType = itk::Image<itk::RGBPixel<unsigned char>, 2>; |
| 60 |
RGBImageInfo(ImageType * image, std::string description = "") |
| 61 |
{ |
| 62 |
m_Image = image; |
| 63 |
m_Description = description; |
| 64 |
} |
| 65 |
|
| 66 |
ImageType::Pointer m_Image; |
| 67 |
std::string m_Description; |
| 68 |
}; |
| 69 |
|
| 70 |
/** |
| 71 |
* \class QuickView |
| 72 |
* \brief A convenient class to render itk images with vtk |
| 73 |
* |
| 74 |
* This class presents a convenient and efficient mechanism to display |
| 75 |
* ITK images in VTK render windows. |
| 76 |
* |
| 77 |
* The AddImage and AddRGBImage methods collect ITK images to be |
| 78 |
* rendered in a collection of VTK RenderWindow's. Each image can |
| 79 |
* be flipped about the vertical axis. An optional description will be |
| 80 |
* displayed at the bottom of each render window. |
| 81 |
* |
| 82 |
* If m_ShareCamera is true, a single <a href="https://www.vtk.org/doc/nightly/html/classvtkCamera.html">vtkCamera</a> |
| 83 |
* will be used for each render window (default is false). |
| 84 |
* |
| 85 |
* Each image is rescaled to have a range between 0 and 255. Currently, the size |
| 86 |
* of each render window is fixed at 300,300 and the text size for descriptions |
| 87 |
* is fixed at 10. |
| 88 |
* |
| 89 |
* The Visualize method displays the render windows and starts a |
| 90 |
* <a href="https://www.vtk.org/doc/nightly/html/classvtkInteractorStyleImage.html">vtkInteractorStyleImage</a>. |
| 91 |
* The layout and background color of each render window is fixed. The optional |
| 92 |
* boolean for the constructor, if false, bypasses the interactor. This is useful |
| 93 |
* for running tests. |
| 94 |
* |
| 95 |
* Typical usage: |
| 96 |
* |
| 97 |
\code |
| 98 |
QuickView viewer; |
| 99 |
viewer.AddImage(someFilter->GetOutput(). |
| 100 |
true (to flip image) or false. |
| 101 |
"text to display with the image"); |
| 102 |
|
| 103 |
viewer.AddRGBImage(someFilter->GetOutput(). |
| 104 |
true (to flip image) or false. |
| 105 |
"text to display with the image"); |
| 106 |
\endcode |
| 107 |
* |
| 108 |
* \ingroup ITKVtkGlue |
| 109 |
*/ |
| 110 |
class ITKVtkGlue_EXPORT QuickView |
| 111 |
{ |
| 112 |
public: |
| 113 |
QuickView() |
| 114 |
{ |
| 115 |
m_ShareCamera = true; |
| 116 |
m_Interpolate = true; |
| 117 |
m_Counter = 0; |
| 118 |
m_Snapshot = false; |
| 119 |
m_SnapshotPath = ""; |
| 120 |
m_SnapshotPrefix = "snapshot_"; |
| 121 |
m_SnapshotExtension = "png"; |
| 122 |
m_NumberOfColumns = 4; |
| 123 |
m_ViewPortSize = 300; |
| 124 |
} |
| 125 |
/** Add an image to be rendered. */ |
| 126 |
template <typename TImage> |
| 127 |
void |
| 128 |
AddImage(TImage *, bool FlipVertical = true, std::string Description = ""); |
| 129 |
|
| 130 |
/** Add an RGB image to be rendered */ |
| 131 |
template <typename TImage> |
| 132 |
void |
| 133 |
AddRGBImage(TImage *, bool FlipVertical = true, std::string Description = ""); |
| 134 |
|
| 135 |
/** Render the images. If interact is true, start a vtk |
| 136 |
* Interactor. If false, return after one render. |
| 137 |
*/ |
| 138 |
void |
| 139 |
Visualize(bool interact = true); |
| 140 |
|
| 141 |
|
| 142 |
/** Each render window will have its own camera */ |
| 143 |
void |
| 144 |
ShareCameraOff() |
| 145 |
{ |
| 146 |
m_ShareCamera = false; |
| 147 |
} |
| 148 |
|
| 149 |
/** Each render window will use the same camera */ |
| 150 |
void |
| 151 |
ShareCameraOn() |
| 152 |
{ |
| 153 |
m_ShareCamera = true; |
| 154 |
} |
| 155 |
|
| 156 |
/** Use pixel replication in rendered image */ |
| 157 |
void |
| 158 |
InterpolateOff() |
| 159 |
{ |
| 160 |
m_Interpolate = false; |
| 161 |
} |
| 162 |
|
| 163 |
/** Use pixel interpolation in rendered image */ |
| 164 |
void |
| 165 |
InterpolateOn() |
| 166 |
{ |
| 167 |
m_Interpolate = true; |
| 168 |
} |
| 169 |
|
| 170 |
/** Each render window will take a snapshot */ |
| 171 |
void |
| 172 |
SnapshotOn() |
| 173 |
{ |
| 174 |
m_Snapshot = true; |
| 175 |
} |
| 176 |
|
| 177 |
/** Each render window will take a snapshot */ |
| 178 |
void |
| 179 |
SnapshotOff() |
| 180 |
{ |
| 181 |
m_Snapshot = false; |
| 182 |
} |
| 183 |
|
| 184 |
void |
| 185 |
SetSnapshotPath(const std::string & iPath) |
| 186 |
{ |
| 187 |
m_SnapshotPath = iPath; |
| 188 |
} |
| 189 |
|
| 190 |
void |
| 191 |
SetSnapshotPrefix(const std::string & iPrefix) |
| 192 |
{ |
| 193 |
m_SnapshotPrefix = iPrefix; |
| 194 |
} |
| 195 |
|
| 196 |
/** Provide the image format to be used when taking snapshot */ |
| 197 |
void |
| 198 |
SetSnapshotExtension(const std::string & iExtension) |
| 199 |
{ |
| 200 |
m_SnapshotExtension = iExtension; |
| 201 |
std::transform(m_SnapshotExtension.begin(), m_SnapshotExtension.end(), m_SnapshotExtension.begin(), ::tolower); |
| 202 |
} |
| 203 |
|
| 204 |
/** Set the number of columns, default 4.*/ |
| 205 |
void |
| 206 |
SetNumberOfColumns(const unsigned int columns) |
| 207 |
{ |
| 208 |
m_NumberOfColumns = columns; |
| 209 |
} |
| 210 |
|
| 211 |
/** Set the viewport size, default 300.*/ |
| 212 |
void |
| 213 |
SetViewPortSize(const unsigned int size) |
| 214 |
{ |
| 215 |
m_ViewPortSize = size; |
| 216 |
} |
| 217 |
|
| 218 |
private: |
| 219 |
std::vector<ImageInfo> Images; // Container for images |
| 220 |
std::vector<RGBImageInfo> RGBImages; // Container for rgb images |
| 221 |
itk::IdentifierType m_Counter; |
| 222 |
std::string m_SnapshotPath; |
| 223 |
std::string m_SnapshotPrefix; |
| 224 |
std::string m_SnapshotExtension; |
| 225 |
bool m_ShareCamera; |
| 226 |
bool m_Snapshot; |
| 227 |
bool m_Interpolate; |
| 228 |
unsigned int m_NumberOfColumns; |
| 229 |
unsigned int m_ViewPortSize; |
| 230 |
}; |
| 231 |
|
| 232 |
#endif |