       return obj.e
     }
   }
-  if (obj.refl == Refl_t.DIFF) {                  // Ideal DIFFUSE reflection
+  switch (obj.refl) {
+  case Refl_t.DIFF:                  // Ideal DIFFUSE reflection
     let r1=2*M_PI*Xi.next(), r2=Xi.next(), r2s=sqrt(r2);
-    let w=nl
-    let bignlx = fabs(w.x)>0.1
-    let tempv = bignlx ? Vec(x: 0, y: 1, z: 0) : Vec(x: 1, y: 0, z: 0)
-    let u = tempv%w.norm()
+    let w = nl
+    let u = ((fabs(w.x)>0.1 ? Vec(x:0, y:1, z:0) : Vec(x:1, y:0, z:0)) % w).norm()
     let v = w % u
     let d = (u*cos(r1)*r2s + v*sin(r1)*r2s + w*sqrt(1-r2)).norm()
     return obj.e + f * radiance(Ray(o: x, d:d), depth, Xi)
-  } else if (obj.refl == Refl_t.SPEC) {            // Ideal SPECULAR reflection
+  case Refl_t.SPEC: // Ideal SPECULAR reflection
     return obj.e + f * (radiance(Ray(o:x, d:r.d-n*2*n.dot(r.d)), depth, Xi))
-  }
-  let reflRay = Ray(o:x, d:r.d-n*2*n.dot(r.d))    // Ideal dielectric REFRACTION
-  let into = n.dot(nl)>0                // Ray from outside going in?
-  let nc = 1, nt=1.5
-  let nnt = into ? Double(nc) / nt : nt / Double(nc)
-  let ddn = r.d.dot(nl)
-  let cos2t=1-nnt*nnt*(1-ddn*ddn)
-  if (cos2t<0) {    // Total internal reflection
-    return obj.e + f * radiance(reflRay, depth, Xi)
-  }
-  let tdir = (r.d * nnt - n * ((into ? 1 : -1)*(ddn*nnt+sqrt(cos2t)))).norm()
-  let a = nt-Double(nc), b = nt+Double(nc), R0 = a*a/(b*b), c = 1-(into ? -ddn : tdir.dot(n))
-  let Re=R0+(1-R0)*c*c*c*c*c,Tr=1-Re,P=0.25+0.5*Re,RP=Re/P,TP=Tr/(1-P)
-  return obj.e + f * (depth>2 ? (Xi.next()<P ?   // Russian roulette
-    radiance(reflRay,depth,Xi) * RP : radiance(Ray(o: x, d: tdir),depth,Xi)*TP) :
-    radiance(reflRay,depth,Xi) * Re + radiance(Ray(o: x, d: tdir),depth,Xi)*Tr);
+  case Refl_t.REFR:
+    let reflRay = Ray(o:x, d:r.d-n*2*n.dot(r.d))    // Ideal dielectric REFRACTION
+    let into = n.dot(nl)>0                // Ray from outside going in?
+    let nc = 1, nt=1.5
+    let nnt = into ? Double(nc) / nt : nt / Double(nc)
+    let ddn = r.d.dot(nl)
+    let cos2t=1-nnt*nnt*(1-ddn*ddn)
+    if (cos2t<0) {    // Total internal reflection
+      return obj.e + f * radiance(reflRay, depth, Xi)
+    }
+    let tdir = (r.d * nnt - n * ((into ? 1 : -1)*(ddn*nnt+sqrt(cos2t)))).norm()
+    let a = nt-Double(nc), b = nt+Double(nc), R0 = a*a/(b*b), c = 1-(into ? -ddn : tdir.dot(n))
+    let Re=R0+(1-R0)*c*c*c*c*c,Tr=1-Re,P=0.25+0.5*Re,RP=Re/P,TP=Tr/(1-P)
+    return obj.e + f * (depth>2 ? (Xi.next()<P ?   // Russian roulette
+      radiance(reflRay,depth,Xi) * RP : radiance(Ray(o: x, d: tdir),depth,Xi)*TP) :
+      radiance(reflRay,depth,Xi) * Re + radiance(Ray(o: x, d: tdir),depth,Xi)*Tr);
+  }
 }
 
 
 func main() {
   let argc = C_ARGC, argv = C_ARGV
-  // let w=1024, h=768
-  let w = 40, h = 30
+  let w=1024, h=768
+  // let w = 40, h = 30
   let samps = argc==2 ? Int(atoi(argv[1])/4) : 1 // # samples
   let cam = Ray(o:Vec(x:50,y:52,z:295.6), d:Vec(x:0,y:-0.042612,z:-1).norm()) // cam pos, dir
   let cx = Vec(x:Double(w) * 0.5135 / Double(h), y:0, z:0)

}

let stderr = StandardErrorOutputStream()
struct Vec {
  let x, y, z : Double
  init() { self.init(x:0, y:0, z:0) }

}

struct Ray { let o, d : Vec; init(o : Vec, d : Vec) {self.o = o; self.d = d}}

enum Refl_t { case DIFF; case SPEC; case REFR }  // material types, used in radiance()

struct Sphere {
  let rad : Double       // radius
  let p, e, c : Vec      // position, emission, color
