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) 2005 Blender Foundation.
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup bke
22  */
23 
24 #include "atomic_ops.h"
25 
26 #include "MEM_guardedalloc.h"
27 
28 #include "DNA_mesh_types.h"
29 #include "DNA_meshdata_types.h"
30 #include "DNA_object_types.h"
31 
32 #include "BLI_math_geom.h"
33 #include "BLI_threads.h"
34 
35 #include "BKE_bvhutils.h"
36 #include "BKE_lib_id.h"
37 #include "BKE_mesh.h"
38 #include "BKE_mesh_runtime.h"
39 #include "BKE_shrinkwrap.h"
40 #include "BKE_subdiv_ccg.h"
41 
42 /* -------------------------------------------------------------------- */
43 /** \name Mesh Runtime Struct Utils
44  * \{ */
45 
46 /**
47  * Default values defined at read time.
48  */
BKE_mesh_runtime_reset(Mesh * mesh)49 void BKE_mesh_runtime_reset(Mesh *mesh)
50 {
51   memset(&mesh->runtime, 0, sizeof(mesh->runtime));
52   mesh->runtime.eval_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime eval_mutex");
53   BLI_mutex_init(mesh->runtime.eval_mutex);
54   mesh->runtime.bvh_cache = NULL;
55 }
56 
57 /* Clear all pointers which we don't want to be shared on copying the datablock.
58  * However, keep all the flags which defines what the mesh is (for example, that
59  * it's deformed only, or that its custom data layers are out of date.) */
BKE_mesh_runtime_reset_on_copy(Mesh * mesh,const int UNUSED (flag))60 void BKE_mesh_runtime_reset_on_copy(Mesh *mesh, const int UNUSED(flag))
61 {
62   Mesh_Runtime *runtime = &mesh->runtime;
63 
64   runtime->mesh_eval = NULL;
65   runtime->edit_data = NULL;
66   runtime->batch_cache = NULL;
67   runtime->subdiv_ccg = NULL;
68   memset(&runtime->looptris, 0, sizeof(runtime->looptris));
69   runtime->bvh_cache = NULL;
70   runtime->shrinkwrap_data = NULL;
71 
72   mesh->runtime.eval_mutex = MEM_mallocN(sizeof(ThreadMutex), "mesh runtime eval_mutex");
73   BLI_mutex_init(mesh->runtime.eval_mutex);
74 }
75 
BKE_mesh_runtime_clear_cache(Mesh * mesh)76 void BKE_mesh_runtime_clear_cache(Mesh *mesh)
77 {
78   if (mesh->runtime.eval_mutex != NULL) {
79     BLI_mutex_end(mesh->runtime.eval_mutex);
80     MEM_freeN(mesh->runtime.eval_mutex);
81     mesh->runtime.eval_mutex = NULL;
82   }
83   if (mesh->runtime.mesh_eval != NULL) {
84     mesh->runtime.mesh_eval->edit_mesh = NULL;
85     BKE_id_free(NULL, mesh->runtime.mesh_eval);
86     mesh->runtime.mesh_eval = NULL;
87   }
88   BKE_mesh_runtime_clear_geometry(mesh);
89   BKE_mesh_batch_cache_free(mesh);
90   BKE_mesh_runtime_clear_edit_data(mesh);
91 }
92 
93 /* This is a ported copy of DM_ensure_looptri_data(dm) */
94 /**
95  * Ensure the array is large enough
96  *
97  * \note This function must always be thread-protected by caller.
98  * It should only be used by internal code.
99  */
mesh_ensure_looptri_data(Mesh * mesh)100 static void mesh_ensure_looptri_data(Mesh *mesh)
101 {
102   const unsigned int totpoly = mesh->totpoly;
103   const int looptris_len = poly_to_tri_count(totpoly, mesh->totloop);
104 
105   BLI_assert(mesh->runtime.looptris.array_wip == NULL);
106 
107   SWAP(MLoopTri *, mesh->runtime.looptris.array, mesh->runtime.looptris.array_wip);
108 
109   if ((looptris_len > mesh->runtime.looptris.len_alloc) ||
110       (looptris_len < mesh->runtime.looptris.len_alloc * 2) || (totpoly == 0)) {
111     MEM_SAFE_FREE(mesh->runtime.looptris.array_wip);
112     mesh->runtime.looptris.len_alloc = 0;
113     mesh->runtime.looptris.len = 0;
114   }
115 
116   if (totpoly) {
117     if (mesh->runtime.looptris.array_wip == NULL) {
118       mesh->runtime.looptris.array_wip = MEM_malloc_arrayN(
119           looptris_len, sizeof(*mesh->runtime.looptris.array_wip), __func__);
120       mesh->runtime.looptris.len_alloc = looptris_len;
121     }
122 
123     mesh->runtime.looptris.len = looptris_len;
124   }
125 }
126 
127 /* This is a ported copy of CDDM_recalc_looptri(dm). */
BKE_mesh_runtime_looptri_recalc(Mesh * mesh)128 void BKE_mesh_runtime_looptri_recalc(Mesh *mesh)
129 {
130   mesh_ensure_looptri_data(mesh);
131   BLI_assert(mesh->totpoly == 0 || mesh->runtime.looptris.array_wip != NULL);
132 
133   BKE_mesh_recalc_looptri(mesh->mloop,
134                           mesh->mpoly,
135                           mesh->mvert,
136                           mesh->totloop,
137                           mesh->totpoly,
138                           mesh->runtime.looptris.array_wip);
139 
140   BLI_assert(mesh->runtime.looptris.array == NULL);
141   atomic_cas_ptr((void **)&mesh->runtime.looptris.array,
142                  mesh->runtime.looptris.array,
143                  mesh->runtime.looptris.array_wip);
144   mesh->runtime.looptris.array_wip = NULL;
145 }
146 
147 /* This is a ported copy of dm_getNumLoopTri(dm). */
BKE_mesh_runtime_looptri_len(const Mesh * mesh)148 int BKE_mesh_runtime_looptri_len(const Mesh *mesh)
149 {
150   const int looptri_len = poly_to_tri_count(mesh->totpoly, mesh->totloop);
151   BLI_assert(ELEM(mesh->runtime.looptris.len, 0, looptri_len));
152   return looptri_len;
153 }
154 
155 /* This is a ported copy of dm_getLoopTriArray(dm). */
BKE_mesh_runtime_looptri_ensure(Mesh * mesh)156 const MLoopTri *BKE_mesh_runtime_looptri_ensure(Mesh *mesh)
157 {
158   MLoopTri *looptri;
159 
160   ThreadMutex *mesh_eval_mutex = (ThreadMutex *)mesh->runtime.eval_mutex;
161   BLI_mutex_lock(mesh_eval_mutex);
162 
163   looptri = mesh->runtime.looptris.array;
164 
165   if (looptri != NULL) {
166     BLI_assert(BKE_mesh_runtime_looptri_len(mesh) == mesh->runtime.looptris.len);
167   }
168   else {
169     BKE_mesh_runtime_looptri_recalc(mesh);
170     looptri = mesh->runtime.looptris.array;
171   }
172 
173   BLI_mutex_unlock(mesh_eval_mutex);
174 
175   return looptri;
176 }
177 
178 /* This is a copy of DM_verttri_from_looptri(). */
BKE_mesh_runtime_verttri_from_looptri(MVertTri * r_verttri,const MLoop * mloop,const MLoopTri * looptri,int looptri_num)179 void BKE_mesh_runtime_verttri_from_looptri(MVertTri *r_verttri,
180                                            const MLoop *mloop,
181                                            const MLoopTri *looptri,
182                                            int looptri_num)
183 {
184   int i;
185   for (i = 0; i < looptri_num; i++) {
186     r_verttri[i].tri[0] = mloop[looptri[i].tri[0]].v;
187     r_verttri[i].tri[1] = mloop[looptri[i].tri[1]].v;
188     r_verttri[i].tri[2] = mloop[looptri[i].tri[2]].v;
189   }
190 }
191 
BKE_mesh_runtime_ensure_edit_data(struct Mesh * mesh)192 bool BKE_mesh_runtime_ensure_edit_data(struct Mesh *mesh)
193 {
194   if (mesh->runtime.edit_data != NULL) {
195     return false;
196   }
197 
198   mesh->runtime.edit_data = MEM_callocN(sizeof(EditMeshData), "EditMeshData");
199   return true;
200 }
201 
BKE_mesh_runtime_reset_edit_data(Mesh * mesh)202 bool BKE_mesh_runtime_reset_edit_data(Mesh *mesh)
203 {
204   EditMeshData *edit_data = mesh->runtime.edit_data;
205   if (edit_data == NULL) {
206     return false;
207   }
208 
209   MEM_SAFE_FREE(edit_data->polyCos);
210   MEM_SAFE_FREE(edit_data->polyNos);
211   MEM_SAFE_FREE(edit_data->vertexCos);
212   MEM_SAFE_FREE(edit_data->vertexNos);
213 
214   return true;
215 }
216 
BKE_mesh_runtime_clear_edit_data(Mesh * mesh)217 bool BKE_mesh_runtime_clear_edit_data(Mesh *mesh)
218 {
219   if (mesh->runtime.edit_data == NULL) {
220     return false;
221   }
222   BKE_mesh_runtime_reset_edit_data(mesh);
223 
224   MEM_freeN(mesh->runtime.edit_data);
225   mesh->runtime.edit_data = NULL;
226 
227   return true;
228 }
229 
BKE_mesh_runtime_clear_geometry(Mesh * mesh)230 void BKE_mesh_runtime_clear_geometry(Mesh *mesh)
231 {
232   if (mesh->runtime.bvh_cache) {
233     bvhcache_free(mesh->runtime.bvh_cache);
234     mesh->runtime.bvh_cache = NULL;
235   }
236   MEM_SAFE_FREE(mesh->runtime.looptris.array);
237   /* TODO(sergey): Does this really belong here? */
238   if (mesh->runtime.subdiv_ccg != NULL) {
239     BKE_subdiv_ccg_destroy(mesh->runtime.subdiv_ccg);
240     mesh->runtime.subdiv_ccg = NULL;
241   }
242   BKE_shrinkwrap_discard_boundary_data(mesh);
243 }
244 
245 /** \} */
246 
247 /* -------------------------------------------------------------------- */
248 /** \name Mesh Batch Cache Callbacks
249  * \{ */
250 
251 /* Draw Engine */
252 void (*BKE_mesh_batch_cache_dirty_tag_cb)(Mesh *me, eMeshBatchDirtyMode mode) = NULL;
253 void (*BKE_mesh_batch_cache_free_cb)(Mesh *me) = NULL;
254 
BKE_mesh_batch_cache_dirty_tag(Mesh * me,eMeshBatchDirtyMode mode)255 void BKE_mesh_batch_cache_dirty_tag(Mesh *me, eMeshBatchDirtyMode mode)
256 {
257   if (me->runtime.batch_cache) {
258     BKE_mesh_batch_cache_dirty_tag_cb(me, mode);
259   }
260 }
BKE_mesh_batch_cache_free(Mesh * me)261 void BKE_mesh_batch_cache_free(Mesh *me)
262 {
263   if (me->runtime.batch_cache) {
264     BKE_mesh_batch_cache_free_cb(me);
265   }
266 }
267 
268 /** \} */
269 
270 /** \name Mesh runtime debug helpers.
271  * \{ */
272 /* evaluated mesh info printing function,
273  * to help track down differences output */
274 
275 #ifndef NDEBUG
276 #  include "BLI_dynstr.h"
277 
mesh_runtime_debug_info_layers(DynStr * dynstr,CustomData * cd)278 static void mesh_runtime_debug_info_layers(DynStr *dynstr, CustomData *cd)
279 {
280   int type;
281 
282   for (type = 0; type < CD_NUMTYPES; type++) {
283     if (CustomData_has_layer(cd, type)) {
284       /* note: doesn't account for multiple layers */
285       const char *name = CustomData_layertype_name(type);
286       const int size = CustomData_sizeof(type);
287       const void *pt = CustomData_get_layer(cd, type);
288       const int pt_size = pt ? (int)(MEM_allocN_len(pt) / size) : 0;
289       const char *structname;
290       int structnum;
291       CustomData_file_write_info(type, &structname, &structnum);
292       BLI_dynstr_appendf(
293           dynstr,
294           "        dict(name='%s', struct='%s', type=%d, ptr='%p', elem=%d, length=%d),\n",
295           name,
296           structname,
297           type,
298           (const void *)pt,
299           size,
300           pt_size);
301     }
302   }
303 }
304 
BKE_mesh_runtime_debug_info(Mesh * me_eval)305 char *BKE_mesh_runtime_debug_info(Mesh *me_eval)
306 {
307   DynStr *dynstr = BLI_dynstr_new();
308   char *ret;
309 
310   BLI_dynstr_append(dynstr, "{\n");
311   BLI_dynstr_appendf(dynstr, "    'ptr': '%p',\n", (void *)me_eval);
312 #  if 0
313   const char *tstr;
314   switch (me_eval->type) {
315     case DM_TYPE_CDDM:
316       tstr = "DM_TYPE_CDDM";
317       break;
318     case DM_TYPE_CCGDM:
319       tstr = "DM_TYPE_CCGDM";
320       break;
321     default:
322       tstr = "UNKNOWN";
323       break;
324   }
325   BLI_dynstr_appendf(dynstr, "    'type': '%s',\n", tstr);
326 #  endif
327   BLI_dynstr_appendf(dynstr, "    'totvert': %d,\n", me_eval->totvert);
328   BLI_dynstr_appendf(dynstr, "    'totedge': %d,\n", me_eval->totedge);
329   BLI_dynstr_appendf(dynstr, "    'totface': %d,\n", me_eval->totface);
330   BLI_dynstr_appendf(dynstr, "    'totpoly': %d,\n", me_eval->totpoly);
331   BLI_dynstr_appendf(dynstr, "    'deformed_only': %d,\n", me_eval->runtime.deformed_only);
332 
333   BLI_dynstr_append(dynstr, "    'vertexLayers': (\n");
334   mesh_runtime_debug_info_layers(dynstr, &me_eval->vdata);
335   BLI_dynstr_append(dynstr, "    ),\n");
336 
337   BLI_dynstr_append(dynstr, "    'edgeLayers': (\n");
338   mesh_runtime_debug_info_layers(dynstr, &me_eval->edata);
339   BLI_dynstr_append(dynstr, "    ),\n");
340 
341   BLI_dynstr_append(dynstr, "    'loopLayers': (\n");
342   mesh_runtime_debug_info_layers(dynstr, &me_eval->ldata);
343   BLI_dynstr_append(dynstr, "    ),\n");
344 
345   BLI_dynstr_append(dynstr, "    'polyLayers': (\n");
346   mesh_runtime_debug_info_layers(dynstr, &me_eval->pdata);
347   BLI_dynstr_append(dynstr, "    ),\n");
348 
349   BLI_dynstr_append(dynstr, "    'tessFaceLayers': (\n");
350   mesh_runtime_debug_info_layers(dynstr, &me_eval->fdata);
351   BLI_dynstr_append(dynstr, "    ),\n");
352 
353   BLI_dynstr_append(dynstr, "}\n");
354 
355   ret = BLI_dynstr_get_cstring(dynstr);
356   BLI_dynstr_free(dynstr);
357   return ret;
358 }
359 
BKE_mesh_runtime_debug_print(Mesh * me_eval)360 void BKE_mesh_runtime_debug_print(Mesh *me_eval)
361 {
362   char *str = BKE_mesh_runtime_debug_info(me_eval);
363   puts(str);
364   fflush(stdout);
365   MEM_freeN(str);
366 }
367 
368 /* XXX Should go in customdata file? */
BKE_mesh_runtime_debug_print_cdlayers(CustomData * data)369 void BKE_mesh_runtime_debug_print_cdlayers(CustomData *data)
370 {
371   int i;
372   const CustomDataLayer *layer;
373 
374   printf("{\n");
375 
376   for (i = 0, layer = data->layers; i < data->totlayer; i++, layer++) {
377 
378     const char *name = CustomData_layertype_name(layer->type);
379     const int size = CustomData_sizeof(layer->type);
380     const char *structname;
381     int structnum;
382     CustomData_file_write_info(layer->type, &structname, &structnum);
383     printf("        dict(name='%s', struct='%s', type=%d, ptr='%p', elem=%d, length=%d),\n",
384            name,
385            structname,
386            layer->type,
387            (const void *)layer->data,
388            size,
389            (int)(MEM_allocN_len(layer->data) / size));
390   }
391 
392   printf("}\n");
393 }
394 
BKE_mesh_runtime_is_valid(Mesh * me_eval)395 bool BKE_mesh_runtime_is_valid(Mesh *me_eval)
396 {
397   const bool do_verbose = true;
398   const bool do_fixes = false;
399 
400   bool is_valid = true;
401   bool changed = true;
402 
403   if (do_verbose) {
404     printf("MESH: %s\n", me_eval->id.name + 2);
405   }
406 
407   is_valid &= BKE_mesh_validate_all_customdata(
408       &me_eval->vdata,
409       me_eval->totvert,
410       &me_eval->edata,
411       me_eval->totedge,
412       &me_eval->ldata,
413       me_eval->totloop,
414       &me_eval->pdata,
415       me_eval->totpoly,
416       false, /* setting mask here isn't useful, gives false positives */
417       do_verbose,
418       do_fixes,
419       &changed);
420 
421   is_valid &= BKE_mesh_validate_arrays(me_eval,
422                                        me_eval->mvert,
423                                        me_eval->totvert,
424                                        me_eval->medge,
425                                        me_eval->totedge,
426                                        me_eval->mface,
427                                        me_eval->totface,
428                                        me_eval->mloop,
429                                        me_eval->totloop,
430                                        me_eval->mpoly,
431                                        me_eval->totpoly,
432                                        me_eval->dvert,
433                                        do_verbose,
434                                        do_fixes,
435                                        &changed);
436 
437   BLI_assert(changed == false);
438 
439   return is_valid;
440 }
441 
442 #endif /* NDEBUG */
443 
444 /** \} */
445