CONFIG LIB  1.5
Configuration Files Library (by TGG 2020)
vector3d.cpp
1 /*********************************************************************/
2 /* */
3 /* Config files library - (C) TGG 2019 */
4 /* */
5 /*********************************************************************/
6 /* Warszawa, 2019 */
7 /*********************************************************************/
8 /* */
9 /* File: vector3d.cpp */
10 /* */
11 /* Author: F.A.Dul, modif. by T.Grabowski */
12 /* */
13 /* Contents - vector 3d class */
14 /* */
15 /* */
16 /*********************************************************************/
17 /* */
18 /* */
19 
20 #include "vector3d.h"
21 
22 using namespace std;
23 
24 // Methods of VECTOR_3D class
25 
27 {
28  if( this != &a )
29  {
30  x = a.x;
31  y = a.y;
32  z = a.z;
33  }
34 
35  return *this;
36 }
37 
38 double VECTOR_3D::Normalize( void )
39 {
40  double dlg = this->Len();
41 
42  if( dlg > 0.0 )
43  {
44  x /= dlg;
45  y /= dlg;
46  z /= dlg;
47  }
48 
49  return dlg;
50 }
51 
52 // roatation functions of VECTOR_3D classs
53 
54 void VECTOR_3D::rotX( double alfa)
55 {
56  double yy=y;
57  double zz=z;
58  y = yy*cos(alfa) - zz*sin(alfa);
59  z = yy*sin(alfa) + zz*cos(alfa);
60 }
61 
62 void VECTOR_3D::rotY( double alfa)
63 {
64  double xx=x;
65  double zz=z;
66  x = zz*sin(alfa) + xx*cos(alfa);
67  z = zz*cos(alfa) - xx*sin(alfa);
68 }
69 
70 void VECTOR_3D::rotZ( double alfa)
71 {
72  double xx=x;
73  double yy=y;
74  x = xx*cos(alfa) - yy*sin(alfa);
75  y = xx*sin(alfa) + yy*cos(alfa);
76 }
77 
78 void VECTOR_3D::rotdX( double alfa )
79 {
80  double yy = y;
81  double zz = z;
82 
83  if (alfa==0.)
84  {
85  }
86  else
87  {
88  if (alfa==90.)
89  {
90  y = -zz;
91  z = yy;
92  }
93  else
94  {
95  if (alfa==180.)
96  {
97  y = -yy;
98  z = -zz;
99  }
100  else
101  {
102  if (alfa==270.)
103  {
104  y = zz;
105  z = -yy;
106  }
107  else
108  {
109  alfa *= M_PI/180.0;
110  y = yy*cos(alfa) - zz*sin(alfa);
111  z = yy*sin(alfa) + zz*cos(alfa);
112  }
113  }
114  }
115  }
116 }
117 
118 void VECTOR_3D::rotdY( double alfa, double dX )
119 {
120  double xx = x;
121  double zz = z;
122 
123  if (alfa==0.)
124  {
125  }
126  else
127  {
128  if (alfa==90.)
129  {
130  x = zz + dX;
131  z = -xx + dX;
132  }
133  else
134  {
135  if (alfa==180.)
136  {
137  x = -xx + 2.*dX;
138  z = -zz;
139  }
140  else
141  {
142  if (alfa==270.)
143  {
144  x = -zz + dX;
145  z = xx - dX;
146  }
147  else
148  {
149  alfa *= M_PI/180.0;
150  x = zz*sin(alfa) + (xx-dX)*cos(alfa) + dX;
151  z = zz*cos(alfa) - (xx-dX)*sin(alfa);
152  }
153  }
154  }
155  }
156 }
157 
158 void VECTOR_3D::rotdZ( double alfa )
159 {
160  double xx = x;
161  double yy = y;
162 
163  if (alfa==0.)
164  {
165  }
166  else
167  {
168  if (alfa==90.)
169  {
170  x = -yy;
171  y = xx;
172  }
173  else
174  {
175  if (alfa==180.)
176  {
177  x = -xx;
178  y = -yy;
179  }
180  else
181  {
182  if (alfa==270.)
183  {
184  x = yy;
185  y = -xx;
186  }
187  else
188  {
189  alfa *= M_PI/180.0;
190  x = xx*cos(alfa) - yy*sin(alfa);
191  y = xx*sin(alfa) + yy*cos(alfa);
192  }
193  }
194  }
195  }
196 }
197 
198 // VECTOR_3D operators
199 
200 // dot product
201 
202 double operator *( const VECTOR_3D &A , const VECTOR_3D &B )
203 {
204  return ( A.x * B.x + A.y * B.y + A.z * B.z );
205 }
206 
207 // cross product
208 
209 VECTOR_3D operator %( const VECTOR_3D &A , const VECTOR_3D &B )
210 {
211  VECTOR_3D c;
212 
213  c.x = A.y * B.z - A.z * B.y;
214  c.y = A.z * B.x - A.x * B.z;
215  c.z = A.x * B.y - A.y * B.x;
216 
217  return c;
218 }
219 
220 // scalar mult
221 
222 VECTOR_3D operator *( const VECTOR_3D &A , const double &s )
223 {
224  VECTOR_3D c;
225 
226  c.x = A.x * s;
227  c.y = A.y * s;
228  c.z = A.z * s;
229 
230  return c;
231 }
232 
233 VECTOR_3D operator *( const double &s , const VECTOR_3D &A )
234 {
235  VECTOR_3D c;
236 
237  c.x = A.x * s;
238  c.y = A.y * s;
239  c.z = A.z * s;
240 
241  return c;
242 }
243 
244 VECTOR_3D operator /( const VECTOR_3D &A , const double &s )
245 {
246  VECTOR_3D c;
247 
248  c.x = A.x / s;
249  c.y = A.y / s;
250  c.z = A.z / s;
251 
252  return c;
253 }
254 
255 // sum
256 
257 VECTOR_3D operator +( const VECTOR_3D &A , const VECTOR_3D &B )
258 {
259  VECTOR_3D c;
260 
261  c.x = A.x + B.x;
262  c.y = A.y + B.y;
263  c.z = A.z + B.z;
264 
265  return c;
266 }
267 /*
268 VECTOR_3D operator +( const VECTOR_3D &A )
269 {
270  VECTOR_3D c;
271 
272  c.x = A.x;
273  c.y = A.y;
274  c.z = A.z;
275 
276  return c;
277 }
278 */
279 VECTOR_3D operator -( const VECTOR_3D &A , const VECTOR_3D &B )
280 {
281  VECTOR_3D c;
282 
283  c.x = A.x - B.x;
284  c.y = A.y - B.y;
285  c.z = A.z - B.z;
286 
287  return c;
288 }
289 
291 {
292  VECTOR_3D c;
293 
294  c.x = -A.x;
295  c.y = -A.y;
296  c.z = -A.z;
297 
298  return c;
299 }
300 
301 // Shorthand Ops
302 
303 void operator %=( VECTOR_3D &A , const VECTOR_3D &B )
304 {
305  VECTOR_3D tmp;
306 
307  tmp.x = A.y * B.z - A.z * B.y;
308  tmp.y = A.z * B.x - A.x * B.z;
309  tmp.z = A.x * B.y - A.y * B.x;
310 
311  A.x = tmp.x;
312  A.y = tmp.y;
313  A.z = tmp.z;
314 }
315 
316 void operator +=( VECTOR_3D &A , const VECTOR_3D &B )
317 {
318  A.x += B.x;
319  A.y += B.y;
320  A.z += B.z;
321 }
322 
323 void operator -=( VECTOR_3D &A , const VECTOR_3D &B )
324 {
325  A.x -= B.x;
326  A.y -= B.y;
327  A.z -= B.z;
328 }
329 
330 void operator &=( VECTOR_3D &A , const VECTOR_3D &B )
331 {
332  A.x *= B.x;
333  A.y *= B.y;
334  A.z *= B.z;
335 }
336 
337 void operator *=( VECTOR_3D &A , const double &s )
338 {
339  A.x *= s;
340  A.y *= s;
341  A.z *= s;
342 }
343 
344 void operator /=( VECTOR_3D &A , const double &s )
345 {
346  A.x /= s;
347  A.y /= s;
348  A.z /= s;
349 }
350 
351 // compare two vectors
352 
353 bool operator ==( const VECTOR_3D &A , const VECTOR_3D &B )
354 {
355  return ( A.x == B.x && A.y == B.y && A.z == B.z );
356 }
357 
359 
361 {
362  return VECTOR_3D( 0.0 , 0.0 , 0.0 );
363 }
364 
366 {
367  return VECTOR_3D( 1.0 , 1.0 , 1.0 );
368 }
369 
371 {
372  return VECTOR_3D( 1.0 , 0.0 , 0.0 );
373 }
374 
376 {
377  return VECTOR_3D( 0.0 , 1.0 , 0.0 );
378 }
379 
381 {
382  return VECTOR_3D( 0.0 , 0.0 , 1.0 );
383 }
384 
386 
387 void v_printf( char *name , const VECTOR_3D &vec )
388 {
389  printf( "%s=[%19.11f,%19.11f,%19.11f]\n", name , vec.x , vec.y , vec.z );
390 }
391 
392 void v_printf( char *fmt , char *name , const VECTOR_3D &vec )
393 {
394  printf( fmt , name , vec.x , vec.y , vec.z );
395 }
396 
397 void v_sprintf( char *dest , char *name , const VECTOR_3D &vec )
398 {
399  sprintf(dest,"%s=[%19.11f,%19.11f %19.11f]", name , vec.x , vec.y , vec.z );
400 }
401 
402 void v_sprintf( char *dest , char *fmt , char *name , const VECTOR_3D &vec )
403 {
404  sprintf(dest , fmt , name , vec.x , vec.y , vec.z );
405 }
406 
407 void v_fprintf( FILE *dest , char *name , const VECTOR_3D &vec )
408 {
409  fprintf(dest,"%s=[%19.11f,%19.11f %19.11f]\n", name , vec.x , vec.y , vec.z );
410 }
411 
412 void v_fprintf( FILE *dest , char *fmt , char *name , const VECTOR_3D &vec )
413 {
414  fprintf( dest , fmt , name , vec.x , vec.y , vec.z );
415 }
416 
417 ostream& operator << ( ostream & out, const VECTOR_3D & vec )
418 {
419  out << "[" << vec.x << " , " << vec.y << " , " << vec.z << "]";
420  return out;
421 }
422 
423 //**************************************************************************
424 
425 void vout (FILE *ff,VECTOR_3D vec)
426 {
427  fprintf (ff,"% 12.7f % 12.7f % 12.7f",vec.x,vec.y,vec.z);
428 }
429 
430 //**************************************************************************
431 
432 void voutn (FILE *ff,VECTOR_3D vec)
433 {
434  fprintf (ff,"% 12.7f % 12.7f % 12.7f",vec.x,-vec.y,vec.z);
435 }
436 
437 //**************************************************************************
438 
439 void vout0 (FILE *ff,VECTOR_3D vec)
440 {
441  fprintf (ff,"% 12.7f % 12.7f % 12.7f",vec.x,0.,vec.z);
442 }
443 
444 
operator-
MATRIX_3x3 operator-(const MATRIX_3x3 &A, const MATRIX_3x3 &B)
Arithmetic operator - subtraction of 2 matrices (sum of A and -B)
Definition: matr3d.cpp:73
vout
void vout(FILE *ff, VECTOR_3D vec)
printf vector components (x,y,z) to stream ff
Definition: vector3d.cpp:425
operator==
bool operator==(const VECTOR_3D &A, const VECTOR_3D &B)
Boolean operator - compares two vectors - true if all appropriate coordinates are equal.
Definition: vector3d.cpp:353
v_fprintf
void v_fprintf(FILE *dest, char *name, const VECTOR_3D &vec)
print to stream "dest" name and vector components
Definition: vector3d.cpp:407
VECTOR_3D::z
double z
z coordinate
Definition: vector3d.h:38
voutn
void voutn(FILE *ff, VECTOR_3D vec)
printf vector components (x,-y,z) to stream ff
Definition: vector3d.cpp:432
VECTOR_3D::rotY
void rotY(double alpha)
rotates the vector relative to the Y axis by an alpha[rad] angle
Definition: vector3d.cpp:62
VECTOR_3D::Normalize
double Normalize(void)
normalizes the vector to unit - coordiantes are divided by the vector value, returnes the vector valu...
Definition: vector3d.cpp:38
VECTOR_3D::rotdX
void rotdX(double alfa)
rotates the vector relative to the X axis by an alpha[deg] angle (precise values for 0,...
Definition: vector3d.cpp:78
VECTOR_3D::x
double x
x coordinate
Definition: vector3d.h:38
VECTOR_3D::rotdZ
void rotdZ(double alfa)
rotates the vector relative to the Z axis by an alpha[deg] angle (precise values for 0,...
Definition: vector3d.cpp:158
v_sprintf
void v_sprintf(char *dest, char *name, const VECTOR_3D &vec)
print to string "dest" name and vector components
Definition: vector3d.cpp:397
operator%
VECTOR_3D operator%(const VECTOR_3D &A, const VECTOR_3D &B)
Arithmetic operator - cross (vector) product of two vectors.
Definition: vector3d.cpp:209
operator-=
void operator-=(VECTOR_3D &A, const VECTOR_3D &B)
Arithmetic operator - subtraction assigment.
Definition: vector3d.cpp:323
VECTOR_3__E0
VECTOR_3D VECTOR_3__E0(void)
returns vector (0,0,0)
Definition: vector3d.cpp:360
VECTOR_3D
Vector 3D class - vector manipulation.
Definition: vector3d.h:34
operator+
MATRIX_3x3 operator+(const MATRIX_3x3 &A, const MATRIX_3x3 &B)
Arithmetic operator - sum of 2 matrices.
Definition: matr3d.cpp:61
VECTOR_3__Ez
VECTOR_3D VECTOR_3__Ez(void)
returns vector (0,0,1)
Definition: vector3d.cpp:380
operator/=
void operator/=(MATRIX_3x3 &A, const double &s)
Arithmetic operator - division assignment.
Definition: matr3d.cpp:133
VECTOR_3D::operator=
VECTOR_3D & operator=(const VECTOR_3D &v)
assignment operator
Definition: vector3d.cpp:26
vector3d.h
Vector 3D class and functions.
VECTOR_3__Ex
VECTOR_3D VECTOR_3__Ex(void)
returns vector (1,0,0)
Definition: vector3d.cpp:370
VECTOR_3D::rotZ
void rotZ(double alpha)
rotates the vector relative to the Z axis by an alpha[rad] angle
Definition: vector3d.cpp:70
VECTOR_3__E1
VECTOR_3D VECTOR_3__E1(void)
returns vector (1,1,1)
Definition: vector3d.cpp:365
VECTOR_3D::rotX
void rotX(double alpha)
Rotating functions rotates the vector relative to the X axis by an alpha[rad] angle
Definition: vector3d.cpp:54
VECTOR_3D::rotdY
void rotdY(double alfa, double dX=0.)
rotates the vector relative to the Y axis (shifted by dX longwise X) by an alpha[deg] angle (precise ...
Definition: vector3d.cpp:118
v_printf
void v_printf(char *name, const VECTOR_3D &vec)
print to stdout name and vector components
Definition: vector3d.cpp:387
operator&=
void operator&=(VECTOR_3D &A, const VECTOR_3D &B)
Arithmetic operator - multiplication assigment (coordiante by coordiante)
Definition: vector3d.cpp:330
operator%=
void operator%=(VECTOR_3D &A, const VECTOR_3D &B)
Arithmetic operator - cross product assigment.
Definition: vector3d.cpp:303
operator/
MATRIX_3x3 operator/(const MATRIX_3x3 &A, const double &s)
Arithmetic operator - division of matrix by scalar (product of matrix and 1/s)
Definition: matr3d.cpp:122
operator*=
void operator*=(VECTOR_3D &A, const double &s)
Arithmetic operator - multiplication assigment.
Definition: vector3d.cpp:337
operator+=
void operator+=(VECTOR_3D &A, const VECTOR_3D &B)
Arithmetic operator - addition assigment.
Definition: vector3d.cpp:316
operator<<
std::ostream & operator<<(std::ostream &out, const VECTOR_3D &v)
print to std:ostream
VECTOR_3__Ey
VECTOR_3D VECTOR_3__Ey(void)
returns vector (0,1,0)
Definition: vector3d.cpp:375
operator*
MATRIX_3x3 operator*(const MATRIX_3x3 &A, const MATRIX_3x3 &B)
Arithmetic operator - multiplication of 2 matrices.
Definition: matr3d.cpp:150
vout0
void vout0(FILE *ff, VECTOR_3D vec)
printf vector components (x,0,z) to stream ff
Definition: vector3d.cpp:439
VECTOR_3D::y
double y
y coordinate
Definition: vector3d.h:38