Пример #1
0
ImageMesh::ImageMesh(const std::string & name, InputParameters parameters) :
    GeneratedMesh(name, parameters),
    _scale_to_one(getParam<bool>("scale_to_one")),
    _cells_per_pixel(getParam<Real>("cells_per_pixel"))
{
  // Set up the parameters associated with file ranges
  int status = parseFileRange(_pars);

  // Failure is not an option
  if (status != 0)
    mooseError(getFileRangeErrorMessage(status));
}
Пример #2
0
ImageMesh::ImageMesh(const InputParameters & parameters) :
    GeneratedMesh(parameters),
    _scale_to_one(getParam<bool>("scale_to_one")),
    _cells_per_pixel(getParam<Real>("cells_per_pixel"))
{
  // Set up the parameters associated with file ranges (this needs to be moved to validParams, if possible)
  int status = parseFileRange(_app.getInputParameterWarehouse().getInputParameters(name()));

  // Failure is not an option
  if (status != 0)
    mooseError(getFileRangeErrorMessage(status));
}
Пример #3
0
void
ImageFunction::initialSetup()
{
#ifdef LIBMESH_HAVE_VTK
  // Get access to the Mesh object
  FEProblem * fe_problem = getParam<FEProblem *>("_fe_problem");
  MooseMesh & mesh = fe_problem->mesh();

  // Set the dimensions from the Mesh if not set by the User
  if (isParamValid("dimensions"))
    _physical_dims = getParam<Point>("dimensions");

  else
  {
    _physical_dims(0) = mesh.getParam<Real>("xmax") - mesh.getParam<Real>("xmin");
#if LIBMESH_DIM > 1
    _physical_dims(1) = mesh.getParam<Real>("ymax") - mesh.getParam<Real>("ymin");
#endif
#if LIBMESH_DIM > 2
    _physical_dims(2) = mesh.getParam<Real>("zmax") - mesh.getParam<Real>("zmin");
#endif
  }

  // Set the origin from the Mesh if not set in the input file
  if (isParamValid("origin"))
    _origin = getParam<Point>("origin");
  else
  {
    _origin(0) = mesh.getParam<Real>("xmin");
#if LIBMESH_DIM > 1
    _origin(1) = mesh.getParam<Real>("ymin");
#endif
#if LIBMESH_DIM > 2
    _origin(2) = mesh.getParam<Real>("zmin");
#endif
  }

  // An array of filenames, to be filled in
  std::vector<std::string> filenames;

  // The file suffix, to be determined
  std::string file_suffix;

  // Try to parse our own file range parameters.  If that fails, then
  // see if the associated Mesh is an ImageMesh and use its.  If that
  // also fails, then we have to throw an error...
  //
  // The parseFileRange method sets parameters, thus a writable reference to the InputParameters
  // object must be obtained from the warehouse. Generally, this should be avoided, but
  // this is a special case.
  int status = parseFileRange(_app.getInputParameterWarehouse().getInputParameters(_name));

  if (status != 0)
  {
    // We don't have parameters, so see if we can get them from ImageMesh
    ImageMesh * image_mesh = dynamic_cast<ImageMesh*>(&mesh);
    if (!image_mesh)
      mooseError("No file range parameters were provided and the Mesh is not an ImageMesh.");

    // Get the ImageMesh's parameters.  This should work, otherwise
    // errors would already have been thrown...
    const InputParameters & im_params = image_mesh->parameters();
    filenames = im_params.get<std::vector<std::string> >("filenames");
    file_suffix = im_params.get<std::string>("file_suffix");
  }
  else
  {
    // Use our own parameters
    filenames = getParam<std::vector<std::string> >("filenames");
    file_suffix = getParam<std::string>("file_suffix");
  }

  // Storage for the file names
  _files = vtkSmartPointer<vtkStringArray>::New();

  for (unsigned i=0; i<filenames.size(); ++i)
    _files->InsertNextValue(filenames[i]);

  // Error if no files where located
  if (_files->GetNumberOfValues() == 0)
    mooseError("No image file(s) located");


  // Read the image stack.  Hurray for VTK not using polymorphism in a
  // smart way... we actually have to explicitly create the type of
  // reader based on the file extension, using an if-statement...
  if (file_suffix == "png")
    _image = vtkSmartPointer<vtkPNGReader>::New();
  else if (file_suffix == "tiff" || file_suffix == "tif")
    _image = vtkSmartPointer<vtkTIFFReader>::New();
  else
    mooseError("Un-supported file type '" << file_suffix << "'");

  // Now that _image is set up, actually read the images
  // Indicate that data read has started
  _console << "Reading image(s)..." << std::endl;

  // Extract the data
  _image->SetFileNames(_files);
  _image->Update();
  _data = _image->GetOutput();
  _algorithm = _image->GetOutputPort();

  // Set the image dimensions and voxel size member variable
  int * dims = _data->GetDimensions();
  for (unsigned int i = 0; i < 3; ++i)
  {
    _dims.push_back(dims[i]);
    _voxel.push_back(_physical_dims(i)/_dims[i]);
  }

  // Set the dimensions of the image and bounding box
  _data->SetSpacing(_voxel[0], _voxel[1], _voxel[2]);
  _data->SetOrigin(_origin(0), _origin(1), _origin(2));
  _bounding_box.min() = _origin;
  _bounding_box.max() = _origin + _physical_dims;

  // Indicate data read is completed
  _console << "          ...image read finished" << std::endl;

  // Set the component parameter
  // If the parameter is not set then vtkMagnitude() will applied
  if (isParamValid("component"))
  {
    unsigned int n = _data->GetNumberOfScalarComponents();
    _component = getParam<unsigned int>("component");
    if (_component >= n)
      mooseError("'component' parameter must be empty or have a value of 0 to " << n-1);
  }
  else
    _component = 0;

  // Apply filters, the toggling on and off of each filter is handled internally
  vtkMagnitude();
  vtkShiftAndScale();
  vtkThreshold();
  vtkFlip();
#endif
}