-
Notifications
You must be signed in to change notification settings - Fork 0
/
make3D_C++.cc
189 lines (163 loc) · 4.82 KB
/
make3D_C++.cc
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
178
179
180
181
182
183
184
185
186
187
188
189
/* Shine - NMR NOESY simulation program
Copyright (C) 2015 Michael Riss
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <math.h>
using namespace std;
// This is a port of make3d of the fortran spirit package.
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define LW2RHO 2.35482
class entry{
public:
int residx;
int residy;
int residz;
string atmx;
string atmy;
string atmz;
float shx;
float shy;
float shz;
float lwx;
float lwy;
float lwz;
float vol;
};
int main( int argc, char** argv ){
if( argc != 3 ){
cout << "Usage: make3D_C++ <peaklistfile> <spectrafile>" << endl;
exit(1);
}
ifstream in( argv[1] );
if( !in.good() ){
cout << "Can't open " << argv[1] << ". Exiting." << endl;
exit(1);
}
int nrmix;
float mixt;
unsigned int resx, resy, resz;
float swx, swy, swz;
in >> nrmix >> mixt;
in >> resx >> resy >> resz;
in >> swx >> swy >> swz;
float rsx, rsy, rsz;
rsx = (float)resx / swx;
rsy = (float)resy / swy;
rsz = (float)resz / swz;
string sshx, sshy, sshz;
vector<entry> list;
char buffer[1024];
in.getline( buffer, 1024 ); // finish current line
while( true ){
entry e;
in.getline( buffer, 1024 );
if( in.eof() ){
break;
}
stringstream pbuf( buffer );
pbuf >> e.residx >> e.atmx >> e.residy >> e.atmy >> e.residz >> e.atmz
>> sshx >> sshy >> sshz >> e.lwx >> e.lwy >> e.lwz;
if( sshx == "nan" ){
e.shx = NAN;
}
else{
e.shx = atof( sshx.c_str() ) - 1.0;
}
if( sshy == "nan" ){
e.shy = NAN;
}
else{
e.shy = atof( sshy.c_str() ) - 1.0;
}
if( sshz == "nan" ){
e.shz = NAN;
}
else{
e.shz = atof( sshz.c_str() ) - 1.0;
}
e.lwx = e.lwx * rsx;
e.lwy = e.lwy * rsy;
e.lwz = e.lwz * rsz;
in.getline( buffer, 1024 );
if( in.eof() ){
break;
}
stringstream pbuf2( buffer );
pbuf2 >> e.vol;
e.vol = e.vol * ( 1000000.0 * 1.0 / ( e.lwx * e.lwy * e.lwz ) ) * 10000.0;
list.push_back( e );
}
float* matrix = new float[resz * resy * resx];
for( unsigned int z = 0; z < resz; z++ ){
for( unsigned int y = 0; y < resy; y++ ){
for( unsigned int x = 0; x < resx; x++ ){
matrix[x + y*resx + z*resy*resx] = 0.0;
}
}
}
for( vector<entry>::iterator it = list.begin();
it != list.end();
it++ ){
if( isnan( it->shx ) || isnan( it->shy ) || isnan( it->shz ) ){
continue;
}
float lwzmax = 3.6 * it->lwz + 1.0;
int z1 = (int) ( it->shz - lwzmax );
int z2 = (int) ( it->shz + lwzmax );
float lwymax = 3.6 * it->lwy + 1.0;
int y1 = (int) ( it->shy - lwymax );
int y2 = (int) ( it->shy + lwymax );
float lwxmax = 3.6 * it->lwx + 1.0;
int x1 = (int) ( it->shx - lwxmax );
int x2 = (int) ( it->shx + lwxmax );
float rhoz = it->lwz / LW2RHO;
float rhoy = it->lwy / LW2RHO;
float rhox = it->lwx / LW2RHO;
for( unsigned int z = (z1+resz) % resz;
z != (z2+1) % resz; z = (z+1) % resz ){
for( unsigned int y = (y1+resy) % resy;
y != (y2+1) % resy; y = (y+1) % resy ){
for( unsigned int x = (x1+resx) % resx;
x != (x2+1) %resx; x = (x+1) % resx ){
float dz = fabsf( (float) z - it->shz );
float dzf = fabsf( (float) resz - dz );
float dy = fabsf( (float) y - it->shy );
float dyf = fabsf( (float) resy - dy );
float dx = fabsf( (float) x - it->shx );
float dxf = fabsf( (float) resx - dx );
float add = it->vol *
( expf( -( dz * dz ) / ( 2.0 * rhoz*rhoz ) ) +
expf( -( dzf * dzf ) / ( 2.0 * rhoz*rhoz ) ) ) *
( expf( -( dy * dy ) / ( 2.0 * rhoy*rhoy ) ) +
expf( -( dyf * dyf ) / ( 2.0 * rhoy*rhoy ) ) ) *
( expf( -( dx * dx ) / ( 2.0 * rhox*rhox ) ) +
expf( -( dxf * dxf ) / ( 2.0 * rhox*rhox ) ) );
matrix[x + y*resx + z*resy*resx] += add;
}
}
}
}
ofstream out( argv[2] );
if( !out.good() ){
cout << "Cannot open " << argv[2] << ". Exiting." << endl;
exit(1);
}
out.write( (char*) matrix, resz * resy * resx * sizeof(float) );
out.close();
delete matrix;
return 0;
}