use of imagingbook.lib.math.Complex in project imagingbook-common by imagingbook.
the class FourierDescriptor method setCoefficient.
public void setCoefficient(int m, double a, double b) {
int mm = Arithmetic.mod(m, G.length);
G[mm] = new Complex(a, b);
}
use of imagingbook.lib.math.Complex in project imagingbook-common by imagingbook.
the class FourierDescriptor method makeFourierPairsReconstruction.
/**
* Reconstructs the shape obtained from FD-pairs 0,...,Mp as a polygon (path).
*
* @param Mp number of Fourier coefficient pairs
* @return reconstructed shape
*/
public Path2D makeFourierPairsReconstruction(int Mp) {
int M = G.length;
Mp = Math.min(Mp, M / 2);
int recPoints = Math.max(minReconstructionSamples, G.length * 3);
Path2D path = new Path2D.Float();
for (int i = 0; i < recPoints; i++) {
double t = (double) i / recPoints;
// assumes that coefficient 0 is never scaled
Complex pt = new Complex(getCoefficient(0));
// calculate a particular reconstruction point
for (int m = 1; m <= Mp; m++) {
Complex ep = getEllipsePoint(getCoefficient(-m), getCoefficient(m), m, t);
pt = pt.add(ep.multiply(reconstructionScale));
}
double xt = pt.re;
double yt = pt.im;
if (i == 0) {
path.moveTo(xt, yt);
} else {
path.lineTo(xt, yt);
}
}
path.closePath();
return path;
}
use of imagingbook.lib.math.Complex in project imagingbook-common by imagingbook.
the class FourierDescriptor method makeRotationInvariant.
private double makeRotationInvariant(int Mp) {
Complex z = new Complex(0, 0);
for (int m = 1; m <= Mp; m++) {
Complex Gm = getCoefficient(-m);
Complex Gp = getCoefficient(+m);
double w = 1.0 / m;
z = z.add(Gm.multiply(w));
z = z.add(Gp.multiply(w));
}
double beta = z.arg();
for (int m = 1; m <= Mp; m++) {
setCoefficient(-m, getCoefficient(-m).rotate(-beta));
setCoefficient(m, getCoefficient(m).rotate(-beta));
}
return beta;
}
use of imagingbook.lib.math.Complex in project imagingbook-common by imagingbook.
the class FourierDescriptor method getReconstructionPoint.
/**
* Reconstructs a single spatial point from this FD using
* coefficients [mm,...,mp] = [m-,...,m+] at the fractional path position t in [0,1].
*
* @param t path position
* @param mm most negative frequency index
* @param mp most positive frequency index
* @return single contour point
*/
private Complex getReconstructionPoint(double t, int mm, int mp) {
double x = G[0].re;
double y = G[0].im;
for (int m = mm; m <= mp; m++) {
if (m != 0) {
Complex Gm = getCoefficient(m);
double A = reconstructionScale * Gm.re;
double B = reconstructionScale * Gm.im;
double phi = 2 * Math.PI * m * t;
double sinPhi = Math.sin(phi);
double cosPhi = Math.cos(phi);
x = x + A * cosPhi - B * sinPhi;
y = y + A * sinPhi + B * cosPhi;
}
}
return new Complex(x, y);
}
use of imagingbook.lib.math.Complex in project imagingbook-common by imagingbook.
the class FourierDescriptor method makeEllipse.
// -----------------------------------------------------------------------
public Path2D makeEllipse(Complex G1, Complex G2, int m, double xOffset, double yOffset) {
Path2D path = new Path2D.Float();
int recPoints = Math.max(minReconstructionSamples, G.length * 3);
for (int i = 0; i < recPoints; i++) {
double t = (double) i / recPoints;
Complex p1 = this.getEllipsePoint(G1, G2, m, t);
double xt = p1.re;
double yt = p1.im;
if (i == 0) {
path.moveTo(xt + xOffset, yt + yOffset);
} else {
path.lineTo(xt + xOffset, yt + yOffset);
}
}
path.closePath();
return path;
}