-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathMolecularEditor_old_main.cpp
More file actions
359 lines (288 loc) · 11.5 KB
/
MolecularEditor_old_main.cpp
File metadata and controls
359 lines (288 loc) · 11.5 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <math.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <SDL2/SDL_image.h>
//#include <SDL2/SDL_ttf.h>
//#include "Texture.h"
#include "Draw2D.h"
#include "fastmath.h"
#include "Vec3.h"
#include "Mat3.h"
#include "quaternion.h"
//#include "DynamicOpt.h"
#include "AtomTypes.h"
#include "MoleculeType.h"
#include "MolecularWorld.h"
#include "Draw.h"
#include "Draw3D.h"
#include "AppSDL2OGL_3D.h"
#include "SDL_utils.h"
#include "testUtils.h"
// font rendering:
// http://www.willusher.io/sdl2%20tutorials/2013/12/18/lesson-6-true-type-fonts-with-sdl_ttf
// http://stackoverflow.com/questions/28880562/rendering-text-with-sdl2-and-opengl
/*
int drawAtom( MoleculeType * mol, int i, int nsphere, float atomscale, uint32_t color ){
Draw::setRGB( color );
int nvert = Draw3D::drawSphere_oct( nsphere, atomscale*mol->typeList->vdwRs[mol->atypes[i]], mol->xyzs[i] );
return nvert;
}
int drawBond( MoleculeType * mol, int i, int j, int nstick, float bondwidth ){
Vec3f ai,aj;
convert( mol->xyzs[i], ai );
convert( mol->xyzs[j], aj );
int nvert = Draw3D::drawCylinderStrip( nstick, bondwidth, bondwidth, ai, aj );
return nvert;
}
*/
int renderMoleculeCPK ( MoleculeType * mol, int nsphere, int nstick, float atomscale, float bondwidth ){
if( mol->viewlist > 0 ) { glDeleteLists( mol->viewlist, 1 ); }
int nvert = 0;
mol->viewlist = glGenLists(1);
glNewList( mol->viewlist , GL_COMPILE );
glShadeModel ( GL_SMOOTH );
for( int i=0; i<mol->natoms; i++ ){
//printf("render atom %i \n", i);
//nvert+= drawAtom( mol, i, nsphere, atomscale, mol->typeList->colors[ mol->atypes[i] ] );
//printf("render atom %i %i \n", mol->atypes[i] );
uint32_t color = mol->typeList->colors[ mol->atypes[i] ];
//printf("render atom %i type %i color %i \n", i, mol->atypes[i], color );
Draw::setRGB( color );
nvert += Draw3D::drawSphere_oct( nsphere, atomscale*mol->typeList->vdwRs[mol->atypes[i]], mol->xyzs[i] );
}
if( mol->bonds != NULL ){
glColor3f( 0.2f, 0.2f, 0.2f );
for( int ib=0; ib<mol->nbonds; ib+=2 ){
//nvert+= drawBond( mol, , mol->bonds[ib+1], nstick, bondwidth );
Vec3f ai,aj;
convert( mol->xyzs[ mol->bonds[ib ] ], ai );
convert( mol->xyzs[ mol->bonds[ib+1] ], aj );
nvert += Draw3D::drawCylinderStrip( nstick, bondwidth, bondwidth, ai, aj );
}
}
glEndList();
//printf( " nvert %i \n", nvert );
return mol->viewlist;
}
// ============================
// MolecularEditorApp
// ============================
class MolecularEditorApp : public AppSDL2OGL_3D {
public:
MolecularWorld world;
int perFrame = 100;
bool converged = true;
double fmaxConverg = 0.00001;
FILE * fout_xyz = NULL;
virtual void draw ();
virtual void drawHUD();
//virtual void mouseHandling( );
virtual void eventHandling ( const SDL_Event& event );
MolecularEditorApp( int& id, int WIDTH_, int HEIGHT_ );
};
MolecularEditorApp::MolecularEditorApp( int& id, int WIDTH_, int HEIGHT_ ) : AppSDL2OGL_3D( id, WIDTH_, HEIGHT_ ) {
//MolecularWorld( char const* filename, MoleculeType * molTypeList );
world.fromDir( "inputs/", "atomTypes.ini", "molTypes.ini", "instances.ini" );
//world.loadLinkers("inputs/linkers.ini");
world.loadSplines("inputs/splines.ini"); //exit(0);
double r_start=4+0.001;
double r_end =10-0.001;
int nr =100;
double dr =(r_end-r_start)/(nr-1);
for(int i=0; i<nr; i++){
double r = i*dr+r_start;
double r2 = r*r;
double f = world.splines[0].get_splineR2(r2);
printf("%i %g %g\n", i, r, f);
}
//exit(0);
world.loadBonds ("inputs/bonds.ini");
world.checkBonds( 0.9, 1.2 );
//exit(0);
world.setCutoff( 6.0 );
world.makeFF ( );
world.optimizer->initOpt( 0.05, 0.15 );
for(int i=0; i<world.nMolTypes; i++){
printf(" rendering mol %i \n", i );
//world.molTypes[i].toCOG_average();
world.molTypes[i].findBonds( 0.6 );
//renderMoleculeCPK( &world.molTypes[i], 4, 8, 0.5, 0.2 );
//renderMoleculeCPK( &world.molTypes[i], 1, 3, 0.1, 0.05 );
//renderMoleculeCPK( &world.molTypes[i], 6, 3, 1.0, 0.05 );
renderMoleculeCPK( &world.molTypes[i], 6, 3, 0.9, 0.05 );
}
//world.atom2map( 0 );
//fout_xyz = fopen("relaxation.xyz","w");
/*
for(double r=0.5; r<10.0; r+=0.1){
world.pos[1].x = r;
world.optimizer->cleanForce();
world.assembleForces();
printf( "%f %f\n", r, world.fpos[1].x );
}
exit(0);
*/
}
void MolecularEditorApp::draw(){
glClearColor( 0.5f, 0.5f, 0.5f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//glDisable( GL_DEPTH_TEST );
//converged = true;
//delay = 100; world.optimizer->dt_max = 0.00001; world.optimizer->dt_max = 0.00001; perFrame=1;
//delay = 1000; perFrame=1;
//perFrame=1; // world.optimizer->dt_max = 0.01;
//world.optimizer->dt_max = 0.01;
//world.nonCovalent = false;
if( !converged ){
long tick1 = getCPUticks();
int iter;
for(iter=0; iter<perFrame; iter++){
world.rigidOptStep( );
printf(" opt step %i fmax %g \n", world.optimizer->stepsDone, world.fmax );
if( world.fmax < fmaxConverg ){
converged = true;
printf(" converged after %i step \n", world.optimizer->stepsDone );
if(fout_xyz){ fclose(fout_xyz); fout_xyz = NULL; }
fout_xyz = fopen("relaxed.xyz", "w");
char str[256];
sprintf(str,"# fmax = %g", world.fmax );
world.exportAtomsXYZ( fout_xyz, str );
fclose(fout_xyz); fout_xyz = NULL;
break;
}
if( (getCPUticks()-tick1)>4e+8 ) break;
}
double ticks = (getCPUticks() - tick1)/((double)perFrame);
printf("======= %i %5.2f Mticks/iter %5.2f tick/atom %5.2f ticks/interaction \n", iter, ticks*1.0e-6, ticks/world.nAtomTot, ticks/world.nInteractions );
world.saveInstances( "instances_lastStep.ini" );
if(fout_xyz){
char str[256];
sprintf(str,"# fmax = %g", world.fmax );
world.exportAtomsXYZ( fout_xyz, str );
}
}
//exit(0);
//world.nonBondingFroces_buf(); return;
glMatrixMode(GL_MODELVIEW);
//glMatrixMode(GL_PROJECTION);
glEnable(GL_LIGHTING);
for (int i=0; i<world.nmols; i++){
if( world.instances[i]->viewlist > 0 ){
/*
glPushMatrix();
Mat3d rotmat;
float glMat[16];
//rot[i].toMatrix_unitary2( rotmat );
//rot[i].toMatrix_unitary( rotmat );
//printf( "%i (%3.3f,%3.3f,%3.3f) (%3.3f,%3.3f,%3.3f,%3.3f)\n", i, world.pos[i].x,world.pos[i].y,world.pos[i].z, world.rot[i].x,world.rot[i].y,world.rot[i].z,world.rot[i].w );
world.rot[i].toMatrix( rotmat );
glColor3f(0.0f,0.0f,0.0f); Draw3D::drawPointCross(world.pos[i],1.0);
Draw3D::toGLMat( world.pos[i], rotmat, glMat ); // somehow not working
//Draw3D::toGLMat( {0.0,0.0,0.0}, rotmat, glMat );
glMultMatrixf( glMat );
//glTranslatef( world.pos[i].x,world.pos[i].y,world.pos[i].z );
//glMultTransposeMatrixf( glMat );
//glLoadMatrixf( glMat );
glCallList ( world.instances[i]->viewlist );
glPopMatrix();
*/
//Draw3D::drawMatInPos( world.rot[i], world.pos[i] );
Draw3D::drawShape( world.instances[i]->viewlist, world.pos[i], world.rot[i] );
}
};
//printf(" nLinkers %i &linkers %i \n" , world.nLinkers, world.linkers );
glDisable(GL_LIGHTING);
glColor3f(0.0f,1.0f,0.0f);
if( world.linkers ){
for (int il=0; il<world.nLinkers; il++){
Mat3d T;
Vec3d gpi,gpj;
MolecularLink& li = world.linkers[il];
int i = li.i;
world.rot[i].toMatrix( T);
T.dot_to( li.posi, gpi );
gpi.add( world.pos[i] );
int j = li.j;
world.rot[j].toMatrix(T);
T.dot_to( li.posj, gpj );
gpj.add( world.pos[j] );
Draw3D::drawLine( gpi, gpj );
Draw3D::drawPointCross(gpi,0.5);
Draw3D::drawPointCross(gpj,0.5);
//printf( "%i (%i,%i) (%3.3f,%3.3f,%3.3f) (%3.3f,%3.3f,%3.3f)\n" , il, i,j, li.posi.x, li.posi.y, li.posi.z, li.posj.x, li.posj.y, li.posj.z );
//printf( "%i (%3.3f,%3.3f,%3.3f) (%3.3f,%3.3f,%3.3f)\n" , il, gpi.x, gpi.y, gpi.z, gpj.x,gpj.y,gpj.z);
}
}
if( world.bonds ){
for (int il=0; il<world.nBonds; il++){
Mat3d T;
Vec3d lpi,lpj,gpi,gpj;
MolecularBond& bi = world.bonds[il];
int i = bi.imol;
world.rot[i].toMatrix( T);
lpi = world.instances[i]->xyzs[bi.iatom];
T.dot_to( lpi, gpi );
gpi.add( world.pos[i] );
int j = bi.jmol;
world.rot[j].toMatrix(T);
lpj = world.instances[j]->xyzs[bi.jatom];
T.dot_to( lpj, gpj );
gpj.add( world.pos[j] );
Draw3D::drawLine( gpi, gpj );
//printf( "%i (%i,%i) (%3.3f,%3.3f,%3.3f) (%3.3f,%3.3f,%3.3f)\n" , il, i,j, li.posi.x, li.posi.y, li.posi.z, li.posj.x, li.posj.y, li.posj.z );
//printf( "%i (%3.3f,%3.3f,%3.3f) (%3.3f,%3.3f,%3.3f)\n" , il, gpi.x, gpi.y, gpi.z, gpj.x,gpj.y,gpj.z);
}
}
//exit(0);
};
void MolecularEditorApp::drawHUD(){}
void MolecularEditorApp::eventHandling ( const SDL_Event& event ){
//printf( "NBodyWorldApp::eventHandling() \n" );
switch( event.type ){
case SDL_KEYDOWN :
switch( event.key.keysym.sym ){
case SDLK_SPACE: converged = !converged; break;
//case SDLK_0: formation_view_mode = 0; printf( "view : default\n" ); break;
//case SDLK_1: formation_view_mode = VIEW_INJURY; printf( "view : injury\n" ); break;
//case SDLK_2: formation_view_mode = VIEW_STAMINA; printf( "view : stamina\n" ); break;
//case SDLK_3: formation_view_mode = VIEW_CHARGE; printf( "view : charge\n" ); break;
//case SDLK_4: formation_view_mode = VIEW_MORAL; printf( "view : moral\n" ); break;
}
break;
case SDL_MOUSEBUTTONDOWN:
switch( event.button.button ){
case SDL_BUTTON_LEFT:
break;
case SDL_BUTTON_RIGHT:
break;
}
break;
/*
case SDL_MOUSEBUTTONUP:
switch( event.button.button ){
case SDL_BUTTON_LEFT:
//printf( "left button pressed !!!! " );
world.picked = NULL;
break;
}
break;
*/
};
AppSDL2OGL_3D::eventHandling( event );
camStep = zoom*0.05;
}
// ===================== MAIN
MolecularEditorApp * thisApp;
int main(int argc, char *argv[]){
SDL_Init(SDL_INIT_VIDEO);
SDL_GL_SetAttribute(SDL_GL_SHARE_WITH_CURRENT_CONTEXT, 1);
int junk;
thisApp = new MolecularEditorApp( junk , 800, 600 );
thisApp->zoom = 30;
thisApp->loop( 1000000 );
return 0;
}