/[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 1270 by gross, Thu Aug 23 03:13:11 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 1017  class PlaneSurface(Primitive, Manifold2D Line 924  class PlaneSurface(Primitive, Manifold2D
924         """         """
925         if not isinstance(loop.getUnderlyingPrimitive(),CurveLoop):         if not isinstance(loop.getUnderlyingPrimitive(),CurveLoop):
926             raise TypeError("argument loop needs to be a CurveLoop object.")             raise TypeError("argument loop needs to be a CurveLoop object.")
927         for l in loop.getCurves():         # for l in loop.getCurves():
928             if not isinstance(l.getUnderlyingPrimitive(),Line):         #     if not isinstance(l.getUnderlyingPrimitive(),Line):
929               raise TypeError("loop may be formed by Lines only.")         #       raise TypeError("loop may be formed by Lines only.")
930         for i in range(len(holes)):         for i in range(len(holes)):
931              if not isinstance(holes[i].getUnderlyingPrimitive(), CurveLoop):              if not isinstance(holes[i].getUnderlyingPrimitive(), CurveLoop):
932                   raise TypeError("%i-th hole needs to be a CurveLoop object.")                   raise TypeError("%i-th hole needs to be a CurveLoop object.")
933              for l in holes[i].getCurves():         #      for l in holes[i].getCurves():
934                 if not isinstance(l.getUnderlyingPrimitive(),Line):         #         if not isinstance(l.getUnderlyingPrimitive(),Line):
935                    raise TypeError("holes may be formed by Lines only.")         #            raise TypeError("holes may be formed by Lines only.")
936         #TODO: check if lines and holes are in a plane         #TODO: check if lines and holes are in a plane
937         #TODO: are holes really holes?         #TODO: are holes really holes?
938         Primitive.__init__(self)         Primitive.__init__(self)
# 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         self.__dim=None
1235         self.__tag=tag         self.clearItems()
1236           self.addItem(*items)
1237           self.setName(name)
1238    
1239        def getDim(self):
1240           """
1241           returns the dimension of the items
1242           """
1243           if self.__dim == None:
1244               items=self.getItems()
1245               if len(items)>0:
1246                    if isinstance(items[0] ,Manifold1D):
1247                         self.__dim=1
1248                    elif isinstance(items[0] ,Manifold2D):
1249                         self.__dim=2
1250                    elif isinstance(items[0] ,Manifold3D):
1251                        self.__dim=3
1252                    else:
1253                        self.__dim=0
1254           return self.__dim
1255        def __repr__(self):
1256           """
1257           returns a string representation
1258           """
1259           return "%s(%s)"%(self.getName(),self.getID())
1260        def getManifoldClass(self):
1261            """
1262            returns the manifold class expected from items
1263            """
1264            d=self.getDim()
1265            if d == None:
1266               raise ValueError("undefined spatial diemnsion.")
1267            else:
1268               if d==0:
1269                  return Point
1270               elif d==1:
1271                  return Manifold1D
1272               elif d==2:
1273                  return Manifold2D
1274               else:
1275                  return Manifold3D
1276    
1277        def getName(self):
1278            """
1279            returns the name of the set
1280            """
1281            return self.__name
1282        def setName(self,name):
1283            """
1284            sets the name.
1285            """
1286            self.__name=str(name)
1287    
1288        def addItems(self,*items):
1289            """
1290            adds items. An item my be any L{Primitive} but no L{PropertySet}
1291            """
1292            self.addItem(*items)
1293    
1294        def addItem(self,*items):
1295            """
1296            adds items. An item my be any L{Primitive} but no L{PropertySet}
1297            """
1298            for i in items:
1299                if not i in self.__items:
1300                   if len(self.__items)>0:
1301                      m=self.getManifoldClass()
1302                      if not isinstance(i, m):
1303                         raise TypeError("argument %s is not a %s class object."%(i, m.__name__))
1304                   self.__items.append(i)
1305        def getNumItems(self):
1306            """
1307            returns the number of items in the property set
1308            """
1309            return len(self.__items)
1310    
1311        def getItems(self):
1312            """
1313            returns the list of items
1314            """
1315            return self.__items
1316    
1317        def clearItems(self):
1318            """
1319            clears the list of items
1320            """
1321            self.__items=[]
1322      def collectPrimitiveBases(self):      def collectPrimitiveBases(self):
1323          out=[self]+self.getBoundaryLoop().collectPrimitiveBases()          """
1324          for i in self.getHoles(): out+=i.collectPrimitiveBases()          returns primitives used to construct the PropertySet
1325            """
1326            out=[self]
1327            for i in self.getItems(): out+=i.collectPrimitiveBases()
1328          return out          return out
1329    
1330        def getTag(self):
1331             """
1332             returns the tag used for this property set
1333             """
1334             return self.getID()

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

  ViewVC Help
Powered by ViewVC 1.1.26