svn merge ^/trunk/blender -r43482:43524
[blender.git] / source / blender / blenkernel / intern / DerivedMesh.c
index 89988b0225839378496e42f0f5e9e47f50be7136..63605c14f6cb3cb5567bb4f1fb4714eff2d2fadd 100644 (file)
@@ -352,6 +352,26 @@ void DM_DupPolys(DerivedMesh *source, DerivedMesh *target)
        }
 }
 
+/* note: until all modifiers can take MPoly's as input,
+ * use this at the start of modifiers  */
+void DM_ensure_tessface(DerivedMesh *dm)
+{
+       const int numTessFaces = dm->getNumTessFaces(dm);
+       const int numPolys =     dm->getNumPolys(dm);
+
+       if ( (numTessFaces == 0) && (numPolys != 0)) {
+               dm->recalcTesselation(dm);
+
+               if (dm->getNumTessFaces(dm)) {
+                       printf("warning %s: could not create tessfaces from %d polygons, dm->type=%d\n",
+                              __func__, numPolys, dm->type);
+               }
+               else {
+                       printf("info %s: polys -> ngons calculated\n", __func__);
+               }
+       }
+}
+
 void DM_to_mesh(DerivedMesh *dm, Mesh *me, Object *ob)
 {
        /* dm might depend on me, so we need to do everything with a local copy */
@@ -787,7 +807,7 @@ static DerivedMesh *create_orco_dm(Object *ob, Mesh *me, BMEditMesh *em, int lay
        float (*orco)[3];
        int free;
 
-       if(em) dm= CDDM_from_BMEditMesh(em, me, 0);
+       if(em) dm= CDDM_from_BMEditMesh(em, me, FALSE, FALSE);
        else dm= CDDM_from_mesh(me, ob);
 
        orco= get_orco_coords_dm(ob, em, layer, &free);
@@ -1007,15 +1027,30 @@ static void add_weight_mcol_dm(Object *ob, DerivedMesh *dm, int const draw_flag)
        MPoly *mp = dm->getPolyArray(dm);
        int i, j, totface=dm->getNumTessFaces(dm), totloop;
        int *origIndex = dm->getVertDataArray(dm, CD_ORIGINDEX);
+       unsigned char *wtcol_f_step;
 
        wtcol_f = MEM_mallocN(sizeof (unsigned char) * totface*4*4, "weightmap_f");
+       wtcol_f_step = wtcol_f;
 
        /*first add colors to the tesselation faces*/
-       for (i=0; i<totface; i++, mf++) {
+       for (i=0; i<totface; i++, mf++, wtcol_f_step += (4 * 4)) {
                /*origindex being NULL means we're operating on original mesh data*/
+#if 0
                unsigned int fidx= mf->v4 ? 3:2;
+
+#else  /* better zero out triangles 4th component. else valgrind complains when the buffer's copied */
+               unsigned int fidx;
+               if (mf->v4) {
+                       fidx = 3;
+               }
+               else {
+                       fidx = 2;
+                       *(int *)(&wtcol_f_step[3 * 4]) = 0;
+               }
+#endif
+
                do {
-                       copy_v4_v4_char((char *)&wtcol_f[(4 * i + fidx) * 4],
+                       copy_v4_v4_char((char *)&wtcol_f_step[fidx * 4],
                                        (char *)&wtcol_v[4 * (*(&mf->v1 + fidx))]);
                } while (fidx--);
        }
@@ -1511,7 +1546,7 @@ static void mesh_calc_modifiers(Scene *scene, Object *ob, float (*inputVertexCos
        finaldm->recalcTesselation(finaldm);
        /* if we have no modifiers applied we'lll still want the tessface normals
         * to be calculated from the polygon noramals,
-        * 'CDDM_calc_normals' checks for this case - campbell */
+        * 'CDDM_calc_normals_mapping' checks for this case - campbell */
        finaldm->calcNormals(finaldm);
        /* Need to watch this, it can cause issues, see bug [#29338]             */
        /* take care with this block, we really need testing frameworks          */
@@ -1645,7 +1680,7 @@ static void editbmesh_calc_modifiers(Scene *scene, Object *ob, BMEditMesh *em, D
                                }
 
                        } else {
-                               dm = CDDM_from_BMEditMesh(em, ob->data, 0);
+                               dm = CDDM_from_BMEditMesh(em, ob->data, FALSE, FALSE);
 
                                if(deformedVerts) {
                                        CDDM_apply_vert_coords(dm, deformedVerts);
@@ -1727,7 +1762,7 @@ static void editbmesh_calc_modifiers(Scene *scene, Object *ob, BMEditMesh *em, D
                if(!(cage_r && dm == *cage_r)) dm->release(dm);
 
                CDDM_apply_vert_coords(*final_r, deformedVerts);
-               CDDM_calc_normals(*final_r);
+               CDDM_calc_normals_mapping(*final_r);
        } else if (dm) {
                *final_r = dm;
                (*final_r)->calcNormals(*final_r); /* BMESH_ONLY - BMESH_TODO. check if this is needed */
@@ -2704,3 +2739,84 @@ static DerivedMesh *navmesh_dm_createNavMeshForVisualization(DerivedMesh *dm)
 #endif /* WITH_GAMEENGINE */
 
 /* --- NAVMESH (end) --- */
+
+
+/* derivedmesh info printing function,
+ * to help track down differences DM output */
+
+#ifndef NDEBUG
+#include "BLI_dynstr.h"
+
+static void dm_debug_info_layers(DynStr *dynstr, DerivedMesh *dm, void *(*getElemDataArray)(DerivedMesh *, int))
+{
+       int type;
+
+       for (type = 0; type < CD_NUMTYPES; type++) {
+               /* note: doesnt account for multiple layers */
+               void *pt = getElemDataArray(dm, type);
+               if (pt) {
+                       const char *name = CustomData_layertype_name(type);
+                       const int size = CustomData_sizeof(type);
+                       const char *structname;
+                       int structnum;
+                       CustomData_file_write_info(type, &structname, &structnum);
+                       BLI_dynstr_appendf(dynstr,
+                                          "        dict(name='%s', struct='%s', type=%d, ptr='%p', elem=%d, length=%d),\n",
+                                                          name, structname, type, (void *)pt, size, (int)(MEM_allocN_len(pt) / size));
+               }
+       }
+}
+
+char *DM_debug_info(DerivedMesh *dm)
+{
+       DynStr *dynstr= BLI_dynstr_new();
+       char *ret;
+       const char *tstr;
+
+       BLI_dynstr_appendf(dynstr, "{\n");
+       BLI_dynstr_appendf(dynstr, "    'ptr': '%p',\n", (void *)dm);
+       switch (dm->type) {
+               case DM_TYPE_CDDM:     tstr = "DM_TYPE_CDDM";     break;
+               case DM_TYPE_EDITBMESH: tstr = "DM_TYPE_EDITMESH";  break;
+               case DM_TYPE_CCGDM:    tstr = "DM_TYPE_CCGDM";     break;
+               default:               tstr = "UNKNOWN";           break;
+       }
+       BLI_dynstr_appendf(dynstr, "    'type': '%s',\n", tstr);
+       BLI_dynstr_appendf(dynstr, "    'numVertData': %d,\n", dm->numVertData);
+       BLI_dynstr_appendf(dynstr, "    'numEdgeData': %d,\n", dm->numEdgeData);
+       BLI_dynstr_appendf(dynstr, "    'numTessFaceData': %d,\n", dm->numTessFaceData);
+       BLI_dynstr_appendf(dynstr, "    'numPolyData': %d,\n", dm->numPolyData);
+       BLI_dynstr_appendf(dynstr, "    'deformedOnly': %d,\n", dm->deformedOnly);
+
+       BLI_dynstr_appendf(dynstr, "    'vertexLayers': (\n");
+       dm_debug_info_layers(dynstr, dm, dm->getVertDataArray);
+       BLI_dynstr_appendf(dynstr, "    ),\n");
+
+       BLI_dynstr_appendf(dynstr, "    'edgeLayers': (\n");
+       dm_debug_info_layers(dynstr, dm, dm->getEdgeDataArray);
+       BLI_dynstr_appendf(dynstr, "    ),\n");
+
+       BLI_dynstr_appendf(dynstr, "    'tessFaceLayers': (\n");
+       dm_debug_info_layers(dynstr, dm, dm->getTessFaceDataArray);
+       BLI_dynstr_appendf(dynstr, "    ),\n");
+
+       BLI_dynstr_appendf(dynstr, "    'PolyLayers': (\n");
+       dm_debug_info_layers(dynstr, dm, DM_get_poly_data_layer);
+       BLI_dynstr_appendf(dynstr, "    ),\n");
+
+       BLI_dynstr_appendf(dynstr, "}\n");
+
+       ret = BLI_dynstr_get_cstring(dynstr);
+       BLI_dynstr_free(dynstr);
+       return ret;
+}
+
+void DM_debug_print(DerivedMesh *dm)
+{
+       char *str = DM_debug_info(dm);
+       printf("%s", str);
+       fflush(stdout);
+       MEM_freeN(str);
+}
+
+#endif /* NDEBUG */