Flow123d  release_2.2.0-914-gf1a3a4f
vector.cpp
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file vector.cpp
15  * @brief
16  */
17 
18 #include <cmath>
19 #include "mesh/ngh/include/point.h"
22 
23 using namespace mathfce;
24 
25 namespace ngh {
26 
27 int TVector::numberInstance = 0;
28 
29 int TVector::generateId() {
30  return TVector::numberInstance++;
31 }
32 
33 TVector::TVector() {
34  id = generateId();
35 
36  coors[ 0 ] = 0;
37  coors[ 1 ] = 0;
38  coors[ 2 ] = 0;
39 
40  Compute();
41 }
42 
43 TVector::TVector(double xx1, double xx2, double xx3) {
44  id = generateId();
45 
46  coors[ 0 ] = xx1;
47  coors[ 1 ] = xx2;
48  coors[ 2 ] = xx3;
49 
50  Compute();
51 }
52 
53 TVector::TVector(TPoint P1, TPoint P2) {
54  id = generateId();
55 
56  coors[ 0 ] = P2.X() - P1.X();
57  coors[ 1 ] = P2.Y() - P1.Y();
58  coors[ 2 ] = P2.Z() - P1.Z();
59 
60  Compute();
61 }
62 
63 TVector::TVector(const TVector &x)
64 {
65  id = generateId();
66 
67  coors[0]=x.coors[0];
68  coors[1]=x.coors[1];
69  coors[2]=x.coors[2];
70 
71  Compute();
72 }
73 
74 TVector::~TVector() {
75  ;
76 }
77 
78 void TVector::Compute() {
79  CompLength();
80 }
81 
82 void TVector::CompLength() {
83  length = sqrt(pow(this->coors[ 0 ], 2.0) + pow(this->coors[ 1 ], 2.0) + pow(this->coors[ 2 ], 2.0));
84 }
85 
86 TVector& TVector::operator =(const TPoint &P) {
87  this->coors[ 0 ] = P.X();
88  this->coors[ 1 ] = P.Y();
89  this->coors[ 2 ] = P.Z();
90 
91  Compute();
92 
93  return *this;
94 }
95 
96 TVector TVector::operator +(const TVector &V) {
97  TVector res(0, 0, 0);
98 
99  for (int i = 0; i < 3; i++) {
100  res.coors[ i ] = coors[i] + V.coors[ i ];
101  }
102 
103  res.Compute();
104 
105  return res;
106 }
107 
108 TVector TVector::operator +(const TPoint &P) {
109  TVector res(0, 0, 0);
110 
111  for (int i = 0; i < 3; i++) {
112  res.coors[ i ] = coors[i] + P.Get(i + 1);
113  }
114 
115  res.Compute();
116 
117  return res;
118 }
119 
120 TVector TVector::operator -(const TVector &V) {
121  TVector res(0, 0, 0);
122 
123  for (int i = 0; i < 3; i++) {
124  res.coors[ i ] = coors[i] - V.coors[ i ];
125  }
126 
127  res.Compute();
128 
129  return res;
130 }
131 
132 void TVector::SetVector(double xx1, double xx2, double xx3) {
133  coors[ 0 ] = xx1;
134  coors[ 1 ] = xx2;
135  coors[ 2 ] = xx3;
136  Compute();
137 }
138 
139 TVector operator *(const TVector &U, double x) {
140  TVector tmp;
141  tmp.SetVector(U.X1() * x, U.X2() * x, U.X3() * x);
142  return tmp;
143 }
144 
145 TVector operator *(double x, const TVector &U) {
146  TVector tmp;
147  tmp.SetVector(U.X1() * x, U.X2() * x, U.X3() * x);
148  return tmp;
149 }
150 
152  if (!mathfce::IsZero(length)) {
153  return false;
154  } else {
155  return true;
156  }
157 }
158 
159 double TVector::Length() const {
160  return length;
161 }
162 
163 void TVector::Get(double &xx1, double &xx2, double &xx3) const {
164  xx1 = coors[ 0 ];
165  xx2 = coors[ 1 ];
166  xx3 = coors[ 2 ];
167  return;
168 }
169 
170 void TVector::Get(double *U) const {
171  for (int i = 0; i < 3; i++) {
172  U[ i ] = coors[ i ];
173  }
174 
175  return;
176 }
177 
178 double TVector::Get(int i) const {
179  return coors[ i - 1 ];
180 }
181 
182 TVector Cross(const TVector &U, const TVector &V) {
183  double x1, x2, x3;
184  double u1, u2, u3;
185  double v1, v2, v3;
186 
187  U.Get(u1, u2, u3);
188  V.Get(v1, v2, v3);
189 
190  x1 = u2 * v3 - u3 * v2;
191  x2 = u3 * v1 - u1 * v3;
192  x3 = u1 * v2 - u2 * v1;
193 
194  TVector X(x1, x2, x3);
195 
196  return X;
197 }
198 
199 double Dot(const TVector &U, const TVector &V) {
200  double u1, u2, u3;
201  double v1, v2, v3;
202 
203  U.Get(u1, u2, u3);
204  V.Get(v1, v2, v3);
205 
206  double product = u1 * v1 + u2 * v2 + u3 * v3;
207 
208  return product;
209 }
210 
212  for (int i = 0; i < 3; i++) {
213  if ((fabs(coors[ i ]) - fabs(U.coors[ i ])) > epsilon) {
214  return false;
215  }
216  }
217  return true;
218 }
219 
220 double TVector::X1() const {
221  return coors[ 0 ];
222 }
223 
224 double TVector::X2() const {
225  return coors[ 1 ];
226 }
227 
228 double TVector::X3() const {
229  return coors[ 2 ];
230 }
231 
232 bool AreParallel(const TVector &U, const TVector &V) {
233  // if vector W is zero, then these two bisectors are parallel or same
234  TVector W;
235  W = Cross(U, V);
236  if ((W.Length() < epsilon * 1 * U.Length())
237  || (W.Length() < epsilon * 1 * V.Length())) {
238  return true;
239  } else {
240  return false;
241  }
242 }
243 
244 bool ArePerpendicular(const TVector &U, const TVector &V) {
245  double x = Dot(U, V) / (U.Length() * V.Length());
246  return IsZero(x);
247 }
248 
249 
250 } // namespace ngh
double coors[3]
Definition: myvector.h:32
#define Length(wts)
Definition: quad.c:31
Definition: abscissa.h:25
double Get(int) const
Definition: point.cpp:123
void Compute()
Definition: vector.cpp:78
void Get(double &, double &, double &) const
Definition: vector.cpp:163
double Y() const
Definition: point.cpp:115
bool IsZero(double)
Definition: mathfce.cpp:22
void SetVector(double, double, double)
Definition: vector.cpp:132
double X2() const
Definition: vector.cpp:224
bool operator==(const Null &, const Null &)
double Length() const
Definition: vector.cpp:159
double X3() const
Definition: vector.cpp:228
bool AreParallel(const TVector &, const TVector &)
Definition: vector.cpp:232
double X() const
Definition: point.cpp:111
double Z() const
Definition: point.cpp:119
double Dot(const TVector &, const TVector &)
Definition: vector.cpp:199
const double epsilon
Definition: mathfce.h:23
bool ArePerpendicular(const TVector &, const TVector &)
Definition: vector.cpp:244
TVector operator*(double x, const TVector &U)
Definition: vector.cpp:145
TVector Cross(const TVector &, const TVector &)
Definition: vector.cpp:182
double X1() const
Definition: vector.cpp:220