RE: [OpenMap Users] Rotation in openmap

From: Chapman, Martin <MChapman@sanz.com>
Date: Thu May 13 2004 - 11:44:25 EDT

Arun,

Use the OMScalingRaster class. There is a setRotationAngle() function
or something like that. It has issues though. The image would float
around the map when I zoomed in and out so I made the following changes
to the OMRasterObject class. Basically, the original code was doing a
affine translation before the rotation, which gives different results
than if you do a translation before a rotation. So I changed the code
to apply both transforms at once during construction of the
AffineTransform object...see the code below from the OMRasterObject:
You'll have to calculate the slope of your line though and use that as
the angle parameter.

public void render(Graphics g) {
        if (getNeedToRegenerate() || getNeedToReposition() ||
!isVisible()) {
            if (DEBUG) Debug.output("EWRasterObject.render(): need to
regenerate or not visible!");
            return;
        }

        //copy the graphic, so our transform doesn't cascade to
others...
        g = g.create();

        // Just a little check to find out if someone is rushing
        // things. If a Image isn't fully loaded, the getWidth will
        // return -1. This is just a courtesy notification in case
        // someone isn't seeing their image, and don't know why.
        if (colorModel == COLORMODEL_IMAGEICON && (getWidth() == -1)) {
            Debug.error("EWRasterObject.render: Attempting to draw a
Image that is not ready! Image probably wasn't available.");
        }

        if (bitmap != null)
        {
            if (isMatted())
            {
                super.render(g);
            }

                // I ADDED THIS
            AffineTransform at = new
AffineTransform(Math.cos(rotationAngle),
 
Math.sin(rotationAngle),
 
-Math.sin(rotationAngle),
 
Math.cos(rotationAngle),
                                                     point1.x,
                                                     point1.y);

                "THIS IS ORIGINAL CODE
            //if (g instanceof Graphics2D && rotationAngle !=
DEFAULT_ROTATIONANGLE)
           // {
                //rotate about our image center point
            // ( (Graphics2D) g).rotate(rotationAngle, point1.x +
width / 2, point1.y + height / 2);
           // }

            if (DEBUG)
            {
                Debug.output("EWRasterObject.render() | drawing " +
                             width + "x" + height + " image at " +
                             point1.x + ", " + point1.y);
            }

            if (g instanceof Graphics2D && bitmap instanceof
RenderedImage)
            {
                // Affine translation for placement...
                ((Graphics2D)g).drawRenderedImage((BufferedImage)bitmap,
at);

                // Undo the affine translation for future graphics??
                //((Graphics2D)g).translate(-point1.x, -point1.y);
            }
            else
            {
                g.drawImage(bitmap, point1.x, point1.y, this);
            }
        }
        else
        {
            if (DEBUG) Debug.output("EWRasterObject.render: ignoring
null bitmap");
        }

        if (Debug.debugging("rasterobjects")) {
            super.render(g);
        }
    }

Also I made the following change to OMScalingRaster class because
OpenMap was clipping the image wrong.

protected void scaleTo(Projection thisProj) {

        if (DEBUG) Debug.output("EWScalingRaster: scaleTo()");

        if (sourceImage == null) {
            Debug.output("EWScalingRaster.scaleTo() sourceImage is
null");
            return;
        }

        // Get the projection window rectangle in pix
        Rectangle winRect = new Rectangle(thisProj.getWidth(),
                                          thisProj.getHeight());

        // Get image projection rectangle
        Rectangle projRect = new Rectangle();
        //projRect.setLocation(point1);
        projRect.setRect(point1.getX(), point1.getY(), point2.getX() -
point1.getX(), point2.getY() - point1.getY());

        Rectangle sourceRect = new Rectangle();
        sourceRect.width = sourceImage.getWidth();
        sourceRect.height = sourceImage.getHeight();

        // Now we have everything we need to sort out this new
        // projection.
        setVisible(false); // Assume we wont see it
        clipRect = null;

        Rectangle iRect = winRect.intersection(projRect);
        if (!iRect.isEmpty()) {
            // Now we have the visible rectangle of the projected
            // image we need to figure out which pixels from the
            // source image get scaled to produce it.

            // Assume will need whole image, set the clipRect so it's
            // on the map, somewhere.
            clipRect = new Rectangle();
            clipRect.setBounds(sourceRect);

            // If big enough to see
            if ((iRect.width >= 1) && (iRect.height >= 1)) {

                // If it didn't all fit
                /*if (!winRect.contains(projRect)) {
                    // calc X scale factor
                    float xScaleFactor = (float)sourceRect.width /
(float)projRect.width;
                    // and Y scale factor
                    float yScaleFactor = (float)sourceRect.height /
(float)projRect.height;
                    int xOffset = (int)((iRect.x - projRect.x)); //
and x offset
                    int yOffset = (int)((iRect.y - projRect.y)); //
and y offset
                    clipRect.x = (int)(xOffset * xScaleFactor); //
scale the x position
                    clipRect.y = (int)(yOffset * yScaleFactor); //
scale the y position
                    clipRect.width = (int)(iRect.width * xScaleFactor);
// scale the width
                    clipRect.height = (int)(iRect.height *
yScaleFactor); // scale the height
                }*/
                // Now we can grab the bit we want out of the source and
                // scale it to fit the intersection.

                // Calc width adjustment
                double widthAdj = (point2.getX() - point1.getX()) /
(double)clipRect.width;
                // Calc height adjustment
                double heightAdj = (point2.getY() - point1.getY()) /
(double)clipRect.height;
                // Create the transform
                AffineTransform xform = new AffineTransform(widthAdj,
0.0, 0.0, heightAdj, 0.0, 0.0);
                // Create the transform op.
                AffineTransformOp xformOp = new AffineTransformOp(xform,
AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
                // Scale clip area -> newImage
                // extract sub-image
                BufferedImage newImage =
xformOp.filter(sourceImage.getSubimage(clipRect.x, clipRect.y,
clipRect.width, clipRect.height), null);
                bitmap = newImage;
                //point1.setLocation(iRect.x, iRect.y);
                setVisible(true);
            }
        }
    }

Hope that works!

Martin
 

-----Original Message-----
From: Trainee AES, (IE10) [mailto:trainee.aes@honeywell.com]
Sent: Thursday, May 13, 2004 6:34 AM
To: 'openmap-users@bbn.com'
Subject: [OpenMap Users] Rotation in openmap

Hi Everyone,
   How can i rotate an image through a line, line can be drawn
anywhere in the map, and according to the line i have to rotate my
image. My basic aim is to move image in the line direction.

Regards,
Arun Jaipuria.

--
[To unsubscribe to this list send an email to "majdart@bbn.com" with the
following text in the BODY of the message "unsubscribe openmap-users"]
--
[To unsubscribe to this list send an email to "majdart@bbn.com"
with the following text in the BODY of the message "unsubscribe openmap-users"]
Received on Thu May 13 11:45:26 2004

This archive was generated by hypermail 2.1.8 : Thu May 12 2005 - 07:18:38 EDT