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 "MEM_guardedalloc.h"
25 
26 #include "DNA_mesh_types.h"
27 #include "DNA_meshdata_types.h"
28 #include "DNA_modifier_types.h"
29 #include "DNA_scene_types.h"
30 
31 #include "BKE_customdata.h"
32 #include "BKE_lib_id.h"
33 #include "BKE_mesh.h"
34 #include "BKE_mesh_runtime.h"
35 #include "BKE_modifier.h"
36 #include "BKE_multires.h"
37 #include "BKE_subdiv.h"
38 #include "BKE_subsurf.h"
39 #include "BLI_math_vector.h"
40 
41 #include "DEG_depsgraph_query.h"
42 
43 #include "multires_reshape.h"
44 
45 /* -------------------------------------------------------------------- */
46 /** \name Reshape from object
47  * \{ */
48 
multiresModifier_reshapeFromVertcos(struct Depsgraph * depsgraph,struct Object * object,struct MultiresModifierData * mmd,const float (* vert_coords)[3],const int num_vert_coords)49 bool multiresModifier_reshapeFromVertcos(struct Depsgraph *depsgraph,
50                                          struct Object *object,
51                                          struct MultiresModifierData *mmd,
52                                          const float (*vert_coords)[3],
53                                          const int num_vert_coords)
54 {
55   MultiresReshapeContext reshape_context;
56   if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
57     return false;
58   }
59   multires_reshape_store_original_grids(&reshape_context);
60   multires_reshape_ensure_grids(object->data, reshape_context.top.level);
61   if (!multires_reshape_assign_final_coords_from_vertcos(
62           &reshape_context, vert_coords, num_vert_coords)) {
63     multires_reshape_context_free(&reshape_context);
64     return false;
65   }
66   multires_reshape_smooth_object_grids_with_details(&reshape_context);
67   multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
68   multires_reshape_context_free(&reshape_context);
69   return true;
70 }
71 
72 /* Returns truth on success, false otherwise.
73  *
74  * This function might fail in cases like source and destination not having
75  * matched amount of vertices. */
multiresModifier_reshapeFromObject(struct Depsgraph * depsgraph,struct MultiresModifierData * mmd,struct Object * dst,struct Object * src)76 bool multiresModifier_reshapeFromObject(struct Depsgraph *depsgraph,
77                                         struct MultiresModifierData *mmd,
78                                         struct Object *dst,
79                                         struct Object *src)
80 {
81   struct Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
82   struct Object *src_eval = DEG_get_evaluated_object(depsgraph, src);
83   Mesh *src_mesh_eval = mesh_get_eval_final(depsgraph, scene_eval, src_eval, &CD_MASK_BAREMESH);
84 
85   int num_deformed_verts;
86   float(*deformed_verts)[3] = BKE_mesh_vert_coords_alloc(src_mesh_eval, &num_deformed_verts);
87 
88   const bool result = multiresModifier_reshapeFromVertcos(
89       depsgraph, dst, mmd, deformed_verts, num_deformed_verts);
90 
91   MEM_freeN(deformed_verts);
92 
93   return result;
94 }
95 
96 /** \} */
97 
98 /* -------------------------------------------------------------------- */
99 /** \name Reshape from modifier
100  * \{ */
101 
multiresModifier_reshapeFromDeformModifier(struct Depsgraph * depsgraph,struct Object * object,struct MultiresModifierData * mmd,struct ModifierData * deform_md)102 bool multiresModifier_reshapeFromDeformModifier(struct Depsgraph *depsgraph,
103                                                 struct Object *object,
104                                                 struct MultiresModifierData *mmd,
105                                                 struct ModifierData *deform_md)
106 {
107   MultiresModifierData highest_mmd = *mmd;
108   highest_mmd.sculptlvl = highest_mmd.totlvl;
109   highest_mmd.lvl = highest_mmd.totlvl;
110   highest_mmd.renderlvl = highest_mmd.totlvl;
111 
112   /* Create mesh for the multires, ignoring any further modifiers (leading
113    * deformation modifiers will be applied though). */
114   Mesh *multires_mesh = BKE_multires_create_mesh(depsgraph, object, &highest_mmd);
115   int num_deformed_verts;
116   float(*deformed_verts)[3] = BKE_mesh_vert_coords_alloc(multires_mesh, &num_deformed_verts);
117 
118   /* Apply deformation modifier on the multires, */
119   const ModifierEvalContext modifier_ctx = {
120       .depsgraph = depsgraph,
121       .object = object,
122       .flag = MOD_APPLY_USECACHE | MOD_APPLY_IGNORE_SIMPLIFY,
123   };
124   BKE_modifier_deform_verts(
125       deform_md, &modifier_ctx, multires_mesh, deformed_verts, multires_mesh->totvert);
126   BKE_id_free(NULL, multires_mesh);
127 
128   /* Reshaping */
129   bool result = multiresModifier_reshapeFromVertcos(
130       depsgraph, object, &highest_mmd, deformed_verts, num_deformed_verts);
131 
132   /* Cleanup */
133   MEM_freeN(deformed_verts);
134 
135   return result;
136 }
137 
138 /** \} */
139 
140 /* -------------------------------------------------------------------- */
141 /** \name Reshape from grids
142  * \{ */
143 
multiresModifier_reshapeFromCCG(const int tot_level,Mesh * coarse_mesh,struct SubdivCCG * subdiv_ccg)144 bool multiresModifier_reshapeFromCCG(const int tot_level,
145                                      Mesh *coarse_mesh,
146                                      struct SubdivCCG *subdiv_ccg)
147 {
148   MultiresReshapeContext reshape_context;
149   if (!multires_reshape_context_create_from_ccg(
150           &reshape_context, subdiv_ccg, coarse_mesh, tot_level)) {
151     return false;
152   }
153 
154   multires_ensure_external_read(coarse_mesh, reshape_context.top.level);
155 
156   multires_reshape_store_original_grids(&reshape_context);
157   multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
158   if (!multires_reshape_assign_final_coords_from_ccg(&reshape_context, subdiv_ccg)) {
159     multires_reshape_context_free(&reshape_context);
160     return false;
161   }
162   multires_reshape_smooth_object_grids_with_details(&reshape_context);
163   multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
164   multires_reshape_context_free(&reshape_context);
165   return true;
166 }
167 
168 /** \} */
169 
170 /* -------------------------------------------------------------------- */
171 /** \name Subdivision
172  * \{ */
173 
multiresModifier_subdivide(Object * object,MultiresModifierData * mmd,const eMultiresSubdivideModeType mode)174 void multiresModifier_subdivide(Object *object,
175                                 MultiresModifierData *mmd,
176                                 const eMultiresSubdivideModeType mode)
177 {
178   const int top_level = mmd->totlvl + 1;
179   multiresModifier_subdivide_to_level(object, mmd, top_level, mode);
180 }
181 
multiresModifier_subdivide_to_level(struct Object * object,struct MultiresModifierData * mmd,const int top_level,const eMultiresSubdivideModeType mode)182 void multiresModifier_subdivide_to_level(struct Object *object,
183                                          struct MultiresModifierData *mmd,
184                                          const int top_level,
185                                          const eMultiresSubdivideModeType mode)
186 {
187   if (top_level <= mmd->totlvl) {
188     return;
189   }
190 
191   Mesh *coarse_mesh = object->data;
192   if (coarse_mesh->totloop == 0) {
193     /* If there are no loops in the mesh implies there is no CD_MDISPS as well. So can early output
194      * from here as there is nothing to subdivide. */
195     return;
196   }
197 
198   MultiresReshapeContext reshape_context;
199 
200   /* There was no multires at all, all displacement is at 0. Can simply make sure all mdisps grids
201    * are allocated at a proper level and return. */
202   const bool has_mdisps = CustomData_has_layer(&coarse_mesh->ldata, CD_MDISPS);
203   if (!has_mdisps) {
204     CustomData_add_layer(&coarse_mesh->ldata, CD_MDISPS, CD_CALLOC, NULL, coarse_mesh->totloop);
205   }
206 
207   /* NOTE: Subdivision happens from the top level of the existing multires modifier. If it is set
208    * to 0 and there is mdisps layer it would mean that the modifier went out of sync with the data.
209    * This happens when, for example, linking modifiers from one object to another.
210    *
211    * In such cases simply ensure grids to be the proper level.
212    *
213    * If something smarter is needed it is up to the operators which does data synchronization, so
214    * that the mdisps layer is also synchronized. */
215   if (!has_mdisps || top_level == 1 || mmd->totlvl == 0) {
216     multires_reshape_ensure_grids(coarse_mesh, top_level);
217     if (ELEM(mode, MULTIRES_SUBDIVIDE_LINEAR, MULTIRES_SUBDIVIDE_SIMPLE)) {
218       multires_subdivide_create_tangent_displacement_linear_grids(object, mmd);
219     }
220     else {
221       multires_set_tot_level(object, mmd, top_level);
222     }
223     return;
224   }
225 
226   multires_flush_sculpt_updates(object);
227 
228   if (!multires_reshape_context_create_from_subdivide(&reshape_context, object, mmd, top_level)) {
229     return;
230   }
231 
232   multires_reshape_store_original_grids(&reshape_context);
233   multires_reshape_ensure_grids(coarse_mesh, reshape_context.top.level);
234   multires_reshape_assign_final_elements_from_orig_mdisps(&reshape_context);
235 
236   /* Free original grids which makes it so smoothing with details thinks all the details were
237    * added against base mesh's limit surface. This is similar behavior to as if we've done all
238    * displacement in sculpt mode at the old top level and then propagated to the new top level.*/
239   multires_reshape_free_original_grids(&reshape_context);
240 
241   if (ELEM(mode, MULTIRES_SUBDIVIDE_LINEAR, MULTIRES_SUBDIVIDE_SIMPLE)) {
242     multires_reshape_smooth_object_grids(&reshape_context, mode);
243   }
244   else {
245     multires_reshape_smooth_object_grids_with_details(&reshape_context);
246   }
247 
248   multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
249   multires_reshape_context_free(&reshape_context);
250 
251   multires_set_tot_level(object, mmd, top_level);
252 }
253 
254 /** \} */
255 
256 /* -------------------------------------------------------------------- */
257 /** \name Apply base
258  * \{ */
259 
multiresModifier_base_apply(struct Depsgraph * depsgraph,Object * object,MultiresModifierData * mmd)260 void multiresModifier_base_apply(struct Depsgraph *depsgraph,
261                                  Object *object,
262                                  MultiresModifierData *mmd)
263 {
264   multires_force_sculpt_rebuild(object);
265 
266   MultiresReshapeContext reshape_context;
267   if (!multires_reshape_context_create_from_object(&reshape_context, depsgraph, object, mmd)) {
268     return;
269   }
270 
271   multires_reshape_store_original_grids(&reshape_context);
272 
273   /* At this point base_mesh is object's mesh, the subdiv is initialized to the deformed state of
274    * the base mesh.
275    * Store coordinates of top level grids in object space which will define true shape we would
276    * want to reshape to after modifying the base mesh. */
277   multires_reshape_assign_final_coords_from_mdisps(&reshape_context);
278 
279   /* For modifying base mesh we only want to consider deformation caused by multires displacement
280    * and ignore all deformation which might be caused by deformation modifiers leading the multires
281    * one.
282    * So refine the subdiv to the original mesh vertices positions, which will also need to make
283    * it so object space displacement is re-evaluated for them (as in, can not re-use any knowledge
284    * from the final coordinates in the object space ). */
285   multires_reshape_apply_base_refine_from_base(&reshape_context);
286 
287   /* Modify original mesh coordinates. This happens in two steps:
288    * - Coordinates are set to their final location, where they are intended to be in the final
289    *   result.
290    * - Heuristic moves them a bit, kind of canceling out the effect of subsurf (so then when
291    *   multires modifier applies subsurf vertices are placed at the desired location). */
292   multires_reshape_apply_base_update_mesh_coords(&reshape_context);
293   multires_reshape_apply_base_refit_base_mesh(&reshape_context);
294 
295   /* Reshape to the stored final state.
296    * Not that the base changed, so the subdiv is to be refined to the new positions. Unfortunately,
297    * this can not be done foe entirely cheap: if there were deformation modifiers prior to the
298    * multires they need to be re-evaluated for the new base mesh. */
299   multires_reshape_apply_base_refine_from_deform(&reshape_context);
300   multires_reshape_object_grids_to_tangent_displacement(&reshape_context);
301 
302   multires_reshape_context_free(&reshape_context);
303 }
304 
305 /** \} */
306