123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271 |
-
-
-
-
-
-
- #pragma once
-
- #ifndef HELPER_INC
- #define HELPER_INC
-
- #include "lemma.h"
- #include "yaml-cpp/yaml.h"
-
- namespace Lemma {
-
- template <class T>
- inline std::string to_string (const T& t) {
- std::stringstream ss;
- ss << t;
- return ss.str();
- }
-
-
-
- std::string enum2String(const FREQUENCYUNITS& Units);
- std::string enum2String(const TIMEUNITS& Units);
- std::string enum2String(const MAGUNITS& Units);
- std::string enum2String(const TEMPUNITS& Units);
- std::string enum2String(const FEMCOILORIENTATION& Units);
- std::string enum2String(const ORIENTATION& Units);
- std::string enum2String(const FIELDCOMPONENT& Comp);
- std::string enum2String(const HANKELTRANSFORMTYPE& Htype);
- std::string enum2String(const FIELDCALCULATIONS& Htype);
-
-
-
- template <typename T>
- T string2Enum( const std::string& str );
-
-
-
-
-
-
- class IndentingOStreambuf : public std::streambuf {
- std::streambuf* myDest;
- bool myIsAtStartOfLine;
- std::string myIndent;
- std::ostream* myOwner;
- protected:
- virtual int overflow( int ch )
- {
- if ( myIsAtStartOfLine && ch != '\n' ) {
- myDest->sputn( myIndent.data(), myIndent.size() );
- }
- myIsAtStartOfLine = ch == '\n';
- return myDest->sputc( ch );
- }
- public:
- explicit IndentingOStreambuf(
- std::streambuf* dest, int indent = 4 )
- : myDest( dest )
- , myIsAtStartOfLine( true )
- , myIndent( indent, ' ' )
- , myOwner( NULL )
- {
- }
- explicit IndentingOStreambuf(
- std::ostream& dest, int indent = 4 )
- : myDest( dest.rdbuf() )
- , myIsAtStartOfLine( true )
- , myIndent( indent, ' ' )
- , myOwner( &dest )
- {
- myOwner->rdbuf( this );
- }
- virtual ~IndentingOStreambuf()
- {
- if ( myOwner != NULL ) {
- myOwner->rdbuf( myDest );
- }
- }
- };
-
-
- }
-
-
-
- namespace YAML {
-
- template<>
- struct convert<Lemma::Complex> {
- static Node encode(const Lemma::Complex& rhs) {
- Node node;
- node["real"] = rhs.real();
- node["imag"] = rhs.imag();
-
-
-
-
-
- node.SetTag( "Complex" );
- return node;
- }
-
- static bool decode(const Node& node, Lemma::Complex& rhs) {
-
- if( node.Tag() != "Complex" ) {
- return false;
- }
- rhs = Lemma::Complex( node["real"].as<Lemma::Real>(), node["imag"].as<Lemma::Real>() );
-
-
- return true;
- }
-
- };
-
- template<>
- struct convert<Lemma::Vector3Xr> {
- static Node encode(const Lemma::Vector3Xr& rhs) {
- Node node;
- node["size"] = rhs.cols();
-
- for (int ic=0; ic<rhs.cols(); ++ic) {
- node[ic].push_back( rhs(0, ic) );
- node[ic].push_back( rhs(1, ic) );
- node[ic].push_back( rhs(2, ic) );
- }
- node.SetTag( "Vector3Xr" );
- return node;
- }
-
- static bool decode(const Node& node, Lemma::Vector3Xr& rhs) {
- if( node.Tag() != "Vector3Xr" ) {
- return false;
- }
- rhs.resize( Eigen::NoChange, node["size"].as<int>() );
- for (unsigned int ic=0; ic<node.size(); ++ic) {
- int ir=0;
- for(YAML::const_iterator it=node[ic].begin();it!=node[ic].end();++it) {
- rhs(ir, ic) = it->as<Lemma::Real>();
- ++ir;
- }
- }
- return true;
- }
- };
-
- template<>
- struct convert<Lemma::VectorXr> {
- static Node encode(const Lemma::VectorXr& rhs) {
- Node node;
- node["size"] = rhs.size();
- for (int ic=0; ic<rhs.size(); ++ic) {
- node["data"].push_back( rhs(ic) );
- }
- node.SetTag( "VectorXr" );
- return node;
- }
-
- static bool decode(const Node& node, Lemma::VectorXr& rhs) {
- if( node.Tag() != "VectorXr" ) {
- return false;
- }
- rhs.resize( node["size"].as<int>() );
- int ir=0;
- for(YAML::const_iterator it=node["data"].begin(); it!=node["data"].end(); ++it) {
- rhs(ir) = it->as<Lemma::Real>();
- ++ir;
- }
- return true;
- }
- };
-
- template<>
- struct convert<Lemma::VectorXcr> {
- static Node encode(const Lemma::VectorXcr& rhs) {
- Node node;
- node["size"] = rhs.size();
- for (int ic=0; ic<rhs.size(); ++ic) {
- node["data"].push_back( rhs(ic) );
- }
- node.SetTag( "VectorXcr" );
- return node;
- }
-
- static bool decode(const Node& node, Lemma::VectorXcr& rhs) {
- if( node.Tag() != "VectorXcr" ) {
- return false;
- }
- rhs.resize( node["size"].as<int>() );
- int ir=0;
- for(YAML::const_iterator it=node["data"].begin(); it!=node["data"].end(); ++it) {
- rhs(ir) = it->as<Lemma::Complex>();
- ++ir;
- }
- return true;
- }
- };
-
-
- template<>
- struct convert<Lemma::VectorXi> {
- static Node encode(const Lemma::VectorXi& rhs) {
- Node node;
- node["size"] = rhs.size();
- for (int ic=0; ic<rhs.size(); ++ic) {
- node["data"].push_back( rhs(ic) );
- }
- node.SetTag( "VectorXi" );
- return node;
- }
-
- static bool decode(const Node& node, Lemma::VectorXi& rhs) {
- if( node.Tag() != "VectorXi" ) {
- return false;
- }
- rhs.resize( node["size"].as<int>() );
- int ir=0;
- for(YAML::const_iterator it=node["data"].begin(); it!=node["data"].end(); ++it) {
- rhs(ir) = it->as<int>();
- ++ir;
- }
- return true;
- }
- };
-
- template<>
- struct convert<Lemma::Vector3r> {
- static Node encode(const Lemma::Vector3r& rhs) {
- Node node;
- for (int ic=0; ic<rhs.size(); ++ic) {
- node[0].push_back( rhs(ic) );
- }
- node.SetTag( "Vector3r" );
- return node;
- }
-
- static bool decode(const Node& node, Lemma::Vector3r& rhs) {
- if( node.Tag() != "Vector3r" ) {
- return false;
- }
- int ir=0;
- for(YAML::const_iterator it=node[0].begin(); it!=node[0].end(); ++it) {
- rhs(ir) = it->as<Lemma::Real>();
- ++ir;
- }
- return true;
- }
- };
-
- }
-
- #endif
|