Exemplo n.º 1
0
    def addLineSegments(this,
        Vs, Lines,
        scaling = 1,
        precision = 5
    ):
        """
        Adds to the current page a PS string of the faces in Lines using the x
        and y coordinates from Vs.

        Vs: a list of coordinates
        Lines: a list of lines, each face is a list of vertex nrs and it may
               consist of several connected line segments.
        """
        if this.debug:
            print "%s.addLineSegments" % this.name
            #print 'Vs:'
            #for v in Vs: print '  ', v
            #print 'Lines:'
            #print 'Lines:'
            #for l in Lines: print '  ', l
            print 'len(Vs):', len(Vs)
            print 'Lines[0]:', Lines[0]
        vStr = '/vertices [\n'
        # use copies, for the cleanup
        Vs = [[c for c in v] for v in Vs]
        Lines = [[i for i in l] for l in Lines]
        # sometimes the Vs contains many more vertices then needed: clean up:
        glue.cleanUpVsFs(Vs, Lines)
        for i in range(len(Vs)):
            v = Vs[i]
            vStr = '%s  [%s %s] %% %d\n' % (
                vStr,
                glue.f2s(v[0], precision),
                glue.f2s(v[1], precision),
                i
            )
        vStr = '%s] def' % vStr
        fStr_ = ''
        maxX = maxY = -sys.maxint-1 # 2-complement
        minX = minY =  sys.maxint
        for partOfFace in Lines:
            oneFaceStr = ''
            for vNr in partOfFace:
                v = Vs[vNr]
                if v[0] < minX: minX = v[0]
                if v[0] > maxX: maxX = v[0]
                if v[1] < minY: minY = v[1]
                if v[1] > maxY: maxY = v[1]
                oneFaceStr = '%s %d' % (oneFaceStr, vNr)
            fStr_ = '%s[%s]' % (fStr_, oneFaceStr[1:])
        bboxStr = '/bbox [%s %s %s %s] def' % (
            glue.f2s(minX, precision),
            glue.f2s(minY, precision),
            glue.f2s(maxX, precision),
            glue.f2s(maxY, precision)
        )
        fStr = '/faces [\n'
        break_limit = 78
        while len(fStr_) > break_limit:
            break_at = break_limit
            while fStr_[break_at] != '[':
                break_at -= 1
                assert break_at > 0, 'Not implemented, TODO: handle long line faces'
            fStr = '%s  %s\n' % (fStr, fStr_[:break_at])
            fStr_ = fStr_[break_at:]
        fStr = '%s  %s\n' % (fStr, fStr_)
        fStr = '%s] def' % fStr
        scalingStr = '/scalingSize %d def' % scaling
        addBBoxStr = ""
        if False:
            addBBoxStr = """
  %% draw origin
  %0 0 2 0 360 arc fill
  %% draw bbox
  %bbox 0 get scalingSize mul bbox 1 get scalingSize mul moveto
  %bbox 0 get scalingSize mul bbox 3 get scalingSize mul lineto
  %bbox 2 get scalingSize mul bbox 3 get scalingSize mul lineto
  %bbox 2 get scalingSize mul bbox 1 get scalingSize mul lineto
  %closepath stroke
"""
        # add vStr by + since it contains '%'s
        drawLinesStr = vStr + """
%s
%s
%s
.1 setlinewidth

/dx bbox 2 get bbox 0 get sub scalingSize mul def
/dy bbox 3 get bbox 1 get sub scalingSize mul def

gsave
  bbox 0 get scalingSize mul neg
  bbox 1 get scalingSize mul neg
  translate
  faces aload length { %%repeat
    dup 0 get
    vertices exch get
    aload pop
    scalingSize mul exch
    scalingSize mul exch
    moveto
    aload length 1 sub { %%repeat
      vertices exch get
      aload pop
      scalingSize mul exch
      scalingSize mul exch
      lineto
    } repeat
    pop
    closepath stroke
  } repeat

%s
grestore
""" % (fStr, bboxStr, scalingStr, addBBoxStr)

        this.addToPage(drawLinesStr,
            scaling*(maxX - minX),
            scaling*(maxY - minY)
        )
Exemplo n.º 2
0
    def addLineSegments(this, Vs, Lines, scaling=1, precision=5):
        """
        Adds to the current page a PS string of the faces in Lines using the x
        and y coordinates from Vs.

        Vs: a list of coordinates
        Lines: a list of lines, each face is a list of vertex nrs and it may
               consist of several connected line segments.
        """
        if this.debug:
            print "%s.addLineSegments" % this.name
            #print 'Vs:'
            #for v in Vs: print '  ', v
            #print 'Lines:'
            #print 'Lines:'
            #for l in Lines: print '  ', l
            print 'len(Vs):', len(Vs)
            print 'Lines[0]:', Lines[0]
        vStr = '/vertices [\n'
        # use copies, for the cleanup
        Vs = [[c for c in v] for v in Vs]
        Lines = [[i for i in l] for l in Lines]
        # sometimes the Vs contains many more vertices then needed: clean up:
        glue.cleanUpVsFs(Vs, Lines)
        for i in range(len(Vs)):
            v = Vs[i]
            vStr = '%s  [%s %s] %% %d\n' % (vStr, glue.f2s(
                v[0], precision), glue.f2s(v[1], precision), i)
        vStr = '%s] def' % vStr
        fStr_ = ''
        maxX = maxY = -sys.maxint - 1  # 2-complement
        minX = minY = sys.maxint
        for partOfFace in Lines:
            oneFaceStr = ''
            for vNr in partOfFace:
                v = Vs[vNr]
                if v[0] < minX: minX = v[0]
                if v[0] > maxX: maxX = v[0]
                if v[1] < minY: minY = v[1]
                if v[1] > maxY: maxY = v[1]
                oneFaceStr = '%s %d' % (oneFaceStr, vNr)
            fStr_ = '%s[%s]' % (fStr_, oneFaceStr[1:])
        bboxStr = '/bbox [%s %s %s %s] def' % (
            glue.f2s(minX, precision), glue.f2s(minY, precision),
            glue.f2s(maxX, precision), glue.f2s(maxY, precision))
        fStr = '/faces [\n'
        break_limit = 78
        while len(fStr_) > break_limit:
            break_at = break_limit
            while fStr_[break_at] != '[':
                break_at -= 1
                assert break_at > 0, 'Not implemented, TODO: handle long line faces'
            fStr = '%s  %s\n' % (fStr, fStr_[:break_at])
            fStr_ = fStr_[break_at:]
        fStr = '%s  %s\n' % (fStr, fStr_)
        fStr = '%s] def' % fStr
        scalingStr = '/scalingSize %d def' % scaling
        addBBoxStr = ""
        if False:
            addBBoxStr = """
  %% draw origin
  %0 0 2 0 360 arc fill
  %% draw bbox
  %bbox 0 get scalingSize mul bbox 1 get scalingSize mul moveto
  %bbox 0 get scalingSize mul bbox 3 get scalingSize mul lineto
  %bbox 2 get scalingSize mul bbox 3 get scalingSize mul lineto
  %bbox 2 get scalingSize mul bbox 1 get scalingSize mul lineto
  %closepath stroke
"""
        # add vStr by + since it contains '%'s
        drawLinesStr = vStr + """
%s
%s
%s
.1 setlinewidth

/dx bbox 2 get bbox 0 get sub scalingSize mul def
/dy bbox 3 get bbox 1 get sub scalingSize mul def

gsave
  bbox 0 get scalingSize mul neg
  bbox 1 get scalingSize mul neg
  translate
  faces aload length { %%repeat
    dup 0 get
    vertices exch get
    aload pop
    scalingSize mul exch
    scalingSize mul exch
    moveto
    aload length 1 sub { %%repeat
      vertices exch get
      aload pop
      scalingSize mul exch
      scalingSize mul exch
      lineto
    } repeat
    pop
    closepath stroke
  } repeat

%s
grestore
""" % (fStr, bboxStr, scalingStr, addBBoxStr)

        this.addToPage(drawLinesStr, scaling * (maxX - minX),
                       scaling * (maxY - minY))
Exemplo n.º 3
0
    def glDrawSingleRemoveUnscaledEdges(this):
        isScaledDown = not Geom3D.eq(this.c.scale, 1.0, margin=0.001)
        if not this.projectedTo3D:
            # print 'reprojecting...'
            try:
                del this.cell
            except AttributeError:
                pass
            Ns3D = []
            if this.rot4 != None:
                Vs4D = [this.rot4 * v for v in this.Vs]
            # TODO fix Ns.. if needed..
            #    if this.Ns != []:cleanUp
            #        Ns4D = [this.rot4*n for n in this.Ns]
            else:
                Vs4D = [v for v in this.Vs]
            Vs3D = this.projectVsTo3D(Vs4D)
            #for i in range(0, len(this.Es), 2):
            #    v0 = Vs4D[this.Es[i]]
            #    v1 = Vs4D[this.Es[i+1]]
            #    print 'delta v:', v0 - v1
            #    print 'Edge [%d, %d]; len:' % (this.Es[i], this.Es[i+1]), (v1-v0).length()
            #if this.Ns != []:
            #    Ns3D = this.projectVsTo3D(Ns4D)
            # Now project all to one 3D shape. 1 3D shape is chosen, instean of
            # projecting each cell to one shape because of different reasons:
            #  - when drawing transparent faces all the opaqe fae should be
            #    drawn first.
            #  - if drawing the cells per shape, the glVertexPointer should be
            #    called for each cell. (Currently SimpleShape will not call this
            #    function unless the vertices have been changed...
            shapeVs = []
            shapeEs = []
            shapeFs = []
            shapeColIndices = []
            if this.c.draw:
                shapeCols = this.c.col[0]
            else:
                shapeCols = this.f.col[0]
            if this.removeTransparency:
                shapeCols = [c[0:3] for c in shapeCols]
            if this.e.draw and (not isScaledDown or this.e.showUnscaled):
                shapeVs = Vs3D
                shapeEs = this.Es
            # Add a shape with faces for each cell
            for i in xrange(len(this.Cs)):
                # use a copy, since we will filter (v indices will change):
                cellFs = [f[:] for f in this.Cs[i]]
                if this.c.draw:
                    shapeColIndices.extend([this.c.col[1][i] for f in cellFs])
                else:
                    shapeColIndices.extend(this.f.col[1][i])
                # Now cleanup Vs3D
                # TODO
                # if this.e.draw and (not isScaledDown or this.e.showUnscaled):
                # Then shapeVs = Vs3D already, and the code below is all
                # unecessary.
                cellVs = Vs3D[:]
                nrUsed = glue.cleanUpVsFs(cellVs, cellFs)
                # Now attaching to current Vs, will change index:
                offset = len(shapeVs)
                cellFs = [[vIndex + offset for vIndex in f] for f in cellFs]
                # Now scale from gravitation centre:
                if isScaledDown:
                    g = GeomTypes.Vec3([0, 0, 0])
                    sum = 0
                    for vIndex in range(len(cellVs)):
                        g = g + nrUsed[vIndex] * GeomTypes.Vec3(cellVs[vIndex])
                        sum = sum + nrUsed[vIndex]
                    if sum != 0:
                        g = g / sum
                    #print this.name, 'g:', g
                    cellVs = [
                        this.c.scale * (GeomTypes.Vec3(v) - g) + g
                        for v in cellVs
                    ]

                shapeVs.extend(cellVs)
                shapeFs.extend(cellFs)
                # for shapeColIndices.extend() see above
            this.cell = Geom3D.SimpleShape(
                shapeVs,
                shapeFs,
                shapeEs,
                [],  # Vs , Fs, Es, Ns
                (shapeCols, shapeColIndices),
                name='%s_projection' % (this.name))
            this.cell.setVertexProperties(radius=this.v.radius,
                                          color=this.v.col)
            this.cell.setEdgeProperties(radius=this.e.radius,
                                        color=this.e.col,
                                        drawEdges=this.e.draw)
            this.cell.setFaceProperties(drawFaces=this.f.draw)
            this.cell.glInitialised = True  # done as first step in this func
            this.projectedTo3D = True
            this.updateTransparency = False
            if this.e.draw and isScaledDown:
                this.cell.recreateEdges()
                # Don't use, out of performance issues:
                # cellEs = this.cell.getEdgeProperties()['Es']
                # --------------------------
                # Bad performance during scaling:
                # cellEs = this.cell.getEdgeProperties()['Es']
                # this.cell.recreateEdges()
                # cellEs.extend(this.cell.getEdgeProperties()['Es'])
                # -------end bad perf---------
                cellEs = this.cell.Es
                if this.e.showUnscaled:
                    cellEs.extend(this.Es)
                this.cell.setEdgeProperties(Es=cellEs)
        if this.updateTransparency:
            cellCols = this.cell.getFaceProperties()['colors']
            if this.removeTransparency:
                shapeCols = [c[0:3] for c in cellCols[0]]
            else:
                if this.c.draw:
                    shapeCols = this.c.col[0]
                else:
                    shapeCols = this.f.col[0]
            cellCols = (shapeCols, cellCols[1])
            this.cell.setFaceProperties(colors=cellCols)
            this.updateTransparency = False
Exemplo n.º 4
0
    def glDrawSingleRemoveUnscaledEdges(this):
        isScaledDown = not Geom3D.eq(this.c.scale, 1.0, margin=0.001)
        if not this.projectedTo3D:
            # print 'reprojecting...'
            try:
                del this.cell
            except AttributeError:
                pass
            Ns3D = []
            if this.rot4 != None:
                Vs4D = [this.rot4 * v for v in this.Vs]
            # TODO fix Ns.. if needed..
            #    if this.Ns != []:cleanUp
            #        Ns4D = [this.rot4*n for n in this.Ns]
            else:
                Vs4D = [v for v in this.Vs]
            Vs3D = this.projectVsTo3D(Vs4D)
            # for i in range(0, len(this.Es), 2):
            #    v0 = Vs4D[this.Es[i]]
            #    v1 = Vs4D[this.Es[i+1]]
            #    print 'delta v:', v0 - v1
            #    print 'Edge [%d, %d]; len:' % (this.Es[i], this.Es[i+1]), (v1-v0).length()
            # if this.Ns != []:
            #    Ns3D = this.projectVsTo3D(Ns4D)
            # Now project all to one 3D shape. 1 3D shape is chosen, instean of
            # projecting each cell to one shape because of different reasons:
            #  - when drawing transparent faces all the opaqe fae should be
            #    drawn first.
            #  - if drawing the cells per shape, the glVertexPointer should be
            #    called for each cell. (Currently SimpleShape will not call this
            #    function unless the vertices have been changed...
            shapeVs = []
            shapeEs = []
            shapeFs = []
            shapeColIndices = []
            if this.c.draw:
                shapeCols = this.c.col[0]
            else:
                shapeCols = this.f.col[0]
            if this.removeTransparency:
                shapeCols = [c[0:3] for c in shapeCols]
            if this.e.draw and (not isScaledDown or this.e.showUnscaled):
                shapeVs = Vs3D
                shapeEs = this.Es
            # Add a shape with faces for each cell
            for i in xrange(len(this.Cs)):
                # use a copy, since we will filter (v indices will change):
                cellFs = [f[:] for f in this.Cs[i]]
                if this.c.draw:
                    shapeColIndices.extend([this.c.col[1][i] for f in cellFs])
                else:
                    shapeColIndices.extend(this.f.col[1][i])
                # Now cleanup Vs3D
                # TODO
                # if this.e.draw and (not isScaledDown or this.e.showUnscaled):
                # Then shapeVs = Vs3D already, and the code below is all
                # unecessary.
                cellVs = Vs3D[:]
                nrUsed = glue.cleanUpVsFs(cellVs, cellFs)
                # Now attaching to current Vs, will change index:
                offset = len(shapeVs)
                cellFs = [[vIndex + offset for vIndex in f] for f in cellFs]
                # Now scale from gravitation centre:
                if isScaledDown:
                    g = GeomTypes.Vec3([0, 0, 0])
                    sum = 0
                    for vIndex in range(len(cellVs)):
                        g = g + nrUsed[vIndex] * GeomTypes.Vec3(cellVs[vIndex])
                        sum = sum + nrUsed[vIndex]
                    if sum != 0:
                        g = g / sum
                    # print this.name, 'g:', g
                    cellVs = [this.c.scale * (GeomTypes.Vec3(v) - g) + g for v in cellVs]

                shapeVs.extend(cellVs)
                shapeFs.extend(cellFs)
                # for shapeColIndices.extend() see above
            this.cell = Geom3D.SimpleShape(
                shapeVs,
                shapeFs,
                shapeEs,
                [],  # Vs , Fs, Es, Ns
                (shapeCols, shapeColIndices),
                name="%s_projection" % (this.name),
            )
            this.cell.setVertexProperties(radius=this.v.radius, color=this.v.col)
            this.cell.setEdgeProperties(radius=this.e.radius, color=this.e.col, drawEdges=this.e.draw)
            this.cell.setFaceProperties(drawFaces=this.f.draw)
            this.cell.glInitialised = True  # done as first step in this func
            this.projectedTo3D = True
            this.updateTransparency = False
            if this.e.draw and isScaledDown:
                this.cell.recreateEdges()
                # Don't use, out of performance issues:
                # cellEs = this.cell.getEdgeProperties()['Es']
                # --------------------------
                # Bad performance during scaling:
                # cellEs = this.cell.getEdgeProperties()['Es']
                # this.cell.recreateEdges()
                # cellEs.extend(this.cell.getEdgeProperties()['Es'])
                # -------end bad perf---------
                cellEs = this.cell.Es
                if this.e.showUnscaled:
                    cellEs.extend(this.Es)
                this.cell.setEdgeProperties(Es=cellEs)
        if this.updateTransparency:
            cellCols = this.cell.getFaceProperties()["colors"]
            if this.removeTransparency:
                shapeCols = [c[0:3] for c in cellCols[0]]
            else:
                if this.c.draw:
                    shapeCols = this.c.col[0]
                else:
                    shapeCols = this.f.col[0]
            cellCols = (shapeCols, cellCols[1])
            this.cell.setFaceProperties(colors=cellCols)
            this.updateTransparency = False