diff options
author | Campbell Barton <ideasman42@gmail.com> | 2011-06-26 21:16:06 +0400 |
---|---|---|
committer | Campbell Barton <ideasman42@gmail.com> | 2011-06-26 21:16:06 +0400 |
commit | 31f0b6639ac4f7f9748baea1844abefeeec64d7c (patch) | |
tree | 632e67e5ddb85a67f0a718c54fe7daa7cb8beb51 /source/blender/python | |
parent | 1186bdfc0864475be2395591b3b15c7a9d515a84 (diff) |
more compact code for recent sphere/line intersection functions.
Diffstat (limited to 'source/blender/python')
-rw-r--r-- | source/blender/python/generic/mathutils_geometry.c | 127 |
1 files changed, 54 insertions, 73 deletions
diff --git a/source/blender/python/generic/mathutils_geometry.c b/source/blender/python/generic/mathutils_geometry.c index df7d3c66c47..55c1e69d558 100644 --- a/source/blender/python/generic/mathutils_geometry.c +++ b/source/blender/python/generic/mathutils_geometry.c @@ -1,4 +1,4 @@ -/* +/* * $Id$ * * ***** BEGIN GPL LICENSE BLOCK ***** @@ -573,11 +573,9 @@ PyDoc_STRVAR(M_Geometry_intersect_line_sphere_doc, ); static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObject* args) { - PyObject *ret; VectorObject *line_a, *line_b, *sphere_co; float sphere_radius; int clip= TRUE; - float lambda; float isect_a[3]; float isect_b[3]; @@ -602,45 +600,38 @@ static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObje PyErr_SetString(PyExc_RuntimeError, "geometry.intersect_line_sphere(...) can't use 2D Vectors"); return NULL; } + else { + short use_a= TRUE; + short use_b= TRUE; + float lambda; + + PyObject *ret= PyTuple_New(2); + + switch(isect_line_sphere_v3(line_a->vec, line_b->vec, sphere_co->vec, sphere_radius, isect_a, isect_b)) { + case 1: + if(!(!clip || (((lambda= line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a= FALSE; + use_b= FALSE; + break; + case 2: + if(!(!clip || (((lambda= line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a= FALSE; + if(!(!clip || (((lambda= line_point_factor_v3(isect_b, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_b= FALSE; + break; + default: + use_a= FALSE; + use_b= FALSE; + } - ret= PyTuple_New(2); + if(use_a) { PyTuple_SET_ITEM(ret, 0, newVectorObject(isect_a, 3, Py_NEW, NULL)); } + else { PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); } - switch(isect_line_sphere_v3(line_a->vec, line_b->vec, sphere_co->vec, sphere_radius, isect_a, isect_b)) { - case 1: - /* ret 1 */ - if(!clip || (((lambda= line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f))) { - PyTuple_SET_ITEM(ret, 0, newVectorObject(isect_a, 3, Py_NEW, NULL)); - } - else { - PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); - } - /* ret 2 */ - PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); - break; - case 2: - /* ret 1 */ - if(!clip || (((lambda= line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f))) { - PyTuple_SET_ITEM(ret, 0, newVectorObject(isect_a, 3, Py_NEW, NULL)); - } - else { - PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); - } - /* ret 2 */ - if(!clip || (((lambda= line_point_factor_v3(isect_b, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f))) { - PyTuple_SET_ITEM(ret, 1, newVectorObject(isect_b, 3, Py_NEW, NULL)); - } - else { - PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); - } - break; - default: - PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); - PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); - } + if(use_b) { PyTuple_SET_ITEM(ret, 1, newVectorObject(isect_b, 3, Py_NEW, NULL)); } + else { PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); } - return ret; + return ret; + } } +/* keep in sync with M_Geometry_intersect_line_sphere */ PyDoc_STRVAR(M_Geometry_intersect_line_sphere_2d_doc, ".. function:: intersect_line_sphere_2d(line_a, line_b, sphere_co, sphere_radius, clip=True)\n" "\n" @@ -660,11 +651,9 @@ PyDoc_STRVAR(M_Geometry_intersect_line_sphere_2d_doc, ); static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyObject* args) { - PyObject *ret; VectorObject *line_a, *line_b, *sphere_co; float sphere_radius; int clip= TRUE; - float lambda; float isect_a[3]; float isect_b[3]; @@ -684,43 +673,35 @@ static PyObject *M_Geometry_intersect_line_sphere_2d(PyObject *UNUSED(self), PyO ) { return NULL; } + else { + short use_a= TRUE; + short use_b= TRUE; + float lambda; + + PyObject *ret= PyTuple_New(2); + + switch(isect_line_sphere_v3(line_a->vec, line_b->vec, sphere_co->vec, sphere_radius, isect_a, isect_b)) { + case 1: + if(!(!clip || (((lambda= line_point_factor_v2(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a= FALSE; + use_b= FALSE; + break; + case 2: + if(!(!clip || (((lambda= line_point_factor_v2(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_a= FALSE; + if(!(!clip || (((lambda= line_point_factor_v2(isect_b, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f)))) use_b= FALSE; + break; + default: + use_a= FALSE; + use_b= FALSE; + } - ret= PyTuple_New(2); + if(use_a) { PyTuple_SET_ITEM(ret, 0, newVectorObject(isect_a, 2, Py_NEW, NULL)); } + else { PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); } - switch(isect_line_sphere_v2(line_a->vec, line_b->vec, sphere_co->vec, sphere_radius, isect_a, isect_b)) { - case 1: - /* ret 1 */ - if(!clip || (((lambda= line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f))) { - PyTuple_SET_ITEM(ret, 0, newVectorObject(isect_a, 2, Py_NEW, NULL)); - } - else { - PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); - } - /* ret 2 */ - PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); - break; - case 2: - /* ret 1 */ - if(!clip || (((lambda= line_point_factor_v3(isect_a, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f))) { - PyTuple_SET_ITEM(ret, 0, newVectorObject(isect_a, 2, Py_NEW, NULL)); - } - else { - PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); - } - /* ret 2 */ - if(!clip || (((lambda= line_point_factor_v3(isect_b, line_a->vec, line_b->vec)) >= 0.0f) && (lambda <= 1.0f))) { - PyTuple_SET_ITEM(ret, 1, newVectorObject(isect_b, 2, Py_NEW, NULL)); - } - else { - PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); - } - break; - default: - PyTuple_SET_ITEM(ret, 0, Py_None); Py_INCREF(Py_None); - PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); - } + if(use_b) { PyTuple_SET_ITEM(ret, 1, newVectorObject(isect_b, 2, Py_NEW, NULL)); } + else { PyTuple_SET_ITEM(ret, 1, Py_None); Py_INCREF(Py_None); } - return ret; + return ret; + } } PyDoc_STRVAR(M_Geometry_intersect_point_line_doc, |