all: fix broken docunits
[nit.git] / lib / geometry / boxes.nit
index 61af367..b8c9798 100644 (file)
@@ -74,6 +74,15 @@ interface Boxed[N: Numeric]
                return self.left <= other.right and other.left <= self.right and
                        self.top >= other.bottom and other.top >= self.bottom
        end
+
+       # Create a bounding box that encloses the actual bounding box.
+       # `dist` is the distance between the inner boundaries and the outer boundaries.
+       # ~~~
+       # var p = new Point[Int](5,10)
+       # var b = p.padded(3)
+       # assert b.left == 2 and b.right == 8 and b.top == 13 and b.bottom == 7
+       # ~~~
+       fun padded(dist: N): Box[N] do return new Box[N].lrtb(left - dist, right + dist, top + dist, bottom - dist)
 end
 
 # A 2d bounded object and an implementation of `Boxed`
@@ -201,6 +210,8 @@ interface Boxed3d[N: Numeric]
                return super and (not other isa Boxed3d[N] or
                        (self.back <= other.front and other.back <= self.front))
        end
+
+       redef fun padded(dist: N): Box3d[N] do return new Box3d[N].lrtbfb(left - dist, right + dist, top + dist, bottom - dist, front + dist, back - dist)
 end
 
 # A 3d bounded object and an implementation of Boxed
@@ -317,3 +328,31 @@ redef class ILine3d[N]
        redef fun front do return point_left.z.min(point_right.z)
        redef fun back do return point_left.z.max(point_right.z)
 end
+
+# Base for all data structures containing multiple Boxed Objects
+interface BoxedCollection[E: Boxed[Numeric]]
+       super SimpleCollection[E]
+
+       # returns all the items overlapping with `region`
+       fun items_overlapping(region :Boxed[Numeric]): SimpleCollection[E] is abstract
+end
+
+# A BoxedCollection implemented with an array, linear performances for searching but really
+# fast for creation and filling
+class BoxedArray[E: Boxed[Numeric]]
+       super BoxedCollection[E]
+
+       private var data: Array[E] = new Array[E]
+
+       redef fun add(item: E) do data.add(item)
+       redef fun items_overlapping(item: Boxed[Numeric]): SimpleCollection[E]
+       do
+               var arr = new Array[E]
+               for i in data do
+                       if i.intersects(item) then arr.add(i)
+               end
+               return arr
+       end
+
+       redef fun iterator do return data.iterator
+end