propane 3.3.1-java → 3.6.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (157) hide show
  1. checksums.yaml +4 -4
  2. data/.mvn/extensions.xml +1 -2
  3. data/.mvn/wrapper/MavenWrapperDownloader.java +117 -0
  4. data/.mvn/wrapper/maven-wrapper.properties +2 -3
  5. data/.travis.yml +9 -0
  6. data/CHANGELOG.md +17 -5
  7. data/Gemfile +2 -0
  8. data/README.md +17 -8
  9. data/Rakefile +16 -30
  10. data/bin/propane +3 -1
  11. data/lib/propane.rb +6 -4
  12. data/lib/propane/app.rb +20 -10
  13. data/lib/propane/creators/sketch_class.rb +7 -1
  14. data/lib/propane/creators/sketch_factory.rb +4 -2
  15. data/lib/propane/creators/sketch_writer.rb +1 -0
  16. data/lib/propane/helper_methods.rb +23 -24
  17. data/lib/propane/helpers/numeric.rb +2 -0
  18. data/lib/propane/helpers/version_error.rb +1 -0
  19. data/lib/propane/library.rb +5 -1
  20. data/lib/propane/library_loader.rb +2 -0
  21. data/lib/propane/native_folder.rb +21 -15
  22. data/lib/propane/native_loader.rb +3 -0
  23. data/lib/propane/runner.rb +14 -6
  24. data/lib/propane/version.rb +2 -1
  25. data/library/boids/boids.rb +21 -11
  26. data/library/color_group/color_group.rb +28 -0
  27. data/library/control_panel/control_panel.rb +8 -5
  28. data/library/dxf/dxf.rb +6 -0
  29. data/library/file_chooser/chooser.rb +10 -9
  30. data/library/file_chooser/file_chooser.rb +10 -9
  31. data/library/library_proxy/library_proxy.rb +2 -0
  32. data/library/net/net.rb +7 -0
  33. data/library/simplex_noise/simplex_noise.rb +2 -0
  34. data/library/slider/slider.rb +23 -22
  35. data/library/vector_utils/vector_utils.rb +4 -0
  36. data/library/video_event/video_event.rb +4 -1
  37. data/mvnw +127 -51
  38. data/mvnw.cmd +182 -145
  39. data/pom.rb +53 -50
  40. data/pom.xml +17 -8
  41. data/propane.gemspec +13 -11
  42. data/src/main/java/monkstone/ColorUtil.java +13 -1
  43. data/src/main/java/monkstone/MathToolModule.java +253 -203
  44. data/src/main/java/monkstone/PropaneLibrary.java +2 -2
  45. data/src/main/java/monkstone/fastmath/Deglut.java +1 -1
  46. data/src/main/java/monkstone/filechooser/Chooser.java +2 -1
  47. data/src/main/java/monkstone/noise/SimplexNoise.java +2 -2
  48. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +1 -1
  49. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +1 -1
  50. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +1 -1
  51. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +1 -1
  52. data/src/main/java/monkstone/slider/SliderBar.java +1 -1
  53. data/src/main/java/monkstone/slider/SliderGroup.java +1 -1
  54. data/src/main/java/monkstone/slider/WheelHandler.java +7 -6
  55. data/src/main/java/monkstone/vecmath/package-info.java +1 -1
  56. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +2 -2
  57. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +1 -1
  58. data/src/main/java/monkstone/videoevent/CaptureEvent.java +27 -0
  59. data/src/main/java/monkstone/videoevent/{VideoInterface.java → MovieEvent.java} +11 -27
  60. data/src/main/java/monkstone/videoevent/package-info.java +1 -1
  61. data/src/main/java/processing/awt/PGraphicsJava2D.java +2164 -1661
  62. data/src/main/java/processing/awt/PImageAWT.java +377 -0
  63. data/src/main/java/processing/awt/PShapeJava2D.java +280 -268
  64. data/src/main/java/processing/awt/PSurfaceAWT.java +942 -829
  65. data/src/main/java/processing/awt/ShimAWT.java +581 -0
  66. data/src/main/java/processing/core/PApplet.java +831 -824
  67. data/src/main/java/processing/core/PConstants.java +477 -447
  68. data/src/main/java/processing/core/PFont.java +914 -880
  69. data/src/main/java/processing/core/PGraphics.java +229 -213
  70. data/src/main/java/processing/core/PImage.java +620 -318
  71. data/src/main/java/processing/core/PMatrix.java +172 -159
  72. data/src/main/java/processing/core/PMatrix2D.java +478 -409
  73. data/src/main/java/processing/core/PMatrix3D.java +762 -735
  74. data/src/main/java/processing/core/PShape.java +2888 -2652
  75. data/src/main/java/processing/core/PShapeOBJ.java +436 -415
  76. data/src/main/java/processing/core/PShapeSVG.java +1702 -1479
  77. data/src/main/java/processing/core/PStyle.java +40 -37
  78. data/src/main/java/processing/core/PSurface.java +139 -97
  79. data/src/main/java/processing/core/PSurfaceNone.java +296 -208
  80. data/src/main/java/processing/core/PVector.java +997 -965
  81. data/src/main/java/processing/core/ThinkDifferent.java +12 -17
  82. data/src/main/java/processing/data/DoubleDict.java +756 -710
  83. data/src/main/java/processing/data/DoubleList.java +749 -696
  84. data/src/main/java/processing/data/FloatDict.java +748 -702
  85. data/src/main/java/processing/data/FloatList.java +751 -697
  86. data/src/main/java/processing/data/IntDict.java +720 -673
  87. data/src/main/java/processing/data/IntList.java +699 -633
  88. data/src/main/java/processing/data/JSONArray.java +931 -873
  89. data/src/main/java/processing/data/JSONObject.java +1262 -1165
  90. data/src/main/java/processing/data/JSONTokener.java +351 -341
  91. data/src/main/java/processing/data/LongDict.java +710 -663
  92. data/src/main/java/processing/data/LongList.java +701 -635
  93. data/src/main/java/processing/data/Sort.java +37 -41
  94. data/src/main/java/processing/data/StringDict.java +525 -486
  95. data/src/main/java/processing/data/StringList.java +626 -580
  96. data/src/main/java/processing/data/Table.java +3690 -3510
  97. data/src/main/java/processing/data/TableRow.java +182 -183
  98. data/src/main/java/processing/data/XML.java +957 -883
  99. data/src/main/java/processing/dxf/RawDXF.java +404 -0
  100. data/src/main/java/processing/event/Event.java +87 -66
  101. data/src/main/java/processing/event/KeyEvent.java +48 -41
  102. data/src/main/java/processing/event/MouseEvent.java +88 -113
  103. data/src/main/java/processing/event/TouchEvent.java +10 -6
  104. data/src/main/java/processing/javafx/PGraphicsFX2D.java +20 -345
  105. data/src/main/java/processing/javafx/PSurfaceFX.java +149 -121
  106. data/src/main/java/processing/net/Client.java +744 -0
  107. data/src/main/java/processing/net/Server.java +388 -0
  108. data/src/main/java/processing/opengl/FontTexture.java +289 -270
  109. data/src/main/java/processing/opengl/FrameBuffer.java +386 -364
  110. data/src/main/java/processing/opengl/LinePath.java +547 -500
  111. data/src/main/java/processing/opengl/LineStroker.java +588 -581
  112. data/src/main/java/processing/opengl/PGL.java +3047 -2914
  113. data/src/main/java/processing/opengl/PGraphics2D.java +408 -315
  114. data/src/main/java/processing/opengl/PGraphics3D.java +107 -72
  115. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +12378 -12075
  116. data/src/main/java/processing/opengl/PJOGL.java +1753 -1670
  117. data/src/main/java/processing/opengl/PShader.java +1266 -1257
  118. data/src/main/java/processing/opengl/PShapeOpenGL.java +4678 -4580
  119. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1114 -1027
  120. data/src/main/java/processing/opengl/Texture.java +1492 -1401
  121. data/src/main/java/processing/opengl/VertexBuffer.java +57 -55
  122. data/test/create_test.rb +21 -20
  123. data/test/deglut_spec_test.rb +4 -2
  124. data/test/helper_methods_test.rb +49 -20
  125. data/test/math_tool_test.rb +39 -32
  126. data/test/native_folder.rb +47 -0
  127. data/test/respond_to_test.rb +3 -2
  128. data/test/sketches/key_event.rb +2 -2
  129. data/test/sketches/library/my_library/my_library.rb +3 -0
  130. data/test/test_helper.rb +2 -0
  131. data/test/vecmath_spec_test.rb +35 -22
  132. data/vendors/Rakefile +33 -62
  133. metadata +54 -45
  134. data/src/main/java/processing/core/util/image/ImageLoadFacade.java +0 -161
  135. data/src/main/java/processing/core/util/image/ImageSaveFacade.java +0 -169
  136. data/src/main/java/processing/core/util/image/constants/TifConstants.java +0 -45
  137. data/src/main/java/processing/core/util/image/load/AwtImageLoadStrategy.java +0 -80
  138. data/src/main/java/processing/core/util/image/load/Base64StringImageLoadStrategy.java +0 -73
  139. data/src/main/java/processing/core/util/image/load/FallbackImageLoadStrategy.java +0 -70
  140. data/src/main/java/processing/core/util/image/load/ImageIoImageLoadStrategy.java +0 -132
  141. data/src/main/java/processing/core/util/image/load/ImageLoadStrategy.java +0 -48
  142. data/src/main/java/processing/core/util/image/load/ImageLoadUtil.java +0 -45
  143. data/src/main/java/processing/core/util/image/load/TgaImageLoadStrategy.java +0 -255
  144. data/src/main/java/processing/core/util/image/load/TiffImageLoadStrategy.java +0 -98
  145. data/src/main/java/processing/core/util/image/save/ImageSaveStrategy.java +0 -49
  146. data/src/main/java/processing/core/util/image/save/ImageSaveUtil.java +0 -48
  147. data/src/main/java/processing/core/util/image/save/ImageWriterImageSaveStrategy.java +0 -179
  148. data/src/main/java/processing/core/util/image/save/SaveImageException.java +0 -41
  149. data/src/main/java/processing/core/util/image/save/TgaImageSaveStrategy.java +0 -198
  150. data/src/main/java/processing/core/util/image/save/TiffImageSaveStrategy.java +0 -91
  151. data/src/main/java/processing/core/util/image/save/TiffNakedFilenameImageSaveStrategy.java +0 -57
  152. data/src/main/java/processing/core/util/io/InputFactory.java +0 -285
  153. data/src/main/java/processing/core/util/io/PathUtil.java +0 -109
  154. data/src/main/java/processing/opengl/shaders/LightVert-brcm.glsl +0 -154
  155. data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +0 -154
  156. data/src/main/java/processing/opengl/shaders/TexLightVert-brcm.glsl +0 -160
  157. data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +0 -160
@@ -2,461 +2,482 @@ package processing.core;
2
2
 
3
3
  import java.io.BufferedReader;
4
4
  import java.io.File;
5
+ import java.io.IOException;
5
6
  import java.util.ArrayList;
6
7
  import java.util.HashMap;
7
8
  import java.util.Map;
8
9
 
9
10
  /**
10
- * This class is not part of the Processing API and should not be used directly.
11
- * Instead, use loadShape() and methods like it, which will make use of this
12
- * class. Using this class directly will cause your code to break when combined
13
- * with future versions of Processing.
11
+ * This class is not part of the Processing API and should not be used
12
+ * directly. Instead, use loadShape() and methods like it, which will make
13
+ * use of this class. Using this class directly will cause your code to break
14
+ * when combined with future versions of Processing.
14
15
  * <p>
15
16
  * OBJ loading implemented using code from Saito's OBJLoader library:
16
- * http://code.google.com/p/saitoobjloader/ and OBJReader from Ahmet Kizilay
17
+ * http://code.google.com/p/saitoobjloader/
18
+ * and OBJReader from Ahmet Kizilay
17
19
  * http://www.openprocessing.org/visuals/?visualID=191
18
20
  *
19
21
  */
20
22
  public class PShapeOBJ extends PShape {
21
23
 
22
- /**
23
- * Initializes a new OBJ Object with the given filename.
24
- */
25
- public PShapeOBJ(PApplet parent, String filename) {
26
- this(parent, parent.createReader(filename), getBasePath(parent, filename));
24
+ /**
25
+ * Initializes a new OBJ Object with the given filename.
26
+ * @param parent
27
+ * @param filename
28
+ */
29
+ public PShapeOBJ(PApplet parent, String filename) {
30
+ this(parent, parent.createReader(filename), getBasePath(parent, filename));
31
+ }
32
+
33
+ public PShapeOBJ(PApplet parent, BufferedReader reader) {
34
+ this(parent, reader, "");
35
+ }
36
+
37
+ public PShapeOBJ(PApplet parent, BufferedReader reader, String basePath) {
38
+ ArrayList<OBJFace> faces = new ArrayList<>();
39
+ ArrayList<OBJMaterial> materials = new ArrayList<>();
40
+ ArrayList<PVector> coords = new ArrayList<>();
41
+ ArrayList<PVector> normals = new ArrayList<>();
42
+ ArrayList<PVector> texcoords = new ArrayList<>();
43
+ parseOBJ(parent, basePath, reader,
44
+ faces, materials, coords, normals, texcoords);
45
+
46
+ // The OBJ geometry is stored with each face in a separate child shape.
47
+ parent = null;
48
+ family = GROUP;
49
+ addChildren(faces, materials, coords, normals, texcoords);
50
+ }
51
+
52
+
53
+ protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
54
+ ArrayList<PVector> coords,
55
+ ArrayList<PVector> normals,
56
+ ArrayList<PVector> texcoords) {
57
+ family = GEOMETRY;
58
+ switch (face.vertIdx.size()) {
59
+ case 3:
60
+ kind = TRIANGLES;
61
+ break;
62
+ case 4:
63
+ kind = QUADS;
64
+ break;
65
+ default:
66
+ kind = POLYGON;
67
+ break;
27
68
  }
28
69
 
29
- public PShapeOBJ(PApplet parent, BufferedReader reader) {
30
- this(parent, reader, "");
70
+ stroke = false;
71
+ fill = true;
72
+
73
+ // Setting material properties for the new face
74
+ fillColor = rgbaValue(mtl.kd);
75
+ ambientColor = rgbaValue(mtl.ka);
76
+ specularColor = rgbaValue(mtl.ks);
77
+ shininess = mtl.ns;
78
+ if (mtl.kdMap != null) {
79
+ // If current material is textured, then tinting the texture using the
80
+ // diffuse color.
81
+ tintColor = rgbaValue(mtl.kd, mtl.d);
31
82
  }
32
83
 
33
- public PShapeOBJ(PApplet parent, BufferedReader reader, String basePath) {
34
- ArrayList<OBJFace> faces = new ArrayList<OBJFace>();
35
- ArrayList<OBJMaterial> materials = new ArrayList<OBJMaterial>();
36
- ArrayList<PVector> coords = new ArrayList<PVector>();
37
- ArrayList<PVector> normals = new ArrayList<PVector>();
38
- ArrayList<PVector> texcoords = new ArrayList<PVector>();
39
- parseOBJ(parent, basePath, reader,
40
- faces, materials, coords, normals, texcoords);
41
-
42
- // The OBJ geometry is stored with each face in a separate child shape.
43
- parent = null;
44
- family = GROUP;
45
- addChildren(faces, materials, coords, normals, texcoords);
46
- }
47
-
48
- protected PShapeOBJ(OBJFace face, OBJMaterial mtl,
49
- ArrayList<PVector> coords,
50
- ArrayList<PVector> normals,
51
- ArrayList<PVector> texcoords) {
52
- family = GEOMETRY;
53
- if (face.vertIdx.size() == 3) {
54
- kind = TRIANGLES;
55
- } else if (face.vertIdx.size() == 4) {
56
- kind = QUADS;
57
- } else {
58
- kind = POLYGON;
59
- }
60
-
61
- stroke = false;
62
- fill = true;
63
-
64
- // Setting material properties for the new face
65
- fillColor = rgbaValue(mtl.kd);
66
- ambientColor = rgbaValue(mtl.ka);
67
- specularColor = rgbaValue(mtl.ks);
68
- shininess = mtl.ns;
69
- if (mtl.kdMap != null) {
70
- // If current material is textured, then tinting the texture using the
71
- // diffuse color.
72
- tintColor = rgbaValue(mtl.kd, mtl.d);
73
- }
74
-
75
- vertexCount = face.vertIdx.size();
76
- vertices = new float[vertexCount][12];
77
- for (int j = 0; j < face.vertIdx.size(); j++) {
78
- int vertIdx, normIdx, texIdx;
79
- PVector vert, norms, tex;
80
-
81
- vert = norms = tex = null;
82
-
83
- vertIdx = face.vertIdx.get(j).intValue() - 1;
84
- vert = coords.get(vertIdx);
85
-
86
- if (j < face.normIdx.size()) {
87
- normIdx = face.normIdx.get(j).intValue() - 1;
88
- if (-1 < normIdx) {
89
- norms = normals.get(normIdx);
90
- }
91
- }
92
-
93
- if (j < face.texIdx.size()) {
94
- texIdx = face.texIdx.get(j).intValue() - 1;
95
- if (-1 < texIdx) {
96
- tex = texcoords.get(texIdx);
97
- }
98
- }
99
-
100
- vertices[j][X] = vert.x;
101
- vertices[j][Y] = vert.y;
102
- vertices[j][Z] = vert.z;
84
+ vertexCount = face.vertIdx.size();
85
+ vertices = new float[vertexCount][12];
86
+ for (int j = 0; j < face.vertIdx.size(); j++){
87
+ int vertIdx, normIdx, texIdx;
88
+ PVector vert, norms, tex;
103
89
 
104
- vertices[j][PGraphics.R] = mtl.kd.x;
105
- vertices[j][PGraphics.G] = mtl.kd.y;
106
- vertices[j][PGraphics.B] = mtl.kd.z;
107
- vertices[j][PGraphics.A] = 1;
90
+ vert = norms = tex = null;
108
91
 
109
- if (norms != null) {
110
- vertices[j][PGraphics.NX] = norms.x;
111
- vertices[j][PGraphics.NY] = norms.y;
112
- vertices[j][PGraphics.NZ] = norms.z;
113
- }
92
+ vertIdx = face.vertIdx.get(j) - 1;
93
+ vert = coords.get(vertIdx);
114
94
 
115
- if (tex != null) {
116
- vertices[j][PGraphics.U] = tex.x;
117
- vertices[j][PGraphics.V] = tex.y;
118
- }
119
-
120
- if (mtl != null && mtl.kdMap != null) {
121
- image = mtl.kdMap;
122
- }
95
+ if (j < face.normIdx.size()) {
96
+ normIdx = face.normIdx.get(j) - 1;
97
+ if (-1 < normIdx) {
98
+ norms = normals.get(normIdx);
123
99
  }
124
- }
100
+ }
125
101
 
126
- protected void addChildren(ArrayList<OBJFace> faces,
127
- ArrayList<OBJMaterial> materials,
128
- ArrayList<PVector> coords,
129
- ArrayList<PVector> normals,
130
- ArrayList<PVector> texcoords) {
131
- int mtlIdxCur = -1;
132
- OBJMaterial mtl = null;
133
- for (int i = 0; i < faces.size(); i++) {
134
- OBJFace face = faces.get(i);
135
-
136
- // Getting current material.
137
- if (mtlIdxCur != face.matIdx || face.matIdx == -1) {
138
- // To make sure that at least we get the default material
139
- mtlIdxCur = PApplet.max(0, face.matIdx);
140
- mtl = materials.get(mtlIdxCur);
141
- }
142
-
143
- // Creating child shape for current face.
144
- PShape child = new PShapeOBJ(face, mtl, coords, normals, texcoords);
145
- addChild(child);
102
+ if (j < face.texIdx.size()) {
103
+ texIdx = face.texIdx.get(j) - 1;
104
+ if (-1 < texIdx) {
105
+ tex = texcoords.get(texIdx);
146
106
  }
107
+ }
108
+
109
+ vertices[j][X] = vert.x;
110
+ vertices[j][Y] = vert.y;
111
+ vertices[j][Z] = vert.z;
112
+
113
+ vertices[j][PGraphics.R] = mtl.kd.x;
114
+ vertices[j][PGraphics.G] = mtl.kd.y;
115
+ vertices[j][PGraphics.B] = mtl.kd.z;
116
+ vertices[j][PGraphics.A] = 1;
117
+
118
+ if (norms != null) {
119
+ vertices[j][PGraphics.NX] = norms.x;
120
+ vertices[j][PGraphics.NY] = norms.y;
121
+ vertices[j][PGraphics.NZ] = norms.z;
122
+ }
123
+
124
+ if (tex != null) {
125
+ vertices[j][PGraphics.U] = tex.x;
126
+ vertices[j][PGraphics.V] = tex.y;
127
+ }
128
+
129
+ if (mtl != null && mtl.kdMap != null) {
130
+ image = mtl.kdMap;
131
+ }
132
+ }
133
+ }
134
+
135
+
136
+ protected void addChildren(ArrayList<OBJFace> faces,
137
+ ArrayList<OBJMaterial> materials,
138
+ ArrayList<PVector> coords,
139
+ ArrayList<PVector> normals,
140
+ ArrayList<PVector> texcoords) {
141
+ int mtlIdxCur = -1;
142
+ OBJMaterial mtl = null;
143
+ for (int i = 0; i < faces.size(); i++) {
144
+ OBJFace face = faces.get(i);
145
+
146
+ // Getting current material.
147
+ if (mtlIdxCur != face.matIdx || face.matIdx == -1) {
148
+ // To make sure that at least we get the default material
149
+ mtlIdxCur = PApplet.max(0, face.matIdx);
150
+ mtl = materials.get(mtlIdxCur);
151
+ }
152
+
153
+ // Creating child shape for current face.
154
+ PShape child = new PShapeOBJ(face, mtl, coords, normals, texcoords);
155
+ addChild(child);
147
156
  }
157
+ }
158
+
159
+
160
+ static protected void parseOBJ(PApplet parent, String path,
161
+ BufferedReader reader,
162
+ ArrayList<OBJFace> faces,
163
+ ArrayList<OBJMaterial> materials,
164
+ ArrayList<PVector> coords,
165
+ ArrayList<PVector> normals,
166
+ ArrayList<PVector> texcoords) {
167
+ Map<String, Integer> mtlTable = new HashMap<>();
168
+ int mtlIdxCur = -1;
169
+ boolean readv, readvn, readvt;
170
+ try {
171
+
172
+ readv = readvn = readvt = false;
173
+ String line;
174
+ String gname = "object";
175
+ while ((line = reader.readLine()) != null) {
176
+ // Parse the line.
177
+ line = line.trim();
178
+ if (line.equals("") || line.indexOf('#') == 0) {
179
+ // Empty line of comment, ignore line
180
+ continue;
181
+ }
148
182
 
149
- static protected void parseOBJ(PApplet parent, String path,
150
- BufferedReader reader,
151
- ArrayList<OBJFace> faces,
152
- ArrayList<OBJMaterial> materials,
153
- ArrayList<PVector> coords,
154
- ArrayList<PVector> normals,
155
- ArrayList<PVector> texcoords) {
156
- Map<String, Integer> mtlTable = new HashMap<String, Integer>();
157
- int mtlIdxCur = -1;
158
- boolean readv, readvn, readvt;
159
- try {
160
-
161
- readv = readvn = readvt = false;
162
- String line;
163
- String gname = "object";
164
- while ((line = reader.readLine()) != null) {
165
- // Parse the line.
166
- line = line.trim();
167
- if (line.equals("") || line.indexOf('#') == 0) {
168
- // Empty line of comment, ignore line
169
- continue;
183
+ // The below patch/hack comes from Carlos Tomas Marti and is a
184
+ // fix for single backslashes in Rhino obj files
185
+
186
+ // BEGINNING OF RHINO OBJ FILES HACK
187
+ // Statements can be broken in multiple lines using '\' at the
188
+ // end of a line.
189
+ // In regular expressions, the backslash is also an escape
190
+ // character.
191
+ // The regular expression \\ matches a single backslash. This
192
+ // regular expression as a Java string, becomes "\\\\".
193
+ // That's right: 4 backslashes to match a single one.
194
+ while (line.contains("\\")) {
195
+ line = line.split("\\\\")[0];
196
+ final String s = reader.readLine();
197
+ if (s != null)
198
+ line += s;
199
+ }
200
+ // END OF RHINO OBJ FILES HACK
201
+
202
+ String[] parts = line.split("\\s+");
203
+ // if not a blank line, process the line.
204
+ if (parts.length > 0) {
205
+ switch (parts[0]) {
206
+ case "v":
207
+ {
208
+ // vertex
209
+ PVector tempv = new PVector(Float.parseFloat(parts[1]),
210
+ Float.parseFloat(parts[2]),
211
+ Float.parseFloat(parts[3]));
212
+ coords.add(tempv);
213
+ readv = true;
214
+ break;
215
+ }
216
+ case "vn":
217
+ // normal
218
+ PVector tempn = new PVector(Float.parseFloat(parts[1]),
219
+ Float.parseFloat(parts[2]),
220
+ Float.parseFloat(parts[3]));
221
+ normals.add(tempn);
222
+ readvn = true;
223
+ break;
224
+ case "vt":
225
+ {
226
+ // uv, inverting v to take into account Processing's inverted Y axis
227
+ // with respect to OpenGL.
228
+ PVector tempv = new PVector(Float.parseFloat(parts[1]),
229
+ 1 - Float.parseFloat(parts[2]));
230
+ texcoords.add(tempv);
231
+ readvt = true;
232
+ break;
233
+ }
234
+ // Object name is ignored, for now.
235
+ case "o":
236
+ break;
237
+ case "mtllib":
238
+ if (parts[1] != null) {
239
+ String fn = parts[1];
240
+ if (!fn.contains(File.separator) && !path.equals("")) {
241
+ // Relative file name, adding the base path.
242
+ fn = path + File.separator + fn;
243
+ }
244
+ BufferedReader mreader = parent.createReader(fn);
245
+ if (mreader != null) {
246
+ parseMTL(parent, fn, path, mreader, materials, mtlTable);
247
+ mreader.close();
170
248
  }
249
+ } break;
250
+ case "g":
251
+ gname = 1 < parts.length ? parts[1] : "";
252
+ break;
253
+ case "usemtl":
254
+ // Getting index of current active material (will be applied on
255
+ // all subsequent faces).
256
+ if (parts[1] != null) {
257
+ String mtlname = parts[1];
258
+ if (mtlTable.containsKey(mtlname)) {
259
+ Integer tempInt = mtlTable.get(mtlname);
260
+ mtlIdxCur = tempInt;
261
+ } else {
262
+ mtlIdxCur = -1;
263
+ }
264
+ } break;
265
+ case "f":
266
+ // Face setting
267
+ OBJFace face = new OBJFace();
268
+ face.matIdx = mtlIdxCur;
269
+ face.name = gname;
270
+ for (int i = 1; i < parts.length; i++) {
271
+ String seg = parts[i];
272
+
273
+ if (seg.indexOf("/") > 0) {
274
+ String[] forder = seg.split("/");
275
+
276
+ if (forder.length > 2) {
277
+ // Getting vertex and texture and normal indexes.
278
+ if (forder[0].length() > 0 && readv) {
279
+ face.vertIdx.add(Integer.valueOf(forder[0]));
280
+ }
281
+
282
+ if (forder[1].length() > 0 && readvt) {
283
+ face.texIdx.add(Integer.valueOf(forder[1]));
284
+ }
171
285
 
172
- // The below patch/hack comes from Carlos Tomas Marti and is a
173
- // fix for single backslashes in Rhino obj files
174
- // BEGINNING OF RHINO OBJ FILES HACK
175
- // Statements can be broken in multiple lines using '\' at the
176
- // end of a line.
177
- // In regular expressions, the backslash is also an escape
178
- // character.
179
- // The regular expression \\ matches a single backslash. This
180
- // regular expression as a Java string, becomes "\\\\".
181
- // That's right: 4 backslashes to match a single one.
182
- while (line.contains("\\")) {
183
- line = line.split("\\\\")[0];
184
- final String s = reader.readLine();
185
- if (s != null) {
186
- line += s;
286
+ if (forder[2].length() > 0 && readvn) {
287
+ face.normIdx.add(Integer.valueOf(forder[2]));
187
288
  }
188
- }
189
- // END OF RHINO OBJ FILES HACK
190
-
191
- String[] parts = line.split("\\s+");
192
- // if not a blank line, process the line.
193
- if (parts.length > 0) {
194
- if (parts[0].equals("v")) {
195
- // vertex
196
- PVector tempv = new PVector(Float.valueOf(parts[1]).floatValue(),
197
- Float.valueOf(parts[2]).floatValue(),
198
- Float.valueOf(parts[3]).floatValue());
199
- coords.add(tempv);
200
- readv = true;
201
- } else if (parts[0].equals("vn")) {
202
- // normal
203
- PVector tempn = new PVector(Float.valueOf(parts[1]).floatValue(),
204
- Float.valueOf(parts[2]).floatValue(),
205
- Float.valueOf(parts[3]).floatValue());
206
- normals.add(tempn);
207
- readvn = true;
208
- } else if (parts[0].equals("vt")) {
209
- // uv, inverting v to take into account Processing's inverted Y axis
210
- // with respect to OpenGL.
211
- PVector tempv = new PVector(Float.valueOf(parts[1]).floatValue(),
212
- 1 - Float.valueOf(parts[2]).
213
- floatValue());
214
- texcoords.add(tempv);
215
- readvt = true;
216
- } else if (parts[0].equals("o")) {
217
- // Object name is ignored, for now.
218
- } else if (parts[0].equals("mtllib")) {
219
- if (parts[1] != null) {
220
- String fn = parts[1];
221
- if (fn.indexOf(File.separator) == -1 && !path.equals("")) {
222
- // Relative file name, adding the base path.
223
- fn = path + File.separator + fn;
224
- }
225
- BufferedReader mreader = parent.createReader(fn);
226
- if (mreader != null) {
227
- parseMTL(parent, fn, path, mreader, materials, mtlTable);
228
- mreader.close();
229
- }
230
- }
231
- } else if (parts[0].equals("g")) {
232
- gname = 1 < parts.length ? parts[1] : "";
233
- } else if (parts[0].equals("usemtl")) {
234
- // Getting index of current active material (will be applied on
235
- // all subsequent faces).
236
- if (parts[1] != null) {
237
- String mtlname = parts[1];
238
- if (mtlTable.containsKey(mtlname)) {
239
- Integer tempInt = mtlTable.get(mtlname);
240
- mtlIdxCur = tempInt.intValue();
241
- } else {
242
- mtlIdxCur = -1;
243
- }
244
- }
245
- } else if (parts[0].equals("f")) {
246
- // Face setting
247
- OBJFace face = new OBJFace();
248
- face.matIdx = mtlIdxCur;
249
- face.name = gname;
250
-
251
- for (int i = 1; i < parts.length; i++) {
252
- String seg = parts[i];
253
-
254
- if (seg.indexOf("/") > 0) {
255
- String[] forder = seg.split("/");
256
-
257
- if (forder.length > 2) {
258
- // Getting vertex and texture and normal indexes.
259
- if (forder[0].length() > 0 && readv) {
260
- face.vertIdx.add(Integer.valueOf(forder[0]));
261
- }
262
-
263
- if (forder[1].length() > 0 && readvt) {
264
- face.texIdx.add(Integer.valueOf(forder[1]));
265
- }
266
-
267
- if (forder[2].length() > 0 && readvn) {
268
- face.normIdx.add(Integer.valueOf(forder[2]));
269
- }
270
- } else if (forder.length > 1) {
271
- // Getting vertex and texture/normal indexes.
272
- if (forder[0].length() > 0 && readv) {
273
- face.vertIdx.add(Integer.valueOf(forder[0]));
274
- }
275
-
276
- if (forder[1].length() > 0) {
277
- if (readvt) {
278
- face.texIdx.add(Integer.valueOf(forder[1]));
279
- } else if (readvn) {
280
- face.normIdx.add(Integer.valueOf(forder[1]));
281
- }
282
-
283
- }
284
-
285
- } else if (forder.length > 0) {
286
- // Getting vertex index only.
287
- if (forder[0].length() > 0 && readv) {
288
- face.vertIdx.add(Integer.valueOf(forder[0]));
289
- }
290
- }
291
- } else {
292
- // Getting vertex index only.
293
- if (seg.length() > 0 && readv) {
294
- face.vertIdx.add(Integer.valueOf(seg));
295
- }
296
- }
297
- }
298
-
299
- faces.add(face);
289
+ } else if (forder.length > 1) {
290
+ // Getting vertex and texture/normal indexes.
291
+ if (forder[0].length() > 0 && readv) {
292
+ face.vertIdx.add(Integer.valueOf(forder[0]));
300
293
  }
294
+
295
+ if (forder[1].length() > 0) {
296
+ if (readvt) {
297
+ face.texIdx.add(Integer.valueOf(forder[1]));
298
+ } else if (readvn) {
299
+ face.normIdx.add(Integer.valueOf(forder[1]));
300
+ }
301
+
302
+ }
303
+
304
+ } else if (forder.length > 0) {
305
+ // Getting vertex index only.
306
+ if (forder[0].length() > 0 && readv) {
307
+ face.vertIdx.add(Integer.valueOf(forder[0]));
308
+ }
309
+ }
310
+ } else {
311
+ // Getting vertex index only.
312
+ if (seg.length() > 0 && readv) {
313
+ face.vertIdx.add(Integer.valueOf(seg));
314
+ }
301
315
  }
302
- }
316
+ } faces.add(face);
317
+ break;
318
+ default:
319
+ break;
320
+ }
321
+ }
322
+ }
303
323
 
304
- if (materials.size() == 0) {
305
- // No materials definition so far. Adding one default material.
306
- OBJMaterial defMtl = new OBJMaterial();
307
- materials.add(defMtl);
308
- }
324
+ if (materials.isEmpty()) {
325
+ // No materials definition so far. Adding one default material.
326
+ OBJMaterial defMtl = new OBJMaterial();
327
+ materials.add(defMtl);
328
+ }
309
329
 
310
- } catch (Exception e) {
311
- e.printStackTrace();
312
- }
330
+ } catch (IOException | NumberFormatException e) {
313
331
  }
314
-
315
- static protected void parseMTL(PApplet parent, String mtlfn, String path,
316
- BufferedReader reader,
317
- ArrayList<OBJMaterial> materials,
318
- Map<String, Integer> materialsHash) {
319
- try {
320
- String line;
321
- OBJMaterial currentMtl = null;
322
- while ((line = reader.readLine()) != null) {
323
- // Parse the line
324
- line = line.trim();
325
- String parts[] = line.split("\\s+");
326
- if (parts.length > 0) {
327
- // Extract the material data.
328
- if (parts[0].equals("newmtl")) {
329
- // Starting new material.
330
- String mtlname = parts[1];
331
- currentMtl = addMaterial(mtlname, materials, materialsHash);
332
- } else {
333
- if (currentMtl == null) {
334
- currentMtl = addMaterial("material" + materials.size(),
335
- materials, materialsHash);
336
- }
337
- if (parts[0].equals("map_Kd") && parts.length > 1) {
338
- // Loading texture map.
339
- String texname = parts[1];
340
- if (texname.indexOf(File.separator) == -1 && !path.equals("")) {
341
- // Relative file name, adding the base path.
342
- texname = path + File.separator + texname;
343
- }
344
-
345
- File file = new File(parent.dataPath(texname));
346
- if (file.exists()) {
347
- currentMtl.kdMap = parent.loadImage(texname);
348
- } else {
349
- System.err.println("The texture map \"" + texname + "\" "
350
- + "in the materials definition file \"" + mtlfn + "\" "
351
- + "is missing or inaccessible, make sure "
352
- + "the URL is valid or that the file has been "
353
- + "added to your sketch and is readable.");
354
- }
355
- } else if (parts[0].equals("Ka") && parts.length > 3) {
356
- // The ambient color of the material
357
- currentMtl.ka.x = Float.valueOf(parts[1]).floatValue();
358
- currentMtl.ka.y = Float.valueOf(parts[2]).floatValue();
359
- currentMtl.ka.z = Float.valueOf(parts[3]).floatValue();
360
- } else if (parts[0].equals("Kd") && parts.length > 3) {
361
- // The diffuse color of the material
362
- currentMtl.kd.x = Float.valueOf(parts[1]).floatValue();
363
- currentMtl.kd.y = Float.valueOf(parts[2]).floatValue();
364
- currentMtl.kd.z = Float.valueOf(parts[3]).floatValue();
365
- } else if (parts[0].equals("Ks") && parts.length > 3) {
366
- // The specular color weighted by the specular coefficient
367
- currentMtl.ks.x = Float.valueOf(parts[1]).floatValue();
368
- currentMtl.ks.y = Float.valueOf(parts[2]).floatValue();
369
- currentMtl.ks.z = Float.valueOf(parts[3]).floatValue();
370
- } else if ((parts[0].equals("d")
371
- || parts[0].equals("Tr")) && parts.length > 1) {
372
- // Reading the alpha transparency.
373
- currentMtl.d = Float.valueOf(parts[1]).floatValue();
374
- } else if (parts[0].equals("Ns") && parts.length > 1) {
375
- // The specular component of the Phong shading model
376
- currentMtl.ns = Float.valueOf(parts[1]).floatValue();
377
- }
378
- }
379
- }
332
+ }
333
+
334
+
335
+ static protected void parseMTL(PApplet parent, String mtlfn, String path,
336
+ BufferedReader reader,
337
+ ArrayList<OBJMaterial> materials,
338
+ Map<String, Integer> materialsHash) {
339
+ try {
340
+ String line;
341
+ OBJMaterial currentMtl = null;
342
+ while ((line = reader.readLine()) != null) {
343
+ // Parse the line
344
+ line = line.trim();
345
+ String[] parts = line.split("\\s+");
346
+ if (parts.length > 0) {
347
+ // Extract the material data.
348
+ if (parts[0].equals("newmtl")) {
349
+ // Starting new material.
350
+ String mtlname = parts[1];
351
+ currentMtl = addMaterial(mtlname, materials, materialsHash);
352
+ } else {
353
+ if (currentMtl == null) {
354
+ currentMtl = addMaterial("material" + materials.size(),
355
+ materials, materialsHash);
380
356
  }
381
- } catch (Exception e) {
382
- e.printStackTrace();
357
+ if (parts[0].equals("map_Kd") && parts.length > 1) {
358
+ // Loading texture map.
359
+ String texname = parts[1];
360
+ if (!texname.contains(File.separator) && !path.equals("")) {
361
+ // Relative file name, adding the base path.
362
+ texname = path + File.separator + texname;
363
+ }
364
+
365
+ File file = new File(parent.dataPath(texname));
366
+ if (file.exists()) {
367
+ currentMtl.kdMap = parent.loadImage(texname);
368
+ } else {
369
+ System.err.println("The texture map \"" + texname + "\" " +
370
+ "in the materials definition file \"" + mtlfn + "\" " +
371
+ "is missing or inaccessible, make sure " +
372
+ "the URL is valid or that the file has been " +
373
+ "added to your sketch and is readable.");
374
+ }
375
+ } else if (parts[0].equals("Ka") && parts.length > 3) {
376
+ // The ambient color of the material
377
+ currentMtl.ka.x = Float.parseFloat(parts[1]);
378
+ currentMtl.ka.y = Float.parseFloat(parts[2]);
379
+ currentMtl.ka.z = Float.parseFloat(parts[3]);
380
+ } else if (parts[0].equals("Kd") && parts.length > 3) {
381
+ // The diffuse color of the material
382
+ currentMtl.kd.x = Float.parseFloat(parts[1]);
383
+ currentMtl.kd.y = Float.parseFloat(parts[2]);
384
+ currentMtl.kd.z = Float.parseFloat(parts[3]);
385
+ } else if (parts[0].equals("Ks") && parts.length > 3) {
386
+ // The specular color weighted by the specular coefficient
387
+ currentMtl.ks.x = Float.parseFloat(parts[1]);
388
+ currentMtl.ks.y = Float.parseFloat(parts[2]);
389
+ currentMtl.ks.z = Float.parseFloat(parts[3]);
390
+ } else if ((parts[0].equals("d") ||
391
+ parts[0].equals("Tr")) && parts.length > 1) {
392
+ // Reading the alpha transparency.
393
+ currentMtl.d = Float.parseFloat(parts[1]);
394
+ } else if (parts[0].equals("Ns") && parts.length > 1) {
395
+ // The specular component of the Phong shading model
396
+ currentMtl.ns = Float.parseFloat(parts[1]);
397
+ }
398
+ }
383
399
  }
400
+ }
401
+ } catch (IOException | NumberFormatException e) {
384
402
  }
385
-
386
- protected static OBJMaterial addMaterial(String mtlname,
387
- ArrayList<OBJMaterial> materials,
388
- Map<String, Integer> materialsHash) {
389
- OBJMaterial currentMtl = new OBJMaterial(mtlname);
390
- materialsHash.put(mtlname, Integer.valueOf(materials.size()));
391
- materials.add(currentMtl);
392
- return currentMtl;
403
+ }
404
+
405
+ protected static OBJMaterial addMaterial(String mtlname,
406
+ ArrayList<OBJMaterial> materials,
407
+ Map<String, Integer> materialsHash) {
408
+ OBJMaterial currentMtl = new OBJMaterial(mtlname);
409
+ materialsHash.put(mtlname, materials.size());
410
+ materials.add(currentMtl);
411
+ return currentMtl;
412
+ }
413
+
414
+ protected static int rgbaValue(PVector color) {
415
+ return 0xFF000000 | ((int)(color.x * 255) << 16) |
416
+ ((int)(color.y * 255) << 8) |
417
+ (int)(color.z * 255);
418
+ }
419
+
420
+
421
+ protected static int rgbaValue(PVector color, float alpha) {
422
+ return ((int)(alpha * 255) << 24) |
423
+ ((int)(color.x * 255) << 16) |
424
+ ((int)(color.y * 255) << 8) |
425
+ (int)(color.z * 255);
426
+ }
427
+
428
+
429
+ // Stores a face from an OBJ file
430
+ static protected class OBJFace {
431
+ ArrayList<Integer> vertIdx;
432
+ ArrayList<Integer> texIdx;
433
+ ArrayList<Integer> normIdx;
434
+ int matIdx;
435
+ String name;
436
+
437
+ OBJFace() {
438
+ vertIdx = new ArrayList<>();
439
+ texIdx = new ArrayList<>();
440
+ normIdx = new ArrayList<>();
441
+ matIdx = -1;
442
+ name = "";
393
443
  }
444
+ }
394
445
 
395
- protected static int rgbaValue(PVector color) {
396
- return 0xFF000000 | ((int) (color.x * 255) << 16)
397
- | ((int) (color.y * 255) << 8)
398
- | (int) (color.z * 255);
399
- }
400
446
 
401
- protected static int rgbaValue(PVector color, float alpha) {
402
- return ((int) (alpha * 255) << 24)
403
- | ((int) (color.x * 255) << 16)
404
- | ((int) (color.y * 255) << 8)
405
- | (int) (color.z * 255);
447
+ static protected String getBasePath(PApplet parent, String filename) {
448
+ // Obtaining the path
449
+ File file = new File(parent.dataPath(filename));
450
+ if (!file.exists()) {
451
+ file = parent.sketchFile(filename);
406
452
  }
407
-
408
- // Stores a face from an OBJ file
409
- static protected class OBJFace {
410
-
411
- ArrayList<Integer> vertIdx;
412
- ArrayList<Integer> texIdx;
413
- ArrayList<Integer> normIdx;
414
- int matIdx;
415
- String name;
416
-
417
- OBJFace() {
418
- vertIdx = new ArrayList<Integer>();
419
- texIdx = new ArrayList<Integer>();
420
- normIdx = new ArrayList<Integer>();
421
- matIdx = -1;
422
- name = "";
423
- }
453
+ String absolutePath = file.getAbsolutePath();
454
+ return absolutePath.substring(0,
455
+ absolutePath.lastIndexOf(File.separator));
456
+ }
457
+
458
+
459
+ // Stores a material defined in an MTL file.
460
+ static protected class OBJMaterial {
461
+ String name;
462
+ PVector ka;
463
+ PVector kd;
464
+ PVector ks;
465
+ float d;
466
+ float ns;
467
+ PImage kdMap;
468
+
469
+ OBJMaterial() {
470
+ this("default");
424
471
  }
425
472
 
426
- static protected String getBasePath(PApplet parent, String filename) {
427
- // Obtaining the path
428
- File file = new File(parent.dataPath(filename));
429
- if (!file.exists()) {
430
- file = parent.sketchFile(filename);
431
- }
432
- String absolutePath = file.getAbsolutePath();
433
- return absolutePath.substring(0,
434
- absolutePath.lastIndexOf(File.separator));
435
- }
436
-
437
- // Stores a material defined in an MTL file.
438
- static protected class OBJMaterial {
439
-
440
- String name;
441
- PVector ka;
442
- PVector kd;
443
- PVector ks;
444
- float d;
445
- float ns;
446
- PImage kdMap;
447
-
448
- OBJMaterial() {
449
- this("default");
450
- }
451
-
452
- OBJMaterial(String name) {
453
- this.name = name;
454
- ka = new PVector(0.5f, 0.5f, 0.5f);
455
- kd = new PVector(0.5f, 0.5f, 0.5f);
456
- ks = new PVector(0.5f, 0.5f, 0.5f);
457
- d = 1.0f;
458
- ns = 0.0f;
459
- kdMap = null;
460
- }
473
+ OBJMaterial(String name) {
474
+ this.name = name;
475
+ ka = new PVector(0.5f, 0.5f, 0.5f);
476
+ kd = new PVector(0.5f, 0.5f, 0.5f);
477
+ ks = new PVector(0.5f, 0.5f, 0.5f);
478
+ d = 1.0f;
479
+ ns = 0.0f;
480
+ kdMap = null;
461
481
  }
482
+ }
462
483
  }