/** * Calculate the intrinsic scale of the shape. This is the RMS distance of * all the points from the centroid. * * @return the scale of the object. */ public float computeIntrinsicScale() { final Point2d cog = this.calculateCentroid(); float scale = 0; for (final Point2d pt : this) { final double x = pt.getX() - cog.getX(); final double y = pt.getY() - cog.getY(); scale += x * x + y * y; } return (float) Math.sqrt(scale / points.size()); }
/** * Scale the {@link PointList} about its centre of gravity. Scalefactors * between 0 and 1 shrink the {@link PointList}. * * @param sc * the scale factor */ @Override public void scaleCentroid(float sc) { final Point2d cog = calculateCentroid(); this.translate(-cog.getX(), -cog.getY()); this.scale(sc); this.translate(cog.getX(), cog.getY()); }
Point2d cog = toAlign.calculateCentroid(); Matrix trans = TransformUtilities.translateToPointMatrix(cog, this.referenceCog); toAlign.translate((float)trans.get(0,2), (float)trans.get(1,2));
/** * Construct the {@link ProcrustesAnalysis} with the given * reference shape. The reference shape is optionally normalised * to a standardised scale and translated to the origin. * * @param reference The reference shape. * @param normalise if true, then the reference is normalised (changing * the reference shape itself). */ public ProcrustesAnalysis(PointList reference, boolean normalise) { this.reference = reference; referenceCog = reference.calculateCentroid(); scaling = computeScale(reference, referenceCog.getX(), referenceCog.getY()); if (normalise) { reference.translate(-referenceCog.getX(), -referenceCog.getY()); reference.scale((float) scaling); referenceCog.setX(0); referenceCog.setY(0); scaling = 1; } }
protected static PointList alignPointsAndAverage(List<PointList> shapes, PointList reference, double referenceScaling, Point2d referenceCog) { ProcrustesAnalysis pa = new ProcrustesAnalysis(reference); for (PointList shape : shapes) { pa.align(shape); } PointList mean = PointList.computeMean(shapes); //normalise translation to reference Point2d cog = mean.calculateCentroid(); Matrix trans = TransformUtilities.translateToPointMatrix(cog, referenceCog); mean.translate((float)trans.get(0,2), (float)trans.get(1,2)); //normalise scaling to reference double scale = ProcrustesAnalysis.computeScale(mean, referenceCog.getX(), referenceCog.getY()); float sf = (float)(scale / referenceScaling); mean.scale(referenceCog, sf); return mean; }