fermisurfer  2.0.0
fermisurfer
basic_math.cpp
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 Copyright (c) 2014 Mitsuaki KAWAMURA
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 THE SOFTWARE.
23 */
24 /**@file
25 @brief Mathematical operations used in various step
26 */
27 #if defined(HAVE_CONFIG_H)
28 #include <config.h>
29 #endif
30 #if defined(HAVE_GL_GL_H)
31 #include <GL/gl.h>
32 #elif defined(HAVE_OPENGL_GL_H)
33 #include <OpenGL/gl.h>
34 #endif
35 
36 #include <cmath>
37 
38 #if defined(_OPENMP)
39 #include <omp.h>
40 #endif
41 
42 /**
43  @brief Work as Modulo function of fortran
44  @return Modulated value
45 */
46 int modulo(
47  int i, //!< [in]
48  int n //!< [in]
49 ) {
50  int j;
51  j = (i + 100 * n) % n;
52  return j;
53 }/*modulo(int i, int n)*/
54 /**
55  @brief Solve linear system
56  @return Determinant
57 */
58 GLfloat solve3(
59  GLfloat a[3][3], //!< [in] Matix
60  GLfloat b[3] //!< [in,out] Right hand side vector
61 )
62 {
63  int i;
64  GLfloat det, c[3];
65  /**/
66  det = a[0][0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1])
67  + a[0][1] * (a[1][2] * a[2][0] - a[1][0] * a[2][2])
68  + a[0][2] * (a[1][0] * a[2][1] - a[1][1] * a[2][0]);
69  /**/
70  c[0] = b[0] * (a[1][1] * a[2][2] - a[1][2] * a[2][1])
71  + b[1] * (a[0][2] * a[2][1] - a[0][1] * a[2][2])
72  + b[2] * (a[0][1] * a[1][2] - a[0][2] * a[1][1]);
73  /**/
74  c[1] = b[0] * (a[1][2] * a[2][0] - a[1][0] * a[2][2])
75  + b[1] * (a[0][0] * a[2][2] - a[0][2] * a[2][0])
76  + b[2] * (a[0][2] * a[1][0] - a[0][0] * a[1][2]);
77  /**/
78  c[2] = b[0] * (a[1][0] * a[2][1] - a[1][1] * a[2][0])
79  + b[1] * (a[0][1] * a[2][0] - a[0][0] * a[2][1])
80  + b[2] * (a[0][0] * a[1][1] - a[0][1] * a[1][0]);
81  /**/
82  for (i = 0; i<3; ++i) b[i] = c[i] / det;
83  return det;
84 }/*GLfloat solve3*/
85 /**
86  @brief Simple sort
87 */
88 void eigsort(
89  int n, //!< [in] the number of components
90  GLfloat *key, //!< [in] Variables to be sorted [n].
91  int *swap //!< [out] Order of index (sorted)
92 )
93 {
94  int i, j, k;
95 
96  for (i = 0; i < n; ++i) swap[i] = i;
97 
98  for (i = 0; i < n - 1; ++i) {
99  for (j = i + 1; j < n; ++j) {
100  if (key[swap[j]] < key[swap[i]]) {
101  /*
102  Swap
103  */
104  k = swap[j];
105  swap[j] = swap[i];
106  swap[i] = k;
107  }/*if (sortee[j][0] < sortee[i][0])*/
108  }/*for (j = i + 1; j < n; ++j)*/
109  }/*for (i = 0; i < n - 1; ++i)*/
110 }/*eigsort*/
111 /**
112  @brief Calculate normal vector from corners of triangle
113 */
115  GLfloat in1[3], //!< [in] Corner 1
116  GLfloat in2[3], //!< [in] Corner 2
117  GLfloat in3[3], //!< [in] Corner 3
118  GLfloat out[3] //!< [out] The normal vector
119 )
120 {
121  int i;
122  GLfloat norm;
123  out[0] = in1[1] * in2[2] - in1[2] * in2[1]
124  + in2[1] * in3[2] - in2[2] * in3[1]
125  + in3[1] * in1[2] - in3[2] * in1[1];
126  out[1] = in1[2] * in2[0] - in1[0] * in2[2]
127  + in2[2] * in3[0] - in2[0] * in3[2]
128  + in3[2] * in1[0] - in3[0] * in1[2];
129  out[2] = in1[0] * in2[1] - in1[1] * in2[0]
130  + in2[0] * in3[1] - in2[1] * in3[0]
131  + in3[0] * in1[1] - in3[1] * in1[0];
132  norm = sqrtf(out[0] * out[0] + out[1] * out[1] + out[2] * out[2]);
133  for (i = 0; i<3; i++) out[i] = out[i] / norm;
134 } /* normal_vec */
135 /**
136  @brief OpenMP wrapper, get the number of threads
137  @return the number of threads
138 */
139 int get_thread() {
140  int ithread;
141 #if defined(_OPENMP)
142  ithread = omp_get_thread_num();
143 #else
144  ithread = 0;
145 #endif
146  return ithread;
147 }
get_thread
int get_thread()
OpenMP wrapper, get the number of threads.
Definition: basic_math.cpp:139
eigsort
void eigsort(int n, GLfloat *key, int *swap)
Simple sort.
Definition: basic_math.cpp:88
solve3
GLfloat solve3(GLfloat a[3][3], GLfloat b[3])
Solve linear system.
Definition: basic_math.cpp:58
normal_vec
void normal_vec(GLfloat in1[3], GLfloat in2[3], GLfloat in3[3], GLfloat out[3])
Calculate normal vector from corners of triangle.
Definition: basic_math.cpp:114
modulo
int modulo(int i, int n)
Work as Modulo function of fortran.
Definition: basic_math.cpp:46