Commit 822a313a authored by Alexander Lapshin's avatar Alexander Lapshin

.

parent 84183d1a
...@@ -282,14 +282,14 @@ void loadjson(const jsonval& parent, std::map<T2, T>& map) ...@@ -282,14 +282,14 @@ void loadjson(const jsonval& parent, std::map<T2, T>& map)
if (parent.IsArray()) { if (parent.IsArray()) {
for (rapidjson::SizeType i = 0; i < parent.Size(); i++) { for (rapidjson::SizeType i = 0; i < parent.Size(); i++) {
T item; T item;
loadjson(parent[i], item); item.load(parent[i]);
map[item.getId()] = item; map[item.getId()] = item;
} }
} }
else if (parent.IsObject()) { else if (parent.IsObject()) {
for (jsonval::ConstMemberIterator itr = parent.MemberBegin(); itr != parent.MemberEnd(); ++itr) { for (jsonval::ConstMemberIterator itr = parent.MemberBegin(); itr != parent.MemberEnd(); ++itr) {
T item; T item;
loadjson(itr->value, item); item.load(itr->value);
map[item.getId()] = item; map[item.getId()] = item;
} }
} }
...@@ -331,6 +331,39 @@ static bool loadjson(const jsonval& parent, const std::string& key, double& valu ...@@ -331,6 +331,39 @@ static bool loadjson(const jsonval& parent, const std::string& key, double& valu
return true; return true;
} }
static bool loadjson(const jsonval& parent, const std::string& key, float& value, const bool exist = true, bool* loaded = nullptr)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
if (a.IsNumber()) {
value = a.GetDouble();
}
else if (a.IsString()) {
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, int& value, const bool exist = true, bool* loaded = nullptr) static bool loadjson(const jsonval& parent, const std::string& key, int& value, const bool exist = true, bool* loaded = nullptr)
{ {
jsonval::ConstMemberIterator itr; jsonval::ConstMemberIterator itr;
...@@ -364,6 +397,39 @@ static bool loadjson(const jsonval& parent, const std::string& key, int& value, ...@@ -364,6 +397,39 @@ static bool loadjson(const jsonval& parent, const std::string& key, int& value,
return true; return true;
} }
static bool loadjson(const jsonval& parent, const std::string& key, short& value, const bool exist = true, bool* loaded = nullptr)
{
jsonval::ConstMemberIterator itr;
if (!extract_value(parent, key, exist, loaded, itr)) {
return false;
}
const jsonval& a = itr->value;
if (a.IsNumber()) {
value = a.GetInt();
}
else if (a.IsString()) {
std::string str = a.GetString();
if (str.empty() && !exist) {
if (loaded) {
*loaded = false;
}
return false;
}
if (!Converter::ToNumber(str, value)) {
throw (Exp() << "bad value format '" << str << "'" << " in container '");
}
}
else {
throw (Exp() << "element '" << key << "'" << " has unexpected format '");
}
return true;
}
static bool loadjson(const jsonval& parent, const std::string& key, size_t& value, const bool exist = true, bool* loaded = nullptr) static bool loadjson(const jsonval& parent, const std::string& key, size_t& value, const bool exist = true, bool* loaded = nullptr)
{ {
jsonval::ConstMemberIterator itr; jsonval::ConstMemberIterator itr;
...@@ -629,6 +695,12 @@ static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, do ...@@ -629,6 +695,12 @@ static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, do
ss.AddMember(jname, value, alc); ss.AddMember(jname, value, alc);
} }
static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, float value)
{
jsonval jname(conName, alc);
ss.AddMember(jname, value, alc);
}
static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const char* value) static void PutValue(jsonalloc& alc, jsonval& ss, const std::string& conName, const char* value)
{ {
jsonval jname(conName, alc); jsonval jname(conName, alc);
......
...@@ -28,54 +28,36 @@ public: ...@@ -28,54 +28,36 @@ public:
static bool ToNumber(const std::string& str, unsigned short int& val) static bool ToNumber(const std::string& str, unsigned short int& val)
{ {
// std::istringstream ss(str); val = std::stoi(str, nullptr);
// ss >> val; return true;
// return !ss.fail(); }
// int ival;
// bool res = str2int(ival, str.c_str()) == XSUCCESS;
// val = ival;
static bool ToNumber(const std::string& str, short& val)
{
val = std::stoi(str, nullptr); val = std::stoi(str, nullptr);
return true; return true;
} }
static bool ToNumber(const std::string& str, size_t& val) static bool ToNumber(const std::string& str, size_t& val)
{ {
//std::istringstream ss(str);
//ss >> val;
//return !ss.fail();
val = std::stoi(str, nullptr); val = std::stoi(str, nullptr);
return true; return true;
} }
static bool ToNumber(const std::string& str, int& val) static bool ToNumber(const std::string& str, int& val)
{ {
//std::istringstream ss(str);
//ss >> val;
//return !ss.fail();
val = std::stoi(str, nullptr); val = std::stoi(str, nullptr);
return true; return true;
} }
static bool ToNumber(const std::string& str, double& val) static bool ToNumber(const std::string& str, double& val)
{ {
// std::istringstream ss(str);
// ss >> val;
// return !ss.fail();
val = std::stod(str, nullptr); val = std::stod(str, nullptr);
return true; return true;
} }
static bool ToNumber(const std::string& str, float& val) static bool ToNumber(const std::string& str, float& val)
{ {
// std::istringstream ss(str);
// ss >> val;
// return !ss.fail();
val = std::stof(str, nullptr); val = std::stof(str, nullptr);
return true; return true;
} }
......
#include "MagData.h" #include "MagData.h"
#include "Consts.h" #include "Consts.h"
#include "globals.h"
MagData::MagData() MagData::MagData() = default;
:
m_phase(0),
m_range(0),
m_mag(0),
m_hasData(false),
m_hasGeomData(false)
{
}
void MagData::SetOpticData(double stdphase, double stdtrange, double stdmag) void MagData::SetOpticData(const double stdphase_deg, const double stdtrange_km, const double stdmag)
{ {
m_hasData = true; m_hasData = true;
m_mag = 0; m_stdphase_rad = stdphase_deg * deg2rad;
m_phase = 0; m_stdrange_tkm = stdtrange_km * 1E-3;
m_range = 0;
m_stdphase = stdphase;
m_stdrange = stdtrange;
m_stdmag = stdmag; m_stdmag = stdmag;
} }
...@@ -30,19 +20,10 @@ void MagData::SetGeomData(double rcs, double albedo) ...@@ -30,19 +20,10 @@ void MagData::SetGeomData(double rcs, double albedo)
m_albedo = albedo; m_albedo = albedo;
} }
bool MagData::Load(myXML::Contain* parent)
{
LoadXML(parent, "Phase", m_stdphase);
LoadXML(parent, "Range", m_stdrange);
LoadXML(parent, "Stdmag", m_stdmag, false, &m_hasData);
return true;
}
void MagData::load(const jsonval& parent) void MagData::load(const jsonval& parent)
{ {
loadjson(parent, "phase", m_stdphase); loadjson(parent, "phase", m_stdphase_rad);
loadjson(parent, "range", m_stdrange); loadjson(parent, "range", m_stdrange_tkm);
loadjson(parent, "mag", m_stdmag); loadjson(parent, "mag", m_stdmag);
m_hasData = true; m_hasData = true;
} }
...@@ -63,7 +44,7 @@ double MagData::CalcRealMag(double A0g, double sunMag, double phase, double rang ...@@ -63,7 +44,7 @@ double MagData::CalcRealMag(double A0g, double sunMag, double phase, double rang
void MagData::CalcMag(double phase, double range) void MagData::CalcMag(double phase, double range)
{ {
const double sunMag = -26.58; const double sunMag = -26.58;
m_mag = CalcRealMag(CalcA0g(m_stdmag, m_stdphase, m_stdrange, sunMag), sunMag, phase, range); m_mag = CalcRealMag(CalcA0g(m_stdmag, m_stdphase_rad, m_stdrange_tkm, sunMag), sunMag, phase, range);
m_phase = phase; m_phase = phase;
m_range = range; m_range = range;
} }
......
#pragma once #pragma once
#include "XMLFunctions.h"
#include "json_functions.h" #include "json_functions.h"
class MagData class MagData
{ {
public: public:
MagData(); MagData();
void SetOpticData(double stdphase, double stdtrange, double stdmag); void SetOpticData(double stdphase_deg, double stdtrange_km, double stdmag);
void SetGeomData(double rcs, double albedo); void SetGeomData(double rcs, double albedo);
bool Load(myXML::Contain* parent);
void load(const jsonval& parent); void load(const jsonval& parent);
double GetMag() const { return m_mag; } double GetMag() const { return m_mag; }
double GetRcs() const { return m_rcs; } double GetRcs() const { return m_rcs; }
...@@ -18,20 +16,20 @@ public: ...@@ -18,20 +16,20 @@ public:
void CalcMag(double phase, double range); void CalcMag(double phase, double range);
void CalcGeomMag(double phase, double range); void CalcGeomMag(double phase, double range);
double get_std_mag() const { return m_stdmag; } double get_std_mag() const { return m_stdmag; }
double get_std_phase() const { return m_stdphase; } double get_std_phase() const { return m_stdphase_rad; }
double get_std_range() const { return m_stdrange; } double get_std_range() const { return m_stdrange_tkm; }
private: private:
static double CalcA0g(double stdmag, double stdphase, double stdrange, double sunMag); static double CalcA0g(double stdmag, double stdphase, double stdrange, double sunMag);
static double CalcRealMag(double A0g, double sunMag, double phase, double range); static double CalcRealMag(double A0g, double sunMag, double phase, double range);
double m_stdmag; double m_stdmag;
double m_stdphase; double m_stdphase_rad{0};
double m_stdrange; double m_stdrange_tkm{40};
double m_phase; double m_phase;
double m_range; double m_range;
double m_mag; double m_mag;
double m_rcs; double m_rcs;
double m_albedo; double m_albedo;
bool m_hasData; bool m_hasData{false};
bool m_hasGeomData; bool m_hasGeomData{false};
}; };
...@@ -336,9 +336,10 @@ template <typename T, typename T2> ...@@ -336,9 +336,10 @@ template <typename T, typename T2>
std::vector<T> map_keys(const std::map<T, T2>& map) std::vector<T> map_keys(const std::map<T, T2>& map)
{ {
std::vector<T> result; std::vector<T> result;
result.reserve(map.size());
for (typename std::map<T, T2>::const_iterator it = map.begin(); it != map.end(); ++it) { for (const auto& it: map) {
result.push_back(it->first); result.push_back(it.first);
} }
return result; return result;
......
...@@ -43,7 +43,11 @@ void orb_block_data::load(const jsonval& parent) ...@@ -43,7 +43,11 @@ void orb_block_data::load(const jsonval& parent)
loadjson(parent, "mind_km", m_mind, false); loadjson(parent, "mind_km", m_mind, false);
std::string vecs; std::string vecs;
loadjson(parent, "vecs", vecs); loadjson(parent, "vecs", vecs, false);
if(vecs.empty()) {
return;
}
std::vector<std::string> parts; std::vector<std::string> parts;
splitstrf(vecs, parts, ","); splitstrf(vecs, parts, ",");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment