2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2010,2014, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
37 #include "gromacs/math/3dview.h"
45 #include "gromacs/utility/smalloc.h"
51 #include "gmx_fatal.h"
55 void m4_op(mat4 m, rvec x, vec4 v)
59 for (i = 0; (i < N); i++)
61 v[i] = m[XX][i]*x[XX]+m[YY][i]*x[YY]+m[ZZ][i]*x[ZZ]+m[WW][i];
69 for (i = 0; (i < N); i++)
71 for (j = 0; (j < N); j++)
85 void print_m4(FILE *fp, const char *s, mat4 A)
91 fprintf(fp, "%s: ", s);
92 for (i = 0; i < N; i++)
95 for (j = 0; j < N; j++)
97 fprintf(fp, "%10.5f", A[i][j]);
104 void print_v4(FILE *fp, char *s, int dim, real *a)
110 fprintf(fp, "%s: ", s);
111 for (j = 0; j < dim; j++)
113 fprintf(fp, "%10.5f", a[j]);
119 void mult_matrix(mat4 A, mat4 B, mat4 C)
123 for (i = 0; i < N; i++)
125 for (j = 0; j < N; j++)
128 for (k = 0; (k < N); k++)
130 A[i][j] += B[i][k]*C[k][j];
136 void rotate(int axis, real angle, mat4 A)
143 A[YY][YY] = cos(angle);
144 A[YY][ZZ] = -sin(angle);
145 A[ZZ][YY] = sin(angle);
146 A[ZZ][ZZ] = cos(angle);
149 A[XX][XX] = cos(angle);
150 A[XX][ZZ] = sin(angle);
151 A[ZZ][XX] = -sin(angle);
152 A[ZZ][ZZ] = cos(angle);
155 A[XX][XX] = cos(angle);
156 A[XX][YY] = -sin(angle);
157 A[YY][XX] = sin(angle);
158 A[YY][YY] = cos(angle);
161 gmx_fatal(FARGS, "Error: invalid axis: %d", axis);
165 void translate(real tx, real ty, real tz, mat4 A)
173 static void set_scale(t_3dview *view, real sx, real sy)
179 void calculate_view(t_3dview *view)
182 mat4 To, Te, T1, T2, T3, T4, T5, N1, D1, D2, D3, D4, D5;
183 real dx, dy, dz, l, r;
189 l = sqrt(dx*dx+dy*dy+dz*dz);
190 r = sqrt(dx*dx+dy*dy);
192 print_v4(debug, "eye", N, view->eye);
193 printf("del: %10.5f%10.5f%10.5f l: %10.5f, r: %10.5f\n", dx, dy, dz, l, r);
197 gmx_fatal(FARGS, "Error: Zero Length Vector - No View Specified");
199 translate((real)(-view->origin[XX]),
200 (real)(-view->origin[YY]), (real)(-view->origin[ZZ]), To);
201 translate((real)(-view->eye[XX]),
202 (real)(-view->eye[YY]), (real)(-view->eye[ZZ]), Te);
205 T2[YY][YY] = 0, T2[YY][ZZ] = -1, T2[ZZ][YY] = 1, T2[ZZ][ZZ] = 0;
210 T3[XX][XX] = -dy/r, T3[XX][ZZ] = dx/r, T3[ZZ][XX] = -dx/r, T3[ZZ][ZZ] = -dy/r;
214 T4[YY][YY] = r/l, T4[YY][ZZ] = dz/l, T4[ZZ][YY] = -dz/l, T4[ZZ][ZZ] = r/l;
220 /* N1[XX][XX]=4,N1[YY][YY]=4; */
222 mult_matrix(T1, To, view->Rot);
223 mult_matrix(D1, Te, T2);
224 mult_matrix(D2, T3, T4);
225 mult_matrix(D3, T5, N1);
226 mult_matrix(D4, T1, D1);
227 mult_matrix(D5, D2, D3);
229 mult_matrix(view->proj, D4, D5);
232 print_m4(debug, "T1", T1);
233 print_m4(debug, "T2", T2);
234 print_m4(debug, "T3", T3);
235 print_m4(debug, "T4", T4);
236 print_m4(debug, "T5", T5);
237 print_m4(debug, "N1", N1);
238 print_m4(debug, "Rot", view->Rot);
239 print_m4(debug, "Proj", view->proj);
243 gmx_bool zoom_3d(t_3dview *view, real fac)
250 for (i = 0; (i < DIM); i++)
258 bm = max(norm(view->box[XX]), max(norm(view->box[YY]), norm(view->box[ZZ])));
259 if (dr1*fac < 1.1*bm) /* Don't come to close */
265 for (i = 0; (i < DIM); i++)
269 calculate_view(view);
273 void init_rotate_3d(t_3dview *view)
275 real rot = DEG2RAD*15;
278 for (i = 0; (i < DIM); i++)
280 rotate(i, rot, view->RotP[i]);
281 rotate(i, (real)(-rot), view->RotM[i]);
283 print_m4(debug, "RotP", view->RotP[i]);
284 print_m4(debug, "RotM", view->RotM[i]);
290 void rotate_3d(t_3dview *view, int axis, gmx_bool bPositive)
297 mult_matrix(m4, view->Rot, view->RotP[axis]);
301 mult_matrix(m4, view->Rot, view->RotM[axis]);
303 for (i = 0; (i < N); i++)
305 for (j = 0; (j < N); j++)
307 view->Rot[i][j] = m4[i][j];
311 calculate_view(view);
314 void translate_view(t_3dview *view, int axis, gmx_bool bPositive)
317 printf("Translate called\n");
321 view->origin[axis] += view->box[axis][axis]/8;
325 view->origin[axis] -= view->box[axis][axis]/8;
327 calculate_view(view);
330 void reset_view(t_3dview *view)
335 printf("Reset view called\n");
337 set_scale(view, 4.0, 4.0);
338 clear_rvec(view->eye);
339 calc_box_center(view->ecenter, view->box, view->origin);
340 view->eye[ZZ] = 3.0*max(view->box[XX][XX], view->box[YY][YY]);
342 view->eye[WW] = view->origin[WW] = 0.0;
344 /* Initiate the matrix */
346 calculate_view(view);
348 init_rotate_3d(view);
351 t_3dview *init_view(matrix box)
358 /* Copy parameters into variables */
359 for (i = 0; (i < DIM); i++)
361 for (j = 0; (j < DIM); j++)
363 view->box[i][j] = box[i][j];
367 view->ecenter = ecenterDEF;