Flow123d  release_2.2.0-36-g163dc99
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 
26 
28  return TVector::numberInstance++;
29 }
30 
32  id = generateId();
33 
34  coors[ 0 ] = 0;
35  coors[ 1 ] = 0;
36  coors[ 2 ] = 0;
37 
38  Compute();
39 }
40 
41 TVector::TVector(double xx1, double xx2, double xx3) {
42  id = generateId();
43 
44  coors[ 0 ] = xx1;
45  coors[ 1 ] = xx2;
46  coors[ 2 ] = xx3;
47 
48  Compute();
49 }
50 
52  id = generateId();
53 
54  coors[ 0 ] = P2.X() - P1.X();
55  coors[ 1 ] = P2.Y() - P1.Y();
56  coors[ 2 ] = P2.Z() - P1.Z();
57 
58  Compute();
59 }
60 
62 {
63  id = generateId();
64 
65  coors[0]=x.coors[0];
66  coors[1]=x.coors[1];
67  coors[2]=x.coors[2];
68 
69  Compute();
70 }
71 
73  ;
74 }
75 
77  CompLength();
78 }
79 
81  length = sqrt(pow(this->coors[ 0 ], 2.0) + pow(this->coors[ 1 ], 2.0) + pow(this->coors[ 2 ], 2.0));
82 }
83 
85  this->coors[ 0 ] = P.X();
86  this->coors[ 1 ] = P.Y();
87  this->coors[ 2 ] = P.Z();
88 
89  Compute();
90 
91  return *this;
92 }
93 
95  TVector res(0, 0, 0);
96 
97  for (int i = 0; i < 3; i++) {
98  res.coors[ i ] = coors[i] + V.coors[ i ];
99  }
100 
101  res.Compute();
102 
103  return res;
104 }
105 
107  TVector res(0, 0, 0);
108 
109  for (int i = 0; i < 3; i++) {
110  res.coors[ i ] = coors[i] + P.Get(i + 1);
111  }
112 
113  res.Compute();
114 
115  return res;
116 }
117 
119  TVector res(0, 0, 0);
120 
121  for (int i = 0; i < 3; i++) {
122  res.coors[ i ] = coors[i] - V.coors[ i ];
123  }
124 
125  res.Compute();
126 
127  return res;
128 }
129 
130 void TVector::SetVector(double xx1, double xx2, double xx3) {
131  coors[ 0 ] = xx1;
132  coors[ 1 ] = xx2;
133  coors[ 2 ] = xx3;
134  Compute();
135 }
136 
137 TVector operator *(const TVector &U, double x) {
138  TVector tmp;
139  tmp.SetVector(U.X1() * x, U.X2() * x, U.X3() * x);
140  return tmp;
141 }
142 
143 TVector operator *(double x, const TVector &U) {
144  TVector tmp;
145  tmp.SetVector(U.X1() * x, U.X2() * x, U.X3() * x);
146  return tmp;
147 }
148 
150  if (!mathfce::IsZero(length)) {
151  return false;
152  } else {
153  return true;
154  }
155 }
156 
157 double TVector::Length() const {
158  return length;
159 }
160 
161 void TVector::Get(double &xx1, double &xx2, double &xx3) const {
162  xx1 = coors[ 0 ];
163  xx2 = coors[ 1 ];
164  xx3 = coors[ 2 ];
165  return;
166 }
167 
168 void TVector::Get(double *U) const {
169  for (int i = 0; i < 3; i++) {
170  U[ i ] = coors[ i ];
171  }
172 
173  return;
174 }
175 
176 double TVector::Get(int i) const {
177  return coors[ i - 1 ];
178 }
179 
180 TVector Cross(const TVector &U, const TVector &V) {
181  double x1, x2, x3;
182  double u1, u2, u3;
183  double v1, v2, v3;
184 
185  U.Get(u1, u2, u3);
186  V.Get(v1, v2, v3);
187 
188  x1 = u2 * v3 - u3 * v2;
189  x2 = u3 * v1 - u1 * v3;
190  x3 = u1 * v2 - u2 * v1;
191 
192  TVector X(x1, x2, x3);
193 
194  return X;
195 }
196 
197 double Dot(const TVector &U, const TVector &V) {
198  double u1, u2, u3;
199  double v1, v2, v3;
200 
201  U.Get(u1, u2, u3);
202  V.Get(v1, v2, v3);
203 
204  double product = u1 * v1 + u2 * v2 + u3 * v3;
205 
206  return product;
207 }
208 
210  for (int i = 0; i < 3; i++) {
211  if ((fabs(coors[ i ]) - fabs(U.coors[ i ])) > epsilon) {
212  return false;
213  }
214  }
215  return true;
216 }
217 
218 double TVector::X1() const {
219  return coors[ 0 ];
220 }
221 
222 double TVector::X2() const {
223  return coors[ 1 ];
224 }
225 
226 double TVector::X3() const {
227  return coors[ 2 ];
228 }
229 
230 bool AreParallel(const TVector &U, const TVector &V) {
231  // if vector W is zero, then these two bisectors are parallel or same
232  TVector W;
233  W = Cross(U, V);
234  if ((W.Length() < epsilon * 1 * U.Length())
235  || (W.Length() < epsilon * 1 * V.Length())) {
236  return true;
237  } else {
238  return false;
239  }
240 }
241 
242 bool ArePerpendicular(const TVector &U, const TVector &V) {
243  double x = Dot(U, V) / (U.Length() * V.Length());
244  return IsZero(x);
245 }
TVector Cross(const TVector &U, const TVector &V)
Definition: vector.cpp:180
double X2() const
Definition: vector.cpp:222
TVector & operator=(const TPoint &)
Definition: vector.cpp:84
~TVector()
Definition: vector.cpp:72
double X1() const
Definition: vector.cpp:218
bool operator==(const TVector &)
Definition: vector.cpp:209
bool IsZero(double)
Definition: mathfce.cpp:22
double X3() const
Definition: vector.cpp:226
double X() const
Definition: point.cpp:108
TVector()
Definition: vector.cpp:31
bool AreParallel(const TVector &U, const TVector &V)
Definition: vector.cpp:230
Definition: point.h:27
double Y() const
Definition: point.cpp:112
double Z() const
Definition: point.cpp:116
bool ArePerpendicular(const TVector &U, const TVector &V)
Definition: vector.cpp:242
TVector operator+(const TVector &)
Definition: vector.cpp:94
TVector operator*(const TVector &U, double x)
Definition: vector.cpp:137
void CompLength()
Definition: vector.cpp:80
double Get(int) const
Definition: point.cpp:120
const double epsilon
Definition: mathfce.h:23
double Length() const
Definition: vector.cpp:157
double coors[3]
Definition: myvector.h:29
bool IsZero()
Definition: vector.cpp:149
void SetVector(double, double, double)
Definition: vector.cpp:130
TVector operator-(const TVector &)
Definition: vector.cpp:118
void Get(double &, double &, double &) const
Definition: vector.cpp:161
double Dot(const TVector &U, const TVector &V)
Definition: vector.cpp:197
void Compute()
Definition: vector.cpp:76
int generateId()
Definition: vector.cpp:27
static int numberInstance
Definition: myvector.h:26