Java ArrayList <GenericObject> .remove(GenericObject)返回false,但仍会减小ArrayList的大小

jul*_*anc 2 java mesh arraylist data-structures

这是一些上下文:我正在进行一项分配,以折叠存储半边数据结构的网格中的边.这是直接相关的代码.

    System.out.println("Initial Size: " +heds.faces.size());
    if (! heds.faces.remove(currentHE.twin.leftFace));
    {
        System.out.println("We have a twin problem");
            //this will always print 
    }
    // Yet this will always be 1 less than the initial
    System.out.println("After twin removal: " +heds.faces.size());

    if ( !heds.faces.remove(currentHE.leftFace)) 
    {
        System.out.println("We have a problem");
    }
    System.out.println("Third: " +heds.faces.size());
Run Code Online (Sandbox Code Playgroud)

所以问题是"我们有一个双胞胎问题"总是打印,但它不应该,"双胞胎去除后"总是比初始尺寸小一个.

如果您觉得需要,这是剩下的信息.heds在类"HEDS"(半边数据结构)中定义:

     public HEDS( PolygonSoup soup ) {
    HalfEdge potentialTwin;
    HalfEdge[] currHalfEdges;
    Vertex curr, next;      
    for (int[] face : soup.faceList)
    {
        currHalfEdges = new HalfEdge[face.length];
        for (int i = 0; i < face.length; i++)
        {
            HalfEdge he = new HalfEdge();

            curr = soup.vertexList.get(face[i]);
            next = soup.vertexList.get(face[(i+1)%face.length]);

            he.tail = curr;
            he.head = next;
            currHalfEdges[i] = he;
            halfEdges.put(face[i]+","+face[(i+1)%face.length], he);

            potentialTwin = halfEdges.get(face[(i+1)%face.length]+","+face[i]);

            if (potentialTwin != null)
            {
                he.twin = potentialTwin;
                potentialTwin.twin = he;
            }       
        }
        for (int i = 0; i < currHalfEdges.length; i++)
        {

            currHalfEdges[i].next = currHalfEdges[(i+1)%currHalfEdges.length];
        }

        faces.add(new Face(currHalfEdges[0]));
    }

    // Checking if every half-edge's face was propery defined
    Iterator<Entry<String, HalfEdge>> it = halfEdges.entrySet().iterator();
    while (it.hasNext())
    {
        Map.Entry<String, HalfEdge> pairs = (Map.Entry<String, HalfEdge>)it.next();
        if (!faces.contains(pairs.getValue().twin.leftFace))
        {
            System.out.println("DAMN IT!!!!!");
                    // This is never reached
        }

    }
Run Code Online (Sandbox Code Playgroud)

如果我先取下双胞胎的脸也不重要.此外,可以假设网格在此处是多方面的.

半边缘:

public class HalfEdge {

public HalfEdge twin;
public HalfEdge next;
public Vertex head;
public Vertex tail;
public Face leftFace;

/** 
 * while perhaps wasting space, it may be convenient to
 * have a common edge object for each pair of half edges to 
 * store information about the error metric, optimal vertex
 * location on collapse, and the error 
 */
public Edge e;

/** @return the previous half edge (could just be stored) */
public HalfEdge prev() {
    HalfEdge prev = this;
    while ( prev.next != this ) prev = prev.next;        
    return prev;
}
 /**
 * Computes the valence by walking around the vertex at head.
 * @return valence of the vertex at the head of this half edge
 */
public int valence() {
    HalfEdge loop = this;
    int v = 0;
    do {
        v++;
        loop = loop.next.twin;
    } while ( loop != this );
    return v;
}
Run Code Online (Sandbox Code Playgroud)

面部代码:

public class Face {    
/** sure, why not keep a normal for flat shading? */
public Vector3d n = new Vector3d();

/** Plane equation */
Vector4d p = new Vector4d();

/** Quadratic function for the plane equation */
public Matrix4d K = new Matrix4d();

/** Some half edge on the face */
HalfEdge he;

/** 
 * Constructs a face from a half edge, and computes the flat normal
 * @param he
 */
public Face( HalfEdge he ) {
    this.he = he;
    HalfEdge loop = he;
    do {
        loop.leftFace = this;
        loop = loop.next;
    } while ( loop != he );
    recomputeNormal();
}

public Face(List<Vertex> vertexList, int[] faceVertices)
{

}

public void recomputeNormal() {
    Point3d p0 = he.head.p;
    Point3d p1 = he.next.head.p;
    Point3d p2 = he.next.next.head.p;
    Vector3d v1 = new Vector3d();
    Vector3d v2 = new Vector3d();
    v1.sub(p1,p0);
    v2.sub(p2,p1);
    n.cross( v1,v2 );

    // TODO: compute the plane and matrix K for the quadric error metric

}
Run Code Online (Sandbox Code Playgroud)

}

对最后一个问题感到抱歉,我有点匆忙,并希望有一个简单的解决方案./sf/ask/562201251/

Mar*_*ers 7

相当简单的问题,但你的所有代码都蒙上阴影:

if (! heds.faces.remove(currentHE.twin.leftFace));
{
    //...
}
Run Code Online (Sandbox Code Playgroud)

这是if条件结束时的分号.该分号结束语句,这是你的if语句.它将if块的"then"部分转换为空语句(因此没有任何反应).然后你就会有一个浮动块,总是会发生.

你的代码执行如下:

if (! heds.faces.remove(currentHE.twin.leftFace))
    ; //this empty statement never gets executed, but nothing to execute anyhow


{
    //this will always print (since it's not guarded by the if)
    System.out.println("We have a twin problem");
}
Run Code Online (Sandbox Code Playgroud)

换句话说,heds.faces.remove()确实是真的; 你刚才没有正确报告结果.删除分号,输出应该开始对你有意义.