Changeset 1221


Ignore:
Timestamp:
Apr 12, 2005, 3:43:56 PM (20 years ago)
Author:
prow
Message:

fixed. point_on_line needed an allclose.

Location:
inundation/ga/storm_surge/pmesh
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pmesh/mesh.py

    r1217 r1221  
    21792179    def triangles_to_polySet(self,setName):
    21802180        seg2line = self.seg2line
    2181         ver2point = self.ver2point
     2181        ver2point= self.ver2point
    21822182        line2seg = self.line2seg
    2183         point2ver = self.point2ver
     2183        point2ver= self.point2ver
    21842184
    21852185        from Numeric import array,allclose
     
    21892189        for triangle in Triangles:
    21902190            Triangles_dict[triangle]=None
     2191 
    21912192
    21922193        point_keys = {}
     
    21982199                userVertices[vertex]=True
    21992200
     2201        #######
     2202        for vert in userVertices.keys():
     2203            assert point_keys[(vert.x,vert.y)]==vert
     2204        assert len(point_keys.keys())==len(userVertices.keys())
     2205        #######
     2206
     2207
    22002208        userSegments = self.segs_to_dict(self.userSegments)
    22012209        affine_lines = Affine_Linespace()
     
    22142222                    a = triangle.vertices[i-1]
    22152223                    b = triangle.vertices[i-2]
    2216                     for vert in (a,b):
    2217                         point = ver2point(vert)
    2218                         if not point_keys.has_key(point):
    2219                             userVertices[vert]=True
    2220                             point_keys[point]=vert
    2221 
    22222224                    #Get possible matches:
    22232225                    point_a = ver2point(a)
     
    22302232                    for user_line in possible_lines:
    22312233                        if self.point_on_line(midpoint,user_line):
    2232                             if user_line in userSegments:
     2234                            if userSegments.has_key(user_line):
    22332235                                parent_segment = userSegments.pop(user_line)
    22342236                            else:
     
    22572259                        for point in line:
    22582260                            if point_keys.has_key(point):
    2259                                 vert = point_keys[point]
     2261                                vert = point_keys[point]
    22602262                            else:
    22612263                                vert = Vertex(point[0],point[1])
     
    22672269                        affine_lines.append(line)
    22682270       
     2271        #######
     2272        for vert in userVertices.keys():
     2273            assert point_keys[(vert.x,vert.y)]==vert
     2274        assert len(point_keys.keys())==len(userVertices.keys())
     2275        #######
    22692276        self.userVerticies = []
    22702277        self.userSegments = []
     
    22772284        #FIXME change alpha to user and sync with pmesh
    22782285        #ie self.alphaSegments.append(alphaSegments[key])
    2279 
     2286        #print point_keys.keys()
     2287        #print userVertices.keys()
     2288        #print userVertices.keys()
    22802289        return userVertices,userSegments,alphaSegments
    22812290
     
    23432352        y0=line[0][1]
    23442353        y1=line[1][1]
    2345         return point_on_line(x,y,x0,y0,x1,y1)
    2346         #a wrapper for the c func.
     2354        from Numeric import array, dot, allclose
     2355        from math import sqrt
     2356
     2357        a = array([x - x0, y - y0])
     2358        a_normal = array([a[1], -a[0]])
     2359
     2360        b = array([x1 - x0, y1 - y0])
     2361   
     2362        if allclose(dot(a_normal, b),0):
     2363            #Point is somewhere on the infinite extension of the line
     2364
     2365            len_a = sqrt(sum(a**2))           
     2366            len_b = sqrt(sum(b**2))                             
     2367            if dot(a, b) >= 0 and len_a <= len_b:
     2368               return True
     2369            else:   
     2370               return False
     2371        else:
     2372          return False 
    23472373
    23482374    def line_length(self,line):
     
    31223148    #NOT IMPLIMENTED
    31233149    at a.precision = 2:
    3124         a.round_up[0.0]=
     3150        a.round_up_rel[0.0]=
    31253151        a.round_flat[0.0]=
    3126         a.round_downp0.0]=
     3152        a.round_down_rel[0.0]=
    31273153
    31283154        a.up((0.1,2.04))=
    31293155
    3130     If tolerance = 0, nothing gets rounded into
    3131     two bins. If tolerance = 0.5, everything does.
     3156    If t_rel = 0, nothing gets rounded into
     3157    two bins. If t_rel = 0.5, everything does.
    31323158
    31333159    Ideally, precision can be set high, so that multiple
    3134     entries are rarely in the same bin. And tolerance should
     3160    entries are rarely in the same bin. And t_rel should
    31353161    be low (<0.1 for 1 dimension!,<(0.1/n) for small n!!)
    31363162    so that it is rare to put items in mutiple bins.
    31373163
    31383164    Ex bins per entry = product(a,b,c...,n)
    3139     a = 1 or 2 s.t. Ex(a) = 1+2*tolerance
     3165    a = 1 or 2 s.t. Ex(a) = 1+2*t_rel
    31403166    b = 1 or 2 ...
    31413167
    31423168    BUT!!! to avoid missing the right bin:
    3143     (-10)**(precision+1)*tolerance must be greater than the
     3169    (-10)**(precision+1)*t_rel must be greater than the
    31443170    greatest possible variation that an identical element
    31453171    can display.
    3146     """
    3147     def __init__(self,precision = 6,tolerance = 0.01):
    3148         self.__precision__ = precision
    3149         self.__tolerance__ = tolerance
    3150         assert tolerance <= 0.5
     3172
     3173
     3174    Note that if tol = 0.5 (the max allowed) 0.6 will round to .7 and .5
     3175    but not .6 - this looks wrong, but note that *everything* will round,
     3176    so .6 wont be missed as everything close to it will check in .7 and .5.
     3177    """
     3178    def __init__(self,p_rel = 6,t_rel = 0.01):
     3179        self.__p_rel__ = p_rel
     3180        self.__t_rel__ = t_rel
     3181
     3182        self.__p_abs__ = p_rel+1
     3183        self.__t_abs__ = t_rel
     3184
     3185        assert t_rel <= 0.5
    31513186        self.__items__ = {}
    31523187        from math import frexp
     
    31643199    def __rounded_keys__(self,key):
    31653200        keys = []
    3166         up_key=list(key)
    3167         for i in range(len(up_key)):
    3168             up_key[i]=self.round_up(key[i])
    3169         keys.append(tuple(up_key))
    3170 
    3171         #round down the value
    3172         for i in range(len(up_key)):
    3173             if not keys[0][i]==self.round_down(key[i]):
    3174             #unless round_up = round_down
    3175             #so the keys stay unique
    3176                 for j in range(len(keys)):
    3177                     down_key = list(keys[j])
    3178                     down_key[i]=self.round_down(key[i])
    3179                     down_key=tuple(down_key)
    3180                     keys.append(down_key)
     3201        rounded_key=list(key)
     3202        rounded_values=list(key)
     3203
     3204        roundings = [self.round_up_rel,\
     3205        self.round_down_rel,self.round_flat_rel,\
     3206        self.round_down_abs,self.round_up_abs,\
     3207        self.round_flat_abs]
     3208
     3209
     3210        roundings = \
     3211        [self.round_up_rel, self.round_down_rel, \
     3212         self.round_up_rel2,self.round_down_rel2,\
     3213         self.round_up_abs, self.round_down_abs]
     3214
     3215        #initialise rounded_values
     3216        round = roundings.pop(0)
     3217        for i in range(len(rounded_values)):
     3218            rounded_key[i]=round(key[i])
     3219            rounded_values[i]={}
     3220            rounded_values[i][rounded_key[i]]=None
     3221        keys.append(tuple(rounded_key))
     3222       
     3223        for round in roundings:
     3224            for i in range(len(rounded_key)):
     3225                rounded_value=round(key[i])
     3226                if not rounded_values[i].has_key(rounded_value):
     3227                    #ie unless round_up_rel = round_down_rel
     3228                    #so the keys stay unique
     3229                    for j in range(len(keys)):
     3230                        rounded_key = list(keys[j])
     3231                        rounded_key[i]=rounded_value
     3232                        keys.append(tuple(rounded_key))
    31813233        return keys
    31823234
     
    32303282        return self.__contains__(item)
    32313283
    3232     def round_up(self,value):
    3233          tolerance=self.__tolerance__
     3284    def round_up_rel2(self,value):
     3285         t_rel=self.__t_rel__
     3286         #Rounding up the value
     3287         m,e = self.frexp(value)
     3288         m = m/2
     3289         e = e + 1
     3290         #m is the mantissa, e the exponent
     3291         # 0.5 < |m| < 1.0
     3292         m = m+t_rel*(10**-(self.__p_rel__))
     3293         #bump m up
     3294         m = round(m,self.__p_rel__)
     3295         return m*(2.**e)
     3296
     3297    def round_down_rel2(self,value):
     3298         t_rel=self.__t_rel__
     3299         #Rounding down the value
     3300         m,e = self.frexp(value)
     3301         m = m/2
     3302         e = e + 1
     3303         #m is the mantissa, e the exponent
     3304         # 0.5 < m < 1.0
     3305         m = m-t_rel*(10**-(self.__p_rel__))
     3306         #bump the |m| down, by 5% or whatever
     3307         #self.p_rel dictates
     3308         m = round(m,self.__p_rel__)
     3309         return m*(2.**e)
     3310
     3311    def round_flat_rel2(self,value):
     3312    #redundant
     3313         m,e = self.frexp(value)
     3314         m = m/2
     3315         e = e + 1
     3316         m = round(m,self.__p_rel__)
     3317         return m*(2.**e)
     3318
     3319    def round_up_rel(self,value):
     3320         t_rel=self.__t_rel__
    32343321         #Rounding up the value
    32353322         m,e = self.frexp(value)
    32363323         #m is the mantissa, e the exponent
    32373324         # 0.5 < |m| < 1.0
    3238          m = m*(1+tolerance*(10**-(self.__precision__+1))/((m**2)**0.5))
    3239          #bump m up, by 5% or whatever
    3240          #self.precision dictates
    3241          m = round(m,self.__precision__)
    3242      #round m off so that the bump up
    3243      #will be discernable if and only if
     3325         m = m+t_rel*(10**-(self.__p_rel__))
     3326         #bump m up
     3327         m = round(m,self.__p_rel__)
    32443328         return m*(2.**e)
    32453329
    3246     def round_down(self,value):
    3247          tolerance=self.__tolerance__
     3330    def round_down_rel(self,value):
     3331         t_rel=self.__t_rel__
    32483332         #Rounding down the value
    32493333         m,e = self.frexp(value)
    32503334         #m is the mantissa, e the exponent
    32513335         # 0.5 < m < 1.0
    3252          m = m*(1-tolerance*(10**-(self.__precision__+1))/((m**2)**0.5))
     3336         m = m-t_rel*(10**-(self.__p_rel__))
    32533337         #bump the |m| down, by 5% or whatever
    3254          #self.precision dictates
    3255          m = round(m,self.__precision__)
    3256      #round m off so that the bump down
    3257      #will be discernable if and only if
     3338         #self.p_rel dictates
     3339         m = round(m,self.__p_rel__)
    32583340         return m*(2.**e)
    32593341
    3260     def round_flat(self,value):
     3342    def round_flat_rel(self,value):
    32613343    #redundant
    32623344         m,e = self.frexp(value)
    3263          m = round(m,self.__precision__)
     3345         m = round(m,self.__p_rel__)
    32643346         return m*(2.**e)
     3347
     3348    def round_up_abs(self,value):
     3349         t_abs=self.__t_abs__
     3350         #Rounding up the value
     3351         m = value+t_abs*(10**-(self.__p_abs__))
     3352         #bump m up
     3353         m = round(m,self.__p_abs__)
     3354         return m
     3355
     3356    def round_down_abs(self,value):
     3357         t_abs=self.__t_abs__
     3358         #Rounding down the value
     3359         m = value-t_abs*(10**-(self.__p_abs__))
     3360         #bump the |m| down, by 5% or whatever
     3361         #self.p_rel dictates
     3362         m = round(m,self.__p_abs__)
     3363         return m
     3364
     3365    def round_flat_abs(self,value):
     3366    #redundant?
     3367         m = round(value,self.__p_abs__)
     3368         return m
    32653369
    32663370    def keys(self):
     
    32893393    a[elephant] -> []
    32903394    """
    3291     def __init__(self,mapping,precision = 6, tolerance=0.01):
     3395    def __init__(self,mapping,p_rel = 6, t_rel=0.01):
    32923396        Discretised_Tuple_Set.__init__\
    3293          (self,precision,tolerance = tolerance)
     3397         (self,p_rel,t_rel = t_rel)
    32943398        self.mapping = mapping
    32953399
     
    33083412    Nearly.
    33093413    """
    3310     def __init__(self,precision = 4,tolerance=0.04):
     3414    def __init__(self,p_rel = 3,t_rel=0.1):
    33113415        Mapped_Discretised_Tuple_Set.__init__\
    33123416            (self,self.affine_line,\
    3313             precision=precision,tolerance=tolerance)
     3417            p_rel=p_rel,t_rel=t_rel)
    33143418
    33153419    def affine_line(self,line):
     
    33573461        else:
    33583462            sign_a = a/((a**2)**0.5)
     3463        #This does not change the mathematical
     3464        #properties, but it makes comparism
    33593465        if b == 0:
    33603466            sign_b = 1.
     
    33703476        return a,b,c
    33713477
     3478
    33723479   
    33733480if __name__ == "__main__":
  • inundation/ga/storm_surge/pmesh/pmesh.py

    r1217 r1221  
    354354        print 'len(alphaSegments.keys())'
    355355        print len(alphaSegments.keys())
     356
     357
     358        #######
     359        point_keys = {}
     360        for vert in userVertices.keys():
     361            assert not point_keys.has_key((vert.x,vert.y))
     362            point_keys[(vert.x,vert.y)]=vert
     363        assert len(point_keys.keys())==len(userVertices.keys())
     364        #######
     365
    356366        for v in userVertices.keys():
    357367            if userVertices[v] is True:
Note: See TracChangeset for help on using the changeset viewer.