我发现了一个凸包算法,该算法在投影到2D之后对3D凸平面的给定点进行排序。这个算法叫什么?我查看了常见的算法: jarvis march,quickhull,chan's,graham scan ,但它们都不匹配。代码:
private void buildConvexHull() {
Point3D[] projPoints = new Point3D[unorderedPoints.length];
for (int i = 0; i < unorderedPoints.length; i++) {
Point p = orthoView.project(unorderedPoints[i]); //Orthogonal projection for representing three-dimensional points in two dimensions.
projPoints[i] = new Point3D(p.x, p.y, 0.0); //projected point without third dimension
}
vertices = new Point3D[1];
//determine the point with smallest x coordinate.
Point3D bottom = projPoints[0];
vertices[0] = unorderedPoints[0];
for (int i = 1; i < projPoints.length; i++) {
if (projPoints[i].x < bottom.x) { // if (projPoints[i].y < bottom.y) {
bottom = projPoints[i];
vertices[0] = unorderedPoints[i];
}
}
Point3D p = bottom;
Point3D[] v = new Point3D[1];
v[0] = p;
do {
int i = 0;
if (projPoints[0] == p) {
i = 1;
}
Point3D candidate = projPoints[i];
HalfSpace h = new HalfSpace(p, candidate);
int index = i;
for (i = i + 1; i < projPoints.length; i++) {
if (projPoints[i] != p && h.isInside(projPoints[i]) < 0) {
candidate = projPoints[i];
h = new HalfSpace(p, candidate);
index = i;
}
}
Point3D[] vnew = new Point3D[v.length + 1];
System.arraycopy(v, 0, vnew, 0, v.length);
v = vnew;
v[v.length - 1] = candidate;
Point3D[] verticesnew = new Point3D[vertices.length + 1];
System.arraycopy(vertices, 0, verticesnew, 0, vertices.length);
vertices = verticesnew;
vertices[vertices.length - 1] = unorderedPoints[index];
p = candidate;
} while (p != bottom);
}
Halfspace类的isInside方法:
/**
* A method checking if the given point is inside the halfspace.
*
* @return 1 if it is inside, 0 if it is on the line and otherwise -1.
*/
int isInside(Point3D x) {
Point3D n = a.normal(b, x);
double dz = n.z - a.z;
if (dz > 0) {
return 1;
} else if (dz < 0) {
return -1;
} else {
return 0;
}
}
据我了解,该算法在每次迭代中从还不属于凸包的点集和最后添加到凸包的点集中选择一个点。然后在这两者之间创建一条线。如果没有其他点在外侧,则所选点是跟随点,并将其添加到已处理点的集合中。正确吗?