Ignore:
Timestamp:
Jan 14, 2009, 9:48:37 AM (15 years ago)
Author:
rwilson
Message:

Change Numeric imports to general form - ready to change to NumPy?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • anuga_core/source/anuga/utilities/polygon.py

    r6119 r6158  
    11#!/usr/bin/env python
    22"""Polygon manipulations
    3 
    43"""
    54
    6 
    7 #try:
    8 #    from scipy import Float, Int, zeros, ones, array, concatenate, reshape, dot
    9 #except:
    10 #    #print 'Could not find scipy - using Numeric'
    11 
    12 from Numeric import Float, Int, zeros, ones, array, concatenate, reshape, dot, allclose
    13 
     5import Numeric as num
    146
    157from math import sqrt
     
    4941# result functions for possible states
    5042def lines_dont_coincide(p0,p1,p2,p3):               return (3, None)
    51 def lines_0_fully_included_in_1(p0,p1,p2,p3):       return (2, array([p0,p1]))
    52 def lines_1_fully_included_in_0(p0,p1,p2,p3):       return (2, array([p2,p3]))
    53 def lines_overlap_same_direction(p0,p1,p2,p3):      return (2, array([p0,p3]))
    54 def lines_overlap_same_direction2(p0,p1,p2,p3):     return (2, array([p2,p1]))
    55 def lines_overlap_opposite_direction(p0,p1,p2,p3):  return (2, array([p0,p2]))
    56 def lines_overlap_opposite_direction2(p0,p1,p2,p3): return (2, array([p3,p1]))
     43def lines_0_fully_included_in_1(p0,p1,p2,p3):       return (2, num.array([p0,p1]))
     44def lines_1_fully_included_in_0(p0,p1,p2,p3):       return (2, num.array([p2,p3]))
     45def lines_overlap_same_direction(p0,p1,p2,p3):      return (2, num.array([p0,p3]))
     46def lines_overlap_same_direction2(p0,p1,p2,p3):     return (2, num.array([p2,p1]))
     47def lines_overlap_opposite_direction(p0,p1,p2,p3):  return (2, num.array([p0,p2]))
     48def lines_overlap_opposite_direction2(p0,p1,p2,p3): return (2, num.array([p3,p1]))
    5749
    5850# this function called when an impossible state is found
     
    108100    # FIXME (Ole): Write this in C
    109101
    110     line0 = ensure_numeric(line0, Float)
    111     line1 = ensure_numeric(line1, Float)   
     102    line0 = ensure_numeric(line0, num.Float)
     103    line1 = ensure_numeric(line1, num.Float)   
    112104
    113105    x0 = line0[0,0]; y0 = line0[0,1]
     
    121113    u1 = (x2-x0)*(y1-y0) - (y2-y0)*(x1-x0)
    122114       
    123     if allclose(denom, 0.0, rtol=rtol, atol=atol):
     115    if num.allclose(denom, 0.0, rtol=rtol, atol=atol):
    124116        # Lines are parallel - check if they are collinear
    125         if allclose([u0, u1], 0.0, rtol=rtol, atol=atol):
     117        if num.allclose([u0, u1], 0.0, rtol=rtol, atol=atol):
    126118            # We now know that the lines are collinear
    127119            state_tuple = (point_on_line([x0, y0], line1, rtol=rtol, atol=atol),
     
    143135
    144136        # Sanity check - can be removed to speed up if needed
    145         assert allclose(x, x2 + u1*(x3-x2), rtol=rtol, atol=atol)
    146         assert allclose(y, y2 + u1*(y3-y2), rtol=rtol, atol=atol)       
     137        assert num.allclose(x, x2 + u1*(x3-x2), rtol=rtol, atol=atol)
     138        assert num.allclose(y, y2 + u1*(y3-y2), rtol=rtol, atol=atol)       
    147139
    148140        # Check if point found lies within given line segments
    149141        if 0.0 <= u0 <= 1.0 and 0.0 <= u1 <= 1.0:
    150142            # We have intersection
    151             return 1, array([x, y])
     143            return 1, num.array([x, y])
    152144        else:
    153145            # No intersection
     
    184176
    185177
    186     line0 = ensure_numeric(line0, Float)
    187     line1 = ensure_numeric(line1, Float)   
     178    line0 = ensure_numeric(line0, num.Float)
     179    line1 = ensure_numeric(line1, num.Float)   
    188180
    189181    status, value = _intersection(line0[0,0], line0[0,1],
     
    251243    if len(points.shape) == 1:
    252244        # Only one point was passed in. Convert to array of points
    253         points = reshape(points, (1,2))
     245        points = num.reshape(points, (1,2))
    254246
    255247    indices, count = separate_points_by_polygon(points, polygon,
     
    293285    #if verbose: print 'Checking input to outside_polygon'
    294286    try:
    295         points = ensure_numeric(points, Float)
     287        points = ensure_numeric(points, num.Float)
    296288    except NameError, e:
    297289        raise NameError, e
     
    301293
    302294    try:
    303         polygon = ensure_numeric(polygon, Float)
     295        polygon = ensure_numeric(polygon, num.Float)
    304296    except NameError, e:
    305297        raise NameError, e
     
    311303    if len(points.shape) == 1:
    312304        # Only one point was passed in. Convert to array of points
    313         points = reshape(points, (1,2))
     305        points = num.reshape(points, (1,2))
    314306
    315307    indices, count = separate_points_by_polygon(points, polygon,
     
    320312    if count == len(indices):
    321313        # No points are outside
    322         return array([])
     314        return num.array([])
    323315    else:
    324316        return indices[count:][::-1]  #return reversed
     
    335327    #if verbose: print 'Checking input to outside_polygon'
    336328    try:
    337         points = ensure_numeric(points, Float)
     329        points = ensure_numeric(points, num.Float)
    338330    except NameError, e:
    339331        raise NameError, e
     
    343335
    344336    try:
    345         polygon = ensure_numeric(polygon, Float)
     337        polygon = ensure_numeric(polygon, num.Float)
    346338    except NameError, e:
    347339        raise NameError, e
     
    352344    if len(points.shape) == 1:
    353345        # Only one point was passed in. Convert to array of points
    354         points = reshape(points, (1,2))
     346        points = num.reshape(points, (1,2))
    355347
    356348
     
    422414
    423415    try:
    424         points = ensure_numeric(points, Float)
     416        points = ensure_numeric(points, num.Float)
    425417    except NameError, e:
    426418        raise NameError, e
     
    431423    #if verbose: print 'Checking input to separate_points_by_polygon 2'
    432424    try:
    433         polygon = ensure_numeric(polygon, Float)
     425        polygon = ensure_numeric(polygon, num.Float)
    434426    except NameError, e:
    435427        raise NameError, e
     
    453445        # Only one point was passed in.
    454446        # Convert to array of points
    455         points = reshape(points, (1,2))
     447        points = num.reshape(points, (1,2))
    456448
    457449   
     
    472464
    473465
    474     indices = zeros( M, Int )
     466    indices = num.zeros( M, num.Int )
    475467
    476468    count = _separate_points_by_polygon(points, polygon, indices,
     
    607599
    608600    try:
    609         polygon = ensure_numeric(polygon, Float)
     601        polygon = ensure_numeric(polygon, num.Float)
    610602    except NameError, e:
    611603        raise NameError, e
     
    616608    x = polygon[:,0]
    617609    y = polygon[:,1]
    618     x = concatenate((x, [polygon[0,0]]), axis = 0)
    619     y = concatenate((y, [polygon[0,1]]), axis = 0)
     610    x = num.concatenate((x, [polygon[0,0]]), axis = 0)
     611    y = num.concatenate((y, [polygon[0,1]]), axis = 0)
    620612   
    621613    return x, y
     
    716708
    717709    def __call__(self, x, y):
    718         x = array(x).astype(Float)
    719         y = array(y).astype(Float)
     710        x = num.array(x).astype(num.Float)
     711        y = num.array(y).astype(num.Float)
    720712
    721713        N = len(x)
    722714        assert len(y) == N
    723715
    724         points = concatenate( (reshape(x, (N, 1)),
    725                                reshape(y, (N, 1))), axis=1 )
     716        points = num.concatenate( (num.reshape(x, (N, 1)),
     717                                   num.reshape(y, (N, 1))), axis=1 )
    726718
    727719        if callable(self.default):
    728720            z = self.default(x,y)
    729721        else:
    730             z = ones(N, Float) * self.default
     722            z = num.ones(N, num.Float) * self.default
    731723
    732724        for polygon, value in self.regions:
     
    736728            if callable(value):
    737729                for i in indices:
    738                     xx = array([x[i]])
    739                     yy = array([y[i]])
     730                    xx = num.array([x[i]])
     731                    yy = num.array([y[i]])
    740732                    z[i] = value(xx, yy)[0]
    741733            else:
Note: See TracChangeset for help on using the changeset viewer.