X-Git-Url: http://git.megacz.com/?p=org.ibex.core.git;a=blobdiff_plain;f=src%2Forg%2Fibex%2Fgraphics%2FAffine.java;h=e5d86b6515d65207a3198511780444b7007aad78;hp=31e2e2b25dada1a0ccb1d20537251292b10b617f;hb=faa4ae356d464a80e08540d5eb8bbee6296c1dbc;hpb=8e190fb0ff508ccf4962bbfbf8295a431805c12b diff --git a/src/org/ibex/graphics/Affine.java b/src/org/ibex/graphics/Affine.java index 31e2e2b..e5d86b6 100644 --- a/src/org/ibex/graphics/Affine.java +++ b/src/org/ibex/graphics/Affine.java @@ -1,19 +1,24 @@ +// Copyright 2000-2005 the Contributors, as shown in the revision logs. +// Licensed under the GNU General Public License version 2 ("the License"). +// You may not use this file except in compliance with the License. + // FIXME -// Copyright 2004 Adam Megacz, see the COPYING file for licensing [GPL] package org.ibex.graphics; import java.util.*; +import org.ibex.util.*; /** an affine transform; all operations are destructive */ public final class Affine { - // [ a b e ] - // [ c d f ] + // [ a c e ] + // [ b d f ] // [ 0 0 1 ] public float a, b, c, d, e, f; Affine(float _a, float _b, float _c, float _d, float _e, float _f) { a = _a; b = _b; c = _c; d = _d; e = _e; f = _f; } public String toString() { return "[ " + a + ", " + b + ", " + c + ", " + d + ", " + e + ", " + f + " ]"; } public Affine copy() { return new Affine(a, b, c, d, e, f); } + public boolean doesNotRotate() { return a==0 && b==0 && c==0 && d==0; } public static Affine identity() { return new Affine(1, 0, 0, 1, 0, 0); } public static Affine scale(float sx, float sy) { return new Affine(sx, 0, 0, sy, 0, 0); } public static Affine shear(float degrees) { @@ -22,7 +27,33 @@ public final class Affine { public static Affine flip(boolean horiz, boolean vert) { return new Affine(horiz ? -1 : 1, 0, 0, vert ? -1 : 1, 0, 0); } public float multiply_px(float x, float y) { return x * a + y * c + e; } public float multiply_py(float x, float y) { return x * b + y * d + f; } + public float sign(float x) { return x >= 0 ? 1 : -1; } + public float divide_boundingbox_x(float bx, float by, float aspect) { + return (float)Math.min(Math.abs(bx * (sign(a) * sign(c)) / (aspect * a + c)), + Math.abs(by * (sign(b) * sign(d)) / (aspect * b + d))); + } + public float multiply_boundingbox_x(float x, float y) { + return (float)Math.max((int)Math.abs(multiply_px(x, y) - multiply_px(0, 0)), + (int)Math.abs(multiply_px(x, 0) - multiply_px(0, y))); } + public float multiply_boundingbox_y(float x, float y) { + return (float)Math.max((int)Math.abs(multiply_py(x, y) - multiply_py(0, 0)), + (int)Math.abs(multiply_py(x, 0) - multiply_py(0, y))); } public boolean equalsIgnoringTranslation(Affine x) { return a == x.a && b == x.b && c == x.c && d == x.d; } + public Affine clearTranslation() { e = (float)0.0; f = (float)0.0; return this; } + + public long rotateBox(long box) { return rotateBox(Encode.longToFloat1(box), Encode.longToFloat2(box)); } + public long rotateBox(float x, float y) { + float width = + max(max(multiply_px(0, 0), multiply_px(x, y)), max(multiply_px(x, 0), multiply_px(0, y))) - + min(min(multiply_px(0, 0), multiply_px(x, y)), min(multiply_px(x, 0), multiply_px(0, y))); + float height = + max(max(multiply_py(0, 0), multiply_py(x, y)), max(multiply_py(x, 0), multiply_py(0, y))) - + min(min(multiply_py(0, 0), multiply_py(x, y)), min(multiply_py(x, 0), multiply_py(0, y))); + return Encode.twoFloatsToLong(width, height); + } + + static float min(float a, float b) { if (ab) return a; else return b; } public boolean equals(Object o) { if (!(o instanceof Affine)) return false; @@ -36,8 +67,8 @@ public final class Affine { return new Affine(c, s, -s, c, 0, 0); } - /** this = this * a */ - public Affine multiply(Affine A) { + /** this = a * this */ + public Affine premultiply(Affine A) { float _a = this.a * A.a + this.b * A.c; float _b = this.a * A.b + this.b * A.d; float _c = this.c * A.a + this.d * A.c; @@ -48,15 +79,29 @@ public final class Affine { return this; } - public void invert() { - float det = 1 / (a * d - b * c); - float _a = d * det; - float _b = -1 * b * det; - float _c = -1 * c * det; - float _d = a * det; - float _e = -1 * e * a - f * c; - float _f = -1 * e * b - f * d; + /** this = this * a */ + public Affine multiply(Affine A) { + float _a = A.a * this.a + A.b * this.c; + float _b = A.a * this.b + A.b * this.d; + float _c = A.c * this.a + A.d * this.c; + float _d = A.c * this.b + A.d * this.d; + float _e = A.e * this.a + A.f * this.c + this.e; + float _f = A.e * this.b + A.f * this.d + this.f; a = _a; b = _b; c = _c; d = _d; e = _e; f = _f; + return this; + } + + public Affine inverse() { return copy().invert(); } + public Affine invert() { + float det = (a * d - b * c); + float _a = d / det; + float _b = -1 * b / det; + float _c = -1 * c / det; + float _d = a / det; + float _e = (f*c-e*d)/det; + float _f = (b*e-a*f)/det; + a = _a; b = _b; c = _c; d = _d; e = _e; f = _f; + return this; } public static Affine parse(String t) {