[svn] r5970: nemerle/trunk/snippets/raytracer/ray.n
malekith
svnadmin at nemerle.org
Sun Nov 27 18:04:55 CET 2005
Log:
OK, it works but is painfully slow (4x slower than OCaml).
Author: malekith
Date: Sun Nov 27 18:04:55 2005
New Revision: 5970
Modified:
nemerle/trunk/snippets/raytracer/ray.n
Modified: nemerle/trunk/snippets/raytracer/ray.n
==============================================================================
--- nemerle/trunk/snippets/raytracer/ray.n (original)
+++ nemerle/trunk/snippets/raytracer/ray.n Sun Nov 27 18:04:55 2005
@@ -3,42 +3,41 @@
using System.Math
/*
-let delta = sqrt epsilon_float
+let delta = Sqrt epsilon_float
type vec = { x: float; y: float; z: float }
let ( *| ) s r = {x = s *. r.x; y = s *. r.y; z = s *. r.z}
let ( +| ) a b = {x = a.x +. b.x; y = a.y +. b.y; z = a.z +. b.z}
let ( -| ) a b = {x = a.x -. b.x; y = a.y -. b.y; z = a.z -. b.z}
let dot a b = a.x *. b.x +. a.y *. b.y +. a.z *. b.z
-let unitise r = (1. /. sqrt (dot r r)) *| r
+let unitise r = (1. /. Sqrt (dot r r)) *| r
type scene = Sphere of vec * float | Group of vec * float * scene list
*/
[Record] \
struct Vec
- x : float
- y : float
- z : float
+ x : double
+ y : double
+ z : double
- public static @* (s : float, r : Vec) : Vec
+ public static @* (s : double, r : Vec) : Vec
Vec (s * r.x, s * r.y, s * r.z)
public static @+ (s : Vec, r : Vec) : Vec
Vec (s.x + r.x, s.y + r.y, s.z + r.z)
public static @- (s : Vec, r : Vec) : Vec
Vec (s.x - r.x, s.y - r.y, s.z - r.z)
- public static @** (s : Vec, r : Vec) : float
+ public static @** (s : Vec, r : Vec) : double
s.x * r.x + s.y * r.y + s.z * r.z
public Unitise () : Vec
- (1 / Sqrt (this ** this) :> float) * this
+ (1 / Sqrt (this ** this)) * this
variant Scene
- | Sphere { m : Vec; r : float; }
- | Group { m : Vec; s : float; l : list [Scene]; }
+ | Sphere { m : Vec; r : double; }
+ | Group { m : Vec; s : double; l : list [Scene]; }
-def delta = Sqrt (float.Epsilon) :> float
-def inf = float.PositiveInfinity
-def sqrt (x) { Sqrt (x) :> float }
+def delta = 1.49011611938476562e-08
+def inf = double.PositiveInfinity
/*
let ray_sphere orig dir center radius =
@@ -46,18 +45,18 @@
let b = dot v dir in
let disc = b *. b -. dot v v +. radius *. radius in
if disc < 0. then infinity else
- let disc = sqrt disc in
+ let disc = Sqrt disc in
(fun t2 -> if t2 < 0. then infinity else
((fun t1 -> if t1 > 0. then t1 else t2) (b -. disc))) (b +. disc)
*/
-def ray_sphere (orig, dir, center, radius : float)
+def ray_sphere (orig, dir, center, radius : double)
def v = center - orig
def b = v ** dir
def disc = b * b - (v ** v) + radius * radius
if (disc < 0) inf
else
- def disc = sqrt (disc)
+ def disc = Sqrt (disc)
def t2 = b + disc
def t1 = b - disc
if (t2 < 0) inf
@@ -100,20 +99,20 @@
*/
def ray_trace (light, orig, dir, scene)
def (lambda, normal) = intersect (orig, dir, scene)
- if (lambda == inf) 0.0f
+ if (lambda == inf) 0.0
else
def g = normal ** light
- if (g >= 0) 0.0f
+ if (g >= 0) 0.0
else
def p = orig + lambda * dir + delta * normal
- if ((intersect (p, -1 * light, scene)) [0] < inf) 0.0f
+ if ((intersect (p, -1 * light, scene)) [0] < inf) 0.0
else -g
/*
let rec create level c r =
let obj = Sphere (c, r) in
if level = 1 then obj else
- let a = 3. *. r /. sqrt 12. in
+ let a = 3. *. r /. Sqrt 12. in
let aux x' z' = create (level - 1) (c +| {x=x'; y=a; z=z'}) (0.5 *. r) in
Group (c, 3.*.r, [obj; aux (-.a) (-.a); aux a (-.a); aux (-.a) a; aux a a])
*/
@@ -121,9 +120,9 @@
def obj = Scene.Sphere (c, r)
if (level == 1) obj
else
- def a = 3 * r / sqrt (12)
+ def a = 3 * r / Sqrt (12)
def aux (x', z')
- create (level - 1, c + Vec (x', a, z'), 0.5f * r)
+ create (level - 1, c + Vec (x', a, z'), 0.5 * r)
Scene.Group (c, 3 * r , [obj, aux (-a, -a), aux (a, -a), aux (-a, a), aux (a, a)])
/*
let main level n =
@@ -146,18 +145,18 @@
done
*/
def main (level, n)
- create (level, Vec (0, -1, 0), 1)
+ def scene = create (level, Vec (0, -1, 0), 1)
def light = Vec (-1, -3, 2).Unitise ()
def ss = 4
System.Console.Write ($ "P5\n$n $n\n255\n")
def s = System.Console.OpenStandardOutput ()
for (mutable y = n - 1; y >= 0; y--)
for (mutable x = 0; x < n; x++)
- mutable g = 0.0f
+ mutable g = 0.0
for (mutable dx = 0; dx < ss; dx++)
for (mutable dy = 0; dy < ss; dy++)
def aux (x, d)
- x - n / 2.0f + d / (ss :> float)
+ x - n / 2.0 + d / (ss :> double)
def dir = Vec (aux (x, dx), aux (y, dy), n).Unitise ()
g += ray_trace (light, Vec (0,0,-4), dir, scene)
def res = (0.5 + 255 * g / (ss*ss)) :> int
More information about the svn
mailing list