-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOFFtoVTK.cpp
108 lines (99 loc) · 2.7 KB
/
OFFtoVTK.cpp
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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fstream>
class ChangeFormat{
public:
//~ChangeFormat();
ChangeFormat(){}
ChangeFormat(ChangeFormat& cf){
int len = strlen(cf.in);
this->in = (char*)malloc(len + 1);
strcpy(this->in, cf.in);
len = strlen(cf.out);
this->out = (char*)malloc(len + 1);
strcpy(this->out, cf.out);
}
ChangeFormat& operator=(ChangeFormat& cf){
int len = strlen(cf.in);
this->in = (char*)malloc(len + 1);
strcpy(this->in, cf.in);
len = strlen(cf.out);
this->out = (char*)malloc(len + 1);
strcpy(this->out, cf.out);
return *this;
}
void setOut(char* out){
int len = strlen(out);
this->out = (char*)malloc(len + 1);
strcpy(this->out, out);
}
void setIn(char* in){
int len = strlen(in);
this->in = (char*)malloc(len + 1);
strcpy(this->in, in);
}
void OFFtoVTK();
private:
char* in;
char* out;
size_t vertex_num;
size_t face_num;
size_t edge_num;
void OFFset(FILE*);
void VTKheader(FILE*);
void VTKpolyhedron(FILE*);
};
void ChangeFormat::OFFset(FILE* off){
char buffer[50];
fscanf(off, "%s", buffer);
fscanf(off, "%s", buffer);
this->vertex_num = atoi(buffer);
fscanf(off, "%s", buffer);
this->face_num = atoi(buffer);
fscanf(off, "%s", buffer);
this->edge_num = atoi(buffer);
}
void ChangeFormat::VTKheader(FILE* vtk){
fprintf(vtk, "# vtk DataFile Version 1.0\n");
fprintf(vtk, "Just for test\n");
fprintf(vtk, "ASCII\nDATASET POLYDATA\n");
fprintf(vtk, "POINTS %zu float\n", this->vertex_num);
}
void ChangeFormat::VTKpolyhedron(FILE* vtk){
fprintf(vtk,"POLYGONS %zu %zu\n", this->face_num, 4 * this->face_num);
}
void ChangeFormat::OFFtoVTK(){
char buffer[50];
FILE* in_file = fopen(this->in, "r");
FILE* out_file = fopen(this->out, "w+");
OFFset(in_file);
VTKheader(out_file);
size_t v_times = 3 * this->vertex_num;
for (size_t i = 0; i < v_times; i++){
fscanf(in_file, "%s", buffer);
fprintf(out_file, "%s ", buffer);
if ((i%3) == 2){
fprintf(out_file, "\n");
}
}
VTKpolyhedron(out_file);
size_t f_times = 4 * this->face_num;
for (size_t i = 0; i < f_times; i++){
fscanf(in_file, "%s", buffer);
fprintf(out_file, "%s ", buffer);
if ((i%4) == 3){
fprintf(out_file, "\n");
}
}
}
int main(int argc, char* argv[]){
if (argc < 2){
fprintf(stderr,"please input infile & outfile\n");
exit(1);
}
ChangeFormat cf;
cf.setIn(argv[1]);
cf.setOut(argv[2]);
cf.OFFtoVTK();
}