def triad(rows, cols, lx=1.0, ly=1.0, lz=1.0): mdX = MyMeshData.arrow(rows, cols, length=lx, radius=0.1*lx) mdY = MyMeshData.arrow(rows, cols, length=ly, radius=0.1*ly) mdZ = MyMeshData.arrow(rows, cols, length=lz, radius=0.1*lz) # Orientation trX = Transform3D() trX.rotate(90., 0.0, 1.0, 0.0) vertX = fn.transformCoordinates(trX, mdX.vertexes(),transpose=True) nbrVerts = vertX.size/3 trY = Transform3D() trY.rotate(90., 1.0, 0.0, 0.0) vertY = fn.transformCoordinates(trY, mdY.vertexes(),transpose=True) verts = np.vstack((vertX, vertY, mdZ.vertexes())) faces = np.vstack((mdX.faces(), mdY.faces()+nbrVerts, mdZ.faces()+2*nbrVerts)) nbrFaces = faces.shape[0] #Colors x: red, y: green and z: blue colors = np.zeros((nbrFaces, 4)) colors[0:nbrFaces/3] = np.array([1.0, 0.0, 0.0, 0.6]) colors[nbrFaces/3:nbrFaces*2/3] = np.array([0.0, 1.0, 0.0, 0.6]) colors[nbrFaces*2/3:nbrFaces] = np.array([0.0, 0.0, 1.0, 0.6]) return gl.MeshData(vertexes=verts, faces=faces, faceColors=colors)
def generateFragments(self): tr = self.deviceTransform() if tr is None: return pts = np.empty((2,len(self.data['x']))) pts[0] = self.data['x'] pts[1] = self.data['y'] pts = fn.transformCoordinates(tr, pts) self.fragments = [] for i in xrange(len(self.data)): rec = self.data[i] pos = QtCore.QPointF(pts[0,i], pts[1,i]) x,y,w,h = rec['fragCoords'] rect = QtCore.QRectF(y, x, h, w) self.fragments.append(QtGui.QPainter.PixmapFragment.create(pos, rect))
def triad(rows, cols, lx=1.0, ly=1.0, lz=1.0): mdX = MyMeshData.arrow(rows, cols, length=lx, radius=0.1 * lx) mdY = MyMeshData.arrow(rows, cols, length=ly, radius=0.1 * ly) mdZ = MyMeshData.arrow(rows, cols, length=lz, radius=0.1 * lz) # Orientation trX = Transform3D() trX.rotate(90., 0.0, 1.0, 0.0) vertX = fn.transformCoordinates(trX, mdX.vertexes(), transpose=True) nbrVerts = vertX.size / 3 trY = Transform3D() trY.rotate(90., 1.0, 0.0, 0.0) vertY = fn.transformCoordinates(trY, mdY.vertexes(), transpose=True) verts = np.vstack((vertX, vertY, mdZ.vertexes())) faces = np.vstack( (mdX.faces(), mdY.faces() + nbrVerts, mdZ.faces() + 2 * nbrVerts)) nbrFaces = faces.shape[0] #Colors x: red, y: green and z: blue colors = np.zeros((nbrFaces, 4)) colors[0:nbrFaces / 3] = np.array([1.0, 0.0, 0.0, 0.6]) colors[nbrFaces / 3:nbrFaces * 2 / 3] = np.array([0.0, 1.0, 0.0, 0.6]) colors[nbrFaces * 2 / 3:nbrFaces] = np.array([0.0, 0.0, 1.0, 0.6]) return gl.MeshData(vertexes=verts, faces=faces, faceColors=colors)
def generateFragments(self): tr = self.deviceTransform() if tr is None: return pts = np.empty((2,len(self.data['x']))) pts[0] = self.data['x'] pts[1] = self.data['y'] pts = fn.transformCoordinates(tr, pts) self.fragments = [] pts = np.clip(pts, -2**30, 2**30) ## prevent Qt segmentation fault. ## Still won't be able to render correctly, though. for i in xrange(len(self.data)): rec = self.data[i] pos = QtCore.QPointF(pts[0,i], pts[1,i]) x,y,w,h = rec['fragCoords'] rect = QtCore.QRectF(y, x, h, w) self.fragments.append(QtGui.QPainter.PixmapFragment.create(pos, rect))
def paint(self, p, *args): if self._exportOpts is not False: aa = self._exportOpts.get('antialias', True) scale = self._exportOpts.get( 'resolutionScale', 1.0) ## exporting to image; pixel resolution may have changed else: aa = True #self.opts['antialias'] scale = 1.0 p.setRenderHint(p.Antialiasing, aa) p.resetTransform() tr = self.deviceTransform() if tr is None: return vRange = self.getViewBox().viewRange() mask = np.logical_and( np.logical_and(self.data['x'] > vRange[0][0], self.data['x'] < vRange[0][1]), np.logical_and( self.data['y'] > vRange[1][0], self.data['y'] < vRange[1][1])) ## remove out of view points data = self.data[mask] pts = np.empty((2, len(data['x']))) pts[0] = data['x'] pts[1] = data['y'] pts = fn.transformCoordinates(tr, pts) for i in range(len(self._symbolList)): mask = data['symbol'] == i if self.pointMode: p.setPen(self._symbolList[i].pen) list(imap(p.drawPoint, pts[0, mask], pts[1, mask])) else: x = pts[0, mask] - self._symbolList[i].width / 2.0 y = pts[1, mask] - self._symbolList[i].width / 2.0 list( imap(p.drawPixmap, x, y, repeat(self._symbolList[i].pixmap)))
def correctCoordinates(node, defs, item): # TODO: correct gradient coordinates inside defs ## Remove transformation matrices from <g> tags by applying matrix to coordinates inside. ## Each item is represented by a single top-level group with one or more groups inside. ## Each inner group contains one or more drawing primitives, possibly of different types. groups = node.getElementsByTagName('g') ## Since we leave text unchanged, groups which combine text and non-text primitives must be split apart. ## (if at some point we start correcting text transforms as well, then it should be safe to remove this) groups2 = [] for grp in groups: subGroups = [grp.cloneNode(deep=False)] textGroup = None for ch in grp.childNodes[:]: if isinstance(ch, xml.Element): if textGroup is None: textGroup = ch.tagName == 'text' if ch.tagName == 'text': if textGroup is False: subGroups.append(grp.cloneNode(deep=False)) textGroup = True else: if textGroup is True: subGroups.append(grp.cloneNode(deep=False)) textGroup = False subGroups[-1].appendChild(ch) groups2.extend(subGroups) for sg in subGroups: node.insertBefore(sg, grp) node.removeChild(grp) groups = groups2 for grp in groups: matrix = grp.getAttribute('transform') match = re.match(r'matrix\((.*)\)', matrix) if match is None: vals = [1, 0, 0, 1, 0, 0] else: vals = [float(a) for a in match.groups()[0].split(',')] tr = np.array([[vals[0], vals[2], vals[4]], [vals[1], vals[3], vals[5]]]) removeTransform = False for ch in grp.childNodes: if not isinstance(ch, xml.Element): continue if ch.tagName == 'polyline': removeTransform = True coords = np.array([[float(a) for a in c.split(',')] for c in ch.getAttribute('points').strip().split(' ')]) coords = fn.transformCoordinates(tr, coords, transpose=True) ch.setAttribute('points', ' '.join([','.join([str(a) for a in c]) for c in coords])) elif ch.tagName == 'path': removeTransform = True newCoords = '' oldCoords = ch.getAttribute('d').strip() if oldCoords == '': continue for c in oldCoords.split(' '): x, y = c.split(',') if x[0].isalpha(): t = x[0] x = x[1:] else: t = '' nc = fn.transformCoordinates(tr, np.array([[float(x), float(y)]]), transpose=True) newCoords += t+str(nc[0, 0])+','+str(nc[0, 1])+' ' ch.setAttribute('d', newCoords) elif ch.tagName == 'text': removeTransform = False ## leave text alone for now. Might need this later to correctly render text with outline. #c = np.array([ #[float(ch.getAttribute('x')), float(ch.getAttribute('y'))], #[float(ch.getAttribute('font-size')), 0], #[0,0]]) #c = fn.transformCoordinates(tr, c, transpose=True) #ch.setAttribute('x', str(c[0,0])) #ch.setAttribute('y', str(c[0,1])) #fs = c[1]-c[2] #fs = (fs**2).sum()**0.5 #ch.setAttribute('font-size', str(fs)) ## Correct some font information families = ch.getAttribute('font-family').split(',') if len(families) == 1: font = QtGui.QFont(families[0].strip('" ')) if font.style() == font.SansSerif: families.append('sans-serif') elif font.style() == font.Serif: families.append('serif') elif font.style() == font.Courier: families.append('monospace') ch.setAttribute('font-family', ', '.join([f if ' ' not in f else '"%s"'%f for f in families])) ## correct line widths if needed if removeTransform and ch.getAttribute('vector-effect') != 'non-scaling-stroke' and grp.getAttribute('stroke-width'): w = float(grp.getAttribute('stroke-width')) s = fn.transformCoordinates(tr, np.array([[w, 0], [0, 0]]), transpose=True) w = ((s[0]-s[1])**2).sum()**0.5 ch.setAttribute('stroke-width', str(w)) if removeTransform: grp.removeAttribute('transform')
def correctCoordinates(node, defs, item): # TODO: correct gradient coordinates inside defs ## Remove transformation matrices from <g> tags by applying matrix to coordinates inside. ## Each item is represented by a single top-level group with one or more groups inside. ## Each inner group contains one or more drawing primitives, possibly of different types. groups = node.getElementsByTagName('g') ## Since we leave text unchanged, groups which combine text and non-text primitives must be split apart. ## (if at some point we start correcting text transforms as well, then it should be safe to remove this) groups2 = [] for grp in groups: subGroups = [grp.cloneNode(deep=False)] textGroup = None for ch in grp.childNodes[:]: if isinstance(ch, xml.Element): if textGroup is None: textGroup = ch.tagName == 'text' if ch.tagName == 'text': if textGroup is False: subGroups.append(grp.cloneNode(deep=False)) textGroup = True else: if textGroup is True: subGroups.append(grp.cloneNode(deep=False)) textGroup = False subGroups[-1].appendChild(ch) groups2.extend(subGroups) for sg in subGroups: node.insertBefore(sg, grp) node.removeChild(grp) groups = groups2 for grp in groups: matrix = grp.getAttribute('transform') match = re.match(r'matrix\((.*)\)', matrix) if match is None: vals = [1, 0, 0, 1, 0, 0] else: vals = [float(a) for a in match.groups()[0].split(',')] tr = np.array([[vals[0], vals[2], vals[4]], [vals[1], vals[3], vals[5]]]) removeTransform = False for ch in grp.childNodes: if not isinstance(ch, xml.Element): continue if ch.tagName == 'polyline': removeTransform = True coords = np.array( [[float(a) for a in c.split(',')] for c in ch.getAttribute('points').strip().split(' ')]) coords = fn.transformCoordinates(tr, coords, transpose=True) ch.setAttribute( 'points', ' '.join([','.join([str(a) for a in c]) for c in coords])) elif ch.tagName == 'path': removeTransform = True newCoords = '' oldCoords = ch.getAttribute('d').strip() if oldCoords == '': continue for c in oldCoords.split(' '): x, y = c.split(',') if x[0].isalpha(): t = x[0] x = x[1:] else: t = '' nc = fn.transformCoordinates(tr, np.array( [[float(x), float(y)]]), transpose=True) newCoords += t + str(nc[0, 0]) + ',' + str(nc[0, 1]) + ' ' ch.setAttribute('d', newCoords) elif ch.tagName == 'text': removeTransform = False ## leave text alone for now. Might need this later to correctly render text with outline. #c = np.array([ #[float(ch.getAttribute('x')), float(ch.getAttribute('y'))], #[float(ch.getAttribute('font-size')), 0], #[0,0]]) #c = fn.transformCoordinates(tr, c, transpose=True) #ch.setAttribute('x', str(c[0,0])) #ch.setAttribute('y', str(c[0,1])) #fs = c[1]-c[2] #fs = (fs**2).sum()**0.5 #ch.setAttribute('font-size', str(fs)) ## Correct some font information families = ch.getAttribute('font-family').split(',') if len(families) == 1: font = QtGui.QFont(families[0].strip('" ')) if font.style() == font.SansSerif: families.append('sans-serif') elif font.style() == font.Serif: families.append('serif') elif font.style() == font.Courier: families.append('monospace') ch.setAttribute( 'font-family', ', '.join([ f if ' ' not in f else '"%s"' % f for f in families ])) ## correct line widths if needed if removeTransform and ch.getAttribute( 'vector-effect' ) != 'non-scaling-stroke' and grp.getAttribute('stroke-width'): w = float(grp.getAttribute('stroke-width')) s = fn.transformCoordinates(tr, np.array([[w, 0], [0, 0]]), transpose=True) w = ((s[0] - s[1])**2).sum()**0.5 ch.setAttribute('stroke-width', str(w)) if removeTransform: grp.removeAttribute('transform')