]> gitweb.michael.orlitzky.com - spline3.git/blobdiff - src/Grid.hs
Move the Tetrahedron tests into the Tetrehedron module.
[spline3.git] / src / Grid.hs
index 2cf56ba3f437461e2112dd9f2d5c6db206a2f6fb..5ff4aa28ab95b4e3ad62c9798ccb836327d57164 100644 (file)
@@ -2,10 +2,12 @@
 --   for it. We hide the main Grid constructor because we don't want
 --   to allow instantiation of a grid with h <= 0.
 module Grid (
+  cube_at,
   grid_tests,
   make_grid,
   slow_tests,
-  zoom
+  zoom,
+  zoom_chunk
   )
 where
 
@@ -22,13 +24,12 @@ import Comparisons
 import Cube (Cube(Cube),
              find_containing_tetrahedron,
              tetrahedra,
-             tetrahedron0,
-             tetrahedron15)
+             tetrahedron)
 import Examples
 import FunctionValues
 import Point (Point)
 import ScaleFactor
-import Tetrahedron (c, polynomial, v0, v1, v2, v3)
+import Tetrahedron (Tetrahedron, c, polynomial, v0, v1, v2, v3)
 import ThreeDimensional
 import Values (Values3D, dims, empty3d, zoom_shape)
 
@@ -71,8 +72,9 @@ cubes delta fvs
                    j <- [0..ymax],
                    k <- [0..zmax],
                    let tet_vol = (1/24)*(delta^(3::Int)),
+                   let fvs' = make_values fvs i j k,
                    let cube_ijk =
-                         Cube delta i j k (make_values fvs i j k) tet_vol]
+                         Cube delta i j k fvs' tet_vol]
      where
        xmax = xsize - 1
        ymax = ysize - 1
@@ -133,19 +135,20 @@ find_containing_cube g p =
 
 {-# INLINE zoom_lookup #-}
 zoom_lookup :: Grid -> ScaleFactor -> a -> (R.DIM3 -> Double)
-zoom_lookup g scale_factor _ = zoom_result g scale_factor
+zoom_lookup g scale_factor _ =
+    zoom_result g scale_factor
 
 
 {-# INLINE zoom_result #-}
 zoom_result :: Grid -> ScaleFactor -> R.DIM3 -> Double
-zoom_result g (sfx, sfy, sfz) (R.Z R.:. i R.:. j R.:. k) =
+zoom_result g (sfx, sfy, sfz) (R.Z R.:. m R.:. n R.:. o) =
   f p
   where
     offset = (h g)/2
-    i' = (fromIntegral i) / (fromIntegral sfx) - offset
-    j' = (fromIntegral j) / (fromIntegral sfy) - offset
-    k' = (fromIntegral k) / (fromIntegral sfz) - offset
-    p  = (i', j', k') :: Point
+    m' = (fromIntegral m) / (fromIntegral sfx) - offset
+    n' = (fromIntegral n) / (fromIntegral sfy) - offset
+    o' = (fromIntegral o) / (fromIntegral sfz) - offset
+    p  = (m', n', o') :: Point
     cube = find_containing_cube g p
     t = find_containing_tetrahedron cube p
     f = polynomial t
@@ -155,7 +158,7 @@ zoom :: Grid -> ScaleFactor -> Values3D
 zoom g scale_factor
     | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
     | otherwise =
-        R.force $ R.traverse arr transExtent (zoom_lookup g scale_factor)
+        R.force $ R.unsafeTraverse arr transExtent (zoom_lookup g scale_factor)
           where
             arr = function_values g
             (xsize, ysize, zsize) = dims arr
@@ -164,6 +167,43 @@ zoom g scale_factor
 
 
 
+{-# INLINE zoom_chunk_lookup #-}
+zoom_chunk_lookup :: Grid -> ScaleFactor -> a -> (R.DIM3 -> Double)
+zoom_chunk_lookup g scale_factor _ =
+    zoom_chunk_result g scale_factor
+
+
+{-# INLINE zoom_chunk_result #-}
+zoom_chunk_result :: Grid -> ScaleFactor -> R.DIM3 -> Double
+zoom_chunk_result g (sfx, sfy, sfz) (R.Z R.:. m R.:. n R.:. o)
+  | m /= 1 = 0 -- We're going to drop these anyway.
+  | otherwise = f p
+    where
+      offset = (h g)/2
+      sfx' = fromIntegral sfx
+      sfy' = fromIntegral sfy
+      sfz' = fromIntegral sfz
+      m' = (fromIntegral m) / sfx' - offset
+      n' = (fromIntegral n) / sfy' - offset
+      o' = (fromIntegral o) / sfz' - offset
+      p  = (m', n', o') :: Point
+      cube = find_containing_cube g p
+      t = find_containing_tetrahedron cube p
+      f = polynomial t
+
+
+zoom_chunk :: Grid -> ScaleFactor -> Values3D
+zoom_chunk g scale_factor
+    | xsize == 0 || ysize == 0 || zsize == 0 = empty3d
+    | otherwise =
+        R.force $ R.unsafeTraverse arr transExtent (zoom_chunk_lookup g scale_factor)
+          where
+            arr = function_values g
+            (xsize, ysize, zsize) = dims arr
+            transExtent = zoom_shape scale_factor
+
+
+
 -- | Check all coefficients of tetrahedron0 belonging to the cube
 --   centered on (1,1,1) with a grid constructed from the trilinear
 --   values. See example one in the paper.
@@ -205,7 +245,7 @@ trilinear_c0_t0_tests =
   where
     g = make_grid 1 trilinear
     cube = cube_at g 1 1 1
-    t = tetrahedron0 cube
+    t = tetrahedron cube 0
 
     test_trilinear_c0030 :: Assertion
     test_trilinear_c0030 =
@@ -334,7 +374,7 @@ test_zeros_reproduced =
     where
       g = make_grid 1 zeros
       c0 = cube_at g 1 1 1
-      t0 = tetrahedron0 c0
+      t0 = tetrahedron c0 0
       p = polynomial t0
 
 
@@ -362,18 +402,18 @@ test_trilinear9x9x9_reproduced =
 --
 --   Example from before the fix:
 --
---   > b0 (tetrahedron15 c) p
---   -3.4694469519536365e-18
+--   b1 (tetrahedron c 20) (0, 17.5, 0.5)
+--   -0.0
 --
 test_tetrahedra_collision_sensitivity :: Assertion
 test_tetrahedra_collision_sensitivity =
   assertTrue "tetrahedron collision tests isn't too sensitive" $
-             contains_point t15 p
+             contains_point t20 p
   where
     g = make_grid 1 naturals_1d
-    cube = cube_at g 0 17 1
-    p = (0, 16.75, 0.5) :: Point
-    t15 = tetrahedron15 cube
+    cube = cube_at g 0 18 0
+    p = (0, 17.5, 0.5) :: Point
+    t20 = tetrahedron cube 20
 
 
 prop_cube_indices_never_go_out_of_bounds :: Grid -> Gen Bool