Example usage for java.awt.geom AffineTransform AffineTransform

List of usage examples for java.awt.geom AffineTransform AffineTransform

Introduction

In this page you can find the example usage for java.awt.geom AffineTransform AffineTransform.

Prototype

public AffineTransform(double[] flatmatrix) 

Source Link

Document

Constructs a new AffineTransform from an array of double precision values representing either the 4 non-translation entries or the 6 specifiable entries of the 3x3 transformation matrix.

Usage

From source file:org.micromanager.plugins.magellan.coordinates.AffineCalibrator.java

private AffineTransform computeAffine(Point2D.Double pix1, Point2D.Double pix2, Point2D.Double pix3,
        Point2D.Double stage1, Point2D.Double stage2, Point2D.Double stage3) {
    //solve system x' = xA for A, x is stage points and x' is pixel points
    //6x6 matrix// w w w.j  a va2s. c o  m
    RealMatrix pixPoints = new Array2DRowRealMatrix(new double[][] { { pix1.x, pix1.y, 1, 0, 0, 0 },
            { 0, 0, 0, pix1.x, pix1.y, 1 }, { pix2.x, pix2.y, 1, 0, 0, 0 }, { 0, 0, 0, pix2.x, pix2.y, 1 },
            { pix3.x, pix3.y, 1, 0, 0, 0 }, { 0, 0, 0, pix3.x, pix3.y, 1 } });
    //6x1 matrix
    RealMatrix stagePoints = new Array2DRowRealMatrix(
            new double[] { stage1.x, stage1.y, stage2.x, stage2.y, stage3.x, stage3.y });
    //invert stagePoints matrix
    RealMatrix stagePointsInv = new LUDecomposition(pixPoints).getSolver().getInverse();
    RealMatrix A = stagePointsInv.multiply(stagePoints);
    AffineTransform transform = new AffineTransform(
            new double[] { A.getEntry(0, 0), A.getEntry(3, 0), A.getEntry(1, 0), A.getEntry(4, 0) });
    return transform;
}

From source file:de.uniluebeck.itm.spyglass.SpyglassEnvironment.java

/**
 * Returns the object used for affine operation within the drawing area
 * // ww w .j a  v  a2 s.  co  m
 * @return the object used for affine operation within the drawing area
 * @see DrawingArea
 */
public static AffineTransform getAffineTransformation() {
    final String s = String.valueOf(props.get(PROPERTY_CONFIG_AFFINE_TRANSFORM_MATRIX));
    final String[] matrix = ((s != null) && !s.equals("null")) ? s.split(",")
            : "1.0,0.0,0.0,1.0,0.0,1.0".split(",");
    final double[] flatmatrix = new double[6];

    for (int i = 0; i < matrix.length; i++) {
        flatmatrix[i] = Double.valueOf(matrix[i]);
    }

    return new AffineTransform(flatmatrix);
}

From source file:net.sf.maltcms.chromaui.annotations.PeakAnnotationRenderer.java

private void drawOutline(Shape entity, Graphics2D g2, Color fill, Color stroke, ChartPanel chartPanel,
        boolean scale, float alpha) {
    if (entity != null) {
        //System.out.println("Drawing entity with bbox: "+entity.getBounds2D());
        Shape savedClip = g2.getClip();
        Rectangle2D dataArea = chartPanel.getScreenDataArea();
        Color c = g2.getColor();/*  w w  w.  j  ava2s.c om*/
        Composite comp = g2.getComposite();
        g2.clip(dataArea);
        g2.setColor(fill);
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        JFreeChart chart = chartPanel.getChart();
        XYPlot plot = (XYPlot) chart.getPlot();
        ValueAxis xAxis = plot.getDomainAxis();
        ValueAxis yAxis = plot.getRangeAxis();
        RectangleEdge xAxisEdge = plot.getDomainAxisEdge();
        RectangleEdge yAxisEdge = plot.getRangeAxisEdge();
        Rectangle2D entityBounds = entity.getBounds2D();
        double viewX = xAxis.valueToJava2D(entityBounds.getCenterX(), dataArea, xAxisEdge);
        double viewY = yAxis.valueToJava2D(entityBounds.getCenterY(), dataArea, yAxisEdge);
        double viewW = xAxis.lengthToJava2D(entityBounds.getWidth(), dataArea, xAxisEdge);
        double viewH = yAxis.lengthToJava2D(entityBounds.getHeight(), dataArea, yAxisEdge);
        PlotOrientation orientation = plot.getOrientation();

        //transform model to origin (0,0) in model coordinates
        AffineTransform toOrigin = AffineTransform.getTranslateInstance(-entityBounds.getCenterX(),
                -entityBounds.getCenterY());
        //transform from origin (0,0) to model location
        AffineTransform toModelLocation = AffineTransform.getTranslateInstance(entityBounds.getCenterX(),
                entityBounds.getCenterY());
        //transform from model scale to view scale
        double scaleX = viewW / entityBounds.getWidth();
        double scaleY = viewH / entityBounds.getHeight();
        Logger.getLogger(getClass().getName()).log(Level.FINE, "Scale x: {0} Scale y: {1}",
                new Object[] { scaleX, scaleY });
        AffineTransform toViewScale = AffineTransform.getScaleInstance(scaleX, scaleY);
        AffineTransform toViewLocation = AffineTransform.getTranslateInstance(viewX, viewY);
        AffineTransform flipTransform = AffineTransform.getScaleInstance(1.0f, -1.0f);
        AffineTransform modelToView = new AffineTransform(toOrigin);
        modelToView.preConcatenate(flipTransform);
        modelToView.preConcatenate(toViewScale);
        modelToView.preConcatenate(toViewLocation);
        //
        //            if (orientation == PlotOrientation.HORIZONTAL) {
        //                entity = ShapeUtilities.createTranslatedShape(entity, viewY,
        //                        viewX);
        //            } else if (orientation == PlotOrientation.VERTICAL) {
        //                entity = ShapeUtilities.createTranslatedShape(entity, viewX,
        //                        viewY);
        //            }
        FlatteningPathIterator iter = new FlatteningPathIterator(modelToView.createTransformedShape(entity)
                .getPathIterator(AffineTransform.getTranslateInstance(0, 0)), 5);
        Path2D.Float path = new Path2D.Float();
        path.append(iter, false);

        g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
        g2.fill(path);
        if (stroke != null) {
            g2.setColor(stroke);
            g2.draw(path);
        }
        g2.setComposite(comp);
        g2.setColor(c);
        g2.setClip(savedClip);
    } else {
        Logger.getLogger(getClass().getName()).info("Entity is null!");
    }
}

From source file:org.micromanager.plugins.magellan.propsandcovariants.SurfaceData.java

/**
  *//from ww w.  j av  a  2 s  .  co  m
  * @param corners
  * @param min true to get min, false to get max
  * @return {minDistance,maxDistance, minNormalAngle, maxNormalAngle)
  */
private double[] distanceAndNormalCalc(Point2D.Double[] corners, double zVal) throws InterruptedException {
    //check a grid of points spanning entire position        
    //square is aligned with axes in pixel space, so convert to pixel space to generate test points
    double xSpan = corners[2].getX() - corners[0].getX();
    double ySpan = corners[2].getY() - corners[0].getY();
    Point2D.Double pixelSpan = new Point2D.Double();
    AffineTransform transform = AffineUtils.getAffineTransform(surface_.getCurrentPixelSizeConfig(), 0, 0);
    try {
        transform.inverseTransform(new Point2D.Double(xSpan, ySpan), pixelSpan);
    } catch (NoninvertibleTransformException ex) {
        Log.log("Problem inverting affine transform");
    }
    double minDistance = Integer.MAX_VALUE;
    double maxDistance = 0;
    double minNormalAngle = 90;
    double maxNormalAngle = 0;
    for (double x = 0; x <= pixelSpan.x; x += pixelSpan.x / (double) NUM_XY_TEST_POINTS) {
        for (double y = 0; y <= pixelSpan.y; y += pixelSpan.y / (double) NUM_XY_TEST_POINTS) {
            //convert these abritray pixel coordinates back to stage coordinates
            double[] transformMaxtrix = new double[6];
            transform.getMatrix(transformMaxtrix);
            transformMaxtrix[4] = corners[0].getX();
            transformMaxtrix[5] = corners[0].getY();
            //create new transform with translation applied
            transform = new AffineTransform(transformMaxtrix);
            Point2D.Double stageCoords = new Point2D.Double();
            transform.transform(new Point2D.Double(x, y), stageCoords);
            //test point for inclusion of position
            if (!surface_.waitForCurentInterpolation().isInterpDefined(stageCoords.x, stageCoords.y)) {
                //if position is outside of convex hull, assume min distance is 0
                minDistance = 0;
                //get extrapolated value for max distance
                float interpVal = surface_.getExtrapolatedValue(stageCoords.x, stageCoords.y);
                maxDistance = Math.max(zVal - interpVal, maxDistance);
                //only take actual values for normals
            } else {
                float interpVal = surface_.waitForCurentInterpolation().getInterpolatedValue(stageCoords.x,
                        stageCoords.y);
                float normalAngle = surface_.waitForCurentInterpolation()
                        .getNormalAngleToVertical(stageCoords.x, stageCoords.y);
                minDistance = Math.min(Math.max(0, zVal - interpVal), minDistance);
                maxDistance = Math.max(zVal - interpVal, maxDistance);
                minNormalAngle = Math.min(minNormalAngle, normalAngle);
                maxNormalAngle = Math.max(maxNormalAngle, normalAngle);
            }
        }
    }
    return new double[] { minDistance, maxDistance, minNormalAngle, maxNormalAngle };
}

From source file:org.micromanager.plugins.magellan.surfacesandregions.SurfaceInterpolator.java

/**
 * test whether XY position is completely abve or completely below surface
 * @throws InterruptedException //from   w ww .  j a v a2 s. com
 */
public boolean testPositionRelativeToSurface(XYStagePosition pos, SurfaceInterpolator surface, double zPos,
        int mode, boolean extrapolate) throws InterruptedException {
    //get the corners with padding added in
    Point2D.Double[] corners = getPositionCornersWithPadding(pos, surface.xyPadding_um_);
    //First check position corners before going into a more detailed set of test points
    for (Point2D.Double point : corners) {
        float interpVal;
        if (!surface.waitForCurentInterpolation().isInterpDefined(point.x, point.y)) {
            if (extrapolate) {
                interpVal = surface.getExtrapolatedValue(point.x, point.y);
            } else {
                continue;
            }
        } else {
            interpVal = surface.waitForCurentInterpolation().getInterpolatedValue(point.x, point.y);
        }
        if ((towardsSampleIsPositive_ && mode == ABOVE_SURFACE && zPos >= interpVal)
                || (towardsSampleIsPositive_ && mode == BELOW_SURFACE && zPos <= interpVal)
                || (!towardsSampleIsPositive_ && mode == ABOVE_SURFACE && zPos <= interpVal)
                || (!towardsSampleIsPositive_ && mode == BELOW_SURFACE) && zPos >= interpVal) {
            return false;
        }
    }
    //then check a grid of points spanning entire position        
    //9x9 square of points to check for each position
    //square is aligned with axes in pixel space, so convert to pixel space to generate test points
    double xSpan = corners[2].getX() - corners[0].getX();
    double ySpan = corners[2].getY() - corners[0].getY();
    Point2D.Double pixelSpan = new Point2D.Double();
    AffineTransform transform = AffineUtils.getAffineTransform(getCurrentPixelSizeConfig(), 0, 0);
    try {
        transform.inverseTransform(new Point2D.Double(xSpan, ySpan), pixelSpan);
    } catch (NoninvertibleTransformException ex) {
        Log.log("Problem inverting affine transform");
    }
    outerloop: for (double x = 0; x <= pixelSpan.x; x += pixelSpan.x / (double) NUM_XY_TEST_POINTS) {
        for (double y = 0; y <= pixelSpan.y; y += pixelSpan.y / (double) NUM_XY_TEST_POINTS) {
            //convert these abritray pixel coordinates back to stage coordinates
            double[] transformMaxtrix = new double[6];
            transform.getMatrix(transformMaxtrix);
            transformMaxtrix[4] = corners[0].getX();
            transformMaxtrix[5] = corners[0].getY();
            //create new transform with translation applied
            transform = new AffineTransform(transformMaxtrix);
            Point2D.Double stageCoords = new Point2D.Double();
            transform.transform(new Point2D.Double(x, y), stageCoords);
            //test point for inclusion of position
            float interpVal;
            if (!surface.waitForCurentInterpolation().isInterpDefined(stageCoords.x, stageCoords.y)) {
                if (extrapolate) {
                    interpVal = surface.getExtrapolatedValue(stageCoords.x, stageCoords.y);
                } else {
                    continue;
                }
            } else {
                interpVal = surface.waitForCurentInterpolation().getInterpolatedValue(stageCoords.x,
                        stageCoords.y);
            }
            if ((towardsSampleIsPositive_ && mode == ABOVE_SURFACE && zPos >= interpVal)
                    || (towardsSampleIsPositive_ && mode == BELOW_SURFACE && zPos <= interpVal)
                    || (!towardsSampleIsPositive_ && mode == ABOVE_SURFACE && zPos <= interpVal)
                    || (!towardsSampleIsPositive_ && mode == BELOW_SURFACE) && zPos >= interpVal) {
                return false;
            }
        }
    }
    return true;
}

From source file:edu.valelab.gaussianfit.datasettransformations.CoordinateMapper.java

public static void logAffineTransform(AffineTransform af) {
    AffineTransform tmp = new AffineTransform(af);
    try {//from  w ww.  ja va2 s  .  c om
        AffineTransform inv = tmp.createInverse();
        ij.IJ.log(inv.toString());
    } catch (NoninvertibleTransformException ex) {
        ReportingUtils.logError(ex, "Problem while printing affine transform");
    }
}

From source file:org.apache.fop.render.pcl.PCLRenderer.java

/**
 * {@inheritDoc}/*from   w w  w .  ja  va 2s .co m*/
 */
protected void startVParea(CTM ctm, Rectangle2D clippingRect) {
    saveGraphicsState();
    AffineTransform at = new AffineTransform(ctm.toArray());
    graphicContext.transform(at);
    changePrintDirection();
    if (log.isDebugEnabled()) {
        log.debug("startVPArea: " + at + " --> " + graphicContext.getTransform());
    }
}

From source file:org.apache.fop.render.intermediate.AbstractIFPainter.java

/**
 * Converts a transformation matrix from millipoints to points.
 * @param transform the transformation matrix (in millipoints)
 * @return the converted transformation matrix (in points)
 *///from   w  w  w.j  a  va  2  s .  c om
public static AffineTransform toPoints(AffineTransform transform) {
    final double[] matrix = new double[6];
    transform.getMatrix(matrix);
    //Convert from millipoints to points
    matrix[4] /= 1000;
    matrix[5] /= 1000;
    return new AffineTransform(matrix);
}

From source file:JXTransformer.java

/**
 * Never returns null
 */
public AffineTransform getTransform() {
    return new AffineTransform(at);
}

From source file:JXTransformer.java

public void setTransform(AffineTransform at) {
    if (at == null) {
        throw new IllegalArgumentException("AffineTransform is null");
    }//www  .  ja v  a  2 s  .c  o  m
    this.at = new AffineTransform(at);
    revalidate();
    repaint();
}