| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Pierre Allard, |
| * Regent L'Archeveque - initial API and implementation |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.core.environment.surface.impl; |
| |
| import java.awt.Color; |
| import java.awt.Graphics2D; |
| import java.awt.geom.Ellipse2D; |
| import java.awt.image.BufferedImage; |
| |
| import javax.vecmath.Matrix4d; |
| import javax.vecmath.Vector3d; |
| |
| import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.apogy.common.images.ApogyCommonImagesFactory; |
| import org.eclipse.apogy.common.images.EImage; |
| import org.eclipse.apogy.common.images.EImagesUtilities; |
| import org.eclipse.apogy.common.math.ApogyCommonMathFacade; |
| import org.eclipse.apogy.common.math.Matrix4x4; |
| import org.eclipse.apogy.core.environment.surface.ApogySurfaceEnvironmentFactory; |
| import org.eclipse.apogy.core.environment.surface.ApogySurfaceEnvironmentPackage; |
| import org.eclipse.apogy.core.environment.surface.RectangularRegion; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| |
| public class EllipseShapeImageLayerCustomImpl extends EllipseShapeImageLayerImpl { |
| |
| @Override |
| public void dispose() { |
| } |
| |
| @Override |
| public void updateImage(IProgressMonitor progressMonitor) { |
| // First, find the extend of the area covered by the vertices. |
| RectangularRegion rectangularRegion = getImageMapLayerRegion(); |
| |
| // Updates image parameters. |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogySurfaceEnvironmentPackage.Literals.IMAGE_MAP_LAYER__WIDTH, rectangularRegion.getXDimension()); |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogySurfaceEnvironmentPackage.Literals.IMAGE_MAP_LAYER__HEIGHT, rectangularRegion.getYDimension()); |
| |
| // Generate a transparent image of the required size. |
| int width = (int) Math.round(rectangularRegion.getXDimension() / getRequiredResolution()); |
| int height = (int) Math.round(rectangularRegion.getYDimension() / getRequiredResolution()); |
| BufferedImage bufferedImage = EImagesUtilities.INSTANCE.createTransparentImage(width, height).asBufferedImage(); |
| |
| // Draw the polygon inside the image. |
| Graphics2D g2d = bufferedImage.createGraphics(); |
| g2d.setColor(getDrawingColor()); |
| |
| // Create the polygon. |
| Ellipse2D.Double ellipse = new Ellipse2D.Double(0, 0, width - 1, height - 1); |
| |
| g2d.draw(ellipse); |
| |
| if (isShowCenterLines()) { |
| if (Math.IEEEremainder(width, 2.0) == 0) { |
| int x1 = (int) Math.floor(width * 0.5f); |
| int x2 = x1 - 1; |
| |
| g2d.drawLine(x1, 0, x1, height); |
| g2d.drawLine(x2, 0, x2, height); |
| } else { |
| int xCenter = (int) Math.floor(width * 0.5f); |
| g2d.drawLine(xCenter, 0, xCenter, height); |
| } |
| |
| if (Math.IEEEremainder(height, 2.0) == 0) { |
| int y1 = Math.round(height * 0.5f); |
| int y2 = y1 - 1; |
| |
| g2d.drawLine(0, y1, width, y1); |
| g2d.drawLine(0, y2, width, y2); |
| } else { |
| int yCenter = (int) Math.floor(height * 0.5f); |
| g2d.drawLine(0, yCenter, width, yCenter); |
| } |
| } |
| |
| // Fills the polygon if required. |
| if (isShapedFilled()) { |
| g2d.fill(ellipse); |
| } |
| |
| g2d.dispose(); |
| |
| // Updates the image |
| EImage eImage = ApogyCommonImagesFactory.eINSTANCE.createEImage(); |
| eImage.setImageContent(bufferedImage); |
| |
| // Sets image. |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(this, |
| ApogySurfaceEnvironmentPackage.Literals.IMAGE_MAP_LAYER__IMAGE, eImage); |
| } |
| |
| @Override |
| public RectangularRegion getImageMapLayerRegion() { |
| RectangularRegion rectangularRegion = ApogySurfaceEnvironmentFactory.eINSTANCE.createRectangularRegion(); |
| rectangularRegion.setTransformation(ApogyCommonMathFacade.INSTANCE.createIdentityMatrix4x4()); |
| |
| if (getMap() != null && getMap().getTransformation() != null) { |
| Matrix4d m = getMap().getTransformation().asMatrix4d(); |
| |
| // Adds an offset of -ellipseWidth and -ellipseHeight to center the ellipse. |
| Matrix4d translation = new Matrix4d(); |
| translation.setIdentity(); |
| translation.set(new Vector3d(-getEllipseWidth() / 2.0, -getEllipseHeight() / 2.0, 0)); |
| |
| m.mul(translation); |
| |
| Matrix4x4 transform = ApogyCommonMathFacade.INSTANCE.createMatrix4x4(m); |
| rectangularRegion.setTransformation(transform); |
| } |
| |
| // Fills in the extent. |
| rectangularRegion.setXMin(-getEllipseWidth() / 2.0); |
| rectangularRegion.setXMax(getEllipseWidth() / 2.0); |
| rectangularRegion.setYMin(-getEllipseHeight() / 2.0); |
| rectangularRegion.setYMax(getEllipseHeight() / 2.0); |
| |
| return rectangularRegion; |
| } |
| |
| protected Color getDrawingColor() { |
| if (getColor() != null) { |
| if (getColor().x != -1 && getColor().y != -1 && getColor().z != -1) { |
| return new Color(getColor().x, getColor().y, getColor().z); |
| } else { |
| return null; |
| } |
| } else { |
| return null; |
| } |
| } |
| } // EllipseShapeImageLayerImpl |