Rosetta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
xyzMatrix.io.hh
Go to the documentation of this file.
1 // -*- mode:c++;tab-width:2;indent-tabs-mode:t;show-trailing-whitespace:t;rm-trailing-spaces:t -*-
2 // vi: set ts=2 noet:
3 //
4 // (c) Copyright Rosetta Commons Member Institutions.
5 // (c) This file is part of the Rosetta software suite and is made available under license.
6 // (c) The Rosetta software is developed by the contributing members of the Rosetta Commons.
7 // (c) For more information, see http://www.rosettacommons.org. Questions about this can be
8 // (c) addressed to University of Washington UW TechTransfer, email: license@u.washington.edu.
9 
10 /// @file numeric/xyzMatrix.io.hh
11 /// @brief xyzMatrix input/output functions
12 /// @author Frank M. D'Ippolito (Objexx@objexx.com)
13 /// @author Stuart G. Mentzer (Stuart_Mentzer@objexx.com)
14 
15 
16 #ifndef INCLUDED_numeric_xyzMatrix_io_hh
17 #define INCLUDED_numeric_xyzMatrix_io_hh
18 
19 
20 // Package headers
21 #include <numeric/xyzMatrix.hh>
22 #include <numeric/IOTraits.hh>
23 
24 // C++ headers
25 #include <iostream>
26 #include <iomanip>
27 #include <sstream>
28 
29 
30 namespace numeric {
31 
32 
33 /// @brief stream << xyzMatrix output operator
34 template< typename T >
35 std::ostream &
36 operator <<( std::ostream & stream, xyzMatrix< T > const & m )
37 {
38  // Types
39  using std::setw;
40  typedef IOTraits< T > Traits;
41 
42  // Save current stream state and set persistent state
43  std::ios_base::fmtflags const old_flags = stream.flags();
44  int const old_precision = stream.precision( Traits::precision() );
45  stream << std::right << std::showpoint << std::uppercase;
46 
47  // Output xyzMatrix
48  int const w = Traits::width();
49  stream
50  << setw( w ) << m.xx() << ' ' << setw( w ) << m.xy() << ' ' << setw( w ) << m.xz() << '\n'
51  << setw( w ) << m.yx() << ' ' << setw( w ) << m.yy() << ' ' << setw( w ) << m.yz() << '\n'
52  << setw( w ) << m.zx() << ' ' << setw( w ) << m.zy() << ' ' << setw( w ) << m.zz() << '\n';
53 
54  // Restore previous stream state
55  stream.precision( old_precision );
56  stream.flags( old_flags );
57 
58  return stream;
59 }
60 
61 
62 /// @brief Read an xyzMatrix row from a stream
63 /// @note Supports whitespace-separated values with optional commas between values as
64 /// long as whitespace is also present
65 /// @note Rows can optionally be enclosed in parentheses () or square brackets []
66 /// @note String or char values containing whitespace or commas or enclosed in quotes
67 /// are not supported
68 template< typename T >
69 std::istream &
70 read_row( std::istream & stream, T & x, T & y, T & z )
71 {
72  bool parens = false; // Opening ( present?
73  bool brackets = false; // Opening [ present?
74 
75  { // x
76  std::string input_string;
77  stream >> input_string;
78  if ( input_string == "(" ) { // Skip opening (
79  stream >> input_string;
80  parens = true;
81  } else if ( input_string[ 0 ] == '(' ) { // Skip opening (
82  input_string.erase( 0, 1 );
83  brackets = true;
84  } else if ( input_string == "[" ) { // Skip opening [
85  stream >> input_string;
86  brackets = true;
87  } else if ( input_string[ 0 ] == '[' ) { // Skip opening [
88  input_string.erase( 0, 1 );
89  brackets = true;
90  }
91  std::string::size_type const input_size = input_string.size();
92  if ( ( input_size > 0 ) && ( input_string[ input_size - 1 ] == ',' ) ) {
93  input_string.erase( input_size - 1 ); // Remove trailing ,
94  }
95  std::istringstream num_stream( input_string );
96  num_stream >> x;
97  }
98 
99  { // y
100  std::string input_string;
101  stream >> input_string;
102  if ( input_string == "," ) { // Skip ,
103  stream >> input_string;
104  } else if ( input_string[ 0 ] == ',' ) { // Skip leading ,
105  input_string.erase( 0, 1 );
106  }
107  std::string::size_type const input_size = input_string.size();
108  if ( ( input_size > 0 ) && ( input_string[ input_size - 1 ] == ',' ) ) {
109  input_string.erase( input_size - 1 ); // Remove trailing ,
110  }
111  std::istringstream num_stream( input_string );
112  num_stream >> y;
113  }
114 
115  { // z
116  std::string input_string;
117  stream >> input_string;
118  if ( input_string == "," ) { // Skip ,
119  stream >> input_string;
120  } else if ( input_string[ 0 ] == ',' ) { // Skip leading ,
121  input_string.erase( 0, 1 );
122  }
123  std::string::size_type input_size = input_string.size();
124  if ( parens || brackets ) { // Remove closing ) or ]
125  if ( input_size > 0 ) {
126  if ( parens ) {
127  if ( input_string[ input_size - 1 ] == ')' ) { // Remove closing )
128  input_string.erase( input_size - 1 );
129  --input_size;
130  }
131  } else if ( brackets ) {
132  if ( input_string[ input_size - 1 ] == ']' ) { // Remove closing ]
133  input_string.erase( input_size - 1 );
134  --input_size;
135  }
136  }
137  }
138  }
139  if ( ( input_size > 0 ) && ( input_string[ input_size - 1 ] == ',' ) ) {
140  input_string.erase( input_size - 1 ); // Remove trailing ,
141  }
142  std::istringstream num_stream( input_string );
143  num_stream >> z;
144  }
145 
146  // Remove closing ) or ] if opening ( or [ present
147  if ( parens || brackets ) { // Remove closing ) or ]
148  while ( ( stream.peek() == ' ' ) || ( stream.peek() == '\t' ) ) {
149  stream.ignore();
150  }
151  if ( parens ) { // Remove closing ) if present
152  if ( stream.peek() == ')' ) stream.ignore();
153  } else if ( brackets ) { // Remove closing ] if present
154  if ( stream.peek() == ']' ) stream.ignore();
155  }
156  }
157 
158  return stream;
159 }
160 
161 /// @brief stream >> xyzMatrix input operator
162 /// @note Reads row-ordered matrix elements from one or multiple lines
163 template< typename T >
164 std::istream &
165 operator >>( std::istream & stream, xyzMatrix< T > & m )
166 {
167  read_row( stream, m.xx(), m.xy(), m.xz() );
168  read_row( stream, m.yx(), m.yy(), m.yz() );
169  read_row( stream, m.zx(), m.zy(), m.zz() );
170  return stream;
171 }
172 
173 } // namespace numeric
174 
175 
176 #endif // INCLUDED_numeric_xyzMatrix_io_HH
Value const & zz() const
Value zz const.
Definition: xyzMatrix.hh:1623
std::istream & operator>>(std::istream &stream, BodyPosition< T > &p)
stream >> BodyPosition input operator
cmplx w(cmplx z, double relerr)
Definition: functions.cc:470
def x
Fast 3x3 matrix.
Value const & yx() const
Value yx const.
Definition: xyzMatrix.hh:1533
Value const & xx() const
Value xx const.
Definition: xyzMatrix.hh:1479
Value const & yz() const
Value yz const.
Definition: xyzMatrix.hh:1569
Value const & yy() const
Value yy const.
Definition: xyzMatrix.hh:1551
Numerics input/output type traits.
def z
Numerics input/output type traits.
Definition: IOTraits.hh:25
Value const & zx() const
Value zx const.
Definition: xyzMatrix.hh:1587
std::istream & read_row(std::istream &stream, T &x, T &y, T &z, T &t)
Read an BodyPosition row from a stream.
Value const & zy() const
Value zy const.
Definition: xyzMatrix.hh:1605
NumericTraits< Type > Traits
Definition: constants.cc:30
Value const & xz() const
Value xz const.
Definition: xyzMatrix.hh:1515
Value const & xy() const
Value xy const.
Definition: xyzMatrix.hh:1497
char & uppercase(char &c)
Uppercase a Character.
def y