From dcb6c2159446135a142cad41eec185bb24c45bfb Mon Sep 17 00:00:00 2001 From: bjculkin Date: Wed, 11 Apr 2018 15:29:25 -0400 Subject: Split #9 into one class/file --- CSMath/src/bezier/transforms/TDHCombination.java | 64 +++++++++++ CSMath/src/bezier/transforms/TDHIdentity.java | 21 ++++ .../src/bezier/transforms/TDHLineReflection.java | 68 +++++++++++ CSMath/src/bezier/transforms/TDHMatrix.java | 40 +++++++ CSMath/src/bezier/transforms/TDHPointRotation.java | 67 +++++++++++ CSMath/src/bezier/transforms/TDHRotation.java | 57 ++++++++++ CSMath/src/bezier/transforms/TDHScale.java | 124 +++++++++++++++++++++ CSMath/src/bezier/transforms/TDHShear.java | 59 ++++++++++ CSMath/src/bezier/transforms/TDHTransform.java | 54 +++++++++ CSMath/src/bezier/transforms/TDHTransformType.java | 42 +++++++ CSMath/src/bezier/transforms/TDHTranslate.java | 89 +++++++++++++++ .../src/bezier/transforms/TDHXAxisReflection.java | 20 ++++ .../src/bezier/transforms/TDHYAxisReflection.java | 20 ++++ 13 files changed, 725 insertions(+) create mode 100644 CSMath/src/bezier/transforms/TDHCombination.java create mode 100644 CSMath/src/bezier/transforms/TDHIdentity.java create mode 100644 CSMath/src/bezier/transforms/TDHLineReflection.java create mode 100644 CSMath/src/bezier/transforms/TDHMatrix.java create mode 100644 CSMath/src/bezier/transforms/TDHPointRotation.java create mode 100644 CSMath/src/bezier/transforms/TDHRotation.java create mode 100644 CSMath/src/bezier/transforms/TDHScale.java create mode 100644 CSMath/src/bezier/transforms/TDHShear.java create mode 100644 CSMath/src/bezier/transforms/TDHTransform.java create mode 100644 CSMath/src/bezier/transforms/TDHTransformType.java create mode 100644 CSMath/src/bezier/transforms/TDHTranslate.java create mode 100644 CSMath/src/bezier/transforms/TDHXAxisReflection.java create mode 100644 CSMath/src/bezier/transforms/TDHYAxisReflection.java (limited to 'CSMath/src/bezier/transforms') diff --git a/CSMath/src/bezier/transforms/TDHCombination.java b/CSMath/src/bezier/transforms/TDHCombination.java new file mode 100644 index 0000000..4f9fe10 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHCombination.java @@ -0,0 +1,64 @@ +package bezier.transforms; + +import java.util.ArrayList; +import java.util.List; + +import bezier.TDHPoint; + +public class TDHCombination implements TDHTransform { + public final List forms; + + @Override + public TDHTransformType type() { + return TDHTransformType.COMBINATION; + } + + public TDHCombination(TDHTransform... forms) { + this.forms = new ArrayList<>(forms.length); + + for (TDHTransform form : forms) { + this.forms.add(form); + } + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + TDHPoint ret = punkt; + + for (TDHTransform form : forms) { + ret = form.transform(ret); + } + + return ret; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((forms == null) ? 0 : forms.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TDHCombination other = (TDHCombination) obj; + if (forms == null) { + if (other.forms != null) + return false; + } else if (!forms.equals(other.forms)) + return false; + return true; + } + + @Override + public String toString() { + return "TDHCombination [forms=" + forms + "]"; + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHIdentity.java b/CSMath/src/bezier/transforms/TDHIdentity.java new file mode 100644 index 0000000..c0ae7c2 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHIdentity.java @@ -0,0 +1,21 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +/** + * A transform that does nothing. + * + * @author bjculkin + * + */ +public class TDHIdentity implements TDHTransform { + @Override + public TDHPoint transform(TDHPoint punkt) { + return punkt; + } + + @Override + public String toString() { + return "TDHIdentity []"; + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHLineReflection.java b/CSMath/src/bezier/transforms/TDHLineReflection.java new file mode 100644 index 0000000..1cf4a48 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHLineReflection.java @@ -0,0 +1,68 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +public class TDHLineReflection implements TDHTransform { + public final double a; + public final double b; + public final double c; + + public TDHLineReflection(double a, double b, double c) { + this.a = a; + this.b = b; + this.c = c; + } + + @Override + public TDHTransformType type() { + return TDHTransformType.REFLECTION; + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + double com = (a * a - b * b); + + double x = (punkt.x * com) - (2 * a * b * punkt.y) - (2 * a * c * punkt.z); + double y = (-2 * a * b * punkt.x) + (punkt.y * com) - (2 * b * c * punkt.z); + double z = (punkt.z * com); + + return new TDHPoint(x, y, z); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(a); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(b); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(c); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TDHLineReflection other = (TDHLineReflection) obj; + if (Double.doubleToLongBits(a) != Double.doubleToLongBits(other.a)) + return false; + if (Double.doubleToLongBits(b) != Double.doubleToLongBits(other.b)) + return false; + if (Double.doubleToLongBits(c) != Double.doubleToLongBits(other.c)) + return false; + return true; + } + + @Override + public String toString() { + return "TDHLineReflection [a=" + a + ", b=" + b + ", c=" + c + "]"; + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHMatrix.java b/CSMath/src/bezier/transforms/TDHMatrix.java new file mode 100644 index 0000000..b4e5bb0 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHMatrix.java @@ -0,0 +1,40 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +public class TDHMatrix implements TDHTransform { + public final double[][] mat; + + public TDHMatrix(double[][] mat) { + super(); + this.mat = mat; + } + + @Override + public TDHTransformType type() { + return TDHTransformType.MATRIX; + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + double x = (punkt.x * mat[0][0]) + (punkt.y * mat[1][0]) + (punkt.z * mat[2][0]); + double y = (punkt.x * mat[0][1]) + (punkt.y * mat[1][1]) + (punkt.z * mat[2][1]); + double z = (punkt.x * mat[0][2]) + (punkt.y * mat[1][2]) + (punkt.z * mat[2][2]); + + return new TDHPoint(x, y, z); + } + + public TDHTransform then(double[][] matr) { + double[][] ret = new double[3][3]; + + for (int i = 0; i < 3; i++) { + for (int j = 0; j < 3; j++) { + for (int k = 0; k < 3; k++) { + ret[i][j] += mat[i][k] * matr[k][j]; + } + } + } + + return new TDHMatrix(ret); + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHPointRotation.java b/CSMath/src/bezier/transforms/TDHPointRotation.java new file mode 100644 index 0000000..30f1854 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHPointRotation.java @@ -0,0 +1,67 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +public class TDHPointRotation extends TDHRotation { + public final double x0; + public final double y0; + + public TDHPointRotation(double theta, double x0, double y0) { + super(theta); + + this.x0 = x0; + this.y0 = y0; + } + + @Override + public TDHTransformType type() { + return TDHTransformType.ROTATION; + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + double x1 = (punkt.x * Math.cos(theta)) - (punkt.y * Math.sin(theta)); + double y1 = (punkt.x * Math.sin(theta)) - (punkt.y * Math.cos(theta)); + + double x2 = (-x0 * Math.cos(theta)) + (y0 * Math.sin(theta)) + x0; + double y2 = (-x0 * Math.sin(theta)) - (y0 * Math.cos(theta)) + y0; + + double x = x1 + (punkt.z * x2); + double y = y1 + (punkt.z * y2); + + return new TDHPoint(x, y, punkt.z); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = super.hashCode(); + long temp; + temp = Double.doubleToLongBits(x0); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(y0); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (!super.equals(obj)) + return false; + if (getClass() != obj.getClass()) + return false; + TDHPointRotation other = (TDHPointRotation) obj; + if (Double.doubleToLongBits(x0) != Double.doubleToLongBits(other.x0)) + return false; + if (Double.doubleToLongBits(y0) != Double.doubleToLongBits(other.y0)) + return false; + return true; + } + + @Override + public String toString() { + return "TDHPointRotation [x0=" + x0 + ", y0=" + y0 + "]"; + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHRotation.java b/CSMath/src/bezier/transforms/TDHRotation.java new file mode 100644 index 0000000..b2255d4 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHRotation.java @@ -0,0 +1,57 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +public class TDHRotation implements TDHTransform { + public final double theta; + + public TDHRotation(double theta) { + this.theta = theta; + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + double x = (punkt.x * Math.cos(theta)) - (punkt.y * Math.sin(theta)); + double y = (punkt.x * Math.sin(theta)) - (punkt.y * Math.cos(theta)); + + return new TDHPoint(x, y, punkt.z); + } + + public double[][] matrix() { + return new double[][] { new double[] { Math.cos(theta), Math.sin(theta), 0 }, + new double[] { -Math.sin(theta), Math.cos(theta), 0 }, new double[] { 0, 0, 1 } }; + } + + public TDHTransform invert() { + return new TDHRotation(-theta); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(theta); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TDHRotation other = (TDHRotation) obj; + if (Double.doubleToLongBits(theta) != Double.doubleToLongBits(other.theta)) + return false; + return true; + } + + @Override + public String toString() { + return "TDHRotation [theta=" + theta + "]"; + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHScale.java b/CSMath/src/bezier/transforms/TDHScale.java new file mode 100644 index 0000000..09ab237 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHScale.java @@ -0,0 +1,124 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +/** + * Transform that does coordinate scaling. + * + * @author bjculkin + * + */ +public class TDHScale implements TDHTransform { + /** + * Amount to scale x by. + */ + public final double sx; + /** + * Amount to scale y by. + */ + public final double sy; + /** + * Amount to scale the homogeneous coordinate by. + */ + public final double sz; + + /** + * Create a new scaling that scales each coordinate by an equal amount + * + * @param factor + * The amount to scale the coordinates by. + */ + public TDHScale(double factor) { + this(factor, factor); + } + + /** + * Create a new scaling that scales each coordinate separately. + * + * @param sx + * The amount to scale x by. + * @param sy + * The amount to scale y by. + */ + public TDHScale(double sx, double sy) { + this(sx, sy, 1); + } + + /** + * Create a new scaling that scales each coordinate separately. + * + * Includes the homogeneous coordinate. + * + * @param sx + * The amount to scale x by. + * @param sy + * The amount to scale y by. + * @param sz + * The amount to scale the homogeneous coordinate by. + */ + public TDHScale(double sx, double sy, double sz) { + this.sx = sx; + this.sy = sy; + this.sz = sz; + } + + @Override + public TDHTransformType type() { + return TDHTransformType.SCALE; + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + double x = punkt.x * sx; + double y = punkt.y * sy; + double z = punkt.z * sz; + + return new TDHPoint(x, y, z); + } + + @Override + public double[][] matrix() { + return new double[][] { new double[] { sx, 0, 0 }, new double[] { 0, sy, 0 }, new double[] { 0, 0, sz } }; + } + + public TDHTransform invert() { + return new TDHScale(1 / sx, 1 / sy, 1 / sy); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(sx); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(sy); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(sz); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TDHScale other = (TDHScale) obj; + if (Double.doubleToLongBits(sx) != Double.doubleToLongBits(other.sx)) + return false; + if (Double.doubleToLongBits(sy) != Double.doubleToLongBits(other.sy)) + return false; + if (Double.doubleToLongBits(sz) != Double.doubleToLongBits(other.sz)) + return false; + return true; + } + + @Override + public String toString() { + return "TDHScale [sx=" + sx + ", sy=" + sy + ", sz=" + sz + "]"; + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHShear.java b/CSMath/src/bezier/transforms/TDHShear.java new file mode 100644 index 0000000..40b3814 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHShear.java @@ -0,0 +1,59 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +public class TDHShear implements TDHTransform { + public final double shx; + public final double shy; + + @Override + public TDHTransformType type() { + return TDHTransformType.SHEAR; + } + + public TDHShear(double shx, double shy) { + this.shx = shx; + this.shy = shy; + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + double x = punkt.x + (punkt.y * shx); + double y = punkt.y + (punkt.x * shy); + + return new TDHPoint(x, y, punkt.z); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(shx); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(shy); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TDHShear other = (TDHShear) obj; + if (Double.doubleToLongBits(shx) != Double.doubleToLongBits(other.shx)) + return false; + if (Double.doubleToLongBits(shy) != Double.doubleToLongBits(other.shy)) + return false; + return true; + } + + @Override + public String toString() { + return "TDHShear [shx=" + shx + ", shy=" + shy + "]"; + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHTransform.java b/CSMath/src/bezier/transforms/TDHTransform.java new file mode 100644 index 0000000..48f60a5 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHTransform.java @@ -0,0 +1,54 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +/** + * Transformation applicable to TDHPoints. + * + * @author bjculkin + * + */ +@FunctionalInterface +public interface TDHTransform { + /** + * Get the type of this transform. + * + * Unknown transformations are assumed to be identity transforms. + * + * @return The type of this transform. + */ + default TDHTransformType type() { + return TDHTransformType.IDENTITY; + } + + /** + * Get the matrix representation of the transform. + * + * Unknown transformations are assumed to be identity transforms. + * + * @return The matrix representation of the transform. + */ + default double[][] matrix() { + return new double[][] { new double[] { 1, 0, 0 }, new double[] { 0, 1, 0 }, new double[] { 0, 0, 1 } }; + } + + /** + * Get the inverse of the transform. + * + * Unknown transformations are assumed to be identity transforms. + * + * @return The inverse the transform. + */ + default TDHTransform invert() { + return new TDHIdentity(); + } + + /** + * Apply the transform to a point. + * + * @param punkt + * The point to transform. + * @return A transformed version of the point. + */ + TDHPoint transform(TDHPoint punkt); +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHTransformType.java b/CSMath/src/bezier/transforms/TDHTransformType.java new file mode 100644 index 0000000..e64c13d --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHTransformType.java @@ -0,0 +1,42 @@ +package bezier.transforms; + +/** + * Types of transform to apply to TDHPoints. + * + * @author bjculkin + * + */ +public enum TDHTransformType { + /** + * Coordinate translation. + */ + TRANSLATE, + /** + * Do nothing transform. + */ + IDENTITY, + /** + * Coordinate scaling. + */ + SCALE, + /** + * Coordinate rotation. + */ + ROTATION, + /** + * Coordinate reflection. + */ + REFLECTION, + /** + * Coordinate shearing. + */ + SHEAR, + /** + * Multiple transformations. + */ + COMBINATION, + /** + * Arbitrary matrix transformation. + */ + MATRIX +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHTranslate.java b/CSMath/src/bezier/transforms/TDHTranslate.java new file mode 100644 index 0000000..eb5cfa9 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHTranslate.java @@ -0,0 +1,89 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +/** + * A transform that does coordinate translation. + * + * @author bjculkin + * + */ +public class TDHTranslate implements TDHTransform { + /** + * The amount to translate the x-coordinate by. + */ + public final double h; + /** + * The amount to translate the y-coordinate by. + */ + public final double k; + + /** + * Create a new translation transform. + * + * @param h + * The amount to translate x by. + * @param k + * The amount to translate y by. + */ + public TDHTranslate(double h, double k) { + this.h = h; + this.k = k; + } + + @Override + public TDHTransformType type() { + return TDHTransformType.TRANSLATE; + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + double x = punkt.x + (punkt.z * h); + double y = punkt.y + (punkt.z * k); + + return new TDHPoint(x, y, punkt.z); + } + + @Override + public double[][] matrix() { + return new double[][] { new double[] { 1, 0, 0 }, new double[] { 0, 1, 0 }, new double[] { h, k, 1 } }; + } + + @Override + public TDHTransform invert() { + return new TDHTranslate(-h, -k); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + long temp; + temp = Double.doubleToLongBits(h); + result = prime * result + (int) (temp ^ (temp >>> 32)); + temp = Double.doubleToLongBits(k); + result = prime * result + (int) (temp ^ (temp >>> 32)); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + TDHTranslate other = (TDHTranslate) obj; + if (Double.doubleToLongBits(h) != Double.doubleToLongBits(other.h)) + return false; + if (Double.doubleToLongBits(k) != Double.doubleToLongBits(other.k)) + return false; + return true; + } + + @Override + public String toString() { + return "TDHTranslate [h=" + h + ", k=" + k + "]"; + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHXAxisReflection.java b/CSMath/src/bezier/transforms/TDHXAxisReflection.java new file mode 100644 index 0000000..5eea8cd --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHXAxisReflection.java @@ -0,0 +1,20 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +public class TDHXAxisReflection implements TDHTransform { + @Override + public TDHTransformType type() { + return TDHTransformType.REFLECTION; + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + return new TDHPoint(punkt.x, -punkt.y, punkt.z); + } + + @Override + public String toString() { + return "TDHXAxisReflection []"; + } +} \ No newline at end of file diff --git a/CSMath/src/bezier/transforms/TDHYAxisReflection.java b/CSMath/src/bezier/transforms/TDHYAxisReflection.java new file mode 100644 index 0000000..f302fe4 --- /dev/null +++ b/CSMath/src/bezier/transforms/TDHYAxisReflection.java @@ -0,0 +1,20 @@ +package bezier.transforms; + +import bezier.TDHPoint; + +public class TDHYAxisReflection implements TDHTransform { + @Override + public TDHTransformType type() { + return TDHTransformType.REFLECTION; + } + + @Override + public TDHPoint transform(TDHPoint punkt) { + return new TDHPoint(-punkt.x, punkt.y, punkt.z); + } + + @Override + public String toString() { + return "TDHYAxisReflection []"; + } +} \ No newline at end of file -- cgit v1.2.3