mccube.java

来自「由java实现的marching cube」· Java 代码 · 共 676 行 · 第 1/2 页

JAVA
676
字号
        0, 4, 10, 0, 10, 3, 4, 5, 10, 2, 10, 1, 5, 1, 10,        0, 2, 5, 0, 5, 9, 2, 10, 5, 4, 5, 8, 10, 8, 5,        9, 4, 5, 2, 10, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1,        2, 5, 11, 3, 5, 2, 3, 4, 5, 3, 8, 4, -1, -1, -1,        5, 11, 2, 5, 2, 4, 4, 2, 0, -1, -1, -1, -1, -1, -1,        3, 11, 2, 3, 5, 11, 3, 8, 5, 4, 5, 8, 0, 1, 9,        5, 11, 2, 5, 2, 4, 1, 9, 2, 9, 4, 2, -1, -1, -1,        8, 4, 5, 8, 5, 3, 3, 5, 1, -1, -1, -1, -1, -1, -1,        0, 4, 5, 1, 0, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1,        8, 4, 5, 8, 5, 3, 9, 0, 5, 0, 3, 5, -1, -1, -1,        9, 4, 5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,        4, 10, 7, 4, 9, 10, 9, 11, 10, -1, -1, -1, -1, -1, -1,        0, 8, 3, 4, 9, 7, 9, 10, 7, 9, 11, 10, -1, -1, -1,        1, 11, 10, 1, 10, 4, 1, 4, 0, 7, 4, 10, -1, -1, -1,        3, 1, 4, 3, 4, 8, 1, 11, 4, 7, 4, 10, 11, 10, 4,        4, 10, 7, 9, 10, 4, 9, 2, 10, 9, 1, 2, -1, -1, -1,        9, 7, 4, 9, 10, 7, 9, 1, 10, 2, 10, 1, 0, 8, 3,        10, 7, 4, 10, 4, 2, 2, 4, 0, -1, -1, -1, -1, -1, -1,        10, 7, 4, 10, 4, 2, 8, 3, 4, 3, 2, 4, -1, -1, -1,        2, 9, 11, 2, 7, 9, 2, 3, 7, 7, 4, 9, -1, -1, -1,        9, 11, 7, 9, 7, 4, 11, 2, 7, 8, 7, 0, 2, 0, 7,        3, 7, 11, 3, 11, 2, 7, 4, 11, 1, 11, 0, 4, 0, 11,        1, 11, 2, 8, 7, 4, -1, -1, -1, -1, -1, -1, -1, -1, -1,        4, 9, 1, 4, 1, 7, 7, 1, 3, -1, -1, -1, -1, -1, -1,        4, 9, 1, 4, 1, 7, 0, 8, 1, 8, 7, 1, -1, -1, -1,        4, 0, 3, 7, 4, 3, -1, -1, -1, -1, -1, -1, -1, -1, -1,        4, 8, 7, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,        9, 11, 8, 11, 10, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1,        3, 0, 9, 3, 9, 10, 10, 9, 11, -1, -1, -1, -1, -1, -1,        0, 1, 11, 0, 11, 8, 8, 11, 10, -1, -1, -1, -1, -1, -1,        3, 1, 11, 10, 3, 11, -1, -1, -1, -1, -1, -1, -1, -1, -1,        1, 2, 10, 1, 10, 9, 9, 10, 8, -1, -1, -1, -1, -1, -1,        3, 0, 9, 3, 9, 10, 1, 2, 9, 2, 10, 9, -1, -1, -1,        0, 2, 10, 8, 0, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1,        3, 2, 10, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,        2, 3, 8, 2, 8, 11, 11, 8, 9, -1, -1, -1, -1, -1, -1,        9, 11, 2, 0, 9, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1,        2, 3, 8, 2, 8, 11, 0, 1, 8, 1, 11, 8, -1, -1, -1,        1, 11, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,        1, 3, 8, 9, 1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1,        0, 9, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,        0, 3, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1    };        /**     * contructor of a MCCube object     * @param id identifier of the cube     */    public MCCube(String id)    {        this(id, new MCVertex(new MCVector3(-SIZE, SIZE, -SIZE)), new MCVertex(new MCVector3(SIZE, SIZE, -SIZE)), new MCVertex(new MCVector3(SIZE, -SIZE, -SIZE)), new MCVertex(new MCVector3(-SIZE, -SIZE, -SIZE)), new MCVertex(new MCVector3(-SIZE, SIZE, SIZE)), new MCVertex(new MCVector3(SIZE, SIZE, SIZE)), new MCVertex(new MCVector3(SIZE, -SIZE, SIZE)), new MCVertex(new MCVector3(-SIZE, -SIZE, SIZE)));    }        /**     * constructor of a MCCube object     * @param id identifier of the cube     * @param v0 first vertex     * @param v1 second vertex     * @param v2 third vertex     * @param v3 fourth vertex     * @param v4 fifth vertex     * @param v5 sixth vertex     * @param v6 seventh vertex     * @param v7 eighth vertex     */    public MCCube(String id, MCVertex v0, MCVertex v1, MCVertex v2, MCVertex v3, MCVertex v4, MCVertex v5, MCVertex v6, MCVertex v7)    {        this.id = id;                this.v = new MCVertex[8];        this.v[0] = v0;        this.v[1] = v1;        this.v[2] = v2;        this.v[3] = v3;        this.v[4] = v4;        this.v[5] = v5;        this.v[6] = v6;        this.v[7] = v7;                this.e = new MCVector3[12];                this.modelView = new MCMatrix4();                this.neighbours = new Vector(0, 1);                this.computeEdges();    }        /**     * constructor of a MCCube object     * @param id identifier of the cube     * @param v array containing the vertexes     */    public MCCube(String id, MCVertex[] v)    {        this.id = id;        this.v = v;        this.e = new MCVector3[12];        this.computeEdges();    }        /**     * indicates if a number corresponds to an ambigous case     * @param n number of the case to test     * @return true if the case if ambigous     */    public static boolean isAmbigous(int n)    {        boolean result = false;        for (int index = 0; index < MCCube.ambigous.length; index++)        {            result |= MCCube.ambigous[index] == n;        }        return result;    }        /**     * computes the case number of the cube     * @return the number of the case corresponding to the cube     */    public int caseNumber()    {        int caseNumber = 0;        /*if (this.amb)        {            for (int index = -1; ++index < v.length; caseNumber += (v[index].weight() - seekValue <= 0)?1 << index:0);        }        else        {*/        for (int index = -1; ++index < v.length; caseNumber += (v[index].weight() - seekValue > 0)?1 << index:0);        //}        return caseNumber;    }        /**     * returns the translation corresponding to a neighbour at a specified position (north/south: along z axis, etc.)     * @param position a direction     * @return the translation vector     */    public static MCVector3 translate(int position)    {        switch(position)        {            case MCNeighbour.SOUTH:                return new MCVector3(0,0,-2*SIZE);            case MCNeighbour.NORTH:                return new MCVector3(0,0,2*SIZE);            case MCNeighbour.EAST:                return new MCVector3(2*SIZE,0,0);            case MCNeighbour.WEST:                return new MCVector3(-2*SIZE,0,0);            case MCNeighbour.DOWN:                return new MCVector3(0,2*SIZE,0);            case MCNeighbour.UP:                return new MCVector3(0,-2*SIZE,0);            default:                return new MCVector3();        }    }        /**     * computes the interpolated point along a specified whose weight equals the reference value     * @param v1 first extremity of the edge     * @param v2 second extremity of the edge     * @return the point on the edge where weight equals the isovalue; null is interpolated point is beyond edge boundaries     */    private MCVector3 computeEdge(MCVertex v1, MCVertex v2)    {        double t = (seekValue - v1.weight())/(double) (v2.weight() - v1.weight());        if (t >= 0 && t <= 1)        {            MCVector3 vDir = (new MCVector3(v2.position)).sub(v1.position);            return (new MCVector3(v1.position)).add(vDir.mult(t));        }        return null;    }        /**     * computes interpolated values along each edge of the cube (null if interpolated value doesn't belong to the edge)     */    public void computeEdges()    {        this.e[0] = this.computeEdge(v[0], v[1]);        this.e[1] = this.computeEdge(v[1], v[2]);        this.e[2] = this.computeEdge(v[2], v[3]);        this.e[3] = this.computeEdge(v[3], v[0]);                this.e[4] = this.computeEdge(v[4], v[5]);        this.e[5] = this.computeEdge(v[5], v[6]);        this.e[6] = this.computeEdge(v[6], v[7]);        this.e[7] = this.computeEdge(v[7], v[4]);                this.e[8] = this.computeEdge(v[0], v[4]);        this.e[9] = this.computeEdge(v[1], v[5]);        this.e[10] = this.computeEdge(v[3], v[7]);        this.e[11] = this.computeEdge(v[2], v[6]);    }        /**     * draws edges of the cube     * @param v array of vertexes transformed by the matrix-stack     */    private void drawEdges(MCVector3[] v)    {        MCRenderer.g.setColor(MCRenderer.EDGE_COLOR);                MCRenderer.drawLine(v[0], v[1]);        MCRenderer.drawLine(v[1], v[2]);        MCRenderer.drawLine(v[2], v[3]);        MCRenderer.drawLine(v[3], v[0]);                MCRenderer.drawLine(v[4], v[5]);        MCRenderer.drawLine(v[5], v[6]);        MCRenderer.drawLine(v[6], v[7]);        MCRenderer.drawLine(v[7], v[4]);                MCRenderer.drawLine(v[0], v[4]);        MCRenderer.drawLine(v[1], v[5]);        MCRenderer.drawLine(v[2], v[6]);        MCRenderer.drawLine(v[3], v[7]);    }        /**     * draws triangles between interpolated values along the edges     */    private void drawTriangles()    {        int cn = this.caseNumber();        boolean directTable = !(isAmbigous(cn) && !this.amb);                // address in the table        int offset = (directTable)?cn*15:(255-cn)*15;        for (int index = 0; index < 5; index++)        {            // if there's a triangle            if (faces[offset] != -1)            {                // pick up vertexes of the current triangle                MCVector3 e1 = this.e[faces[offset + 0]];                MCVector3 e2 = this.e[faces[offset + 1]];                MCVector3 e3 = this.e[faces[offset + 2]];                                // transforms the triangle using the matrix stack and the model view                MCMatrix4 modelView = MCCanvas.matrixStack.peek();                e1 = MCCanvas.matrixStack.peek().mult(e1);                e2 = MCCanvas.matrixStack.peek().mult(e2);                e3 = MCCanvas.matrixStack.peek().mult(e3);                                // computes normal (depending on the complementary case)                MCVector3 v1 = (new MCVector3(e2)).sub(e1);                MCVector3 v2 = (new MCVector3(e3)).sub(e1);                MCVector3 n = directTable?v1.cross(v2).normalize():v2.cross(v1).normalize();                                // call the renderer and draws the triangle                MCRenderer.drawTriangleFlat(e1, e2, e3, n);                                // draws the edges of the triangle                MCRenderer.g.setColor(MCRenderer.EDGE_COLOR);                if (MCApplet.wCube.isSelected())                {                    MCRenderer.drawLine(e1, e2);                    MCRenderer.drawLine(e2, e3);                    MCRenderer.drawLine(e1, e3);                }            }            offset += 3;        }    }        /**     * draws the vertexes of the cubes     * @param v array containing the vertexes transformed by the matrix stack     */    private void drawVertexes(MCVector3[] v)    {        // vertexes of the cube        for (int index = 0; index < this.v.length; index++)        {            // modify the color using the vertex weight            int c = (int) (127*this.v[index].weight()/(double) MCApplet.DEFAULT_RANGE);            MCRenderer.g.setColor(MCRenderer.vertexColor(c));            // draws the circle            MCRenderer.drawCircle(new MCVector4(v[index]), MCVertex.SIZE);            MCRenderer.g.setColor(MCRenderer.TEXT_COLOR);            // and then its number            MCRenderer.drawString(v[index].add(new MCVector3(0, -MCVertex.SIZE, 0)), (new Integer(index)).toString());        }                // interpolated vertexes along the edges        MCRenderer.g.setColor(MCRenderer.I_VERTEX_COLOR);        for (int index = 0; index < this.e.length; index++)        {            if (this.e[index] != null)            {                MCRenderer.drawCircle(MCCanvas.matrixStack.peek().mult(new MCVector4(this.e[index])), MCVertex.SIZE);            }        }    }        /**     * performs all display operations depending on rendering parameters     */    public void display()    {        MCCanvas.matrixStack.push();        MCCanvas.matrixStack.mult(this.modelView);                // transforms the vertexes using the top of the matrix stack        MCVector3[] v = new MCVector3[8];        for (int index = 0; index < v.length; index++)        {            v[index] = MCCanvas.matrixStack.peek().mult(new MCVector3(this.v[index].position));        }                // flat rendering through the MCRenderer static class        this.drawTriangles();                // cube edges (if checked in the interface)        if (MCApplet.wCube.isSelected())            this.drawEdges(v);                // vertexes (of the current selected cube only)        if (MCApplet.cbCube.getSelectedItem() == this && MCApplet.wCube.isSelected())            this.drawVertexes(v);                MCCanvas.matrixStack.pop();    }        /**     * returns a string representation of the cube (its name) (useful to add cubes to combo-boxes)     * @return the name of the cube     */    public String toString()    {        return this.id;    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?