def parallels(latitudes, color=(0, 0, 0), linewidth=1, opacity=1): """ Draw parallels on the Earth. Parameters: * latitudes : list The latitudes where the parallels will be drawn. * color : tuple RGB color of the lines. Defaults to black. * linewidth : float The width of the lines * opacity : float The opacity of the lines. Must be between 0 and 1 Returns: * lines : list List of the Mayavi surface elements of each line """ lons = numpy.linspace(0, 360., 100) parallels = [] for lat in latitudes: x, y, z = utils.sph2cart(lons, numpy.ones_like(lons)*lat, 0) lines = mlab.plot3d(x, y, z, color=color, opacity=opacity, tube_radius=None) lines.actor.property.line_width = linewidth parallels.append(lines) return parallels
def parallels(latitudes, color=(0, 0, 0), linewidth=1, opacity=1): """ Draw parallels on the Earth. Parameters: * latitudes : list The latitudes where the parallels will be drawn. * color : tuple RGB color of the lines. Defaults to black. * linewidth : float The width of the lines * opacity : float The opacity of the lines. Must be between 0 and 1 Returns: * lines : list List of the Mayavi surface elements of each line """ lons = numpy.linspace(0, 360., 100) parallels = [] for lat in latitudes: x, y, z = utils.sph2cart(lons, numpy.ones_like(lons) * lat, 0) lines = mlab.plot3d(x, y, z, color=color, opacity=opacity, tube_radius=None) lines.actor.property.line_width = linewidth parallels.append(lines) return parallels
def meridians(longitudes, color=(0, 0, 0), linewidth=1, opacity=1): """ Draw meridians on the Earth. Parameters: * longitudes : list The longitudes where the meridians will be drawn. * color : tuple RGB color of the lines. Defaults to black. * linewidth : float The width of the lines * opacity : float The opacity of the lines. Must be between 0 and 1 Returns: * lines : Mayavi surface The Mayavi surface element of the lines """ lats = numpy.linspace(-90, 270., 100) x, y, z = [], [], [] for lon in longitudes: coords = utils.sph2cart(numpy.ones_like(lats)*lon, lats, 0) x.extend(coords[0].tolist()) y.extend(coords[1].tolist()) z.extend(coords[2].tolist()) x, y, z = numpy.array(x), numpy.array(y), numpy.array(z) lines = mlab.plot3d(x, y, z, color=color, opacity=opacity, tube_radius=None) lines.actor.property.line_width = linewidth return lines
def points(points, color=(0, 0, 0), size=200., opacity=1, spherical=False): """ Plot a series of 3D points. .. note:: Still doesn't plot points with physical properties. Parameters: * points : list The list of points to plot. Each point is an [x, y, z] list with the x, y, and z coordinates of the point * color : tuple = (r, g, b) RGB of the color of the points * size : float The size of the points in meters * opacity : float Decimal percentage of opacity * spherical : True or False If True, will assume the points are in [lon, lat, height] format (in degrees and meters) Returns: * glyph The Mayavi Glyph object corresponding to the points """ _lazy_import_mlab() if spherical: lon, lat, height = numpy.transpose(points) x, y, z = utils.sph2cart(lon, lat, height) else: x, y, z = numpy.transpose(points) glyph = mlab.points3d(x, y, z, color=color, opacity=opacity) glyph.glyph.glyph.scaling = False glyph.glyph.glyph_source.glyph_source.radius = size return glyph
def meridians(longitudes, color=(0, 0, 0), linewidth=1, opacity=1): """ Draw meridians on the Earth. Parameters: * longitudes : list The longitudes where the meridians will be drawn. * color : tuple RGB color of the lines. Defaults to black. * linewidth : float The width of the lines * opacity : float The opacity of the lines. Must be between 0 and 1 Returns: * lines : Mayavi surface The Mayavi surface element of the lines """ lats = numpy.linspace(-90, 270., 100) x, y, z = [], [], [] for lon in longitudes: coords = utils.sph2cart(numpy.ones_like(lats) * lon, lats, 0) x.extend(coords[0].tolist()) y.extend(coords[1].tolist()) z.extend(coords[2].tolist()) x, y, z = numpy.array(x), numpy.array(y), numpy.array(z) lines = mlab.plot3d(x, y, z, color=color, opacity=opacity, tube_radius=None) lines.actor.property.line_width = linewidth return lines
def tesseroids(tesseroids, prop=None, style='surface', opacity=1, edges=True, vmin=None, vmax=None, cmap='blue-red', linewidth=0.1): """ Plot a list of tesseroids using Mayavi2. Will not plot a value None in *tesseroids* Parameters: * tesseroids : list of :class:`fatiando.mesher.Tesseroid` The tesseroids * prop : str or None The physical property of the tesseroids to use as the color scale. If a tesseroid doesn't have *prop*, or if it is None, then it will not be plotted. If prop is a vector (like magnetization), will use the intensity (norm). * style : str Either ``'surface'`` for solid tesseroids or ``'wireframe'`` for just the contour * opacity : float Decimal percentage of opacity * edges : True or False Wether or not to display the edges of the tesseroids in black lines. Will ignore this if ``style='wireframe'`` * vmin, vmax : float Min and max values for the color scale. If *None* will default to the min and max of *prop*. * cmap : Mayavi colormap Color map to use. See the 'Colors and Legends' menu on the Mayavi2 GUI for valid color maps. * linewidth : float The width of the lines (edges). Returns: * surface the last element on the pipeline """ if style not in ['surface', 'wireframe']: raise ValueError, "Invalid style '%s'" % (style) if opacity > 1. or opacity < 0: msg = "Invalid opacity %g. Must be in range [1,0]" % (opacity) raise ValueError, msg # mlab and tvtk are really slow to import _lazy_import_mlab() _lazy_import_tvtk() if prop is None: label = 'scalar' else: label = prop # VTK parameters points = [] cells = [] offsets = [] offset = 0 mesh_size = 0 celldata = [] # To mark what index in the points the cell starts start = 0 for tess in tesseroids: if tess is None or (prop is not None and prop not in tess.props): continue w, e, s, n, top, bottom = tess.get_bounds() if prop is None: scalar = 0. else: p = tess.props[prop] if isinstance(p, int) or isinstance(p, float): scalar = p else: scalar = numpy.linalg.norm(p) points.extend([ utils.sph2cart(w, s, bottom), utils.sph2cart(e, s, bottom), utils.sph2cart(e, n, bottom), utils.sph2cart(w, n, bottom), utils.sph2cart(w, s, top), utils.sph2cart(e, s, top), utils.sph2cart(e, n, top), utils.sph2cart(w, n, top), utils.sph2cart(0.5*(w + e), s, bottom), utils.sph2cart(e, 0.5*(s + n), bottom), utils.sph2cart(0.5*(w + e), n, bottom), utils.sph2cart(w, 0.5*(s + n), bottom), utils.sph2cart(0.5*(w + e), s, top), utils.sph2cart(e, 0.5*(s + n), top), utils.sph2cart(0.5*(w + e), n, top), utils.sph2cart(w, 0.5*(s + n), top), utils.sph2cart(w, s, 0.5*(top + bottom)), utils.sph2cart(e, s, 0.5*(top + bottom)), utils.sph2cart(e, n, 0.5*(top + bottom)), utils.sph2cart(w, n, 0.5*(top + bottom))]) cells.append(20) cells.extend(range(start, start + 20)) start += 20 offsets.append(offset) offset += 21 celldata.append(scalar) mesh_size += 1 cell_array = tvtk.CellArray() cell_array.set_cells(mesh_size, numpy.array(cells)) cell_types = numpy.array([25]*mesh_size, 'i') vtkmesh = tvtk.UnstructuredGrid(points=numpy.array(points, 'f')) vtkmesh.set_cells(cell_types, numpy.array(offsets, 'i'), cell_array) vtkmesh.cell_data.scalars = numpy.array(celldata) vtkmesh.cell_data.scalars.name = label dataset = mlab.pipeline.threshold(mlab.pipeline.add_dataset(vtkmesh)) if vmin is None: vmin = min(vtkmesh.cell_data.scalars) if vmax is None: vmax = max(vtkmesh.cell_data.scalars) if style == 'wireframe': surf = mlab.pipeline.surface(mlab.pipeline.extract_edges(dataset), vmax=vmax, vmin=vmin, colormap=cmap) surf.actor.property.representation = 'wireframe' surf.actor.property.line_width = linewidth if style == 'surface': surf = mlab.pipeline.surface(dataset, vmax=vmax, vmin=vmin, colormap=cmap) surf.actor.property.representation = 'surface' if edges: edge = mlab.pipeline.surface(mlab.pipeline.extract_edges(dataset), vmax=vmax, vmin=vmin) edge.actor.property.representation = 'wireframe' edge.actor.mapper.scalar_visibility = 0 edge.actor.property.line_width = linewidth edge.actor.property.opacity = opacity surf.actor.property.opacity = opacity surf.actor.property.backface_culling = 1 return surf
def tesseroids(tesseroids, prop=None, style='surface', opacity=1, edges=True, vmin=None, vmax=None, cmap='blue-red', color=None, linewidth=1, edgecolor=(0, 0, 0), scale=(1, 1, 1)): """ Plot a list of tesseroids using Mayavi2. Will not plot a value None in *tesseroids* Parameters: * tesseroids : list of :class:`fatiando.mesher.Tesseroid` The tesseroids * prop : str or None The physical property of the tesseroids to use as the color scale. If a tesseroid doesn't have *prop*, or if it is None, then it will not be plotted. If prop is a vector (like magnetization), will use the intensity (norm). * style : str Either ``'surface'`` for solid tesseroids or ``'wireframe'`` for just the contour * opacity : float Decimal percentage of opacity * edges : True or False Wether or not to display the edges of the tesseroids in black lines. Will ignore this if ``style='wireframe'`` * vmin, vmax : float Min and max values for the color scale. If *None* will default to the min and max of *prop*. * cmap : Mayavi colormap Color map to use. See the 'Colors and Legends' menu on the Mayavi2 GUI for valid color maps. * color : None or tuple = (r, g, b) If not None, then for all tesseroids to have this RGB color * linewidth : float The width of the lines (edges) of the tesseroids. * edgecolor : tuple = (r, g, b) RGB of the color of the edges. If style='wireframe', then will be ignored. Use parameter *color* instead * scale : (slon, slat, sz) Scale factors used to exaggerate on a particular direction, e.g., if scale = (1, 1, 2), the vertical dimension will be 2x larger than the others Returns: * surface the last element on the pipeline """ if style not in ['surface', 'wireframe']: raise ValueError("Invalid style '%s'" % (style)) if opacity > 1. or opacity < 0: raise ValueError("Invalid opacity %g. Must be in range [1,0]" % (opacity)) # mlab and tvtk are really slow to import _lazy_import_mlab() _lazy_import_tvtk() if prop is None: label = 'scalar' else: label = prop # VTK parameters points = [] cells = [] offsets = [] offset = 0 mesh_size = 0 celldata = [] # To mark what index in the points the cell starts start = 0 for tess in tesseroids: if tess is None or (prop is not None and prop not in tess.props): continue w, e, s, n, top, bottom = tess.get_bounds() w *= scale[0] e *= scale[0] s *= scale[1] n *= scale[1] top *= scale[2] bottom *= scale[2] if prop is None: scalar = 0. else: p = tess.props[prop] if isinstance(p, int) or isinstance(p, float): scalar = p else: scalar = numpy.linalg.norm(p) points.extend([ utils.sph2cart(w, s, bottom), utils.sph2cart(e, s, bottom), utils.sph2cart(e, n, bottom), utils.sph2cart(w, n, bottom), utils.sph2cart(w, s, top), utils.sph2cart(e, s, top), utils.sph2cart(e, n, top), utils.sph2cart(w, n, top), utils.sph2cart(0.5 * (w + e), s, bottom), utils.sph2cart(e, 0.5 * (s + n), bottom), utils.sph2cart(0.5 * (w + e), n, bottom), utils.sph2cart(w, 0.5 * (s + n), bottom), utils.sph2cart(0.5 * (w + e), s, top), utils.sph2cart(e, 0.5 * (s + n), top), utils.sph2cart(0.5 * (w + e), n, top), utils.sph2cart(w, 0.5 * (s + n), top), utils.sph2cart(w, s, 0.5 * (top + bottom)), utils.sph2cart(e, s, 0.5 * (top + bottom)), utils.sph2cart(e, n, 0.5 * (top + bottom)), utils.sph2cart(w, n, 0.5 * (top + bottom)) ]) cells.append(20) cells.extend(list(range(start, start + 20))) start += 20 offsets.append(offset) offset += 21 celldata.append(scalar) mesh_size += 1 cell_array = tvtk.CellArray() cell_array.set_cells(mesh_size, numpy.array(cells)) cell_types = numpy.array([25] * mesh_size, 'i') vtkmesh = tvtk.UnstructuredGrid(points=numpy.array(points, 'f')) vtkmesh.set_cells(cell_types, numpy.array(offsets, 'i'), cell_array) vtkmesh.cell_data.scalars = numpy.array(celldata) vtkmesh.cell_data.scalars.name = label dataset = mlab.pipeline.threshold(mlab.pipeline.add_dataset(vtkmesh)) if vmin is None: vmin = min(vtkmesh.cell_data.scalars) if vmax is None: vmax = max(vtkmesh.cell_data.scalars) if style == 'wireframe': surf = mlab.pipeline.surface(mlab.pipeline.extract_edges(dataset), vmax=vmax, vmin=vmin, colormap=cmap) surf.actor.property.representation = 'wireframe' surf.actor.property.line_width = linewidth if style == 'surface': surf = mlab.pipeline.surface(dataset, vmax=vmax, vmin=vmin, colormap=cmap) surf.actor.property.representation = 'surface' if edges: edge = mlab.pipeline.surface(mlab.pipeline.extract_edges(dataset), vmax=vmax, vmin=vmin) edge.actor.property.representation = 'wireframe' edge.actor.mapper.scalar_visibility = 0 edge.actor.property.line_width = linewidth edge.actor.property.opacity = opacity edge.actor.property.color = edgecolor surf.actor.property.opacity = opacity surf.actor.property.backface_culling = False if color is not None: surf.actor.mapper.scalar_visibility = 0 surf.actor.property.color = color return surf