def check(): """Warn the user that calpy was not found.""" if calpy_path is None: detect() if not utils.hasModule('calpy',check=True): GD.warning("Sorry, I can not run this example, because you do not have calpy installed (at least not in a place where I can find it).") exit()
def partitionByAngle(self,angle,firstprop=0,startat=0,maxruns=-1): """Detects different parts of the surface. Faces are considered to belong to the same part if the angle between these faces is smaller than the given value. """ p = -ones((self.nfaces()),dtype=int) if self.nfaces() <= 0: return p # Construct table of elements connected to each edge conn = self.connections() # Bail out if some edge has more than two connected faces if conn.shape[1] != 2: GD.warning("Surface is not a manifold") return p # Flag edges that connect two faces conn2 = (conn >= 0).sum(axis=-1) == 2 # compute normals and flag small angles over edges cosangle = cosd(angle) n = self.areaNormals()[1][conn[conn2]] small_angle = ones(conn2.shape,dtype=bool) small_angle[conn2] = dotpr(n[:,0],n[:,1]) >= cosangle # Remember edges and elements left for processing todo = ones((self.nedges(),),dtype=bool) # start with element startat startat = clip(startat,0,self.nfaces()) elems = array([startat]) prop = max(0,firstprop) run = 0 while elems.size > 0 and (maxruns < 0 or run < maxruns): run += 1 # Store prop value p[elems] = prop # Determine border edges = unique(self.faces[elems]) edges = edges[todo[edges]] if edges.size > 0: # flag edges as done todo[edges] = 0 # take elements connected over small angle elems = conn[edges][small_angle[edges]].ravel() if elems.size > 0: continue # No more elements in this part: start a new one elems = where(p<firstprop)[0] if elems.size > 0: # Start a new part elems = elems[[0]] prop += 1 return p
def edgeFront(self,startat=0,okedges=None,front_increment=1): """Generator function returning the frontal elements. startat is an element number or list of numbers of the starting front. On first call, this function returns the starting front. Each next() call returns the next front. """ print "FRONT_INCREMENT %s" % front_increment p = -ones((self.nfaces()),dtype=int) if self.nfaces() <= 0: return # Construct table of elements connected to each edge conn = self.edgeConnections() # Bail out if some edge has more than two connected faces if conn.shape[1] != 2: GD.warning("Surface is not a manifold") return # Check size of okedges if okedges is None: okedges = arange(self.nedges()) else: if okedges.ndim != 1 or okedges.shape[0] != self.nedges(): raise ValueError,"okedges has incorrect shape" # Remember edges left for processing todo = ones((self.nedges(),),dtype=bool) elems = clip(asarray(startat),0,self.nfaces()) prop = 0 while elems.size > 0: # Store prop value for current elems p[elems] = prop yield p prop += front_increment # Determine border edges = unique(self.faces[elems]) edges = edges[todo[edges]] if edges.size > 0: # flag edges as done todo[edges] = 0 # take connected elements elems = conn[okedges[edges]].ravel() #elems = conn[edges].ravel() elems = elems[(elems >= 0) * (p[elems] < 0) ] if elems.size > 0: continue # No more elements in this part: start a new one print "NO MORE ELEMENTS" elems = where(p<0)[0] if elems.size > 0: # Start a new part elems = elems[[0]] prop += 1
def partitionByFront(self,okedges,firstprop=0,startat=0,maxruns=-1,check=True): """Detects different parts of the surface using a frontal method. func is a function that takes the edge connection table as input and produces an array with nedges values flagging with a True/nonzero value all edges where the connected elements should belong to the same part. """ p = -ones((self.nfaces()),dtype=int) if self.nfaces() <= 0: return p # Construct table of elements connected to each edge conn = self.connections() # Bail out if some edge has more than two connected faces if conn.shape[1] != 2: GD.warning("Surface is not a manifold") return p # Check size of okedges if okedges.ndim != 1 or okedges.shape[0] != self.nedges(): raise ValueError,"okedges has incorrect shape" # Remember edges left for processing todo = ones((self.nedges(),),dtype=bool) startat = clip(startat,0,self.nfaces()) elems = array([startat]) prop = max(0,firstprop) run = 0 while elems.size > 0 and (maxruns < 0 or run < maxruns): run += 1 # Store prop value p[elems] = prop # Determine border edges = unique(self.faces[elems]) edges = edges[todo[edges]] if edges.size > 0: # flag edges as done todo[edges] = 0 # take elements connected over small angle elems = conn[edges][okedges[edges]].ravel() if elems.size > 0: continue # No more elements in this part: start a new one elems = where(p<firstprop)[0] if elems.size > 0: # Start a new part elems = elems[[0]] prop += 1 return p
def checkImageFormat(fmt,verbose=False): """Checks image format; if verbose, warn if it is not. Returns the image format, or None if it is not OK. """ GD.debug("Format requested: %s" % fmt) GD.debug("Formats available: %s" % imageFormats()) if fmt in imageFormats(): if fmt == 'tex' and verbose: GD.warning("This will only write a LaTeX fragment to include the 'eps' image\nYou have to create the .eps image file separately.\n") return fmt else: if verbose: error("Sorry, can not save in %s format!\n" "I suggest you use 'png' format ;)"%fmt) return None
def createMovie(): """Create a movie from a saved sequence of images.""" if not multisave: GD.warning('You need to start multisave mode first!') return names,format,window,border,hotkey,autosave,rootcrop = multisave glob = names.glob() if glob.split('.')[-1] != 'jpg': GD.warning("Currently you need to save in 'jpg' format to create movies") return #cmd = "mencoder -ovc lavc -fps 5 -o output.avi %s" % names.glob() cmd = "ffmpeg -r 1 -i %s output.mp4" % names.glob() GD.debug(cmd) utils.runCommand(cmd)
def saveImage(filename=None,window=False,multi=False,hotkey=True,autosave=False,border=False,rootcrop=False,format=None,verbose=False): """Saves an image to file or Starts/stops multisave maode. With a filename and multi==False (default), the current viewport rendering is saved to the named file. With a filename and multi==True, multisave mode is started. Without a filename, multisave mode is turned off. Two subsequent calls starting multisave mode without an intermediate call to turn it off, do not cause an error. The first multisave mode will implicitely be ended before starting the second. In multisave mode, each call to saveNext() will save an image to the next generated file name. Filenames are generated by incrementing a numeric part of the name. If the supplied filename (after removing the extension) has a trailing numeric part, subsequent images will be numbered continuing from this number. Otherwise a numeric part '-000' will be added to the filename. If window is True, the full pyFormex window is saved. If window and border are True, the window decorations will be included. If window is False, only the current canvas viewport is saved. If hotkey is True, a new image will be saved by hitting the 'S' key. If autosave is True, a new image will be saved on each execution of the 'draw' function. If neither hotkey nor autosave are True, images can only be saved by executing the saveNext() function from a script. If no format is specified, it is derived from the filename extension. fmt should be one of the valid formats as returned by imageFormats() If verbose=True, error/warnings are activated. This is usually done when this function is called from the GUI. """ global multisave # Leave multisave mode if no filename or starting new multisave mode if multisave and (filename is None or multi): GD.message("Leave multisave mode") QtCore.QObject.disconnect(GD.gui,QtCore.SIGNAL("Save"),saveNext) multisave = None if filename is None: return #chdir(filename) name,ext = os.path.splitext(filename) # Get/Check format if not format: # is None: format = checkImageFormat(imageFormatFromExt(ext)) if not format: return if multi: # Start multisave mode names = utils.NameSequence(name,ext) if os.path.exists(names.peek()): next = names.next() GD.message("Start multisave mode to files: %s (%s)" % (names.name,format)) #print hotkey if hotkey: QtCore.QObject.connect(GD.gui,QtCore.SIGNAL("Save"),saveNext) if verbose: GD.warning("Each time you hit the '%s' key,\nthe image will be saved to the next number." % GD.cfg['keys/save']) multisave = (names,format,window,border,hotkey,autosave,rootcrop) print "MULTISAVE %s "% str(multisave) return multisave is None else: # Save the image if window: if rootcrop: sta = save_main_window(filename,format,border=border) else: sta = save_window(filename,format) else: sta = save(GD.canvas,filename,format) if sta: GD.debug("Error while saving image %s" % filename) else: GD.message("Image file %s written" % filename) return