Merged changes in the trunk up to revision 54110.
[blender.git] / source / blender / blenkernel / intern / subsurf_ccg.c
index 01409a0f7e4fd7ba380eb5b45e0040833b6cbc2e..ce6e158b6d978cdb268ed8cb382e069ea56bc83f 100644 (file)
@@ -1017,7 +1017,11 @@ static void ccgDM_getFinalFace(DerivedMesh *dm, int faceNum, MFace *mf)
                mf->flag = faceFlags[i].flag;
                mf->mat_nr = faceFlags[i].mat_nr;
        }
-       else mf->flag = ME_SMOOTH;
+       else {
+               mf->flag = ME_SMOOTH;
+}
+
+       mf->edcode = 0;
 }
 
 /* Translate GridHidden into the ME_HIDE flag for MVerts. Assumes
@@ -1097,6 +1101,14 @@ void subsurf_copy_grid_paint_mask(DerivedMesh *dm, const MPoly *mpoly,
        }
 }
 
+/* utility functon */
+BLI_INLINE void ccgDM_to_MVert(MVert *mv, const CCGKey *key, CCGElem *elem)
+{
+       copy_v3_v3(mv->co, CCG_elem_co(key, elem));
+       normal_float_to_short_v3(mv->no, CCG_elem_no(key, elem));
+       mv->flag = mv->bweight = 0;
+}
+
 static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert)
 {
        CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
@@ -1107,7 +1119,7 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert)
        int totvert, totedge, totface;
        int gridSize = ccgSubSurf_getGridSize(ss);
        int edgeSize = ccgSubSurf_getEdgeSize(ss);
-       int i = 0;
+       unsigned int i = 0;
 
        CCG_key_top_level(&key, ss);
 
@@ -1117,24 +1129,20 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert)
                int x, y, S, numVerts = ccgSubSurf_getFaceNumVerts(f);
 
                vd = ccgSubSurf_getFaceCenterData(f);
-               copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd));
-               normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd));
-               i++;
+               ccgDM_to_MVert(&mvert[i++], &key, vd);
                
                for (S = 0; S < numVerts; S++) {
-                       for (x = 1; x < gridSize - 1; x++, i++) {
+                       for (x = 1; x < gridSize - 1; x++) {
                                vd = ccgSubSurf_getFaceGridEdgeData(ss, f, S, x);
-                               copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd));
-                               normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd));
+                               ccgDM_to_MVert(&mvert[i++], &key, vd);
                        }
                }
 
                for (S = 0; S < numVerts; S++) {
                        for (y = 1; y < gridSize - 1; y++) {
-                               for (x = 1; x < gridSize - 1; x++, i++) {
+                               for (x = 1; x < gridSize - 1; x++) {
                                        vd = ccgSubSurf_getFaceGridData(ss, f, S, x, y);
-                                       copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd));
-                                       normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd));
+                                       ccgDM_to_MVert(&mvert[i++], &key, vd);
                                }
                        }
                }
@@ -1145,15 +1153,14 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert)
                CCGEdge *e = ccgdm->edgeMap[index].edge;
                int x;
 
-               for (x = 1; x < edgeSize - 1; x++, i++) {
-                       vd = ccgSubSurf_getEdgeData(ss, e, x);
-                       copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd));
+               for (x = 1; x < edgeSize - 1; x++) {
                        /* This gives errors with -debug-fpe
                         * the normals don't seem to be unit length.
                         * this is most likely caused by edges with no
                         * faces which are now zerod out, see comment in:
                         * ccgSubSurf__calcVertNormals(), - campbell */
-                       normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd));
+                       vd = ccgSubSurf_getEdgeData(ss, e, x);
+                       ccgDM_to_MVert(&mvert[i++], &key, vd);
                }
        }
 
@@ -1162,12 +1169,20 @@ static void ccgDM_copyFinalVertArray(DerivedMesh *dm, MVert *mvert)
                CCGVert *v = ccgdm->vertMap[index].vert;
 
                vd = ccgSubSurf_getVertData(ss, v);
-               copy_v3_v3(mvert[i].co, CCG_elem_co(&key, vd));
-               normal_float_to_short_v3(mvert[i].no, CCG_elem_no(&key, vd));
-               i++;
+               ccgDM_to_MVert(&mvert[i++], &key, vd);
        }
 }
 
+
+/* utility functon */
+BLI_INLINE void ccgDM_to_MEdge(MEdge *med, const int v1, const int v2, const short flag)
+{
+       med->v1 = v1;
+       med->v2 = v2;
+       med->crease = med->bweight = 0;
+       med->flag = flag;
+}
+
 static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
 {
        CCGDerivedMesh *ccgdm = (CCGDerivedMesh *) dm;
@@ -1176,8 +1191,9 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
        int totedge, totface;
        int gridSize = ccgSubSurf_getGridSize(ss);
        int edgeSize = ccgSubSurf_getEdgeSize(ss);
-       int i = 0;
+       unsigned int i = 0;
        short *edgeFlags = ccgdm->edgeFlags;
+       const short ed_interior_flag = ccgdm->drawInteriorEdges ? (ME_EDGEDRAW | ME_EDGERENDER) : 0;
 
        totface = ccgSubSurf_getNumFaces(ss);
        for (index = 0; index < totface; index++) {
@@ -1186,36 +1202,22 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
 
                for (S = 0; S < numVerts; S++) {
                        for (x = 0; x < gridSize - 1; x++) {
-                               MEdge *med = &medge[i];
-
-                               if (ccgdm->drawInteriorEdges)
-                                       med->flag = ME_EDGEDRAW | ME_EDGERENDER;
-                               med->v1 = getFaceIndex(ss, f, S, x, 0, edgeSize, gridSize);
-                               med->v2 = getFaceIndex(ss, f, S, x + 1, 0, edgeSize, gridSize);
-                               i++;
+                               ccgDM_to_MEdge(&medge[i++],
+                                              getFaceIndex(ss, f, S, x,     0, edgeSize, gridSize),
+                                              getFaceIndex(ss, f, S, x + 1, 0, edgeSize, gridSize),
+                                              ed_interior_flag);
                        }
 
                        for (x = 1; x < gridSize - 1; x++) {
                                for (y = 0; y < gridSize - 1; y++) {
-                                       MEdge *med;
-
-                                       med = &medge[i];
-                                       if (ccgdm->drawInteriorEdges)
-                                               med->flag = ME_EDGEDRAW | ME_EDGERENDER;
-                                       med->v1 = getFaceIndex(ss, f, S, x, y,
-                                                              edgeSize, gridSize);
-                                       med->v2 = getFaceIndex(ss, f, S, x, y + 1,
-                                                              edgeSize, gridSize);
-                                       i++;
-
-                                       med = &medge[i];
-                                       if (ccgdm->drawInteriorEdges)
-                                               med->flag = ME_EDGEDRAW | ME_EDGERENDER;
-                                       med->v1 = getFaceIndex(ss, f, S, y, x,
-                                                              edgeSize, gridSize);
-                                       med->v2 = getFaceIndex(ss, f, S, y + 1, x,
-                                                              edgeSize, gridSize);
-                                       i++;
+                                       ccgDM_to_MEdge(&medge[i++],
+                                                      getFaceIndex(ss, f, S, x, y,    edgeSize, gridSize),
+                                                      getFaceIndex(ss, f, S, x, y + 1, edgeSize, gridSize),
+                                                      ed_interior_flag);
+                                       ccgDM_to_MEdge(&medge[i++],
+                                                      getFaceIndex(ss, f, S, y, x,     edgeSize, gridSize),
+                                                      getFaceIndex(ss, f, S, y + 1, x, edgeSize, gridSize),
+                                                      ed_interior_flag);
                                }
                        }
                }
@@ -1224,31 +1226,32 @@ static void ccgDM_copyFinalEdgeArray(DerivedMesh *dm, MEdge *medge)
        totedge = ccgSubSurf_getNumEdges(ss);
        for (index = 0; index < totedge; index++) {
                CCGEdge *e = ccgdm->edgeMap[index].edge;
-               unsigned int flags = 0;
+               short ed_flag = 0;
                int x;
                int edgeIdx = GET_INT_FROM_POINTER(ccgSubSurf_getEdgeEdgeHandle(e));
 
-               if (!ccgSubSurf_getEdgeNumFaces(e)) flags |= ME_LOOSEEDGE;
+               if (!ccgSubSurf_getEdgeNumFaces(e)) {
+                       ed_flag |= ME_LOOSEEDGE;
+               }
 
                if (edgeFlags) {
                        if (edgeIdx != -1) {
 #ifdef WITH_FREESTYLE
-                               flags |= ((edgeFlags[index] & (ME_SEAM | ME_SHARP | ME_FREESTYLE_EDGE)) | ME_EDGEDRAW | ME_EDGERENDER);
+                               ed_flag |= ((edgeFlags[index] & (ME_SEAM | ME_SHARP | ME_FREESTYLE_EDGE)) | ME_EDGEDRAW | ME_EDGERENDER);
 #else
-                               flags |= ((edgeFlags[index] & (ME_SEAM | ME_SHARP)) | ME_EDGEDRAW | ME_EDGERENDER);
+                               ed_flag |= ((edgeFlags[index] & (ME_SEAM | ME_SHARP)) | ME_EDGEDRAW | ME_EDGERENDER);
 #endif
                        }
                }
                else {
-                       flags |= ME_EDGEDRAW | ME_EDGERENDER;
+                       ed_flag |= ME_EDGEDRAW | ME_EDGERENDER;
                }
 
                for (x = 0; x < edgeSize - 1; x++) {
-                       MEdge *med = &medge[i];
-                       med->v1 = getEdgeIndex(ss, e, x, edgeSize);
-                       med->v2 = getEdgeIndex(ss, e, x + 1, edgeSize);
-                       med->flag = flags;
-                       i++;
+                       ccgDM_to_MEdge(&medge[i++],
+                                      getEdgeIndex(ss, e, x, edgeSize),
+                                      getEdgeIndex(ss, e, x + 1, edgeSize),
+                                      ed_flag);
                }
        }
 }
@@ -1286,6 +1289,7 @@ static void ccgDM_copyFinalFaceArray(DerivedMesh *dm, MFace *mface)
                                                              edgeSize, gridSize);
                                        mf->mat_nr = mat_nr;
                                        mf->flag = flag;
+                                       mf->edcode = 0;
 
                                        i++;
                                }