blob: fb51f7c8c8e39b262421892d27d1247d83666f14 [file] [log] [blame]
/*******************************************************************************
* 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.data3d.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.vecmath.Point3d;
import org.eclipse.apogy.common.geometry.data.ApogyCommonGeometryDataPackage;
import org.eclipse.apogy.common.geometry.data3d.CartesianCoordinatesSet;
import org.eclipse.apogy.common.geometry.data3d.CartesianCoordinatesSetExtent;
import org.eclipse.apogy.common.geometry.data3d.CartesianPositionCoordinates;
import org.eclipse.apogy.common.geometry.data3d.Geometry3DUtilities;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.impl.AdapterImpl;
public class CartesianCoordinatesSetCustomImpl extends CartesianCoordinatesSetImpl {
private int currentPointId = 0;
/**
* Map used to maintain relationships between coordinates and point Id.
*/
private Map<CartesianPositionCoordinates, Integer> pointsToPointIdMap;
protected CartesianCoordinatesSetCustomImpl() {
super();
// Finally, we add ourselves as a listener of this point cloud.
this.eAdapters().add(new AdapterImpl() {
@Override
@SuppressWarnings("unchecked")
public void notifyChanged(Notification notification) {
if (notification.getFeatureID(
CartesianCoordinatesSet.class) == ApogyCommonGeometryDataPackage.COORDINATES_SET__POINTS) {
if (notification.getEventType() == Notification.ADD
|| notification.getEventType() == Notification.ADD_MANY) {
if (notification.getNewValue() != null) {
if (notification.getNewValue() instanceof List) {
List<CartesianPositionCoordinates> points = (List<CartesianPositionCoordinates>) notification
.getNewValue();
for (CartesianPositionCoordinates point : points) {
pointAdded(point);
}
} else {
CartesianPositionCoordinates point = (CartesianPositionCoordinates) notification
.getNewValue();
pointAdded(point);
}
}
}
}
}
});
}
public CartesianCoordinatesSetExtent getExtent() {
return Geometry3DUtilities.getCartesianCoordinatesSetExtent(getPoints());
}
private Map<CartesianPositionCoordinates, Integer> getPointsToPointIdMap() {
if (this.pointsToPointIdMap == null) {
this.pointsToPointIdMap = new HashMap<CartesianPositionCoordinates, Integer>();
}
return this.pointsToPointIdMap;
}
public int getPointId(CartesianPositionCoordinates point) {
Integer pid = getPointsToPointIdMap().get(point);
int pidValue = pid == null ? -1 : pid.intValue();
return pidValue;
}
protected void pointAdded(CartesianPositionCoordinates point) {
getPointsToPointIdMap().put(point, this.currentPointId++);
}
@Override
public boolean equals(Object obj) {
boolean equals = false;
if (obj instanceof CartesianCoordinatesSet) {
CartesianCoordinatesSet pointCloud = (CartesianCoordinatesSet) obj;
equals = pointCloud.getPoints().size() == this.getPoints().size();
// We verify that every point value are the same.
for (int i = 0; i < pointCloud.getPoints().size() && equals; i++) {
CartesianPositionCoordinates pSource = pointCloud.getPoints().get(i);
CartesianPositionCoordinates pTarget = this.getPoints().get(i);
Point3d p = pSource.asPoint3d();
p.sub(pTarget.asPoint3d());
p.absolute();
// To be equal, the difference between point i from
// source and point i from target, must be less than 1e-10.
equals = p.x <= 1e-10 && p.y <= 1e-10 && p.z <= 1e-10;
}
}
return equals;
}
} // CartesianCoordinatesSetImpl