/[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 931 by gross, Fri Jan 19 03:06:33 2007 UTC revision 1109 by btully, Thu Apr 19 04:21:43 2007 UTC
# Line 75  class PrimitiveBase(object): Line 75  class PrimitiveBase(object):
75          """          """
76          returns the points used to construct the primitive          returns the points used to construct the primitive
77          """          """
78          out=set()          out=[]
79          for i in self.getPrimitives():          for i in self.getPrimitives():
80             if isinstance(i,Point): out.add(i)             if isinstance(i,Point): out.append(i)
81          return list(out)          return out
82    
83      def getPrimitives(self):      def getPrimitives(self):
84          """          """
85          returns a list of primitives used to construct the primitive with no double entries          returns a list of primitives used to construct the primitive with no double entries
86          """          """
87          out=set()          out=[]
88          return list(set([p for p in self.collectPrimitiveBases()]))          for p in self.collectPrimitiveBases():
89                if not p  in out: out.append(p)
90            return out
91    
92      def copy(self):      def copy(self):
93         """         """
# Line 215  class Primitive(object): Line 217  class Primitive(object):
217          """          """
218          raise NotImplementedError("__neg__ is not implemented.")          raise NotImplementedError("__neg__ is not implemented.")
219    
     def getGmshCommand(self, local_scaling_factor=1.):  
         """  
         returns the Gmsh command(s) to create the primitive  
   
         @note: this class is overwritten by subclass  
         """  
         raise NotImplementedError("getGmshCommand is not implemented.")  
   
220      def substitute(self,sub_dict):      def substitute(self,sub_dict):
221          """          """
222          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.
# Line 305  class ReversePrimitive(object): Line 299  class ReversePrimitive(object):
299            """            """
300            return self.__primitive            return self.__primitive
301    
     def getGmshCommand(self, local_scaling_factor=1.):  
         """  
         returns the Gmsh command(s) to create the primitive  
         """  
         return self.__primitive.getGmshCommand(local_scaling_factor)  
   
302      def collectPrimitiveBases(self):      def collectPrimitiveBases(self):
303          """          """
304          returns a list of primitives used to construct the primitive. It may contain primitives twice          returns a list of primitives used to construct the primitive. It may contain primitives twice
# Line 402  class Point(Primitive, PrimitiveBase): Line 390  class Point(Primitive, PrimitiveBase):
390          self.setCoordinates(transformation(self.getCoordinates()))          self.setCoordinates(transformation(self.getCoordinates()))
391    
392    
     def getGmshCommand(self, local_scaling_factor=1.):  
         """  
         returns the Gmsh command(s) to create the primitive  
         """  
         c=self.getCoordinates()  
         return "Point(%s) = {%s , %s, %s , %s };"%(self.getID(),c[0],c[1],c[2], self.getLocalScale()*local_scaling_factor)  
   
393      def __neg__(self):      def __neg__(self):
394          """          """
395          returns a view of the object with reverse orientiention. As a point has no direction the object itself is returned.          returns a view of the object with reverse orientiention. As a point has no direction the object itself is returned.
# Line 572  class Spline(Curve): Line 553  class Spline(Curve):
553      """      """
554      a spline curve defined through a list of control points.      a spline curve defined through a list of control points.
555      """      """
556      def getGmshCommand(self,scaling_factor=1.):      pass
         """  
         returns the Gmsh command(s) to create the Curve  
         """  
         out=""  
         for i in self.getControlPoints():  
             if len(out)>0:  
                 out+=", %s"%i.getDirectedID()  
             else:  
                 out="%s"%i.getDirectedID()  
         return "Spline(%s) = {%s};"%(self.getID(),out)  
       
557    
558  class BezierCurve(Curve):  class BezierCurve(Curve):
559      """      """
560      a Bezier curve      a Bezier curve
561      """      """
562      def getGmshCommand(self,scaling_factor=1.):      pass
         """  
         returns the Gmsh command(s) to create the Curve  
         """  
         out=""  
         for i in self.getControlPoints():  
             if len(out)>0:  
                 out+=", %s"%i.getDirectedID()  
             else:  
                 out="%s"%i.getDirectedID()  
         return "Bezier(%s) = {%s};"%(self.getID(),out)  
563    
564  class BSpline(Curve):  class BSpline(Curve):
565      """      """
566      a BSpline curve. Control points may be repeated.      a BSpline curve. Control points may be repeated.
567      """      """
568      def getGmshCommand(self,scaling_factor=1.):      pass
         """  
         returns the Gmsh command(s) to create the Curve  
         """  
         out=""  
         for i in self.getControlPoints():  
             if len(out)>0:  
                 out+=", %s"%i.getDirectedID()  
             else:  
                 out="%s"%i.getDirectedID()  
         return "BSpline(%s) = {%s};"%(self.getID(),out)  
569    
570  class Line(Curve):  class Line(Curve):
571      """      """
# Line 628  class Line(Curve): Line 578  class Line(Curve):
578          if len(points)!=2:          if len(points)!=2:
579             raise TypeError("Line needs two points")             raise TypeError("Line needs two points")
580          Curve.__init__(self,*points)          Curve.__init__(self,*points)
     def getGmshCommand(self,scaling_factor=1.):  
         """  
         returns the Gmsh command(s) to create the Curve  
         """  
         return "Line(%s) = {%s, %s};"%(self.getID(),self.getStartPoint().getDirectedID(),self.getEndPoint().getDirectedID())  
   
581    
582  class ArcBase(Manifold1D):  class ArcBase(Manifold1D):
583      def __init__(self):      def __init__(self):
# Line 709  class Arc(ArcBase, Primitive): Line 653  class Arc(ArcBase, Primitive):
653              sub_dict[self]=Arc(self.getCenterPoint().substitute(sub_dict),self.getStartPoint().substitute(sub_dict),self.getEndPoint().substitute(sub_dict))              sub_dict[self]=Arc(self.getCenterPoint().substitute(sub_dict),self.getStartPoint().substitute(sub_dict),self.getEndPoint().substitute(sub_dict))
654          return sub_dict[self]          return sub_dict[self]
655    
     def getGmshCommand(self,scaling_factor=1.):  
        """  
        returns the Gmsh command(s) to create the primitive  
        """  
        return "Circle(%s) = {%s, %s, %s};"%(self.getID(),self.getStartPoint().getDirectedID(),self.getCenterPoint().getDirectedID(),self.getEndPoint().getDirectedID())  
656    
657      def isColocated(self,primitive):      def isColocated(self,primitive):
658         """         """
# Line 774  class CurveLoop(Primitive, PrimitiveBase Line 713  class CurveLoop(Primitive, PrimitiveBase
713                raise TypeError("%s-th argument is not a Manifold1D object."%i)                raise TypeError("%s-th argument is not a Manifold1D object."%i)
714         # for the curves a loop:         # for the curves a loop:
715         used=[ False for i in curves]         used=[ False for i in curves]
716         self.__curves=[curves[0]]         self.__curves=list(curves)
        used[0]=True  
        while not min(used):  
           found=False  
           for i in xrange(len(curves)):  
              if not used[i]:  
                 if self.__curves[-1].getEndPoint() == curves[i].getStartPoint():  
                    self.__curves.append(curves[i])  
                    used[i]=True  
                    found=True  
                    break  
           if not found:  
              raise ValueError("loop is not closed.")  
        if not self.__curves[0].getStartPoint() == self.__curves[-1].getEndPoint():  
           raise ValueError("loop is not closed.")  
717         Primitive.__init__(self)         Primitive.__init__(self)
718         PrimitiveBase.__init__(self)         PrimitiveBase.__init__(self)
719    
# Line 848  class CurveLoop(Primitive, PrimitiveBase Line 773  class CurveLoop(Primitive, PrimitiveBase
773                  return True                  return True
774         return False         return False
775    
     def getGmshCommand(self,scaling_factor=1.):  
         """  
         returns the Gmsh command(s) to create the primitive  
         """  
         out=""  
         for i in self.getCurves():  
             if len(out)>0:  
                 out+=", %s"%i.getDirectedID()  
             else:  
                 out="%s"%i.getDirectedID()  
         return "Line Loop(%s) = {%s};"%(self.getID(),out)  
   
776  class ReverseCurveLoop(ReversePrimitive, PrimitiveBase):  class ReverseCurveLoop(ReversePrimitive, PrimitiveBase):
777      """      """
778      An oriented loop of one-dimensional manifolds (= curves and arcs)      An oriented loop of one-dimensional manifolds (= curves and arcs)
# Line 939  class RuledSurface(Primitive, Manifold2D Line 852  class RuledSurface(Primitive, Manifold2D
852          """          """
853          return self.getBoundaryLoop().getCurves()          return self.getBoundaryLoop().getCurves()
854    
     def getGmshCommand(self,scaling_factor=1.):  
         """  
         returns the Gmsh command(s) to create the primitive  
         """  
         return "Ruled Surface(%s) = {%s};"%(self.getID(),self.getBoundaryLoop().getDirectedID())  
   
855      def substitute(self,sub_dict):      def substitute(self,sub_dict):
856          """          """
857          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.
# Line 1044  class PlaneSurface(Primitive, Manifold2D Line 951  class PlaneSurface(Primitive, Manifold2D
951          """          """
952          return self.__loop          return self.__loop
953    
     def getGmshCommand(self,scaling_factor=1.):  
         """  
         returns the Gmsh command(s) to create the primitive  
         """  
         out=""  
         for i in self.getHoles():  
             if len(out)>0:  
                 out+=", %s"%i.getDirectedID()  
             else:  
                 out="%s"%i.getDirectedID()  
         if len(out)>0:  
           return "Plane Surface(%s) = {%s, %s};"%(self.getID(),self.getBoundaryLoop().getDirectedID(), out)  
         else:  
           return "Plane Surface(%s) = {%s};"%(self.getID(),self.getBoundaryLoop().getDirectedID())  
   
954      def substitute(self,sub_dict):      def substitute(self,sub_dict):
955          """          """
956          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.
# Line 1156  class SurfaceLoop(Primitive, PrimitiveBa Line 1048  class SurfaceLoop(Primitive, PrimitiveBa
1048         for i in range(len(surfaces)):         for i in range(len(surfaces)):
1049             if not isinstance(surfaces[i].getUnderlyingPrimitive(),Manifold2D):             if not isinstance(surfaces[i].getUnderlyingPrimitive(),Manifold2D):
1050                raise TypeError("%s-th argument is not a Manifold2D object."%i)                raise TypeError("%s-th argument is not a Manifold2D object."%i)
1051           self.__surfaces=list(surfaces)
1052         Primitive.__init__(self)         Primitive.__init__(self)
1053         PrimitiveBase.__init__(self)         PrimitiveBase.__init__(self)
        # for the curves a loop:  
        used=[ False for s in surfaces]  
        self.__surfaces=[surfaces[0]]  
        used[0]= True  
        edges=[ e for e in surfaces[0].getBoundary() ]  
        used_edges=[ False for e in surfaces[0].getBoundary() ]  
        while not min(used):  
           found=False  
           for i in xrange(len(surfaces)):  
              if not used[i]:  
                 i_boundary=surfaces[i].getBoundary()  
                 for ib in xrange(len(i_boundary)):    
                     if i_boundary[ib] in edges:  
                          found=True  
                          break  
                 if found:  
                     used[i]=True  
                     self.__surfaces.append(surfaces[i])  
                     for ib in xrange(len(i_boundary)):    
                        if i_boundary[ib] in edges:  
                          if used_edges[edges.index(i_boundary[ib])]:  
                             raise ValueError("boundary segment %s is shared by more than one surface."%str(i_boundary[ib].getUnderlyingPrimitive()))  
                          used_edges[edges.index(i_boundary[ib])]=True  
                        else:  
                          edges.append(i_boundary[ib])  
                          used_edges.append(False)  
                     break  
           if not found:  
                raise ValueError("loop is not closed.")  
        if not min(used_edges):  
           raise ValueError("loop is not closed. Surface is missing.")  
1054      def __len__(self):      def __len__(self):
1055         """         """
1056         return the number of curves in the SurfaceLoop         return the number of curves in the SurfaceLoop
# Line 1215  class SurfaceLoop(Primitive, PrimitiveBa Line 1077  class SurfaceLoop(Primitive, PrimitiveBa
1077         for c in self.getSurfaces(): out+=c.collectPrimitiveBases()         for c in self.getSurfaces(): out+=c.collectPrimitiveBases()
1078         return out         return out
1079    
     def getGmshCommand(self,scaling_factor=1.):  
         """  
         returns the Gmsh command(s) to create the primitive  
         """  
         out=""  
         for i in self.getSurfaces():  
             if len(out)>0:  
                 out+=", %s"%i.getDirectedID()  
             else:  
                 out="%s"%i.getDirectedID()  
         return "Surface Loop(%s) = {%s};"%(self.getID(),out)  
   
1080      def substitute(self,sub_dict):      def substitute(self,sub_dict):
1081          """          """
1082          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.
# Line 1333  class Volume(Manifold3D, Primitive): Line 1183  class Volume(Manifold3D, Primitive):
1183         """         """
1184         return self.__loop         return self.__loop
1185    
     def getGmshCommand(self,scaling_factor=1.):  
         """  
         returns the Gmsh command(s) to create the primitive  
         """  
         out=""  
         for i in self.getHoles():  
             if len(out)>0:  
                 out+=", %s"%i.getDirectedID()  
             else:  
                 out="%s"%i.getDirectedID()  
         if len(out)>0:  
           return "Volume(%s) = {%s, %s};"%(self.getID(),self.getSurfaceLoop().getDirectedID(), out)  
         else:  
           return "Volume(%s) = {%s};"%(self.getID(),self.getSurfaceLoop().getDirectedID())  
   
1186      def substitute(self,sub_dict):      def substitute(self,sub_dict):
1187          """          """
1188          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.
# Line 1390  class Volume(Manifold3D, Primitive): Line 1225  class Volume(Manifold3D, Primitive):
1225          for h in self.getHoles(): out+=h.getSurfaces()          for h in self.getHoles(): out+=h.getSurfaces()
1226          return out          return out
1227    
1228  class PropertySet(PrimitiveBase):  class PropertySet(Primitive, PrimitiveBase):
1229      """      """
1230      defines a group L{PrimitiveBase} objects.      defines a group of L{Primitive} which can be accessed through a name
1231      """      """
1232      def __init__(self,tag=None,*items):      def __init__(self,name,*items):
1233         super(PropertySet, self).__init__()         Primitive.__init__(self)
1234         self.__items=items         if len(items)==0:
1235         self.__tag=tag            raise ValueError("at least one item must be give.")
1236           if isinstance(items[0] ,Manifold1D):
1237                dim=1
1238           elif isinstance(items[0] ,Manifold2D):
1239                dim=2
1240           elif isinstance(items[0] ,Manifold3D):
1241               dim=3
1242           else:
1243               dim=0
1244           self.__dim=dim
1245           self.clearItems()
1246           self.addItem(*items)
1247           self.setName(name)
1248        def __repr__(self):
1249           """
1250           returns a string representation
1251           """
1252           return "%s(%s)"%(self.getName(),self.getID())
1253        def getManifoldClass(self):
1254            """
1255            returns the manifold class expected from items
1256            """
1257            d=self.getDim()
1258            if d==0:
1259               return Point
1260            elif d==1:
1261               return Manifold1D
1262            elif d==2:
1263               return Manifold2D
1264            else:
1265               return Manifold3D
1266        def getDim(self):
1267            """
1268            returns the dimension of the items
1269            """
1270            return self.__dim
1271        def getName(self):
1272            """
1273            returns the name of the set
1274            """
1275            return self.__name
1276        def setName(self,name):
1277            """
1278            sets the name.
1279            """
1280            self.__name=str(name)
1281            
1282        def addItem(self,*items):
1283            """
1284            adds items. An item my be any L{Primitive} but no L{PropertySet}
1285            """
1286            m=self.getManifoldClass()
1287            for i in items:
1288                if not i in self.__items:
1289                   if not isinstance(i, m):
1290                      raise TypeError("argument %s is not a %s class object."%(i, m.__name__))
1291                   self.__items.append(i)
1292        def getItems(self):
1293            """
1294            returns the list of items
1295            """
1296            return self.__items
1297    
1298        def clearItems(self):
1299            """
1300            clears the list of items
1301            """
1302            self.__items=[]
1303      def collectPrimitiveBases(self):      def collectPrimitiveBases(self):
1304          out=[self]+self.getBoundaryLoop().collectPrimitiveBases()          """
1305          for i in self.getHoles(): out+=i.collectPrimitiveBases()          returns primitives used to construct the PropertySet
1306            """
1307            out=[self]
1308            for i in self.getItems(): out+=i.collectPrimitiveBases()
1309          return out          return out
1310    
1311        def getTag(self):
1312             """
1313             returns the tag used for this property set
1314             """
1315             return self.getID()

Legend:
Removed from v.931  
changed lines
  Added in v.1109

  ViewVC Help
Powered by ViewVC 1.1.26