Changeset 8030


Ignore:
Timestamp:
Oct 10, 2010, 5:13:03 PM (13 years ago)
Author:
habili
Message:

Modified polygon overlap algorithm and modified name to _polygon_overlap

File:
1 edited

Legend:

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

    r8029 r8030  
    315315
    316316
    317 int __polygon_triangle_overlap(double* polygon,
     317int __triangle_polygon_overlap(double* polygon,
    318318                               double* triangle,
    319319                               int polygon_number_of_vertices)
    320320{
    321     int i, ii, j, jj;
     321    int i, ii, j, jj, A, B;
    322322    double p0_x, p0_y, p1_x, p1_y, pp_x, pp_y;
    323323    double t0_x, t0_y, t1_x, t1_y, tp_x, tp_y;
     
    328328    p0_x = polygon[0];
    329329    p0_y = polygon[1];
     330   
     331    A = 0;
     332    B = 0;
    330333   
    331334    for (i = 1; i < polygon_number_of_vertices + 1; i++)
     
    350353           
    351354            tp_x = -(t1_y - t0_y); //perpendicular to triangle vector
    352             tp_y = t1_x - t0_x;
     355            tp_y = t1_x - t0_x; //perpendicular to polygon vector
    353356       
    354357            u_x = p1_x - p0_x;
     
    368371               
    369372                a = v_dot_tp/u_dot_tp;
    370                 b = v_dot_pp/w_dot_pp;
    371                
    372                 if (a >= 0.0f && a <= 1.0f)
     373                b = -v_dot_pp/w_dot_pp;
     374                             
     375                if (a >= 0.0f && a <= 1.0f && b >=0.0f && b <=1.0f)
    373376                {
    374377                    return 1; //overlap
    375378                }
    376379               
    377                 if (b >= 0.0f && b <= 1.0f)
     380                if (b >= 0.0f && b <= 1.0f && a > 1.0f)
     381                {
     382                    A++;
     383                }
     384               
     385                if (a >= 0.0f && a <= 1.0f && b > 1.0f)
     386                {
     387                    B++;
     388                }
     389               
     390                if (A == 4 || B == 3)
    378391                {
    379392                    return 1; //overlap
     
    391404    return 0; //no overlap
    392405}
    393                                
     406                 
     407
     408int __polygon_overlap(double* polygon,
     409                      double* triangles,
     410                      long* indices,
     411                      int M, //number of triangles
     412                      int polygon_number_of_vertices)
     413{
     414    double* triangle;
     415    int i, inside_index, outside_index;
     416   
     417    inside_index = 0;    // Keep track of triangles that overlap
     418    outside_index = M - 1; // Keep track of triangles that don't overlap (starting from end)
     419   
     420    for (i = 0; i < M; i++)
     421    {
     422        triangle = triangles + 6*i;
     423       
     424        if (__triangle_polygon_overlap(polygon,
     425                                      triangle,
     426                                      polygon_number_of_vertices))
     427        {
     428            indices[inside_index] = i;
     429            inside_index++;
     430        }
     431        else
     432        {
     433            indices[outside_index] = i;
     434            outside_index -= 1;           
     435        }
     436    }
     437   
     438    return inside_index;
     439}             
    394440
    395441
     
    654700
    655701
    656 PyObject *_polygon_triangle_overlap(PyObject *self, PyObject *args) {
     702PyObject *_polygon_overlap(PyObject *self, PyObject *args)
     703{
    657704  //
    658705  // _polygon_triangle_overlap(polygon, triangle)
     
    662709  PyArrayObject
    663710    *polygon,
    664     *triangle;
     711    *triangles,
     712    *indices;
    665713   
    666714    int res;
     
    669717     
    670718  // Convert Python arguments to C
    671   if (!PyArg_ParseTuple(args, "OO",
     719  if (!PyArg_ParseTuple(args, "OOO",
    672720                        &polygon,
    673                         &triangle)) {
     721                        &triangles,
     722            &indices)) {
    674723   
    675724    PyErr_SetString(PyExc_RuntimeError,
     
    679728
    680729  // Call underlying routine
    681   res = __polygon_triangle_overlap((double*) polygon->data,
    682                              (double*) triangle -> data,
     730  res = __polygon_overlap((double*) polygon->data,
     731                             (double*) triangles->data,
     732                 (long*) indices->data,
     733                 (int) triangles->dimensions[0]/3,
    683734                 (int) polygon->dimensions[0]);                                               
    684735
     
    860911  {"_interpolate_polyline", _interpolate_polyline,
    861912                                 METH_VARARGS, "Print out"},                             
     913  {"_polygon_overlap", _polygon_overlap,
     914                                 METH_VARARGS, "Print out"},
    862915  {"_is_inside_triangle", _is_inside_triangle,
    863916                                 METH_VARARGS, "Print out"},
     
    866919
    867920
    868 
    869921// Module initialisation
    870922void initpolygon_ext(void){
Note: See TracChangeset for help on using the changeset viewer.