Geometric Vector collections

29 views
Skip to first unread message

Rich Oliver

unread,
Dec 20, 2016, 11:07:46 AM12/20/16
to scala-language
A substantial amount of my code base uses 2 dimensional vectors. Pretty standard stuff, that hopefully will one day be included in the standard library:

final class Vec2 (val x: Double, val y: Double) extends PersistCompound
{
   override def equals(other: Any): Boolean = other match
   {
      case Vec2(px, py) => (x =\ px) && (y =\ py)
      case _ => false
   }
   def +(other: Vec2): Vec2 = Vec2(x + other.x, y + other.y)
...........
}


I'm currently using an implicit class to add methods:

object Vec2
{
   /** Translates each Vec2 member of collection equivalent to trav.map(_ + offset) */
      def +++ (offset: Vec2)(implicit bf: generic.CanBuildFrom[Repr, Vec2, Repr]): Repr =
      {
         def builder =
         { // extracted to keep method size under 35 bytes, so that it can be JIT-inlined
            val b = bf(travLike.repr)
            b.sizeHint(travLike)
            b
         }
         val b = builder
         for (x <- travLike) b += x + offset
         b.result
      }

   /** Creates a bounding rectangle for a collection of 2d points */
      def boundingRect: BoundingRect =
      {
         var minX, maxX, minY, maxY = 0.0
         var i = 0
         for (v <- travLike)
         {
            if (i == 0)
            {
               minX = v.x
               maxX = v.x
               minY = v.y
               maxY = v.y
            }
            else
            {
               minX = minX.min(v.x)
               maxX = maxX.max(v.x)
               minY = minY.min(v.y)
               maxY = maxY.max(v.y)
            }
            i += 1
         }
         if (i == 0) throw new Exception("boundingRect method called on empty Vec2 collection") else {}
         BoundingRect(minX, maxX, minY, maxY)              
      }  
   ....................
   }
 }


1 Pretty horrible code. So my first question is: are those the correct ways to write those methods in 2.12 using the implicits if I want them to be efficient?

2 Should there be an inline annotation on the + method of the Vec2 class?

3 However I'm now wondering if I should use a custom collection class and then just create the methods directly on the class and always use the custom type where I currently have Seq[Vec2]. The two obvious implementation strategies are a wrapped Array or a custom List class with custom Cons and Nil classes. I'm leaning towards the latter as most of my Vec2 collections are small and it makes, building and iterating with immutability straight forward.

4  The 2d, 3d and 4d Vectors strike me as very important classes. The 3d Vector is given as the canonical example of a struct in Scala Native. So I wondered how the thinking for the new collections library in 2.13 will effect these classes?

5 Will we be able to use Scala Native Vector Structs while still maintaining a largely common code base for ScalaJvm and ScalaJs? This seems to me completely entangled with the question of collections.
Reply all
Reply to author
Forward
0 new messages