Package slum :: Module datatypes
[hide private]
[frames] | no frames]

Source Code for Module slum.datatypes

  1  # 
  2  # datatypes.py - all slum custom datatypes 
  3  # 
  4  #    Copyright (C) 2008 - Roberto Hradec 
  5  # 
  6  # --------------------------------------------------------------------------- 
  7  #        This file is part of SLUM. 
  8  # 
  9  #    SLUM is free software: you can redistribute it and/or modify 
 10  #    it under the terms of the GNU General Public License as published by 
 11  #    the Free Software Foundation, either version 3 of the License, or 
 12  #    (at your option) any later version. 
 13  # 
 14  #    SLUM is distributed in the hope that it will be useful, 
 15  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 16  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 17  #    GNU General Public License for more details. 
 18  # 
 19  #    You should have received a copy of the GNU General Public License 
 20  #    along with SLUM.  If not, see <http://www.gnu.org/licenses/>. 
 21  # --------------------------------------------------------------------------- 
 22   
 23  import math 
 24   
25 -class color:
26 ''' slum datatype to hold color data. 27 The "internal" variable is a list that defines all the possible keys for this datatype. For color, 28 they are [r,g,b], but they can be overriden by derivated classes, like vector, which defines it as 29 x,y,z. 30 This datatype is defined to simplify a shader implementation, specially when defining slum 31 shader parameters.''' 32 33 internal = ['r','g','b']
34 - def __init__(self, data=0, *args):
35 self.checkValue(data) 36 for each in self.internal: 37 self.__dict__[each] = data 38 nargs = len(args) 39 if nargs: 40 if nargs==len(self.internal)-1: 41 internalIndex=1 42 for each in args: 43 self.checkValue(each) 44 self.__dict__[self.internal[internalIndex]] = each 45 internalIndex += 1 46 else: 47 raise Exception('dataype requires 1 or %d parameters in initialization. Received only %d parameters.' % (len(self.internal),nargs+1))
48 - def checkKey(self, key):
49 if key>len(self.internal) or key<0: 50 raise Exception('dataype values can only have 3 elements. Element %d not valid' % key )
51 - def checkValue(self, value):
52 if type(value) not in [int,long,float]: 53 raise Exception('dataype "%s" not supported' % type(value))
54 - def __getitem__(self, key):
55 self.checkKey(key) 56 return self.__dict__[self.internal[key]]
57 - def __setitem__(self, key, value):
58 self.checkKey(key) 59 self.checkValue(value) 60 self.__dict__[self.internal[key]] = value
61 - def __repr__(self):
62 data = [] 63 for each in self.internal: 64 data.append("%s" % str(self.__dict__[each])) 65 return '%s(%s)' % (self.__class__.__name__,','.join(data))
66 - def __len__(self):
67 return 3
68 - def __delitem__(self, key):
69 pass
70 - def __add__(self, x):
71 d=[] 72 if x.__class__ in [color, vector, point, normal]: 73 count = 0 74 for each in self.internal: 75 d.append( self.__dict__[each] + x.__dict__[x.internal[count]] ) 76 count += 1 77 else: 78 for each in self.internal: 79 d.append( self.__dict__[each] + x ) 80 return self.__class__( d[0], d[1], d[2] )
81
82 - def __mul__(self, x):
83 d=[] 84 if x.__class__ in [color, vector, point, normal]: 85 count = 0 86 for each in self.internal: 87 d.append( self.__dict__[each] * x.__dict__[x.internal[count]] ) 88 count += 1 89 else: 90 for each in self.internal: 91 d.append( self.__dict__[each] * x ) 92 return self.__class__( d[0], d[1], d[2] )
93
94 - def __div__(self, x):
95 d=[] 96 if x.__class__ in [color, vector, point, normal]: 97 count = 0 98 for each in self.internal: 99 d.append( self.__dict__[each] / x.__dict__[x.internal[count]] ) 100 count += 1 101 else: 102 for each in self.internal: 103 d.append( self.__dict__[each] / x ) 104 return self.__class__( d[0], d[1], d[2] )
105
106 - def __sub__(self, x):
107 return self.__add__(-x)
108
109 - def __neg__(self):
110 d = [] 111 for each in self.internal: 112 d.append( -self.__dict__[each] ) 113 return self.__class__( d[0], d[1], d[2] )
114 115
116 -class vector(color):
117 ''' 118 slum datatype to hold vector data. 119 its derivated from color, overriding the internal to x,y,z. 120 also, theres 2 added method to manipulate vectors: lenght and normalize 121 ''' 122 internal = ['x','y','z']
123 - def length(self):
124 return math.sqrt( 125 (self.__dict__[self.internal[0]] * self.__dict__[self.internal[0]]) + 126 (self.__dict__[self.internal[1]] * self.__dict__[self.internal[1]]) + 127 (self.__dict__[self.internal[2]] * self.__dict__[self.internal[2]]) 128 )
129 - def normalize(self):
130 len = self.length() 131 return vector( 132 self.__dict__[self.internal[0]]/len, 133 self.__dict__[self.internal[1]]/len, 134 self.__dict__[self.internal[2]]/len 135 )
136
137 -class normal(vector):
138 ''' 139 slum datatype to hold normal data. 140 its exactly like vector. Its defined just as a placeholder for people used to rsl. (like me :D) 141 ''' 142 pass
143
144 -class point(vector):
145 ''' 146 slum datatype to hold point data. 147 its exactly like vector. Its defined just as a placeholder for people used to rsl. (like me :D) 148 ''' 149 pass
150 151
152 -class bound:
153 - def __init__(self, min, max):
154 if min.__class__ != vector or max.__class__ != vector: 155 raise Exception("min/max not a vector datatype.") 156 self.min = min 157 self.max = max
158 - def size(self):
159 return self.max - self.min
160 - def center(self):
161 return self.min + ( self.size() ) * 0.5
162