Replaced all occurences of str(n)casecmp with gmx_str(n)casecmp.
[gromacs/rigid-bodies.git] / src / gmxlib / 3dview.c
blob68c6dded5b156c3a9e90cd4ee5b91ca7ff8342ce
1 /*
2 *
3 * This source code is part of
4 *
5 * G R O M A C S
6 *
7 * GROningen MAchine for Chemical Simulations
8 *
9 * VERSION 3.2.0
10 * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
11 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
12 * Copyright (c) 2001-2004, The GROMACS development team,
13 * check out http://www.gromacs.org for more information.
15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation; either version 2
18 * of the License, or (at your option) any later version.
20 * If you want to redistribute modifications, please consider that
21 * scientific software is very special. Version control is crucial -
22 * bugs must be traceable. We will be happy to consider code for
23 * inclusion in the official distribution, but derived work must not
24 * be called official GROMACS. Details are found in the README & COPYING
25 * files - if they are missing, get the official version at www.gromacs.org.
27 * To help us fund GROMACS development, we humbly ask that you cite
28 * the papers on the package - you can find them in the top README file.
30 * For more info, check our website at http://www.gromacs.org
32 * And Hey:
33 * GROningen Mixture of Alchemy and Childrens' Stories
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #include <math.h>
40 #include "sysstuff.h"
41 #include "smalloc.h"
42 #include "macros.h"
43 #include "physics.h"
44 #include "3dview.h"
45 #include "pbc.h"
46 #include "vec.h"
48 #define N 4
50 void m4_op(mat4 m,rvec x,vec4 v)
52 int i;
54 for(i=0; (i<N); i++)
55 v[i]=m[XX][i]*x[XX]+m[YY][i]*x[YY]+m[ZZ][i]*x[ZZ]+m[WW][i];
58 void unity_m4(mat4 m)
60 int i,j;
62 for(i=0; (i<N); i++)
63 for(j=0; (j<N); j++)
64 if (i==j)
65 m[i][j]=1.0;
66 else
67 m[i][j]=0.0;
70 void print_m4(FILE *fp,const char *s,mat4 A)
72 int i,j;
74 if (fp) {
75 fprintf(fp,"%s: ",s);
76 for (i=0; i<N; i++) {
77 fprintf(fp,"\t");
78 for (j=0; j<N; j++)
79 fprintf(fp,"%10.5f",A[i][j]);
80 fprintf(fp,"\n");
85 void print_v4(FILE *fp,char *s,int dim,real *a)
87 int j;
89 if (fp) {
90 fprintf(fp,"%s: ",s);
91 for (j=0; j<dim; j++)
92 fprintf(fp,"%10.5f",a[j]);
93 fprintf(fp,"\n");
97 void mult_matrix(mat4 A, mat4 B, mat4 C)
99 int i,j,k;
101 for (i=0; i<N; i++)
102 for (j=0; j<N; j++) {
103 A[i][j]=0;
104 for(k=0; (k<N); k++)
105 A[i][j]+=B[i][k]*C[k][j];
109 void rotate(int axis, real angle, mat4 A)
111 unity_m4(A);
113 switch (axis) {
114 case XX:
115 A[YY][YY] = cos(angle);
116 A[YY][ZZ] = -sin(angle);
117 A[ZZ][YY] = sin(angle);
118 A[ZZ][ZZ] = cos(angle);
119 break;
120 case YY:
121 A[XX][XX] = cos(angle);
122 A[XX][ZZ] = sin(angle);
123 A[ZZ][XX] = -sin(angle);
124 A[ZZ][ZZ] = cos(angle);
125 break;
126 case ZZ:
127 A[XX][XX] = cos(angle);
128 A[XX][YY] = -sin(angle);
129 A[YY][XX] = sin(angle);
130 A[YY][YY] = cos(angle);
131 break;
132 default:
133 gmx_fatal(FARGS,"Error: invalid axis: %d",axis);
137 void translate(real tx, real ty, real tz, mat4 A)
139 unity_m4(A);
140 A[3][XX] = tx;
141 A[3][YY] = ty;
142 A[3][ZZ] = tz;
145 static void set_scale(t_3dview *view,real sx, real sy)
147 view->sc_x=sx;
148 view->sc_y=sy;
151 void calculate_view(t_3dview *view)
153 #define SMALL 1e-6
154 mat4 To,Te,T1,T2,T3,T4,T5,N1,D1,D2,D3,D4,D5;
155 real dx,dy,dz,l,r;
157 /* eye center */
158 dx=view->eye[XX];
159 dy=view->eye[YY];
160 dz=view->eye[ZZ];
161 l = sqrt(dx*dx+dy*dy+dz*dz);
162 r = sqrt(dx*dx+dy*dy);
163 #ifdef DEBUG
164 print_v4(debug,"eye",N,view->eye);
165 printf("del: %10.5f%10.5f%10.5f l: %10.5f, r: %10.5f\n",dx,dy,dz,l,r);
166 #endif
167 if (l < SMALL)
168 gmx_fatal(FARGS,"Error: Zero Length Vector - No View Specified");
169 translate((real)(-view->origin[XX]),
170 (real)(-view->origin[YY]),(real)(-view->origin[ZZ]),To);
171 translate((real)(-view->eye[XX]),
172 (real)(-view->eye[YY]),(real)(-view->eye[ZZ]),Te);
174 unity_m4(T2);
175 T2[YY][YY]=0, T2[YY][ZZ]=-1, T2[ZZ][YY]=1, T2[ZZ][ZZ]=0;
177 unity_m4(T3);
178 if (r > 0)
179 T3[XX][XX]=-dy/r, T3[XX][ZZ]=dx/r, T3[ZZ][XX]=-dx/r, T3[ZZ][ZZ]=-dy/r;
181 unity_m4(T4);
182 T4[YY][YY]=r/l, T4[YY][ZZ]=dz/l, T4[ZZ][YY]=-dz/l, T4[ZZ][ZZ]=r/l;
184 unity_m4(T5);
185 T5[ZZ][ZZ]=-1;
187 unity_m4(N1);
188 /* N1[XX][XX]=4,N1[YY][YY]=4; */
190 mult_matrix(T1,To,view->Rot);
191 mult_matrix(D1,Te,T2);
192 mult_matrix(D2,T3,T4);
193 mult_matrix(D3,T5,N1);
194 mult_matrix(D4,T1,D1);
195 mult_matrix(D5,D2,D3);
197 mult_matrix(view->proj,D4,D5);
199 #ifdef DEBUG
200 print_m4(debug,"T1",T1);
201 print_m4(debug,"T2",T2);
202 print_m4(debug,"T3",T3);
203 print_m4(debug,"T4",T4);
204 print_m4(debug,"T5",T5);
205 print_m4(debug,"N1",N1);
206 print_m4(debug,"Rot",view->Rot);
207 print_m4(debug,"Proj",view->proj);
208 #endif
211 bool zoom_3d(t_3dview *view,real fac)
213 real dr;
214 real bm,dr1,dr2;
215 int i;
217 dr2=0;
218 for(i=0; (i<DIM); i++) {
219 dr=view->eye[i];
220 dr2+=dr*dr;
222 dr1=sqrt(dr2);
223 if (fac < 1) {
224 bm=max(norm(view->box[XX]),max(norm(view->box[YY]),norm(view->box[ZZ])));
225 if (dr1*fac < 1.1*bm) /* Don't come to close */
226 return FALSE;
229 for(i=0; (i<DIM); i++)
230 view->eye[i]*=fac;
231 calculate_view(view);
232 return TRUE;
235 void init_rotate_3d(t_3dview *view)
237 real rot=DEG2RAD*15;
238 int i;
240 for(i=0; (i<DIM); i++) {
241 rotate(i, rot ,view->RotP[i]);
242 rotate(i,(real)(-rot),view->RotM[i]);
243 #ifdef DEBUG
244 print_m4(debug,"RotP",view->RotP[i]);
245 print_m4(debug,"RotM",view->RotM[i]);
246 #endif
251 void rotate_3d(t_3dview *view,int axis,bool bPositive)
253 int i,j;
254 mat4 m4;
256 if (bPositive)
257 mult_matrix(m4,view->Rot,view->RotP[axis]);
258 else
259 mult_matrix(m4,view->Rot,view->RotM[axis]);
260 for(i=0; (i<N); i++)
261 for(j=0; (j<N); j++)
262 view->Rot[i][j]=m4[i][j];
264 calculate_view(view);
267 void translate_view(t_3dview *view,int axis,bool bPositive)
269 #ifdef DEBUG
270 printf("Translate called\n");
271 #endif
272 if (bPositive)
273 view->origin[axis]+=view->box[axis][axis]/8;
274 else
275 view->origin[axis]-=view->box[axis][axis]/8;
276 calculate_view(view);
279 void reset_view(t_3dview *view)
281 int i;
283 #ifdef DEBUG
284 printf("Reset view called\n");
285 #endif
286 set_scale(view,4.0,4.0);
287 clear_rvec(view->eye);
288 calc_box_center(view->ecenter,view->box,view->origin);
289 view->eye[ZZ]=3.0*max(view->box[XX][XX],view->box[YY][YY]);
290 zoom_3d(view,1.0);
291 view->eye[WW]=view->origin[WW]=0.0;
293 /* Initiate the matrix */
294 unity_m4(view->Rot);
295 calculate_view(view);
297 init_rotate_3d(view);
300 t_3dview *init_view(matrix box)
302 t_3dview *view;
303 int i,j;
305 snew(view,1);
307 /* Copy parameters into variables */
308 for(i=0; (i<DIM); i++)
309 for(j=0; (j<DIM); j++)
310 view->box[i][j]=box[i][j];
312 view->ecenter = ecenterDEF;
314 reset_view(view);
316 return view;