CONFIG LIB 1.5
Configuration Files Library (by TGG 2020)
Loading...
Searching...
No Matches
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
22using 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
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
54void 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
62void 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
70void 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
78void 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
118void 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
158void 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
202double 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
209VECTOR_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
222VECTOR_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
233VECTOR_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
244VECTOR_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
257VECTOR_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/*
268VECTOR_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*/
279VECTOR_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
290VECTOR_3D operator -( const VECTOR_3D &A )
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
303void 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
316void 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
323void 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
330void 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
337void operator *=( VECTOR_3D &A , const double &s )
338{
339 A.x *= s;
340 A.y *= s;
341 A.z *= s;
342}
343
344void 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
353bool 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
360VECTOR_3D VECTOR_3__E0( void )
361{
362 return VECTOR_3D( 0.0 , 0.0 , 0.0 );
363}
364
365VECTOR_3D VECTOR_3__E1( void )
366{
367 return VECTOR_3D( 1.0 , 1.0 , 1.0 );
368}
369
370VECTOR_3D VECTOR_3__Ex( void )
371{
372 return VECTOR_3D( 1.0 , 0.0 , 0.0 );
373}
374
375VECTOR_3D VECTOR_3__Ey( void )
376{
377 return VECTOR_3D( 0.0 , 1.0 , 0.0 );
378}
379
380VECTOR_3D VECTOR_3__Ez( void )
381{
382 return VECTOR_3D( 0.0 , 0.0 , 1.0 );
383}
384
386
387void 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
392void v_printf( char *fmt , char *name , const VECTOR_3D &vec )
393{
394 printf( fmt , name , vec.x , vec.y , vec.z );
395}
396
397void 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
402void 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
407void 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
412void 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
417ostream& operator << ( ostream & out, const VECTOR_3D & vec )
418{
419 out << "[" << vec.x << " , " << vec.y << " , " << vec.z << "]";
420 return out;
421}
422
423//**************************************************************************
424
425void 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
432void 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
439void vout0 (FILE *ff,VECTOR_3D vec)
440{
441 fprintf (ff,"% 12.7f % 12.7f % 12.7f",vec.x,0.,vec.z);
442}
443
444
Vector 3D class - vector manipulation.
Definition vector3d.h:34
void rotX(double alpha)
Rotating functions rotates the vector relative to the X axis by an alpha[rad] angle
Definition vector3d.cpp:54
void rotY(double alpha)
rotates the vector relative to the Y axis by an alpha[rad] angle
Definition vector3d.cpp:62
VECTOR_3D & operator=(const VECTOR_3D &v)
assignment operator
Definition vector3d.cpp:26
double Len(void) const
the value of the vector
Definition vector3d.h:52
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
void rotZ(double alpha)
rotates the vector relative to the Z axis by an alpha[rad] angle
Definition vector3d.cpp:70
double x
x coordinate
Definition vector3d.h:38
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
double z
z coordinate
Definition vector3d.h:38
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
double Normalize(void)
normalizes the vector to unit - coordiantes are divided by the vector value, returnes the vector valu...
Definition vector3d.cpp:38
double y
y coordinate
Definition vector3d.h:38
Vector 3D class and functions.