use of com.ardor3d.math.LineSegment3 in project energy3d by concord-consortium.
the class MeshLib method computeOutline.
public static ArrayList<ReadOnlyVector3> computeOutline(final FloatBuffer buf) {
final Map<LineSegment3, Boolean> visitMap = new HashMap<LineSegment3, Boolean>();
for (int i = 0; i < buf.limit(); i += 9) {
for (int trianglePointIndex = 0; trianglePointIndex < 9; trianglePointIndex += 3) {
buf.position(i + trianglePointIndex);
Vector3 p1 = new Vector3(buf.get(), buf.get(), buf.get());
buf.position(i + (trianglePointIndex + 3) % 9);
Vector3 p2 = new Vector3(buf.get(), buf.get(), buf.get());
if (p2.getX() < p1.getX() || (p2.getX() == p1.getX() && p2.getY() < p1.getY())) {
final Vector3 tmp = p1;
p1 = p2;
p2 = tmp;
}
final LineSegment3 line = new LineSegment3(p1, p2);
final Boolean pastVisit = visitMap.get(line);
if (pastVisit == null) {
visitMap.put(line, true);
} else {
visitMap.put(line, false);
}
}
}
final ArrayList<ReadOnlyVector3> outlinePoints = new ArrayList<ReadOnlyVector3>();
for (final LineSegment3 line : visitMap.keySet()) {
if (visitMap.get(line)) {
final Vector3 negativeEnd = line.getNegativeEnd(null);
outlinePoints.add(negativeEnd);
outlinePoints.add(line.getPositiveEnd(null));
}
}
final ArrayList<ReadOnlyVector3> sortedOutlinePoints = new ArrayList<ReadOnlyVector3>(outlinePoints.size() / 2);
sortedOutlinePoints.add(outlinePoints.get(0));
ReadOnlyVector3 lastPoint = outlinePoints.get(1);
sortedOutlinePoints.add(lastPoint);
outlinePoints.remove(1);
outlinePoints.remove(0);
while (!outlinePoints.isEmpty()) {
boolean foundSomething = false;
for (int i = 0; i < outlinePoints.size(); i++) {
if (Util.isEqual(outlinePoints.get(i), lastPoint)) {
final int otherEndIndex = i % 2 == 0 ? i + 1 : i - 1;
lastPoint = outlinePoints.get(otherEndIndex);
sortedOutlinePoints.add(lastPoint);
outlinePoints.remove(Math.max(i, otherEndIndex));
outlinePoints.remove(Math.min(i, otherEndIndex));
foundSomething = true;
break;
}
}
if (!foundSomething) {
break;
}
}
// remove last point if duplicated of first point
if (Util.isEqual(sortedOutlinePoints.get(0), sortedOutlinePoints.get(sortedOutlinePoints.size() - 1))) {
sortedOutlinePoints.remove(sortedOutlinePoints.size() - 1);
}
return sortedOutlinePoints;
}
Aggregations