[Yt-svn] yt: 2 new changesets

hg at spacepope.org hg at spacepope.org
Thu Jun 10 06:23:53 PDT 2010


hg Repository: yt
details:   yt/rev/349c439374ac
changeset: 1766:349c439374ac
user:      Matthew Turk <matthewturk at gmail.com>
date:
Wed Jun 09 11:12:34 2010 -0400
description:
Adding more docstrings, changed clear_plots to clear_figures in interactive PC

hg Repository: yt
details:   yt/rev/33b7b37a6f7d
changeset: 1767:33b7b37a6f7d
user:      Matthew Turk <matthewturk at gmail.com>
date:
Thu Jun 10 09:23:38 2010 -0400
description:
Adding Python/Cython code for *first pass* at perspective projections.  I'm
getting more artifacts than I expected, even without the explicit inclusion of
a front cutting plane...  Next up is the cutting plane (to make it a proper
frustum) and then the artifacts can be handled.

diffstat:

 yt/_amr_utils/VolumeIntegrator.pyx         |  33 ++++++++++++----
 yt/extensions/volume_rendering/__init__.py |   2 +-
 yt/extensions/volume_rendering/camera.py   |  42 ++++++++++++++++++--
 yt/raven/plot_collection.py                |  50 ++++++++++++++++++++++---
 4 files changed, 107 insertions(+), 20 deletions(-)

diffs (207 lines):

diff -r 2298396aca46 -r 33b7b37a6f7d yt/_amr_utils/VolumeIntegrator.pyx
--- a/yt/_amr_utils/VolumeIntegrator.pyx	Tue Jun 08 17:16:09 2010 -0400
+++ b/yt/_amr_utils/VolumeIntegrator.pyx	Thu Jun 10 09:23:38 2010 -0400
@@ -226,12 +226,13 @@
     cdef int nv[2]
     cdef int vp_strides[3]
     cdef int im_strides[3]
+    cdef int vd_strides[3]
     cdef public object ax_vec, ay_vec
     cdef np.float64_t *x_vec, *y_vec
 
     def __cinit__(self, 
                   np.ndarray[np.float64_t, ndim=3] vp_pos,
-                  np.ndarray[np.float64_t, ndim=1] vp_dir,
+                  np.ndarray vp_dir,
                   np.ndarray[np.float64_t, ndim=1] center,
                   bounds,
                   np.ndarray[np.float64_t, ndim=3] image,
@@ -258,6 +259,11 @@
         for i in range(3):
             self.vp_strides[i] = vp_pos.strides[i] / 8
             self.im_strides[i] = image.strides[i] / 8
+        if vp_dir.ndim > 1:
+            for i in range(3):
+                self.vd_strides[i] = vp_dir.strides[i] / 8
+        else:
+            self.vd_strides[0] = self.vd_strides[1] = self.vd_strides[2] = -1
 
     @cython.boundscheck(False)
     @cython.wraparound(False)
@@ -338,19 +344,30 @@
         cdef int vi, vj, hit, i, ni, nj, nn
         cdef int iter[4]
         cdef np.float64_t v_pos[3], v_dir[3], rgba[6], extrema[4]
+        hit = 0
         self.calculate_extent(vp, extrema)
         vp.get_start_stop(extrema, iter)
         iter[0] = iclip(iter[0], 0, vp.nv[0])
         iter[1] = iclip(iter[1], 0, vp.nv[0])
         iter[2] = iclip(iter[2], 0, vp.nv[1])
         iter[3] = iclip(iter[3], 0, vp.nv[1])
-        hit = 0
-        for vi in range(iter[0], iter[1]):
-            for vj in range(iter[2], iter[3]):
-                vp.copy_into(vp.vp_pos, v_pos, vi, vj, 3, vp.vp_strides)
-                vp.copy_into(vp.image, rgba, vi, vj, 3, vp.im_strides)
-                self.integrate_ray(v_pos, vp.vp_dir, rgba, tf)
-                vp.copy_back(rgba, vp.image, vi, vj, 3, vp.im_strides)
+        if vp.vd_strides[0] == -1:
+            for vi in range(iter[0], iter[1]):
+                for vj in range(iter[2], iter[3]):
+                    vp.copy_into(vp.vp_pos, v_pos, vi, vj, 3, vp.vp_strides)
+                    vp.copy_into(vp.image, rgba, vi, vj, 3, vp.im_strides)
+                    self.integrate_ray(v_pos, vp.vp_dir, rgba, tf)
+                    vp.copy_back(rgba, vp.image, vi, vj, 3, vp.im_strides)
+        else:
+            # If we do not have an orthographic projection, we have to cast all
+            # our rays (until we can get an extrema calculation...)
+            for vi in range(vp.nv[0]):
+                for vj in range(vp.nv[1]):
+                    vp.copy_into(vp.vp_pos, v_pos, vi, vj, 3, vp.vp_strides)
+                    vp.copy_into(vp.image, rgba, vi, vj, 3, vp.im_strides)
+                    vp.copy_into(vp.vp_dir, v_dir, vi, vj, 3, vp.vd_strides)
+                    self.integrate_ray(v_pos, v_dir, rgba, tf)
+                    vp.copy_back(rgba, vp.image, vi, vj, 3, vp.im_strides)
         return hit
 
     @cython.boundscheck(False)
diff -r 2298396aca46 -r 33b7b37a6f7d yt/extensions/volume_rendering/__init__.py
--- a/yt/extensions/volume_rendering/__init__.py	Tue Jun 08 17:16:09 2010 -0400
+++ b/yt/extensions/volume_rendering/__init__.py	Thu Jun 10 09:23:38 2010 -0400
@@ -38,4 +38,4 @@
 from image_handling import export_rgba, import_rgba, \
                            plot_channel, plot_rgb
 from software_sampler import VolumeRendering
-from camera import Camera, StereoPairCamera
+from camera import Camera, PerspectiveCamera, StereoPairCamera
diff -r 2298396aca46 -r 33b7b37a6f7d yt/extensions/volume_rendering/camera.py
--- a/yt/extensions/volume_rendering/camera.py	Tue Jun 08 17:16:09 2010 -0400
+++ b/yt/extensions/volume_rendering/camera.py	Thu Jun 10 09:23:38 2010 -0400
@@ -94,13 +94,13 @@
                          self.resolution[1])[None,:]
         inv_mat = self.inv_mat
         bc = self.back_center
-        vectors = na.zeros((self.resolution[0], self.resolution[1], 3),
+        positions = na.zeros((self.resolution[0], self.resolution[1], 3),
                           dtype='float64', order='C')
-        vectors[:,:,0] = inv_mat[0,0]*px+inv_mat[0,1]*py+self.back_center[0]
-        vectors[:,:,1] = inv_mat[1,0]*px+inv_mat[1,1]*py+self.back_center[1]
-        vectors[:,:,2] = inv_mat[2,0]*px+inv_mat[2,1]*py+self.back_center[2]
+        positions[:,:,0] = inv_mat[0,0]*px+inv_mat[0,1]*py+self.back_center[0]
+        positions[:,:,1] = inv_mat[1,0]*px+inv_mat[1,1]*py+self.back_center[1]
+        positions[:,:,2] = inv_mat[2,0]*px+inv_mat[2,1]*py+self.back_center[2]
         bounds = (px.min(), px.max(), py.min(), py.max())
-        vector_plane = au.VectorPlane(vectors, self.box_vectors[2],
+        vector_plane = au.VectorPlane(positions, self.box_vectors[2],
                                       self.back_center, bounds, image,
                                       self.unit_vectors[0],
                                       self.unit_vectors[1])
@@ -134,6 +134,38 @@
             self.zoom(f)
             yield self.snapshot()
 
+
+class PerspectiveCamera(Camera):
+    def get_vector_plane(self, image):
+        # We should move away from pre-generation of vectors like this and into
+        # the usage of on-the-fly generation in the VolumeIntegrator module
+        # We might have a different width and back_center
+        px = na.linspace(-self.width[0]/2.0, self.width[0]/2.0,
+                         self.resolution[0])[:,None]
+        py = na.linspace(-self.width[1]/2.0, self.width[1]/2.0,
+                         self.resolution[1])[None,:]
+        inv_mat = self.inv_mat
+        bc = self.back_center
+        positions = na.zeros((self.resolution[0], self.resolution[1], 3),
+                          dtype='float64', order='C')
+        positions[:,:,0] = inv_mat[0,0]*px+inv_mat[0,1]*py+self.back_center[0]
+        positions[:,:,1] = inv_mat[1,0]*px+inv_mat[1,1]*py+self.back_center[1]
+        positions[:,:,2] = inv_mat[2,0]*px+inv_mat[2,1]*py+self.back_center[2]
+        bounds = (px.min(), px.max(), py.min(), py.max())
+        
+        # We are likely adding on an odd cutting condition here
+        vectors = self.front_center - positions
+        n = vectors * vectors
+        n = na.sum(n, axis=2)**0.5
+        vectors /= n[:,:,None]
+        print vectors.shape, vectors.dtype, vectors.flags
+
+        vector_plane = au.VectorPlane(positions, vectors,
+                                      self.back_center, bounds, image,
+                                      self.unit_vectors[0],
+                                      self.unit_vectors[1])
+        return vector_plane
+
 class StereoPairCamera(Camera):
     def __init__(self, original_camera, relative_separation = 0.005):
         self.original_camera = original_camera
diff -r 2298396aca46 -r 33b7b37a6f7d yt/raven/plot_collection.py
--- a/yt/raven/plot_collection.py	Tue Jun 08 17:16:09 2010 -0400
+++ b/yt/raven/plot_collection.py	Thu Jun 10 09:23:38 2010 -0400
@@ -1477,21 +1477,59 @@
         super(PlotCollectionInteractive, self).__init__(*args, **kwargs)
 
     def redraw(self):
+        r"""Redraw all affiliated plots.
+
+        To ensure that any interactive windows are up to date, this function
+        can be called to redraw all images into them.
+        """
         for plot in self.plots:
             plot._redraw_image()
         self.pylab.show()
 
-    def clear_plots(self):
+    def clear_figures(self):
+        r"""Clear all interactive figures affiliated with this collection.
+
+        Because reusing figures between plot collections can be tricky,
+        occasionally they must be manually cleared to re-obtain empty figures
+        for future plotting.  This will clear all figures.
+        """
         for plot in self.plots:
             self.pylab.figure(plot._fig_num)
             self.pylab.clf()
-        PlotCollection.clear_plots(self)
 
 def get_multi_plot(nx, ny, colorbar = 'vertical', bw = 4, dpi=300):
-    """
-    This returns *nx* and *ny* axes on a single figure, set up so that the
-    *colorbar* can be placed either vertically or horizontally in a bonus
-    column or row, respectively.  The axes all have base width of *bw* inches.
+    r"""Construct a multiple axes plot object, with or without a colorbar, into
+    which multiple plots may be inserted.
+
+    This will create a set of `matplotlib.axes.Axes`, all lined up into a grid,
+    which are then returned to the user and which can be used to plot multiple
+    plots on a single figure.
+
+    Parameters
+    ----------
+    nx : int
+        Number of axes to create along the x-direction
+    ny : int
+        Number of axes to create along the y-direction
+    colorbar : {'vertical', 'horizontal', None}, optional
+        Should Axes objects for colorbars be allocated, and if so, should they
+        correspond to the horizontal or vertical set of axes?
+
+    Returns
+    -------
+    fig : `matplotlib.figure.Figure
+        The figure created inside which the axes reside
+    tr : list of list of `matplotlib.axes.Axes` objects
+        This is a list, where the inner list is along the x-axis and the outer
+        is along the y-axis
+    cbars : list of `matplotlib.axes.Axes` objects
+        Each of these is an axes onto which a colorbar can be placed.
+
+    Notes
+    -----
+    This is a simple implementation for a common use case.  Viewing the source
+    can be instructure, and is encouraged to see how to generate more
+    complicated or more specific sets of multiplots for your own purposes.
     """
     PlotTypes.Initialize()
     hf, wf = 1.0/ny, 1.0/nx



More information about the yt-svn mailing list