1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup bke
22  */
23 
24 #include "multires_reshape.h"
25 
26 #include <string.h>
27 
28 #include "BLI_utildefines.h"
29 
30 #include "BKE_ccg.h"
31 #include "BKE_subdiv_ccg.h"
32 
multires_reshape_assign_final_coords_from_ccg(const MultiresReshapeContext * reshape_context,struct SubdivCCG * subdiv_ccg)33 bool multires_reshape_assign_final_coords_from_ccg(const MultiresReshapeContext *reshape_context,
34                                                    struct SubdivCCG *subdiv_ccg)
35 {
36   CCGKey reshape_level_key;
37   BKE_subdiv_ccg_key(&reshape_level_key, subdiv_ccg, reshape_context->reshape.level);
38 
39   const int reshape_grid_size = reshape_context->reshape.grid_size;
40   const float reshape_grid_size_1_inv = 1.0f / (((float)reshape_grid_size) - 1.0f);
41 
42   int num_grids = subdiv_ccg->num_grids;
43   for (int grid_index = 0; grid_index < num_grids; ++grid_index) {
44     CCGElem *ccg_grid = subdiv_ccg->grids[grid_index];
45     for (int y = 0; y < reshape_grid_size; ++y) {
46       const float v = (float)y * reshape_grid_size_1_inv;
47       for (int x = 0; x < reshape_grid_size; ++x) {
48         const float u = (float)x * reshape_grid_size_1_inv;
49 
50         GridCoord grid_coord;
51         grid_coord.grid_index = grid_index;
52         grid_coord.u = u;
53         grid_coord.v = v;
54 
55         ReshapeGridElement grid_element = multires_reshape_grid_element_for_grid_coord(
56             reshape_context, &grid_coord);
57 
58         BLI_assert(grid_element.displacement != NULL);
59         memcpy(grid_element.displacement,
60                CCG_grid_elem_co(&reshape_level_key, ccg_grid, x, y),
61                sizeof(float[3]));
62 
63         if (reshape_level_key.has_mask) {
64           BLI_assert(grid_element.mask != NULL);
65           *grid_element.mask = *CCG_grid_elem_mask(&reshape_level_key, ccg_grid, x, y);
66         }
67       }
68     }
69   }
70 
71   return true;
72 }
73