@@ -418,6 +418,48 @@ void Cell::refine_func(node_map_t& nodes, function test_func, double *xs, double
418418 }
419419}
420420
421+ void Cell::refine_image (node_map_t & nodes, double * image, int_t *shape_cells, double *xs, double *ys, double *zs, bool diagonal_balance){
422+ // early exit if my level is higher than or equal to target
423+ if (level == max_level){
424+ return ;
425+ }
426+ int_t start_ix = points[0 ].location_ind [0 ]/2 ;
427+ int_t start_iy = points[0 ].location_ind [1 ]/2 ;
428+ int_t start_iz = n_dim == 2 ? 0 : points[0 ].location_ind [2 ]/2 ;
429+ int_t nx = shape_cells[0 ]
430+ int_t ny = shape_cells[1 ]
431+ int_t nz = shape_cells[2 ]
432+
433+ // same as 2**(max_level - level), but quicker since power of 2 and integers
434+ int_t span = 1 <<(max_level - level);
435+ int_t span_z = n_dim == 2 ? 1 : span;
436+ int_t i_image = (nz * ny) * start_ix + nz * start_iy + start_iz;
437+ double val_start = image[0 ];
438+ bool subdivide = false ;
439+
440+ // if any of the image data contained in the cell are different, subdivide myself
441+ for (int_t ix=0 ; ix<span && !subdivide; ++ix){
442+ for (int_t iy=0 ; iy<span && !subdivide; ++iy){
443+ for (int_t iz=0 ; iz<span_z && !subdivide; ++iz){
444+ subdivide = image[i_image] != val_start;
445+ i_image += 1 ;
446+ }
447+ i_image += nz;
448+ }
449+ i_image += nz * ny;
450+ }
451+ if (subdivide){
452+ if (is_leaf ()){
453+ divide (nodes, xs, ys, zs, true , diag_balance);
454+ }
455+ // recurse into children
456+ for (int_t i = 0 ; i < (1 <<n_dim); ++i){
457+ children[i]->refine_image (nodes, geom, p_level, xs, ys, zs, diag_balance);
458+ }
459+ }
460+
461+ }
462+
421463void Cell::divide (node_map_t & nodes, double * xs, double * ys, double * zs, bool balance, bool diag_balance){
422464 // Gaurd against dividing a cell that is already at the max level
423465 if (level == max_level){
@@ -896,6 +938,18 @@ void Tree::refine_function(function test_func, bool diagonal_balance){
896938 roots[iz][iy][ix]->refine_func (nodes, test_func, xs, ys, zs, diagonal_balance);
897939};
898940
941+ void Tree:refine_image(double *image, bool diagonal_balance){
942+ int_t [3 ] shape_cells;
943+ shape_cells[0 ] = nx/2 ;
944+ shape_cells[1 ] = ny/2 ;
945+ shape_cells[2 ] = nz/2 ;
946+ for (int_t iz=0 ; iz<nz_roots; ++iz)
947+ for (int_t iy=0 ; iy<ny_roots; ++iy)
948+ for (int_t ix=0 ; ix<nx_roots; ++ix)
949+ roots[iz][iy][ix]->refine_image (nodes, image, xs, ys, zs, diagonal_balance);
950+ }
951+
952+
899953void Tree::finalize_lists (){
900954 for (int_t iz=0 ; iz<nz_roots; ++iz)
901955 for (int_t iy=0 ; iy<ny_roots; ++iy)
0 commit comments