OpenFPM  5.2.0
Project that contain the implementation of distributed structures
main.cpp
Go to the documentation of this file.
1 //
2 // Created by jstark on 2020-05-18. Updated on 2022-01-05.
3 //
39 
41 // Include redistancing header files
42 #include "util/PathsAndFiles.hpp"
47 
48 
73 
74 int main(int argc, char* argv[])
75 {
76 // std::string image_name = "circle";
77 // std::string image_name = "triangle";
78 // std::string image_name = "face";
79  std::string image_name = "dolphin";
80 
81  typedef double phi_type;
82  // initialize library
83  openfpm_init(&argc, &argv);
85  // Set current working directory, define output paths and create folders where output will be saved
86  std::string cwd = get_cwd();
87  const std::string path_output = cwd + "/output_" + image_name;
88 // const std::string path_output = cwd + "/output_face";
89 
90  create_directory_if_not_exist(path_output);
91 
93  // Now we set the input paths. We need a binary file with the pixel values and a csv file with the
94  // size of the stack (in #pixels / dimension)
95  const std::string path_input ="input/";
96  const std::string path_to_image = path_input + image_name + ".bin";
97  const std::string path_to_size = path_input + "size_" + image_name + ".csv";
98 
99  /*
100  * in case of 2D (single image):
101  */
102  const unsigned int grid_dim = 2;
103  // some indices
104  const size_t x = 0;
105  const size_t y = 1;
106 
107  const size_t Phi_0_grid = 0;
108  const size_t Phi_SDF_grid = 1;
109 
110  const size_t Phi_SDF_vd = 0;
111  const size_t Phi_grad_vd = 1;
112  const size_t Phi_magnOfGrad_vd = 2;
114 
115 
130  const phi_type refinement [] = {0.5, 0.5}; // (2D) factor by which grid should be finer / coarser as
131  // underlying image in each dimension (e.g. to get isotropic grid from anisotropic image resolution)
134  // read the stack size (number of pixel values per dimension) from a binary file
135  // alternatively, you can directly define the stack size as: std::vector<size_t> stack_size {#pixels in x, #pixels in y}
150  std::vector<int> stack_size = get_size(path_to_size);
152  auto & v_cl = create_vcluster();
153  if (v_cl.rank() == 0)
154  {
155  for(std::vector<int>::size_type i = 0; i != stack_size.size(); i++)
156  {
157  std::cout << "#Pixel in dimension " << i << " = " << stack_size[i] << std::endl;
158  std::cout << "original refinement in dimension " << i << " = " << refinement[i] << std::endl;
159  }
160  }
161 
162  // Array with grid dimensions after refinement. This size-array will be used for the grid creation.
163  const size_t sz[grid_dim] = {(size_t)std::round(stack_size[x] * refinement[x]), (size_t)std::round(stack_size[y] *
164  refinement[y])}; // 2D
166  // Here we create a 2D grid that stores 2 properties:
167  // Prop1: store the initial Phi;
168  // Prop2: here the re-initialized Phi (signed distance function) will be written to in the re-distancing step
182  Box<grid_dim, phi_type> box({0.0, 0.0}, {5.0, 5.0}); // 2D
184 
185  Ghost<grid_dim, long int> ghost(0);
187  typedef grid_dist_id<grid_dim, phi_type, props > grid_in_type;
188  grid_in_type g_dist(sz, box, ghost);
189  g_dist.setPropNames({"Phi_0", "Phi_SDF"});
190 
191  // initialize complete grid including ghost layer with -1
192  init_grid_and_ghost<Phi_0_grid>(g_dist, -1.0);
193 
194  // Now we can initialize the grid with the pixel values from the image stack
195  load_pixel_onto_grid<Phi_0_grid>(g_dist, path_to_image, stack_size);
196  g_dist.write(path_output + "/grid_from_images_initial", FORMAT_BINARY); // Save the initial grid as vtk file
197 
198 
200  // Now we want to convert the initial Phi into a signed distance function (SDF) with magnitude of gradient = 1
201  // For the initial re-distancing we use the Sussman method
202  // 1.) Set some redistancing options (for details see example sussman disk or sphere)
203  Redist_options<phi_type> redist_options;
204  redist_options.min_iter = 1e3;
205  redist_options.max_iter = 1e4;
206 
207  redist_options.convTolChange.value = 1e-7;
208  redist_options.convTolChange.check = true;
209  redist_options.convTolResidual.value = 1e-6; // is ignored if convTolResidual.check = false;
210  redist_options.convTolResidual.check = false;
211 
212  redist_options.interval_check_convergence = 1e3;
213  redist_options.width_NB_in_grid_points = 10;
214  redist_options.print_current_iterChangeResidual = true;
215  redist_options.print_steadyState_iter = true;
216  redist_options.save_temp_grid = true;
217 
218  RedistancingSussman<grid_in_type, phi_type> redist_obj(g_dist, redist_options); // Instantiation of Sussman-redistancing
219  // class
220 // std::cout << "dt = " << redist_obj.get_time_step() << std::endl;
221  // Run the redistancing. in the <> brackets provide property-index where 1.) your initial Phi is stored and 2.) where the resulting SDF should be written to.
222  redist_obj.run_redistancing<Phi_0_grid, Phi_SDF_grid>();
223 
224  g_dist.write(path_output + "/grid_images_post_redistancing", FORMAT_BINARY); // Save the result as vtk file
225 
227  // Get narrow band: Place particles on interface (narrow band width e.g. 2 grid points on each side of the interface)
228  size_t bc[grid_dim] = {NON_PERIODIC, NON_PERIODIC};
229  // Create an empty vector to which narrow-band particles will be added. You can choose, how many properties you want.
230  // Minimum is 1 property, to which the Phi_SDF can be written
231  // In this example we chose 3 properties. The 1st for the Phi_SDF, the 2nd for the gradient of phi and the 3rd for
232  // the magnitude of the gradient
233  typedef aggregate<phi_type, Point<grid_dim, phi_type>, phi_type> props_nb;
235  Ghost<grid_dim, phi_type> ghost_vd(0);
236  vd_type vd_narrow_band(0, box, bc, ghost_vd);
237  vd_narrow_band.setPropNames({"Phi_SDF", "Phi_grad", "Phi_magnOfGrad"});
238 
239  NarrowBand<grid_in_type, phi_type> narrowBand(g_dist, redist_options.width_NB_in_grid_points); // Instantiation of
240  // NarrowBand class
241 
242  // Get the narrow band. You can decide, if you only want the Phi_SDF saved to your particles or
243  // if you also want the gradients or gradients and magnitude of gradient.
244  // The function will know depending on how many property-indices you provide in the <> brackets.
245  // First property-index must always be the index of the SDF on the grid!
246  // E.g.: The following line would only write only the Phi_SDF from the grid to your narrow-band particles
247  // narrowBand.get_narrow_band<Phi_SDF_grid, Phi_SDF_vd>(g_dist, vd_narrow_band);
248  // Whereas this will give you the gradients and magnOfGrad of Phi as well:
249  narrowBand.get_narrow_band<Phi_SDF_grid, Phi_SDF_vd, Phi_grad_vd, Phi_magnOfGrad_vd>(g_dist, vd_narrow_band);
250 
251  vd_narrow_band.write(path_output + "/vd_narrow_band_images", FORMAT_BINARY); // Save particles as vtk file
252 
253  openfpm_finalize(); // Finalize openFPM library
254  return 0;
255 }
257 
Header file containing functions for loading pixel values from 2D image or 3D image stack (volume) st...
std::vector< int > get_size(const std::string &path_to_file)
Read the number of pixels per dimension from a csv-file in order to create a grid with the same size.
Class for getting the narrow band around the interface.
Header file containing functions for creating files and folders.
static void create_directory_if_not_exist(std::string path, bool silent=0)
Creates a directory if not already existent.
static std::string get_cwd()
Gets the current working directory and returns path as string.
Class for reinitializing a level-set function into a signed distance function using Sussman redistanc...
This class represent an N-dimensional box.
Definition: Box.hpp:60
Definition: Ghost.hpp:40
Class for getting the narrow band around the interface.
Definition: NarrowBand.hpp:43
Class for reinitializing a level-set function into a signed distance function using Sussman redistanc...
This is a distributed grid.
Distributed vector.
Structure to bundle options for redistancing.
Definition: particle_cp.hpp:33
aggregate of properties, from a list of object if create a struct that follow the OPENFPM native stru...
Definition: aggregate.hpp:221