JSBSim Flight Dynamics Model  1.2.0 (05 Nov 2023)
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 /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
52 CLASS IMPLEMENTATION
53 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
54 
55 FGColumnVector3::FGColumnVector3(void)
56 {
57  data[0] = data[1] = data[2] = 0.0;
58  // Debug(0);
59 }
60 
61 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
62 
63 string FGColumnVector3::Dump(const string& delimiter) const
64 {
65  ostringstream buffer;
66  buffer << std::setprecision(16) << data[0] << delimiter;
67  buffer << std::setprecision(16) << data[1] << delimiter;
68  buffer << std::setprecision(16) << data[2];
69  return buffer.str();
70 }
71 
72 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 
74 ostream& operator<<(ostream& os, const FGColumnVector3& col)
75 {
76  os << col(1) << " , " << col(2) << " , " << col(3);
77  return os;
78 }
79 
80 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 
82 FGColumnVector3 FGColumnVector3::operator/(const double scalar) const
83 {
84  if (scalar != 0.0)
85  return operator*( 1.0/scalar );
86 
87  cerr << "Attempt to divide by zero in method \
88  FGColumnVector3::operator/(const double scalar), \
89  object " << data[0] << " , " << data[1] << " , " << data[2] << endl;
90  return FGColumnVector3();
91 }
92 
93 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94 
95 FGColumnVector3& FGColumnVector3::operator/=(const double scalar)
96 {
97  if (scalar != 0.0)
98  operator*=( 1.0/scalar );
99  else
100  cerr << "Attempt to divide by zero in method \
101  FGColumnVector3::operator/=(const double scalar), \
102  object " << data[0] << " , " << data[1] << " , " << data[2] << endl;
103 
104  return *this;
105 }
106 
107 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
108 
109 double FGColumnVector3::Magnitude(void) const
110 {
111  return sqrt( data[0]*data[0] + data[1]*data[1] + data[2]*data[2] );
112 }
113 
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 
116 FGColumnVector3& FGColumnVector3::Normalize(void)
117 {
118  double Mag = Magnitude();
119 
120  if (Mag != 0.0)
121  operator*=( 1.0/Mag );
122 
123  return *this;
124 }
125 
126 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
127 
128 double FGColumnVector3::Magnitude(const int idx1, const int idx2) const {
129  return sqrt( data[idx1-1]*data[idx1-1] + data[idx2-1]*data[idx2-1] );
130 }
131 
132 
133 } // namespace JSBSim
This class implements a 3 element column vector.