b2api
B2000++ API Reference Manual, VERSION 4.6
 
Loading...
Searching...
No Matches
b2vec3.H
Go to the documentation of this file.
1//------------------------------------------------------------------------
2// b2tensor_calculus.H --
3//
4//
5// written by Thomas Ludwig
6//
7// Copyright (c) 2017
8// SMR Engineering & Development SA
9// 2502 Bienne, Switzerland
10//
11// All Rights Reserved. Proprietary source code. The contents of
12// this file may not be disclosed to third parties, copied or
13// duplicated in any form, in whole or in part, without the prior
14// written permission of SMR.
15//------------------------------------------------------------------------
16
17#ifndef _B2_VEC3_H_
18#define _B2_VEC3_H_
19
20#include <algorithm>
21#include <cmath>
22#include <complex>
23#include <iostream>
24#include <limits>
25
26#include "b2csda.H"
27
33namespace b2000 {
34
35template <typename T>
36struct Vec3 {
37 T d[3];
38
39 Vec3() { std::fill_n(d, 3, T(0)); }
40
41 Vec3(const T d_[3]) { std::copy(d_, d_ + 3, d); }
42
43 Vec3(const T d1, const T d2, const T d3) {
44 d[0] = d1;
45 d[1] = d2;
46 d[2] = d3;
47 }
48
49 const T& operator[](const int i) const { return d[i]; }
50
51 T& operator[](const int i) { return d[i]; }
52
53 const T* data() const { return d; }
54
55 T* data() { return d; }
56
57 inline T norm() const { return std::sqrt(d[0] * d[0] + d[1] * d[1] + d[2] * d[2]); }
58
59 inline T normalise() {
60 const T v = norm();
61 if (v != 0) {
62 const T f = 1. / v;
63 for (int i = 0; i != 3; ++i) { d[i] *= f; }
64 }
65 return v;
66 }
67
68 void set_zero() { std::fill_n(d, 3, T(0)); }
69
70 Vec3& assign(const Vec3& o) {
71 std::copy(o.d, o.d + 3, d);
72 return *this;
73 }
74
75 Vec3& assign(const T o[3]) {
76 std::copy(o, o + 3, d);
77 return *this;
78 }
79
80 Vec3& operator=(const Vec3& o) {
81 std::copy(o.d, o.d + 3, d);
82 return *this;
83 }
84
85 Vec3& operator+=(const Vec3& o) {
86 for (int i = 0; i != 3; ++i) { d[i] += o.d[i]; }
87 return *this;
88 }
89
90 Vec3& operator-=(const Vec3& o) {
91 for (int i = 0; i != 3; ++i) { d[i] -= o.d[i]; }
92 return *this;
93 }
94
95 Vec3& operator*=(const T f) {
96 for (int i = 0; i != 3; ++i) { d[i] *= f; }
97 return *this;
98 }
99
100 Vec3& operator/=(const T f) {
101 for (int i = 0; i != 3; ++i) { d[i] /= f; }
102 return *this;
103 }
104
105 bool operator==(const Vec3& o) const { return (d[0] == o[0] && d[1] == o[1] && d[2] == o[2]); }
106
107 bool operator!=(const Vec3& o) const { return (d[0] != o[0] || d[1] != o[1] || d[2] != o[2]); }
108};
109
110template <typename T>
111inline std::ostream& operator<<(std::ostream& o, const Vec3<T>& v) {
112 o << "[" << v[0] << "," << v[1] << "," << v[2] << "]";
113 return o;
114}
115
116template <typename T>
117inline Vec3<T> operator+(const Vec3<T>& v1, const Vec3<T>& v2) {
118 return Vec3<T>(v1[0] + v2[0], v1[1] + v2[1], v1[2] + v2[2]);
119}
120
121template <typename T>
122inline Vec3<T> operator-(const Vec3<T>& v1) {
123 return Vec3<T>(-v1[0], -v1[1], -v1[2]);
124}
125
126template <typename T>
127inline Vec3<T> operator-(const Vec3<T>& v1, const Vec3<T>& v2) {
128 return Vec3<T>(v1[0] - v2[0], v1[1] - v2[1], v1[2] - v2[2]);
129}
130
131template <typename T>
132inline Vec3<T> operator*(const double f, const Vec3<T>& v) {
133 return Vec3<T>(f * v[0], f * v[1], f * v[2]);
134}
135
136template <typename T>
137inline Vec3<T> operator*(const Vec3<T>& v, const double f) {
138 return Vec3<T>(f * v[0], f * v[1], f * v[2]);
139}
140
141template <typename T>
142inline T dot(const Vec3<T>& v1, const Vec3<T>& v2) {
143 return v1[0] * v2[0] + v1[1] * v2[1] + v1[2] * v2[2];
144}
145
146template <typename T>
147inline Vec3<T> outer_product(const Vec3<T>& v1, const Vec3<T>& v2) {
148 return Vec3<T>(
149 v1[1] * v2[2] - v1[2] * v2[1], v1[2] * v2[0] - v1[0] * v2[2],
150 v1[0] * v2[1] - v1[1] * v2[0]);
151}
152
153template <typename T>
154inline Vec3<T> normalised(const Vec3<T>& v) {
155 Vec3<T> vv(v);
156 vv.normalise();
157 return vv;
158}
159
160template <typename T>
161inline T norm(const Vec3<T>& v) {
162 return std::sqrt(dot(v, v));
163}
164
165} // namespace b2000
166
167#endif /* _B2_VEC3_H_ */
csda< T > norm(const csda< T > &a)
Definition b2csda.H:343
bool operator==(const csda< T1 > &a, const csda< T2 > &b)
Comparison of two csda numbers is performed on the real part only.
Definition b2csda.H:226
bool operator!=(const csda< T1 > &a, const csda< T2 > &b)
Comparison of two csda numbers is performed on the real part only.
Definition b2csda.H:244
Contains the base classes for implementing Finite Elements.
Definition b2boundary_condition.H:32