Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
sagnik committed Aug 20, 2016
1 parent aac03dd commit 156a6d7
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 210 deletions.
4 changes: 3 additions & 1 deletion rectangle/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ addCompilerPlugin(scalaMacros)
lazy val nitroOss = "com.gonitro"
lazy val avroCodegen = nitroOss %% "avro-codegen-runtime" % "0.3.4"
lazy val shapeless = "com.chuusai" %% "shapeless" % "2.2.5"
lazy val scalaTest = "org.scalatest" %% "scalatest" % "2.2.4"

libraryDependencies ++= Seq(
shapeless,
avroCodegen
avroCodegen,
scalaTest
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,70 +3,38 @@ package edu.psu.sagnik.research.pdsimplify.rectanglehelpers
import edu.psu.sagnik.research.data.RectangleOTL

/**
* Created by schoudhury on 8/18/16.
*/
* Created by schoudhury on 8/18/16.
*/
object RectangleHelpers {

def round(x:Float)=scala.math.round(x)
def round(x: Float) = scala.math.round(x)

/**
* Evaluates to a string that contains the Rectangle's
* point information as x1,y2,x2,y2 .
*/
* Evaluates to a string that contains the Rectangle's
* point information as x1,y2,x2,y2 .
*/
def asCoordinatesStr(r: RectangleOTL): String =
s"[topLeftX]:${r.xTopLeft},[topLeftY]:${r.yTopLeft},[width]:${r.widthRight},[height]:${r.heightDown}"


//a bit of caution: this works only for axes parallel rectangles.
// That suffice for our purpose, but this isn't a generic method.
def rectDistance(r1:RectangleOTL,r2:RectangleOTL):Float={
val dy1=if (r1.y2<r2.y1) r2.y1-r1.y2 else 0
val dy2=if (r1.y1>r2.y2) r1.y1-r2.y2 else 0
val dx1=if (r1.x2<r2.x1) r2.x1-r1.x2 else 0
val dx2=if (r1.x1>r2.x2) r1.x1-r2.x2 else 0
dx1+dx2+dy1+dy2
}

def rectExtend(r:Rectangle,rs:Seq[Rectangle],checkAgainstRS:Seq[Rectangle],pageBB:Rectangle,direction:String):Option[Rectangle]=
if (
!rs.exists(a=>rectInterSects(a,r)) && //haven't found any intersection with body or caption text boxes
(r.x1>pageBB.x1 && r.x2<pageBB.x2 && r.y1>pageBB.y1 && r.y2 < pageBB.y2) // all coordinates OK with pageBB
)
rectExtend(changeRect(r,1f,direction),rs,checkAgainstRS,pageBB,direction)
else {
if (checkAgainstRS.filter(a=>rectInterSects(Rectangle(r.x1+2,r.y1+2,r.x2-2,r.y2-2),a)).length>2 //we have got an extended rectangle.
// Need to see if this is empty, or has a very small size
&& (r.x2-r.x1>10f && r.y2-r.y1> 10f)
)
Some(changeRect(r,-1f,direction))
else
None
}

def changeRect(r:Rectangle,changeparam:Float,direction:String):Rectangle=
direction match {
case "left" => r.copy(x1 = r.x1 - changeparam)
case "right" => r.copy(x2 = r.x2 + changeparam)
case "top" => r.copy(y2 = r.y2 + changeparam)
case "down" => r.copy(y1 = r.y1 - changeparam)
case _ => r
}


/*
TODO: test these methods
* */
def rectVerticalDistance(r1:Rectangle,r2:Rectangle):Float={
val dy1=if (r1.y2<r2.y1) r2.y1-r1.y2 else 0
val dy2=if (r1.y1>r2.y2) r1.y1-r2.y2 else 0
dy1+dy2
}

def rectHorizontalDistance(r1:Rectangle,r2:Rectangle):Float={
val dx1=if (r1.x2<r2.x1) r2.x1-r1.x2 else 0
val dx2=if (r1.x1>r2.x2) r1.x1-r2.x2 else 0
dx1+dx2
def rectDistance(r1: RectangleOTL, r2: RectangleOTL): Float = {
val r1x1 = r1.xTopLeft
val r1y1 = r1.yTopLeft
val r1x2 = r1.xTopLeft + r1.widthRight
val r1y2 = r1.yTopLeft + r1.heightDown
val r2x1 = r2.xTopLeft
val r2y1 = r2.yTopLeft
val r2x2 = r2.xTopLeft + r2.widthRight
val r2y2 = r2.yTopLeft + r2.heightDown

val dy1 = if (r1y2 < r2y1) r2y1 - r1y2 else 0
val dy2 = if (r1y1 > r2y2) r1y1 - r2y2 else 0
val dx1 = if (r1x2 < r2x1) r2x1 - r1x2 else 0
val dx2 = if (r1x1 > r2x2) r1x1 - r2x2 else 0
dx1 + dx2 + dy1 + dy2

dx1 + dx2 + dy1 + dy2
}

}
*/

153 changes: 0 additions & 153 deletions rectangle/src/test/scala/RectangleHelperTest.scala

This file was deleted.

113 changes: 113 additions & 0 deletions rectangle/src/test/scala/TestRectangleHelper.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package edu.psu.sagnik.research.pdsimplify.rectanglehelpers

import edu.psu.sagnik.research.data.RectangleOTL
import org.scalatest.FunSuite
/**
* Created by schoudhury on 8/18/16.
*/

class TestRectangleHelper extends FunSuite {

val centralRect = RectangleOTL(
xTopLeft = 50, yTopLeft = 50,
widthRight = 50, heightDown = 50
)

import edu.psu.sagnik.research.pdsimplify.rectanglehelpers.RectangleHelpers._
test("rectangle distance, non spanning, non intersecting, top left") {
val r = RectangleOTL(
xTopLeft = 0, yTopLeft = 0,
widthRight = 10, heightDown = 10
)
assert(rectDistance(centralRect, r) == 80f)
assert(rectDistance(r, centralRect) == 80f)
}

test("rectangle distance, non spanning, non intersecting, top") {

val r = RectangleOTL(
xTopLeft = 50, yTopLeft = 0,
widthRight = 50, heightDown = 10
)

assert(rectDistance(centralRect, r) == 40f)
assert(rectDistance(r, centralRect) == 40f)
}

test("rectangle distance, non spanning, non intersecting, top right") {

val r = RectangleOTL(
xTopLeft = 140, yTopLeft = 0,
widthRight = 10, heightDown = 10
)

assert(rectDistance(centralRect, r) == 80f)
assert(rectDistance(r, centralRect) == 80f)
}

test("rectangle distance, non spanning, non intersecting, right") {

val r = RectangleOTL(
xTopLeft = 140, yTopLeft = 50,
widthRight = 10, heightDown = 50
)

assert(rectDistance(centralRect, r) == 40f)
assert(rectDistance(r, centralRect) == 40f)
}

test("rectangle distance, non spanning, non intersecting, right below") {

val r = RectangleOTL(
xTopLeft = 140, yTopLeft = 140,
widthRight = 10, heightDown = 10
)
assert(rectDistance(centralRect, r) == 80f)
assert(rectDistance(r, centralRect) == 80f)
}

test("rectangle distance, non spanning, non intersecting, below") {

val r = RectangleOTL(
xTopLeft = 50, yTopLeft = 140,
widthRight = 10, heightDown = 10
)

assert(rectDistance(centralRect, r) == 40f)
assert(rectDistance(r, centralRect) == 40f)
}

test("rectangle distance, non spanning, non intersecting, below left") {

val r = RectangleOTL(
xTopLeft = 0, yTopLeft = 140,
widthRight = 10, heightDown = 10
)

assert(rectDistance(centralRect, r) == 80f)
assert(rectDistance(r, centralRect) == 80f)
}

test("rectangle distance, non spanning, non intersecting, left") {

val r = RectangleOTL(
xTopLeft = 0, yTopLeft = 50,
widthRight = 10, heightDown = 50
)

assert(rectDistance(centralRect, r) == 40f)
assert(rectDistance(r, centralRect) == 40f)
}

test("rectangle distance, spanning, non intersecting, below") {

val r = RectangleOTL(
xTopLeft = 0, yTopLeft = 140,
widthRight = 150, heightDown = 10
)

assert(rectDistance(centralRect, r) == 40f)
assert(rectDistance(r, centralRect) == 40f)
}

}

0 comments on commit 156a6d7

Please sign in to comment.