diff options
author | Joseph Eagar <joeedh@gmail.com> | 2009-09-05 10:10:30 +0400 |
---|---|---|
committer | Joseph Eagar <joeedh@gmail.com> | 2009-09-05 10:10:30 +0400 |
commit | 971155b3733f56ebf3aedddc65a6110a5b1229ae (patch) | |
tree | 635396624de576ba2bd715b1b94bb729316ffbcb | |
parent | bc0a071c29868f4e0cffdec663bb5bc2bbd2c40f (diff) |
part one of profiling/optimizing. made cddm not update tesselations itself in cddm_from_mesh. also made BMO_Test/Set/ClearFlag into macros, and tweaked the way normals are calculated.
-rw-r--r-- | source/blender/blenkernel/intern/cdderivedmesh.c | 11 | ||||
-rw-r--r-- | source/blender/blenkernel/intern/modifiers_bmesh.c | 18 | ||||
-rw-r--r-- | source/blender/bmesh/bmesh_operator_api.h | 7 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_operators.c | 91 | ||||
-rw-r--r-- | source/blender/bmesh/intern/bmesh_polygon.c | 22 | ||||
-rw-r--r-- | source/blender/editors/mesh/bmeshutils.c | 8 | ||||
-rw-r--r-- | source/blender/editors/sculpt_paint/paint_vertex.c | 155 |
7 files changed, 165 insertions, 147 deletions
diff --git a/source/blender/blenkernel/intern/cdderivedmesh.c b/source/blender/blenkernel/intern/cdderivedmesh.c index cbb9103ff61..14f03522017 100644 --- a/source/blender/blenkernel/intern/cdderivedmesh.c +++ b/source/blender/blenkernel/intern/cdderivedmesh.c @@ -951,8 +951,8 @@ DerivedMesh *CDDM_from_mesh(Mesh *mesh, Object *ob) mesh->totvert); CustomData_merge(&mesh->edata, &dm->edgeData, mask, alloctype, mesh->totedge); - //CustomData_merge(&mesh->fdata, &dm->faceData, mask, alloctype, - // mesh->totface); + CustomData_merge(&mesh->fdata, &dm->faceData, mask|CD_MASK_ORIGINDEX, alloctype, + mesh->totface); CustomData_merge(&mesh->ldata, &dm->loopData, mask, alloctype, mesh->totloop); CustomData_merge(&mesh->pdata, &dm->polyData, mask, alloctype, @@ -962,7 +962,7 @@ DerivedMesh *CDDM_from_mesh(Mesh *mesh, Object *ob) cddm->medge = CustomData_get_layer(&dm->edgeData, CD_MEDGE); cddm->mloop = CustomData_get_layer(&dm->loopData, CD_MLOOP); cddm->mpoly = CustomData_get_layer(&dm->polyData, CD_MPOLY); - //cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE); + cddm->mface = CustomData_get_layer(&dm->faceData, CD_MFACE); index = CustomData_get_layer(&dm->vertData, CD_ORIGINDEX); for(i = 0; i < mesh->totvert; ++i, ++index) @@ -976,9 +976,8 @@ DerivedMesh *CDDM_from_mesh(Mesh *mesh, Object *ob) for(i = 0; i < mesh->totpoly; ++i, ++index) *index = i; - /*recalculates mfaces and sets the mface origindex layer - to index mypolys.*/ - cdDM_recalcTesselation((DerivedMesh*)cddm); + if (!CustomData_has_layer(&cddm->dm.faceData, CD_ORIGINDEX)) + CustomData_add_layer(&dm->faceData, CD_ORIGINDEX, CD_CALLOC, NULL, mesh->totface); return dm; } diff --git a/source/blender/blenkernel/intern/modifiers_bmesh.c b/source/blender/blenkernel/intern/modifiers_bmesh.c index e7defaad567..7b4edc3c622 100644 --- a/source/blender/blenkernel/intern/modifiers_bmesh.c +++ b/source/blender/blenkernel/intern/modifiers_bmesh.c @@ -177,6 +177,7 @@ BMEditMesh *CDDM_To_BMesh(DerivedMesh *dm, BMEditMesh *existing) } MEM_freeN(medge); + /*do faces*/ k = 0; dfiter = dm->newFaceIter(dm); for (; !dfiter->done; dfiter->step(dfiter)) { @@ -215,7 +216,6 @@ BMEditMesh *CDDM_To_BMesh(DerivedMesh *dm, BMEditMesh *existing) CustomData_to_bmesh_block(&dm->polyData, &bm->pdata, dfiter->index, &f->head.data); } - dfiter->free(dfiter); MEM_freeN(vtable); @@ -275,7 +275,7 @@ static DerivedMesh *arrayModifier_doArray(ArrayModifierData *amd, Scene *scene, Object *ob, DerivedMesh *dm, int initFlags) { - DerivedMesh *cddm = CDDM_copy(dm); + DerivedMesh *cddm = dm; //CDDM_copy(dm); copying shouldn't be necassary here, as all modifiers return CDDM's BMEditMesh *em = CDDM_To_BMesh(cddm, NULL); BMOperator op, oldop, weldop; int i, j, indexLen; @@ -388,8 +388,8 @@ static DerivedMesh *arrayModifier_doArray(ArrayModifierData *amd, MTC_Mat4CpyMat4(final_offset, tmp_mat); } - cddm->needsFree = 1; - cddm->release(cddm); + //cddm->needsFree = 1; + //cddm->release(cddm); BMO_Init_Op(&weldop, "weldverts"); BMO_InitOpf(em->bm, &op, "dupe geom=%avef"); @@ -475,7 +475,9 @@ static DerivedMesh *arrayModifier_doArray(ArrayModifierData *amd, if (j > 0) BMO_Finish_Op(em->bm, &op); - BMO_Exec_Op(em->bm, &weldop); + if (amd->flags & MOD_ARR_MERGE) + BMO_Exec_Op(em->bm, &weldop); + BMO_Finish_Op(em->bm, &weldop); //BMO_CallOpf(em->bm, "removedoubles verts=%av dist=%f", amd->merge_dist); @@ -533,11 +535,11 @@ DerivedMesh *doMirrorOnAxis(MirrorModifierData *mmd, float mtx[4][4], imtx[4][4]; int i, j; - cddm = CDDM_copy(dm); + cddm = dm; //CDDM_copy(dm); copying shouldn't be necassary here, as all modifiers return CDDM's em = CDDM_To_BMesh(dm, NULL); - cddm->needsFree = 1; - cddm->release(cddm); + //cddm->needsFree = 1; + //cddm->release(cddm); /*convienence variable*/ bm = em->bm; diff --git a/source/blender/bmesh/bmesh_operator_api.h b/source/blender/bmesh/bmesh_operator_api.h index de6bbdcdcdf..fd8889f5c71 100644 --- a/source/blender/bmesh/bmesh_operator_api.h +++ b/source/blender/bmesh/bmesh_operator_api.h @@ -131,9 +131,14 @@ void BMO_Finish_Op(struct BMesh *bm, struct BMOperator *op); if you need to store a value per element, use a ghash or a mapping slot to do it.*/ /*flags 15 and 16 (1<<14 and 1<<15) are reserved for bmesh api use*/ +#define BMO_TestFlag(bm, element, flag) (((BMHeader*)element)->flags[bm->stackdepth-1].mask & (flag)) +#define BMO_SetFlag(bm, element, flag) (((BMHeader*)element)->flags[bm->stackdepth-1].mask |= (flag)) +#define BMO_ClearFlag(bm, element, flag) (((BMHeader*)element)->flags[bm->stackdepth-1].mask &= ~(flag)) + +/*profiling showed a significant amount of time spent in BMO_TestFlag void BMO_SetFlag(struct BMesh *bm, void *element, int flag); void BMO_ClearFlag(struct BMesh *bm, void *element, int flag); -int BMO_TestFlag(struct BMesh *bm, void *element, int flag); +int BMO_TestFlag(struct BMesh *bm, void *element, int flag);*/ /*count the number of elements with a specific flag. type can be a bitmask of BM_FACE, BM_EDGE, or BM_FACE.*/ diff --git a/source/blender/bmesh/intern/bmesh_operators.c b/source/blender/bmesh/intern/bmesh_operators.c index 95d388dadf0..041f0320488 100644 --- a/source/blender/bmesh/intern/bmesh_operators.c +++ b/source/blender/bmesh/intern/bmesh_operators.c @@ -357,47 +357,6 @@ void BMO_Get_Vec(BMOperator *op, char *slotname, float *vec_out) } /* - * BMO_SETFLAG - * - * Sets a flag for a certain element - * -*/ -void BMO_SetFlag(BMesh *bm, void *element, int flag) -{ - BMHeader *head = element; - head->flags[bm->stackdepth-1].mask |= flag; -} - -/* - * BMO_CLEARFLAG - * - * Clears a specific flag from a given element - * -*/ - -void BMO_ClearFlag(BMesh *bm, void *element, int flag) -{ - BMHeader *head = element; - head->flags[bm->stackdepth-1].mask &= ~flag; -} - -/* - * BMO_TESTFLAG - * - * Tests whether or not a flag is set for a specific element - * - * -*/ - -int BMO_TestFlag(BMesh *bm, void *element, int flag) -{ - BMHeader *head = element; - if(head->flags[bm->stackdepth-1].mask & flag) - return 1; - return 0; -} - -/* * BMO_COUNTFLAG * * Counts the number of elements of a certain type that @@ -1347,3 +1306,53 @@ int BMO_CallOpf(BMesh *bm, char *fmt, ...) { return 1; } + +/* + * BMO_SETFLAG + * + * Sets a flag for a certain element + * +*/ +#ifdef BMO_SetFlag +#undef BMO_SetFlag +#endif +void BMO_SetFlag(BMesh *bm, void *element, int flag) +{ + BMHeader *head = element; + head->flags[bm->stackdepth-1].mask |= flag; +} + +/* + * BMO_CLEARFLAG + * + * Clears a specific flag from a given element + * +*/ + +#ifdef BMO_ClearFlag +#undef BMO_ClearFlag +#endif +void BMO_ClearFlag(BMesh *bm, void *element, int flag) +{ + BMHeader *head = element; + head->flags[bm->stackdepth-1].mask &= ~flag; +} + +/* + * BMO_TESTFLAG + * + * Tests whether or not a flag is set for a specific element + * + * +*/ + +#ifdef BMO_TestFlag +#undef BMO_TestFlag +#endif +int BMO_TestFlag(BMesh *bm, void *element, int flag) +{ + BMHeader *head = element; + if(head->flags[bm->stackdepth-1].mask & flag) + return 1; + return 0; +} diff --git a/source/blender/bmesh/intern/bmesh_polygon.c b/source/blender/bmesh/intern/bmesh_polygon.c index 38a28e3e84b..bce372f5083 100644 --- a/source/blender/bmesh/intern/bmesh_polygon.c +++ b/source/blender/bmesh/intern/bmesh_polygon.c @@ -90,6 +90,7 @@ static void compute_poly_normal(float normal[3], float (*verts)[3], int nverts) double u[3], v[3], w[3];/*, *w, v1[3], v2[3];*/ double n[3] = {0.0, 0.0, 0.0}, l, v1[3], v2[3]; + double l2; int i, s=0; /*this fixes some weird numerical error*/ @@ -129,7 +130,15 @@ static void compute_poly_normal(float normal[3], float (*verts)[3], int nverts) n[2] += (u[0] - v[0]) * (u[1] + v[1]); } - l = sqrt(n[0]*n[0]+n[1]*n[1]+n[2]*n[2]); + l = n[0]*n[0]+n[1]*n[1]+n[2]*n[2]; + l = sqrt(l); + /*fast square root, newton/babylonian method: + l2 = l*0.1; + + l2 = (l/l2 + l2)*0.5; + l2 = (l/l2 + l2)*0.5; + l2 = (l/l2 + l2)*0.5; + */ if (l == 0.0) { normal[0] = 0.0f; @@ -137,11 +146,11 @@ static void compute_poly_normal(float normal[3], float (*verts)[3], int nverts) normal[2] = 1.0f; return; - } + } else l = 1.0f / l; - n[0] /= l; - n[1] /= l; - n[2] /= l; + n[0] *= l; + n[1] *= l; + n[2] *= l; normal[0] = (float) n[0]; normal[1] = (float) n[1]; @@ -437,7 +446,7 @@ void bmesh_update_face_normal(BMesh *bm, BMFace *f, float (*projectverts)[3]) BMLoop *l; int i; - if(1) { //f->len > 4){ + if(f->len > 4) { i = 0; l = f->loopbase; do{ @@ -446,7 +455,6 @@ void bmesh_update_face_normal(BMesh *bm, BMFace *f, float (*projectverts)[3]) i += 1; }while(l!=f->loopbase); - //compute_poly_plane(projectverts, f->len); compute_poly_normal(f->no, projectverts, f->len); } else if(f->len == 3){ diff --git a/source/blender/editors/mesh/bmeshutils.c b/source/blender/editors/mesh/bmeshutils.c index 667e55c6c1a..099dbd86e0e 100644 --- a/source/blender/editors/mesh/bmeshutils.c +++ b/source/blender/editors/mesh/bmeshutils.c @@ -257,14 +257,6 @@ void EDBM_LoadEditBMesh(Scene *scene, Object *ob) BMesh *bm = me->edit_btmesh->bm; BMO_CallOpf(bm, "object_load_bmesh scene=%p object=%p", scene, ob); - -#if 0 - EditMesh *em = bmesh_to_editmesh(me->edit_btmesh->bm); - - load_editMesh(scene, ob, em); - free_editMesh(em); - MEM_freeN(em); -#endif } void EDBM_FreeEditBMesh(BMEditMesh *tm) diff --git a/source/blender/editors/sculpt_paint/paint_vertex.c b/source/blender/editors/sculpt_paint/paint_vertex.c index 4392c34af2a..2d44969aaf2 100644 --- a/source/blender/editors/sculpt_paint/paint_vertex.c +++ b/source/blender/editors/sculpt_paint/paint_vertex.c @@ -204,60 +204,8 @@ static unsigned int vpaint_get_current_col(VPaint *vp) return rgba_to_mcol(brush->rgb[0], brush->rgb[1], brush->rgb[2], 1.0f); } -void do_shared_vertexcol(Mesh *me) +static void do_shared_vertex_tesscol(Mesh *me) { - MLoop *ml = me->mloop; - MLoopCol *lcol = me->mloopcol; - MTexPoly *mtp = me->mtpoly; - MPoly *mp = me->mpoly; - float (*scol)[5]; - int i; - - /* if no mloopcol: do not do */ - /* if mtexpoly: only the involved faces, otherwise all */ - - if(me->mloopcol==0 || me->totvert==0 || me->totpoly==0) return; - - scol = MEM_callocN(sizeof(float)*me->totvert*5, "scol"); - - for (i=0; i<me->totloop; i++, ml++, lcol++) { - if (i >= mp->loopstart + mp->totloop) { - mp++; - if (mtp) mtp++; - } - - if (mtp && !(mtp->mode & TF_SHAREDCOL)) - continue; - - scol[ml->v][0] += lcol->r; - scol[ml->v][1] += lcol->g; - scol[ml->v][2] += lcol->b; - scol[ml->v][3] += lcol->a; - scol[ml->v][4] += 1.0; - } - - for (i=0; i<me->totvert; i++) { - if (!scol[i][4]) continue; - - scol[i][0] /= scol[i][4]; - scol[i][1] /= scol[i][4]; - scol[i][2] /= scol[i][4]; - scol[i][3] /= scol[i][4]; - } - - ml = me->mloop; - lcol = me->mloopcol; - for (i=0; i<me->totloop; i++, ml++, lcol++) { - if (!scol[ml->v][4]) continue; - - lcol->r = scol[ml->v][0]; - lcol->g = scol[ml->v][1]; - lcol->b = scol[ml->v][2]; - lcol->a = scol[ml->v][3]; - } - - MEM_freeN(scol); -#if 0 /* if no mcol: do not do */ /* if tface: only the involved faces, otherwise all */ MFace *mface; @@ -320,7 +268,63 @@ void do_shared_vertexcol(Mesh *me) } MEM_freeN(scolmain); -#endif +} + +void do_shared_vertexcol(Mesh *me) +{ + MLoop *ml = me->mloop; + MLoopCol *lcol = me->mloopcol; + MTexPoly *mtp = me->mtpoly; + MPoly *mp = me->mpoly; + float (*scol)[5]; + int i; + + /* if no mloopcol: do not do */ + /* if mtexpoly: only the involved faces, otherwise all */ + + if(me->mloopcol==0 || me->totvert==0 || me->totpoly==0) return; + + scol = MEM_callocN(sizeof(float)*me->totvert*5, "scol"); + + for (i=0; i<me->totloop; i++, ml++, lcol++) { + if (i >= mp->loopstart + mp->totloop) { + mp++; + if (mtp) mtp++; + } + + if (mtp && !(mtp->mode & TF_SHAREDCOL)) + continue; + + scol[ml->v][0] += lcol->r; + scol[ml->v][1] += lcol->g; + scol[ml->v][2] += lcol->b; + scol[ml->v][3] += lcol->a; + scol[ml->v][4] += 1.0; + } + + for (i=0; i<me->totvert; i++) { + if (!scol[i][4]) continue; + + scol[i][0] /= scol[i][4]; + scol[i][1] /= scol[i][4]; + scol[i][2] /= scol[i][4]; + scol[i][3] /= scol[i][4]; + } + + ml = me->mloop; + lcol = me->mloopcol; + for (i=0; i<me->totloop; i++, ml++, lcol++) { + if (!scol[ml->v][4]) continue; + + lcol->r = scol[ml->v][0]; + lcol->g = scol[ml->v][1]; + lcol->b = scol[ml->v][2]; + lcol->a = scol[ml->v][3]; + } + + MEM_freeN(scol); + + do_shared_vertex_tesscol(me); } void make_vertexcol(Scene *scene, int shade) /* single ob */ @@ -1261,6 +1265,13 @@ static int wpaint_stroke_test_start(bContext *C, wmOperator *op, wmEvent *event) me= get_mesh(ob); if(me==NULL || me->totface==0) return OPERATOR_PASS_THROUGH; + /*we can't assume mfaces have a correct origindex layer that indices to mpolys. + so instead we have to regenerate the tesselation faces altogether.*/ + me->totface = mesh_recalcTesselation(&me->fdata, &me->ldata, &me->pdata, + me->mvert, me->totface, me->totloop, me->totpoly); + mesh_update_customdata_pointers(me); + makeDerivedMesh(scene, ob, NULL, CD_MASK_BAREMESH); + /* if nothing was added yet, we make dverts and a vertex deform group */ if (!me->dvert) create_dverts(&me->id); @@ -1646,7 +1657,8 @@ struct VPaintData { }; -static void vpaint_build_poly_facemap(struct VPaintData *vd, Mesh *me) +static void vpaint_build_poly_facemap(struct VPaintData *vd, Mesh *me, + Object *ob, Scene *scene) { MFace *mf; polyfacemap_e *e; @@ -1662,8 +1674,9 @@ static void vpaint_build_poly_facemap(struct VPaintData *vd, Mesh *me) so instead we have to regenerate the tesselation faces altogether.*/ me->totface = mesh_recalcTesselation(&me->fdata, &me->ldata, &me->pdata, me->mvert, me->totface, me->totloop, me->totpoly); - mesh_update_customdata_pointers(me); + makeDerivedMesh(scene, ob, NULL, CD_MASK_BAREMESH); + origIndex = CustomData_get_layer(&me->fdata, CD_ORIGINDEX); mf = me->mface; @@ -1688,6 +1701,7 @@ static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent VPaint *vp= ts->vpaint; struct VPaintData *vpd; Object *ob= CTX_data_active_object(C); + Scene *scene = CTX_data_scene(C); Mesh *me; float mat[4][4], imat[4][4]; @@ -1709,7 +1723,7 @@ static int vpaint_stroke_test_start(bContext *C, struct wmOperator *op, wmEvent vpd->vertexcosnos= mesh_get_mapped_verts_nors(vpd->vc.scene, ob); vpd->indexar= get_indexarray(); vpd->paintcol= vpaint_get_current_col(vp); - vpaint_build_poly_facemap(vpd, me); + vpaint_build_poly_facemap(vpd, me, ob, scene); /* for filtering */ copy_vpaint_prev(vp, (unsigned int *)me->mloopcol, me->totloop); @@ -1827,22 +1841,13 @@ static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P if(alpha) vpaint_blend(vp, lcol+i, lcolorig+i, vpd->paintcol, alpha); } -#ifdef CPYCOL -#if 0 + #ifdef CPYCOL #undef CPYCOL #endif #define CPYCOL(c, l) (c)->a = (l)->a, (c)->r = (l)->r, (c)->g = (l)->g, (c)->b = (l)->b - /*update vertex colors for tesselations. this code - isn't strictly necassary, since CDDM regenerates - the tesselations when it creates a CDDM from - a Mesh. this also makes it hard to test that this - code is working correctly. - - if it turns out having cddm constantly recalculating - the tesselation is a problem, we can always turn on - this code and add an exception where cddm doesn't - redo the tesselation if it's in vpaint mode.*/ + /*update vertex colors for tesselations incrementally, + rather then regenerating the tesselation altogether.*/ for (e=vpd->polyfacemap[(indexar[index]-1)].first; e; e=e->next) { mf = me->mface + e->facenr; mc = me->mcol + e->facenr*4; @@ -1853,25 +1858,23 @@ static void vpaint_stroke_update_step(bContext *C, struct PaintStroke *stroke, P if (ml->v == mf->v1) CPYCOL(mc, mlc); else if (ml->v == mf->v2) - CPYCOL(mc+4, mlc); + CPYCOL(mc+1, mlc); else if (ml->v == mf->v3) - CPYCOL(mc+8, mlc); + CPYCOL(mc+2, mlc); else if (mf->v4 && ml->v == mf->v4) - CPYCOL(mc+12, mlc); + CPYCOL(mc+3, mlc); } } #undef CPYCOL -#endif } } MTC_Mat4SwapMat4(vc->rv3d->persmat, mat); do_shared_vertexcol(me); - - ED_region_tag_redraw(vc->ar); - + + ED_region_tag_redraw(vc->ar); DAG_object_flush_update(vc->scene, ob, OB_RECALC_DATA); } |