Search in sources :

Example 1 with ScreenPosition

use of de.fhpotsdam.unfolding.utils.ScreenPosition in project constellation by constellation-app.

the class AbstractPathsLayer method update.

@Override
public PImage update() {
    if (graph == null) {
        return null;
    }
    final Set<Marker> onScreenMarkers;
    final List<Tuple<GraphElement, GraphElement>> paths = new ArrayList<>();
    try (final ReadableGraph readableGraph = graph.getReadableGraph()) {
        // update on screen markers, collecting the ids of the vertices involved in valid paths along the way
        final ScreenPosition topLeft = map.getScreenPosition(map.getTopLeftBorder());
        final ScreenPosition bottomRight = map.getScreenPosition(map.getBottomRightBorder());
        onScreenMarkers = renderer.getMarkerCache().keys().stream().filter(marker -> {
            final ScreenPosition markerPosition = map.getScreenPosition(marker.getLocation());
            final boolean onScreen = markerPosition != null && markerPosition.x > topLeft.x && markerPosition.y > topLeft.y && markerPosition.x < bottomRight.x && markerPosition.y < bottomRight.y;
            if (drawPathsToOffscreenMarkers() || onScreen) {
                final Set<GraphElement> elementsAtMarker = renderer.getMarkerCache().get(marker);
                if (elementsAtMarker != null) {
                    elementsAtMarker.forEach(element -> paths.addAll(getPathsForElement(readableGraph, element)));
                }
            }
            return onScreen;
        }).collect(Collectors.toSet());
        onScreenMarkerCount = onScreenMarkers.size();
    }
    if (onScreenMarkers.isEmpty()) {
        return null;
    }
    final Map<GraphElement, Marker> elementToMarkerCache = new HashMap<>();
    renderer.getMarkerCache().keys().forEach(marker -> renderer.getMarkerCache().get(marker).forEach(element -> elementToMarkerCache.put(element, marker)));
    // set up a color palette
    final int[] palette = Arrays.asList(ConstellationColor.createLinearPalette(N_COLORS, SRC_COLOR, DST_COLOR)).stream().mapToInt(c -> MarkerUtilities.color(c)).toArray();
    final int width = renderer.width - 5;
    final int height = renderer.height - 5;
    final PGraphics pathsImage = renderer.createGraphics(width, height, PConstants.JAVA2D);
    pathsImage.beginDraw();
    // deduplicate paths, storing duplicate counts
    int maxWeight = 1;
    final Map<Tuple<GraphElement, GraphElement>, Integer> dedupedPaths = new HashMap<>();
    for (final Tuple<GraphElement, GraphElement> path : paths) {
        if (dedupedPaths.containsKey(path)) {
            final int weight = dedupedPaths.get(path) + 1;
            if (weight > maxWeight) {
                maxWeight = weight;
            }
            dedupedPaths.put(path, weight);
        } else {
            dedupedPaths.put(path, 1);
        }
    }
    // draw weighted paths
    final int maxWeightFinal = maxWeight;
    dedupedPaths.forEach((path, weight) -> {
        final Marker sourceMarker = elementToMarkerCache.get(path.getFirst());
        final Marker destinationMarker = elementToMarkerCache.get(path.getSecond());
        final boolean validPath = (drawPathsToOffscreenMarkers() && (onScreenMarkers.contains(sourceMarker) || onScreenMarkers.contains(destinationMarker))) || (onScreenMarkers.contains(sourceMarker) && onScreenMarkers.contains(destinationMarker));
        if (validPath) {
            final Location sourceLocation = sourceMarker != null ? sourceMarker.getLocation() : null;
            final Location destinationLocation = destinationMarker != null ? destinationMarker.getLocation() : null;
            if (sourceLocation != null && destinationLocation != null) {
                final ScreenPosition sourcePosition = map.getScreenPosition(sourceLocation);
                final ScreenPosition destinationPosition = map.getScreenPosition(destinationLocation);
                final float lineWidth = Math.max(maxWeightFinal > MAX_LINE_WIDTH ? (MAX_LINE_WIDTH * (weight / (float) maxWeightFinal)) + 1 : weight + 1, 2);
                pathsImage.strokeWeight(lineWidth);
                pathsImage.pushMatrix();
                pathsImage.translate(sourcePosition.x, sourcePosition.y);
                pathsImage.rotate(PApplet.atan2((destinationPosition.y - sourcePosition.y), (destinationPosition.x - sourcePosition.x)));
                final float translatedDestiniationPosition = (float) Math.hypot(destinationPosition.x - sourcePosition.x, destinationPosition.y - sourcePosition.y);
                drawColoredLine(pathsImage, translatedDestiniationPosition, lineWidth, palette);
                pathsImage.popMatrix();
            }
        }
    });
    pathsImage.endDraw();
    return pathsImage;
}
Also used : Arrays(java.util.Arrays) Tuple(au.gov.asd.tac.constellation.utilities.datastructure.Tuple) MarkerUtilities(au.gov.asd.tac.constellation.views.mapview.utilities.MarkerUtilities) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) ConstellationColor(au.gov.asd.tac.constellation.utilities.color.ConstellationColor) PApplet(processing.core.PApplet) GraphElement(au.gov.asd.tac.constellation.views.mapview.utilities.GraphElement) Set(java.util.Set) HashMap(java.util.HashMap) PGraphics(processing.core.PGraphics) Marker(de.fhpotsdam.unfolding.marker.Marker) PImage(processing.core.PImage) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) PConstants(processing.core.PConstants) List(java.util.List) Location(de.fhpotsdam.unfolding.geo.Location) Map(java.util.Map) ScreenPosition(de.fhpotsdam.unfolding.utils.ScreenPosition) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) HashMap(java.util.HashMap) PGraphics(processing.core.PGraphics) ArrayList(java.util.ArrayList) Marker(de.fhpotsdam.unfolding.marker.Marker) ScreenPosition(de.fhpotsdam.unfolding.utils.ScreenPosition) GraphElement(au.gov.asd.tac.constellation.views.mapview.utilities.GraphElement) Tuple(au.gov.asd.tac.constellation.utilities.datastructure.Tuple) Location(de.fhpotsdam.unfolding.geo.Location)

Example 2 with ScreenPosition

use of de.fhpotsdam.unfolding.utils.ScreenPosition in project constellation by constellation-app.

the class DayNightLayer method update.

@Override
public PImage update() {
    final int width = renderer.width - 5;
    final int height = renderer.height - 5;
    final PGraphics dayNightImage = renderer.createGraphics(width, height, PConstants.JAVA2D);
    dayNightImage.beginDraw();
    final long currentTime = System.currentTimeMillis();
    final Location sunLocation = getSunPosition(currentTime);
    final ScreenPosition sunPosition = map.getScreenPosition(sunLocation);
    final Location leftShadowLocation = getShadowPosition(sunLocation, ShadowOrientation.LEFT);
    final Location rightShadowLocation = getShadowPosition(sunLocation, ShadowOrientation.RIGHT);
    // draw the sun
    dayNightImage.stroke(STROKE_COLOR);
    dayNightImage.fill(SUN_COLOR);
    dayNightImage.ellipse(sunPosition.x, sunPosition.y, 10, 10);
    // calculate shadow radius
    final float twighlightCivilRadiusMeters = getShadowRadiusFromAngle(0.566666);
    final float twighlightNauticalRadiusMeters = getShadowRadiusFromAngle(6.0);
    final float twighlightAstronomicalRadiusMeters = getShadowRadiusFromAngle(12.0);
    final float nightRadiusMeters = getShadowRadiusFromAngle(18.0);
    // left twilight civil
    final Location leftTwighlightCivilRadiusLocation = new Location(leftShadowLocation.getLat() - Distance.Haversine.kilometersToDecimalDegrees(twighlightCivilRadiusMeters / 1000), leftShadowLocation.getLon() - Distance.Haversine.kilometersToDecimalDegrees(twighlightCivilRadiusMeters / 1000));
    final List<Location> leftTwighlightCivilLocations = MarkerUtilities.generateCircle(leftShadowLocation, leftTwighlightCivilRadiusLocation);
    final List<MapPosition> leftTwighlightCivilPositions = leftTwighlightCivilLocations.stream().map(location -> new MapPosition(map.mapDisplay.getObjectFromLocation(location))).collect(Collectors.toList());
    dayNightImage.noStroke();
    dayNightImage.fill(TWIGHLIGHT_CIVIL_COLOR);
    dayNightImage.beginShape();
    leftTwighlightCivilPositions.forEach(position -> dayNightImage.vertex(position.x, position.y));
    dayNightImage.endShape(PConstants.CLOSE);
    // left twilight nautical
    final Location leftTwighlightNauticalRadiusLocation = new Location(leftShadowLocation.getLat() - Distance.Haversine.kilometersToDecimalDegrees(twighlightNauticalRadiusMeters / 1000), leftShadowLocation.getLon() - Distance.Haversine.kilometersToDecimalDegrees(twighlightNauticalRadiusMeters / 1000));
    final List<Location> leftTwighlightNauticalLocations = MarkerUtilities.generateCircle(leftShadowLocation, leftTwighlightNauticalRadiusLocation);
    final List<MapPosition> leftTwighlightNauticalPositions = leftTwighlightNauticalLocations.stream().map(location -> new MapPosition(map.mapDisplay.getObjectFromLocation(location))).collect(Collectors.toList());
    dayNightImage.noStroke();
    dayNightImage.fill(TWIGHLIGHT_NAUTICAL_COLOR);
    dayNightImage.beginShape();
    leftTwighlightNauticalPositions.forEach(position -> dayNightImage.vertex(position.x, position.y));
    dayNightImage.endShape(PConstants.CLOSE);
    // left twilight astronomical
    final Location leftTwighlightAstronomicalRadiusLocation = new Location(leftShadowLocation.getLat() + Distance.Haversine.kilometersToDecimalDegrees(twighlightAstronomicalRadiusMeters / 1000), leftShadowLocation.getLon() + Distance.Haversine.kilometersToDecimalDegrees(twighlightAstronomicalRadiusMeters / 1000));
    final List<Location> leftTwighlightAstronomicalLocations = MarkerUtilities.generateCircle(leftShadowLocation, leftTwighlightAstronomicalRadiusLocation);
    final List<MapPosition> leftTwighlightAstronomicalPositions = leftTwighlightAstronomicalLocations.stream().map(location -> new MapPosition(map.mapDisplay.getObjectFromLocation(location))).collect(Collectors.toList());
    dayNightImage.noStroke();
    dayNightImage.fill(TWIGHLIGHT_ASTRONOMICAL_COLOR);
    dayNightImage.beginShape();
    leftTwighlightAstronomicalPositions.forEach(position -> dayNightImage.vertex(position.x, position.y));
    dayNightImage.endShape(PConstants.CLOSE);
    // left night
    final Location leftNightRadiusLocation = new Location(leftShadowLocation.getLat() + Distance.Haversine.kilometersToDecimalDegrees(nightRadiusMeters / 1000), leftShadowLocation.getLon() + Distance.Haversine.kilometersToDecimalDegrees(nightRadiusMeters / 1000));
    final List<Location> leftNightLocations = MarkerUtilities.generateCircle(leftShadowLocation, leftNightRadiusLocation);
    final List<MapPosition> leftNightPositions = leftNightLocations.stream().map(location -> new MapPosition(map.mapDisplay.getObjectFromLocation(location))).collect(Collectors.toList());
    dayNightImage.noStroke();
    dayNightImage.fill(NIGHT_COLOR);
    dayNightImage.beginShape();
    leftNightPositions.forEach(position -> dayNightImage.vertex(position.x, position.y));
    dayNightImage.endShape(PConstants.CLOSE);
    // right twilight civil
    final Location rightTwighlightCivilRadiusLocation = new Location(rightShadowLocation.getLat() - Distance.Haversine.kilometersToDecimalDegrees(twighlightCivilRadiusMeters / 1000), rightShadowLocation.getLon() - Distance.Haversine.kilometersToDecimalDegrees(twighlightCivilRadiusMeters / 1000));
    final List<Location> rightTwighlightCivilLocations = MarkerUtilities.generateCircle(rightShadowLocation, rightTwighlightCivilRadiusLocation);
    final List<MapPosition> rightTwighlightCivilPositions = rightTwighlightCivilLocations.stream().map(location -> new MapPosition(map.mapDisplay.getObjectFromLocation(location))).collect(Collectors.toList());
    dayNightImage.noStroke();
    dayNightImage.fill(TWIGHLIGHT_CIVIL_COLOR);
    dayNightImage.beginShape();
    rightTwighlightCivilPositions.forEach(position -> dayNightImage.vertex(position.x, position.y));
    dayNightImage.endShape(PConstants.CLOSE);
    // right twilight nautical
    final Location rightTwighlightNauticalRadiusLocation = new Location(rightShadowLocation.getLat() - Distance.Haversine.kilometersToDecimalDegrees(twighlightNauticalRadiusMeters / 1000), rightShadowLocation.getLon() - Distance.Haversine.kilometersToDecimalDegrees(twighlightNauticalRadiusMeters / 1000));
    final List<Location> rightTwighlightNauticalLocations = MarkerUtilities.generateCircle(rightShadowLocation, rightTwighlightNauticalRadiusLocation);
    final List<MapPosition> rightTwighlightNauticalPositions = rightTwighlightNauticalLocations.stream().map(location -> new MapPosition(map.mapDisplay.getObjectFromLocation(location))).collect(Collectors.toList());
    dayNightImage.noStroke();
    dayNightImage.fill(TWIGHLIGHT_NAUTICAL_COLOR);
    dayNightImage.beginShape();
    rightTwighlightNauticalPositions.forEach(position -> dayNightImage.vertex(position.x, position.y));
    dayNightImage.endShape(PConstants.CLOSE);
    // right twilight astronomical
    final Location rightTwighlightAstronomicalRadiusLocation = new Location(rightShadowLocation.getLat() + Distance.Haversine.kilometersToDecimalDegrees(twighlightAstronomicalRadiusMeters / 1000), rightShadowLocation.getLon() + Distance.Haversine.kilometersToDecimalDegrees(twighlightAstronomicalRadiusMeters / 1000));
    final List<Location> rightTwighlightAstronomicalLocations = MarkerUtilities.generateCircle(rightShadowLocation, rightTwighlightAstronomicalRadiusLocation);
    final List<MapPosition> rightTwighlightAstronomicalPositions = rightTwighlightAstronomicalLocations.stream().map(location -> new MapPosition(map.mapDisplay.getObjectFromLocation(location))).collect(Collectors.toList());
    dayNightImage.noStroke();
    dayNightImage.fill(TWIGHLIGHT_ASTRONOMICAL_COLOR);
    dayNightImage.beginShape();
    rightTwighlightAstronomicalPositions.forEach(position -> dayNightImage.vertex(position.x, position.y));
    dayNightImage.endShape(PConstants.CLOSE);
    // right night
    final Location rightNightRadiusLocation = new Location(rightShadowLocation.getLat() + Distance.Haversine.kilometersToDecimalDegrees(nightRadiusMeters / 1000), rightShadowLocation.getLon() + Distance.Haversine.kilometersToDecimalDegrees(nightRadiusMeters / 1000));
    final List<Location> rightNightLocations = MarkerUtilities.generateCircle(rightShadowLocation, rightNightRadiusLocation);
    final List<MapPosition> rightNightPositions = rightNightLocations.stream().map(location -> new MapPosition(map.mapDisplay.getObjectFromLocation(location))).collect(Collectors.toList());
    dayNightImage.noStroke();
    dayNightImage.fill(NIGHT_COLOR);
    dayNightImage.beginShape();
    rightNightPositions.forEach(position -> dayNightImage.vertex(position.x, position.y));
    dayNightImage.endShape(PConstants.CLOSE);
    dayNightImage.endDraw();
    return dayNightImage;
}
Also used : PConstants(processing.core.PConstants) Distance(au.gov.asd.tac.constellation.utilities.geospatial.Distance) List(java.util.List) MarkerUtilities(au.gov.asd.tac.constellation.views.mapview.utilities.MarkerUtilities) Location(de.fhpotsdam.unfolding.geo.Location) ServiceProvider(org.openide.util.lookup.ServiceProvider) ScreenPosition(de.fhpotsdam.unfolding.utils.ScreenPosition) PGraphics(processing.core.PGraphics) PImage(processing.core.PImage) MapPosition(de.fhpotsdam.unfolding.utils.MapPosition) Collectors(java.util.stream.Collectors) PGraphics(processing.core.PGraphics) ScreenPosition(de.fhpotsdam.unfolding.utils.ScreenPosition) MapPosition(de.fhpotsdam.unfolding.utils.MapPosition) Location(de.fhpotsdam.unfolding.geo.Location)

Example 3 with ScreenPosition

use of de.fhpotsdam.unfolding.utils.ScreenPosition in project constellation by constellation-app.

the class PointMarkerErrorLayer method update.

@Override
public PImage update() {
    if (map.getMarkers().isEmpty()) {
        return null;
    }
    markerCount = map.getMarkers().size();
    final int width = renderer.width - 5;
    final int height = renderer.height - 5;
    // get on screen markers
    final ScreenPosition topLeft = map.getScreenPosition(map.getTopLeftBorder());
    final ScreenPosition bottomRight = map.getScreenPosition(map.getBottomRightBorder());
    final List<ConstellationAbstractMarker> markers = renderer.getMarkerCache().getAllMarkers().stream().filter(marker -> {
        final ScreenPosition markerPosition = map.getScreenPosition(marker.getLocation());
        return markerPosition != null && markerPosition.x > topLeft.x && markerPosition.y > topLeft.y && markerPosition.x < bottomRight.x && markerPosition.y < bottomRight.y;
    }).collect(Collectors.toList());
    // create error region data from markers
    final PGraphics errorRegionImage = renderer.createGraphics(width, height, PConstants.JAVA2D);
    errorRegionImage.beginDraw();
    errorRegionImage.stroke(STROKE_COLOR);
    errorRegionImage.fill(ERROR_REGION_COLOR);
    if (graph != null) {
        final ReadableGraph readableGraph = graph.getReadableGraph();
        try {
            final int vertexPrecisionAttributeId = SpatialConcept.VertexAttribute.PRECISION.get(readableGraph);
            final int transactionPrecisionAttributeId = SpatialConcept.TransactionAttribute.PRECISION.get(readableGraph);
            for (final ConstellationAbstractMarker marker : markers) {
                if (!(marker instanceof ConstellationPointMarker)) {
                    continue;
                }
                float minimumPrecision = DEFAULT_PRECISION;
                final Set<GraphElement> elements = renderer.getMarkerCache().get(marker);
                for (final GraphElement element : elements) {
                    final float elementPrecision;
                    switch(element.getType()) {
                        case VERTEX:
                            if (vertexPrecisionAttributeId != Graph.NOT_FOUND) {
                                elementPrecision = readableGraph.getFloatValue(vertexPrecisionAttributeId, element.getId());
                                minimumPrecision = Math.max(elementPrecision, minimumPrecision);
                            } else {
                                elementPrecision = DEFAULT_PRECISION;
                                minimumPrecision = DEFAULT_PRECISION;
                            }
                            break;
                        case TRANSACTION:
                            elementPrecision = transactionPrecisionAttributeId != Graph.NOT_FOUND ? readableGraph.getFloatValue(transactionPrecisionAttributeId, element.getId()) : DEFAULT_PRECISION;
                            break;
                        default:
                            elementPrecision = DEFAULT_PRECISION;
                            break;
                    }
                    minimumPrecision = Math.max(elementPrecision, minimumPrecision);
                }
                // don't bother drawing if there isn't a precision
                if (minimumPrecision == 0) {
                    continue;
                }
                final Location errorRegionRadiusLocation = new Location(marker.getLocation().getLat() - Haversine.kilometersToDecimalDegrees(minimumPrecision), marker.getLocation().getLon() - Haversine.kilometersToDecimalDegrees(minimumPrecision));
                final List<Location> errorRegionLocations = MarkerUtilities.generateCircle(marker.getLocation(), errorRegionRadiusLocation);
                final List<MapPosition> errorRegionPositions = errorRegionLocations.stream().map(location -> new MapPosition(map.mapDisplay.getObjectFromLocation(location))).collect(Collectors.toList());
                errorRegionImage.beginShape();
                errorRegionPositions.forEach(position -> errorRegionImage.vertex(position.x, position.y));
                errorRegionImage.endShape(PConstants.CLOSE);
            }
        } finally {
            readableGraph.release();
        }
    }
    errorRegionImage.endDraw();
    return errorRegionImage;
}
Also used : ConstellationAbstractMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationAbstractMarker) MarkerUtilities(au.gov.asd.tac.constellation.views.mapview.utilities.MarkerUtilities) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) GraphElement(au.gov.asd.tac.constellation.views.mapview.utilities.GraphElement) Set(java.util.Set) SpatialConcept(au.gov.asd.tac.constellation.graph.schema.analytic.concept.SpatialConcept) PGraphics(processing.core.PGraphics) PImage(processing.core.PImage) ConstellationAbstractMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationAbstractMarker) Collectors(java.util.stream.Collectors) Graph(au.gov.asd.tac.constellation.graph.Graph) PConstants(processing.core.PConstants) List(java.util.List) Location(de.fhpotsdam.unfolding.geo.Location) Haversine(au.gov.asd.tac.constellation.utilities.geospatial.Distance.Haversine) ServiceProvider(org.openide.util.lookup.ServiceProvider) ConstellationPointMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationPointMarker) ScreenPosition(de.fhpotsdam.unfolding.utils.ScreenPosition) MapPosition(de.fhpotsdam.unfolding.utils.MapPosition) ReadableGraph(au.gov.asd.tac.constellation.graph.ReadableGraph) PGraphics(processing.core.PGraphics) ScreenPosition(de.fhpotsdam.unfolding.utils.ScreenPosition) GraphElement(au.gov.asd.tac.constellation.views.mapview.utilities.GraphElement) ConstellationPointMarker(au.gov.asd.tac.constellation.views.mapview.markers.ConstellationPointMarker) MapPosition(de.fhpotsdam.unfolding.utils.MapPosition) Location(de.fhpotsdam.unfolding.geo.Location)

Example 4 with ScreenPosition

use of de.fhpotsdam.unfolding.utils.ScreenPosition in project constellation by constellation-app.

the class ThiessenPolygonsLayer method update.

@Override
public PImage update() {
    // update on screen markers
    final ScreenPosition topLeft = map.getScreenPosition(map.getTopLeftBorder());
    final ScreenPosition bottomRight = map.getScreenPosition(map.getBottomRightBorder());
    final List<Marker> onScreenMarkers = map.getMarkers().stream().filter(marker -> {
        final ScreenPosition markerPosition = map.getScreenPosition(marker.getLocation());
        return !marker.isHidden() && markerPosition != null && markerPosition.x > topLeft.x && markerPosition.y > topLeft.y && markerPosition.x < bottomRight.x && markerPosition.y < bottomRight.y;
    }).collect(Collectors.toList());
    onScreenMarkerCount = onScreenMarkers.size();
    if (onScreenMarkers.isEmpty()) {
        return null;
    }
    // map markers to screen positions
    final Map<Marker, ScreenPosition> positionMap = onScreenMarkers.stream().collect(Collectors.toMap(marker -> marker, marker -> map.getScreenPosition(marker.getLocation()), (marker1, marker2) -> marker1));
    // map markers to colors
    final ConstellationColor[] palette = ConstellationColor.createPalette(onScreenMarkerCount);
    final Map<Marker, Integer> paletteMap = new HashMap<>();
    onScreenMarkers.forEach(marker -> paletteMap.put(marker, MarkerUtilities.color(palette[onScreenMarkers.indexOf(marker)])));
    final int width = renderer.width - 5;
    final int height = renderer.height - 5;
    final PImage voronoiImage = renderer.createImage(width, height, PConstants.ARGB);
    voronoiImage.loadPixels();
    for (int pixelIndex = 0; pixelIndex < voronoiImage.pixels.length; pixelIndex++) {
        // find the closest marker to this pixel
        final ScreenPosition pixelPosition = pixelPosition(pixelIndex, width, height);
        Marker closestMarker = null;
        for (final Marker marker : onScreenMarkers) {
            if (closestMarker == null) {
                closestMarker = marker;
            } else {
                final ScreenPosition markerPosition = positionMap.get(marker);
                final ScreenPosition closestMarkerPosition = positionMap.get(closestMarker);
                if (markerPosition != null && closestMarkerPosition != null && euclidianDistance((int) pixelPosition.x, (int) pixelPosition.y, (int) markerPosition.x, (int) markerPosition.y) < euclidianDistance((int) pixelPosition.x, (int) pixelPosition.y, (int) closestMarkerPosition.x, (int) closestMarkerPosition.y)) {
                    closestMarker = marker;
                }
            }
        }
        // color this pixel based on its closest marker
        if (closestMarker != null && paletteMap.get(closestMarker) != null) {
            voronoiImage.pixels[pixelIndex] = paletteMap.get(closestMarker);
        }
    }
    voronoiImage.updatePixels();
    return voronoiImage;
}
Also used : PConstants(processing.core.PConstants) List(java.util.List) MarkerUtilities(au.gov.asd.tac.constellation.views.mapview.utilities.MarkerUtilities) ConstellationColor(au.gov.asd.tac.constellation.utilities.color.ConstellationColor) Map(java.util.Map) ServiceProvider(org.openide.util.lookup.ServiceProvider) ScreenPosition(de.fhpotsdam.unfolding.utils.ScreenPosition) HashMap(java.util.HashMap) Marker(de.fhpotsdam.unfolding.marker.Marker) PImage(processing.core.PImage) Collectors(java.util.stream.Collectors) ConstellationColor(au.gov.asd.tac.constellation.utilities.color.ConstellationColor) PImage(processing.core.PImage) HashMap(java.util.HashMap) Marker(de.fhpotsdam.unfolding.marker.Marker) ScreenPosition(de.fhpotsdam.unfolding.utils.ScreenPosition)

Example 5 with ScreenPosition

use of de.fhpotsdam.unfolding.utils.ScreenPosition in project constellation by constellation-app.

the class ToolsOverlay method overlay.

@Override
public void overlay() {
    boolean leftMousePressed = renderer.mouseButton == PConstants.LEFT;
    // draw tool overlay
    renderer.noStroke();
    renderer.fill(BACKGROUND_COLOUR);
    renderer.rect(x, y, width, height);
    float yOffset = y + MARGIN;
    // update measure tool state
    final float measureToolX = x + 60;
    final float measureToolWidth = VALUE_BOX_MEDIUM_WIDTH + VALUE_BOX_SHORT_WIDTH;
    final float measureSystemX = x + 60 + VALUE_BOX_MEDIUM_WIDTH + VALUE_BOX_SHORT_WIDTH + (PADDING * 2);
    final float measureSystemWidth = VALUE_BOX_SHORT_WIDTH;
    if (renderer.mouseX > measureToolX && renderer.mouseX < measureToolX + measureToolWidth && renderer.mouseY > yOffset && renderer.mouseY < yOffset + VALUE_BOX_HEIGHT) {
        if (leftMousePressed && mouseLeftMeasureToolRegion && !drawActive) {
            measureActive = !measureActive;
            mouseLeftMeasureToolRegion = false;
        }
    } else {
        mouseLeftMeasureToolRegion = true;
    }
    if (!measureActive && renderer.mouseX > measureSystemX && renderer.mouseX < measureSystemX + measureSystemWidth && renderer.mouseY > yOffset && renderer.mouseY < yOffset + VALUE_BOX_HEIGHT) {
        if (leftMousePressed && mouseLeftMeasureSystemRegion) {
            final MeasurementSystem[] measurementSystems = MeasurementSystem.values();
            measureSystem = measurementSystems[(measureSystem.ordinal() + 1) % measurementSystems.length];
            mouseLeftMeasureSystemRegion = false;
        }
    } else {
        mouseLeftMeasureSystemRegion = true;
    }
    // draw measure tool
    drawLabel("Measure", x + 60, yOffset);
    drawValue(measureSystem.getDisplayText(), measureSystemX, yOffset, measureSystemWidth, false, false);
    if (drawActive) {
        final Location start = getDrawToolStart();
        final Location end = getDrawToolEnd();
        if (start != null && end != null) {
            final float distance = measureSystem.getMeasureFunction().apply(start, end).floatValue();
            drawValue(String.format("%s", PApplet.nf(distance, 1, 3)), measureToolX, yOffset, measureToolWidth, false, false);
        } else {
            drawValue(DISABLED, measureToolX, yOffset, measureToolWidth, false, false);
        }
    } else if (measureActive) {
        final Location start = getMeasureToolStart();
        final Location end = getMeasureToolEnd();
        if (start != null && end != null) {
            float distance = measureSystem.getMeasureFunction().apply(start, end).floatValue();
            if (measurePath) {
                for (int i = 1; i < measureVertices.size(); i++) {
                    final Location startVertex = measureVertices.get(i - 1);
                    final Location endVertex = measureVertices.get(i);
                    distance += measureSystem.getMeasureFunction().apply(startVertex, endVertex).floatValue();
                }
            }
            drawValue(String.format("%s", PApplet.nf(distance, 1, 3)), measureToolX, yOffset, measureToolWidth, false, true);
        } else {
            drawValue("Enabled", measureToolX, yOffset, measureToolWidth, false, true);
        }
    } else {
        drawValue(DISABLED, measureToolX, yOffset, measureToolWidth, false, false);
    }
    // update map based on measure tool state
    if (mouseLeftMeasureToolRegion && measureActive) {
        final int measureToolColor = renderer.color(255, 0, 0, 127);
        renderer.stroke(measureToolColor);
        if (measurePath && measureVertices.size() > 1) {
            for (int i = 1; i < measureVertices.size(); i++) {
                final ScreenPosition previousVertex = map.getScreenPosition(measureVertices.get(i - 1));
                final ScreenPosition currentVertex = map.getScreenPosition(measureVertices.get(i));
                renderer.strokeWeight(5);
                renderer.point(previousVertex.x, previousVertex.y);
                renderer.point(currentVertex.x, currentVertex.y);
                renderer.strokeWeight(2);
                renderer.line(previousVertex.x, previousVertex.y, currentVertex.x, currentVertex.y);
            }
        } else {
            renderer.strokeWeight(5);
            renderer.point(measureOriginX, measureOriginY);
        }
        if (measureOriginX != -1 && measureOriginY != -1 && measureDeltaX != -1 && measureDeltaY != -1) {
            renderer.strokeWeight(5);
            renderer.point(measureDeltaX, measureDeltaY);
            renderer.strokeWeight(2);
            renderer.line(measureOriginX, measureOriginY, measureDeltaX, measureDeltaY);
            if (measureCircle) {
                final float radius = (float) Math.sqrt(Math.pow((measureDeltaX - measureOriginX), 2) + Math.pow((measureDeltaY - measureOriginY), 2));
                renderer.noFill();
                renderer.strokeWeight(2);
                renderer.ellipseMode(PConstants.RADIUS);
                renderer.ellipse(measureOriginX, measureOriginY, radius, radius);
            }
        }
    }
    // draw separator
    yOffset += VALUE_BOX_HEIGHT + PADDING * 2;
    drawSeparator(yOffset);
    yOffset += PADDING * 2;
    // update draw tool state
    final float drawToolX = x + 60;
    final float drawToolWidth = VALUE_BOX_MEDIUM_WIDTH + VALUE_BOX_SHORT_WIDTH;
    final float addToGraphX = x + 60 + VALUE_BOX_MEDIUM_WIDTH + VALUE_BOX_SHORT_WIDTH + (PADDING * 2);
    final float addToGraphWidth = VALUE_BOX_SHORT_WIDTH;
    if (renderer.mouseX > drawToolX && renderer.mouseX < drawToolX + drawToolWidth && renderer.mouseY > yOffset && renderer.mouseY < yOffset + VALUE_BOX_HEIGHT) {
        if (leftMousePressed && mouseLeftDrawToolRegion && !measureActive) {
            drawActive = !drawActive;
            mouseLeftDrawToolRegion = false;
        }
    } else {
        mouseLeftDrawToolRegion = true;
    }
    if (!drawActive && renderer.mouseX > addToGraphX && renderer.mouseX < addToGraphX + addToGraphWidth && renderer.mouseY > yOffset && renderer.mouseY < yOffset + VALUE_BOX_HEIGHT) {
        if (leftMousePressed && mouseLeftAddToGraphRegion) {
            try {
                final Graph currentGraph = GraphManager.getDefault().getActiveGraph();
                PluginExecution.withPlugin(MapViewPluginRegistry.COPY_CUSTOM_MARKERS_TO_GRAPH).executeNow(currentGraph);
                final MarkerCache markerCache = renderer.getMarkerCache();
                markerCache.getCustomMarkers().forEach(marker -> {
                    markerCache.get(marker).remove(GraphElement.NON_ELEMENT);
                    marker.setCustom(false);
                });
                renderer.updateMarkers(currentGraph, renderer.getMarkerState());
            } catch (final PluginException ex) {
                LOGGER.log(Level.SEVERE, "Error copying custom markers to graph", ex);
            } catch (final InterruptedException ex) {
                LOGGER.log(Level.SEVERE, "Error copying custom markers to graph", ex);
                Thread.currentThread().interrupt();
            }
            mouseLeftAddToGraphRegion = false;
        }
    } else {
        mouseLeftAddToGraphRegion = true;
    }
    // draw draw tool
    drawLabel("Draw", x + 60, yOffset);
    drawValue("+", addToGraphX, yOffset, addToGraphWidth, false, false);
    if (drawActive) {
        drawValue("Enabled", drawToolX, yOffset, drawToolWidth, false, true);
        yOffset += VALUE_BOX_HEIGHT + (PADDING * 4);
        final float drawDescriptionHeight = (VALUE_BOX_HEIGHT * 16) + (PADDING * 2) + 1;
        renderer.noStroke();
        renderer.fill(BACKGROUND_COLOUR);
        renderer.rect(x, yOffset - 1, width, drawDescriptionHeight);
        final String drawDescription = " > Click on the map to draw a point marker.\n" + " > Click on the map while holding shift to begin drawing a circle" + "  marker, click again with or without shift to complete it.\n" + " > Click on the map while holding control to begin drawing a polygon" + "  marker, continue clicking while holding control to draw edges," + "  then release control and click once more to complete it.\n" + " > Click on a drawn marker to remove it.";
        drawInfo(drawDescription, yOffset - (PADDING * 2), width - (MARGIN * 2) - (PADDING * 2), true);
    } else {
        drawValue(DISABLED, drawToolX, yOffset, drawToolWidth, false, false);
    }
    // update map based on draw tool state
    if (mouseLeftDrawToolRegion && drawActive) {
        final int polygonColor = renderer.color(0, 0, 0, 127);
        renderer.stroke(polygonColor);
        renderer.strokeJoin(PConstants.ROUND);
        renderer.strokeCap(PConstants.ROUND);
        if (drawPolygon && drawVertices.size() > 1) {
            for (int i = 1; i < drawVertices.size(); i++) {
                final ScreenPosition previousVertex = map.getScreenPosition(drawVertices.get(i - 1));
                final ScreenPosition currentVertex = map.getScreenPosition(drawVertices.get(i));
                renderer.strokeWeight(5);
                renderer.point(previousVertex.x, previousVertex.y);
                renderer.point(currentVertex.x, currentVertex.y);
                renderer.strokeWeight(2);
                renderer.line(previousVertex.x, previousVertex.y, currentVertex.x, currentVertex.y);
            }
        } else {
            renderer.strokeWeight(5);
            renderer.point(drawOriginX, drawOriginY);
        }
        if (drawOriginX != -1 && drawOriginY != -1 && drawDeltaX != -1 && drawDeltaY != -1) {
            renderer.strokeWeight(5);
            renderer.point(drawDeltaX, drawDeltaY);
            renderer.strokeWeight(2);
            renderer.line(drawOriginX, drawOriginY, drawDeltaX, drawDeltaY);
            if (drawCircle) {
                final float radius = (float) Math.sqrt(Math.pow((drawDeltaX - drawOriginX), 2) + Math.pow((drawDeltaY - drawOriginY), 2));
                renderer.noFill();
                renderer.strokeWeight(2);
                renderer.ellipseMode(PConstants.RADIUS);
                renderer.ellipse(drawOriginX, drawOriginY, radius, radius);
            }
        }
    }
    active = measureActive || drawActive;
}
Also used : Graph(au.gov.asd.tac.constellation.graph.Graph) PluginException(au.gov.asd.tac.constellation.plugins.PluginException) MarkerCache(au.gov.asd.tac.constellation.views.mapview.utilities.MarkerCache) ScreenPosition(de.fhpotsdam.unfolding.utils.ScreenPosition) Location(de.fhpotsdam.unfolding.geo.Location)

Aggregations

ScreenPosition (de.fhpotsdam.unfolding.utils.ScreenPosition)10 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 PConstants (processing.core.PConstants)5 PImage (processing.core.PImage)5 MarkerUtilities (au.gov.asd.tac.constellation.views.mapview.utilities.MarkerUtilities)4 Location (de.fhpotsdam.unfolding.geo.Location)4 Marker (de.fhpotsdam.unfolding.marker.Marker)3 ServiceProvider (org.openide.util.lookup.ServiceProvider)3 PGraphics (processing.core.PGraphics)3 Graph (au.gov.asd.tac.constellation.graph.Graph)2 ReadableGraph (au.gov.asd.tac.constellation.graph.ReadableGraph)2 ConstellationColor (au.gov.asd.tac.constellation.utilities.color.ConstellationColor)2 ConstellationAbstractMarker (au.gov.asd.tac.constellation.views.mapview.markers.ConstellationAbstractMarker)2 GraphElement (au.gov.asd.tac.constellation.views.mapview.utilities.GraphElement)2 MapPosition (de.fhpotsdam.unfolding.utils.MapPosition)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 SpatialConcept (au.gov.asd.tac.constellation.graph.schema.analytic.concept.SpatialConcept)1