-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
200 lines (165 loc) · 6.46 KB
/
Main.java
File metadata and controls
200 lines (165 loc) · 6.46 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
class Main {
static final int LOG_DEBUG = 0;
static final int LOG_ERROR = 1;
static final String[] logLevelStrs = {"debug", "error"};
enum TargetLanguage { JAVA, SWIFT }
// **** CONFIG ****
static final int logLevel = LOG_DEBUG;
static final TargetLanguage targetLanguage = TargetLanguage.SWIFT;
static final int numVerticesPerLine = 3;
static final int numFacesPerLine = 8;
static final boolean xyzrgbEnable = true;
static final int numVertexColorsPerLine = 4; // Effective only if targetLanguage == JAVA and xyzrgbEnable = true
// ****************
static class _3DModel {
String[][] vertices = {};
int[][] faces = {};
String[][] vertexColors = {};
void printOutputArrays() {
if (targetLanguage == TargetLanguage.JAVA) {
System.out.println("final float[] vertices = {");
for (int i = 0; i < vertices.length; i += numVerticesPerLine) {
for (int j = 0; j < numVerticesPerLine && i + j < vertices.length; j++)
System.out.print("\t" + String.join("f, ", vertices[i + j]) + "f,");
System.out.println();
}
System.out.println("};");
System.out.println("\nfinal short[] faces = {");
for (int i = 0; i < faces.length; i += numFacesPerLine) {
for (int j = 0; j < numFacesPerLine && i + j < faces.length; j++)
System.out.print("\t" + faces[i + j][0] + ", " + faces[i + j][1] + ", " + faces[i + j][2] + ",");
System.out.println();
}
System.out.println("};");
if (xyzrgbEnable) {
System.out.println("\nfinal float[] vertexColors = {");
for (int i = 0; i < vertexColors.length; i += numVertexColorsPerLine) {
for (int j = 0; j < numVertexColorsPerLine && i + j < vertexColors.length; j++)
System.out.print("\t" + String.join("f, ", vertexColors[i + j]) + "f,");
System.out.println();
}
System.out.println("};");
}
} else { // SWIFT
System.out.println("let vertices: [SIMD3<Float>] = [");
for (int i = 0; i < vertices.length; i += numVerticesPerLine) {
for (int j = 0; j < numVerticesPerLine && i + j < vertices.length; j++)
System.out.print("\t[" + String.join(", ", vertices[i + j]) + "],");
System.out.println();
}
System.out.println("]");
System.out.println("\nlet faces: [UInt16] = [");
for (int i = 0; i < faces.length; i += numFacesPerLine) {
for (int j = 0; j < numFacesPerLine && i + j < faces.length; j++)
System.out.print("\t" + faces[i + j][0] + ", " + faces[i + j][1] + ", " + faces[i + j][2] + ",");
System.out.println();
}
System.out.println("]");
if (xyzrgbEnable) {
System.out.println("\nlet vertexColors: [SIMD3<Float>] = [");
for (int i = 0; i < vertexColors.length; i += numVertexColorsPerLine) {
for (int j = 0; j < numVertexColorsPerLine && i + j < vertexColors.length; j++)
System.out.print("\t[" + String.join(", ", vertexColors[i + j]) + "],");
System.out.println();
}
System.out.println("]");
}
}
}
}
public static void main(String[] args) {
if (args.length == 0) {
logMsg(LOG_ERROR, "Input file path not provided");
return;
}
final File inputFile = new File(args[0]);
final Scanner scanner;
try {
scanner = new Scanner(inputFile);
} catch (FileNotFoundException e) {
logMsg(LOG_ERROR, "Failed to find input file (FileNotFoundException)");
return;
}
final _3DModel model = OBJWavefrontParse(scanner);
if (model == null)
return;
System.out.println("\n**** RESULT ****");
model.printOutputArrays();
System.out.println("****************");
}
//
// Prints / discards a log message based on the logLevel class-variable.
//
static void logMsg(int msgLogLevel, String msg) {
if (msgLogLevel >= logLevel)
System.out.println(logLevelStrs[msgLogLevel] + ": " + msg);
}
//
// Parses the contents of scanner as an OBJ file using the config
// class-variables (such as xyzrgbEnable) and returns a _3DModel based
// on the OBJ content, or null on error.
//
static _3DModel OBJWavefrontParse(Scanner scanner) {
_3DModel model = new _3DModel();
for (int lineNr = 1; scanner.hasNextLine(); lineNr++) {
final String line = scanner.nextLine();
logMsg(LOG_DEBUG, "Parsing line " + lineNr);
final int spaceIndex = line.indexOf(' ');
if (spaceIndex < 1)
continue;
int sepIndex;
switch (line.substring(0, spaceIndex)) {
case "v":
model.vertices = Arrays.copyOf(model.vertices, model.vertices.length + 1);
model.vertices[model.vertices.length - 1] = new String[3];
sepIndex = spaceIndex;
for (int i = 0; i < 3; i++) {
int nextSepIndex = line.indexOf(' ', sepIndex + 1);
if (nextSepIndex == -1)
nextSepIndex = line.length();
model.vertices[model.vertices.length - 1][i] = line.substring(sepIndex + 1, nextSepIndex);
sepIndex = nextSepIndex;
}
if (xyzrgbEnable) {
model.vertexColors = Arrays.copyOf(model.vertexColors, model.vertexColors.length + 1);
model.vertexColors[model.vertexColors.length - 1] = new String[3];
for (int i = 0; i < 3; i++) {
int nextSepIndex = line.indexOf(' ', sepIndex + 1);
if (nextSepIndex == -1)
nextSepIndex = line.length();
model.vertexColors[model.vertexColors.length - 1][i] = line.substring(sepIndex + 1, nextSepIndex);
sepIndex = nextSepIndex;
}
}
continue;
case "f":
model.faces = Arrays.copyOf(model.faces, model.faces.length + 1);
model.faces[model.faces.length - 1] = new int[3];
sepIndex = spaceIndex;
for (int i = 0; i < 3; i++) {
int nextSepIndex = line.indexOf(' ', sepIndex + 1);
if (nextSepIndex == -1)
nextSepIndex = line.length();
String entryStr = line.substring(sepIndex + 1, nextSepIndex);
final int slashIndex = entryStr.indexOf('/');
if (slashIndex != -1)
entryStr = entryStr.substring(0, slashIndex);
try {
model.faces[model.faces.length - 1][i] = Integer.parseInt(entryStr) - 1;
} catch (NumberFormatException e) {
logMsg(LOG_ERROR, "NumberFormatException on an entry in a face");
return null;
}
sepIndex = nextSepIndex;
}
continue;
}
logMsg(LOG_DEBUG, "Skipping line starting with \"" + line.substring(0, spaceIndex) + "\"");
}
return model;
}
}