-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReadOBJ.h
153 lines (125 loc) · 3.01 KB
/
ReadOBJ.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
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
#pragma once
#ifndef READOBJ_H_
#define READOBJ_H_
#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<map>
#include"HalfEdge.h"
typedef std::vector<Vertex> AVertex;
typedef std::vector<Face> AFace;
typedef std::vector<HalfEdge*> Aedge;
typedef std::vector<int> vec;
typedef std::pair<int, int> Pair;
typedef std::map<Pair, int> MyMap;
void readOBJ(std::string filename,AVertex &vertex,AFace &face,int &Nnode,int &Nface)
{
Nnode = Nface = 0;
std::ifstream in;
in.open(filename);
if (!in)
{
std::cout << "Open Failed!" << "check filename:"<< filename << "is right?" << '\n';
}
std::string buffer, type;
while(in>>type)
{
if (type == "v")
{
Nnode++;
Vertex ver;
in >> ver.x >> ver.y >> ver.z;
vertex.push_back(ver);
}
if (type == "f")
{
Nface++;
Face f;
in >> f.order[0] >> f.order[1] >> f.order[2];
f.order[0] -= 1;
f.order[1] -= 1;
f.order[2] -= 1;
face.push_back(f);
}
}
in.close();
}
void initEdge(Aedge &edge,int &Nedge,AVertex &vertex,AFace &face)
{
int Nnode = vertex.size()+10;
MyMap mapone;
int e_num = 1;
for (int i = 0; i < face.size(); i++)
{
HalfEdge *edge1 = new HalfEdge();
HalfEdge *edge2 = new HalfEdge();
HalfEdge *edge3 = new HalfEdge();
edge1->origin = face[i].order[0];
edge2->origin = face[i].order[1];
edge3->origin = face[i].order[2];
edge1->next = edge2;
edge2->next = edge3;
edge3->next = edge1;
HalfEdge *temp = new HalfEdge();
if (mapone[Pair(face[i].order[1],face[i].order[0])] != 0)
{
temp = edge[mapone[Pair(face[i].order[1], face[i].order[0])] - 1];
edge1->opposite = temp;
temp->opposite = edge1;
}
else
{
edge1->opposite = NULL;
mapone[Pair(face[i].order[0],face[i].order[1])] = e_num;
}
e_num++;
if (mapone[Pair(face[i].order[2],face[i].order[1])] != 0)
{
temp = edge[mapone[Pair(face[i].order[2], face[i].order[1])] - 1];
edge2->opposite = temp;
temp->opposite = edge2;
}
else
{
edge2->opposite = NULL;
mapone[Pair(face[i].order[1],face[i].order[2])] = e_num;
}
e_num++;
if (mapone[Pair(face[i].order[0],face[i].order[2])] != 0)
{
temp = edge[mapone[Pair(face[i].order[0], face[i].order[2])] - 1];
edge3->opposite = temp;
temp->opposite = edge3;
}
else
{
edge3->opposite = NULL;
mapone[Pair(face[i].order[2],face[i].order[0])] = e_num;
}
e_num++;
edge1->IncFace = i;
edge2->IncFace = i;
edge3->IncFace = i;
edge.push_back(edge1);
edge.push_back(edge2);
edge.push_back(edge3);
}
Nedge = edge.size();
/*for (int i = 0; i < Nedge; i++)
{
std::cout << edge[i]->IncFace << ' ' << edge[i]->next << ' ' << edge[i]->opposite << ' ' << edge[i]->origin << '\n';
}*/
}
HalfEdge* findOriginEdge(int v,int Nedge,Aedge &edge)
{
for (int k = 0; k < Nedge; k++)
{
if (edge[k]->origin == v)
{
return edge[k];
}
}
return NULL;
}
#endif // !READOBJ_H_