📄 omgraphicutil.java
字号:
HashSet set = new HashSet(); set.add(shape); return set.iterator(); } /** * Create an Iterator containing a set of Shape3D objects, created * from a java.awt.Shape. * * @param shape java.awt.Shape object. * @param baselineHeight the baselined height for all the values * in the grid, if the OMGridGenerator wants to use it. * @param color the color to make the object. * @param filled whether or not to fill the object with color. * @return Iterator containing Shape3D objects created from shape * object. */ public static Iterator createShape3D(Shape shape, double baselineHeight, Color color, boolean filled) { int bufferSize = DEFAULT_NPOINTS_BUFFER_SIZE; double[] data = expandArrayD(bufferSize, null); int dataIndex = 0; // How many spaces are left in the buffer. int refreshCounter = bufferSize; int[] stripCount = new int[1]; stripCount[0] = 0; // null is AffineTransform... PathIterator pi2 = shape.getPathIterator(null); // flatness might need to be calculated, based // on scale or something. Depends on how many // points there should be for an accurate // shape rendition. float flatness = .25f; FlatteningPathIterator pi = new FlatteningPathIterator(pi2, flatness); double[] coords = new double[6]; double pntx = 0; double pnty = 0; double pntz = baselineHeight; HashSet set = new HashSet(); Shape3D shape3D = null; Debug.message("3detail", "OMGraphicUtil.createShape3D(): figuring out coordinates"); // Creating the data[] while (!pi.isDone()) { int type = pi.currentSegment(coords); switch (type) { case PathIterator.SEG_MOVETO: if (dataIndex != 0) { shape3D = createShape3D(data, dataIndex, stripCount, color, filled); if (shape3D != null) { set.add(shape3D); } data = expandArrayD(bufferSize, null); dataIndex = 0; } case PathIterator.SEG_LINETO: // SEG_MOVETO is the first point of // the shape, SEG_LINETO are the // middle and end points. SEG_CLOSE // confirms the close, but we don't // need it. pntx = coords[0]; pnty = coords[1]; if (Debug.debugging("3detail")) { Debug.output("Shape coordinates: " + pntx + ", " + pnty); } // Get Z here, if you want to set the height of the // coordinate... // pntz = // See if there is space in the buffer. if (dataIndex >= data.length) { data = expandArrayD(bufferSize, data); refreshCounter = bufferSize; } data[dataIndex++] = pntx; data[dataIndex++] = pntz; data[dataIndex++] = pnty; // data[dataIndex++] = pntx; // data[dataIndex++] = pnty; // data[dataIndex++] = pntz; stripCount[0]++; refreshCounter -= 3; break; default: // Do nothing, because it's a repeat // of the last SEG_LINETO point. Debug.message("3detail", "Shape coordinates: " + coords[0] + ", " + coords[1] + " rounding out SEG_CLOSE"); } pi.next(); } if (dataIndex != 0) { shape3D = createShape3D(data, dataIndex, stripCount, color, filled); if (shape3D != null) { set.add(shape3D); } } return set.iterator(); } /** * Create a Shape3D from raw components. May return null. Assumes * a stripCount array of size one. * * @param data Description of the Parameter * @param realDataIndex Description of the Parameter * @param stripCount Description of the Parameter * @param color Description of the Parameter * @param filled Description of the Parameter * @return Description of the Return Value */ public static Shape3D createShape3D(double[] data, int realDataIndex, int[] stripCount, Color color, boolean filled) { try { double[] newData = new double[realDataIndex]; System.arraycopy(data, 0, newData, 0, realDataIndex); if (filled) { return createFilled(newData, stripCount, color); } else { return createEdges(newData, color); } } catch (java.lang.IllegalArgumentException iae) { Debug.error("OMGraphicUtil.createShape3D(): IllegalArgumentException caught: \n" + iae.toString()); StringBuffer sb = new StringBuffer(); for (int i = 0; i < stripCount.length; i++) { sb.append("{" + stripCount[i] + "}"); } Debug.output("Something funny happened on " + (filled ? "filled" : "edge") + " data[" + data.length + "], reflecting " + data.length / 3 + " nodes, with stripCount[" + stripCount.length + "] " + sb.toString()); } return null; } public static Shape3D createFilled(double[] data, int[] stripCount, Color color) throws IllegalArgumentException { // j + 1 is the number of shapes. // Might have to track the number of coordinates per shape. // Use a Triangulator to take geometry data and create // polygons out of it. Debug.message("3detail", "OMGraphicUtil: adding polygon, data length " + data.length + ", reflecting " + data.length / 3 + " nodes, with a strip count of " + stripCount); GeometryInfo gi = new GeometryInfo(GeometryInfo.POLYGON_ARRAY); gi.setCoordinates(data); gi.setStripCounts(stripCount); Triangulator tr = new Triangulator(); // Triangulator tr = new Triangulator(1); Debug.message("3detail", "OMGraphicUtil: begin triangulation"); tr.triangulate(gi); Debug.message("3detail", "OMGraphicUtil: end triangulation"); gi.recomputeIndices(); NormalGenerator ng = new NormalGenerator(); ng.generateNormals(gi); gi.recomputeIndices(); Stripifier st = new Stripifier(); st.stripify(gi); gi.recomputeIndices(); Shape3D shape3D = new Shape3D(); shape3D.setAppearance(createMaterialAppearance(color)); shape3D.setGeometry(gi.getGeometryArray()); return shape3D; } public static Shape3D createEdges(double[] data, Color color) throws IllegalArgumentException { int numPoints = data.length / 3; // Create a line for the polyline. Debug.message("3detail", "OMGraphicUtil: adding polyline of " + numPoints + " points."); LineStripArray la = new LineStripArray(numPoints, LineArray.COORDINATES | LineArray.COLOR_4, new int[] { numPoints }); la.setCoordinates(0, data); Color4b[] colors = createColorArray(numPoints, color); la.setColors(0, colors); return new Shape3D(la); } public static Appearance createMaterialAppearance(Color color) { Appearance materialAppear = new Appearance(); PolygonAttributes polyAttrib = new PolygonAttributes(); polyAttrib.setCullFace(PolygonAttributes.CULL_NONE); materialAppear.setPolygonAttributes(polyAttrib); Material material = new Material(); // Might want to look into using a Color4b at some point material.setAmbientColor(new Color3f(color)); materialAppear.setMaterial(material); return materialAppear; } public static Appearance createWireFrameAppearance(Color color) { Appearance materialAppear = new Appearance(); PolygonAttributes polyAttrib = new PolygonAttributes(); polyAttrib.setPolygonMode(PolygonAttributes.POLYGON_LINE); materialAppear.setPolygonAttributes(polyAttrib); ColoringAttributes redColoring = new ColoringAttributes(); redColoring.setColor(1.0f, 0.0f, 0.0f); materialAppear.setColoringAttributes(redColoring); return materialAppear; } /** * Create an array of Color4b objects for an OMGraphic * representation. The colors in an OMGraphic are ARGB, which is * why this creates a Color4b object. Since all the parts of an * OMGraphic are colored the same, create the array of colors to * be all the same color retrieved from the OMGraphic. * * @param size Description of the Parameter * @param color Description of the Parameter * @return Description of the Return Value */ public static Color4b[] createColorArray(int size, Color color) { Color4b[] colors = new Color4b[size]; for (int i = 0; i < size; i++) { colors[i] = new Color4b(color); } return colors; } /** * Create an array to hold double data for 3d polygons and lines. * * @param bufferSize the number of * * <pre> * points * </pre> * * to buffer. Equals three doubles per point. * @param currentArray if not null, will create an array the size * of the current array plus the size needed to hold the * desired number of points. * @return a double[]. */ public static double[] expandArrayD(int bufferSize, double[] currentArray) { if (currentArray == null) { return new double[bufferSize * 3]; } int length = currentArray.length; double[] ret = new double[length + bufferSize * 3]; System.arraycopy(currentArray, 0, ret, 0, length); return ret; } /** * Create an array to hold float data for 3d polygons and lines. * * @param bufferSize the number of * * <pre> * points * </pre> * * to buffer. Equals three floats per point. * @param currentArray if not null, will create an array the size * of the current array plus the size needed to hold the * desired number of points. * @return a float[]. */ public static float[] expandArrayF(int bufferSize, float[] currentArray) { if (currentArray == null) { return new float[bufferSize * 3]; } int length = currentArray.length; float[] ret = new float[length + bufferSize * 3]; System.arraycopy(currentArray, 0, ret, 0, length); return ret; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -