Added Transform.transformPoint(Vector), enabled Ray2 transforms.

I've tried to make a useful distinction between Point and Vector, but sometimes
you just want to use a Vector as a Point. I'm not going to add Point3 and try
to push this distinction into the third dimension, so I'll accommodate using
Vector as Point in 2D, and we'll just use Vector3 as a 3D point when needed.
This commit is contained in:
Michael Bayne
2012-04-30 11:26:18 -07:00
parent f8cce64528
commit c8e4cbeca0
11 changed files with 88 additions and 39 deletions
@@ -309,6 +309,12 @@ public class AffineTransform extends AbstractTransform
(y * m00 - x * m01) * rdet); (y * m00 - x * m01) * rdet);
} }
@Override // from Transform
public Vector transformPoint (IVector v, Vector into) {
float x = v.x(), y = v.y();
return into.set(m00*x + m10*y + tx, m01*x + m11*y + ty);
}
@Override // from Transform @Override // from Transform
public Vector transform (IVector v, Vector into) { public Vector transform (IVector v, Vector into) {
float x = v.x(), y = v.y(); float x = v.x(), y = v.y();
+2 -2
View File
@@ -32,7 +32,7 @@ public interface IPlane
// * // *
// * @return a new plane containing the result. // * @return a new plane containing the result.
// */ // */
// Plane transform (Transform transform); // Plane transform (Transform3D transform);
// /** // /**
// * Transforms this plane by the specified transformation, placing the result in the object // * Transforms this plane by the specified transformation, placing the result in the object
@@ -40,7 +40,7 @@ public interface IPlane
// * // *
// * @return a reference to the result plane, for chaining. // * @return a reference to the result plane, for chaining.
// */ // */
// Plane transform (Transform transform, Plane result); // Plane transform (Transform3D transform, Plane result);
/** /**
* Negates this plane. * Negates this plane.
+12 -12
View File
@@ -19,19 +19,19 @@ public interface IRay2
*/ */
IVector direction (); IVector direction ();
// /** /**
// * Transforms this ray. * Transforms this ray.
// * *
// * @return a new ray containing the result. * @return a new ray containing the result.
// */ */
// Ray2 transform (Transform2D transform); Ray2 transform (Transform transform);
// /** /**
// * Transforms this ray, placing the result in the object provided. * Transforms this ray, placing the result in the object provided.
// * *
// * @return a reference to the result ray, for chaining. * @return a reference to the result ray, for chaining.
// */ */
// Ray2 transform (Transform2D transform, Ray2 result); Ray2 transform (Transform transform, Ray2 result);
/** /**
* Determines whether the ray intersects the specified point. * Determines whether the ray intersects the specified point.
@@ -86,6 +86,11 @@ public class IdentityTransform extends AbstractTransform
return into.set(p); return into.set(p);
} }
@Override // from Transform
public Vector transformPoint (IVector v, Vector into) {
return into.set(v);
}
@Override // from Transform @Override // from Transform
public Vector transform (IVector v, Vector into) { public Vector transform (IVector v, Vector into) {
return into.set(v); return into.set(v);
@@ -231,6 +231,11 @@ public class NonUniformTransform extends AbstractTransform
return Points.inverseTransform(p.x(), p.y(), scaleX, scaleY, rotation, tx, ty, into); return Points.inverseTransform(p.x(), p.y(), scaleX, scaleY, rotation, tx, ty, into);
} }
@Override // from Transform
public Vector transformPoint (IVector v, Vector into) {
return Vectors.transform(v.x(), v.y(), scaleX, scaleY, rotation, tx, ty, into);
}
@Override // from Transform @Override // from Transform
public Vector transform (IVector v, Vector into) { public Vector transform (IVector v, Vector into) {
return Vectors.transform(v.x(), v.y(), scaleX, scaleY, rotation, into); return Vectors.transform(v.x(), v.y(), scaleX, scaleY, rotation, into);
+3 -3
View File
@@ -129,7 +129,7 @@ public class Plane implements IPlane, Serializable
// * // *
// * @return a reference to this plane, for chaining. // * @return a reference to this plane, for chaining.
// */ // */
// public Plane transformLocal (Transform transform) { // public Plane transformLocal (Transform3D transform) {
// return transform(transform, this); // return transform(transform, this);
// } // }
@@ -163,12 +163,12 @@ public class Plane implements IPlane, Serializable
} }
// @Override // from IPlane // @Override // from IPlane
// public Plane transform (Transform transform) { // public Plane transform (Transform3D transform) {
// return transform(transform, new Plane()); // return transform(transform, new Plane());
// } // }
// @Override // from IPlane // @Override // from IPlane
// public Plane transform (Transform transform, Plane result) { // public Plane transform (Transform3D transform, Plane result) {
// transform.transformPointLocal(_normal.mult(-constant, _v1)); // transform.transformPointLocal(_normal.mult(-constant, _v1));
// transform.transformVector(_normal, _v2).normalizeLocal(); // transform.transformVector(_normal, _v2).normalizeLocal();
// return result.fromPointNormal(_v1, _v2); // return result.fromPointNormal(_v1, _v2);
+18 -18
View File
@@ -56,14 +56,14 @@ public class Ray2 implements IRay2
return this; return this;
} }
// /** /**
// * Transforms this ray in-place. * Transforms this ray in-place.
// * *
// * @return a reference to this ray, for chaining. * @return a reference to this ray, for chaining.
// */ */
// public Ray2 transformLocal (Transform2D transform) { public Ray2 transformLocal (Transform transform) {
// return transform(transform, this); return transform(transform, this);
// } }
@Override // from IRay2 @Override // from IRay2
public IVector origin () { public IVector origin () {
@@ -75,17 +75,17 @@ public class Ray2 implements IRay2
return direction; return direction;
} }
// @Override // from IRay2 @Override // from IRay2
// public Ray2 transform (Transform2D transform) { public Ray2 transform (Transform transform) {
// return transform(transform, new Ray2()); return transform(transform, new Ray2());
// } }
// @Override // from IRay2 @Override // from IRay2
// public Ray2 transform (Transform2D transform, Ray2 result) { public Ray2 transform (Transform transform, Ray2 result) {
// transform.transformPoint(origin, result.origin); transform.transformPoint(origin, result.origin);
// transform.transformVector(direction, result.direction).normalizeLocal(); transform.transform(direction, result.direction).normalizeLocal();
// return result; return result;
// } }
@Override // from IRay2 @Override // from IRay2
public boolean intersects (IVector pt) { public boolean intersects (IVector pt) {
@@ -169,6 +169,11 @@ public class RigidTransform extends AbstractTransform
return Points.inverseTransform(p.x(), p.y(), 1, 1, rotation, tx, ty, into); return Points.inverseTransform(p.x(), p.y(), 1, 1, rotation, tx, ty, into);
} }
@Override // from Transform
public Vector transformPoint (IVector v, Vector into) {
return Vectors.transform(v.x(), v.y(), 1, 1, rotation, tx, ty, into);
}
@Override // from Transform @Override // from Transform
public Vector transform (IVector v, Vector into) { public Vector transform (IVector v, Vector into) {
return v.rotate(rotation, into); return v.rotate(rotation, into);
@@ -176,6 +176,12 @@ public interface Transform
* @throws NoninvertibleTransformException if the transform is not invertible. */ * @throws NoninvertibleTransformException if the transform is not invertible. */
Point inverseTransform (IPoint p, Point into); Point inverseTransform (IPoint p, Point into);
/** Transforms the supplied vector as a point (accounting for translation), writing the result
* into {@code into}.
* @param into a vector into which to store the result, may be the same object as {@code v}.
* @return {@code into}, for chaining. */
Vector transformPoint (IVector v, Vector into);
/** Transforms the supplied vector, writing the result into {@code into}. /** Transforms the supplied vector, writing the result into {@code into}.
* @param into a vector into which to store the result, may be the same object as {@code v}. * @param into a vector into which to store the result, may be the same object as {@code v}.
* @return {@code into}, for chaining. */ * @return {@code into}, for chaining. */
@@ -197,6 +197,11 @@ public class UniformTransform extends AbstractTransform
return Points.inverseTransform(p.x(), p.y(), scale, scale, rotation, tx, ty, into); return Points.inverseTransform(p.x(), p.y(), scale, scale, rotation, tx, ty, into);
} }
@Override // from Transform
public Vector transformPoint (IVector p, Vector into) {
return Vectors.transform(p.x(), p.y(), scale, scale, rotation, tx, ty, into);
}
@Override // from Transform @Override // from Transform
public Vector transform (IVector v, Vector into) { public Vector transform (IVector v, Vector into) {
return Vectors.transform(v.x(), v.y(), scale, scale, rotation, into); return Vectors.transform(v.x(), v.y(), scale, scale, rotation, into);
+20 -3
View File
@@ -85,12 +85,21 @@ public class Vectors
return Math.abs(v1.x() - v2.x()) <= epsilon && Math.abs(v1.y() - v2.y()) <= epsilon; return Math.abs(v1.x() - v2.x()) <= epsilon && Math.abs(v1.y() - v2.y()) <= epsilon;
} }
/** Transforms a vector as specified (as a point, accounting for translation), storing the
* result in the vector provided.
* @return a reference to the result vector, for chaining. */
public static Vector transform (float x, float y, float sx, float sy, float rotation,
float tx, float ty, Vector result) {
return transform(x, y, sx, sy, FloatMath.sin(rotation), FloatMath.cos(rotation), tx, ty,
result);
}
/** /**
* Transforms a point as specified, storing the result in the point provided. * Transforms a vector as specified, storing the result in the vector provided.
* @return a reference to the result vector, for chaining. * @return a reference to the result vector, for chaining.
*/ */
public static Vector transform (float x, float y, float sx, float sy, float rotation, public static Vector transform (float x, float y, float sx, float sy, float rotation,
Vector result) { Vector result) {
return transform(x, y, sx, sy, FloatMath.sin(rotation), FloatMath.cos(rotation), result); return transform(x, y, sx, sy, FloatMath.sin(rotation), FloatMath.cos(rotation), result);
} }
@@ -103,8 +112,16 @@ public class Vectors
return result.set((x*cosa - y*sina) * sx, (x*sina + y*cosa) * sy); return result.set((x*cosa - y*sina) * sx, (x*sina + y*cosa) * sy);
} }
/** Transforms a vector as specified (as a point, accounting for translation), storing the
* result in the vector provided.
* @return a reference to the result vector, for chaining. */
public static Vector transform (float x, float y, float sx, float sy, float sina, float cosa,
float tx, float ty, Vector result) {
return result.set((x*cosa - y*sina) * sx + tx, (x*sina + y*cosa) * sy + ty);
}
/** /**
* Inverse transforms a point as specified, storing the result in the point provided. * Inverse transforms a vector as specified, storing the result in the vector provided.
* @return a reference to the result vector, for chaining. * @return a reference to the result vector, for chaining.
*/ */
public static Vector inverseTransform (float x, float y, float sx, float sy, float rotation, public static Vector inverseTransform (float x, float y, float sx, float sy, float rotation,