Changeset 375

Show
Ignore:
Timestamp:
07/14/09 22:53:41 (14 months ago)
Author:
laura
Message:

SLERP & LERP on Quaternion

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/adaptare/trunk/hfall/Quaternion.py

    r374 r375  
    3838 
    3939    def __add__(self, other): 
    40         return Qauternion(self.w + other.w, 
    41                           self.x + other.x, 
     40        return Quaternion(self.w + other.w, 
     41                          self.x + other.x,  
    4242                          self.y + other.y, 
    4343                          self.z + other.z) 
     
    9898        z = self.z ** 2 
    9999        w = self.w ** 2 
    100         return math.math.sqrt(x + y + z +w) 
     100        return math.sqrt(x + y + z +w) 
    101101 
    102102    def normalize(self): 
     
    106106        self.w /= l 
    107107        self.z /= l 
     108        return self 
    108109 
    109110    def __str__(self): 
     
    248249                                            1 - 2 * x ** 2 - 2 * z ** 2) 
    249250        return vectorvals 
    250  
    251     def LERP(self, undefined): 
    252         pass 
    253  
    254     def SLERP(self, undefined): 
    255         pass 
     251         
     252    @staticmethod 
     253    def dot(Q1, Q2): 
     254        if isinstance(Q1, Quaternion): 
     255            if isinstance(Q2, Quaternion): 
     256                return Q1.x * Q2.x + Q1.y * Q2.y + Q1.z * Q2.z + Q1.w * Q2.w 
     257            else: 
     258                raise TypeError("Invalid argument") 
     259        else: 
     260            raise TypeError("Invalid argument") 
     261 
     262    # Linearly interpolate each component, then normalize the Quaternion 
     263    @staticmethod 
     264    def LERP(Q1, Q2, t): 
     265        if isinstance(Q1, Quaternion): 
     266            if isinstance(Q2, Quaternion): 
     267                return (Q1 * (1-t) + Q2 * t).normalize() 
     268            else: 
     269                raise TypeError("Invalid argument") 
     270        else: 
     271            raise TypeError("Invalid argument") 
     272         
     273    #Spherical linear interpolation 
     274    @staticmethod 
     275    def SLERP(Q1, Q2, t): 
     276        if isinstance(Q1, Quaternion): 
     277            if isinstance(Q2, Quaternion): 
     278             
     279                dot = Quaternion.dot(Q1, Q2) 
     280                if dot < 0: 
     281                    dot = -dot 
     282                    Q3 = -Q2 
     283                else: 
     284                    Q3 = Q2 
     285                 
     286                if dot < 0.95: 
     287                    angle = math.acos(dot) 
     288                    w1 = math.sin(angle * (1-t)) / math.sin(angle) 
     289                    w2 = math.sin(angle * t) / math.sin(angle) 
     290                    return Q1 * w1 + Q3 * w2 
     291                else: 
     292                # The angle is small. We use linear interpolation 
     293                    Quaternion.LERP(Q1, Q3, t) 
     294                     
     295            else: 
     296                raise TypeError("Invalid argument") 
     297        else: 
     298            raise TypeError("Invalid argument")