📄 triangulator.java
字号:
loops[i1] = ind; noHashingPnts = false; NoHash.prepareNoHashPnts(this, i1); EarClip.classifyEars(this, ind); reset = false; done[0] = false; } } } } i1 = i2; } /* if (troubles) System.out.println("\n\nTriangulation completed!\n"); else System.out.println("\n\nTriangulation successfully completed!\n"); */ // System.out.println("\n...writing the output data: "); // Output triangles here. writeTriangleToGeomInfo(); } void printVtxList() { int i; System.out.println("numReflex " + numReflex + " reflexVertices " + reflexVertices); for(i= 0; i<numVtxList; i++) System.out.println(i + " pnt " + vtxList[i].pnt + ", next " + vtxList[i].next ); } void printListData() { for(int i=0; i<numList; i++) System.out.println("list[" + i + "].index " + list[i].index + ", prev " + list[i].prev + ", next " + list[i].next + ", convex " + list[i].convex + ", vertexIndex " + list[i].vcntIndex); } void preProcessList(int i1) { int tInd, tInd1, tInd2; resetPolyList(loops[i1]); tInd = loops[i1]; tInd1 = tInd; tInd2 = list[tInd1].next; while(tInd2 != tInd) { if(list[tInd1].index == list[tInd2].index) { if(tInd2 == loops[i1]) loops[i1] = list[tInd2].next; deleteLinks(tInd2); } tInd1 = list[tInd1].next; tInd2 = list[tInd1].next; } } void writeTriangleToGeomInfo() { int i, currIndex; // There are 2 approaches to take here : (1) Output all triangles as // a single face.(Easy) (2) Preserve the faces of the polyhedron and // sets of triangles per face. ( Seems to be the preferred approach, but // a check in GeometryInfo and the old GeomInfoConverter doesn't seems // to support this. Will check with Dan and Paul. Will take the easy way first. // For debugging .... if(DEBUG == 1) { System.out.println("List (number " + numList + ") : "); for(i=0; i<numList; i++) { System.out.println("index " + list[i].index + " prev " + list[i].prev + " next " + list[i].next + " convex " + list[i].convex); System.out.println(" vertexIndex " + list[i].vcntIndex + " colorIndex " + list[i].vcntIndex + " normalIndex " + list[i].vcntIndex + " textureIndex " + list[i].vcntIndex); } System.out.println("Points (number " + numPoints + ") : "); for(i=0; i<numPoints; i++) { System.out.println("x " + points[i].x + " y " + points[i].y); } System.out.println("Triangles (number " + numTriangles + ") : "); for(i=0; i<numTriangles; i++) { System.out.println("v1 " + triangles[i].v1 + " v2 " + triangles[i].v2 + " v3 " + triangles[i].v3); } } gInfo.setPrimitive(GeometryInfo.TRIANGLE_ARRAY); gInfo.setContourCounts(null); gInfo.forgetOldPrim(); gInfo.setStripCounts(null); currIndex = 0; int newVertexIndices[] = new int[numTriangles*3]; int index; for(i=0; i<numTriangles; i++) { index = list[triangles[i].v1].getCommonIndex(); newVertexIndices[currIndex++] = vertexIndices[index]; index = list[triangles[i].v2].getCommonIndex(); newVertexIndices[currIndex++] = vertexIndices[index]; index = list[triangles[i].v3].getCommonIndex(); newVertexIndices[currIndex++] = vertexIndices[index]; } gInfo.setCoordinateIndices(newVertexIndices); /* for(i=0;i<newVertexIndices.length;) { System.out.println("v1 " + newVertexIndices[i++] + ", v2 " + newVertexIndices[i++] + ", v3 " + newVertexIndices[i++]); } System.out.println("Pysical point:"); for(i=0;i<newVertexIndices.length;) { System.out.println("v1 " + vertices[newVertexIndices[i++]] + ", v2 " + vertices[newVertexIndices[i++]] + ", v3 " + vertices[newVertexIndices[i++]]); } */ if(normals != null) { int oldNormalIndices[] = gInfo.getNormalIndices(); int newNormalIndices[] = new int[numTriangles*3]; currIndex = 0; for(i=0; i<numTriangles; i++) { index = list[triangles[i].v1].getCommonIndex(); newNormalIndices[currIndex++] = oldNormalIndices[index]; index = list[triangles[i].v2].getCommonIndex(); newNormalIndices[currIndex++] = oldNormalIndices[index]; index = list[triangles[i].v3].getCommonIndex(); newNormalIndices[currIndex++] = oldNormalIndices[index]; } gInfo.setNormalIndices(newNormalIndices); } if(colors != null) { currIndex = 0; int oldColorIndices[] = gInfo.getColorIndices(); int newColorIndices[] = new int[numTriangles*3]; for(i=0; i<numTriangles; i++) { index = list[triangles[i].v1].getCommonIndex(); newColorIndices[currIndex++] = oldColorIndices[index]; index = list[triangles[i].v2].getCommonIndex(); newColorIndices[currIndex++] = oldColorIndices[index]; index = list[triangles[i].v3].getCommonIndex(); newColorIndices[currIndex++] = oldColorIndices[index]; } gInfo.setColorIndices(newColorIndices); } for(int j = 0; j < numTexSets; j++) { int newTextureIndices[] = new int[numTriangles*3]; int oldTextureIndices[] = gInfo.getTextureCoordinateIndices(j); currIndex = 0; for(i=0; i<numTriangles; i++) { index = list[triangles[i].v1].getCommonIndex(); newTextureIndices[currIndex++] = oldTextureIndices[index]; index = list[triangles[i].v2].getCommonIndex(); newTextureIndices[currIndex++] = oldTextureIndices[index]; index = list[triangles[i].v3].getCommonIndex(); newTextureIndices[currIndex++] = oldTextureIndices[index]; } gInfo.setTextureCoordinateIndices(j, newTextureIndices); } } void setEpsilon(double eps) { epsilon = eps; } // Methods of handling ListNode. boolean inPolyList(int ind) { return ((ind >= 0) && (ind < numList) && (numList <= maxNumList)); } void updateIndex(int ind, int index) { // assert(InPolyList(ind)); list[ind].index = index; } int getAngle(int ind) { return list[ind].convex; } void setAngle(int ind, int convex) { list[ind].convex = convex; } void resetPolyList(int ind) { // assert(InPolyList(ind)); firstNode = ind; } int getNode() { // assert(InPolyList(first_node)); return firstNode; } boolean inLoopList(int loop) { return ((loop >= 0) && (loop < numLoops) && (numLoops <= maxNumLoops)); } void deleteHook(int currLoop) { int ind1, ind2; if(inLoopList(currLoop)==false) System.out.println("Triangulator:deleteHook : Loop access out of range."); ind1 = loops[currLoop]; ind2 = list[ind1].next; if((inPolyList(ind1))&&(inPolyList(ind2))) { deleteLinks(ind1); loops[currLoop] = ind2; } else System.out.println("Triangulator:deleteHook : List access out of range."); } /** * Deletes node ind from list (with destroying its data fields) */ void deleteLinks(int ind) { if((inPolyList(ind))&&(inPolyList(list[ind].prev))&& (inPolyList(list[ind].next))) { if (firstNode == ind) firstNode = list[ind].next; list[list[ind].next].prev = list[ind].prev; list[list[ind].prev].next = list[ind].next; list[ind].prev = list[ind].next = ind; } else System.out.println("Triangulator:deleteLinks : Access out of range."); } void rotateLinks(int ind1, int ind2) { int ind; int ind0, ind3; // assert(InPolyList(ind1)); // assert(InPolyList(ind2)); ind0 = list[ind1].next; ind3 = list[ind2].next; // assert(InPolyList(ind0)); // assert(InPolyList(ind3)); // Swap. ind = list[ind1].next; list[ind1].next = list[ind2].next; list[ind2].next = ind; list[ind0].prev = ind2; list[ind3].prev = ind1; } void storeChain(int ind) { if (numChains >= maxNumChains) { // System.out.println("Triangulator:storeChain Expanding chain array ..."); maxNumChains += 20; int old[] = chains; chains = new int[maxNumChains]; if(old != null) System.arraycopy(old, 0, chains, 0, old.length); } chains[numChains] = ind; ++numChains; } int getNextChain(boolean[] done) { if (numChains > 0) { done[0] = true; --numChains; return chains[numChains]; } else { done[0] = false; numChains = 0; return 0; } } void splitSplice(int ind1, int ind2, int ind3, int ind4) { list[ind1].next = ind4; list[ind4].prev = ind1; list[ind2].prev = ind3; list[ind3].next = ind2; } /** * Allocates storage for a dummy list node; pointers are set to itself. * @return pointer to node */ int makeHook() { int ind; ind = numList; if (numList >= maxNumList) { maxNumList += INC_LIST_BK; // System.out.println("Triangulator: Expanding list array ...."); ListNode old[] = list; list = new ListNode[maxNumList]; System.arraycopy(old, 0, list, 0, old.length); } list[numList] = new ListNode(-1); list[numList].prev = ind; list[numList].next = ind; list[numList].index = -1; ++numList; return ind; } int makeLoopHeader() { int i; int ind; ind = makeHook(); if(numLoops >= maxNumLoops) { maxNumLoops += INC_LOOP_BK; // System.out.println("Triangulator: Expanding loops array ...."); int old[] = loops; loops = new int[maxNumLoops]; System.arraycopy(old, 0, loops, 0, old.length); } loops[numLoops] = ind; i = numLoops; ++numLoops; return i; } /** * Allocates storage for a new list node, and stores the index of the point * at this node. Pointers are set to -1. * @return pointer to node */ int makeNode(int index) { int ind; if (numList >= maxNumList) { maxNumList += INC_LIST_BK; //System.out.println("Triangulator: Expanding list array ...."); ListNode old[] = list; list = new ListNode[maxNumList]; System.arraycopy(old, 0, list, 0, old.length); } list[numList] = new ListNode(index); ind = numList; list[numList].index = index; list[numList].prev = -1; list[numList].next = -1; ++numList; return ind; } /** * Inserts node ind2 after node ind1. */ void insertAfter(int ind1, int ind2) { int ind3; if((inPolyList(ind1))&&(inPolyList(ind2))) { list[ind2].next = list[ind1].next; list[ind2].prev = ind1; list[ind1].next = ind2; ind3 = list[ind2].next; if(inPolyList(ind3)) list[ind3].prev = ind2; else System.out.println("Triangulator:deleteHook : List access out of range."); return; } else System.out.println("Triangulator:deleteHook : List access out of range."); } /** * Returns pointer to the successor of ind1. */ int fetchNextData(int ind1) { return list[ind1].next; } /** * obtains the data store at ind1 */ int fetchData(int ind1) { return list[ind1].index; } /** * returns pointer to the successor of ind1. */ int fetchPrevData(int ind1) { return list[ind1].prev; } /** * swap the list pointers in order to change the orientation. */ void swapLinks(int ind1) { int ind2, ind3; ind2 = list[ind1].next; list[ind1].next = list[ind1].prev; list[ind1].prev = ind2; ind3 = ind2; while (ind2 != ind1) { ind3 = list[ind2].next; list[ind2].next = list[ind2].prev; list[ind2].prev = ind3; ind2 = ind3; } } // Methods for handling Triangle. void storeTriangle(int i, int j, int k) { /* if (ccwLoop) triangles.add(new Triangle(i,j,k)); else triangles.add(new Triangle(j,i,k)); */ if(numTriangles >= maxNumTriangles) { // System.out.println("Triangulator:storeTriangle Expanding triangle array.."); maxNumTriangles += INC_TRI_BK; Triangle old[] = triangles; triangles = new Triangle[maxNumTriangles]; if(old != null) System.arraycopy(old, 0, triangles, 0, old.length); } if (ccwLoop) triangles[numTriangles] = new Triangle(i,j,k); else triangles[numTriangles] = new Triangle(j,i,k); numTriangles++; } // Methods for handling Point. void initPnts(int number) { if (maxNumPoints < number) { maxNumPoints = number; points = new Point2f[maxNumPoints]; } for(int i = 0; i<number; i++) points[i] = new Point2f(0.0f, 0.0f); numPoints = 0; } boolean inPointsList(int index) { return ((index >= 0) && (index < numPoints) && (numPoints <= maxNumPoints)); } int storePoint(double x, double y) { int i; if (numPoints >= maxNumPoints) { // System.out.println("Triangulator:storePoint Expanding points array ..."); maxNumPoints += INC_POINT_BK; Point2f old[] = points; points = new Point2f[maxNumPoints]; if(old != null) System.arraycopy(old, 0, points, 0, old.length); } points[numPoints] = new Point2f((float)x, (float)y); // points[numPoints].x = (float)x; // points[numPoints].y = (float)y; i = numPoints; ++numPoints; return i; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -