-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathogl2obj.m
executable file
·173 lines (153 loc) · 4.69 KB
/
ogl2obj.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
filein = 'molden.ogl';
fileout = 'test.obj';
% function [atomtype, atomcoord, atomconn] = obj2obj(filein, fileout)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% ogl2obj.m - converts OGL file format from molden visualizer into OBJ 3d
% graphics file format
%
% Last Updated: 6/22/2007 WM
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% OGL file format
% line 1: header
% line 2: molecule header
% line 3: size of molecule
% line 4: number of atoms
% lines : definition of each molecule
% : <atomic number> <x coord> <y coord> <z coord> <number of
% connections> <connected to atom number> <...>
% line : surface header postive part of wavefunction
% lines : surface data consisting of normal, then vertex of triangles
% line : surface header negative part
% lines : surface data
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Data Variables:
% atomtype - atomic number
% atomcoord - coordinate of atom
% atomconn - connections b/w this atom and others
% surface1 - normal and vertex data alternating for triangles for + wavefn
% surface2 - normal and vertex data alternating for triangles for - wavefn
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp 'Reading OGL...'
fid = fopen(filein);
flag_surf1 = 0;
flag_surf2 = 0;
nsurf1 = 0;
nsurf2 = 0;
% check header
tline = strtrim(fgetl(fid));
if ~strcmp(tline,'[MOLDENOGL]')
disp 'error'
return
end
% get molecule
tline = strtrim(fgetl(fid));
[tline rem] = strtok(tline);
if ~strcmp(tline,'[MOLECULE]')
disp 'error'
return
end
% get size
tline = strtrim(fgetl(fid));
[num1 rem] = strtok(tline);
[num2 rem] = strtok(rem);
[num3 rem] = strtok(rem);
Lx = str2num(num1);
Ly = str2num(num2);
Lz = str2num(num3);
% get number of atoms
tline = strtrim(fgetl(fid));
Natoms = str2num(tline);
% list atoms
for i = 1:Natoms
% get atom type
tline = strtrim(fgetl(fid));
[buf rem] = strtok(tline);
atomtype(i) = str2num(buf);
% get coordinates of atom
[buf rem] = strtok(rem);
atomcoord(i,1) = str2num(buf);
[buf rem] = strtok(rem);
atomcoord(i,2) = str2num(buf);
[buf rem] = strtok(rem);
atomcoord(i,3) = str2num(buf);
% get number of connections
[buf rem] = strtok(rem);
j = 0;
% get connections
while(rem)
[buf rem] = strtok(rem);
j = j + 1;
atomconn(i,j) = str2num(buf);
end
end
% read surface information
while feof(fid) == 0
tline = strtrim(fgetl(fid));
if flag_surf1 && ~flag_surf2 && strcmp('[SURFACE]',tline)
disp '2nd surface'
flag_surf2 = 1;
elseif ~flag_surf1 && strcmp('[SURFACE]',tline)
disp '1st surface'
flag_surf1 = 1;
elseif flag_surf1
[num1 rem] = strtok(tline);
[num2 rem] = strtok(rem);
[num3 rem] = strtok(rem);
if flag_surf2
nsurf2 = nsurf2 + 1 ;
surface2(nsurf2, 1) = str2num(num1);
surface2(nsurf2, 2) = str2num(num2);
surface2(nsurf2, 3) = str2num(num3);
elseif flag_surf1
nsurf1 = nsurf1 +1 ;
surface1(nsurf1, 1) = str2num(num1);
surface1(nsurf1, 2) = str2num(num2);
surface1(nsurf1, 3) = str2num(num3);
end
end
end
fclose(fid);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Write surfaces
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp 'Writing OBJ...'
fout = fopen(fileout, 'w');
% verticies
disp 'verts...'
for i = 1:nsurf1/2
fprintf(fout, 'v %f %f %f\n', surface1(2*i,1), surface1(2*i,2), surface1(2*i,3));
end
for i = 1:nsurf2/2
fprintf(fout, 'v %f %f %f\n', surface2(2*i,1), surface2(2*i,2), surface2(2*i,3));
end
% normals
for i = 1:nsurf1/2
fprintf(fout, 'vn %f %f %f\n', surface1(2*i-1, 1), surface1(2*i-1, 2), surface1(2*i-1, 3));
end
for i = 1:nsurf2/2
fprintf(fout, 'vn %f %f %f\n', surface2(2*i-1, 1), surface2(2*i-1, 2), surface2(2*i-1, 3));
end
% faces
disp 'faces...'
fprintf(fout,'g surface1\n');
for i = 1:(nsurf1)/6
fprintf(fout, 'f %i//%i %i//%i %i//%i\n', ...
3*i-2, 3*i-2, ...
3*i-1, 3*i-1, ...
3*i, 3*i);
end
fprintf(fout,'g surface2\n');
for j = 1:nsurf2/6
i = j + nsurf1/6;
fprintf(fout, 'f %i//%i %i//%i %i//%i\n', ...
3*i-2, 3*i-2, ...
3*i-1, 3*i-1, ...
3*i, 3*i);
end
fclose(fout);
plotmolecule(atomtype, atomcoord, atomconn);
plotwavefn(surface1, surface2);
disp 'Done'
beep