📄 stripifier.java
字号:
seq1[1] = seq1[2]; seq1[2] = swap; } // find matches if ((seq[len-3].index == seq1[1].index) && (seq[len-1].index == seq1[0].index)) { // System.out.println("reduce0"); strm.swapEnd(); strm.append(seq1[1]); strm.addStream(strm1); faceTable[i] = EMPTY; faceTable[strm.tail] = id; faceTable[k] = EMPTY; } else if ((seq[len-3].index == seq1[0].index) && (seq[len-1].index == seq1[2].index)) { // System.out.println("reduce0"); seq1[0] = seq1[2]; strm.swapEnd(); strm.append(seq1[1]); strm.addStream(strm1); faceTable[i] = EMPTY; faceTable[strm.tail] = id; faceTable[k] = EMPTY; } else if ((seq[len-3].index == seq1[0].index) && (seq[len-1].index == seq1[1].index)) { // System.out.println("reduce0"); strm.swapEnd(); strm.addStream(strm1); faceTable[i] = EMPTY; faceTable[strm.tail] = id; faceTable[k] = EMPTY; } else if (sync) strm1.invert(); } } } } } /** * puts the stripified data back into the GeometryInfo object */ void putBackData(GeometryInfo gi, ArrayList strips) { int[] tempStripCounts = new int[strips.size()]; int ciSize = 0; int stripLength; for (int i = 0; i < strips.size();) { stripLength = ((Istream)strips.get(i)).length; if (stripLength != 0) { tempStripCounts[i] = stripLength; ciSize += stripLength; i++; } else { strips.remove(i); } } if (ciSize > 3) { gi.setPrimitive(gi.TRIANGLE_STRIP_ARRAY); int[] stripCounts = new int[strips.size()]; System.arraycopy(tempStripCounts, 0, stripCounts, 0, strips.size()); gi.setStripCounts(stripCounts); // create one array with all the strips int[] coords = new int[ciSize]; // create arrays for normals, textures and colors if necessary int[] normals = null; int[][] textures = null; int[] colors = null; javax.vecmath.Color3b[] stripColors = null; if (hasNormals) normals = new int[ciSize]; if (hasTextures) { textures = new int[texSetCount][ciSize]; } if (hasColors) colors = new int[ciSize]; if (colorStrips) { stripColors = new javax.vecmath.Color3b[ciSize]; colors = new int[ciSize]; } int count = 0; Istream currStrip; for (int i = 0; i < strips.size(); i++) { currStrip = (Istream)strips.get(i); if (currStrip.length < 3) { throw new RuntimeException("currStrip.length = " + currStrip.length); } java.awt.Color stripColor = null; if (colorStrips) { int r = ((int)(Math.random()*1000))%255; int g = ((int)(Math.random()*1000))%255; int b = ((int)(Math.random()*1000))%255; stripColor = new java.awt.Color(r, g, b); } for (int j = 0; j < currStrip.length; j++) { coords[count] = currStrip.istream[j].index; if (hasNormals) normals[count] = currStrip.istream[j].normal; if (hasTextures) { for (int k = 0; k < texSetCount; k++) { textures[k][count] = currStrip.istream[j].texture[k]; } } if (hasColors) colors[count] = currStrip.istream[j].color; if (colorStrips) stripColors[count] = new javax.vecmath.Color3b(stripColor); count++; } } gi.setCoordinateIndices(coords); if (hasNormals) gi.setNormalIndices(normals); if (hasTextures) { for (int i = 0; i < texSetCount; i++) { gi.setTextureCoordinateIndices(i, textures[i]); } } if (hasColors) gi.setColorIndices(colors); if (colorStrips) { gi.setColors(stripColors); colors = gi.getListIndices(stripColors); gi.setColorIndices(colors); } } } /** * Stores the infomration about a vertex */ class Vertex { int index; int normal = EMPTY; int numTexSets = 0; int[] texture = null; int color = EMPTY; Vertex(int vertIndex) { this(vertIndex, EMPTY, 0, null, EMPTY); } Vertex(int vertIndex, int vertNormal, int vertNumTexSets, int[] vertTexture, int vertColor) { index = vertIndex; normal = vertNormal; numTexSets = vertNumTexSets; if (numTexSets > 0) { texture = new int[numTexSets]; System.arraycopy(vertTexture, 0, texture, 0, numTexSets); } color = vertColor; } boolean equals(Vertex v) { for (int i = 0; i < numTexSets; i++) { if (texture[i] != v.texture[i]) { return false; } } return ((v.index == index) && (v.normal == normal) && (v.color == color)); } // will this yield same results as c code ??? boolean lessThan(Vertex v) { if (index < v.index) return true; if (index > v.index) return false; if (normal < v.normal) return true; if (normal > v.normal) return false; for (int i = 0; i < numTexSets; i++) { if (texture[i] < v.texture[i]) return true; if (texture[i] > v.texture[i]) return false; } if (color < v.color) return true; if (color > v.color) return false; return false; } } /** * Stores the information about an edge of a triangle */ class Edge { Vertex v1, v2; int face; Edge(Vertex vertex1, Vertex vertex2, int faceIndex) { face = faceIndex; // this could be causing wrapping problem if (vertex1.lessThan(vertex2)) { v1 = vertex1; v2 = vertex2; } else { v1 = vertex2; v2 = vertex1; } } /** * Determine whether the edges have the same vertices */ boolean equals(Edge edge) { return ((v1.equals(edge.v1)) && (v2.equals(edge.v2))); } /** * Used to sort the edges. If this is less than the edge parameter, * return true. First check if vertex1 is less than vertex1 of the * edge provided. If so, return true. If the first vertices are equal * then check vertex2. */ boolean lessThan(Edge edge) { if (v1.lessThan(edge.v1)) return true; else if (v1.equals(edge.v1)) return (v2.lessThan(edge.v2)); else return false; } } /** * Stores the information about the face of a triangle */ class Face { int key; int numNhbrs = 0; Vertex[] verts = null; // edges are kept in order s.t. the ith edge is the opposite // edge of the ith vertex Edge[] edges = null; /** * Creates a new Face with the three given vertices */ Face(int index, Vertex v1, Vertex v2, Vertex v3) { key = index; verts = new Vertex[3]; verts[0] = v1; verts[1] = v2; verts[2] = v3; edges = new Edge[3]; edges[0] = null; edges[1] = null; edges[2] = null; numNhbrs = 3; } /** * returns the index of the face that neighbors the edge supplied * by the parameter */ int getNeighbor(int edge) { return edges[edge].face; } /** * returns the index of the edge that is shared by the triangle * specified by the key parameter */ int findSharedEdge(int key) { if (edges[0].face == key) return 0; else if (edges[1].face == key) return 1; else if (edges[2].face == key) return 2; else return -1; /* error */ } int getEdgeIndex(Edge edge) { if (edges[0].equals(edge)) return 0; else if (edges[1].equals(edge)) return 1; else return 2; } void counterEdgeDel(Edge edge) { if (DEBUG) { System.out.println("counterEdgeDel"); } if ((edges[0]).equals(edge)) { edges[0].face = EMPTY; numNhbrs--; } else if ((edges[1]).equals(edge)) { edges[1].face = EMPTY; numNhbrs--; } else if ((edges[2]).equals(edge)) { edges[2].face = EMPTY; numNhbrs--; } else { if (DEBUG) { System.out.println("error in counterEdgeDel"); } } } void printAdjacency() { System.out.println("Face " + key + ": "); System.out.println("\t numNhbrs = " + numNhbrs); System.out.println("\t edge 0: Face " + edges[0].face); System.out.println("\t edge 1: Face " + edges[1].face); System.out.println("\t edge 2: Face " + edges[2].face); } void printVertices() { System.out.println("Face " + key + ": (" + verts[0].index + ", " + verts[1].index + ", " + verts[2].index + ")"); } } /** * stores the information for a face node */ class Node { Face face; // the data: the face Node parent; // the parent node Node left; // the left child Node right; // the right child int depth; // the topological distance of the node from the root int numChildren; // the number of children int attrib; // characteristic of the node eg. color // the attributes - 3 states for the Node static final int WHITE = 0; // not being accessed yet static final int GREY = 1; // being accessed but not done yet static final int BLACK = 2; // done Node(Face f) { face = f; } /** * inserts this node below the parent supplied. */ void insert(Node p) { parent = p; depth = p.depth + 1; attrib = GREY; if (parent.left == null) parent.left = this; else parent.right = this; (parent.numChildren)++; } /** * remove this node from its parent */ void remove() { if (parent != null) { if (parent.left == this) { parent.left = parent.right; parent.right = null; } else { parent.right = null; } (parent.numChildren)--; } } /** * sets the depth to 0 and the attrib to GREY */ void setRoot() { depth = 0; attrib = GREY; } /** * returns true if the attrib is WHITE */ boolean notAccessed() { return (attrib == WHITE); } /** * sets the color to BLACK */ void processed() { attrib = BLACK; } /** * a node is the root if it doesn't have a parent */ boolean isRoot() { return (parent == null); } /** * prints the information in this Node */ void print() { System.out.println(this); System.out.println("Node depth: " + depth); face.printVertices(); System.out.print("parent: "); if (parent != null) parent.face.printVertices(); else System.out.println("null"); System.out.print("left: "); if (left != null) left.face.printVertices(); else System.out.println("null"); System.out.print("right: "); if (right != null) right.face.printVertices(); else System.out.println("null"); System.out.println("attrib: " + attrib); System.out.println(""); } } /** * sorts the Nodes by depth */ class SortedList { ArrayList list; /** * create a new SortedList */ SortedList() { list = new ArrayList(); } /** * insert into the list sorted by depth. start looking at start * to save some time. Returns the index of the next item of the * inserted element */ int sortedInsert(Node data, int start) { // adjust start to where insert sorted while ((start < list.size()) && (((Node)list.get(start)).depth <= data.depth)) { start++; } // insert at start index list.add(start, data); // return start+1 -- the index of the next element return (start+1); } /** * remove and return 1st element */ Node pop() { if (!list.isEmpty()) return (Node)list.remove(0); else return null; } } class Istream { // fan encoding boolean fan = false; // length of the strip int length = 0; // array that specifies triangle strip Vertex[] istream; // indices of the head and tail vertices int head, tail; /** * creates a new Istream to store the triangle strip */ Istream(Vertex[] list, int size, boolean isFan) { if (size == 0) throw new RuntimeException("size is 0"); fan = isFan; length = size; istream = new Vertex[length]; int i; System.arraycopy(list, 0, istream, 0, length); } /** * adds a new vertex to the end of the stream * makes the int array bigger, if necessary */ v
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -