4 * ***** BEGIN GPL LICENSE BLOCK *****
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * This is a new part of Blender.
25 * Contributor(s): Joseph Gilbert, Campbell Barton
27 * ***** END GPL LICENSE BLOCK *****
32 /* - Not needed for now though other geometry functions will probably need them
33 #include "BLI_arithb.h"
34 #include "BKE_utildefines.h"
37 /* Used for PolyFill */
38 #include "BKE_displist.h"
39 #include "MEM_guardedalloc.h"
40 #include "BLI_blenlib.h"
42 #include "BKE_utildefines.h"
43 #include "BKE_curve.h"
44 #include "BLI_boxpack2d.h"
45 #include "BLI_arithb.h"
47 #define SWAP_FLOAT(a,b,tmp) tmp=a; a=b; b=tmp
50 /*-- forward declarations -- */
51 static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq );
52 static PyObject *M_Geometry_LineIntersect2D( PyObject * self, PyObject * args );
53 static PyObject *M_Geometry_ClosestPointOnLine( PyObject * self, PyObject * args );
54 static PyObject *M_Geometry_PointInTriangle2D( PyObject * self, PyObject * args );
55 static PyObject *M_Geometry_PointInQuad2D( PyObject * self, PyObject * args );
56 static PyObject *M_Geometry_BoxPack2D( PyObject * self, PyObject * args );
57 static PyObject *M_Geometry_BezierInterp( PyObject * self, PyObject * args );
60 /*-------------------------DOC STRINGS ---------------------------*/
61 static char M_Geometry_doc[] = "The Blender Geometry module\n\n";
62 static char M_Geometry_PolyFill_doc[] = "(veclist_list) - takes a list of polylines (each point a vector) and returns the point indicies for a polyline filled with triangles";
63 static char M_Geometry_LineIntersect2D_doc[] = "(lineA_p1, lineA_p2, lineB_p1, lineB_p2) - takes 2 lines (as 4 vectors) and returns a vector for their point of intersection or None";
64 static char M_Geometry_ClosestPointOnLine_doc[] = "(pt, line_p1, line_p2) - takes a point and a line and returns a (Vector, float) for the point on the line, and the bool so you can know if the point was between the 2 points";
65 static char M_Geometry_PointInTriangle2D_doc[] = "(pt, tri_p1, tri_p2, tri_p3) - takes 4 vectors, one is the point and the next 3 define the triangle, only the x and y are used from the vectors";
66 static char M_Geometry_PointInQuad2D_doc[] = "(pt, quad_p1, quad_p2, quad_p3, quad_p4) - takes 5 vectors, one is the point and the next 4 define the quad, only the x and y are used from the vectors";
67 static char M_Geometry_BoxPack2D_doc[] = "";
68 static char M_Geometry_BezierInterp_doc[] = "";
69 /*-----------------------METHOD DEFINITIONS ----------------------*/
70 struct PyMethodDef M_Geometry_methods[] = {
71 {"PolyFill", ( PyCFunction ) M_Geometry_PolyFill, METH_O, M_Geometry_PolyFill_doc},
72 {"LineIntersect2D", ( PyCFunction ) M_Geometry_LineIntersect2D, METH_VARARGS, M_Geometry_LineIntersect2D_doc},
73 {"ClosestPointOnLine", ( PyCFunction ) M_Geometry_ClosestPointOnLine, METH_VARARGS, M_Geometry_ClosestPointOnLine_doc},
74 {"PointInTriangle2D", ( PyCFunction ) M_Geometry_PointInTriangle2D, METH_VARARGS, M_Geometry_PointInTriangle2D_doc},
75 {"PointInQuad2D", ( PyCFunction ) M_Geometry_PointInQuad2D, METH_VARARGS, M_Geometry_PointInQuad2D_doc},
76 {"BoxPack2D", ( PyCFunction ) M_Geometry_BoxPack2D, METH_O, M_Geometry_BoxPack2D_doc},
77 {"BezierInterp", ( PyCFunction ) M_Geometry_BezierInterp, METH_VARARGS, M_Geometry_BezierInterp_doc},
81 #if (PY_VERSION_HEX >= 0x03000000)
82 static struct PyModuleDef M_Geometry_module_def = {
83 PyModuleDef_HEAD_INIT,
84 "Geometry", /* m_name */
85 M_Geometry_doc, /* m_doc */
87 M_Geometry_methods, /* m_methods */
95 /*----------------------------MODULE INIT-------------------------*/
96 PyObject *Geometry_Init(const char *from)
100 #if (PY_VERSION_HEX >= 0x03000000)
101 submodule = PyModule_Create(&M_Geometry_module_def);
102 PyDict_SetItemString(PySys_GetObject("modules"), M_Geometry_module_def.m_name, submodule);
104 submodule = Py_InitModule3(from, M_Geometry_methods, M_Geometry_doc);
110 /*----------------------------------Geometry.PolyFill() -------------------*/
111 /* PolyFill function, uses Blenders scanfill to fill multiple poly lines */
112 static PyObject *M_Geometry_PolyFill( PyObject * self, PyObject * polyLineSeq )
114 PyObject *tri_list; /*return this list of tri's */
115 PyObject *polyLine, *polyVec;
116 int i, len_polylines, len_polypoints, ls_error = 0;
118 /* display listbase */
119 ListBase dispbase={NULL, NULL};
121 float *fp; /*pointer to the array of malloced dl->verts to set the points from the vectors */
122 int index, *dl_face, totpoints=0;
125 dispbase.first= dispbase.last= NULL;
128 if(!PySequence_Check(polyLineSeq)) {
129 PyErr_SetString( PyExc_TypeError, "expected a sequence of poly lines" );
133 len_polylines = PySequence_Size( polyLineSeq );
135 for( i = 0; i < len_polylines; ++i ) {
136 polyLine= PySequence_GetItem( polyLineSeq, i );
137 if (!PySequence_Check(polyLine)) {
138 freedisplist(&dispbase);
139 Py_XDECREF(polyLine); /* may be null so use Py_XDECREF*/
140 PyErr_SetString( PyExc_TypeError, "One or more of the polylines is not a sequence of Mathutils.Vector's" );
144 len_polypoints= PySequence_Size( polyLine );
145 if (len_polypoints>0) { /* dont bother adding edges as polylines */
147 if (EXPP_check_sequence_consistency( polyLine, &vector_Type ) != 1) {
148 freedisplist(&dispbase);
150 PyErr_SetString( PyExc_TypeError, "A point in one of the polylines is not a Mathutils.Vector type" );
154 dl= MEM_callocN(sizeof(DispList), "poly disp");
155 BLI_addtail(&dispbase, dl);
157 dl->nr= len_polypoints;
159 dl->parts= 1; /* no faces, 1 edge loop */
160 dl->col= 0; /* no material */
161 dl->verts= fp= MEM_callocN( sizeof(float)*3*len_polypoints, "dl verts");
162 dl->index= MEM_callocN(sizeof(int)*3*len_polypoints, "dl index");
164 for( index = 0; index<len_polypoints; ++index, fp+=3) {
165 polyVec= PySequence_GetItem( polyLine, index );
166 if(VectorObject_Check(polyVec)) {
168 if(!BaseMath_ReadCallback((VectorObject *)polyVec))
171 fp[0] = ((VectorObject *)polyVec)->vec[0];
172 fp[1] = ((VectorObject *)polyVec)->vec[1];
173 if( ((VectorObject *)polyVec)->size > 2 )
174 fp[2] = ((VectorObject *)polyVec)->vec[2];
176 fp[2]= 0.0f; /* if its a 2d vector then set the z to be zero */
190 freedisplist(&dispbase); /* possible some dl was allocated */
191 PyErr_SetString( PyExc_TypeError, "A point in one of the polylines is not a Mathutils.Vector type" );
194 else if (totpoints) {
195 /* now make the list to return */
196 filldisplist(&dispbase, &dispbase);
198 /* The faces are stored in a new DisplayList
199 thats added to the head of the listbase */
202 tri_list= PyList_New(dl->parts);
204 freedisplist(&dispbase);
205 PyErr_SetString( PyExc_RuntimeError, "Geometry.PolyFill failed to make a new list" );
211 while(index < dl->parts) {
212 PyList_SetItem(tri_list, index, Py_BuildValue("iii", dl_face[0], dl_face[1], dl_face[2]) );
216 freedisplist(&dispbase);
218 /* no points, do this so scripts dont barf */
219 freedisplist(&dispbase); /* possible some dl was allocated */
220 tri_list= PyList_New(0);
227 static PyObject *M_Geometry_LineIntersect2D( PyObject * self, PyObject * args )
229 VectorObject *line_a1, *line_a2, *line_b1, *line_b2;
230 float a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y, xi, yi, a1,a2,b1,b2, newvec[2];
231 if( !PyArg_ParseTuple ( args, "O!O!O!O!",
232 &vector_Type, &line_a1,
233 &vector_Type, &line_a2,
234 &vector_Type, &line_b1,
235 &vector_Type, &line_b2)
237 PyErr_SetString( PyExc_TypeError, "expected 4 vector types\n" );
241 if(!BaseMath_ReadCallback(line_a1) || !BaseMath_ReadCallback(line_a2) || !BaseMath_ReadCallback(line_b1) || !BaseMath_ReadCallback(line_b2))
244 a1x= line_a1->vec[0];
245 a1y= line_a1->vec[1];
246 a2x= line_a2->vec[0];
247 a2y= line_a2->vec[1];
249 b1x= line_b1->vec[0];
250 b1y= line_b1->vec[1];
251 b2x= line_b2->vec[0];
252 b2y= line_b2->vec[1];
254 if((MIN2(a1x, a2x) > MAX2(b1x, b2x)) ||
255 (MAX2(a1x, a2x) < MIN2(b1x, b2x)) ||
256 (MIN2(a1y, a2y) > MAX2(b1y, b2y)) ||
257 (MAX2(a1y, a2y) < MIN2(b1y, b2y)) ) {
260 /* Make sure the hoz/vert line comes first. */
261 if (fabs(b1x - b2x) < eul || fabs(b1y - b2y) < eul) {
262 SWAP_FLOAT(a1x, b1x, xi); /*abuse xi*/
263 SWAP_FLOAT(a1y, b1y, xi);
264 SWAP_FLOAT(a2x, b2x, xi);
265 SWAP_FLOAT(a2y, b2y, xi);
268 if (fabs(a1x-a2x) < eul) { /* verticle line */
269 if (fabs(b1x-b2x) < eul){ /*verticle second line */
270 Py_RETURN_NONE; /* 2 verticle lines dont intersect. */
272 else if (fabs(b1y-b2y) < eul) {
273 /*X of vert, Y of hoz. no calculation needed */
276 return newVectorObject(newvec, 2, Py_NEW);
279 yi = (float)(((b1y / fabs(b1x - b2x)) * fabs(b2x - a1x)) + ((b2y / fabs(b1x - b2x)) * fabs(b1x - a1x)));
281 if (yi > MAX2(a1y, a2y)) {/* New point above seg1's vert line */
283 } else if (yi < MIN2(a1y, a2y)) { /* New point below seg1's vert line */
288 return newVectorObject(newvec, 2, Py_NEW);
289 } else if (fabs(a2y-a1y) < eul) { /* hoz line1 */
290 if (fabs(b2y-b1y) < eul) { /*hoz line2*/
291 Py_RETURN_NONE; /*2 hoz lines dont intersect*/
294 /* Can skip vert line check for seg 2 since its covered above. */
295 xi = (float)(((b1x / fabs(b1y - b2y)) * fabs(b2y - a1y)) + ((b2x / fabs(b1y - b2y)) * fabs(b1y - a1y)));
296 if (xi > MAX2(a1x, a2x)) { /* New point right of hoz line1's */
298 } else if (xi < MIN2(a1x, a2x)) { /*New point left of seg1's hoz line */
303 return newVectorObject(newvec, 2, Py_NEW);
306 b1 = (a2y-a1y)/(a2x-a1x);
307 b2 = (b2y-b1y)/(b2x-b1x);
311 if (b1 - b2 == 0.0) {
315 xi = - (a1-a2)/(b1-b2);
317 if ((a1x-xi)*(xi-a2x) >= 0 && (b1x-xi)*(xi-b2x) >= 0 && (a1y-yi)*(yi-a2y) >= 0 && (b1y-yi)*(yi-b2y)>=0) {
320 return newVectorObject(newvec, 2, Py_NEW);
325 static PyObject *M_Geometry_ClosestPointOnLine( PyObject * self, PyObject * args )
327 VectorObject *pt, *line_1, *line_2;
328 float pt_in[3], pt_out[3], l1[3], l2[3];
332 if( !PyArg_ParseTuple ( args, "O!O!O!",
334 &vector_Type, &line_1,
335 &vector_Type, &line_2)
337 PyErr_SetString( PyExc_TypeError, "expected 3 vector types\n" );
341 if(!BaseMath_ReadCallback(pt) || !BaseMath_ReadCallback(line_1) || !BaseMath_ReadCallback(line_2))
344 /* accept 2d verts */
345 if (pt->size==3) { VECCOPY(pt_in, pt->vec);}
346 else { pt_in[2]=0.0; VECCOPY2D(pt_in, pt->vec) }
348 if (line_1->size==3) { VECCOPY(l1, line_1->vec);}
349 else { l1[2]=0.0; VECCOPY2D(l1, line_1->vec) }
351 if (line_2->size==3) { VECCOPY(l2, line_2->vec);}
352 else { l2[2]=0.0; VECCOPY2D(l2, line_2->vec) }
354 /* do the calculation */
355 lambda = lambda_cp_line_ex(pt_in, l1, l2, pt_out);
357 ret = PyTuple_New(2);
358 PyTuple_SET_ITEM( ret, 0, newVectorObject(pt_out, 3, Py_NEW) );
359 PyTuple_SET_ITEM( ret, 1, PyFloat_FromDouble(lambda) );
363 static PyObject *M_Geometry_PointInTriangle2D( PyObject * self, PyObject * args )
365 VectorObject *pt_vec, *tri_p1, *tri_p2, *tri_p3;
367 if( !PyArg_ParseTuple ( args, "O!O!O!O!",
368 &vector_Type, &pt_vec,
369 &vector_Type, &tri_p1,
370 &vector_Type, &tri_p2,
371 &vector_Type, &tri_p3)
373 PyErr_SetString( PyExc_TypeError, "expected 4 vector types\n" );
377 if(!BaseMath_ReadCallback(pt_vec) || !BaseMath_ReadCallback(tri_p1) || !BaseMath_ReadCallback(tri_p2) || !BaseMath_ReadCallback(tri_p3))
380 return PyLong_FromLong(IsectPT2Df(pt_vec->vec, tri_p1->vec, tri_p2->vec, tri_p3->vec));
383 static PyObject *M_Geometry_PointInQuad2D( PyObject * self, PyObject * args )
385 VectorObject *pt_vec, *quad_p1, *quad_p2, *quad_p3, *quad_p4;
387 if( !PyArg_ParseTuple ( args, "O!O!O!O!O!",
388 &vector_Type, &pt_vec,
389 &vector_Type, &quad_p1,
390 &vector_Type, &quad_p2,
391 &vector_Type, &quad_p3,
392 &vector_Type, &quad_p4)
394 PyErr_SetString( PyExc_TypeError, "expected 5 vector types\n" );
398 if(!BaseMath_ReadCallback(pt_vec) || !BaseMath_ReadCallback(quad_p1) || !BaseMath_ReadCallback(quad_p2) || !BaseMath_ReadCallback(quad_p3) || !BaseMath_ReadCallback(quad_p4))
401 return PyLong_FromLong(IsectPQ2Df(pt_vec->vec, quad_p1->vec, quad_p2->vec, quad_p3->vec, quad_p4->vec));
404 static int boxPack_FromPyObject(PyObject * value, boxPack **boxarray )
407 PyObject *list_item, *item_1, *item_2;
411 /* Error checking must alredy be done */
412 if( !PyList_Check( value ) ) {
413 PyErr_SetString( PyExc_TypeError, "can only back a list of [x,y,x,w]" );
417 len = PyList_Size( value );
419 (*boxarray) = MEM_mallocN( len*sizeof(boxPack), "boxPack box");
422 for( i = 0; i < len; i++ ) {
423 list_item = PyList_GET_ITEM( value, i );
424 if( !PyList_Check( list_item ) || PyList_Size( list_item ) < 4 ) {
425 MEM_freeN(*boxarray);
426 PyErr_SetString( PyExc_TypeError, "can only back a list of [x,y,x,w]" );
432 item_1 = PyList_GET_ITEM(list_item, 2);
433 item_2 = PyList_GET_ITEM(list_item, 3);
435 if (!PyNumber_Check(item_1) || !PyNumber_Check(item_2)) {
436 MEM_freeN(*boxarray);
437 PyErr_SetString( PyExc_TypeError, "can only back a list of 2d boxes [x,y,x,w]" );
441 box->w = (float)PyFloat_AsDouble( item_1 );
442 box->h = (float)PyFloat_AsDouble( item_2 );
444 /* verts will be added later */
449 static void boxPack_ToPyObject(PyObject * value, boxPack **boxarray)
455 len = PyList_Size( value );
457 for( i = 0; i < len; i++ ) {
459 list_item = PyList_GET_ITEM( value, box->index );
460 PyList_SET_ITEM( list_item, 0, PyFloat_FromDouble( box->x ));
461 PyList_SET_ITEM( list_item, 1, PyFloat_FromDouble( box->y ));
463 MEM_freeN(*boxarray);
467 static PyObject *M_Geometry_BoxPack2D( PyObject * self, PyObject * boxlist )
469 boxPack *boxarray = NULL;
470 float tot_width, tot_height;
474 if(!PyList_Check(boxlist)) {
475 PyErr_SetString( PyExc_TypeError, "expected a sequence of boxes [[x,y,w,h], ... ]" );
479 len = PyList_Size( boxlist );
482 return Py_BuildValue( "ff", 0.0, 0.0);
484 error = boxPack_FromPyObject(boxlist, &boxarray);
485 if (error!=0) return NULL;
487 /* Non Python function */
488 boxPack2D(boxarray, len, &tot_width, &tot_height);
490 boxPack_ToPyObject(boxlist, &boxarray);
492 return Py_BuildValue( "ff", tot_width, tot_height);
495 static PyObject *M_Geometry_BezierInterp( PyObject * self, PyObject * args )
497 VectorObject *vec_k1, *vec_h1, *vec_k2, *vec_h2;
501 float *coord_array, *fp;
504 float k1[4] = {0.0, 0.0, 0.0, 0.0};
505 float h1[4] = {0.0, 0.0, 0.0, 0.0};
506 float k2[4] = {0.0, 0.0, 0.0, 0.0};
507 float h2[4] = {0.0, 0.0, 0.0, 0.0};
510 if( !PyArg_ParseTuple ( args, "O!O!O!O!i",
511 &vector_Type, &vec_k1,
512 &vector_Type, &vec_h1,
513 &vector_Type, &vec_h2,
514 &vector_Type, &vec_k2, &resolu) || (resolu<=1)
516 PyErr_SetString( PyExc_TypeError, "expected 4 vector types and an int greater then 1\n" );
520 if(!BaseMath_ReadCallback(vec_k1) || !BaseMath_ReadCallback(vec_h1) || !BaseMath_ReadCallback(vec_k2) || !BaseMath_ReadCallback(vec_h2))
523 dims= MAX4(vec_k1->size, vec_h1->size, vec_h2->size, vec_k2->size);
525 for(i=0; i < vec_k1->size; i++) k1[i]= vec_k1->vec[i];
526 for(i=0; i < vec_h1->size; i++) h1[i]= vec_h1->vec[i];
527 for(i=0; i < vec_k2->size; i++) k2[i]= vec_k2->vec[i];
528 for(i=0; i < vec_h2->size; i++) h2[i]= vec_h2->vec[i];
530 coord_array = MEM_callocN(dims * (resolu) * sizeof(float), "BezierInterp");
531 for(i=0; i<dims; i++) {
532 forward_diff_bezier(k1[i], h1[i], h2[i], k2[i], coord_array+i, resolu-1, dims);
535 list= PyList_New(resolu);
537 for(i=0; i<resolu; i++, fp= fp+dims) {
538 PyList_SET_ITEM(list, i, newVectorObject(fp, dims, Py_NEW));
540 MEM_freeN(coord_array);