Ejemplo n.º 1
0
def drawNurbsCurves(x,knots,color=None,alpha=1.0,samplingTolerance=5.0):
    """Draw a collection of Nurbs curves.

    x: (nctrl,ndim) or (ncurve,nctrl,ndim) float array: control points,
       specifying either a single curve or ncurve curves defined by the
       same number of control points. ndim can be 3 or 4. If 4, the 4-th
       coordinate is interpreted as a weight for that point.
    knots: (nknots) or (ncurve,nknots) float array: knot vector, containing the
       parameter values to be used in the nurbs definition. Remark that
       nknots must be larger than nctrl. The order of the curve is
       nknots-nctrl and the degree of the curve is order-1.
       If a single knot vector is given, the same is used for all curves.
       Otherwise, the number of knot vectors must match the number of nurbs
       curves.

    If color is given it is an (ncurves,3) array of RGB values.
    """
    nctrl,ndim = x.shape[-2:]
    nknots = asarray(knots).shape[-1]
    order = nknots-nctrl
    if  order > 8:
        utils.warn('Nurbs curves of degree > 7 can currently not be drawn! You can create some approximation by evaluating the curve at some points.')
        return

    if x.ndim == 2:
        x = x.reshape(-1,nctrl,ndim)
        if color is not None and color.ndim == 2:
            color = color.reshape(-1,nctrl,color.shape[-1])

    if color is not None:
        pf.debug('Coords shape: %s' % str(x.shape),pf.DEBUG.DRAW)
        pf.debug('Color shape: %s' % str(color.shape),pf.DEBUG.DRAW)
        if color.ndim == 1:
            pf.debug('Single color',pf.DEBUG.DRAW)
        elif color.ndim == 2 and color.shape[0] == x.shape[0]:
            pf.debug('Element color: %s colors' % color.shape[0],pf.DEBUG.DRAW)
        elif color.shape == x.shape[:-1] + (3,):
            pf.debug('Vertex color: %s colors' % str(color.shape[:-1]),pf.DEBUG.DRAW)
        else:
            raise ValueError,"Number of colors (%s) should equal 1 or the number of curves(%s) or the number of curves * number of vertices" % (color.shape[0],x.shape[0])

        pf.debug("Color shape = %s" % str(color.shape),pf.DEBUG.DRAW)
        if color.shape[-1] not in (3,4):
            raise ValueError,"Expected 3 or 4 color components"

    if color is not None:
        pf.debug("Final Color shape = %s" % str(color.shape),pf.DEBUG.DRAW)

    nurb = GLU.gluNewNurbsRenderer()
    if not nurb:
        raise RuntimeError,"Could not create a new NURBS renderer"

    GLU.gluNurbsProperty(nurb,GLU.GLU_SAMPLING_TOLERANCE,samplingTolerance)

    mode = {3:GL.GL_MAP1_VERTEX_3, 4:GL.GL_MAP1_VERTEX_4}[ndim]

    if color is not None and color.ndim == 1:
        # Handle single color
        pf.debug('Set single color: OK',pf.DEBUG.DRAW)
        glColor(color)
        color = None

    ki = knots
    for i,xi in enumerate(x):
        if color is not None and color.ndim == 2:
            # Handle element color
            glColor(color[i])
        if knots.ndim > 1:
            ki = knots[i]
        GLU.gluBeginCurve(nurb)
        if color is not None and color.ndim == 3:
            # Handle vertex color
            ci = color[i]
            if ci.shape[-1] == 3:
                # gluNurbs always wants 4 colors
                ci = growAxis(ci,1,axis=-1,fill=alpha)
            GLU.gluNurbsCurve(nurb,ki,ci,GL.GL_MAP1_COLOR_4)
        GLU.gluNurbsCurve(nurb,ki,xi,mode)
        GLU.gluEndCurve(nurb)

    GLU.gluDeleteNurbsRenderer(nurb)
Ejemplo n.º 2
0
def drawNurbsSurfaces(x,sknots,tknots,color=None,alpha=1.0,normals='auto',samplingTolerance=20.0):
    """Draw a collection of Nurbs surfaces.

    x: (ns,nt,ndim) or (nsurf,ns,nt,ndim) float array:
       (ns,nt) shaped control points array,
       specifying either a single surface or nsurf surfaces defined by the
       same number of control points. ndim can be 3 or 4. If 4, the 4-th
       coordinate is interpreted as a weight for that point.
    sknots: (nsk) or (nsurf,nsk) float array: knot vector, containing
       the parameter values to be used in the s direction of the surface.
       Remark that nsk must be larger than ns. The order of the surface
       in s-direction is nsk-ns and the degree of the s-curves is nsk-ns-1.
       If a single sknot vector is given, the same is used for all surfaces.
       Otherwise, the number of sknot vectors must match the number of nurbs
       surfaces.
    tknots: (ntk) or (nsurf,ntk) float array: knot vector, containing
       the parameter values to be used in the t direction of the surface.
       Remark that ntk must be larger than nt. The order of the surface
       in t-direction is ntk-nt and the degree of the t-curves is ntk-nt-1.
       If a single sknot vector is given, the same is used for all surfaces.
       Otherwise, the number of sknot vectors must match the number of nurbs
       surfaces.

    If color is given it is an (nsurf,3) array of RGB values.
    """
    import timer
    t = timer.Timer()

    ns,nt,ndim = x.shape[-3:]
    nsk = asarray(sknots).shape[-1]
    ntk = asarray(tknots).shape[-1]
    sorder = nsk-ns
    torder = ntk-nt
    if sorder > 8 or torder > 8:
        utils.warn('Nurbs surfaces of degree > 7 can currently not be drawn! You can approximate the surface by a lower order surface.')
        return

    if x.ndim == 3:
        x = x.reshape(-1,ns,nt,ndim)
        if color is not None and color.ndim == 3:
            color = color.reshape(-1,ns,nt,color.shape[-1])

    if color is not None:
        pf.debug('Coords shape: %s' % str(x.shape),pf.DEBUG.DRAW)
        pf.debug('Color shape: %s' % str(color.shape),pf.DEBUG.DRAW)
        if color.ndim == 1:
            pf.debug('Single color',pf.DEBUG.DRAW)
        elif color.ndim == 2 and color.shape[0] == x.shape[0]:
            pf.debug('Element color: %s' % color.shape[0],pf.DEBUG.DRAW)
        elif color.shape == x.shape[:-1] + (3,):
            pf.debug('Vertex color: %s' % str(color.shape[:-1]),pf.DEBUG.DRAW)
        else:
            raise ValueError,"Number of colors (%s) should equal 1 or the number of faces(%s) or the number of faces * number of vertices" % (color.shape[0],x.shape[0])

        pf.debug("Color shape = %s" % str(color.shape),pf.DEBUG.DRAW)
        if color.shape[-1] not in (3,4):
            raise ValueError,"Expected 3 or 4 color components"

    if normals == 'auto':
        GL.glEnable(GL.GL_AUTO_NORMAL)
    else:
        GL.glDisable(GL.GL_AUTO_NORMAL)

    # The following uses:
    # x: (nsurf,ns,nt,4)
    # sknots: (nsknots) or (nsurf,nsknots)
    # tknots: (ntknots) or (nsurf,ntknots)
    # color: None or (4) or (nsurf,4) or (nsurf,ns,nt,4)
    # samplingTolerance

    if pf.options.fastnurbs:
        alpha=0.5
        x = x.astype(float32)
        sknots = sknots.astype(float32)
        tknots = tknots.astype(float32)
        if color is not None:
            color = color.astype(float32)

            if color.shape[-1] == 3:
                # gluNurbs always wants 4 colors
                color = growAxis(color,3,axis=-1,fill=alpha)

        nb = drawgl.draw_nurbs_surfaces(x,sknots,tknots,color,alpha,samplingTolerance)

    else:
        nurb = GLU.gluNewNurbsRenderer()
        if not nurb:
            raise RuntimeError,"Could not create a new NURBS renderer"

        GLU.gluNurbsProperty(nurb,GLU.GLU_SAMPLING_TOLERANCE,samplingTolerance)

        mode = {3:GL.GL_MAP2_VERTEX_3, 4:GL.GL_MAP2_VERTEX_4}[ndim]

        if color is not None and color.ndim == 1:
            # Handle single color
            pf.debug('Set single color: OK',pf.DEBUG.DRAW)
            glColor(color)
            color = None

        si = sknots
        ti = tknots
        for i,xi in enumerate(x):
            if color is not None and color.ndim == 2:
                # Handle element color
                glColor(color[i])
            if sknots.ndim > 1:
                si = sknots[i]
            if tknots.ndim > 1:
                ti = tknots[i]
            GLU.gluBeginSurface(nurb)
            if color is not None and color.ndim == 4:
                # Handle vertex color
                ci = color[i]
                if ci.shape[-1] == 3:
                    # gluNurbs always wants 4 colors
                    ci = growAxis(ci,1,axis=-1,fill=alpha)
                GLU.gluNurbsSurface(nurb,si,ti,ci,GL.GL_MAP2_COLOR_4)
            GLU.gluNurbsSurface(nurb,si,ti,xi,mode)
            GLU.gluEndSurface(nurb)

        GLU.gluDeleteNurbsRenderer(nurb)

    print("drawNurbsSurfaces: %s seconds" % t.seconds())
Ejemplo n.º 3
0
 def __del__(self):
     glu.gluDeleteNurbsRenderer(self.glID)