From 0ffde4fae3bd6a62302d829d319955b012a9b73a Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Tue, 7 May 2013 00:00:32 +0000 Subject: expose bmesh volume calculation to python api (use for print toolbox addon). --- source/blender/bmesh/intern/bmesh_queries.c | 8 ++++-- source/blender/bmesh/intern/bmesh_queries.h | 2 +- .../blender/bmesh/operators/bmo_smooth_laplacian.c | 4 +-- source/blender/python/bmesh/bmesh_py_types.c | 32 ++++++++++++++++++++++ 4 files changed, 41 insertions(+), 5 deletions(-) diff --git a/source/blender/bmesh/intern/bmesh_queries.c b/source/blender/bmesh/intern/bmesh_queries.c index a4ddc0cbba4..8a1d513a8fc 100644 --- a/source/blender/bmesh/intern/bmesh_queries.c +++ b/source/blender/bmesh/intern/bmesh_queries.c @@ -1720,7 +1720,7 @@ static void bm_mesh_calc_volume_face(BMFace *f, float *r_vol) *r_vol += (1.0f / 6.0f) * dot_v3v3(p1, cross); } } -float BM_mesh_calc_volume(BMesh *bm) +float BM_mesh_calc_volume(BMesh *bm, bool is_signed) { /* warning, calls own tessellation function, may be slow */ float vol = 0.0f; @@ -1731,5 +1731,9 @@ float BM_mesh_calc_volume(BMesh *bm) bm_mesh_calc_volume_face(f, &vol); } - return fabsf(vol); + if (is_signed == false) { + vol = fabsf(vol); + } + + return vol; } diff --git a/source/blender/bmesh/intern/bmesh_queries.h b/source/blender/bmesh/intern/bmesh_queries.h index 3d3e1a3d9d9..6b1bfca2d80 100644 --- a/source/blender/bmesh/intern/bmesh_queries.h +++ b/source/blender/bmesh/intern/bmesh_queries.h @@ -113,6 +113,6 @@ bool BM_edge_is_any_vert_flag_test(BMEdge *e, const char hflag); bool BM_face_is_any_vert_flag_test(BMFace *f, const char hflag); bool BM_face_is_any_edge_flag_test(BMFace *f, const char hflag); -float BM_mesh_calc_volume(BMesh *bm); +float BM_mesh_calc_volume(BMesh *bm, bool is_signed); #endif /* __BMESH_QUERIES_H__ */ diff --git a/source/blender/bmesh/operators/bmo_smooth_laplacian.c b/source/blender/bmesh/operators/bmo_smooth_laplacian.c index 12368d1aeda..91a52cdabef 100644 --- a/source/blender/bmesh/operators/bmo_smooth_laplacian.c +++ b/source/blender/bmesh/operators/bmo_smooth_laplacian.c @@ -464,7 +464,7 @@ static void validate_solution(LaplacianSystem *sys, int usex, int usey, int usez } if (preserve_volume) { - vini = BM_mesh_calc_volume(sys->bm); + vini = BM_mesh_calc_volume(sys->bm, false); } BMO_ITER (v, &siter, sys->op->slots_in, "verts", BM_VERT) { m_vertex_id = BM_elem_index_get(v); @@ -481,7 +481,7 @@ static void validate_solution(LaplacianSystem *sys, int usex, int usey, int usez } } if (preserve_volume) { - vend = BM_mesh_calc_volume(sys->bm); + vend = BM_mesh_calc_volume(sys->bm, false); volume_preservation(sys->op, vini, vend, usex, usey, usez); } diff --git a/source/blender/python/bmesh/bmesh_py_types.c b/source/blender/python/bmesh/bmesh_py_types.c index 55a6f3c41c4..23837404b31 100644 --- a/source/blender/python/bmesh/bmesh_py_types.c +++ b/source/blender/python/bmesh/bmesh_py_types.c @@ -1171,6 +1171,35 @@ static PyObject *bpy_bmesh_transform(BPy_BMElem *self, PyObject *args, PyObject Py_RETURN_NONE; } +PyDoc_STRVAR(bpy_bmesh_calc_volume_doc, +".. method:: calc_volume(signed=False)\n" +"\n" +" Calculate mesh volume based on face normals.\n" +"\n" +" :arg signed: when signed is true, negative values may be returned.\n" +" :type signed: bool\n" +" :return: The volume of the mesh.\n" +" :rtype: float\n" +); +static PyObject *bpy_bmesh_calc_volume(BPy_BMElem *self, PyObject *args, PyObject *kw) +{ + static const char *kwlist[] = {"signed", NULL}; + PyObject *is_signed = Py_False; + + BPY_BM_CHECK_OBJ(self); + + if (!PyArg_ParseTupleAndKeywords(args, kw, + "|O!:calc_volume", + (char **)kwlist, + &PyBool_Type, &is_signed)) + { + return NULL; + } + else { + return PyFloat_FromDouble(BM_mesh_calc_volume(self->bm, is_signed != Py_False)); + } +} + /* Elem * ---- */ @@ -2454,6 +2483,9 @@ static struct PyMethodDef bpy_bmesh_methods[] = { {"select_flush", (PyCFunction)bpy_bmesh_select_flush, METH_O, bpy_bmesh_select_flush_doc}, {"normal_update", (PyCFunction)bpy_bmesh_normal_update, METH_NOARGS, bpy_bmesh_normal_update_doc}, {"transform", (PyCFunction)bpy_bmesh_transform, METH_VARARGS | METH_KEYWORDS, bpy_bmesh_transform_doc}, + + /* calculations */ + {"calc_volume", (PyCFunction)bpy_bmesh_calc_volume, METH_VARARGS | METH_KEYWORDS, bpy_bmesh_calc_volume_doc}, {NULL, NULL, 0, NULL} }; -- cgit v1.2.3