Scrayz

Work has finally slowed down enough that I have inched my Scala based ray tracer along, just a tiny bit. I'm keeping the code on gitla...

March 9, 2020

Scrayz

Work has finally slowed down enough that I have inched my Scala based ray tracer along, just a tiny bit.

I'm keeping the code on gitlab this time, and so far it has been different, but problem free.  CI is built in on GitLab, but it did take a bunch of fiddling due to me not understanding a bunch of stuff, and out of date docs (of course).  So, on the CI side of things, I think it will run my tests on push now. Next up will be to enable Coveralls, "just because".


Now that I have that going, I started flipping through the book a little bit again.

The first step (after reading) is to get the tests written.  Luckily there are files full of features to start with. So, that's exactly what I'm going to do using ScalaTest. It supports multiple styles, but to stay with the style in the book, I'm going to use FeatureSpec.

Itt is fairly straightforward at the top-most level to get the tests in a format ScalaTest likes:

import org.scalatest.FeatureSpec         
import org.scalatest.GivenWhenThen               

class TupleFeatures extends FeatureSpec with GivenWhenThen {
  feature("Tuples, Vectors, and Points") {

    scenario("A tuple with w=1.0 is a point") {
      Given("a ← tuple(4.3, -4.2, 3.1, 1.0)")
      Then("a.x = 4.3")               
      And(" a.y = -4.2")                      
      And(" a.z = 3.1")              
      And(" a.w = 1.0")                                  
      And(" a is a point")      
      And(" a is not a vector")  
    } 
  }
}



So, the next step there is to figure out where and how you write the background steps that set things up for you.


While flipping through the book again, it started off with Tuples. I thought I knew what to do but there is a slight rub.  The book uses four values (x, y, z, w) where the "thing" is a point of w = 1.0 and a vector if w = 0.0. I may be able to do something with the type system to handle this, I'm not sure.

My first thought was to not try to combine them at all, so after some failed attempts and a StackOverflow question, I ended up going down this road:

package com.craigtreptow.scrayz

package object types {
  final case class X(v: Double) extends AnyVal
  final case class Y(v: Double) extends AnyVal
  final case class Z(v: Double) extends AnyVal
  final case class Point(x: X, y: Y, z: Z)
}


package com.craigtreptow.scrayz.types

object Main extends App {
  val x: X = X(1.1)
  val y: Y = Y(2.2)
  val z: Z = Z(3.3)
  val p: Point = Point(x, z, z)

  println(p)
  println(p.x)
  println(p.y)
  println(p.z)
}


We'll see if this works out, but, hey, it's a place to start.

No comments:

Post a Comment