/[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 928 by gross, Tue Jan 16 08:36:03 2007 UTC revision 929 by gross, Wed Jan 17 07:41:13 2007 UTC
# Line 55  setToleranceForColocation() Line 55  setToleranceForColocation()
55    
56  class PrimitiveBase(object):  class PrimitiveBase(object):
57      """      """
58      template for elementary geometrical object      template for a set of primitives
59      """      """
60      def __init__(self,id):      def __init__(self):
61         """         """
62         initializes PrimitiveBase instance object with id         initializes PrimitiveBase instance object with id
63         """         """
64         self.__ID=id         pass
   
     def getID(self):  
        """  
        returns the primitive ID  
        """  
        return self.__ID  
   
     def __repr__(self):  
        return "%s(%s)"%(self.getClass().__name__,self.getID())  
65    
66      def __cmp__(self,other):      def __cmp__(self,other):
67         """         """
68         compares object with other by comparing the absolute value of the ID         compares object with other by comparing the absolute value of the ID
69         """         """
70         if isinstance(self, PrimitiveBase):         if isinstance(other, PrimitiveBase):
71             return cmp(abs(self.getID()),abs(other.getID()))             return cmp(self.getID(),other.getID())
72         else:         else:
73             return False             return False
   
74      def getConstructionPoints(self):      def getConstructionPoints(self):
75          """          """
76          returns the points used to construct the primitive          returns the points used to construct the primitive
# Line 95  class PrimitiveBase(object): Line 85  class PrimitiveBase(object):
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=set()
88          for p in self.collectPrimitives():          return list(set([p for p in self.collectPrimitiveBases()]))
            if isintance(p,ReversePrimitive):  
               out.add(-p)  
            elif isintance(p,Primitive):  
               out.add(p)  
         return list(set(self.collectPrimitives()))  
89    
90      def copy(self):      def copy(self):
91         """         """
# Line 176  class PrimitiveBase(object): Line 161  class PrimitiveBase(object):
161         """         """
162         for p in self.getConstructionPoints(): p.setLocalScale(factor)         for p in self.getConstructionPoints(): p.setLocalScale(factor)
163    
     def __neg__(self):  
         """  
         returns a view of the object with reverse orientiention  
         """  
         if isinstance(self,ReversePrimitive):  
             return self.getUnderlyingPrimitive()  
         else:  
             return ReversedPrimitive(self)  
   
164      def apply(self,transformation):      def apply(self,transformation):
165          """          """
166          returns a new object by applying the transformation          returns a new object by applying the transformation
# Line 193  class PrimitiveBase(object): Line 169  class PrimitiveBase(object):
169          out.modifyBy(transformation)          out.modifyBy(transformation)
170          return out          return out
171    
172      def collectPrimitives(self):  
173    
174    class Primitive(object):
175        """
176        A general primitive
177        """
178        def __init__(self):
179           """
180           initializes PrimitiveBase instance object with id
181           """
182           global global_primitive_id_counter
183           self.__ID=global_primitive_id_counter
184           global_primitive_id_counter+=1
185    
186        def getID(self):
187           """
188           returns the primitive ID
189           """
190           return self.__ID
191    
192        def getDirectedID(self):
193          """          """
194          returns a list of primitives used to construct the primitive. It may contain primitives twice          returns the primitive ID where a negative signs means that the reversed ordring is used.
195                    """
196            return self.getID()
197    
198        def __repr__(self):
199           return "%s(%s)"%(self.__class__.__name__,self.getID())
200    
201        def getUnderlyingPrimitive(self):
202            """
203            returns the underlying primitive
204            """
205            return self
206    
207        def __neg__(self):
208            """
209            returns a view onto the curve with reversed ordering
210    
211          @note: this class is overwritten by subclass          @note: this class is overwritten by subclass
212          """          """
213          return [self]          raise NotImplementedError("__neg__ is not implemented.")
214    
215      def getGmshCommand(self, local_scaling_factor=1.):      def getGmshCommand(self, local_scaling_factor=1.):
216          """          """
# Line 207  class PrimitiveBase(object): Line 218  class PrimitiveBase(object):
218    
219          @note: this class is overwritten by subclass          @note: this class is overwritten by subclass
220          """          """
221          raise NotImplementedError("getGmshCommand is not implemented for this class %s."%self.__class__.__name__)          raise NotImplementedError("getGmshCommand is not implemented.")
   
     def isColocated(self,primitive):  
        """  
        returns True is the two primitives are located at the smae position  
   
        @note: this class is overwritten by subclass  
        """  
        raise NotImplementedError("isColocated is not implemented for this class %s."%self.__class__.__name__)  
222    
223      def substitute(self,sub_dict):      def substitute(self,sub_dict):
224          """          """
# Line 225  class PrimitiveBase(object): Line 228  class PrimitiveBase(object):
228    
229          @note: this class is overwritten by subclass          @note: this class is overwritten by subclass
230          """          """
231          if not sub_dict.has_key(self):          raise NotImplementedError("substitute is not implemented.")
            sub_dict[self]=self.getClass()  
         return sub_dict[self]  
232    
233  class Primitive(PrimitiveBase):      def collectPrimitiveBases(self):
     """  
     A general primitive  
     """  
     def __init__(self):  
        """  
        instantiate a primitve  
        """  
        global global_primitive_id_counter  
        self.__ID=global_primitive_id_counter  
        PrimitiveBase.__init__(self,global_primitive_id_counter)  
   
     def getClass(self):  
234          """          """
235          returns the class of the object          returns a list of primitives used to construct the primitive. It may contain primitives twice
236            
237            @note: this class is overwritten by subclass
238          """          """
239          return self.__class__          raise NotImplementedError("collectPrimitiveBases is not implemented.")
240    
241        def isColocated(self,primitive):
242           """
243           returns True is the two primitives are located at the smae position
244    
245           @note: this class is overwritten by subclass
246           """
247           raise NotImplementedError("isColocated is not implemented.")
248    
249    
250  class ReversePrimitive(PrimitiveBase):  class ReversePrimitive(object):
251      """      """
252      A view onto a primitive creating an reverse orientation      A view onto a primitive creating an reverse orientation
253      """      """
# Line 255  class ReversePrimitive(PrimitiveBase): Line 255  class ReversePrimitive(PrimitiveBase):
255         """         """
256         instantiate a view onto primitve         instantiate a view onto primitve
257         """         """
258         PrimitiveBase.__init__(self,-primitive.getID())         if not isinstance(primitive, Primitive):
259               raise ValueError("argument needs to be a Primitive class object.")
260         self.__primitive=primitive         self.__primitive=primitive
261    
262        def getID(self):
263           """
264           returns the primitive ID
265           """
266           return self.__primitive.getID()
267    
268      def getUnderlyingPrimitive(self):      def getUnderlyingPrimitive(self):
269          """          """
270          returns the underlying primitive          returns the underlying primitive
271          """          """
272          return self.__primitive          return self.__primitive
273    
274      def getClass(self):      def __repr__(self):
275           return "-%s(%s)"%(self.__primitive.__class__.__name__,self.getID())
276    
277        def getDirectedID(self):
278            """
279            returns the primitive ID where a negative signs means that the reversed ordring is used.
280            """
281            return -self.__primitive.getID()
282    
283        def substitute(self,sub_dict):
284          """          """
285          returns the class of the underlying object          returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.
286            If a substitute for the object is given by C{sub_dict} the value is returned, otherwise a new instance
287            with substituted arguments is returned.
288          """          """
289          return self.getUnderlyingPrimitive().__class__()          if not sub_dict.has_key(self):
290                sub_dict[self]=-self.getUnderlyingPrimitive().substitute(sub_dict)
291            return sub_dict[self]
292                
293        def __neg__(self):
294              """
295              returns a view onto the curve with reversed ordering
296              """
297              return self.__primitive
298    
299        def getGmshCommand(self, local_scaling_factor=1.):
300            """
301            returns the Gmsh command(s) to create the primitive
302            """
303            return self.__primitive.getGmshCommand(local_scaling_factor)
304    
305  class Point(Primitive):      def collectPrimitiveBases(self):
306            """
307            returns a list of primitives used to construct the primitive. It may contain primitives twice
308            """
309            return self.__primitive.collectPrimitiveBases()
310    
311        def isColocated(self,primitive):
312           """
313           returns True is the two primitives are located at the smae position
314    
315           @note: this class is overwritten by subclass
316           """
317           return self.__primitive.isColocated(primitive)
318    
319    class Point(Primitive, PrimitiveBase):
320      """      """
321      a three dimensional point      a three dimensional point
322      """      """
# Line 278  class Point(Primitive): Line 324  class Point(Primitive):
324         """         """
325         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
326         """         """
327           PrimitiveBase.__init__(self)
328         Primitive.__init__(self)         Primitive.__init__(self)
329         self.setCoordinates(numarray.array([x,y,z],_TYPE))         self.setCoordinates(numarray.array([x,y,z],_TYPE))
330         self.setLocalScale(local_scale)         self.setLocalScale(local_scale)
# Line 289  class Point(Primitive): Line 336  class Point(Primitive):
336         if factor<=0.:         if factor<=0.:
337            raise ValueError("scaling factor must be positive.")            raise ValueError("scaling factor must be positive.")
338         self.__local_scale=factor         self.__local_scale=factor
339    
340      def getLocalScale(self):      def getLocalScale(self):
341         """         """
342         returns the local refinement factor         returns the local refinement factor
# Line 308  class Point(Primitive): Line 356  class Point(Primitive):
356         else:         else:
357            self._x=x            self._x=x
358    
359      def collectPrimitives(self):      def collectPrimitiveBases(self):
360         """         """
361         returns primitives used to construct the primitive         returns primitives used to construct the primitive
362         """         """
# Line 366  class Manifold1D(PrimitiveBase): Line 414  class Manifold1D(PrimitiveBase):
414          """          """
415          create a one-dimensional manifold          create a one-dimensional manifold
416          """          """
417          PrimitiveBass.__init__(self)          PrimitiveBase.__init__(self)
418    
419      def getStartPoint(self):      def getStartPoint(self):
420           """           """
# Line 381  class Manifold1D(PrimitiveBase): Line 429  class Manifold1D(PrimitiveBase):
429           raise NotImplementedError()           raise NotImplementedError()
430    
431  class CurveBase(Manifold1D):  class CurveBase(Manifold1D):
432        """
433        A Curve is defined by a set of control points
434        """
435      def __init__(self):      def __init__(self):
436            """            """
437            create a one-dimensional primitive            create curve
438            """            """
439            Manifold1D.__init__(self)            Manifold1D.__init__(self)
440    
# Line 397  class CurveBase(Manifold1D): Line 448  class CurveBase(Manifold1D):
448           """           """
449           returns start point           returns start point
450           """           """
451           return self.getControlPoints[0]           return self.getControlPoints()[0]
452    
453      def getEndPoint(self):      def getEndPoint(self):
454           """           """
455           returns end point           returns end point
456           """           """
457           return self.getControlPoints[-1]           return self.getControlPoints()[-1]
458    
459      def collectPrimitives(self):      def getControlPoints(self):
460             """
461             returns a list of the points
462             """
463             raise NotImplementedError()
464    
465    class Curve(CurveBase, Primitive):
466        """
467        a curve defined through a list of control points.
468        """
469        def __init__(self,*points):
470         """         """
471         returns primitives used to construct the Curve         defines a curve form control points
472         """         """
473         out=[self]         if len(points)<2:
474         for p in self.getControlPoints(): out+=p.collectPrimitives()             raise TypeError("Curve needs at least two points")
475         return out         i=0
476           for p in points:
477                  i+=1
478                  if not isinstance(p,Point): raise TypeError("%s-th argument is not a Point object."%i)
479           self.__points=points
480           CurveBase.__init__(self)
481           Primitive.__init__(self)
482    
483        def getControlPoints(self):
484             """
485             returns a list of the points
486             """
487             return self.__points
488          
489        def __neg__(self):
490              """
491              returns a view onto the curve with reversed ordering
492              """
493              return ReverseCurve(self)
494    
495      def substitute(self,sub_dict):      def substitute(self,sub_dict):
496          """          """
# Line 422  class CurveBase(Manifold1D): Line 501  class CurveBase(Manifold1D):
501          if not sub_dict.has_key(self):          if not sub_dict.has_key(self):
502              new_p=[]              new_p=[]
503              for p in self.getControlPoints(): new_p.append(p.substitute(sub_dict))              for p in self.getControlPoints(): new_p.append(p.substitute(sub_dict))
504              sub_dict[self]=self.getGeneratingClass()(*tuple(new_p))              sub_dict[self]=self.__class__(*tuple(new_p))
505          return sub_dict[self]          return sub_dict[self]
506    
507        def collectPrimitiveBases(self):
508           """
509           returns primitives used to construct the Curve
510           """
511           out=[self]
512           for p in self.getControlPoints(): out+=p.collectPrimitiveBases()
513           return out
514    
515      def isColocated(self,primitive):      def isColocated(self,primitive):
516         """         """
517         returns True curves are on the same position         returns True curves are on the same position
518         """         """
519         if isinstance(primitive,self.__class__):         if isinstance(primitive.getUnderlyingPrimitive(),self.__class__):
520            if len(primitive) == len(self):            if len(primitive) == len(self):
521               cp0=self.getControlPoints()               cp0=self.getControlPoints()
522               cp1=primitive.getControlPoints()               cp1=primitive.getControlPoints()
# Line 448  class CurveBase(Manifold1D): Line 535  class CurveBase(Manifold1D):
535         else:         else:
536            return False            return False
537    
538          class ReverseCurve(CurveBase, ReversePrimitive):
 class Curve(CurveBase):  
     """  
     a curve defined through a list of control points.  
     """  
     def __init__(self,*points):  
        """  
        defines a curve form control points  
        """  
        CurveBase.__init__(self)  
        if len(points)<2:  
            raise TypeError("Curve needs at least two points")  
        super(Curve, self).__init__()  
        i=0  
        for p in points:  
               i+=1  
               if not isinstance(p,Point): raise TypeError("%s-th argument is not a Point object."%i)  
        self.__points=points  
   
     def __neg__(self):  
           """  
           returns a view onto the curce with reversed ordering  
           """  
           return ReverseCurve(self)  
     def getControlPoints(self):  
          """  
          returns a list of the points  
          """  
          return self.__points  
   
     def getGeneratingClass(self):  
          return self.__class__  
   
 class ReverseCurve(CurveBase):  
539      """      """
540      a curve defined through a list of control points.      a curve defined through a list of control points.
541      """      """
# Line 489  class ReverseCurve(CurveBase): Line 543  class ReverseCurve(CurveBase):
543         """         """
544         defines a curve form control points         defines a curve form control points
545         """         """
546           if not isinstance(curve, Curve):
547               raise TypeError("ReverseCurve needs to be an instance of Curve")
548         CurveBase.__init__(self)         CurveBase.__init__(self)
549         if not isinstance(curve, CurveBase):         ReversePrimitive.__init__(self,curve)
            raise TypeError("curve needs to be an instance of Curve")  
        self.__curve=curve  
550    
     def __neg__(self):  
           """  
           returns a view onto the curce with reversed ordering  
           """  
           return self.__curve  
551      def getControlPoints(self):      def getControlPoints(self):
552           """           """
553           returns a list of the points           returns a list of the points
554           """           """
555           out=[p for p in self.__curve.getControlPoints()]           out=[p for p in self.getUnderlyingPrimitive().getControlPoints()]
556           out.reverse()           out.reverse()
557           return out           return out
     def getGeneratingClass(self):  
          return self.__curve.getGeneratingClass()  
558    
559  class Spline(Curve):  class Spline(Curve):
560      """      """
561      a spline curve defined through a list of control points.      a spline curve defined through a list of control points.
562      """      """
563      def getGmshCommand(self):      def getGmshCommand(self,scaling_factor=1.):
564          """          """
565          returns the Gmsh command(s) to create the Curve          returns the Gmsh command(s) to create the Curve
566          """          """
567          out=""          out=""
568          for i in self.getControlPoints():          for i in self.getControlPoints():
569              if len(out)>0:              if len(out)>0:
570                  out+=", %s"%i.getID()                  out+=", %s"%i.getDirectedID()
571              else:              else:
572                  out="%s"%i.getID()                  out="%s"%i.getDirectedID()
573          return "Spline(%s) = {%s};"%(self.getID(),out)          return "Spline(%s) = {%s};"%(self.getID(),out)
574            
575    
# Line 530  class BezierCurve(Curve): Line 577  class BezierCurve(Curve):
577      """      """
578      a Bezier curve      a Bezier curve
579      """      """
580      def getGmshCommand(self):      def getGmshCommand(self,scaling_factor=1.):
581          """          """
582          returns the Gmsh command(s) to create the Curve          returns the Gmsh command(s) to create the Curve
583          """          """
584          out=""          out=""
585          for i in self.getControlPoints():          for i in self.getControlPoints():
586              if len(out)>0:              if len(out)>0:
587                  out+=", %s"%i.getID()                  out+=", %s"%i.getDirectedID()
588              else:              else:
589                  out="%s"%i.getID()                  out="%s"%i.getDirectedID()
590          return "Bezier(%s) = {%s};"%(self.getID(),out)          return "Bezier(%s) = {%s};"%(self.getID(),out)
591    
592  class BSpline(Curve):  class BSpline(Curve):
593      """      """
594      a BSpline curve. Control points may be repeated.      a BSpline curve. Control points may be repeated.
595      """      """
596      def getGmshCommand(self):      def getGmshCommand(self,scaling_factor=1.):
597          """          """
598          returns the Gmsh command(s) to create the Curve          returns the Gmsh command(s) to create the Curve
599          """          """
600          out=""          out=""
601          for i in self.getControlPoints():          for i in self.getControlPoints():
602              if len(out)>0:              if len(out)>0:
603                  out+=", %s"%i.getID()                  out+=", %s"%i.getDirectedID()
604              else:              else:
605                  out="%s"%i.getID()                  out="%s"%i.getDirectedID()
606          return "BSpline(%s) = {%s};"%(self.getID(),out)          return "BSpline(%s) = {%s};"%(self.getID(),out)
607    
608  class Line(Curve):  class Line(Curve):
609      """      """
610      a line is defined by two L{Point}s      a line is defined by two pointDirecteds
611      """      """
612      def __init__(self,*points):      def __init__(self,*points):
613          """          """
# Line 568  class Line(Curve): Line 615  class Line(Curve):
615          """          """
616          if len(points)!=2:          if len(points)!=2:
617             raise TypeError("Line needs two points")             raise TypeError("Line needs two points")
618          super(Line, self).__init__(*points)          Curve.__init__(self,*points)
619      def getGmshCommand(self):      def getGmshCommand(self,scaling_factor=1.):
620          """          """
621          returns the Gmsh command(s) to create the Curve          returns the Gmsh command(s) to create the Curve
622          """          """
623          return "Line(%s) = {%s, %s};"%(self.getID(),self.getStartPoint().getID(),self.getEndPoint().getID())          return "Line(%s) = {%s, %s};"%(self.getID(),self.getStartPoint().getDirectedID(),self.getEndPoint().getDirectedID())
624    
625    
626  class ArcBase(Manifold1D):  class ArcBase(Manifold1D):
627      def collectPrimitives(self):      def __init__(self):
628              """
629              create curve
630              """
631              Manifold1D.__init__(self)
632        def collectPrimitiveBases(self):
633         """         """
634         returns the primitives used to construct the Curve         returns the primitives used to construct the Curve
635         """         """
636         out=[self]         out=[self]
637         out+=self.getStartPoint().collectPrimitives()         out+=self.getStartPoint().collectPrimitiveBases()
638         out+=self.getEndPoint().collectPrimitives()         out+=self.getEndPoint().collectPrimitiveBases()
639         out+=self.getCenterPoint().collectPrimitives()         out+=self.getCenterPoint().collectPrimitiveBases()
640         return out         return out
641    
     def getGmshCommand(self):  
        """  
        returns the Gmsh command(s) to create the primitive  
        """  
        return "Circle(%s) = {%s, %s, %s};"%(self.getID(),self.getStartPoint().getID(),self.getCenterPoint().getID(),self.getEndPoint().getID())  
   
     def substitute(self,sub_dict):  
         """  
         returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.  
         If a substitute for the object is given by C{sub_dict} the value is returned, otherwise a new instance  
         with substituted arguments is returned.  
         """  
         if not sub_dict.has_key(self):  
             sub_dict[self]=Arc(self.getCenterPoint().substitute(sub_dict),self.getStartPoint().substitute(sub_dict),self.getEndPoint().substitute(sub_dict))  
         return sub_dict[self]  
642    
643      def isColocated(self,primitive):      def getCenterPoint(self):
644         """           """
645         returns True curves are on the same position           returns center
646         """           """
647         if isinstance(primitive,Arc):           raise NotImplementedError()
             return (self.getCenterPoint().isColocated(primitive.getCenterPoint())) and ( \  
                    (self.getEndPoint().isColocated(primitive.getEndPoint()) and self.getStartPoint().isColocated(primitive.getStartPoint()) ) \  
                 or (self.getEndPoint().isColocated(primitive.getStartPoint()) and self.getStartPoint().isColocated(primitive.getEndPoint()) ) )  
        else:  
           return False  
648    
649  class Arc(ArcBase):  class Arc(ArcBase, Primitive):
650      """      """
651      defines an arc which is strictly, smaller than Pi      defines an arc which is strictly, smaller than Pi
652      """      """
# Line 627  class Arc(ArcBase): Line 659  class Arc(ArcBase):
659         if not isinstance(start,Point): raise TypeError("start needs to be a Point object.")         if not isinstance(start,Point): raise TypeError("start needs to be a Point object.")
660         # TODO: check length of circle.         # TODO: check length of circle.
661         ArcBase.__init__(self)         ArcBase.__init__(self)
662           Primitive.__init__(self)
663         self.__center=center         self.__center=center
664         self.__start=start         self.__start=start
665         self.__end=end         self.__end=end
666      def __neg__(self):      def __neg__(self):
667         return ReverseArc(self)            """
668              returns a view onto the curve with reversed ordering
669              """
670              return ReverseArc(self)
671    
672      def getStartPoint(self):      def getStartPoint(self):
673         """         """
# Line 651  class Arc(ArcBase): Line 687  class Arc(ArcBase):
687         """         """
688         return self.__center         return self.__center
689    
690  class ReverseArc(_ArcBase):      def substitute(self,sub_dict):
691            """
692            returns a copy of self with substitutes for the primitives used to construct it given by the dictionary C{sub_dict}.
693            If a substitute for the object is given by C{sub_dict} the value is returned, otherwise a new instance
694            with substituted arguments is returned.
695            """
696            if not sub_dict.has_key(self):
697                sub_dict[self]=Arc(self.getCenterPoint().substitute(sub_dict),self.getStartPoint().substitute(sub_dict),self.getEndPoint().substitute(sub_dict))
698            return sub_dict[self]
699    
700        def getGmshCommand(self,scaling_factor=1.):
701           """
702           returns the Gmsh command(s) to create the primitive
703           """
704           return "Circle(%s) = {%s, %s, %s};"%(self.getID(),self.getStartPoint().getDirectedID(),self.getCenterPoint().getDirectedID(),self.getEndPoint().getDirectedID())
705    
706        def isColocated(self,primitive):
707           """
708           returns True curves are on the same position
709           """
710           if isinstance(primitive.getUnderlyingPrimitive(),Arc):
711                return (self.getCenterPoint().isColocated(primitive.getCenterPoint())) and ( \
712                       (self.getEndPoint().isColocated(primitive.getEndPoint()) and self.getStartPoint().isColocated(primitive.getStartPoint()) ) \
713                    or (self.getEndPoint().isColocated(primitive.getStartPoint()) and self.getStartPoint().isColocated(primitive.getEndPoint()) ) )
714           else:
715              return False
716    
717    class ReverseArc(ArcBase, ReversePrimitive):
718      """      """
719      defines an arc which is strictly, smaller than Pi      defines an arc which is strictly, smaller than Pi
720      """      """
# Line 659  class ReverseArc(_ArcBase): Line 722  class ReverseArc(_ArcBase):
722         """         """
723         creates an arc by the start point, end point and center         creates an arc by the start point, end point and center
724         """         """
725         if not isinstance(arc,Arc): raise TypeError("arc needs to be an instance of Arc.")         if not isinstance(arc, Arc):
726               raise TypeError("ReverseCurve needs to be an instance of Arc")
727         ArcBase.__init__(self)         ArcBase.__init__(self)
728         self.__arc=arc         ReversePrimitive.__init__(self,arc)
729    
730      def getStartPoint(self):      def getStartPoint(self):
731         """         """
732         returns start point         returns start point
733         """         """
734         return self.__arc.getEndPoint()         return self.getUnderlyingPrimitive().getEndPoint()
735    
736      def getEndPoint(self):      def getEndPoint(self):
737         """         """
738         returns end point         returns end point
739         """         """
740         return self.__arc.getStartPoint()         return self.getUnderlyingPrimitive().getStartPoint()
741    
742      def getCenterPoint(self):      def getCenterPoint(self):
743         """         """
744         returns center         returns center
745         """         """
746         return self.__arc.getCenterPoint()         return self.getUnderlyingPrimitive().getCenterPoint()
   
     def __neg__(self):  
        return self.__arc  
747    
748  #====================================  class CurveLoop(Primitive, PrimitiveBase):
 class CurveLoop(Primitive):  
749      """      """
750      An oriented loop of 1D primitives (= curves and arcs)      An oriented loop of 1D primitives (= curves and arcs)
751    
# Line 695  class CurveLoop(Primitive): Line 755  class CurveLoop(Primitive):
755         """         """
756         creates a polygon from a list of line curves. The curves must form a closed loop.         creates a polygon from a list of line curves. The curves must form a closed loop.
757         """         """
758         super(CurveLoop, self).__init__()         Primitive.__init__(self)
759           PrimitiveBase.__init__(self)
760         if len(curves)<2:         if len(curves)<2:
761              raise TypeError("at least two curves have to be given.")              raise TypeError("at least two curves have to be given.")
762         for i in range(len(curves)):         for i in range(len(curves)):
# Line 725  class CurveLoop(Primitive): Line 786  class CurveLoop(Primitive):
786         """         """
787         return self.__curves         return self.__curves
788    
789        def __neg__(self):
790           """
791           returns a view onto the curve with reversed ordering
792           """
793           return ReverseCurveLoop(self)
794    
795      def __len__(self):      def __len__(self):
796         """         """
797         return the number of curves in the CurveLoop         return the number of curves in the CurveLoop
798         """         """
799         return len(self.getCurves())         return len(self.getCurves())
800    
801      def collectPrimitives(self):  
802        def collectPrimitiveBases(self):
803         """         """
804         returns primitives used to construct the CurveLoop         returns primitives used to construct the CurveLoop
805         """         """
806         out=[self]         out=[self]
807         for c in self.getCurves(): out+=c.collectPrimitives()         for c in self.getCurves(): out+=c.collectPrimitiveBases()
808         return out         return out
809    
810      def substitute(self,sub_dict):      def substitute(self,sub_dict):
# Line 770  class CurveLoop(Primitive): Line 838  class CurveLoop(Primitive):
838         else:         else:
839            return False            return False
840    
841      def getGmshCommand(self):      def getGmshCommand(self,scaling_factor=1.):
842          """          """
843          returns the Gmsh command(s) to create the primitive          returns the Gmsh command(s) to create the primitive
844          """          """
845          out=""          out=""
846          for i in self.getCurves():          for i in self.getCurves():
847              if len(out)>0:              if len(out)>0:
848                  out+=", %s"%i.getID()                  out+=", %s"%i.getDirectedID()
849              else:              else:
850                  out="%s"%i.getID()                  out="%s"%i.getDirectedID()
851          return "Line Loop(%s) = {%s};"%(self.getID(),out)          return "Line Loop(%s) = {%s};"%(self.getID(),out)
852    
853  class Primitive2D(Primitive):  class ReverseCurveLoop(ReversePrimitive, PrimitiveBase):
854        """
855        An oriented loop of 1D primitives (= curves and arcs)
856    
857        The loop must be closed and the L{Manifold1D}s should be oriented consistently.
858        """
859        def __init__(self,curve_loop):
860           """
861           creates a polygon from a list of line curves. The curves must form a closed loop.
862           """
863           if not isinstance(curve_loop, CurveLoop):
864               raise ValueError("arguments need to be an instance of CurveLoop.")
865           ReversePrimitive.__init__(self, curve_loop)
866           PrimitiveBase.__init__(self)
867    
868        def getCurves(self):
869           """
870           returns the curves defining the CurveLoop
871           """
872           return [ -c for c in  self.getUnderlyingPrimitive().getCurves() ]
873    
874        def __len__(self):
875            return len(self.getUnderlyingPrimitive())
876    
877    class PrimitiveBase2D(PrimitiveBase):
878      """      """
879      general two-dimensional primitive      general two-dimensional primitive
880      """      """
# Line 790  class Primitive2D(Primitive): Line 882  class Primitive2D(Primitive):
882            """            """
883            create a two-dimensional primitive            create a two-dimensional primitive
884            """            """
885            super(Primitive2D, self).__init__()            super(PrimitiveBase2D, self).__init__()
886    
887      def getBoundary(self):      def getBoundary(self):
888          """          """
# Line 822  class Primitive2D(Primitive): Line 914  class Primitive2D(Primitive):
914         """         """
915         raise NotImplementedError("getHoles is not implemented for this class %s."%self.__class__.__name__)         raise NotImplementedError("getHoles is not implemented for this class %s."%self.__class__.__name__)
916    
917      def collectPrimitives(self):      def collectPrimitiveBases(self):
918          """          """
919          returns primitives used to construct the Surface          returns primitives used to construct the Surface
920          """          """
921          out=[self] + self.getBoundaryLoop().collectPrimitives()          out=[self] + self.getBoundaryLoop().collectPrimitiveBases()
922          for i in self.getHoles(): out+=i.collectPrimitives()          for i in self.getHoles(): out+=i.collectPrimitiveBases()
923          return out          return out
924    
925  class RuledSurface(Primitive2D):  class RuledSurface(PrimitiveBase2D):
926      """      """
927      A ruled surface, i.e., a surface that can be interpolated using transfinite interpolation      A ruled surface, i.e., a surface that can be interpolated using transfinite interpolation
928      """      """
# Line 862  class RuledSurface(Primitive2D): Line 954  class RuledSurface(Primitive2D):
954          """          """
955          return []          return []
956    
957      def getGmshCommand(self):      def getGmshCommand(self,scaling_factor=1.):
958          """          """
959          returns the Gmsh command(s) to create the primitive          returns the Gmsh command(s) to create the primitive
960          """          """
961          return "Ruled Surface(%s) = {%s};"%(self.getID(),self.getBoundaryLoop().getID())          return "Ruled Surface(%s) = {%s};"%(self.getID(),self.getBoundaryLoop().getDirectedID())
962    
963      def substitute(self,sub_dict):      def substitute(self,sub_dict):
964          """          """
# Line 893  def createRuledSurface(*curves): Line 985  def createRuledSurface(*curves):
985        """        """
986        return RuledSurface(CurveLoop(*curves))        return RuledSurface(CurveLoop(*curves))
987    
988  class PlaneSurface(Primitive2D):  class PlaneSurface(PrimitiveBase2D):
989      """      """
990      a plane surface with holes      a plane surface with holes
991      """      """
# Line 933  class PlaneSurface(Primitive2D): Line 1025  class PlaneSurface(Primitive2D):
1025          """          """
1026          return self.__loop          return self.__loop
1027    
1028      def getGmshCommand(self):      def getGmshCommand(self,scaling_factor=1.):
1029          """          """
1030          returns the Gmsh command(s) to create the primitive          returns the Gmsh command(s) to create the primitive
1031          """          """
1032          out=""          out=""
1033          for i in self.getHoles():          for i in self.getHoles():
1034              if len(out)>0:              if len(out)>0:
1035                  out+=", %s"%i.getID()                  out+=", %s"%i.getDirectedID()
1036              else:              else:
1037                  out="%s"%i.getID()                  out="%s"%i.getDirectedID()
1038          if len(out)>0:          if len(out)>0:
1039            return "Plane Surface(%s) = {%s, %s};"%(self.getID(),self.getBoundaryLoop().getID(), out)            return "Plane Surface(%s) = {%s, %s};"%(self.getID(),self.getBoundaryLoop().getDirectedID(), out)
1040          else:          else:
1041            return "Plane Surface(%s) = {%s};"%(self.getID(),self.getBoundaryLoop().getID())            return "Plane Surface(%s) = {%s};"%(self.getID(),self.getBoundaryLoop().getDirectedID())
1042    
1043      def substitute(self,sub_dict):      def substitute(self,sub_dict):
1044          """          """
# Line 979  class PlaneSurface(Primitive2D): Line 1071  class PlaneSurface(Primitive2D):
1071         else:         else:
1072            return False            return False
1073    
1074  class SurfaceLoop(Primitive):  class SurfaceLoop(PrimitiveBase):
1075      """      """
1076      a loop of 2D primitives. It defines the shell of a volume.      a loop of 2D primitives. It defines the shell of a volume.
1077    
# Line 993  class SurfaceLoop(Primitive): Line 1085  class SurfaceLoop(Primitive):
1085         if len(surfaces)<2:         if len(surfaces)<2:
1086              raise TypeError("at least two surfaces have to be given.")              raise TypeError("at least two surfaces have to be given.")
1087         for i in range(len(surfaces)):         for i in range(len(surfaces)):
1088             if not isinstance(surfaces[i],Primitive2D):             if not isinstance(surfaces[i],PrimitiveBase2D):
1089                raise TypeError("%s-th argument is not a Primitive2D object."%i)                raise TypeError("%s-th argument is not a PrimitiveBase2D object."%i)
1090         # for the curves a loop:         # for the curves a loop:
1091         used=[ True for s in surfaces]         used=[ True for s in surfaces]
1092         self.__surfaces=[surfaces[0]]         self.__surfaces=[surfaces[0]]
# Line 1035  class SurfaceLoop(Primitive): Line 1127  class SurfaceLoop(Primitive):
1127         """         """
1128         return self.__curves         return self.__curves
1129    
1130      def collectPrimitives(self):      def collectPrimitiveBases(self):
1131         """         """
1132         returns primitives used to construct the SurfaceLoop         returns primitives used to construct the SurfaceLoop
1133         """         """
1134         out=[self]         out=[self]
1135         for c in self.getSurfaces(): out+=c.collectPrimitives()         for c in self.getSurfaces(): out+=c.collectPrimitiveBases()
1136         return out         return out
1137      def getGmshCommand(self):      def getGmshCommand(self,scaling_factor=1.):
1138          """          """
1139          returns the Gmsh command(s) to create the primitive          returns the Gmsh command(s) to create the primitive
1140          """          """
1141          out=""          out=""
1142          for i in self.getSurfaces():          for i in self.getSurfaces():
1143              if len(out)>0:              if len(out)>0:
1144                  out+=", %s"%i.getID()                  out+=", %s"%i.getDirectedID()
1145              else:              else:
1146                  out="%s"%i.getID()                  out="%s"%i.getDirectedID()
1147          return "Surface Loop(%s) = {%s};"%(self.getID(),out)          return "Surface Loop(%s) = {%s};"%(self.getID(),out)
1148      def substitute(self,sub_dict):      def substitute(self,sub_dict):
1149          """          """
# Line 1085  class SurfaceLoop(Primitive): Line 1177  class SurfaceLoop(Primitive):
1177           return False           return False
1178    
1179  #==========================  #==========================
1180  class Volume(Primitive):  class Volume(PrimitiveBase):
1181      """      """
1182      a volume with holes.      a volume with holes.
1183      """      """
# Line 1112  class Volume(Primitive): Line 1204  class Volume(Primitive):
1204         return self.__loop         return self.__loop
1205      def __add__(self,other):      def __add__(self,other):
1206         return Volume(self.getSurfaceLoop()+other, holes=[h+other for h in self.getHoles()])         return Volume(self.getSurfaceLoop()+other, holes=[h+other for h in self.getHoles()])
1207      def collectPrimitives(self):      def collectPrimitiveBases(self):
1208          out=[self] + self.getSurfaceLoop().collectPrimitives()          out=[self] + self.getSurfaceLoop().collectPrimitiveBases()
1209          for i in self.getHoles(): out+=i.collectPrimitives()          for i in self.getHoles(): out+=i.collectPrimitiveBases()
1210          return out          return out
1211      def getConstructionPoints(self):      def getConstructionPoints(self):
1212          out=self.getSurfaceLoop().getConstructionPoints()          out=self.getSurfaceLoop().getConstructionPoints()
1213          for i in self.getHoles(): out|=i.Points()          for i in self.getHoles(): out|=i.Points()
1214          return out          return out
1215      def getGmshCommand(self):      def getGmshCommand(self,scaling_factor=1.):
1216          """          """
1217          returns the Gmsh command(s) to create the primitive          returns the Gmsh command(s) to create the primitive
1218          """          """
1219          out=""          out=""
1220          for i in self.getHoles():          for i in self.getHoles():
1221              if len(out)>0:              if len(out)>0:
1222                  out+=", %s"%i.getID()                  out+=", %s"%i.getDirectedID()
1223              else:              else:
1224                  out="%s"%i.getID()                  out="%s"%i.getDirectedID()
1225          if len(out)>0:          if len(out)>0:
1226            return "Volume(%s) = {%s, %s};"%(self.getID(),self.getSurfaceLoop().getID(), out)            return "Volume(%s) = {%s, %s};"%(self.getID(),self.getSurfaceLoop().getDirectedID(), out)
1227          else:          else:
1228            return "Volume(%s) = {%s};"%(self.getID(),self.getSurfaceLoop().getID())            return "Volume(%s) = {%s};"%(self.getID(),self.getSurfaceLoop().getDirectedID())
   
 class ReversedPrimitive(object):  
     def __init__(self,prim):  
        self.__prim=prim  
     def __getattr__(self,name):  
        if name == "getID":  
           return self.getReverseID  
        else:  
           return getattr(self.__prim,name)  
     def getReverseID(self):  
         return -self.__prim.getID()  
1229    
1230  class PropertySet(Primitive):  class PropertySet(PrimitiveBase):
1231      """      """
1232      defines a group L{Primitive} objects.      defines a group L{PrimitiveBase} objects.
1233      """      """
1234      def __init__(self,tag=None,*items):      def __init__(self,tag=None,*items):
1235         super(PropertySet, self).__init__()         super(PropertySet, self).__init__()
1236         self.__items=items         self.__items=items
1237         self.__tag=tag         self.__tag=tag
1238      def collectPrimitives(self):      def collectPrimitiveBases(self):
1239          out=[self]+self.getBoundaryLoop().collectPrimitives()          out=[self]+self.getBoundaryLoop().collectPrimitiveBases()
1240          for i in self.getHoles(): out+=i.collectPrimitives()          for i in self.getHoles(): out+=i.collectPrimitiveBases()
1241          return out          return out
1242    
1243  class PrimitiveStack(object):  class PrimitiveBaseStack(object):
1244        def __init__(self,*items):        def __init__(self,*items):
1245          self.__prims=set()          self.__prims=set()
1246          for i in items:          for i in items:
# Line 1167  class PrimitiveStack(object): Line 1248  class PrimitiveStack(object):
1248          self.__prims=list(self.__prims)          self.__prims=list(self.__prims)
1249          self.__prims.sort()          self.__prims.sort()
1250    
1251        def getGmshCommands(self):        def getGmshCommands(self,scaling_factor=1.):
1252          out=""          out=""
1253          for i in self.__prims:          for i in self.__prims:
1254             out+=i.getGmshCommand()+"\n"             out+=i.getGmshCommand(scaling_factor)+"\n"
1255          return out          return out

Legend:
Removed from v.928  
changed lines
  Added in v.929

  ViewVC Help
Powered by ViewVC 1.1.26