-
-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathPShapeOBJTest.java
More file actions
236 lines (196 loc) · 8.2 KB
/
Copy pathPShapeOBJTest.java
File metadata and controls
236 lines (196 loc) · 8.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package processing.core;
import org.junit.Before;
import org.junit.Test;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class PShapeOBJTest {
private PApplet parent;
private ArrayList<PShapeOBJ.OBJFace> faces;
private ArrayList<PShapeOBJ.OBJMaterial> materials;
private ArrayList<PVector> coords;
private ArrayList<PVector> normals;
private ArrayList<PVector> texcoords;
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Before
public void setUp() {
parent = new PApplet();
faces = new ArrayList<>();
materials = new ArrayList<>();
coords = new ArrayList<>();
normals = new ArrayList<>();
texcoords = new ArrayList<>();
}
// Ensure a basic object is parsed correctly - if this fails, something is wrong
@Test
public void testBasicOBJParsing() {
// Define the data to be parsed
String objData =
"v 0.0 0.0 0.0\n" +
"v 1.0 0.0 0.0\n" +
"v 0.0 1.0 0.0\n" +
"f 1 2 3\n";
// Create a buffered reader, and parse the data
BufferedReader reader = new BufferedReader(new StringReader(objData));
PShapeOBJ.parseOBJ(parent, "", reader, faces, materials, coords, normals, texcoords);
// Ensure the data was parsed correctly - can be expanded on as needed
assertEquals(3, coords.size());
assertEquals(1, faces.size());
assertEquals(3, faces.get(0).vertIdx.size());
}
// Ensure a basic material is parsed correctly
@Test
public void testMaterialParsing() throws Exception {
// Define the data to be parsed
String mtlData =
"newmtl Material1\n" +
"Ka 0.2 0.2 0.2\n" +
"Kd 0.8 0.8 0.8\n" +
"Ks 1.0 1.0 1.0\n" +
"Ns 50.0\n" +
"d 1.0\n";
// Create a temporary file with this data
File mtlFile = tempFolder.newFile("test.mtl");
try (FileWriter writer = new FileWriter(mtlFile)) {
writer.write(mtlData);
}
// Create a buffered reader with the data, and initialize the materials hash
BufferedReader reader = new BufferedReader(new StringReader(mtlData));
Map<String, Integer> materialsHash = new HashMap<>();
// Parse the data
PShapeOBJ.parseMTL(parent, mtlFile.getAbsolutePath(), "", reader, materials, materialsHash);
// Ensure the data was parsed correctly - can be expanded on as needed
assertEquals(1, materials.size());
PShapeOBJ.OBJMaterial material = materials.get(0);
assertEquals("Material1", material.name);
assertEquals(0.2f, material.ka.x, 0.001f);
assertEquals(0.8f, material.kd.y, 0.001f);
assertEquals(1.0f, material.ks.z, 0.001f);
assertEquals(50.0f, material.ns, 0.001f);
assertEquals(1.0f, material.d, 0.001f);
}
// Ensure verticies and normals are parsed correctly for a basic object
@Test
public void testVertexNormalParsing() {
// Define the data to be parsed
String objData =
"v 0.0 0.0 0.0\n" +
"v 1.0 0.0 0.0\n" +
"v 0.0 1.0 0.0\n" +
"vn 0.0 0.0 1.0\n" +
"vn 0.0 1.0 0.0\n" +
"vn 1.0 0.0 0.0\n" +
"f 1//1 2//2 3//3\n";
// Create a buffered reader, and parse the data
BufferedReader reader = new BufferedReader(new StringReader(objData));
PShapeOBJ.parseOBJ(parent, "", reader, faces, materials, coords, normals, texcoords);
// Ensure the data was parsed correctly - can be expanded on as needed
assertEquals(3, normals.size());
assertEquals(3, faces.get(0).normIdx.size());
}
// Ensure parsing properly handles texture coordinates as well
@Test
public void testTextureCoordinateParsing() {
// Define the data to be parsed
String objData =
"v 0.0 0.0 0.0\n" +
"v 1.0 0.0 0.0\n" +
"v 0.0 1.0 0.0\n" +
"vt 0.0 0.0\n" +
"vt 1.0 0.0\n" +
"vt 0.0 1.0\n" +
"f 1/1 2/2 3/3\n";
// Create a buffered reader with the data, and parse the data
BufferedReader reader = new BufferedReader(new StringReader(objData));
PShapeOBJ.parseOBJ(parent, "", reader, faces, materials, coords, normals, texcoords);
// Ensure the data was parsed correctly - can be expanded on as needed
assertEquals(3, texcoords.size());
assertEquals(3, faces.get(0).texIdx.size());
}
// Ensure conversion from PVector to 32-bit color int works correctly
@Test
public void testRGBAValueConversion() {
// Define the PVector color
PVector color = new PVector(1.0f, 0.5f, 0.0f);
// Convert to 32-bit color int
int rgba = PShapeOBJ.rgbaValue(color);
// Ensure the conversion was correct - note alpha is always 0xFF here
assertEquals(0xFF, (rgba >> 24) & 0xFF); // A
assertEquals(0xFF, (rgba >> 16) & 0xFF); // R
assertEquals(0x7F, (rgba >> 8) & 0xFF); // G
assertEquals(0x00, rgba & 0xFF); // B
}
// Ensure conversion from PVector to 32-bit color int (with alpha) works correctly
@Test
public void testRGBAValueConversionWithAlpha() {
// Define the PVector color, and alpha
PVector color = new PVector(1.0f, 0.5f, 0.0f);
float alpha = 0.5f;
//Convert to 32-bit color int
int rgba = PShapeOBJ.rgbaValue(color, alpha);
// Ensure the conversion was correct
assertEquals(0x7F, (rgba >> 24) & 0xFF); // A
assertEquals(0xFF, (rgba >> 16) & 0xFF); // R
assertEquals(0x7F, (rgba >> 8) & 0xFF); // G
assertEquals(0x00, rgba & 0xFF); // B
}
// Ensure a newly-created OBJFace is initialized correctly
@Test
public void testOBJFaceCreation() {
// Create the empty OBJFace
PShapeOBJ.OBJFace face = new PShapeOBJ.OBJFace();
// Verify attributes are initialized correctly - can be expanded on as needed
assertTrue(face.vertIdx.isEmpty());
assertTrue(face.texIdx.isEmpty());
assertTrue(face.normIdx.isEmpty());
assertEquals(-1, face.matIdx);
assertEquals("", face.name);
}
// Ensure a newly-created OBJFace is initialized correctly
@Test
public void testOBJMaterialCreation() {
// Create the empty OBJMaterial
PShapeOBJ.OBJMaterial material = new PShapeOBJ.OBJMaterial("TestMaterial");
// Verify attributes are initialized correctly - can be expanded on as needed
assertEquals("TestMaterial", material.name);
assertEquals(0.5f, material.ka.x, 0.001f);
assertEquals(0.5f, material.kd.y, 0.001f);
assertEquals(0.5f, material.ks.z, 0.001f);
assertEquals(1.0f, material.d, 0.001f);
assertEquals(0.0f, material.ns, 0.001f);
assertNull(material.kdMap);
}
// After previous tests, ensure a more complex shape, with verticies, materials, and normals is parsed correctly
@Test
public void testComplexFaceParsing() {
// Define the data to be parsed
String objData =
"v 0.0 0.0 0.0\n" +
"v 1.0 0.0 0.0\n" +
"v 0.0 1.0 0.0\n" +
"v 1.0 1.0 0.0\n" +
"vt 0.0 0.0\n" +
"vt 1.0 0.0\n" +
"vt 0.0 1.0\n" +
"vt 1.0 1.0\n" +
"vn 0.0 0.0 1.0\n" +
"f 1/1/1 2/2/1 3/3/1 4/4/1\n";
// Create a buffered reader with the data, and parse the data
BufferedReader reader = new BufferedReader(new StringReader(objData));
PShapeOBJ.parseOBJ(parent, "", reader, faces, materials, coords, normals, texcoords);
// Ensure the data was parsed correctly - can be expanded on as needed
assertEquals(1, faces.size());
PShapeOBJ.OBJFace face = faces.get(0);
assertEquals(4, face.vertIdx.size());
assertEquals(4, face.texIdx.size());
assertEquals(4, face.normIdx.size());
}
}