t8  UNKNOWN
t8code is a C library to manage a forest of adaptive space-trees of general element classes in parallel.
t8_vec.h
Go to the documentation of this file.
1 /*
2  This file is part of t8code.
3  t8code is a C library to manage a collection (a forest) of multiple
4  connected adaptive space-trees of general element classes in parallel.
5 
6  Copyright (C) 2015 the developers
7 
8  t8code is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12 
13  t8code is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with t8code; if not, write to the Free Software Foundation, Inc.,
20  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 */
22 
27 #ifndef T8_VEC_H
28 #define T8_VEC_H
29 
30 #include <t8.h>
31 
36 static inline double
37 t8_vec_norm (const double vec[3])
38 {
39  double norm = 0;
40 
41  for (int i = 0; i < 3; i++) {
42  norm += vec[i] * vec[i];
43  }
44  return sqrt (norm);
45 }
46 
50 static inline void
51 t8_vec_normalize (double vec[3])
52 {
53  const double inv_norm = 1.0 / t8_vec_norm (vec);
54 
55  for (int i = 0; i < 3; i++) {
56  vec[i] *= inv_norm;
57  }
58 }
59 
64 static inline void
65 t8_vec_copy (const double vec_in[3], double vec_out[3])
66 {
67  for (int i = 0; i < 3; i++) {
68  vec_out[i] = vec_in[i];
69  }
70 }
71 
78 static inline double
79 t8_vec_dist (const double vec_x[3], const double vec_y[3])
80 {
81  double dist = 0;
82 
83  for (int i = 0; i < 3; i++) {
84  dist += SC_SQR (vec_x[i] - vec_y[i]);
85  }
86  return sqrt (dist);
87 }
88 
93 static inline void
94 t8_vec_ax (double vec_x[3], const double alpha)
95 {
96  for (int i = 0; i < 3; i++) {
97  vec_x[i] *= alpha;
98  }
99 }
100 
106 static inline void
107 t8_vec_axy (const double vec_x[3], double vec_y[3], const double alpha)
108 {
109  for (int i = 0; i < 3; i++) {
110  vec_y[i] = vec_x[i] * alpha;
111  }
112 }
113 
122 static inline void
123 t8_vec_axb (const double vec_x[3], double vec_y[3], const double alpha, const double b)
124 {
125  for (int i = 0; i < 3; i++) {
126  vec_y[i] = alpha * vec_x[i] + b;
127  }
128 }
129 
136 static inline void
137 t8_vec_axpy (const double vec_x[3], double vec_y[3], const double alpha)
138 {
139  for (int i = 0; i < 3; i++) {
140  vec_y[i] += alpha * vec_x[i];
141  }
142 }
143 
149 static inline void
150 t8_vec_axpyz (const double vec_x[3], const double vec_y[3], double vec_z[3], const double alpha)
151 {
152  for (int i = 0; i < 3; i++) {
153  vec_z[i] = vec_y[i] + alpha * vec_x[i];
154  }
155 }
156 
162 static inline double
163 t8_vec_dot (const double vec_x[3], const double vec_y[3])
164 {
165  double dot = 0;
166 
167  for (int i = 0; i < 3; i++) {
168  dot += vec_x[i] * vec_y[i];
169  }
170  return dot;
171 }
172 
178 static inline void
179 t8_vec_cross (const double vec_x[3], const double vec_y[3], double cross[3])
180 {
181  for (int i = 0; i < 3; i++) {
182  cross[i] = vec_x[(i + 1) % 3] * vec_y[(i + 2) % 3] - vec_x[(i + 2) % 3] * vec_y[(i + 1) % 3];
183  }
184 }
185 
191 static inline void
192 t8_vec_diff (const double vec_x[3], const double vec_y[3], double diff[3])
193 {
194  for (int i = 0; i < 3; i++) {
195  diff[i] = vec_x[i] - vec_y[i];
196  }
197 }
198 
207 static inline int
208 t8_vec_eq (const double vec_x[3], const double vec_y[3], const double tol)
209 {
210  T8_ASSERT (tol > 0);
211  return fabs (vec_x[0] - vec_y[0]) <= tol && fabs (vec_x[1] - vec_y[1]) <= tol && fabs (vec_x[2] - vec_y[2]) <= tol;
212 }
213 
218 static inline void
219 t8_vec_rescale (double vec[3], const double new_length)
220 {
221  t8_vec_normalize (vec);
222  t8_vec_ax (vec, new_length);
223 }
224 
231 static inline void
232 t8_vec_tri_normal (const double p1[3], const double p2[3], const double p3[3], double normal[3])
233 {
234  double a[3]; /* First triangle side. */
235  double b[3]; /* Second triangle side. */
236 
237  a[0] = p2[0] - p1[0];
238  a[1] = p2[1] - p1[1];
239  a[2] = p2[2] - p1[2];
240 
241  b[0] = p3[0] - p1[0];
242  b[1] = p3[1] - p1[1];
243  b[2] = p3[2] - p1[2];
244 
245  t8_vec_cross (a, b, normal);
246 }
247 
252 static inline void
253 t8_vec_swap (double p1[3], double p2[3])
254 {
255  double tmp;
256  for (int i = 0; i < 3; i++) {
257  tmp = p1[i];
258  p1[i] = p2[i];
259  p2[i] = tmp;
260  }
261 }
262 
263 #endif /* !T8_VEC_H */
This is the administrative header file for t8code.
#define T8_ASSERT(c)
Widely used assertion.
Definition: t8.h:71