/[escript]/trunk/pycad/py_src/primitives.py
ViewVC logotype

Diff of /trunk/pycad/py_src/primitives.py

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 914 by gross, Wed Dec 6 03:29:49 2006 UTC revision 915 by gross, Thu Dec 14 06:12:53 2006 UTC
# Line 26  __version__="$Revision:$" Line 26  __version__="$Revision:$"
26  __date__="$Date:$"  __date__="$Date:$"
27    
28  import numarray  import numarray
29  from transformations import _TYPE  from transformations import _TYPE, Translation, Dilation, Transformation
30    
31  global global_primitive_id_counter  
32  global_primitive_id_counter=1  def resetGlobalPrimitiveIdCounter():
33       global global_primitive_id_counter
34       global_primitive_id_counter=1
35    
36    resetGlobalPrimitiveIdCounter()
37    
38  class Primitive(object):  class Primitive(object):
39      """      """
# Line 37  class Primitive(object): Line 41  class Primitive(object):
41      """      """
42      def __init__(self):      def __init__(self):
43         """         """
44          
45         """         """
46         global global_primitive_id_counter         global global_primitive_id_counter
47         self.__ID=global_primitive_id_counter         self.__ID=global_primitive_id_counter
48         global_primitive_id_counter+=1         global_primitive_id_counter+=1
49    
50      def getID(self):      def getID(self):
51         return self.__ID         return self.__ID
52    
53      def __repr__(self):      def __repr__(self):
54         return "%s(%s)"%(self.__class__.__name__,self.getID())         return "%s(%s)"%(self.__class__.__name__,self.getID())
55      def __cmp__(self,other):      def __cmp__(self,other):
# Line 92  class Primitive(object): Line 99  class Primitive(object):
99          returns C{set} of primitive used to construct the primitive          returns C{set} of primitive used to construct the primitive
100          """          """
101          return set()          return set()
102                def copy(self):
103           """
104           returns a copy of the object
105           """
106           return Primitive()
107    
108        def apply(self,transformation):
109            """
110            returns a new object by applying the transformation
111            """
112            raise NotImplementedError("apply is not implemented for this class %s."%self.__class__.__name__)
113    
114        def modifyBy(self,transformation):
115            """
116            modifies the object by applying a transformation
117            """
118            raise NotImplementedError("modifyBy not implemented for this class %s."%self.__class__.__name__)
119    
     #==================================================  
     def __neg__(self):  
         return ReversedPrimitive(self)  
     def __pos__(self):  
         return self.copy()  
120      def __add__(self,other):      def __add__(self,other):
121         out=self.copy()          """
122         out+=other          returns a new object shifted by other
123         return out          """
124            return self.apply(Translation(numarray.array(other,_TYPE)))
125    
126        def __sub__(self,other):
127            """
128            returns a new object shifted by other
129            """
130            return self.apply(Translation(-numarray.array(other,_TYPE)))
131    
132      def __iadd__(self,other):      def __iadd__(self,other):
133         self.shift()          """
134      def shift(self,shift):          shifts the point by other
135          for p in self.getPoints(): p+=shift          """
136      def copy(self):          self.modifyBy(Translation(numarray.array(other,_TYPE)))
137          return Primitive()          return self
138    
139        def __isub__(self,other):
140            """
141            shifts the point by -other
142            """
143            self.modifyBy(Translation(-numarray.array(other,_TYPE)))
144            return self
145    
146        def __imul__(self,other):
147            """
148            modifies object by applying L{Transformation} other. If other is not a L{Transformation} it will try convert it.
149            """
150            if isinstance(other,int) or isinstance(other,float):
151                trafo=Dilation(other)
152            elif isinstance(other,numarray.NumArray):
153                trafo=Translation(other)
154            elif isinstance(other,Transformation):
155                trafo=other
156            else:
157                raise TypeError, "cannot convert argument to Trnsformation class object."
158            self.modifyBy(trafo)
159            return self
160    
161        def __rmul__(self,other):
162            """
163            applies L{Transformation} other to object. If other is not a L{Transformation} it will try convert it.
164            """
165            if isinstance(other,int) or isinstance(other,float):
166                trafo=Dilation(other)
167            elif isinstance(other,numarray.NumArray):
168                trafo=Translation(other)
169            elif isinstance(other,Transformation):
170                trafo=other
171            else:
172                raise TypeError, "cannot convert argument to Trnsformation class object."
173            return self.apply(trafo)
174    
175        def __neg__(self):
176            return ReversedPrimitive(self)
177    
178      def getGmshCommand(self):      def getGmshCommand(self):
179          raise NotImplementedError("getGmshCommand is not implemented for this class %s."%self.__class__.__name__)          raise NotImplementedError("getGmshCommand is not implemented for this class %s."%self.__class__.__name__)
     def translate(self,shift):  
         raise NotImplementedError("translate is not implemented for this class %s."%self.__class__.__name__)  
180    
181  class Point(Primitive):  class Point(Primitive):
182      """      """
# Line 123  class Point(Primitive): Line 187  class Point(Primitive):
187         creates a point with coorinates x,y,z with the local refinement factor local_scale         creates a point with coorinates x,y,z with the local refinement factor local_scale
188         """         """
189         super(Point, self).__init__()         super(Point, self).__init__()
190         self.setCoordinates(x,y,z)         self.setCoordinates(numarray.array([x,y,z],_TYPE))
191         self.setLocalScale(local_scale)         self.setLocalScale(local_scale)
192    
193      def setLocalScale(self,factor=1.):      def setLocalScale(self,factor=1.):
194         """         """
195         sets the local refinement factor         sets the local refinement factor
# Line 142  class Point(Primitive): Line 207  class Point(Primitive):
207         returns the coodinates of the point as L{numarray.NumArray} object         returns the coodinates of the point as L{numarray.NumArray} object
208         """         """
209         return self._x         return self._x
210      def setCoordinates(self,x,y,z):      def setCoordinates(self,x):
211         """         """
212         returns the coodinates of the point as L{numarray.NumArray} object         returns the coodinates of the point as L{numarray.NumArray} object
213         """         """
214         self._x=numarray.array([x,y,z],_TYPE)         if not isinstance(x, numarray.NumArray):
215              self._x=numarray.array(x,_TYPE)
216           else:
217              self._x=x
218    
219      def getHistory(self):      def getHistory(self):
220         """         """
221         returns C{set} of primitive used to construct the primitive         returns C{set} of primitive used to construct the primitive
# Line 163  class Point(Primitive): Line 232  class Point(Primitive):
232         c=self.getCoordinates()         c=self.getCoordinates()
233         d=c-point         d=c-point
234         return numarray.dot(d,d)<=tol**2*max(numarray.dot(c,c),numarray.dot(point,point))         return numarray.dot(d,d)<=tol**2*max(numarray.dot(c,c),numarray.dot(point,point))
       
235    
     #=============================================================  
236      def copy(self):      def copy(self):
        c=self.getCoordinates()  
        return Point(c[0],c[1],c[2],local_scale=self.getLocalScale())  
     def isPoint(self):  
         return True  
     def getGmshCommand(self):  
         c=self.getCoordinates()  
         return "Point(%s) = {%e , %e, %e , %e * scale};"%(self.getID(),c[0],c[1],c[2], self.getLocalScale())  
     def shift(self,shift):  
237         """         """
238         shifts the point by a given shift         returns a copy of the point
239         """         """
240         self._x+=numarray.array(shift,numarray.Float64)         c=self.getCoordinates()
241      def translate(self,shift):         return Point(c[0],c[1],c[2],local_scale=self.getLocalScale())
242         """  
243         returns the point shifted by shift      def modifyBy(self,transformation):
244         """          """
245         out=self.copy()          modifies the coordinates by applying a transformation
246         out+=other          """
247         return out          self.setCoordinates(transformation(self.getCoordinates()))
248    
249        def apply(self,transformation):
250            """
251            returns a new L{Point} by applying the transformation
252            """
253            new_p=self.copy()
254            new_p.modifyBy(transformation)
255            return new_p
256    
257    
258        def getGmshCommand(self, local_scaling_factor=1.):
259            c=self.getCoordinates()
260            return "Point(%s) = {%s , %s, %s , %s };"%(self.getID(),c[0],c[1],c[2], self.getLocalScale()*local_scaling_factor)
261    
262  class Curve(Primitive):  class Curve(Primitive):
263        """        """

Legend:
Removed from v.914  
changed lines
  Added in v.915

  ViewVC Help
Powered by ViewVC 1.1.26