]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGColumnVector3.cpp
sync. with JSBSim v. 2.0
[flightgear.git] / src / FDM / JSBSim / math / FGColumnVector3.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGColumnVector3.cpp
4 Author: Originally by Tony Peden [formatted here (and broken??) by JSB]
5 Date started: 1998
6 Purpose: FGColumnVector3 class
7 Called by: Various
8
9 FUNCTIONAL DESCRIPTION
10 --------------------------------------------------------------------------------
11
12 HISTORY
13 --------------------------------------------------------------------------------
14 ??/??/??   TP   Created
15 03/16/2000 JSB  Added exception throwing
16
17 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
18 INCLUDES
19 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
20
21 #include "FGColumnVector3.h"
22 #include <stdio.h>
23
24 namespace JSBSim {
25
26 static const char *IdSrc = "$Id$";
27 static const char *IdHdr = ID_COLUMNVECTOR3;
28
29 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
30 CLASS IMPLEMENTATION
31 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
32
33 FGColumnVector3::FGColumnVector3(void)
34 {
35   data[0] = data[1] = data[2] = 0.0;
36   Debug(0);
37 }
38
39 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40
41 string FGColumnVector3::Dump(string delimeter) const
42 {
43   char buffer[256];
44   sprintf(buffer, "%f%s%f%s%f", Entry(1), delimeter.c_str(), Entry(2), delimeter.c_str(), Entry(3));
45   return string(buffer);
46 }
47
48 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49
50 ostream& operator<<(ostream& os, const FGColumnVector3& col)
51 {
52   os << col(1) << " , " << col(2) << " , " << col(3);
53   return os;
54 }
55
56 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
57
58 FGColumnVector3 FGColumnVector3::operator/(const double scalar) const
59 {
60   if (scalar != 0.0)
61     return operator*( 1.0/scalar );
62
63   cerr << "Attempt to divide by zero in method \
64     FGColumnVector3::operator/(const double scalar), \
65     object " << data[0] << " , " << data[1] << " , " << data[2] << endl;
66   return FGColumnVector3();
67 }
68
69 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
70
71 FGColumnVector3& FGColumnVector3::operator/=(const double scalar)
72 {
73   if (scalar != 0.0)
74     operator*=( 1.0/scalar );
75   else
76     cerr << "Attempt to divide by zero in method \
77       FGColumnVector3::operator/=(const double scalar), \
78       object " << data[0] << " , " << data[1] << " , " << data[2] << endl;
79
80   return *this;
81 }
82
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84
85 double FGColumnVector3::Magnitude(void) const
86 {
87   if (Entry(1) == 0.0 && Entry(2) == 0.0 && Entry(3) == 0.0)
88     return 0.0;
89   else
90     return sqrt( Entry(1)*Entry(1) +  Entry(2)*Entry(2) +  Entry(3)*Entry(3) );
91 }
92
93 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94
95 FGColumnVector3& FGColumnVector3::Normalize(void)
96 {
97   double Mag = Magnitude();
98
99   if (Mag != 0.0)
100     operator*=( 1.0/Mag );
101
102   return *this;
103 }
104
105 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106
107 FGColumnVector3 FGColumnVector3::multElementWise(const FGColumnVector3& V) const
108 {
109   return FGColumnVector3(Entry(1) * V(1), Entry(2) * V(2), Entry(3) * V(3));
110 }
111
112 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
113 //    The bitmasked value choices are as follows:
114 //    unset: In this case (the default) JSBSim would only print
115 //       out the normally expected messages, essentially echoing
116 //       the config files as they are read. If the environment
117 //       variable is not set, debug_lvl is set to 1 internally
118 //    0: This requests JSBSim not to output any messages
119 //       whatsoever.
120 //    1: This value explicity requests the normal JSBSim
121 //       startup messages
122 //    2: This value asks for a message to be printed out when
123 //       a class is instantiated
124 //    4: When this value is set, a message is displayed when a
125 //       FGModel object executes its Run() method
126 //    8: When this value is set, various runtime state variables
127 //       are printed out periodically
128 //    16: When set various parameters are sanity checked and
129 //       a message is printed out when they go out of bounds
130
131 void FGColumnVector3::Debug(int from)
132 {
133   if (debug_lvl <= 0) return;
134
135   if (debug_lvl & 1) { // Standard console startup message output
136   }
137   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
138     if (from == 0) cout << "Instantiated: FGColumnVector3" << endl;
139     if (from == 1) cout << "Destroyed:    FGColumnVector3" << endl;
140   }
141   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
142   }
143   if (debug_lvl & 8 ) { // Runtime state variables
144   }
145   if (debug_lvl & 16) { // Sanity checking
146   }
147   if (debug_lvl & 64) {
148     if (from == 0) { // Constructor
149       cout << IdSrc << endl;
150       cout << IdHdr << endl;
151     }
152   }
153 }
154
155 } // namespace JSBSim