-
Notifications
You must be signed in to change notification settings - Fork 3
/
OBJParser.m
177 lines (157 loc) · 4.47 KB
/
OBJParser.m
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
//
// OBJParser.m
// BabyFingers
//
// Created by Baris Metin
//
#import "util.h"
#import "OBJ.h"
#import "OBJParser.h"
#import "Vertex3D.h"
#import "Face3D.h"
NSString* vertex_start = @"v";
NSString* vertex_normal_start = @"vn";
NSString* texture_coordinate_start = @"vt";
NSString* face_start = @"f";
NSString* group_name_start = @"g";
#pragma mark Global Utilities
BOOL startsWith(NSString* str, NSString* start_str)
{
return [str compare:start_str
options:NSLiteralSearch
range:NSMakeRange(0, [start_str length])] == NSOrderedSame;
}
NSString* lineRest(NSString* line, NSString* start)
{
return [[line substringFromIndex:[start length]]
stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]];
}
#pragma mark OBJPaser
@implementation OBJParser
@synthesize object;
- (id) initWithFile:(NSString*) file
{
self = [super init];
filename = [NSString stringWithString:file];
filedata = [NSData dataWithContentsOfFile:filename];
content = (char*) [filedata bytes];
size = [filedata length];
index = 0;
state = S_UNKNOWN;
object = [[OBJ alloc] init];
return self;
}
- (void) dealloc
{
[object release];
[super dealloc];
}
- (NSString*) readLine
{
if (index >= size) {
return NULL;
}
char* c = content + index;
NSUInteger line_end = index;
while(!is_newline(*c)) {
++line_end; ++c;
}
NSUInteger line_size = line_end - index;
char* line = (char*) malloc(sizeof(char) * (line_size + 1));
memcpy(line, (content + index), line_size);
line[line_size] = '\0';
NSString* ret = [NSString stringWithUTF8String:line];
free(line);
index += line_size + 1; // pass newline
return ret;
}
- (LineType) typeOf:(NSString*)line
{
if (startsWith(line, vertex_start)) {
if (startsWith(line, vertex_normal_start)) {
return T_VERTEX_NORMAL;
} else if (startsWith(line, texture_coordinate_start)) {
return T_TEXCOORD;
}
return T_VERTEX;
} else if (startsWith(line, face_start)) {
return T_FACE;
} else if (startsWith(line, group_name_start)) {
return T_GROUP_NAME;
}
return T_UNKNOWN;
}
- (Vertex3D*) parseVertex3D:(NSString*)str
{
Vertex3D* v = [[Vertex3D alloc] init];
NSArray* chunks = [str componentsSeparatedByString:@" "];
if ([chunks count] == 3) {
[v setX:[[chunks objectAtIndex:0] floatValue]];
[v setY:[[chunks objectAtIndex:1] floatValue]];
[v setZ:[[chunks objectAtIndex:2] floatValue]];
}
[v autorelease];
return v;
}
- (Face3D*) parseFace3D:(NSString*)str
{
Face3D* f = [[Face3D alloc] init];
NSArray* chunks_1 = [str componentsSeparatedByString:@" "];
NSUInteger f_v[3], f_t[3], f_n[3];
for (int i=0; i < 3; i++) {
NSArray* chunks_2 = [[chunks_1 objectAtIndex:i] componentsSeparatedByString:@"/"];
f_v[i] = [[chunks_2 objectAtIndex:0] intValue];
f_t[i] = [[chunks_2 objectAtIndex:1] intValue];
f_n[i] = [[chunks_2 objectAtIndex:2] intValue];
}
[f setV:f_v];
[f setT:f_t];
[f setN:f_n];
[f autorelease];
return f;
}
- (void) parse
{
LineType line_type = T_UNKNOWN;
NSString* line;
while ((line = [self readLine]) != NULL) {
line_type = [self typeOf:line];
switch (line_type) {
case T_VERTEX:
{
state = S_VERTEX;
NSString* vstr = lineRest(line, vertex_start);
Vertex3D* vertex = [self parseVertex3D:vstr];
[object addVertex:vertex];
[vertex release];
break;
}
case T_VERTEX_NORMAL:
{
state = S_VERTEX_NORMAL;
NSString* vnstr = lineRest(line, vertex_normal_start);
Vertex3D* normal = [self parseVertex3D:vnstr];
[object addNormal:normal];
[normal release];
break;
}
case T_TEXCOORD:
{
// TODO
break;
}
case T_FACE:
{
state = S_FACE;
NSString* fstr = lineRest(line, face_start);
Face3D* face = [self parseFace3D:fstr];
[object addFace:face];
[face release];
break;
}
default:
break;
}
}
}
@end