Path: blob/devel/ElmerGUI/Application/src/helpers.cpp
3203 views
/*****************************************************************************1* *2* Elmer, A Finite Element Software for Multiphysical Problems *3* *4* Copyright 1st April 1995 - , CSC - IT Center for Science Ltd., Finland *5* *6* This program is free software; you can redistribute it and/or *7* modify it under the terms of the GNU General Public License *8* as published by the Free Software Foundation; either version 2 *9* of the License, or (at your option) any later version. *10* *11* This program is distributed in the hope that it will be useful, *12* but WITHOUT ANY WARRANTY; without even the implied warranty of *13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *14* GNU General Public License for more details. *15* *16* You should have received a copy of the GNU General Public License *17* along with this program (in file fem/GPL-2); if not, write to the *18* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *19* Boston, MA 02110-1301, USA. *20* *21*****************************************************************************/2223/*****************************************************************************24* *25* ElmerGUI helpers *26* *27*****************************************************************************28* *29* Authors: Mikko Lyly, Juha Ruokolainen and Peter R�back *30* Email: [email protected] *31* Web: http://www.csc.fi/elmer *32* Address: CSC - IT Center for Science Ltd. *33* Keilaranta 14 *34* 02101 Espoo, Finland *35* *36* Original Date: 15 Mar 2008 *37* *38*****************************************************************************/3940#include <iostream>41#include <stdio.h>42#include <string.h>43#include <math.h>44#include "helpers.h"45#include <QMatrix4x4>4647Helpers :: Helpers()48{49}5051Helpers :: ~Helpers()52{53}5455//====================================================================56// Normalize57//====================================================================5859void Helpers::normalize(QREAL_OR_FLOAT *a)60{61double b;6263b = vlen(a);64a[0] /= b;65a[1] /= b;66a[2] /= b;67}6869//====================================================================70// Length71//====================================================================7273QREAL_OR_FLOAT Helpers::vlen(QREAL_OR_FLOAT *a)74{75return sqrt(a[0]*a[0] + a[1]*a[1] + a[2]*a[2]);76}7778//====================================================================79// Cross product80//====================================================================8182void Helpers::crossProduct(QREAL_OR_FLOAT *a, QREAL_OR_FLOAT *b, QREAL_OR_FLOAT *c)83{84c[0] = a[1]*b[2] - a[2]*b[1];85c[1] = a[2]*b[0] - a[0]*b[2];86c[2] = a[0]*b[1] - a[1]*b[0];87}8889//====================================================================90// Invert 4x4 matrix (for visualization only)91//====================================================================92void Helpers::invertMatrix(const QREAL_OR_FLOAT *a, QREAL_OR_FLOAT *inva)93{94QMatrix4x4 matrix(a);9596bool ok(true);9798QMatrix4x4 inverse(matrix.transposed().inverted(&ok));99100if(!ok) fprintf(stderr, "Error: Singular 4x4 matrix\n");101102for(int i = 0; i < 16; ++i)103inva[i] = double(inverse.data()[i]);104}105106107