-
Notifications
You must be signed in to change notification settings - Fork 1
/
objReader.h
112 lines (101 loc) · 2.7 KB
/
objReader.h
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
//
// Created by Idan Maman on 04/10/2022.
//
#ifndef ASCII3DVIEWER_OBJREADER_H
#define ASCII3DVIEWER_OBJREADER_H
//check the obj reader
#include <stdio.h>
#include <sys/fcntl.h>
#include "DataStructers/vector.h"
#include "face.h"
typedef struct obj {
vector * faces ;
vector * vertexes;
}obj;
void printVertex(vertex * ve){
printf("v : x: %f , y:%f , z:%f\n",ve->x,ve->y,ve->z);
}
void printFace(face * fe,vector * ver){
for(size_t i=0;i<fe->sidesNum;i++){
vertex * vi = getIndexVector(fe->sides_array[i],ver);
printf("side %d.%d , point : %f %f %f ",i+1 ,fe->sides_array[i],vi->x,vi->y,vi->z);
}
printf("\n");
}
struct vertex * atov (char * ascii ){
vertex * ve = (struct vertex * ) malloc(sizeof(struct vertex));
char * tokens [3] = {
strtok(ascii, " \0"),
strtok(NULL, " \0"),
strtok(NULL, " \0")
} ;
for(int i=0;i<3;i++){
if(!tokens[i]){
fprintf(stderr,"bad file 1");
exit(-1);
}
}
if(strtok(NULL, ",/0")){
fprintf(stderr,"bad file 2 ");
exit(-1);
}
ve ->x = atof(tokens[0]);
ve->y= atof(tokens[1]);
ve->z = atof(tokens[2]);
return ve ;
}
struct face * atofa (char * ascii ){
face * fe = malloc(sizeof(face));
fe->sidesNum = 0;
for(size_t i=0;i<strlen(ascii);i++){
if(ascii[i] == ' ')
fe->sidesNum ++;
}
fe->sidesNum++;
fe->sides_array = malloc(sizeof(fe->sidesNum)*sizeof(size_t ));
char * token = strtok(ascii," \0");
for(size_t i=0;i<fe->sidesNum && token ;i++){
fe->sides_array[i]=atoi(token);
token= strtok(NULL," \0");
}
return fe ;
}
obj getObjData(char * path ){
vector * vertexes = createVector(sizeof(vertex));
vector * faces = createVector(sizeof(face));
char buffer[1000];;
FILE* ptr = fopen(path, "r");
if (NULL == ptr) {
fprintf("file can't be opened",stderr);
exit(-2);
}
while(fgets(buffer,1000,ptr))
{
buffer[strlen(buffer)-1]='\0';
switch(*buffer){
case '#' :
break;
case 'f' :
addVector(atofa(buffer+2),faces);
break;
case 'v':
switch(buffer[1]){
case ' ':
addVector(atov(buffer+2),vertexes);
break;
case 't':
break;
default:
break;
}
break;
default:
break;
}
}
obj ob;
ob.vertexes = vertexes;
ob.faces=faces;
return ob;
};
#endif //ASCII3DVIEWER_OBJREADER_H