Permalink
Browse files

Reuse precompiled values in interpolation

  • Loading branch information...
1 parent 8c48c8a commit 862f9d6d6a22f2f6be0dadc18cebcc026f5a91ac Reinoud Zandijk committed Mar 30, 2012
Showing with 19 additions and 21 deletions.
  1. +19 −21 Graphics/Implicit/Export/MarchingCubes.hs
View
40 Graphics/Implicit/Export/MarchingCubes.hs
@@ -84,28 +84,26 @@ getMesh3 (x1,y1,z1) (x2,y2,z2) res obj =
-- | Interpolate between two (x,y,z) pairs to find the zero
-- using binary search
-interpolate :: ℝ3 -> ℝ3 -> Obj3 -> ℝ3
-interpolate (x1, y1, z1) (x2, y2, z2) obj =
+interpolate :: ℝ3 -> -> ℝ3 -> -> Obj3 -> ℝ3
+interpolate (x1, y1, z1) left_obj (x2, y2, z2) right_obj obj =
let
eps = 1e-6
dx = x2 - x1
dy = y2 - y1
dz = z2 - z1
d = sqrt (dx**2 + dy**2 + dz**2)
mid = (x1 + dx/2, y1 + dy/2, z1 + dz/2)
- left_obj = obj (x1, y1, z1)
mid_obj = obj mid
- right_obj = obj (x2, y2, z2)
in
-- if product is positive, both sides are on the same side of zero
if (left_obj * right_obj > 0) then (x1, y1, z1) -- no cut so dummy result
else
if (d < eps) then (x1, y1, z1) -- too close, finish
else
if (left_obj * mid_obj < 0) then -- on the left
- interpolate (x1, y1, z1) mid obj
+ interpolate (x1, y1, z1) left_obj mid mid_obj obj
else -- on the right
- interpolate mid (x2, y2, z2) obj
+ interpolate mid mid_obj (x2, y2, z2) right_obj obj
@@ -116,21 +114,6 @@ interpolate (x1, y1, z1) (x2, y2, z2) obj =
getCubeTriangles :: ℝ3 -> ℝ3 -> Obj3 -> [Triangle]
getCubeTriangles (x1, y1, z1) (x2, y2, z2) obj =
let
- x1y1 = interpolate (x1, y1, z1) (x1, y1, z2) obj
- x1y2 = interpolate (x1, y2, z1) (x1, y2, z2) obj
- x2y1 = interpolate (x2, y1, z1) (x2, y1, z2) obj
- x2y2 = interpolate (x2, y2, z1) (x2, y2, z2) obj
-
- x1z1 = interpolate (x1, y1, z1) (x1, y2, z1) obj
- x1z2 = interpolate (x1, y1, z2) (x1, y2, z2) obj
- x2z1 = interpolate (x2, y1, z1) (x2, y2, z1) obj
- x2z2 = interpolate (x2, y1, z2) (x2, y2, z2) obj
-
- y1z1 = interpolate (x1, y1, z1) (x2, y1, z1) obj
- y1z2 = interpolate (x1, y1, z2) (x2, y1, z2) obj
- y2z1 = interpolate (x1, y2, z1) (x2, y2, z1) obj
- y2z2 = interpolate (x1, y2, z2) (x2, y2, z2) obj
-
x1y1z1 = obj (x1, y1, z1)
x2y1z1 = obj (x2, y1, z1)
x1y2z1 = obj (x1, y2, z1)
@@ -140,6 +123,21 @@ getCubeTriangles (x1, y1, z1) (x2, y2, z2) obj =
x1y2z2 = obj (x1, y2, z2)
x2y2z2 = obj (x2, y2, z2)
+ x1y1 = interpolate (x1, y1, z1) x1y1z1 (x1, y1, z2) x1y1z2 obj
+ x1y2 = interpolate (x1, y2, z1) x1y2z1 (x1, y2, z2) x1y2z2 obj
+ x2y1 = interpolate (x2, y1, z1) x2y1z1 (x2, y1, z2) x2y1z2 obj
+ x2y2 = interpolate (x2, y2, z1) x2y2z1 (x2, y2, z2) x2y2z2 obj
+
+ x1z1 = interpolate (x1, y1, z1) x1y1z1 (x1, y2, z1) x1y2z1 obj
+ x1z2 = interpolate (x1, y1, z2) x1y1z2 (x1, y2, z2) x1y2z2 obj
+ x2z1 = interpolate (x2, y1, z1) x2y1z1 (x2, y2, z1) x2y2z1 obj
+ x2z2 = interpolate (x2, y1, z2) x2y1z2 (x2, y2, z2) x2y2z2 obj
+
+ y1z1 = interpolate (x1, y1, z1) x1y1z1 (x2, y1, z1) x2y1z1 obj
+ y1z2 = interpolate (x1, y1, z2) x1y1z2 (x2, y1, z2) x2y1z2 obj
+ y2z1 = interpolate (x1, y2, z1) x1y2z1 (x2, y2, z1) x2y2z1 obj
+ y2z2 = interpolate (x1, y2, z2) x1y2z2 (x2, y2, z2) x2y2z2 obj
+
{-
(x,y,z) = (x1, y1, z1)

0 comments on commit 862f9d6

Please sign in to comment.