Changeset 671


Ignore:
Timestamp:
Dec 5, 2004, 8:21:43 PM (20 years ago)
Author:
ole
Message:

Implemented point_on_line in C.

Location:
inundation/ga/storm_surge/pyvolution
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/test_util.py

    r664 r671  
    391391               
    392392        #Endpoints first       
    393         assert point_on_line( (0, 0), [(0,0), (1,0)]
    394         assert point_on_line( (1, 0), [(0,0), (1,0)] )         
     393        assert point_on_line( 0, 0, 0,0, 1,0
     394        assert point_on_line( 1, 0, 0,0, 1,0 )         
    395395
    396396        #Then points on line   
    397         assert point_on_line( (0.5, 0), [(0,0), (1,0)] )
    398         assert point_on_line( (0, 0.5), [(0,1), (0,0)] )               
    399         assert point_on_line( (1, 0.5), [(1,1), (1,0)] )       
    400         assert point_on_line( (0.5, 0.5), [(0,0), (1,1)] )             
     397        assert point_on_line( 0.5, 0, 0,0, 1,0 )
     398        assert point_on_line( 0, 0.5, 0,1, 0,0 )               
     399        assert point_on_line( 1, 0.5, 1,1, 1,0 )       
     400        assert point_on_line( 0.5, 0.5, 0,0, 1,1 )             
    401401       
    402402        #Then points not on line               
    403         assert not point_on_line( (0.5, 0), [(0,1), (1,1)] )   
    404         assert not point_on_line( (0, 0.5), [(0,0), (1,1)] )
     403        assert not point_on_line( 0.5, 0, 0,1, 1,1 )   
     404        assert not point_on_line( 0, 0.5, 0,0, 1,1 )
    405405       
    406406        #From real example that failed
    407         assert not point_on_line( (40,50), [(40,20), (40,40)] )
     407        assert not point_on_line( 40,50, 40,20, 40,40 )
    408408       
    409409       
    410410        #From real example that failed
    411         assert not point_on_line( (40,19), [(40,20), (40,40)] )
     411        assert not point_on_line( 40,19, 40,20, 40,40 )
    412412                       
    413413       
  • inundation/ga/storm_surge/pyvolution/util.py

    r664 r671  
    4848
    4949
    50 def point_on_line(point, line):
     50#def point_on_line(point, line):
     51def point_on_line(x, y, x0, y0, x1, y1):
    5152    """Determine whether a point is on a line segment
    5253   
    53     Input:
    54         point: (x, y)
    55         line: [ (x0, y0), (x1, y1) ]
     54    Input: x, y, x0, x0, x1, y1: where
     55        point is given by x, y
     56        line is given by (x0, y0) and (x1, y1)
    5657       
    5758    """
     
    6162    from Numeric import array, dot, allclose
    6263    from math import sqrt
    63 
    64     x = point[0]
    65     y = point[1]   
    66    
    67     x0 = line[0][0]                     
    68     x1 = line[1][0]
    69     y0 = line[0][1]       
    70     y1 = line[1][1]
    7164
    7265    a = array([x - x0, y - y0])
     
    173166           
    174167            #Check for case where point is contained in line segment   
    175             if point_on_line( (x,y), [ [px[i], py[i]], [px[j], py[j]] ]):
     168            ##if point_on_line( (x,y), [ [px[i], py[i]], [px[j], py[j]] ]):
     169            if point_on_line(x, y, px[i], py[i], px[j], py[j]):
    176170                if closed:
    177171                    inside = True
     
    515509import compile
    516510if compile.can_use_C_extension('util_ext.c'):
    517     from util_ext import gradient
     511    from util_ext import gradient, point_on_line
    518512else:
    519513    gradient = gradient_python
  • inundation/ga/storm_surge/pyvolution/util_ext.c

    r258 r671  
    1818
    1919
    20 // Gateway to Python
     20
     21int _point_on_line(double x, double y,
     22                   double x0, double y0,
     23                   double x1, double y1) {
     24  /*Determine whether a point is on a line segment
     25   
     26    Input: x, y, x0, x0, x1, y1: where
     27        point is given by x, y
     28        line is given by (x0, y0) and (x1, y1)
     29       
     30  */
     31   
     32  double a0, a1, a_normal0, a_normal1, b0, b1, len_a, len_b;
     33 
     34  a0 = x - x0;
     35  a1 = y - y0;
     36 
     37  a_normal0 = a1;
     38  a_normal1 = -a0;
     39                       
     40  b0 = x1 - x0;
     41  b1 = y1 - y0;
     42   
     43  if ( a_normal0*b0 + a_normal1*b1 == 0 ) {   
     44    //Point is somewhere on the infinite extension of the line
     45
     46    len_a = sqrt(a0*a0 + a1*a1);               
     47    len_b = sqrt(b0*b0 + b1*b1);
     48                                       
     49    if (a0*b0 + a1*b1 >= 0 && len_a <= len_b) {
     50      return 1;
     51    } else {
     52      return 0;
     53    }
     54  } else {
     55    return 0;
     56  }
     57}
     58
     59
     60
     61// Gateways to Python
     62PyObject *point_on_line(PyObject *self, PyObject *args) {
     63  //
     64  // point_on_line(x, y, x0, y0, x1, y1)
     65  //
     66 
     67  double x, y, x0, y0, x1, y1;
     68  int res;
     69  PyObject *result;
     70 
     71  // Convert Python arguments to C 
     72  if (!PyArg_ParseTuple(args, "dddddd", &x, &y, &x0, &y0, &x1, &y1)) 
     73    return NULL;
     74
     75 
     76  // Call underlying routine
     77  res = _point_on_line(x, y, x0, y0, x1, y1);
     78
     79  // Return values a and b
     80  result = Py_BuildValue("i", res);
     81  return result;
     82}
     83
     84
    2185PyObject *gradient(PyObject *self, PyObject *args) {
    2286  //
     
    52116  //{"rotate", (PyCFunction)rotate, METH_VARARGS | METH_KEYWORDS, "Print out"}, 
    53117  {"gradient", gradient, METH_VARARGS, "Print out"},   
     118  {"point_on_line", point_on_line, METH_VARARGS, "Print out"},     
    54119  {NULL, NULL, 0, NULL}   /* sentinel */
    55120};
Note: See TracChangeset for help on using the changeset viewer.