| /******************************************************************************* |
| * 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.common.geometry.data.impl; |
| |
| import org.eclipse.apogy.common.geometry.data.Coordinates; |
| import org.eclipse.apogy.common.geometry.data.CoordinatesSamplingShape; |
| import org.eclipse.apogy.common.geometry.data.CoordinatesSet; |
| import org.eclipse.apogy.common.geometry.data.ShapeSamplingMode; |
| |
| public class CoordinatesSetShapesSamplerCustomImpl<T extends Coordinates> extends CoordinatesSetShapesSamplerImpl<T> { |
| @Override |
| public CoordinatesSet<T> process(CoordinatesSet<T> input) throws Exception { |
| // Updates the processor input. |
| setInput(input); |
| |
| // Creates the coordinate set that will contain the result. |
| CoordinatesSet<T> sampled = createCoordinatesSet(input); |
| |
| if (getProgressMonitor() != null) |
| getProgressMonitor().beginTask( |
| "Sampling using " + getCoordinatesSamplingShapes().size() + " sampling shapes. ", |
| input.getPoints().size()); |
| |
| // Goes through the list of point and checks each of them. |
| for (int i = 0; i < input.getPoints().size(); i++) { |
| T point = input.getPoints().get(i); |
| |
| // If point is inside, add it to the CoordinatesSet |
| if (isPointInside(point)) { |
| sampled.getPoints().add(copyCoordinates(point)); |
| if (getProgressMonitor() != null) |
| getProgressMonitor().worked(1); |
| } |
| } |
| |
| if (getProgressMonitor() != null) |
| getProgressMonitor().done(); |
| |
| // Updates the processor output. |
| setOutput(sampled); |
| |
| return sampled; |
| } |
| |
| /** |
| * Must be overwritten by sub-classes to create the right kind of coordinates |
| * set produced by the process method. |
| * |
| * @return |
| */ |
| protected CoordinatesSet<T> createCoordinatesSet(CoordinatesSet<T> input) { |
| return null; |
| } |
| |
| /** |
| * Must be overwritten by sub-classes to create the right kind of coordinates |
| * that is inside the coordinates set produced by the process method. |
| * |
| * @param point |
| * @return |
| */ |
| protected T copyCoordinates(T point) { |
| throw new UnsupportedOperationException(); |
| } |
| |
| /** |
| * Return whether or not a point is inside at least one sampling shape |
| * |
| * @param point The point. |
| * @return True if the point is inside, false otherwise. |
| */ |
| private boolean isPointInside(T point) { |
| boolean inside = false; |
| if (getShapeSamplingMode() == ShapeSamplingMode.UNION) { |
| inside = false; |
| int j = 0; |
| |
| while (j < getCoordinatesSamplingShapes().size() && !inside) { |
| CoordinatesSamplingShape<T> samplingShape = getCoordinatesSamplingShapes().get(j); |
| inside |= samplingShape.isInside(point); |
| j++; |
| } |
| } else if (getShapeSamplingMode() == ShapeSamplingMode.INTERSECTION) { |
| inside = true; |
| int j = 0; |
| |
| while (j < getCoordinatesSamplingShapes().size() && inside) { |
| CoordinatesSamplingShape<T> samplingShape = getCoordinatesSamplingShapes().get(j); |
| inside &= samplingShape.isInside(point); |
| j++; |
| } |
| } |
| |
| return inside; |
| } |
| } // CoordinatesSetShapesSamplerImpl |