1 /*****************************************************************************
2 * *
3 * Elmer, A Finite Element Software for Multiphysical Problems *
4 * *
5 * Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *
6 * *
7 * This program is free software; you can redistribute it and/or *
8 * modify it under the terms of the GNU General Public License *
9 * as published by the Free Software Foundation; either version 2 *
10 * of the License, or (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program (in file fem/GPL-2); if not, write to the *
19 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
20 * Boston, MA 02110-1301, USA. *
21 * *
22 *****************************************************************************/
23
24 /*****************************************************************************
25 * *
26 * ElmerGUI helpers *
27 * *
28 *****************************************************************************
29 * *
30 * Authors: Mikko Lyly, Juha Ruokolainen and Peter R�back *
31 * Email: Juha.Ruokolainen@csc.fi *
32 * Web: http://www.csc.fi/elmer *
33 * Address: CSC - IT Center for Science Ltd. *
34 * Keilaranta 14 *
35 * 02101 Espoo, Finland *
36 * *
37 * Original Date: 15 Mar 2008 *
38 * *
39 *****************************************************************************/
40
41 #include <iostream>
42 #include <stdio.h>
43 #include <string.h>
44 #include <math.h>
45 #include "helpers.h"
46 #include <QMatrix4x4>
47
Helpers()48 Helpers :: Helpers()
49 {
50 }
51
~Helpers()52 Helpers :: ~Helpers()
53 {
54 }
55
56 //====================================================================
57 // Normalize
58 //====================================================================
59
normalize(qreal * a)60 void Helpers::normalize(qreal *a)
61 {
62 double b;
63
64 b = vlen(a);
65 a[0] /= b;
66 a[1] /= b;
67 a[2] /= b;
68 }
69
70 //====================================================================
71 // Length
72 //====================================================================
73
vlen(qreal * a)74 qreal Helpers::vlen(qreal *a)
75 {
76 return sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);
77 }
78
79 //====================================================================
80 // Cross product
81 //====================================================================
82
crossProduct(qreal * a,qreal * b,qreal * c)83 void Helpers::crossProduct(qreal *a, qreal *b, qreal *c)
84 {
85 c[0] = a[1]*b[2] - a[2]*b[1];
86 c[1] = a[2]*b[0] - a[0]*b[2];
87 c[2] = a[0]*b[1] - a[1]*b[0];
88 }
89
90 //====================================================================
91 // Invert 4x4 matrix (for visualiztion only)
92 //====================================================================
invertMatrix(const qreal * a,qreal * inva)93 void Helpers::invertMatrix(const qreal *a, qreal *inva)
94 {
95 QMatrix4x4 matrix(a);
96
97 bool ok(true);
98
99 QMatrix4x4 inverse(matrix.transposed().inverted(&ok));
100
101 if(!ok) fprintf(stderr, "Error: Singular 4x4 matrix\n");
102
103 for(int i = 0; i < 16; ++i)
104 inva[i] = double(inverse.data()[i]);
105 }
106