-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
1531 lines (1220 loc) · 56.7 KB
/
main.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <FL/Fl.H>
#include <FL/Fl_Gl_Window.H>
#include <FL/Fl_Hor_Slider.H>
#include <FL/Fl_Roller.H>
#include <FL/Fl_Toggle_Button.H>
#include <FL/Fl_Menu_Button.H>
#include <FL/Fl_Menu_Bar.H>
#include <FL/Fl_File_Chooser.H>
#include <FL/Fl_Text_Display.H>
#include <FL/Fl_Help_Dialog.H>
#include <FL/Fl_Progress.H>
#include <FL/Fl_Multiline_Output.H>
#include <FL/fl_draw.H>
#include <FL/gl.h>
#include <GL/glu.h>
#include "geomath.h"
#include "gl_param.h"
#include "atom_rads.h"
#include "graphprim.h"
#include "lbfgs/lbfgs.h"
#define atm(a,x) atoms[bonds[i].a-1].x
#define ax1 atm(a,x)
#define ax2 atm(b,x)
#define ay1 atm(a,y)
#define ay2 atm(b,y)
#define az1 atm(a,z)
#define az2 atm(b,z)
#define len(x,y,z,xx,yy,zz) sqrt(sqr((x)-(xx))+sqr((y)-(yy))+sqr((z)-(zz)))
#define sqr(a) ((a)*(a))
const int
tw = 400, // message window width
wh = 600, ww = wh + tw, // main window height and width
sw = 20, // width or height of axes rotation sliders
sr = 30, // height of zoom roller
ds = 5, // distance between elements
rw = 60; // roller width
const char * AboutText =
"<HTML>\n"
"<HEAD>\n"
"<TITLE>ViewMol3D</TITLE>\n"
"</HEAD>\n"
"<BODY BGCOLOR='#ffffff'>\n"
"<h1 align=center>ViewMol3D</h1>\n"
"<h2 align=center>A 3D OpenGL viewer for molecular structures<br>\n"
"from the output of quantum chemistry calculations.<br></h2>\n"
"<p align=left><font size=4>ViewMol3D uses OpenGL (OpenGL is a trademark of Silicon Graphics,"
"Inc.) 3D graphical system to render molecules as wire frame, sticks, ball-and-sticks and CPK models. The ViewMol3D can draw"
"molecules models from output of several quantum chemistry programs.</font></p>"
"<p align=left><font color=blue size=5>Possibilities</font>"
"<font size=4>"
"<ul>"
" <li><p align=left>Showing the geometry of a molecule</p></li>"
" <li><p align=left>Tracing a geometry optimization or a MD trajectory</p></li>"
" <li><p align=left>Showing normal vibrations of a molecule as arrows</p></li>"
" <li><p align=left>Showing forces acting on each atom in a selected configuration</p></li>"
" <li><p align=left>Saving all generated pictures as BMP/PNG file.</p></li>"
"</ul>"
"</font>"
"<p align=left><font color=blue size=5>File Formats Supported</font>"
"<font size=4>"
"<ul>"
" <li><p align=left>Gaussian 9x/03 OUT files</p></li>"
" <li><p align=left>GAMESS(US)/PC GAMESS OUT files</p></li>"
" <li><p align=left>MOPAC/AMPAC OUT files</p></li>"
" <li><p align=left>XYZ files with Cartesian coordinates</p></li>"
" <li><p align=left>Tripos Alchemy MOL files</p></li>"
"</ul>"
"</font>"
// "<H2>ViewMol3D</H2>\n"
// "<P>is a program for visualization of molecular models.</P>\n"
"</BODY>\n"
"</HTML>";
const int // selection levels
at_level = 1000, // atoms
bn_level = 3000, // bonds
gr_level = 4000; // gradients
mindo3 *mcalc;
atomvector_ehm atoms_ehm;
class MyGlWindow :
public Fl_Gl_Window, // OpenGL window and interface elements
public ReadMoleculeBond, // Molecule specification and reading from files
public LBFGS // L-BFGS optimization
{
public:
// Name of the input file
char fname[256];
// global variables
int
trad, // true if van-der-vaals radii
bond, // is there bonds or not
persp, // perpective
LowQual, // bonds are wires
dline, // bonds are lines
chg, // show atomic charges
present, // presentation mode
monum, // MO number
atnum, // show atomic numbers
viewir, // show freq vecs
frcur, // cursor position for printing
viewgr, // show grad vecs
strnum, // number of structure to show
xaccl, // graphic accelerator is used
screenNum, // number of saved screens
irc, // show IRC graph
clrbnd, // flag of colored bonds
pbuff, // pixel buffers are absent
mrkatm, // show marked atoms
DrawMO, // viaualize molecular orbitals
DrawAxes, // xyz axes
DrawSurf // surface
;
int ap1, ap2, ap3, ap4; // marked atoms numbers
int x_select,y_select; // coordinates of the selection
int select_mode; // default mode GL_RENDER, not GL_SELECT
int select_hit; // which object was select
int slnormalsphere, slnormalcylinder, slpresentsphere, slpresentcylinder;
double quality, old_quality;
double old_angle_x, angle_x;
double old_angle_y, angle_y;
double old_angle_z, angle_z;
int numorb;
double Energy, RMS;
int funcalls;
Fl_Window *win;
Fl_Menu_Bar *menubar;
Fl_Text_Buffer *buff;
Fl_Text_Display *disp;
Fl_Roller *zoom;
Fl_Hor_Slider *slider_x;
Fl_Slider *slider_y;
Fl_Slider *slider_z;
Fl_Button *get_len;
Fl_Button *get_ang;
Fl_Button *get_dih;
Fl_Button *get_zmt;
Fl_Help_Dialog *about;
int ReadInputFile() {
int error=0;
char xstr[64];
strcpy (xstr, &fname[strlen(fname)-3]);
if ((strncmp (xstr, "out", 3) == 0) || (strncmp (xstr, "OUT", 3) == 0) ||
(strncmp (xstr, "gms", 3) == 0) || (strncmp (xstr, "GMS", 3) == 0) ||
(strncmp (xstr, "log", 3) == 0) || (strncmp (xstr, "LOG", 3) == 0) ||
(strncmp (xstr, "mop", 3) == 0) || (strncmp (xstr, "MOP", 3) == 0)) {
if (OutFile (fname,strnum) != 0) {
// sprintf(lpInfo,"Can't open the GAMESS/G98/MOPAC OUT file '%s'", fname);
error=1;
}
} else if ( (strncmp (xstr, "mol", 3) == 0) || (strncmp (xstr, "MOL", 3) == 0) ) {
if (ALCHEMY (fname) != 0) {
// sprintf(lpInfo,"Can't open the ALCHEMY MOL file '%s'", fname);
error=1;
} else dline=0;
} else if ( (strncmp (xstr, "xyz", 3) == 0) || (strncmp (xstr, "XYZ", 3) == 0) ) {
// } else if (strncasecmp (xstr, "xyz", 3) == 0) {
if (XYZ (fname,strnum) != 0) {
// sprintf(lpInfo,"Can't open the XYZ file '%s'", fname);
error=1;
} else dline=0;
} else {
// sprintf(lpInfo,"I don't know what kind of this file:'%s'. Please give me a suitable extention (MOL/OUT/LOG/GMS/MOP)",fname);
error=1;
}
if ( error != 0 ) {
// MessageBox(NULL, lpInfo, "Error", MB_ICONERROR | MB_OK);
return FALSE;
} else return TRUE;
}
double maxcor, x0,y0,xm,ym,z0,zm;
float roma[4][4], romb[4][4];
void CalcMaxCor() {
// if ( ReadMol == NULL ) return;
if ( atoms.size() <= 0 ) return;
float xx=fabs(atoms[0].x);
for(int i=0;i<atoms.size();i++) {
if(fabs(atoms[i].x)>xx) xx=fabs(atoms[i].x);
if(fabs(atoms[i].y)>xx) xx=fabs(atoms[i].y);
if(fabs(atoms[i].z)>xx) xx=fabs(atoms[i].z);
}
maxcor=xx+xx;
x0=-maxcor/1.2; xm=maxcor/1.2;
y0=-maxcor/1.2; ym=maxcor/1.2;
z0=-maxcor/1.2; zm=maxcor/1.2;
}
void SetOrtho() {
if (select_mode == GL_SELECT) {
int vp[4]; // view port
glGetIntegerv(GL_VIEWPORT, vp);
glViewport (0, 0, w(), h());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPickMatrix(x_select, h()-y_select, 1, 1, vp);
} else {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
}
double ww=w(); double hh=h();
double wh = ww/hh;
double mwh = maxcor*wh;
if (!persp) {
if (ww <= hh)
glOrtho (-maxcor, maxcor, -mwh, mwh, -maxcor, maxcor);
else
glOrtho (-mwh, mwh, -maxcor, maxcor, -maxcor, maxcor);
} else {
gluPerspective (50.0, wh, 1.0, 50.0);
}
glMatrixMode (GL_MODELVIEW);
glLoadIdentity ();
if (persp) glTranslated (0, 0, -2 * maxcor); // viewing transform
}
void InitGL() {
glClearColor(0., 0., 0., 0.);
glViewport (0, 0, w(), h());
// if (persp) glTranslated (0, 0, -2 * maxcor); // viewing transform
SetOrtho();
glEnable(GL_AUTO_NORMAL);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
glClearDepth(1.0);
float
light_position1[4]= {float(-maxcor/2),float(-maxcor/2),float(maxcor ),0},
light_position2[4]= {float( maxcor/2),float( maxcor/2),float(maxcor/2),0},
light_position3[4]= {float( 0.0 ),float( 0.0 ),float(maxcor ),0};
const float
lm_ambient1 [4] = { 0.2f, 0.2f, 0.2f, 1.0f },
light_color1 [4] = { 1.0f, 0.8f, 0.7f, 1.0f },
light_color2 [4] = { 0.7f, 0.9f, 1.0f, 1.0f },
light_color3 [4] = { 0.2f, 0.2f, 0.2f, 1.0f },
light_color_diffuse1 [4] = { 0.6f, 0.6f, 0.6f, 1.0f },
light_color_diffuse2 [4] = { 0.6f, 0.6f, 0.6f, 1.0f },
light_color_diffuse3 [4] = { 0.6f, 0.6f, 0.6f, 1.0f };
/*
float v1[4],v2[4],v3[4],v4[4],v5[3],v6,v7,v8,v9,v10; int v11;
glGetLightfv(GL_LIGHT0,GL_AMBIENT,v1); printf("1 %f %f %f %f\n",v1[0],v1[1],v1[2],v1[3]);
glGetLightfv(GL_LIGHT0,GL_DIFFUSE,v2); printf("2 %f %f %f %f\n",v2[0],v2[1],v2[2],v2[3]);
glGetLightfv(GL_LIGHT0,GL_SPECULAR,v3); printf("3 %f %f %f %f\n",v3[0],v3[1],v3[2],v3[3]);
glGetLightfv(GL_LIGHT0,GL_POSITION,v4); printf("4 %f %f %f %f\n",v4[0],v4[1],v4[2],v4[3]);
glGetLightfv(GL_LIGHT0,GL_SPOT_DIRECTION,v5); printf("5 %f %f %f \n",v5[0],v5[1],v5[2]);
glGetLightfv(GL_LIGHT0,GL_SPOT_EXPONENT,&v6); printf("6 %f \n",v6);
glGetLightfv(GL_LIGHT0,GL_SPOT_CUTOFF,&v7); printf("7 %f \n",v7);
glGetLightfv(GL_LIGHT0,GL_CONSTANT_ATTENUATION,&v8); printf("8 %f \n",v8);
glGetLightfv(GL_LIGHT0,GL_LINEAR_ATTENUATION,&v9); printf("9 %f \n",v9);
glGetLightfv(GL_LIGHT0,GL_QUADRATIC_ATTENUATION,&v10); printf("A %f \n",v10);
*/
glLightfv(GL_LIGHT0, GL_POSITION, light_position1);
glLightfv(GL_LIGHT0, GL_SPECULAR, light_color1);
// glLightfv(GL_LIGHT0, GL_DIFFUSE, light_color_diffuse1);
glLightfv(GL_LIGHT1, GL_POSITION, light_position2);
glLightfv(GL_LIGHT1, GL_SPECULAR, light_color2);
// glLightfv(GL_LIGHT1, GL_DIFFUSE, light_color_diffuse2);
glLightfv(GL_LIGHT2, GL_POSITION, light_position3);
glLightfv(GL_LIGHT2, GL_SPECULAR, light_color3);
// glLightfv(GL_LIGHT2, GL_DIFFUSE, light_color_diffuse3);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lm_ambient1);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHT1);
// glEnable(GL_LIGHT2);
glEnable(GL_LIGHTING);
if (present) glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
glEnable(GL_NORMALIZE);
glShadeModel(GL_SMOOTH);
const float
mat_ambient1 [4] = { 1.0f, 1.0f, 1.0f, 1.0f },
mat_diffuse1 [4] = { 0.6f, 0.6f, 0.6f, 1.0f },
mat_specular1 [4] = { 0.8f, 0.8f, 0.8f, 0.8f },
mat_emission1 [4] = { 0.0f, 0.0f, 0.0f, 1.0f };
glMaterialfv(GL_FRONT, GL_AMBIENT , mat_ambient1 );
glMaterialfv(GL_FRONT, GL_DIFFUSE , mat_diffuse1 );
glMaterialfv(GL_FRONT, GL_SPECULAR , mat_specular1);
glMaterialfv(GL_FRONT, GL_EMISSION , mat_emission1);
glMaterialf (GL_FRONT, GL_SHININESS, 40.f );
}
void drawmark (float color[4], float angle, float rad, int sl) {
glMaterialfv (GL_FRONT, GL_DIFFUSE, color);
glRotatef(angle, 1.0, 1.0, 1.0);
// glutWireSphere(rad,sl/2,sl/2);
glTranslatef(0.0,-rad/10.0,0.0); Cylinder(rad,rad,rad/5.0,sl);
glTranslatef(0.0,rad/10.0,-rad/10.0); glRotatef(90, 1.0, 0.0, 0.0); Cylinder(rad,rad,rad/5.0,sl);
glTranslatef(rad/10.0,rad/10.0,0.0); glRotatef(90, 0.0, 0.0, 1.0); Cylinder(rad,rad,rad/5.0,sl);
}
void print_label(int n, int i, float c[3]) {
char p[32];
if (n>0)
sprintf(p,"%u[%u]",i+1,n);
else
sprintf(p,"%u",i+1);
rotvec (c, romb);
glRasterPos3f( atoms[i].x+c[0], // label position
atoms[i].y+c[1], // plus atom radius
atoms[i].z+c[2] );
if (p[0]!=0) gl_draw(p, strlen(p));
}
// DRAW METHOD
void draw() {
// First time: init viewport, etc.
if ( !valid() ) {
valid(1);
CalcMaxCor();
InitGL();
char msg[256];
sprintf(msg,"OpenGL initialized:\n"
" Vendor : %s\n"
" Renderer : %s\n"
" Version : %s\n"
" GLU Version : %s\n",
glGetString (GL_VENDOR ),
glGetString (GL_RENDERER),
glGetString (GL_VERSION ),
gluGetString (GLU_VERSION));
buff->text(msg);
}
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear screen
glPushMatrix();
glMultMatrixf((float*)roma); // rotate the system
glPushMatrix();
// draw atoms
int sl = bond ?
sl = present ? slpresentcylinder : slnormalcylinder // No atoms, just bonds
:
sl = present ? slpresentsphere : slnormalsphere
;
int i=0;
for ( vector <struct atom>::iterator it = atoms.begin(); it!=atoms.end(); it++, i++) {
glPushMatrix ();
float rad;
glTranslated (atoms[i].x, atoms[i].y, atoms[i].z);
if (select_mode == GL_SELECT) glLoadName(at_level+i);
// set material
if (chg) { // atoms charge
if(atoms[i].c>0) glMaterialfv (GL_FRONT, GL_DIFFUSE, PosChg);
else glMaterialfv (GL_FRONT, GL_DIFFUSE, NegChg);
rad=fabs(atoms[i].c)*2;
} else {
if (bond) { // no atoms
if(clrbnd) glMaterialfv (GL_FRONT, GL_DIFFUSE, at_def[atoms[i].type-1]);
else glMaterialfv (GL_FRONT, GL_DIFFUSE, bondmat);
rad=0; // if(dline) - bonds are linear
if(!dline) // if bonds are cylindrical
for ( vector <struct bond>::iterator it = bonds.begin(); it!=bonds.end(); it++) { // Radius of a biggest bonds cylinder
if ((((*it).a-1==i)||((*it).b-1==i))&&((*it).o>rad)) rad=(*it).o;
}
} else { // Atoms are drawn
glMaterialfv (GL_FRONT, GL_DIFFUSE, at_def[atoms[i].type-1]);
if (trad==0) rad=at_rad [atoms[i].type-1]/2; // Normal radii
else rad=at_radv[atoms[i].type-1]; // Van-der-Waals radii
}
}
Sphere(rad,sl,sl); // Draw the Atom
if ( mrkatm ) {
if ( (i==ap1) || (i==ap2) || (i==ap3) || (i==ap4) ) {
rad+=0.02f;
if ( i == ap1 ) drawmark(White, 0,rad,sl);
if ( i == ap2 ) drawmark(Red ,30,rad,sl);
if ( i == ap3 ) drawmark(Green,60,rad,sl);
if ( i == ap4 ) drawmark(Blue ,90,rad,sl);
}
}
glPopMatrix ();
}
// draw atomic numbers
if (atnum) {
revmat(roma,romb); // find matrix inverse to the rotation matrix
int h = int(double(at_rad[0])*double(w())/(maxcor*3)); // font should be apprx equal to the hydrogen atom radius
if ( h<8 ) h=8; // but not too small
gl_font(1, h);
glMaterialfv (GL_FRONT, GL_DIFFUSE, Blue); // blue material for label
for ( int i=0; i<atoms.size(); i++ ) {
float rad;
if (trad==0) rad=at_rad [atoms[i].type-1]/2; // Normal radii
else rad=at_radv[atoms[i].type-1]; // Van-der-Waals radii
float v[3] = {0.0f, 0.0f, 1.0f}; // create normal
rotvec (v, romb); // perpendicular to the screen
glNormal3fv(v); // the normal is perpendicular in screen direction
float c[3] = {0.0f, 0.0f, rad+0.1f}; // coordinates of the label
if ( mrkatm ) {
if ( i == ap1 ) { print_label(1,i,c); c[1]-=0.25; }
if ( i == ap2 ) { print_label(2,i,c); c[1]-=0.25; }
if ( i == ap3 ) { print_label(3,i,c); c[1]-=0.25; }
if ( i == ap4 ) { print_label(4,i,c); c[1]-=0.25; }
if ( (i!=ap1) && (i!=ap2) && (i!=ap3) && (i!=ap4) ) {
print_label(0,i,c);
}
} else {
print_label(0,i,c);
}
}
}
// Draw Bonds
if (trad==0) { // if not CPK model, which doesn't need bonds
if(dline) { // wire bonds
float v1[4],v2[4],v3[4],v4;
glGetMaterialfv(GL_FRONT, GL_AMBIENT, v1); // get default material
glGetMaterialfv(GL_FRONT, GL_DIFFUSE, v2);
glGetMaterialfv(GL_FRONT, GL_SPECULAR, v3);
glGetMaterialfv(GL_FRONT, GL_SHININESS, &v4);
float bndmat[4]= { bondmat[0]*4, bondmat[1]*4, bondmat[2]*4, 1.0 };
glMaterialfv(GL_FRONT, GL_AMBIENT, bndmat); // set extra bond material
glMaterialfv(GL_FRONT, GL_DIFFUSE, bndmat);
glMaterialfv(GL_FRONT, GL_SPECULAR, bndmat);
glMaterialf (GL_FRONT, GL_SHININESS, 51.2f);
glBegin(GL_LINES);
for ( int i=0; i<bonds.size(); i++) {
if (select_mode == GL_SELECT) glLoadName(bn_level+i);
glNormal3d (ax1,ay1,az1); glVertex3d (ax1,ay1,az1);
glNormal3d (ax2,ay2,az2); glVertex3d (ax2,ay2,az2);
}
glEnd();
glMaterialfv(GL_FRONT, GL_AMBIENT , v1); // return back
glMaterialfv(GL_FRONT, GL_DIFFUSE , v2); // default material
glMaterialfv(GL_FRONT, GL_SPECULAR , v3);
glMaterialf (GL_FRONT, GL_SHININESS, v4);
} else { // normal cylindrical bonds
int i = 0;
int sl = present ? slpresentcylinder : slnormalcylinder;
for ( vector <struct bond>::iterator it = bonds.begin();
it != bonds.end(); it++, i++) {
double o = (*it).o;
double l = len(ax1,ay1,az1,ax2,ay2,az2);
double a = 0.0;
if (l>0) a=acos((ay2-ay1)/l)/PI*180.0;
if(clrbnd) { // twocolor bonds
glPushMatrix ();
if (select_mode == GL_SELECT) glLoadName(bn_level+i);
glTranslatef(ax1,ay1,az1);
glRotatef(a, az2-az1, 0.0, ax1-ax2);
glMaterialfv (GL_FRONT, GL_DIFFUSE, at_def[atoms[(*it).a-1].type-1]);
Cylinder(o,o,l/2,sl);
glPopMatrix ();
glPushMatrix ();
if (select_mode == GL_SELECT) glLoadName(bn_level+i);
glTranslatef((ax1+ax2)/2,(ay1+ay2)/2,(az1+az2)/2);
glRotatef(a, az2-az1, 0.0, ax1-ax2);
glMaterialfv (GL_FRONT, GL_DIFFUSE, at_def[atoms[(*it).b-1].type-1]);
Cylinder(o,o,l/2,sl);
glPopMatrix ();
} else { // normal monocolor bonds
glPushMatrix ();
if (select_mode == GL_SELECT) glLoadName(bn_level+i);
glTranslatef(ax1,ay1,az1);
glRotatef(a, az2-az1, 0.0, ax1-ax2);
glMaterialfv(GL_FRONT, GL_DIFFUSE, bondmat);
Cylinder(o,o,l,sl);
glPopMatrix ();
}
}
}
}
// Draw XYZ Axes
if ( DrawAxes == 1 ) {
float v2[4];
glGetMaterialfv(GL_FRONT, GL_DIFFUSE, v2); // get default material
double l=maxcor*0.75;
double d=maxcor*0.01;
int sl = present ? slpresentcylinder : slnormalcylinder;
glMaterialfv ( GL_FRONT, GL_DIFFUSE , Black ); // Origin
glTranslatef(0.0, 0.0, 0.0);
Sphere(d, sl, sl);
glMaterialfv ( GL_FRONT, GL_DIFFUSE , Red ); // X axis
glPushMatrix ();
glTranslatef(0.0, 0.0, 0.0); // origin
glRotatef(270.0, 0, 0, 1);
Cylinder( d, d, l, sl );
glPopMatrix ();
glPushMatrix ();
glTranslatef(l, 0, 0);
glRotatef(270.0, 0, 0, 1);
Cylinder( d*2, 0.001, d*6 , sl );
Cylinder( d*2, d , -d/5 , sl );
glPopMatrix ();
glMaterialfv ( GL_FRONT, GL_DIFFUSE , Green ); // Y axis
glPushMatrix ();
glTranslatef(0.0, 0.0, 0.0); // origin
Cylinder( d, d, l, sl );
glPopMatrix ();
glPushMatrix ();
glTranslatef(0, l, 0);
Cylinder( d*2, 0.001, d*6 , sl );
Cylinder( d*2, d , -d/5 , sl );
glPopMatrix ();
glMaterialfv ( GL_FRONT, GL_DIFFUSE , Blue ); // Z axis
glPushMatrix ();
glTranslatef(0.0, 0.0, 0.0); // origin
glRotatef(90.0, 1, 0, 0);
Cylinder( d, d, l, sl );
glPopMatrix ();
glPushMatrix ();
glTranslatef(0, 0, l);
glRotatef(90.0, 1, 0, 0);
Cylinder( d*2, 0.001, d*6 , sl );
Cylinder( d*2, d , -d/5 , sl );
glPopMatrix ();
glMaterialfv(GL_FRONT, GL_DIFFUSE , v2);
}
// Draw Gradient Vectors
if(viewgr==1 && gradss==1){
glMaterialfv(GL_FRONT, GL_DIFFUSE, irmat);
int i;
double vg=0;
for(i=0;i<atoms.size();i++){ // find largest gradient
double l = len(0,0,0,grads[i].x,grads[i].y,grads[i].z);
if(l>vg) vg=l;
}
vg=3/vg;
for(i=0;i<atoms.size();i++) {
if (select_mode == GL_SELECT) glLoadName(gr_level+i);
double l = len(0,0,0,grads[i].x,grads[i].y,grads[i].z);
if (l>0) { // length of the vector shouldn't be zero
glPushMatrix ();
glTranslatef(atoms[i].x, atoms[i].y, atoms[i].z);
l*=vg;
double a=acos((vg*grads[i].y)/l)/PI*180.0;
glRotatef(a, vg*grads[i].z, 0, -vg*grads[i].x);
int sl = present ? slpresentcylinder : slnormalcylinder;
glPushMatrix();
glScaled(1,l,1);
Cylinder(0.05,0.05,1,sl);
glPopMatrix ();
glPopMatrix ();
glPushMatrix ();
glTranslatef(atoms[i].x+vg*grads[i].x,
atoms[i].y+vg*grads[i].y,
atoms[i].z+vg*grads[i].z);
glRotatef(a, vg*grads[i].z, 0, -vg*grads[i].x);
Cylinder( 0.1, 0.001, 0.3 , sl ); // arrow
Cylinder( 0.1, 0.05 , -0.01, sl );
glPopMatrix ();
}
}
}
glPopMatrix();
glPopMatrix();
}
// HANDLE WINDOW RESIZING
// If window reshaped, need to readjust viewport/ortho
void resize(int X,int Y,int ww,int hh) {
if (ww>hh) ww=hh; else if (ww<hh) hh=ww;
Fl_Gl_Window::resize(X,Y,ww,hh);
glViewport (0, 0, ww, hh);
SetOrtho();
redraw();
}
int mouseX, mouseY, mouseS, mouseXo, mouseYo, mouseSo;
#define MAXSELECT 256
unsigned int *selectBuf;
int handle (int event) {
switch(event) {
case FL_PUSH:
// ... mouse down event ...
// ... position in Fl::event_x() and Fl::event_y()
if ( Fl::event_button() == 1) {
mouseX = Fl::event_x();
mouseY = Fl::event_y();
mouseS = event;
cursor(FL_CURSOR_CROSS);
}
return 1;
case FL_DRAG:
// ... mouse moved while down event ...
if ( Fl::event_button() == 1) {
mouseXo = mouseX; mouseYo = mouseY; mouseSo = mouseS;
mouseX = Fl::event_x(); mouseY = Fl::event_y(); mouseS = event;
rotxyz( 0,1,0, (mouseXo-mouseX), roma );
rotxyz( 1,0,0, (mouseYo-mouseY), roma );
redraw();
}
return 1;
case FL_RELEASE: {
// ... mouse up event ...
cursor(FL_CURSOR_DEFAULT);
selectBuf = new unsigned int[MAXSELECT];
glSelectBuffer(MAXSELECT, selectBuf);
glRenderMode(GL_SELECT);
glInitNames();
glPushName((GLuint)~0);
x_select = mouseX;
y_select = mouseY;
select_mode = GL_SELECT;
glPushMatrix();
SetOrtho();
draw();
glPopMatrix();
int hits = glRenderMode(GL_RENDER);
if (hits <= 0) {
select_hit = -1;
} else {
int o,i;
for( o=i=0; i<hits; i++ )
if (selectBuf[o*4+1] > selectBuf[i*4+1]) o=i;
select_hit = selectBuf[o*4+3];
}
delete[]selectBuf;
if (select_hit >= gr_level) {
int i = select_hit - gr_level;
double l = len(0,0,0,grads[i].x,grads[i].y,grads[i].z);
char s[64];
sprintf(s,"Gradient #%3d length is %12.6lf\n",i+1, l );
disp->insert_position( buff->length() );
disp->insert(s);
} else if (select_hit >= bn_level) {
int i = select_hit - bn_level;
ap1=bonds[i].a-1; ap2=bonds[i].b-1;
char s[64];
sprintf(s,"Bond (%3d,%3d) = %12.6lf\n",ap1+1,ap2+1, getBondLen(atoms[ap1],atoms[ap2]) );
disp->insert_position( buff->length() );
disp->insert(s);
} else if (select_hit >= at_level) {
int i = select_hit - at_level;
ap4=ap3; ap3=ap2; ap2=ap1; ap1=i;
redraw();
}
select_mode = GL_RENDER;
SetOrtho();
}
return 1;
case FL_MOUSEWHEEL :
if (Fl::event_dy()<0) maxcor/=1.05;
if (Fl::event_dy()>0) maxcor*=1.05;
SetOrtho();
redraw();
zoom->value(maxcor);
return 1;
case FL_FOCUS :
case FL_UNFOCUS :
// ... Return 1 if you want keyboard events, 0 otherwise
return 1;
case FL_KEYBOARD:
// ... keypress, key is in Fl::event_key(), ascii in Fl::event_text()
// ... Return 1 if you understand/use the keyboard event, 0 otherwise...
switch (Fl::event_key()) {
case FL_Escape :
win->hide(); // Quit
return 1;
}
return 0;
case FL_SHORTCUT:
// ... shortcut, key is in Fl::event_key(), ascii in Fl::event_text()
// ... Return 1 if you understand/use the shortcut event, 0 otherwise...
return 0;
default:
// pass other events to the base class...
return Fl_Gl_Window::handle(event);
}
}
// Callback: when use picks 'File | Open' from main menu
static void open_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->open_cb_real(w); }
void open_cb_real(Fl_Widget*) {
// Create the file chooser, and show it
Fl_File_Chooser chooser(".", // directory
"XYZ Files (*.{xyz,mol})\tOUT Files (*.{out,log,gms,mop})", // filter
Fl_File_Chooser::SINGLE, // chooser type
"Open File"); // title
chooser.show();
while(chooser.shown()) { Fl::wait(); }
// User hit cancel?
if ( chooser.value() != NULL ) {
atoms.clear();
bonds.clear();
freqs.clear();
grads.clear();
cread = 0;
bread = 0;
strcpy(fname, chooser.value());
if ( ReadInputFile() ) {
char title[256];
strncpy(title, "ViewMol3D :: ", 256);
strcat (title, fname);
win->label(title);
if ( bread == 0 ) CreateBond(mcalc,atoms_ehm); // manual bonding
CalcMaxCor();
zoom->value(maxcor);
InitGL();
redraw();
}
}
}
// Callback: when user picks 'Quit'
static void quit_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->quit_cb_real(w); }
void quit_cb_real(Fl_Widget*) {
win->hide();
}
// Callback: Perspective
static void ren_pers_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->ren_pers_cb_real(w); }
void ren_pers_cb_real(Fl_Widget*) {
persp = 1 - persp;
SetOrtho();
redraw();
}
// Callback: Quality
void RedrawQuality() {
slnormalsphere = int(32.*quality/10.);
slnormalcylinder = int(16.*quality/10.);
slpresentsphere = int(64.*quality/10.);
slpresentcylinder = int(32.*quality/10.);
redraw();
}
static void slider_q_callback(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->slider_q_callback_real(w); }
void slider_q_callback_real(Fl_Widget* o) {
quality = int(((Fl_Slider*)o)->value());
RedrawQuality();
}
Fl_Window * winq;
static void ok_butt_callback(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->ok_butt_callback_real(w); }
void ok_butt_callback_real(Fl_Widget*o) {
winq->hide();
}
static void cn_butt_callback(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->cn_butt_callback_real(w); }
void cn_butt_callback_real(Fl_Widget*o) {
quality = old_quality; // restore quality state
RedrawQuality();
winq->hide();
}
static void ren_qual_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->ren_qual_cb_real(w); }
void ren_qual_cb_real(Fl_Widget*) {
int ww = 300, hh = 100;
int bw = 80 , ds = 10;
winq = new Fl_Window (ww, hh);
winq->set_modal();
Fl_Hor_Slider slider_q ( 0, 25, ww, 30, "Quality:");
slider_q.align(FL_ALIGN_TOP);
slider_q.callback(slider_q_callback,this);
slider_q.value(quality);
slider_q.step(1.0);
slider_q.bounds(1.0, 20.0);
Fl_Button ok_butt(1*ww/4-bw/2, 65, bw, 30, "Ok" ); ok_butt.callback(ok_butt_callback, this);
Fl_Button cn_butt(3*ww/4-bw/2, 65, bw, 30, "Cancel"); cn_butt.callback(cn_butt_callback, this);
old_quality = quality; // save quality state
winq->show();
Fl::run();
winq->remove(slider_q);
winq->remove(ok_butt);
winq->remove(cn_butt);
delete winq;
}
// Callback: Axes
static void ren_axes_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->ren_axes_cb_real(w); }
void ren_axes_cb_real(Fl_Widget*) {
DrawAxes = 1-DrawAxes;
}
// Callback: Reset Zoom
static void ren_rszm_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->ren_rszm_cb_real(w); }
void ren_rszm_cb_real(Fl_Widget*) {
CalcMaxCor();
SetOrtho();
redraw();
}
// Callback: Gradients
static void ren_grad_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->ren_grad_cb_real(w); }
void ren_grad_cb_real(Fl_Widget*) {
viewgr = 1 - viewgr;
redraw();
}
// Callback: Standard Orientation
static void ort_std_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->ort_std_cb_real(w); }
void ort_std_cb_real(Fl_Widget*) {
if ( atoms.size()>0 ) CalcPricipalAxes();
}
// Callback: Z-matrix Orientation
static void ort_zmt_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->ort_zmt_cb_real(w); }
void ort_zmt_cb_real(Fl_Widget*) {
if ( atoms.size()>0 ) ZOrientMolecule( ap1, ap2, ap3 );
}
static void atm_off_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->atm_off_cb_real(w); }
void atm_off_cb_real (Fl_Widget*) { bond = 1-bond ; }
static void atm_cpk_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->atm_cpk_cb_real(w); }
void atm_cpk_cb_real (Fl_Widget*) { trad = 1-trad ; }
static void atm_num_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->atm_num_cb_real(w); }
void atm_num_cb_real (Fl_Widget*) { atnum = 1-atnum; }
static void atm_mar_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->atm_mar_cb_real(w); }
void atm_mar_cb_real (Fl_Widget*) { mrkatm=1-mrkatm; }
static void bnd_off_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->bnd_off_cb_real(w); }
void bnd_off_cb_real (Fl_Widget*) { dline = 1-dline; }
static void bnd_clr_cb(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->bnd_clr_cb_real(w); }
void bnd_clr_cb_real (Fl_Widget*) { clrbnd=1-clrbnd; }
static void slider_x_callback(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->slider_x_callback_real(w); }
void slider_x_callback_real(Fl_Widget* o) {
angle_x = (((Fl_Slider*)o)->value());
rotxyz(0,1,0, old_angle_x-angle_x, roma);
old_angle_x=angle_x;
redraw();
}
static void slider_y_callback(Fl_Widget* w, void* data) { ((MyGlWindow*)data)->slider_y_callback_real(w); }
void slider_y_callback_real(Fl_Widget* o) {
angle_y = (((Fl_Slider*)o)->value());
rotxyz(1,0,0, old_angle_y-angle_y, roma);
old_angle_y=angle_y;
redraw();
}