2d version of line/circle intersec function.
authorCampbell Barton <ideasman42@gmail.com>
Sun, 26 Jun 2011 11:08:12 +0000 (11:08 +0000)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 26 Jun 2011 11:08:12 +0000 (11:08 +0000)
source/blender/blenlib/BLI_math_geom.h
source/blender/blenlib/intern/math_geom.c
source/blender/makesrna/intern/rna_ID.c
source/blender/python/generic/mathutils_geometry.c

index c905ada184d626db284f515f6c4f6c534f0ff75c..506e271071cc7f8ba1a2558ca70fa2f3bb8f1d7b 100644 (file)
@@ -82,6 +82,7 @@ float line_point_factor_v2(const float p[2], const float l1[2], const float l2[2
 int isect_line_line_v2(const float a1[2], const float a2[2], const float b1[2], const float b2[2]);
 int isect_line_line_v2_int(const int a1[2], const int a2[2], const int b1[2], const int b2[2]);
 int isect_line_sphere_v3(const float l1[3], const float l2[3], const float sp[3], const float r, float r_p1[3], float r_p2[3]);
 int isect_line_line_v2(const float a1[2], const float a2[2], const float b1[2], const float b2[2]);
 int isect_line_line_v2_int(const int a1[2], const int a2[2], const int b1[2], const int b2[2]);
 int isect_line_sphere_v3(const float l1[3], const float l2[3], const float sp[3], const float r, float r_p1[3], float r_p2[3]);
+int isect_line_sphere_v2(const float l1[2], const float l2[2], const float sp[2], const float r, float r_p1[2], float r_p2[2]);
 int isect_seg_seg_v2_point(const float v1[2], const float v2[2], const float v3[2], const float v4[2], float vi[2]);
 
 /* Returns the number of point of interests
 int isect_seg_seg_v2_point(const float v1[2], const float v2[2], const float v3[2], const float v4[2], float vi[2]);
 
 /* Returns the number of point of interests
index a71b60896fc5b5195fa5d87d82e0010536ba68b6..9d945e9baa30268695f4d77871324c909658fcd6 100644 (file)
@@ -420,6 +420,60 @@ int isect_line_sphere_v3(const float l1[3], const float l2[3],
        }
 }
 
        }
 }
 
+/* keep in sync with isect_line_sphere_v3 */
+int isect_line_sphere_v2(const float l1[2], const float l2[2],
+                         const float sp[2], const float r,
+                         float r_p1[2], float r_p2[2])
+{
+       const float ldir[2]= {
+           l2[0] - l1[0],
+           l2[1] - l1[1]
+       };
+
+       const float a= dot_v3v3(ldir, ldir);
+
+       const float b= 2.0f *
+               (ldir[0] * (l1[0] - sp[0]) +
+                ldir[1] * (l1[1] - sp[1]));
+
+       const float c=
+               dot_v2v2(sp, sp) +
+               dot_v2v2(l1, l1) -
+               (2.0f * dot_v2v2(sp, l1)) -
+               (r * r);
+
+       const float i = b * b - 4.0f * a * c;
+
+       float mu;
+
+       if (i < 0.0f) {
+               /* no intersections */
+               return 0;
+       }
+       else if (i == 0.0f) {
+               /* one intersection */
+               mu = -b / (2.0f * a);
+               madd_v2_v2v2fl(r_p1, l1, ldir, mu);
+               return 1;
+       }
+       else if (i > 0.0) {
+               const float i_sqrt= sqrt(i); /* avoid calc twice */
+
+               /* first intersection */
+               mu = (-b + i_sqrt) / (2.0f * a);
+               madd_v2_v2v2fl(r_p1, l1, ldir, mu);
+
+               /* second intersection */
+               mu = (-b - i_sqrt) / (2.0f * a);
+               madd_v2_v2v2fl(r_p2, l1, ldir, mu);
+               return 2;
+       }
+       else {
+               /* math domain error - nan */
+               return -1;
+       }
+}
+
 /*
 -1: colliniar
  1: intersection
 /*
 -1: colliniar
  1: intersection
index 3877e1f95552e436560f237d1ddf66b0a1ecb52e..3ce84e3a19f91a9bc037541080bd3ae9ad728aca 100644 (file)
@@ -410,12 +410,12 @@ static void rna_def_ID_materials(BlenderRNA *brna)
        RNA_def_struct_ui_text(srna, "ID Materials", "Collection of materials");
 
        func= RNA_def_function(srna, "append", "material_append_id");
        RNA_def_struct_ui_text(srna, "ID Materials", "Collection of materials");
 
        func= RNA_def_function(srna, "append", "material_append_id");
-       RNA_def_function_ui_description(func, "Add a new material to Mesh.");
+       RNA_def_function_ui_description(func, "Add a new material to the data block.");
        parm= RNA_def_pointer(func, "material", "Material", "", "Material to add.");
        RNA_def_property_flag(parm, PROP_REQUIRED);
        
        func= RNA_def_function(srna, "pop", "material_pop_id");
        parm= RNA_def_pointer(func, "material", "Material", "", "Material to add.");
        RNA_def_property_flag(parm, PROP_REQUIRED);
        
        func= RNA_def_function(srna, "pop", "material_pop_id");
-       RNA_def_function_ui_description(func, "Remove a material from Mesh.");
+       RNA_def_function_ui_description(func, "Remove a material from the data block.");
        parm= RNA_def_int(func, "index", 0, 0, INT_MAX, "", "Index of material to remove.", 0, INT_MAX);
        RNA_def_property_flag(parm, PROP_REQUIRED);
        parm= RNA_def_pointer(func, "material", "Material", "", "Material to remove.");
        parm= RNA_def_int(func, "index", 0, 0, INT_MAX, "", "Index of material to remove.", 0, INT_MAX);
        RNA_def_property_flag(parm, PROP_REQUIRED);
        parm= RNA_def_pointer(func, "material", "Material", "", "Material to remove.");
index f3fe05cd846f2b9afb778da716777ae966fbf518..df7d3c66c47e9ec19a5e7d1acf7112bef529717d 100644 (file)
@@ -641,6 +641,88 @@ static PyObject *M_Geometry_intersect_line_sphere(PyObject *UNUSED(self), PyObje
        return ret;
 }
 
        return ret;
 }
 
+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"
+"   Takes a lines (as 2 vectors), a sphere as a point and a radius and\n"
+"   returns the intersection\n"
+"\n"
+"   :arg line_a: First point of the first line\n"
+"   :type line_a: :class:`mathutils.Vector`\n"
+"   :arg line_b: Second point of the first line\n"
+"   :type line_b: :class:`mathutils.Vector`\n"
+"   :arg sphere_co: The center of the sphere\n"
+"   :type sphere_co: :class:`mathutils.Vector`\n"
+"   :arg sphere_radius: Radius of the sphere\n"
+"   :type sphere_radius: sphere_radius\n"
+"   :return: The intersection points as a pair of vectors or None when there is no intersection\n"
+"   :rtype: A tuple pair containing :class:`mathutils.Vector` or None\n"
+);
+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];
+
+       if(!PyArg_ParseTuple(args, "O!O!O!f|i:intersect_line_sphere_2d",
+         &vector_Type, &line_a,
+         &vector_Type, &line_b,
+         &vector_Type, &sphere_co,
+         &sphere_radius, &clip)
+       ) {
+               return NULL;
+       }
+
+       if(             BaseMath_ReadCallback(line_a) == -1 ||
+               BaseMath_ReadCallback(line_b) == -1 ||
+               BaseMath_ReadCallback(sphere_co) == -1
+       ) {
+               return NULL;
+       }
+
+       ret= PyTuple_New(2);
+
+       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);
+       }
+
+       return ret;
+}
+
 PyDoc_STRVAR(M_Geometry_intersect_point_line_doc,
 ".. function:: intersect_point_line(pt, line_p1, line_p2)\n"
 "\n"
 PyDoc_STRVAR(M_Geometry_intersect_point_line_doc,
 ".. function:: intersect_point_line(pt, line_p1, line_p2)\n"
 "\n"
@@ -1006,6 +1088,7 @@ static PyMethodDef M_Geometry_methods[]= {
        {"intersect_line_line_2d", (PyCFunction) M_Geometry_intersect_line_line_2d, METH_VARARGS, M_Geometry_intersect_line_line_2d_doc},
        {"intersect_line_plane", (PyCFunction) M_Geometry_intersect_line_plane, METH_VARARGS, M_Geometry_intersect_line_plane_doc},
        {"intersect_line_sphere", (PyCFunction) M_Geometry_intersect_line_sphere, METH_VARARGS, M_Geometry_intersect_line_sphere_doc},
        {"intersect_line_line_2d", (PyCFunction) M_Geometry_intersect_line_line_2d, METH_VARARGS, M_Geometry_intersect_line_line_2d_doc},
        {"intersect_line_plane", (PyCFunction) M_Geometry_intersect_line_plane, METH_VARARGS, M_Geometry_intersect_line_plane_doc},
        {"intersect_line_sphere", (PyCFunction) M_Geometry_intersect_line_sphere, METH_VARARGS, M_Geometry_intersect_line_sphere_doc},
+       {"intersect_line_sphere_2d", (PyCFunction) M_Geometry_intersect_line_sphere_2d, METH_VARARGS, M_Geometry_intersect_line_sphere_2d_doc},
        {"interpolate_bezier", (PyCFunction) M_Geometry_interpolate_bezier, METH_VARARGS, M_Geometry_interpolate_bezier_doc},
        {"area_tri", (PyCFunction) M_Geometry_area_tri, METH_VARARGS, M_Geometry_area_tri_doc},
        {"normal", (PyCFunction) M_Geometry_normal, METH_VARARGS, M_Geometry_normal_doc},
        {"interpolate_bezier", (PyCFunction) M_Geometry_interpolate_bezier, METH_VARARGS, M_Geometry_interpolate_bezier_doc},
        {"area_tri", (PyCFunction) M_Geometry_area_tri, METH_VARARGS, M_Geometry_area_tri_doc},
        {"normal", (PyCFunction) M_Geometry_normal, METH_VARARGS, M_Geometry_normal_doc},