Changeset 8028


Ignore:
Timestamp:
Oct 7, 2010, 2:49:29 PM (13 years ago)
Author:
habili
Message:

added _polygon_triangle_overlap to determine if a polygon and a triangle overlap.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/anuga_core/source/anuga/utilities/polygon_ext.c

    r8025 r8028  
    315315
    316316
     317int __polygon_triangle_overlap(double* polygon,
     318                               double* triangle)
     319{
     320    int i, ii, j, jj;
     321    double p0_x, p0_y, p1_x, p1_y, pp_x, pp_y;
     322    double t0_x, t0_y, t1_x, t1_y, tp_x, tp_y;
     323    double u_x, u_y, v_x, v_y, w_x, w_y;
     324    double u_dot_tp, v_dot_tp, v_dot_pp, w_dot_pp;
     325    double a, b;
     326   
     327    p0_x = polygon[0];
     328    p0_y = polygon[1];
     329   
     330    for (i = 1; i < 5; i++)
     331    {
     332        ii = i%4;
     333       
     334        p1_x = polygon[2*ii];
     335        p1_y = polygon[2*ii + 1];
     336       
     337        pp_x = -(p1_y - p0_y);
     338        pp_y = p1_x - p0_x;
     339 
     340        t0_x = triangle[0];
     341        t0_y = triangle[1];
     342 
     343        for (j = 1; j < 4; j++)
     344        {
     345            jj = j%3;
     346                     
     347            t1_x = triangle[2*jj];
     348            t1_y = triangle[2*jj + 1];
     349           
     350            tp_x = -(t1_y - t0_y); //perpendicular to triangle vector
     351            tp_y = t1_x - t0_x;
     352       
     353            u_x = p1_x - p0_x;
     354            u_y = p1_y - p0_y;
     355            v_x = t0_x - p0_x;
     356            v_y = t0_y - p0_y;
     357            w_x = t1_x - t0_x;
     358            w_y = t1_y - t0_y;
     359
     360            u_dot_tp = (u_x*tp_x) + (u_y*tp_y);
     361           
     362            if (u_dot_tp != 0.0f) //Vectors are not parallel
     363            {
     364                v_dot_tp = (v_x*tp_x) + (v_y*tp_y);
     365                v_dot_pp = (v_x*pp_x) + (v_y*pp_y);
     366                w_dot_pp = (w_x*pp_x) + (w_y*pp_y);
     367               
     368                a = v_dot_tp/u_dot_tp;
     369                b = v_dot_pp/w_dot_pp;
     370               
     371                if (a >= 0.0f && a <= 1.0f)
     372                {
     373                    return 1; //overlap
     374                }
     375               
     376                if (b >= 0.0f && b <= 1.0f)
     377                {
     378                    return 1; //overlap
     379                }
     380            }
     381           
     382            t0_x = t1_x;
     383            t0_y = t1_y;
     384        }
     385       
     386        p0_x = p1_x;
     387        p0_y = p1_y;
     388    }
     389
     390    return 0; //no overlap
     391}
     392                               
     393
     394
    317395int __is_inside_triangle(double* point,
    318396                         double* triangle,
     
    575653
    576654
     655PyObject *_polygon_triangle_overlap(PyObject *self, PyObject *args) {
     656  //
     657  // _polygon_triangle_overlap(polygon, triangle)
     658  //
     659
     660 
     661  PyArrayObject
     662    *polygon,
     663    *triangle;
     664   
     665    int res;
     666
     667  PyObject *result;
     668     
     669  // Convert Python arguments to C
     670  if (!PyArg_ParseTuple(args, "OO",
     671                        &polygon,
     672                        &triangle)) {
     673   
     674    PyErr_SetString(PyExc_RuntimeError,
     675                    "_polygon_triangle_overlap could not parse input");
     676    return NULL;
     677  }
     678
     679  // Call underlying routine
     680  res = __polygon_triangle_overlap((double*) polygon->data,
     681                             (double*) triangle -> data);                                                     
     682
     683
     684  // Return result
     685  result = Py_BuildValue("i", res);
     686  return result; 
     687}
    577688
    578689     
Note: See TracChangeset for help on using the changeset viewer.