JSBSim Flight Dynamics Model  1.0 (02 March 2017)
An Open Source Flight Dynamics and Control Software Library in C++
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 (jon@jsbsim.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 "FGJSBBase.h"
41 #include "FGColumnVector3.h"
42 #include <iostream>
43 #include <sstream>
44 #include <iomanip>
45 #include <cmath>
46 
47 using namespace std;
48 
49 namespace JSBSim {
50 
51 IDENT(IdSrc,"$Id: FGColumnVector3.cpp,v 1.17 2014/01/13 10:46:03 ehofman Exp $");
52 IDENT(IdHdr,ID_COLUMNVECTOR3);
53 
54 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
55 CLASS IMPLEMENTATION
56 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
57 
58 FGColumnVector3::FGColumnVector3(void)
59 {
60  data[0] = data[1] = data[2] = 0.0;
61  // Debug(0);
62 }
63 
64 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 
66 string FGColumnVector3::Dump(const string& delimiter) const
67 {
68  ostringstream buffer;
69  buffer << std::setprecision(16) << data[0] << delimiter;
70  buffer << std::setprecision(16) << data[1] << delimiter;
71  buffer << std::setprecision(16) << data[2];
72  return buffer.str();
73 }
74 
75 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
76 
77 ostream& operator<<(ostream& os, const FGColumnVector3& col)
78 {
79  os << col(1) << " , " << col(2) << " , " << col(3);
80  return os;
81 }
82 
83 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
84 
85 FGColumnVector3 FGColumnVector3::operator/(const double scalar) const
86 {
87  if (scalar != 0.0)
88  return operator*( 1.0/scalar );
89 
90  cerr << "Attempt to divide by zero in method \
91  FGColumnVector3::operator/(const double scalar), \
92  object " << data[0] << " , " << data[1] << " , " << data[2] << endl;
93  return FGColumnVector3();
94 }
95 
96 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97 
98 FGColumnVector3& FGColumnVector3::operator/=(const double scalar)
99 {
100  if (scalar != 0.0)
101  operator*=( 1.0/scalar );
102  else
103  cerr << "Attempt to divide by zero in method \
104  FGColumnVector3::operator/=(const double scalar), \
105  object " << data[0] << " , " << data[1] << " , " << data[2] << endl;
106 
107  return *this;
108 }
109 
110 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 
112 double FGColumnVector3::Magnitude(void) const
113 {
114  return sqrt( data[0]*data[0] + data[1]*data[1] + data[2]*data[2] );
115 }
116 
117 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 
119 FGColumnVector3& FGColumnVector3::Normalize(void)
120 {
121  double Mag = Magnitude();
122 
123  if (Mag != 0.0)
124  operator*=( 1.0/Mag );
125 
126  return *this;
127 }
128 
129 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130 
131 double FGColumnVector3::Magnitude(const int idx1, const int idx2) const {
132  return sqrt( data[idx1-1]*data[idx1-1] + data[idx2-1]*data[idx2-1] );
133 }
134 
135 
136 } // namespace JSBSim
STL namespace.
This class implements a 3 element column vector.