Added bound digests

v5stable
Ondřej Hruška 10 years ago
parent 74c11dcec9
commit fa8792a904
  1. 2
      src/mightypork/test/TestConstr.java
  2. 1940
      src/mightypork/utils/math/constraints/ConstraintFactory.java
  3. 41
      src/mightypork/utils/math/constraints/builder/Bounds.java
  4. 399
      src/mightypork/utils/math/num/Num.java
  5. 66
      src/mightypork/utils/math/num/NumConst.java
  6. 15
      src/mightypork/utils/math/num/NumDigest.java
  7. 4
      src/mightypork/utils/math/num/NumMutable.java
  8. 2
      src/mightypork/utils/math/num/NumVar.java
  9. 65
      src/mightypork/utils/math/rect/Rect.java
  10. 2
      src/mightypork/utils/math/rect/RectAdapter.java
  11. 121
      src/mightypork/utils/math/rect/RectConst.java
  12. 41
      src/mightypork/utils/math/rect/RectDigest.java
  13. 13
      src/mightypork/utils/math/vect/Vect.java
  14. 210
      src/mightypork/utils/math/vect/VectConst.java
  15. 20
      src/mightypork/utils/math/vect/VectDigest.java

@ -83,7 +83,7 @@ public class TestConstr {
{
final NumVar a = Num.makeVar(100);
a.assign(a.mul(50).add(10).div(2));
a.setTo(a.mul(50).add(10).div(2));
System.out.println(a);

File diff suppressed because it is too large Load Diff

@ -1,41 +0,0 @@
//package mightypork.utils.math.constraints.builder;
//
//
//import mightypork.utils.math.constraints.ConstraintFactory;
//import mightypork.utils.math.constraints.RectBound;
//import mightypork.utils.math.constraints.VectBound;
//import mightypork.utils.math.rect.Rect;
//import mightypork.utils.math.vect.Vect;
//
//
//public class Bounds {
//
// public RectBB box(Object side) {
// return wrap(ConstraintFactory.box(side));
// }
// public RectBB box(VectBound origin, Object width, Object height){
// return wrap(ConstraintFactory.box(origin, width, height));
// }
//
// public RectBB box(Object width, Object height){
// return wrap(ConstraintFactory.box(width, height));
// }
//
// public RectBB box(Object x, Object y, Object width, Object height){
// return wrap(ConstraintFactory.box(Rect.ZERO, x,y,width,height));
// }
//
// public RectBB wrap(RectBound rb) {
// return new RectBB(rb);
// }
//
// public static class RectBB {
//
// private final RectBound parent;
//
// public RectBB(RectBound parent) {
// this.parent = parent;
// }
//
// }
//}

@ -38,7 +38,7 @@ public abstract class Num implements NumBound {
@FactoryMethod
public static NumVar makeVar(Num copied)
{
return new NumVar(eval(copied));
return new NumVar(copied.value());
}
private Num p_ceil;
@ -59,33 +59,20 @@ public abstract class Num implements NumBound {
private Num p_abs;
/**
* Convert to double, turning null into zero.
*
* @param a num
* @return double
*/
protected static double eval(final NumBound a)
public NumConst freeze()
{
return toNum(a).value();
return new NumConst(value());
}
/**
* Convert {@link NumBound} to {@link Num}, turning null to Num.ZERO.
* Get a snapshot of the current state, to be used for processing.
*
* @param a numeric bound
* @return num
* @return digest
*/
protected static Num toNum(final NumBound a)
public NumDigest digest()
{
return (a == null) ? Num.ZERO : (a.getNum() == null ? Num.ZERO : a.getNum());
}
public NumConst freeze()
{
return new NumConst(value());
return new NumDigest(this);
}
@ -102,27 +89,25 @@ public abstract class Num implements NumBound {
public abstract double value();
public Num abs()
public Num add(final double addend)
{
if (p_abs == null) p_abs = new Num() {
return new Num() {
final Num t = Num.this;
private final Num t = Num.this;
@Override
public double value()
{
return Math.abs(t.value());
return t.value() + addend;
}
};
return p_abs;
}
public Num acos()
public Num add(final Num addend)
{
if (p_acos == null) p_acos = new Num() {
return new Num() {
final Num t = Num.this;
@ -130,31 +115,37 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.acos(t.value());
return t.value() + addend.value();
}
};
return p_acos;
}
public Num add(final double addend)
public Num sub(final double subtrahend)
{
return new Num() {
return add(-subtrahend);
}
public Num abs()
{
if (p_abs == null) p_abs = new Num() {
private final Num t = Num.this;
final Num t = Num.this;
@Override
public double value()
{
return t.value() + addend;
return Math.abs(t.value());
}
};
return p_abs;
}
public Num add(final Num addend)
public Num sub(final Num subtrahend)
{
return new Num() {
@ -164,15 +155,22 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return t.value() + eval(addend);
return t.value() - subtrahend.value();
}
};
}
public Num asin()
public Num div(final double factor)
{
if (p_asin == null) p_asin = new Num() {
return mul(1 / factor);
}
public Num div(final Num factor)
{
return new Num() {
final Num t = Num.this;
@ -180,34 +178,31 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.asin(t.value());
return t.value() / factor.value();
}
};
return p_asin;
}
public Num atan()
public Num mul(final double factor)
{
if (p_atan == null) p_atan = new Num() {
return new Num() {
final Num t = Num.this;
private final Num t = Num.this;
@Override
public double value()
{
return Math.atan(t.value());
return t.value() * factor;
}
};
return p_atan;
}
public Num average(final double other)
public Num mul(final Num factor)
{
return new Num() {
final Num t = Num.this;
@ -216,13 +211,13 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return (t.value() + other) / 2;
return t.value() * factor.value();
}
};
}
public Num average(final Num other)
public Num average(final double other)
{
return new Num() {
@ -232,15 +227,15 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return (t.value() + eval(other)) / 2;
return (t.value() + other) / 2;
}
};
}
public Num cbrt()
public Num average(final Num other)
{
if (p_cbrt == null) p_cbrt = new Num() {
return new Num() {
final Num t = Num.this;
@ -248,17 +243,21 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.cbrt(t.value());
return (t.value() + other.value()) / 2;
}
};
return p_cbrt;
}
public Num ceil()
public Num perc(final double percent)
{
if (p_ceil == null) p_ceil = new Num() {
return mul(percent / 100);
}
public Num perc(final Num percent)
{
return new Num() {
final Num t = Num.this;
@ -266,11 +265,9 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.round(t.value());
return t.value() * (percent.value() / 100);
}
};
return p_ceil;
}
@ -292,9 +289,9 @@ public abstract class Num implements NumBound {
}
public Num cube()
public Num acos()
{
if (p_cube == null) p_cube = new Num() {
if (p_acos == null) p_acos = new Num() {
final Num t = Num.this;
@ -302,25 +299,17 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
final double v = t.value();
return v * v * v;
return Math.acos(t.value());
}
};
return p_cube;
}
public Num div(final double factor)
{
return mul(1 / factor);
return p_acos;
}
public Num div(final Num factor)
public Num sin()
{
return new Num() {
if (p_sin == null) p_sin = new Num() {
final Num t = Num.this;
@ -328,27 +317,17 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return t.value() / eval(factor);
return Math.sin(t.value());
}
};
return p_sin;
}
public boolean eq(double other)
{
return value() == other;
}
public boolean eq(final Num a)
{
return eq(eval(a));
}
public Num floor()
public Num asin()
{
if (p_floor == null) p_floor = new Num() {
if (p_asin == null) p_asin = new Num() {
final Num t = Num.this;
@ -356,89 +335,17 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.floor(t.value());
return Math.asin(t.value());
}
};
return p_floor;
}
public boolean gt(double other)
{
return Math.signum(value() - other) >= 0;
}
public boolean gt(final Num other)
{
return gt(eval(other));
}
public boolean gte(double other)
{
return Math.signum(value() - other) >= 0;
}
public boolean gte(final Num other)
{
return gte(eval(other));
}
public Num half()
{
return mul(0.5);
}
public boolean isNegative()
{
return value() < 0;
}
public boolean isPositive()
{
return value() > 0;
}
public boolean isZero()
{
return value() == 0;
}
public boolean lt(double other)
{
return !gte(other);
}
public boolean lt(final Num other)
{
return !gte(other);
}
public boolean lte(double other)
{
return !gt(other);
}
public boolean lte(final Num other)
{
return !gt(other);
return p_asin;
}
public Num max(final double other)
public Num tan()
{
return new Num() {
if (p_tan == null) p_tan = new Num() {
final Num t = Num.this;
@ -446,15 +353,17 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.max(t.value(), other);
return Math.tan(t.value());
}
};
return p_tan;
}
public Num max(final Num other)
public Num atan()
{
return new Num() {
if (p_atan == null) p_atan = new Num() {
final Num t = Num.this;
@ -462,15 +371,17 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.max(t.value(), eval(other));
return Math.atan(t.value());
}
};
return p_atan;
}
public Num min(final double other)
public Num cbrt()
{
return new Num() {
if (p_cbrt == null) p_cbrt = new Num() {
final Num t = Num.this;
@ -478,15 +389,17 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.min(t.value(), other);
return Math.cbrt(t.value());
}
};
return p_cbrt;
}
public Num min(final Num other)
public Num sqrt()
{
return new Num() {
if (p_sqrt == null) p_sqrt = new Num() {
final Num t = Num.this;
@ -494,32 +407,35 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.min(t.value(), eval(other));
return Math.sqrt(t.value());
}
};
return p_sqrt;
}
public Num mul(final double factor)
public Num neg()
{
return new Num() {
if (p_neg == null) p_neg = new Num() {
private final Num t = Num.this;
final Num t = Num.this;
@Override
public double value()
{
return t.value() * factor;
return -1 * t.value();
}
};
return p_neg;
}
public Num mul(final Num factor)
public Num round()
{
return new Num() {
if (p_round == null) p_round = new Num() {
final Num t = Num.this;
@ -527,15 +443,17 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return t.value() * eval(factor);
return Math.round(t.value());
}
};
return p_round;
}
public Num neg()
public Num floor()
{
if (p_neg == null) p_neg = new Num() {
if (p_floor == null) p_floor = new Num() {
final Num t = Num.this;
@ -543,23 +461,17 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return -1 * t.value();
return Math.floor(t.value());
}
};
return p_neg;
}
public Num perc(final double percent)
{
return mul(percent / 100);
return p_floor;
}
public Num perc(final Num percent)
public Num ceil()
{
return new Num() {
if (p_ceil == null) p_ceil = new Num() {
final Num t = Num.this;
@ -567,9 +479,11 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return t.value() * (eval(percent) / 100);
return Math.round(t.value());
}
};
return p_ceil;
}
@ -599,15 +513,15 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.pow(t.value(), eval(power));
return Math.pow(t.value(), power.value());
}
};
}
public Num round()
public Num cube()
{
if (p_round == null) p_round = new Num() {
if (p_cube == null) p_cube = new Num() {
final Num t = Num.this;
@ -615,17 +529,18 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.round(t.value());
final double v = t.value();
return v * v * v;
}
};
return p_round;
return p_cube;
}
public Num signum()
public Num square()
{
if (p_sgn == null) p_sgn = new Num() {
if (p_square == null) p_square = new Num() {
final Num t = Num.this;
@ -633,17 +548,24 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.signum(t.value());
final double v = t.value();
return v * v;
}
};
return p_sgn;
return p_square;
}
public Num sin()
public Num half()
{
if (p_sin == null) p_sin = new Num() {
return mul(0.5);
}
public Num max(final double other)
{
return new Num() {
final Num t = Num.this;
@ -651,17 +573,15 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.sin(t.value());
return Math.max(t.value(), other);
}
};
return p_sin;
}
public Num sqrt()
public Num max(final Num other)
{
if (p_sqrt == null) p_sqrt = new Num() {
return new Num() {
final Num t = Num.this;
@ -669,17 +589,15 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.sqrt(t.value());
return Math.max(t.value(), other.value());
}
};
return p_sqrt;
}
public Num square()
public Num min(final Num other)
{
if (p_square == null) p_square = new Num() {
return new Num() {
final Num t = Num.this;
@ -687,22 +605,13 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
final double v = t.value();
return v * v;
return Math.min(t.value(), other.value());
}
};
return p_square;
}
public Num sub(final double subtrahend)
{
return add(-subtrahend);
}
public Num sub(final Num subtrahend)
public Num min(final double other)
{
return new Num() {
@ -712,15 +621,15 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return t.value() - eval(subtrahend);
return Math.min(t.value(), other);
}
};
}
public Num tan()
public Num signum()
{
if (p_tan == null) p_tan = new Num() {
if (p_sgn == null) p_sgn = new Num() {
final Num t = Num.this;
@ -728,11 +637,29 @@ public abstract class Num implements NumBound {
@Override
public double value()
{
return Math.tan(t.value());
return Math.signum(t.value());
}
};
return p_tan;
return p_sgn;
}
public boolean isNegative()
{
return value() < 0;
}
public boolean isPositive()
{
return value() > 0;
}
public boolean isZero()
{
return value() == 0;
}
@ -756,7 +683,7 @@ public abstract class Num implements NumBound {
if (!(obj instanceof Num)) return false;
final Num other = (Num) obj;
return eq(other);
return value() == other.value();
}

@ -1,17 +1,15 @@
package mightypork.utils.math.num;
import mightypork.utils.math.constraints.NumBound;
/**
* Constant number {@link NumBound}
* Constant number
*
* @author MightyPork
*/
public class NumConst extends Num {
private final double value;
private NumDigest digest;
NumConst(Num copied) {
@ -32,7 +30,7 @@ public class NumConst extends Num {
/**
* No good to copy a constant.
* @deprecated No good to copy a constant.
*/
@Override
@Deprecated
@ -41,6 +39,11 @@ public class NumConst extends Num {
return this;
}
@Override
public NumDigest digest()
{
return (digest != null) ? digest : (digest = super.digest());
}
@Override
public NumConst add(double addend)
@ -49,6 +52,12 @@ public class NumConst extends Num {
}
public NumConst add(NumConst addend)
{
return Num.make(value + addend.value);
}
@Override
public NumConst sub(double subtrahend)
{
@ -56,6 +65,12 @@ public class NumConst extends Num {
}
public NumConst sub(NumConst addend)
{
return Num.make(value - addend.value);
}
@Override
public NumConst mul(double factor)
{
@ -63,6 +78,12 @@ public class NumConst extends Num {
}
public NumConst mul(NumConst addend)
{
return Num.make(value * addend.value);
}
@Override
public NumConst div(double factor)
{
@ -70,6 +91,12 @@ public class NumConst extends Num {
}
public NumConst div(NumConst addend)
{
return Num.make(value / addend.value);
}
@Override
public NumConst perc(double percents)
{
@ -198,6 +225,12 @@ public class NumConst extends Num {
}
public NumConst average(NumConst other)
{
return super.average(other).freeze();
}
@Override
public NumConst round()
{
@ -225,27 +258,4 @@ public class NumConst extends Num {
return mul(0.5);
}
public NumConst add(NumConst addend)
{
return make(value + addend.value);
}
public NumConst sub(NumConst addend)
{
return make(value - addend.value);
}
public NumConst mul(NumConst addend)
{
return make(value * addend.value);
}
public NumConst div(NumConst addend)
{
return make(value / addend.value);
}
}

@ -0,0 +1,15 @@
package mightypork.utils.math.num;
public class NumDigest {
public final NumConst source;
public final double value;
public NumDigest(Num num) {
this.value = num.value();
this.source = num.freeze();
}
}

@ -21,9 +21,9 @@ public abstract class NumMutable extends Num {
*
* @param value new value
*/
public void assign(Num value)
public void setTo(Num value)
{
setTo(eval(value));
setTo(value);
}

@ -12,7 +12,7 @@ public class NumVar extends NumMutable {
public NumVar(Num value) {
this.value = eval(value);
this(value.value());
}

@ -147,6 +147,10 @@ public abstract class Rect implements RectBound {
private Num p_r;
private Num p_t;
private Num p_b;
private Rect p_edge_l;
private Rect p_edge_r;
private Rect p_edge_t;
private Rect p_edge_b;
/**
@ -161,6 +165,17 @@ public abstract class Rect implements RectBound {
}
/**
* Get a snapshot of the current state, to be used for processing.
*
* @return digest
*/
public RectDigest digest()
{
return new RectDigest(this);
}
@Override
public Rect getRect()
{
@ -313,25 +328,7 @@ public abstract class Rect implements RectBound {
*/
public Rect shrink(final double left, final double right, final double top, final double bottom)
{
return new Rect() {
private final Rect t = Rect.this;
@Override
public Vect size()
{
return t.size().sub(left + right, top + bottom);
}
@Override
public Vect origin()
{
return t.origin().add(left, top);
}
};
return grow(-left, -right, -top, -bottom);
}
@ -574,6 +571,30 @@ public abstract class Rect implements RectBound {
}
public Rect leftEdge()
{
return p_edge_l != null ? p_edge_l : (p_edge_l = topLeft().expand(Num.ZERO, Num.ZERO, Num.ZERO, height()));
}
public Rect rightEdge()
{
return p_edge_r != null ? p_edge_r : (p_edge_r = topRight().expand(Num.ZERO, Num.ZERO, Num.ZERO, height()));
}
public Rect topEdge()
{
return p_edge_t != null ? p_edge_t : (p_edge_t = topLeft().expand(Num.ZERO, width(), Num.ZERO, Num.ZERO));
}
public Rect bottomEdge()
{
return p_edge_b != null ? p_edge_b : (p_edge_b = bottomLeft().expand(Num.ZERO, width(), Num.ZERO, Num.ZERO));
}
/**
* Center to given point
*
@ -623,6 +644,12 @@ public abstract class Rect implements RectBound {
}
/**
* Center to given rect's center
*
* @param parent rect to center to
* @return centered
*/
public Rect centerTo(Rect parent)
{
return centerTo(parent.center());

@ -14,7 +14,7 @@ import mightypork.utils.math.vect.VectAdapter;
public abstract class RectAdapter extends Rect {
// adapters are needed in case the vect returned from source changes
// (is replaced). This way, references to origin and rect will stay intack.
// (is replaced). This way, references to origin and rect will stay intact.
private final VectAdapter originAdapter = new VectAdapter() {

@ -28,6 +28,11 @@ public class RectConst extends Rect {
private VectConst v_bl;
private VectConst v_bc;
private RectConst v_round;
private RectConst v_edge_l;
private RectConst v_edge_r;
private RectConst v_edge_t;
private RectConst v_edge_b;
private RectDigest digest;
/**
@ -76,7 +81,13 @@ public class RectConst extends Rect {
{
return this; // already constant
}
@Override
public RectDigest digest()
{
return (digest != null) ? digest : (digest = super.digest());
}
@Override
public VectConst origin()
@ -104,12 +115,18 @@ public class RectConst extends Rect {
{
return Rect.make(pos.add(x, y), size);
}
public RectConst move(NumConst x, NumConst y)
{
return super.move(x, y).freeze();
}
@Override
public RectConst shrink(double left, double right, double top, double bottom)
{
return Rect.make(pos.add(left, top), size.sub(left + right, top + bottom)).freeze();
return super.shrink(left, right, top, bottom).freeze();
}
@ -117,15 +134,14 @@ public class RectConst extends Rect {
@Override
public RectConst grow(double left, double right, double top, double bottom)
{
return Rect.make(pos.sub(left, top), size.add(left + right, top + bottom)).freeze();
return super.grow(left, right, top, bottom).freeze();
}
@Override
public RectConst round()
{
if (v_round == null) v_round = Rect.make(pos.round(), size.round());
return v_round;
return (v_round != null) ? v_round : (v_round = Rect.make(pos.round(), size.round()));
}
@ -167,8 +183,7 @@ public class RectConst extends Rect {
@Override
public NumConst right()
{
if (v_r == null) v_r = super.right().freeze();
return v_r;
return (v_r != null) ? v_r : (v_r = super.right().freeze());
}
@ -182,8 +197,7 @@ public class RectConst extends Rect {
@Override
public NumConst bottom()
{
if (v_b == null) v_b = super.bottom().freeze();
return v_b;
return (v_b != null) ? v_b : (v_b = super.bottom().freeze());
}
@ -197,64 +211,123 @@ public class RectConst extends Rect {
@Override
public VectConst topCenter()
{
if (v_tc == null) v_tc = super.topCenter().freeze();
return v_tc;
return (v_tc != null) ? v_tc : (v_tc = super.topCenter().freeze());
}
@Override
public VectConst topRight()
{
if (v_tr == null) v_tr = super.topRight().freeze();
return v_tr;
return (v_tr != null) ? v_tr : (v_tr = super.topRight().freeze());
}
@Override
public VectConst centerLeft()
{
if (v_cl == null) v_cl = super.centerLeft().freeze();
return v_cl;
return (v_cl != null) ? v_cl : (v_cl = super.centerLeft().freeze());
}
@Override
public VectConst center()
{
if (v_c == null) v_c = super.center().freeze();
return v_c;
return (v_c != null) ? v_c : (v_c = super.center().freeze());
}
@Override
public VectConst centerRight()
{
if (v_cr == null) v_cr = super.centerRight().freeze();
return v_cr;
return (v_cr != null) ? v_cr : (v_cr = super.centerRight().freeze());
}
@Override
public VectConst bottomLeft()
{
if (v_bl == null) v_bl = super.bottomLeft().freeze();
return v_bl;
return (v_bl != null) ? v_bl : (v_bl = super.bottomLeft().freeze());
}
@Override
public VectConst bottomCenter()
{
if (v_bc == null) v_bc = super.bottomCenter().freeze();
return v_bc;
return (v_bc != null) ? v_bc : (v_bc = super.bottomCenter().freeze());
}
@Override
public VectConst bottomRight()
{
if (v_br == null) v_br = super.bottomRight().freeze();
return v_br;
return (v_br != null) ? v_br : (v_br = super.bottomRight().freeze());
}
@Override
public RectConst leftEdge()
{
return (v_edge_l != null) ? v_edge_l : (v_edge_l = super.leftEdge().freeze());
}
@Override
public RectConst rightEdge()
{
return (v_edge_r != null) ? v_edge_r : (v_edge_r = super.rightEdge().freeze());
}
@Override
public RectConst topEdge()
{
return (v_edge_t != null) ? v_edge_t : (v_edge_t = super.topEdge().freeze());
}
@Override
public RectConst bottomEdge()
{
return (v_edge_b != null) ? v_edge_b : (v_edge_b = super.bottomEdge().freeze());
}
@Override
public Rect shrink(Vect shrink)
{
return super.shrink(shrink);
}
@Override
public RectConst shrink(double x, double y)
{
return super.shrink(x, y).freeze();
}
public RectConst shrink(NumConst left, NumConst right, NumConst top, NumConst bottom)
{
return super.shrink(left, right, top, bottom).freeze();
}
public RectConst grow(NumConst left, NumConst right, NumConst top, NumConst bottom)
{
return super.grow(left, right, top, bottom).freeze();
}
public RectConst centerTo(VectConst point)
{
return super.centerTo(point).freeze();
}
public RectConst centerTo(RectConst parent)
{
return super.centerTo(parent).freeze();
}
}

@ -0,0 +1,41 @@
package mightypork.utils.math.rect;
import mightypork.utils.math.vect.VectConst;
public class RectDigest {
public final RectConst source;
public final VectConst origin;
public final VectConst size;
public final double x;
public final double y;
public final double width;
public final double height;
public final double left;
public final double right;
public final double top;
public final double bottom;
public RectDigest(Rect rect) {
this.source = rect.freeze();
this.origin = rect.origin().freeze();
this.size = rect.size().freeze();
this.x = rect.x().value();
this.y = rect.y().value();
this.width = rect.width().value();
this.height = rect.height().value();
this.left = rect.left().value();
this.right = rect.right().value();
this.top = rect.top().value();
this.bottom = rect.bottom().value();
}
}

@ -8,6 +8,7 @@ import mightypork.utils.math.constraints.VectBound;
import mightypork.utils.math.num.Num;
import mightypork.utils.math.num.NumConst;
import mightypork.utils.math.rect.Rect;
import mightypork.utils.math.rect.RectDigest;
/**
@ -235,6 +236,18 @@ public abstract class Vect implements VectBound {
{
return new VectConst(this);
}
/**
* Get a snapshot of the current state, to be used for processing.
*
* @return digest
*/
public VectDigest digest()
{
return new VectDigest(this);
}
/**

@ -3,6 +3,7 @@ package mightypork.utils.math.vect;
import mightypork.utils.math.num.Num;
import mightypork.utils.math.num.NumConst;
import mightypork.utils.math.rect.RectConst;
/**
@ -25,6 +26,7 @@ public final class VectConst extends Vect {
private NumConst v_xc;
private NumConst v_yc;
private NumConst v_zc;
private VectDigest digest;
VectConst(Vect other) {
@ -63,43 +65,40 @@ public final class VectConst extends Vect {
/**
* @return X constraint
*/
@Override
public final NumConst xn()
{
if (v_xc == null) v_xc = Num.make(this.x);
return v_xc;
return (v_xc != null) ? v_xc : (v_xc = Num.make(this.x));
}
/**
* @return Y constraint
*/
@Override
public final NumConst yn()
{
if (v_yc == null) v_yc = Num.make(this.y);
return v_yc;
return (v_yc != null) ? v_yc : (v_yc = Num.make(this.y));
}
/**
* @return Z constraint
*/
@Override
public final NumConst zn()
{
if (v_zc == null) v_zc = Num.make(this.z);
return v_zc;
return (v_zc != null) ? v_zc : (v_zc = Num.make(this.z));
}
/**
* @deprecated it's useless to copy a constant
*/
@Override
@Deprecated
public VectConst freeze()
@ -108,70 +107,73 @@ public final class VectConst extends Vect {
}
@Override
public VectDigest digest()
{
return (digest != null) ? digest : (digest = super.digest());
}
@Override
public VectConst abs()
{
if (v_abs != null) return v_abs;
return v_abs = Vect.make(Math.abs(x()), Math.abs(y()), Math.abs(z()));
return (v_abs != null) ? v_abs : (v_abs = super.abs().freeze());
}
@Override
public VectConst add(double x, double y)
{
return add(x, y, 0);
return super.add(x, y).freeze();
}
@Override
public VectConst add(double x, double y, double z)
{
return Vect.make(x() + x, y() + y, z() + z);
return super.add(x, y, z).freeze();
}
@Override
public VectConst half()
{
if (v_half != null) return v_half;
return v_half = mul(0.5);
return (v_half != null) ? v_half : (v_half = super.half().freeze());
}
@Override
public VectConst mul(double d)
{
return mul(d, d, d);
return super.mul(d).freeze();
}
@Override
public VectConst mul(double x, double y)
{
return mul(x, y, 1);
return super.mul(x, y).freeze();
}
@Override
public VectConst mul(double x, double y, double z)
{
return Vect.make(x() * x, y() * y, z() * z);
return super.mul(x, y, z).freeze();
}
@Override
public VectConst round()
{
if (v_round != null) return v_round;
return v_round = Vect.make(Math.round(x()), Math.round(y()), Math.round(z()));
return (v_round != null) ? v_round : (v_round = super.round().freeze());
}
@Override
public VectConst floor()
{
if (v_floor != null) return v_floor;
return v_floor = Vect.make(Math.floor(x()), Math.floor(y()), Math.floor(z()));
return (v_floor != null) ? v_floor : (v_floor = super.floor().freeze());
}
@ -179,50 +181,184 @@ public final class VectConst extends Vect {
public VectConst ceil()
{
if (v_ceil != null) return v_ceil;
return v_ceil = Vect.make(Math.ceil(x()), Math.ceil(y()), Math.ceil(z()));
return v_ceil = super.ceil().freeze();
}
@Override
public VectConst sub(double x, double y)
{
return sub(x, y, 0);
return super.sub(x, y).freeze();
}
@Override
public VectConst sub(double x, double y, double z)
{
return Vect.make(x() - x, y() - y, z() - z);
return super.sub(x, y, z).freeze();
}
@Override
public VectConst neg()
{
if (v_neg != null) return v_neg;
return v_neg = Vect.make(-x(), -y(), -z());
return (v_neg != null) ? v_neg : (v_neg = super.neg().freeze());
}
@Override
public VectConst norm(double size)
{
if (isZero()) return this; // can't norm zero vector
final double k = size().mul(1 / size).value();
return mul(k);
return super.norm(size).freeze();
}
@Override
public NumConst size()
{
if (v_size != null) return v_size;
final double x = x(), y = y(), z = z();
return v_size = Num.make(Math.sqrt(x * x + y * y + z * z));
return (v_size != null) ? v_size : (v_size = super.size().freeze());
}
@Override
public VectConst withX(double x)
{
return super.withX(x).freeze();
}
@Override
public VectConst withY(double y)
{
return super.withY(y).freeze();
}
@Override
public VectConst withZ(double z)
{
return super.withZ(z).freeze();
}
public VectConst withX(NumConst x)
{
return super.withX(x).freeze();
}
public VectConst withY(NumConst y)
{
return super.withY(y).freeze();
}
public VectConst withZ(NumConst z)
{
return super.withZ(z).freeze();
}
public VectConst add(VectConst vec)
{
return super.add(vec).freeze();
}
public VectConst add(NumConst x, NumConst y)
{
return super.add(x, y).freeze();
}
public VectConst add(NumConst x, NumConst y, NumConst z)
{
return super.add(x, y, z).freeze();
}
public VectConst mul(VectConst vec)
{
return super.mul(vec).freeze();
}
public VectConst mul(NumConst d)
{
return super.mul(d).freeze();
}
public VectConst mul(NumConst x, NumConst y)
{
return super.mul(x, y).freeze();
}
public VectConst mul(NumConst x, NumConst y, NumConst z)
{
return super.mul(x, y, z).freeze();
}
public VectConst sub(VectConst vec)
{
return super.sub(vec).freeze();
}
public VectConst sub(NumConst x, NumConst y)
{
return super.sub(x, y).freeze();
}
public VectConst sub(NumConst x, NumConst y, NumConst z)
{
return super.sub(x, y, z).freeze();
}
public VectConst norm(NumConst size)
{
return super.norm(size).freeze();
}
public NumConst dist(VectConst point)
{
return super.dist(point).freeze();
}
public VectConst midTo(VectConst point)
{
return super.midTo(point).freeze();
}
public VectConst vectTo(VectConst point)
{
return super.vectTo(point).freeze();
}
public NumConst dot(VectConst vec)
{
return super.dot(vec).freeze();
}
@Override
public RectConst expand(int left, int right, int top, int bottom)
{
return super.expand(left, right, top, bottom).freeze();
}
public RectConst expand(NumConst left, NumConst right, NumConst top, NumConst bottom)
{
return super.expand(left, right, top, bottom).freeze();
}
}

@ -0,0 +1,20 @@
package mightypork.utils.math.vect;
public class VectDigest {
public final VectConst source;
public final double x;
public final double y;
public final double z;
public VectDigest(Vect vect) {
this.source = vect.freeze();
this.x = vect.x();
this.y = vect.y();
this.z = vect.z();
}
}
Loading…
Cancel
Save