]> git.mxchange.org Git - flightgear.git/blob - src/FDM/JSBSim/math/FGColumnVector3.cpp
Sync w. JSBSim as of 20/01/2007
[flightgear.git] / src / FDM / JSBSim / math / FGColumnVector3.cpp
1 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Module: FGColumnVector3.cpp
4 Author: Originally by Tony Peden [formatted here by JSB]
5 Date started: 1998
6 Purpose: FGColumnVector3 class
7 Called by: Various
8
9  ------------- Copyright (C) 1998 Tony Peden and Jon S. Berndt (jsb@hal-pc.org) -
10
11  This program is free software; you can redistribute it and/or modify it under
12  the terms of the GNU Lesser General Public License as published by the Free Software
13  Foundation; either version 2 of the License, or (at your option) any later
14  version.
15
16  This program is distributed in the hope that it will be useful, but WITHOUT
17  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
19  details.
20
21  You should have received a copy of the GNU Lesser General Public License along with
22  this program; if not, write to the Free Software Foundation, Inc., 59 Temple
23  Place - Suite 330, Boston, MA  02111-1307, USA.
24
25  Further information about the GNU Lesser General Public License can also be found on
26  the world wide web at http://www.gnu.org.
27
28 FUNCTIONAL DESCRIPTION
29 --------------------------------------------------------------------------------
30
31 HISTORY
32 --------------------------------------------------------------------------------
33 ??/??/??   TP   Created
34 03/16/2000 JSB  Added exception throwing
35
36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
37 INCLUDES
38 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
39
40 #include "FGColumnVector3.h"
41 #include <stdio.h>
42
43 namespace JSBSim {
44
45 static const char *IdSrc = "$Id$";
46 static const char *IdHdr = ID_COLUMNVECTOR3;
47
48 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
49 CLASS IMPLEMENTATION
50 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
51
52 FGColumnVector3::FGColumnVector3(void)
53 {
54   data[0] = data[1] = data[2] = 0.0;
55   Debug(0);
56 }
57
58 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
59
60 string FGColumnVector3::Dump(string delimeter) const
61 {
62   char buffer[256];
63   sprintf(buffer, "%f%s%f%s%f", Entry(1), delimeter.c_str(), Entry(2), delimeter.c_str(), Entry(3));
64   return string(buffer);
65 }
66
67 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
68
69 ostream& operator<<(ostream& os, const FGColumnVector3& col)
70 {
71   os << col(1) << " , " << col(2) << " , " << col(3);
72   return os;
73 }
74
75 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76
77 FGColumnVector3 FGColumnVector3::operator/(const double scalar) const
78 {
79   if (scalar != 0.0)
80     return operator*( 1.0/scalar );
81
82   cerr << "Attempt to divide by zero in method \
83     FGColumnVector3::operator/(const double scalar), \
84     object " << data[0] << " , " << data[1] << " , " << data[2] << endl;
85   return FGColumnVector3();
86 }
87
88 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89
90 FGColumnVector3& FGColumnVector3::operator/=(const double scalar)
91 {
92   if (scalar != 0.0)
93     operator*=( 1.0/scalar );
94   else
95     cerr << "Attempt to divide by zero in method \
96       FGColumnVector3::operator/=(const double scalar), \
97       object " << data[0] << " , " << data[1] << " , " << data[2] << endl;
98
99   return *this;
100 }
101
102 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104 double FGColumnVector3::Magnitude(void) const
105 {
106   if (Entry(1) == 0.0 && Entry(2) == 0.0 && Entry(3) == 0.0)
107     return 0.0;
108   else
109     return sqrt( Entry(1)*Entry(1) +  Entry(2)*Entry(2) +  Entry(3)*Entry(3) );
110 }
111
112 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113
114 FGColumnVector3& FGColumnVector3::Normalize(void)
115 {
116   double Mag = Magnitude();
117
118   if (Mag != 0.0)
119     operator*=( 1.0/Mag );
120
121   return *this;
122 }
123
124 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125
126 FGColumnVector3 FGColumnVector3::multElementWise(const FGColumnVector3& V) const
127 {
128   return FGColumnVector3(Entry(1) * V(1), Entry(2) * V(2), Entry(3) * V(3));
129 }
130
131 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
132 //    The bitmasked value choices are as follows:
133 //    unset: In this case (the default) JSBSim would only print
134 //       out the normally expected messages, essentially echoing
135 //       the config files as they are read. If the environment
136 //       variable is not set, debug_lvl is set to 1 internally
137 //    0: This requests JSBSim not to output any messages
138 //       whatsoever.
139 //    1: This value explicity requests the normal JSBSim
140 //       startup messages
141 //    2: This value asks for a message to be printed out when
142 //       a class is instantiated
143 //    4: When this value is set, a message is displayed when a
144 //       FGModel object executes its Run() method
145 //    8: When this value is set, various runtime state variables
146 //       are printed out periodically
147 //    16: When set various parameters are sanity checked and
148 //       a message is printed out when they go out of bounds
149
150 void FGColumnVector3::Debug(int from)
151 {
152   if (debug_lvl <= 0) return;
153
154   if (debug_lvl & 1) { // Standard console startup message output
155   }
156   if (debug_lvl & 2 ) { // Instantiation/Destruction notification
157     if (from == 0) cout << "Instantiated: FGColumnVector3" << endl;
158     if (from == 1) cout << "Destroyed:    FGColumnVector3" << endl;
159   }
160   if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
161   }
162   if (debug_lvl & 8 ) { // Runtime state variables
163   }
164   if (debug_lvl & 16) { // Sanity checking
165   }
166   if (debug_lvl & 64) {
167     if (from == 0) { // Constructor
168       cout << IdSrc << endl;
169       cout << IdHdr << endl;
170     }
171   }
172 }
173
174 } // namespace JSBSim