View Javadoc
1   package com.github.celldynamics.quimp.utils.graphics.vrml;
2   
3   import java.io.File;
4   import java.io.FileWriter;
5   import java.io.PrintWriter;
6   
7   import org.scijava.vecmath.Color3f;
8   import org.scijava.vecmath.Point3f;
9   
10  /**
11   *
12   * @author rtyson
13   */
14  public class VRMLobject {
15  
16    private Point3f[] coords;
17    private Color3f[] colorsF;
18    private int[] coordIndices;
19  
20    private boolean ccw = true;
21    private boolean colorPerVertex = true;
22    private boolean normalPerVertex = true;
23    private boolean convex = true;
24    private float creaseAngle = 2.0f;
25    private boolean solid = true;
26  
27    /**
28     * Instantiates a new VRM lobject.
29     *
30     * @param p the p
31     * @param c the c
32     * @param i the i
33     */
34    public VRMLobject(Point3f[] p, Color3f[] c, int[] i) {
35      coords = p;
36      colorsF = c;
37      coordIndices = i;
38    }
39  
40    /**
41     * Transform.
42     *
43     * @param x the x
44     * @param y the y
45     * @param z the z
46     */
47    public void transform(float x, float y, float z) {
48      Point3f tpf = new Point3f(x, y, z);
49  
50      for (int i = 0; i < coords.length; i++) {
51        coords[i].add(tpf);
52      }
53    }
54  
55    /**
56     * Scale.
57     *
58     * @param x the x
59     */
60    public void scale(float x) {
61      for (int i = 0; i < coords.length; i++) {
62        coords[i].scale(x);
63      }
64    }
65  
66    /**
67     * Write.
68     *
69     * @param out the out
70     */
71    public void write(File out) {
72      try {
73        PrintWriter pw = new PrintWriter(new FileWriter(out), true);
74  
75        pw.print("#VRML V2.0 utf8\nShape {\n\tgeometry IndexedFaceSet {");
76        pw.print("\n\t\tccw ");
77        writeBoolean(pw, ccw);
78        pw.print("\n\t\tcolorPerVertex ");
79        writeBoolean(pw, colorPerVertex);
80        pw.print("\n\t\tnormalPerVertex ");
81        writeBoolean(pw, normalPerVertex);
82        pw.print("\n\t\tconvex ");
83        writeBoolean(pw, convex);
84        pw.print("\n\t\tcreaseAngle " + creaseAngle);
85        pw.print("\n\t\tsolid ");
86        writeBoolean(pw, solid);
87  
88        pw.print("\n\t\tcoord Coordinate {\n\t\t\tpoint [");
89        // write coords
90        for (int i = 0; i < coords.length; i++) {
91          pw.print("\n\t\t\t\t" + coords[i].x + " " + coords[i].y + " " + coords[i].z);
92          if (i != coords.length - 1) {
93            pw.print(",");
94            // break;
95          }
96        }
97        pw.print("\n\t\t\t]\n\t\t}");
98  
99        pw.print("\n\t\tcolor Color {\n\t\t\tcolor [");
100       // write colors
101       for (int i = 0; i < colorsF.length; i++) {
102         pw.print("\n\t\t\t\t" + colorsF[i].x + " " + colorsF[i].y + " " + colorsF[i].z);
103         if (i != colorsF.length - 1) {
104           pw.print(",");
105           // break;
106         }
107       }
108       pw.print("\n\t\t\t]\n\t\t}");
109 
110       pw.print("\n\t\tcoordIndex [");
111       // write coordIndices
112       for (int i = 0; i < coordIndices.length; i += 4) {
113         pw.print("\n\t\t\t" + coordIndices[i] + ", " + coordIndices[i + 1] + ", "
114                 + coordIndices[i + 2] + ", " + coordIndices[i + 3] + ", -1");
115         if (i != coordIndices.length - 4) {
116           pw.print(",");
117           // break;
118         }
119       }
120       pw.print("\n\t\t]");
121 
122       pw.print("\n\t}\n}");
123 
124       pw.close();
125 
126     } catch (Exception e) {
127       System.out.println("Could not write VMRL object to " + out.getName());
128     }
129   }
130 
131   private void writeBoolean(PrintWriter pw, boolean b) {
132     if (b) {
133       pw.print("TRUE");
134     } else {
135       pw.print("FALSE");
136     }
137   }
138 }