https://mooseframework.inl.gov
ImageSubdomainGenerator.C
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 
11 #include "CastUniquePointer.h"
12 
13 #include "libmesh/elem.h"
14 
15 // provides round, not std::round (see http://www.cplusplus.com/reference/cmath/round/)
16 #include <cmath>
17 
19 
22 {
24  params.addClassDescription("Samples an image at the coordinates of each element centroid, using "
25  "the resulting pixel color value as each element's subdomain ID");
27 
28  params.addRequiredParam<MeshGeneratorName>("input", "The mesh we want to modify");
29 
30  return params;
31 }
32 
34  : MeshGenerator(parameters), MeshBaseImageSampler(parameters), _input(getMesh("input"))
35 {
36 }
37 
38 std::unique_ptr<MeshBase>
40 {
41  std::unique_ptr<MeshBase> mesh = std::move(_input);
42 
43  // Initialize the ImageSampler
45 
46  // Loop over the elements and sample the image at the element centroid and use the value for the
47  // subdomain id
48  for (auto & elem : mesh->active_element_ptr_range())
49  {
50  subdomain_id_type id = static_cast<subdomain_id_type>(round(sample(elem->vertex_average())));
51  elem->subdomain_id() = id;
52  }
53 
54  return dynamic_pointer_cast<MeshBase>(mesh);
55 }
virtual libMesh::Real sample(const libMesh::Point &p)
Return the pixel value for the given point.
MeshBase & mesh
The main MOOSE class responsible for handling user-defined parameters in almost every MOOSE system...
virtual void setupImageSampler(libMesh::MeshBase &mesh)
Perform initialization of image data.
std::unique_ptr< T_DEST, T_DELETER > dynamic_pointer_cast(std::unique_ptr< T_SRC, T_DELETER > &src)
These are reworked from https://stackoverflow.com/a/11003103.
registerMooseObject("MooseApp", ImageSubdomainGenerator)
static InputParameters validParams()
void addRequiredParam(const std::string &name, const std::string &doc_string)
This method adds a parameter and documentation string to the InputParameters object that will be extr...
static InputParameters validParams()
Constructor.
T round(T x)
Definition: MathUtils.h:77
ImageSubdomainGenerator(const InputParameters &parameters)
static InputParameters validParams()
Definition: MeshGenerator.C:23
A helper class for reading and sampling images using VTK, but with a Meshbase object.
void addClassDescription(const std::string &doc_string)
This method adds a description of the class that will be displayed in the input file syntax dump...
std::unique_ptr< MeshBase > & _input
the mesh to modify
MeshGenerator for defining a subdomain based on image data.
MeshGenerators are objects that can modify or add to an existing mesh.
Definition: MeshGenerator.h:32
std::unique_ptr< MeshBase > generate() override
Generate / modify the mesh.