https://mooseframework.inl.gov
ImageSampler.h
Go to the documentation of this file.
1 //* This file is part of the MOOSE framework
2 //* https://mooseframework.inl.gov
3 //*
4 //* All rights reserved, see COPYRIGHT for full restrictions
5 //* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6 //*
7 //* Licensed under LGPL 2.1, please see LICENSE for details
8 //* https://www.gnu.org/licenses/lgpl-2.1.html
9 
10 #pragma once
11 
12 // MOOSE includes
13 #include "FileRangeBuilder.h"
14 #include "ConsoleStream.h"
15 
16 #include "libmesh/bounding_box.h"
17 #include <array>
18 
19 // VTK includes
20 #ifdef LIBMESH_HAVE_VTK
21 
22 // Some VTK header files have extra semi-colons in them, and clang
23 // loves to warn about it...
24 #include "libmesh/ignore_warnings.h"
25 
26 // If VTK is built without an external nlohmann, then it assumes it
27 // will never be compiled against another nlohmann, and it includes
28 // its own copy but modified with macro tricks. We have probably
29 // already included nlohmann headers, which we didn't tamper with
30 // because OF COURSE NOT, but now we need to take care not to let the
31 // include guards prevent them from including their copy with their
32 // different namespace.
33 #ifndef MOOSE_VTK_UNDEF_NLOHMANNJSON_HEADER_GUARDS
34 #define MOOSE_VTK_UNDEF_NLOHMANNJSON_HEADER_GUARDS 0
35 // Detect if VTK built with external nlohmann
36 #ifdef __has_include
37 #if __has_include("vtk_nlohmannjson.h")
38 #include "vtk_nlohmannjson.h"
39 #if !VTK_MODULE_USE_EXTERNAL_vtknlohmannjson
40 #undef MOOSE_VTK_UNDEF_NLOHMANNJSON_HEADER_GUARDS
41 #define MOOSE_VTK_UNDEF_NLOHMANNJSON_HEADER_GUARDS 1
42 #endif // !VTK_MODULE_USE_EXTERNAL_vtknlohmannjson
43 #endif // __has_include("vtk_nlohmannjson.h")
44 #else // __has_include
45 #error "Could not auto-detect whether VTK built with external nlohmann json. \
46 Define MOOSE_VTK_UNDEF_NLOHMANNJSON_HEADER_GUARDS=1 if built with vendored nlohmann \
47 , otherwise define MOOSE_VTK_UNDEF_NLOHMANNJSON_HEADER_GUARDS=0"
48 #endif // __has_include
49 #endif // MOOSE_VTK_UNDEF_NLOHMANNJSON_HEADER_GUARDS
50 
51 #if MOOSE_VTK_UNDEF_NLOHMANNJSON_HEADER_GUARDS && !defined(MOOSE_VTK_NLOHMANN_INCLUDED)
52 #undef INCLUDE_NLOHMANN_JSON_FWD_HPP_
53 #define MOOSE_VTK_NLOHMANN_INCLUDED
54 #endif
55 
56 #include "vtkSmartPointer.h"
57 #include "vtkPNGReader.h"
58 #include "vtkTIFFReader.h"
59 #include "vtkImageData.h"
60 #include "vtkStringArray.h"
61 #include "vtkImageThreshold.h"
62 #include "vtkImageNormalize.h"
63 #include "vtkImageCast.h"
64 #include "vtkImageShiftScale.h"
65 #include "vtkImageMagnitude.h"
66 #include "vtkImageFlip.h"
67 
68 // If VTK is built without an external nlohmann, then it assumes it
69 // will never be compiled against another nlohmann, and it defines an
70 // nlohmann macro to point to its vtknlohmann copy. In MOOSE their
71 // assumption is wrong.
72 #undef nlohmann
73 
74 #include "libmesh/restore_warnings.h"
75 
76 #endif
77 
78 class MooseMesh;
79 
84 {
85 public:
95 
96  ImageSampler(const InputParameters & parameters);
97 
102  virtual libMesh::Real sample(const libMesh::Point & p) const;
103 
107  virtual void setupImageSampler(MooseMesh & mesh);
108 
109 protected:
113  void vtkShiftAndScale();
114 
118  void vtkThreshold();
119 
125  void vtkMagnitude();
126 
127 private:
128 #ifdef LIBMESH_HAVE_VTK
129 
131  vtkSmartPointer<vtkStringArray> _files;
132 
134  vtkImageData * _data;
135 
137  vtkAlgorithmOutput * _algorithm;
138 
140  vtkSmartPointer<vtkImageReader2> _image;
141 
143  vtkSmartPointer<vtkImageThreshold> _image_threshold;
144 
146  vtkSmartPointer<vtkImageShiftScale> _shift_scale_filter;
147 
149  vtkSmartPointer<vtkImageMagnitude> _magnitude_filter;
150 #endif
151 
154 
156  std::vector<int> _dims;
157 
160 
162  std::vector<double> _voxel;
163 
165 #ifdef LIBMESH_HAVE_VTK
166  unsigned int _component;
167 #endif
168 
171 
174 
177 
179  std::array<bool, 3> _flip;
180 };
libMesh::Point _physical_dims
Physical dimensions of image.
Definition: ImageSampler.h:159
A helper class for re-directing output streams to Console output objects form MooseObjects.
Definition: ConsoleStream.h:30
unsigned int _component
Component to extract.
Definition: ImageSampler.h:166
vtkAlgorithmOutput * _algorithm
VTK-6 seems to work better in terms of "algorithm outputs" rather than vtkImageData pointers...
Definition: ImageSampler.h:137
virtual void setupImageSampler(MooseMesh &mesh)
Perform initialization of image data.
Definition: ImageSampler.C:80
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
vtkSmartPointer< vtkImageReader2 > _image
Complete image data.
Definition: ImageSampler.h:140
ConsoleStream _is_console
Create a console stream object for this helper class.
Definition: ImageSampler.h:176
libMesh::BoundingBox _bounding_box
Bounding box for testing points.
Definition: ImageSampler.h:170
vtkImageData * _data
Complete image data.
Definition: ImageSampler.h:134
std::vector< double > _voxel
Physical pixel size.
Definition: ImageSampler.h:162
vtkSmartPointer< vtkImageMagnitude > _magnitude_filter
Pointer to the magnitude filter.
Definition: ImageSampler.h:149
void vtkShiftAndScale()
Apply image re-scaling using the vtkImageShiftAndRescale object.
Definition: ImageSampler.C:274
vtkSmartPointer< vtkImageShiftScale > _shift_scale_filter
Pointer to the shift and scaling filter.
Definition: ImageSampler.h:146
const InputParameters & _is_pars
Parameters for interface.
Definition: ImageSampler.h:173
MooseMesh wraps a libMesh::Mesh object and enhances its capabilities by caching additional data and s...
Definition: MooseMesh.h:88
static InputParameters validParams()
Constructor.
Definition: ImageSampler.C:20
ImageSampler(const InputParameters &parameters)
Definition: ImageSampler.C:58
void vtkThreshold()
Perform thresholding.
Definition: ImageSampler.C:301
vtkSmartPointer< vtkStringArray > _files
List of file names to extract data.
Definition: ImageSampler.h:131
libMesh::Point _origin
Origin of image.
Definition: ImageSampler.h:153
A helper class for reading and sampling images using VTK.
Definition: ImageSampler.h:83
To be called in the validParams functions of classes that need to operate on ranges of files...
std::vector< int > _dims
Pixel dimension of image.
Definition: ImageSampler.h:156
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
void vtkMagnitude()
Convert the image to greyscale.
Definition: ImageSampler.C:255
vtkSmartPointer< vtkImageThreshold > _image_threshold
Pointer to thresholding filter.
Definition: ImageSampler.h:143
virtual libMesh::Real sample(const libMesh::Point &p) const
Return the pixel value for the given point.
Definition: ImageSampler.C:218
std::array< bool, 3 > _flip
image flip
Definition: ImageSampler.h:179