コード例 #1
0
ファイル: draw.py プロジェクト: BackupTheBerlios/pyformex-svn
def flyAlong(path="flypath", upvector=[0.0, 1.0, 0.0], sleeptime=None):
    """Fly through the current scene along the flypath.

    - `flypath`: a PolyLine or plex-2 Formex.
    """
    from plugins.curve import PolyLine

    if type(path) is str:
        path = named(path)
    if not path:
        warning("You have to define a flypath first!")
        return

    if isinstance(path, PolyLine):
        path = path.toFormex()
    if path.nplex() != 2:
        warning("The flypath should be a plex-2 Formex!")

    for eye, center in path:
        pf.debug("Eye: %s; Center: %s" % (eye, center))
        pf.canvas.camera.lookAt(eye, center, upvector)
        pf.canvas.display()
        pf.canvas.update()
        image.saveNext()
        if sleeptime is None:
            sleeptime = pf.cfg["draw/flywait"]
        sleeptime = float(sleeptime)
        if sleeptime > 0.0:
            sleep(sleeptime)

    pf.canvas.camera.setCenter(*center)
    pf.canvas.camera.setDist(coords.length(center - eye))
    pf.canvas.update()
コード例 #2
0
ファイル: draw.py プロジェクト: BackupTheBerlios/pyformex-svn
def flyAlong(path='flypath',upvector=[0.,1.,0.],sleeptime=None):
    """Fly through the scene along the flypath."""
    if type(path) is str:
        path = named(path)
    if not path:
        warning("You have to define a flypath first!")
        return
    if path.nplex() != 2:
        warning("The flypath should be a plex-2 Formex!")
        
    for eye,center in path:
        GD.debug("Eye: %s; Center: %s" % (eye,center))
        GD.canvas.camera.lookAt(eye,center,upvector)
        GD.canvas.display()
        GD.canvas.update()
        image.saveNext()
        if sleeptime is None:
            sleeptime = GD.cfg['draw/flywait']
        sleeptime = float(sleeptime)
        if sleeptime > 0.0:
            sleep(sleeptime)

    GD.canvas.camera.setCenter(*center)
    GD.canvas.camera.setDist(coords.length(center-eye))
    GD.canvas.update()
コード例 #3
0
ファイル: draw.py プロジェクト: BackupTheBerlios/pyformex-svn
def flyAlong(path,upvector=[0.,1.,0.],sleeptime=None):
    for seg in path:
        GD.debug("Eye: %s; Center: %s" % (seg[0],seg[1]))
        GD.canvas.camera.lookAt(seg[0],seg[1],upvector)
        GD.canvas.display()
        GD.canvas.update()
        image.saveNext()
        if sleeptime is None:
            sleeptime = GD.cfg['draw/flywait']
        sleeptime = float(sleeptime)
        if sleeptime > 0.0:
            sleep(sleeptime)
コード例 #4
0
ファイル: draw.py プロジェクト: BackupTheBerlios/pyformex-svn
def flyAlong(path,upvector=[0.,1.,0.],sleeptime=None):
    """Fly through the current scene along the specified path.

    This function moves the camera through the subsequent points
    of a path, looing at the next point of the path, and keeping
    the upvector of the camera oriented along the specified direction.
    
    - `path`: a PolyLine or plex-2 Formex specifyin the camera path.
    - `upvector`: the direction of the vertical axis of the camera.
    - `sleeptime`: a delay between subsequent images, to slow down
      the camera movement.
    """
    from plugins.curve import PolyLine

    try:
        if not isinstance(path,Formex):
            path = path.toFormex() 
        if not path.nplex() in (2,3):
            raise ValueError
    except:
        raise ValueError,"The camera path should be (convertible to) a plex-2 or plex-3 Formex!"

    if sleeptime is None:
        sleeptime = pf.cfg['draw/flywait']
    saved = delay(sleeptime)
    saved1 = pf.GUI.actions['Continue'].isEnabled()
    pf.GUI.actions['Continue'].setEnabled(True)
    
    for eye,center in path:
        pf.debug("Eye: %s; Center: %s" % (eye,center))
        pf.canvas.camera.lookAt(eye,center,upvector)
        wait()
        pf.canvas.display()
        pf.canvas.update()
        image.saveNext()

    delay(saved)
    pf.GUI.actions['Continue'].setEnabled(saved1)
    pf.canvas.camera.setCenter(*center)
    pf.canvas.camera.setDist(coords.length(center-eye))
    pf.canvas.update()
コード例 #5
0
ファイル: draw.py プロジェクト: BackupTheBerlios/pyformex-svn
def draw(
    F,
    view=None,
    bbox=None,
    color="prop",
    colormap=None,
    bkcolor=None,
    bkcolormap=None,
    alpha=None,
    mode=None,
    linewidth=None,
    linestipple=None,
    shrink=None,
    marksize=None,
    wait=True,
    clear=None,
    allviews=False,
    highlight=False,
    flat=False,
):
    """Draw object(s) with specified settings and direct camera to it.

    The first argument is an object to be drawn. All other arguments are
    settings that influence how  the object is being drawn.

    F is either a Formex or a TriSurface object, or a name of such object
    (global or exported), or a list thereof.
    If F is a list, the draw() function is called repeatedly with each of
    ithe items of the list as first argument and with the remaining arguments
    unchanged.

    The remaining arguments are drawing options. If None, they are filled in
    from the current viewport drawing options.
    
    view is either the name of a defined view or 'last' or None.
    Predefined views are 'front','back','top','bottom','left','right','iso'.
    With view=None the camera settings remain unchanged (but might be changed
    interactively through the user interface). This may make the drawn object
    out of view!
    With view='last', the camera angles will be set to the same camera angles
    as in the last draw operation, undoing any interactive changes.
    The initial default view is 'front' (looking in the -z direction).

    bbox specifies the 3D volume at which the camera will be aimed (using the
    angles set by view). The camera position wil be set so that the volume
    comes in view using the current lens (default 45 degrees).
    bbox is a list of two points or compatible (array with shape (2,3)).
    Setting the bbox to a volume not enclosing the object may make the object
    invisible on the canvas.
    The special value bbox='auto' will use the bounding box of the objects
    getting drawn (object.bbox()), thus ensuring that the camera will focus
    on these objects.
    The special value bbox=None will use the bounding box of the previous
    drawing operation, thus ensuring that the camera's target volume remains
    unchanged.

    color,colormap,linewidth,alpha,marksize are passed to the
    creation of the 3D actor.

        
    if color is None, it is drawn with the color specified on creation.
    if color == 'prop' and a colormap was installed, props define color.
    else, color should be an array of RGB values, either with shape
    (3,) for a single color, or (nelems,3) for differently colored
    elements 


    shrink is a floating point shrink factor that will be applied to object
    before drawing it.

    If the Formex has properties and a color list is specified, then the
    the properties will be used as an index in the color list and each member
    will be drawn with the resulting color.
    If color is one color value, the whole Formex will be drawn with
    that color.
    Finally, if color=None is specified, the whole Formex is drawn in black.
    
    Each draw action activates a locking mechanism for the next draw action,
    which will only be allowed after drawdelay seconds have elapsed. This
    makes it easier to see subsequent images and is far more elegant that an
    explicit sleep() operation, because all script processing will continue
    up to the next drawing instruction.
    The value of drawdelay is set in the config, or 2 seconds by default.
    The user can disable the wait cycle for the next draw operation by
    specifying wait=False. Setting drawdelay=0 will disable the waiting
    mechanism for all subsequent draw statements (until set >0 again).
    """
    # Facility for drawing database objects by name
    if type(F) == str:
        F = named(F)
        if F is None:
            return None

    # We need to get the default for bbox before processing a list,
    # because bbox should be set only once for the whole list of objects
    if bbox is None:
        bbox = pf.canvas.options.get("bbox", "auto")

    if type(F) == list:
        actor = []
        nowait = False
        # print "DRAWING LIST WITH BBOX %s" % bbox
        save_bbox = bbox
        for Fi in F:
            if Fi is F[-1]:
                nowait = wait
                # print "DRAWING WITH BBOX %s, VIEW %s" % (bbox,view)
            actor.append(
                draw(
                    Fi,
                    view,
                    bbox,
                    color,
                    colormap,
                    bkcolor,
                    bkcolormap,
                    alpha,
                    mode,
                    linewidth,
                    linestipple,
                    shrink,
                    marksize,
                    wait=nowait,
                    clear=clear,
                    allviews=allviews,
                    highlight=highlight,
                    flat=flat,
                )
            )
            if Fi is F[0]:
                clear = False
                view = None
                bbox = "last"

        bbox = save_bbox
        if bbox == "auto":
            bbox = coords.bbox(actor)
            pf.canvas.setCamera(bbox, view)
            pf.canvas.update()

        return actor

    # We now should have a single object to draw
    # Check if it is something we can draw

    if not hasattr(F, "actor") and hasattr(F, "toFormex"):
        pf.debug("CONVERTING %s TO FORMEX TO ENABLE DRAWING" % type(F))
        F = F.toFormex()

    if not hasattr(F, "actor"):
        # Don't know how to draw this object
        raise RuntimeError, "draw() can not draw objects of type %s" % type(F)

    # Fill in the remaining defaults
    if shrink is None:
        shrink = pf.canvas.options.get("shrink", None)

    if marksize is None:
        marksize = pf.canvas.options.get("marksize", pf.cfg.get("marksize", 5.0))

    if alpha is None:
        alpha = pf.canvas.options.get("alpha", 0.5)

    # Create the colors
    if color == "prop":
        if hasattr(F, "p"):
            color = F.prop
        elif hasattr(F, "prop"):
            color = F.prop
        else:
            color = colors.black
    elif color == "random":
        # create random colors
        color = numpy.random.rand(F.nelems(), 3)

    pf.GUI.drawlock.wait()

    if clear is None:
        clear = pf.canvas.options.get("clear", False)
    if clear:
        clear_canvas()

    if view is not None and view != "last":
        pf.debug("SETTING VIEW to %s" % view)
        setView(view)

    pf.GUI.setBusy()
    if shrink is not None:
        F = _shrink(F, shrink)

    try:
        actor = F.actor(
            color=color,
            colormap=colormap,
            bkcolor=bkcolor,
            bkcolormap=bkcolormap,
            alpha=alpha,
            mode=mode,
            linewidth=linewidth,
            linestipple=linestipple,
            marksize=marksize,
        )

        if actor is None:
            return None

        if flat:
            actor.specular = 0.0
        if highlight:
            pf.canvas.addHighlight(actor)
        else:
            pf.canvas.addActor(actor)
            if view is not None or bbox not in [None, "last"]:
                pf.debug("CHANGING VIEW to %s" % view)
                if view == "last":
                    view = pf.canvas.options["view"]
                if bbox == "auto":
                    bbox = F.bbox()
                pf.debug("SET CAMERA TO: bbox=%s, view=%s" % (bbox, view))
                pf.canvas.setCamera(bbox, view)
                # setView(view)
        pf.canvas.update()
        pf.app.processEvents()
        # pf.debug("AUTOSAVE %s" % image.autoSaveOn())
        if image.autoSaveOn():
            image.saveNext()
        if wait:  # make sure next drawing operation is retarded
            pf.GUI.drawlock.lock()
    finally:
        pf.GUI.setBusy(False)
    return actor
コード例 #6
0
ファイル: draw.py プロジェクト: BackupTheBerlios/pyformex-svn
def draw(F, view=None,bbox='auto',
         color='prop',colormap=None,linewidth=None,alpha=0.5,
         shrink=None,eltype=None,marksize=None,color1=None,
         wait=True,clear=None,allviews=False):
    """Draw object(s) with specified settings and direct camera to it.

    The first argument is an object to be drawn. All other arguments are
    settings that influence how  the object is being drawn.

    F is either a Formex or a Surface object, or a name of such object
    (global or exported), or a list thereof.
    If F is a list, the draw() function is called repeatedly with each of
    ithe items of the list as first argument and with the remaining arguments
    unchanged.

    view is either the name of a defined view or 'last' or None.
    Predefined views are 'front','back','top','bottom','left','right','iso'.
    With view=None the camera settings remain unchanged (but might be changed
    interactively through the user interface). This may make the drawn object
    out of view!
    With view='last', the camera angles will be set to the same camera angles
    as in the last draw operation, undoing any interactive changes.
    The initial default view is 'front' (looking in the -z direction).

    bbox specifies the 3D volume at which the camera will be aimed (using the
    angles set by view). The camera position wil be set so that the volume
    comes in view using the current lens (default 45 degrees).
    bbox is a list of two points or compatible (array with shape (2,3)).
    Setting the bbox to a volume not enclosing the object may make the object
    invisible on the canvas.
    The default bbox='auto' will use the bounding box of the objects getting
    drawn (object.bbox()), thus ensuring that the camera will focus on the
    shown object.
    With bbox=None, the camera's target volume remains unchanged.

    color,colormap,linewidth,alpha,eltype,marksize,color1 are passed to the
    creation of the 3D actor.

    shrink is a floating point shrink factor that will be applied to object
    before drawing it.

    If the Formex has properties and a color list is specified, then the
    the properties will be used as an index in the color list and each member
    will be drawn with the resulting color.
    If color is one color value, the whole Formex will be drawn with
    that color.
    Finally, if color=None is specified, the whole Formex is drawn in black.
    
    Each draw action activates a locking mechanism for the next draw action,
    which will only be allowed after drawdelay seconds have elapsed. This
    makes it easier to see subsequent images and is far more elegant that an
    explicit sleep() operation, because all script processing will continue
    up to the next drawing instruction.
    The value of drawdelay is set in the config, or 2 seconds by default.
    The user can disable the wait cycle for the next draw operation by
    specifying wait=False. Setting drawdelay=0 will disable the waiting
    mechanism for all subsequent draw statements (until set >0 again).
    """

    global allowwait

    if type(F) == list:
        actor = []
        nowait = False
        for Fi in F:
            if Fi == F[-1]:
                nowait = wait
            actor.append(draw(Fi,view,bbox,
                              color,colormap,linewidth,alpha,
                              shrink,eltype,marksize,color1,
                              wait,clear,allviews))
            if Fi == F[0]:
                clear = False
                view = None
        return actor

    if type(F) == str:
        F = named(F)
        if F is None:
            return None

    if not (isinstance(F,formex.Formex) or
            isinstance(F,surface.Surface) or
            isinstance(F,tools.Plane)):
        raise RuntimeError,"draw() can not draw objects of type %s" % type(F)

    if allowwait:
        drawwait()

    if clear is None:
        clear = DrawOptions.get('clear',False)
    if clear:
        clear_canvas()

    if view is not None and view != 'last':
        GD.debug("SETTING VIEW to %s" % view)
        setView(view)

    if shrink is None:
        shrink = DrawOptions.get('shrink',None)
        
    # Create the colors
    if color == 'prop':
        if hasattr(F,'p'):
            color = F.p
        else:
            color = colors.black
    elif color == 'random':
        # create random colors
        color = numpy.random.random((F.nelems(),3))

    try:
        marksize = float(marksize)
    except:
        marksize = GD.cfg.get('marksize',0.01)

    GD.gui.setBusy()
    if shrink is not None:
        #GD.debug("DRAWING WITH SHRINK = %s" % shrink)
        F = _shrink(F,shrink)
    try:
        if isinstance(F,formex.Formex):
            if color1 is not None:
                #GD.debug("DRAWING WITH COLOR1\n%s" % str(color1))
                canvas.glSmooth()
            actor = actors.FormexActor(F,color=color,colormap=colormap,linewidth=linewidth,eltype=eltype,marksize=marksize,alpha=alpha,color1=color1)
        elif isinstance(F,surface.Surface):
            actor = actors.SurfaceActor(F,color=color,colormap=colormap,linewidth=linewidth,alpha=alpha)
        elif isinstance(F,tools.Plane):
            return drawPlane(F.point(),F.normal())
        GD.canvas.addActor(actor)
        if view is not None or bbox is not None:
            #GD.debug("CHANGING VIEW to %s" % view)
            if view == 'last':
                view = DrawOptions['view']
            if bbox == 'auto':
                bbox = F.bbox()
            #GD.debug("SET CAMERA TO: bbox=%s, view=%s" % (bbox,view))
            GD.canvas.setCamera(bbox,view)
            #setView(view)
        GD.canvas.update()
        GD.app.processEvents()
        #GD.debug("AUTOSAVE %s" % image.autoSaveOn())
        if image.autoSaveOn():
            image.saveNext()
        if allowwait and wait:
    ##        if stepmode:
    ##            drawblock()
    ##        else:
            drawlock()
    finally:
        GD.gui.setBusy(False)
    return actor