Skip to content

Commit

Permalink
Update with latest Arc3D
Browse files Browse the repository at this point in the history
  • Loading branch information
BloCamLimb committed Aug 24, 2024
1 parent 2e7b411 commit 6e57c1a
Show file tree
Hide file tree
Showing 27 changed files with 369 additions and 186 deletions.
8 changes: 4 additions & 4 deletions core/src/test/java/icyllis/modernui/test/TestJ2D.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public static Matrix4 getOldMatrix(float pivotX, float pivotY, float translation
matrix.setTranslate(pivotX + translationX, pivotY + translationY, 0);
matrix.preScale(scaleX, scaleY);
matrix.preTranslate(-pivotX, -pivotY);
Matrix4 matrix2 = Matrix4.identity();
Matrix4 matrix2 = new Matrix4();
matrix2.m34 = 1 / 1920f;
matrix2.preRotate(Math.toRadians(-rotationX),
Math.toRadians(-rotationY),
Expand All @@ -236,11 +236,11 @@ public static Matrix4 getOldMatrix(float pivotX, float pivotY, float translation

public static Matrix4 getNewMatrix(float pivotX, float pivotY, float translationX, float translationY,
float scaleX, float scaleY, float rotationX, float rotationY, float rotationZ) {
Matrix4 matrix = Matrix4.identity();
Matrix4 matrix = new Matrix4();
matrix.preTranslate(pivotX, pivotY);
matrix.preScale(scaleX, scaleY);
matrix.preTranslate(-pivotX, -pivotY);
Matrix4 matrix2 = Matrix4.identity();
Matrix4 matrix2 = new Matrix4();
matrix2.m34 = 1 / 1920f;
matrix2.preRotate(Math.toRadians(-rotationX),
Math.toRadians(-rotationY),
Expand All @@ -253,7 +253,7 @@ public static Matrix4 getNewMatrix(float pivotX, float pivotY, float translation

public static Matrix4 getNewMatrix2(float pivotX, float pivotY, float translationX, float translationY,
float scaleX, float scaleY, float rotationX, float rotationY, float rotationZ) {
Matrix4 matrix = Matrix4.identity();
Matrix4 matrix = new Matrix4();
matrix.m34 = 1 / 1920f;
matrix.preRotate(Math.toRadians(-rotationX),
Math.toRadians(-rotationY),
Expand Down
6 changes: 3 additions & 3 deletions external/Arc3D/src/main/java/icyllis/arc3d/core/Device.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ public abstract class Device extends RefCnt {
private final ImageInfo mInfo;
private final Rect2i mBounds = new Rect2i();

final Matrix4 mLocalToDevice = Matrix4.identity();
final Matrix4 mLocalToDevice = new Matrix4();
final Matrix mLocalToDevice33 = new Matrix();

// mDeviceToGlobal and mGlobalToDevice are inverses of each other
final Matrix4 mDeviceToGlobal = Matrix4.identity();
final Matrix4 mGlobalToDevice = Matrix4.identity();
final Matrix4 mDeviceToGlobal = new Matrix4();
final Matrix4 mGlobalToDevice = new Matrix4();

public Device(ImageInfo info) {
mInfo = info;
Expand Down
80 changes: 65 additions & 15 deletions external/Arc3D/src/main/java/icyllis/arc3d/core/Matrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -2218,21 +2218,7 @@ private static float computeMaxScale(double m11, double m21, double m12, double
return (float) Math.sqrt(0.5 * (s1 + s2));
}

/**
* Returns the minimum scaling factor of this matrix by decomposing the scaling and
* shearing elements. When this matrix has perspective, the scaling factor is specific
* to the given point <var>p</var>.<br>
* Returns -1 if scale factor overflows.
*
* @param px the x-coord of point
* @param py the y-coord of point
* @return minimum scale factor
*/
public float getMinScale(float px, float py) {
if (!hasPerspective()) {
return getMinScale();
}

private float computeMinScale(double px, double py) {
double x = m11 * px + m21 * py + m41;
double y = m12 * px + m22 * py + m42;
double w = m14 * px + m24 * py + m44;
Expand All @@ -2254,6 +2240,23 @@ public float getMinScale(float px, float py) {
return computeMinScale(dfdu, dfdv, dgdu, dgdv);
}

/**
* Returns the minimum scaling factor of this matrix by decomposing the scaling and
* shearing elements. When this matrix has perspective, the scaling factor is specific
* to the given point <var>p</var>.<br>
* Returns -1 if scale factor overflows.
*
* @param px the x-coord of point
* @param py the y-coord of point
* @return minimum scale factor
*/
public float getMinScale(float px, float py) {
if (!hasPerspective()) {
return getMinScale();
}
return computeMinScale(px, py);
}

/**
* Returns the maximum scaling factor of this matrix by decomposing the scaling and
* shearing elements. When this matrix has perspective, the scaling factor is specific
Expand Down Expand Up @@ -2347,6 +2350,53 @@ public float differentialAreaScale(float px, float py) {
return (float) (Math.abs(detJ * denom));
}

/**
* Return the minimum distance needed to move in local (pre-transform) space to ensure that the
* transformed coordinates are at least 1px away from the original mapped point. This minimum
* distance is specific to the given local 'bounds' since the scale factors change with
* perspective.
* <p>
* If the bounds will be clipped by the w=0 plane or otherwise is ill-conditioned, this will
* return positive infinity.
*/
public float localAARadius(Rect2fc bounds) {
return localAARadius(bounds.left(), bounds.top(), bounds.right(), bounds.bottom());
}

/**
* Return the minimum distance needed to move in local (pre-transform) space to ensure that the
* transformed coordinates are at least 1px away from the original mapped point. This minimum
* distance is specific to the given local 'bounds' since the scale factors change with
* perspective.
* <p>
* If the bounds will be clipped by the w=0 plane or otherwise is ill-conditioned, this will
* return positive infinity.
*/
public float localAARadius(float left, float top, float right, float bottom) {
float min;
if ((getType() & kPerspective_Mask) == 0) {
min = getMinScale();
} else {
// Calculate the minimum scale factor over the 4 corners of the bounding box
float tl = computeMinScale(left, top);
float tr = computeMinScale(right, top);
float br = computeMinScale(right, bottom);
float bl = computeMinScale(left, bottom);
min = MathUtil.min(tl, tr, br, bl);
}

// Moving 1 from 'p' before transforming will move at least 'min' and at most 'max' from
// the transformed point. Thus moving between [1/max, 1/min] pre-transformation means post
// transformation moves between [1,max/min] so using 1/min as the local AA radius ensures that
// the post-transformed point is at least 1px away from the original.
float aaRadius = 1.0f / min;
if (Float.isFinite(aaRadius)) { // check Inf and NaN
return aaRadius;
} else {
return Float.POSITIVE_INFINITY;
}
}

/**
* Sets matrix to scale and translate src rect to dst rect. Returns false if
* src is empty, and sets matrix to identity. Returns true if dst is empty,
Expand Down
19 changes: 9 additions & 10 deletions external/Arc3D/src/main/java/icyllis/arc3d/core/Matrix4.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
@SuppressWarnings("unused")
public non-sealed class Matrix4 implements Matrix4c, Cloneable {

private static final Matrix4 IDENTITY = new Matrix4();

// sequential matrix elements, m(ij) (row, column)
// directly using primitives will be faster than array in Java (before Vector API)
// [m11 m12 m13 m14]
Expand All @@ -72,11 +74,10 @@ public non-sealed class Matrix4 implements Matrix4c, Cloneable {
public float m44;

/**
* Create a zero matrix.
*
* @see #identity()
* Create a new identity matrix.
*/
public Matrix4() {
m11 = m22 = m33 = m44 = 1.0f;
}

/**
Expand Down Expand Up @@ -106,20 +107,18 @@ public Matrix4(@Nonnull float... a) {
* @return a copy of the matrix
*/
@Nonnull
public static Matrix4 copy(@Nullable Matrix4 m) {
return m == null ? identity() : m.clone();
public static Matrix4 copy(@Nullable Matrix4c m) {
return m == null ? new Matrix4() : m.clone();
}

/**
* Create a new identity matrix.
* Returns a read-only identity matrix.
*
* @return an identity matrix
*/
@Nonnull
public static Matrix4 identity() {
final Matrix4 m = new Matrix4();
m.m11 = m.m22 = m.m33 = m.m44 = 1.0f;
return m;
public static Matrix4c identity() {
return IDENTITY;
}

/**
Expand Down
25 changes: 25 additions & 0 deletions external/Arc3D/src/main/java/icyllis/arc3d/core/Matrixc.java
Original file line number Diff line number Diff line change
Expand Up @@ -540,6 +540,28 @@ default void mapPoints(float[] src, float[] dst, int count) {
*/
float differentialAreaScale(float px, float py);

/**
* Return the minimum distance needed to move in local (pre-transform) space to ensure that the
* transformed coordinates are at least 1px away from the original mapped point. This minimum
* distance is specific to the given local 'bounds' since the scale factors change with
* perspective.
* <p>
* If the bounds will be clipped by the w=0 plane or otherwise is ill-conditioned, this will
* return positive infinity.
*/
float localAARadius(Rect2fc bounds);

/**
* Return the minimum distance needed to move in local (pre-transform) space to ensure that the
* transformed coordinates are at least 1px away from the original mapped point. This minimum
* distance is specific to the given local 'bounds' since the scale factors change with
* perspective.
* <p>
* If the bounds will be clipped by the w=0 plane or otherwise is ill-conditioned, this will
* return positive infinity.
*/
float localAARadius(float left, float top, float right, float bottom);

/**
* Returns true if all elements of the matrix are finite. Returns false if any
* element is infinity, or NaN.
Expand All @@ -556,4 +578,7 @@ default void mapPoints(float[] src, float[] dst, int count) {

@Override
String toString();

@Nonnull
Matrix clone();
}
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ public Matrix3 toMatrix3() {
public Matrix4 toMatrix4() {
final float sq = lengthSq();
if (sq < 1.0e-6f) {
return Matrix4.identity();
return new Matrix4();
}
final float is;
if (MathUtil.isApproxEqual(sq, 1.0f)) {
Expand Down
30 changes: 15 additions & 15 deletions external/Arc3D/src/main/java/icyllis/arc3d/granite/ClipStack.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Collection<Element> elements() {

private final ClipElement mTmpElement = new ClipElement();

public void clipRect(@Nullable Matrix4c viewMatrix,
public void clipRect(@Nullable Matrixc viewMatrix,
@Nonnull Rect2fc localRect,
int clipOp) {
clip(mTmpElement.init(
Expand Down Expand Up @@ -317,7 +317,7 @@ public <GEO> boolean prepareForDraw(Draw draw,
assert elementsForMask.isEmpty();

mTmpDraw.init(
shapeInDeviceSpace ? Matrix4.identity() : draw.mTransform,
shapeInDeviceSpace ? Matrix.identity() : draw.mTransform,
shapeBounds,
drawBounds
);
Expand Down Expand Up @@ -419,7 +419,7 @@ public interface ClipGeometry {

Rect2fc shape();

Matrix4c viewMatrix();
Matrixc viewMatrix();

Rect2fc outerBounds();

Expand Down Expand Up @@ -568,17 +568,17 @@ public static class Element {

// owned memory
final Rect2f mShape;
final Matrix4 mViewMatrix;
final Matrix mViewMatrix;
int mClipOp;

Element() {
mShape = new Rect2f();
mViewMatrix = new Matrix4();
mViewMatrix = new Matrix();
}

Element(Rect2fc shape, Matrix4c viewMatrix, int clipOp) {
Element(Rect2fc shape, Matrixc viewMatrix, int clipOp) {
mShape = new Rect2f(shape);
mViewMatrix = new Matrix4(viewMatrix);
mViewMatrix = new Matrix(viewMatrix);
mClipOp = clipOp;
}

Expand All @@ -590,7 +590,7 @@ public Rect2fc shape() {

// local to device
// do not modify
public Matrix4c viewMatrix() {
public Matrixc viewMatrix() {
return mViewMatrix;
}

Expand All @@ -614,7 +614,7 @@ static final class ClipElement extends Element implements ClipGeometry {
boolean mInverseFill;

// cached inverse of fLocalToDevice for contains() optimization
final Matrix4 mInverseViewMatrix = new Matrix4();
final Matrix mInverseViewMatrix = new Matrix();

// Device space bounds. These bounds are not snapped to pixels with the assumption that if
// a relation (intersects, contains, etc.) is true for the bounds it will be true for the
Expand Down Expand Up @@ -668,7 +668,7 @@ public void set(ClipElement e) {
public ClipElement init(Rect2ic deviceBounds,
Rect2fc shape,
boolean inverseFill,
Matrix4c viewMatrix,
Matrixc viewMatrix,
int clipOp) {
mShape.set(shape);
mViewMatrix.set(viewMatrix);
Expand Down Expand Up @@ -1108,8 +1108,8 @@ public Rect2fc shape() {
}

@Override
public Matrix4c viewMatrix() {
return Matrix4.identity();
public Matrixc viewMatrix() {
return Matrix.identity();
}

public Rect2fc outerBounds() {
Expand Down Expand Up @@ -1438,11 +1438,11 @@ public Rect2ic scissor(Rect2ic deviceBounds, Rect2fc drawBounds) {

static final class ClipDraw implements ClipGeometry {

final Matrix4 mViewMatrix = new Matrix4();
final Matrix mViewMatrix = new Matrix();
final Rect2f mShape = new Rect2f();
final Rect2f mDrawBounds = new Rect2f();

public ClipDraw init(Matrix4c viewMatrix,
public ClipDraw init(Matrixc viewMatrix,
Rect2fc shape,
Rect2fc drawBounds) {
mViewMatrix.set(viewMatrix);
Expand All @@ -1462,7 +1462,7 @@ public Rect2fc shape() {
}

@Override
public Matrix4 viewMatrix() {
public Matrixc viewMatrix() {
return mViewMatrix;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public final class Draw implements AutoCloseable {
/**
* This matrix transforms geometry's local space to device space.
*/
public Matrix4c mTransform;
public Matrixc mTransform;
public Object mGeometry;
/**
* Clip params (immutable), set by {@link ClipStack}.
Expand Down
Loading

0 comments on commit 6e57c1a

Please sign in to comment.