labeling testing

This commit is contained in:
Hannes Janetzek
2013-02-18 09:48:32 +01:00
parent d25d967332
commit 34bffcc15f
16 changed files with 628 additions and 348 deletions

View File

@@ -329,5 +329,37 @@ public final class GeometryUtils {
return inside;
}
public static float areaSigned(Point p1, Point p2, Point p3) {
return ((p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y))*0.5f;
}
public static float areaSigned(float ax, float ay, float bx, float by, float cx, float cy) {
return ((ax - cx) * (by - cy) - (bx - cx) * (ay - cy)) * 0.5f;
}
public static float area(float ax, float ay, float bx, float by, float cx, float cy) {
float area = ((ax - cx) * (by - cy) - (bx - cx) * (ay - cy)) * 0.5f;
return area < 0 ? -area : area;
}
public static boolean pointInTri(Point pt, Point p1, Point p2, Point p3) {
boolean inside = false;
boolean p1s = p1.y > pt.y;
boolean p2s = p2.y > pt.y;
boolean p3s = p3.y > pt.y;
if ((p1s != p3s)
&& (pt.x < (p3.x - p1.x) * (pt.y - p1.y) / (p3.y - p1.y) + p1.x))
inside = !inside;
if ((p2s != p1s)
&& (pt.x < (p1.x - p2.x) * (pt.y - p2.y) / (p1.y - p2.y) + p2.x))
inside = !inside;
if ((p3s != p2s)
&& (pt.x < (p2.x - p3.x) * (pt.y - p3.y) / (p2.y - p3.y) + p3.x))
inside = !inside;
return inside;
}
}

View File

@@ -15,9 +15,10 @@
package org.oscim.utils;
/**
* @author Hannes Janetzek
* taken from http://en.wikipedia.org/wiki/Cohen%E2%80%93
* Sutherland_algorithm
* from http://en.wikipedia.org/wiki/Cohen%E2%80%93
* Sutherland_algorithm
*
* @adapted by Hannes Janetzek
*/
public class LineClipper {
@@ -28,19 +29,35 @@ public class LineClipper {
private static final int BOTTOM = 4; // 0100
private static final int TOP = 8; // 1000
private int xmin, xmax, ymin, ymax;
private final int xmin, xmax, ymin, ymax;
public final int[] out;
public LineClipper(int minx, int miny, int maxx, int maxy) {
this.xmin = minx;
this.ymin = miny;
this.xmax = maxx;
this.ymax = maxy;
this.out = null;
}
public LineClipper(int minx, int miny, int maxx, int maxy, boolean keepResult) {
this.xmin = minx;
this.ymin = miny;
this.xmax = maxx;
this.ymax = maxy;
if (keepResult)
this.out = new int[4];
else
this.out = null;
}
private int mPrevOutcode;
private int mPrevX;
private int mPrevY;
public int outX;
public int outY;
public void clipStart(int x0, int y0) {
mPrevX = x0;
mPrevY = y0;
@@ -58,8 +75,13 @@ public class LineClipper {
mPrevOutcode = outcode;
}
public boolean clipNext(int x1, int y1) {
boolean accept;
/**
* @param x1 ...
* @param y1 ...
* @return 0 if not intersection, 1 fully within, -1 clipped (and 'out' set to new points)
*/
public int clipNext(int x1, int y1) {
int accept;
int outcode = INSIDE;
if (x1 < xmin)
@@ -73,12 +95,13 @@ public class LineClipper {
if ((mPrevOutcode | outcode) == 0) {
// Bitwise OR is 0. Trivially accept
accept = true;
accept = 1;
} else if ((mPrevOutcode & outcode) != 0) {
// Bitwise AND is not 0. Trivially reject
accept = false;
accept = 0;
} else {
accept = clip(mPrevX, mPrevY, x1, y1, xmin, ymin, xmax, ymax, mPrevOutcode, outcode);
accept = clip(mPrevX, mPrevY, x1, y1, xmin, ymin, xmax, ymax, mPrevOutcode, outcode,
this.out) ? -1 : 0;
}
mPrevOutcode = outcode;
mPrevX = x1;
@@ -91,7 +114,7 @@ public class LineClipper {
// P0 = (x0, y0) to P1 = (x1, y1) against a rectangle with
// diagonal from (xmin, ymin) to (xmax, ymax).
private static boolean clip(int x0, int y0, int x1, int y1,
int xmin, int ymin, int xmax, int ymax, int outcode0, int outcode1) {
int xmin, int ymin, int xmax, int ymax, int outcode0, int outcode1, int[] out) {
boolean accept = false;
@@ -154,9 +177,12 @@ public class LineClipper {
}
}
}
// TODO could do sth with the result x0...
if (accept && out != null) {
out[0] = x0;
out[1] = y0;
out[2] = x1;
out[3] = y1;
}
return accept;
}
}

View File

@@ -158,6 +158,37 @@ public class OBB2D {
computeAxes();
}
public void set(float cx, float cy, float dx, float dy, float width, float height){
float vx = cx - dx;
float vy = cy - dy;
float a = (float) Math.sqrt(vx * vx + vy * vy);
vx /= a;
vy /= a;
float hw = width / 2;
float hh = height / 2;
float ux = vy * hh;
float uy = -vx * hh;
vx *= hw;
vy *= hw;
corner[0] = cx - vx - ux;
corner[1] = cy - vy - uy;
corner[2] = cx + vx - ux;
corner[3] = cy + vy - uy;
corner[4] = cx + vx + ux;
corner[5] = cy + vy + uy;
corner[6] = cx - vx + ux;
corner[7] = cy - vy + uy;
computeAxes();
}
public OBB2D(float cx, float cy, float dx, float dy, float width, float height) {
float vx = cx - dx;