From 983348c9eb5bddb1c062fbeeedf1387736b49ad7 Mon Sep 17 00:00:00 2001 From: Valery Date: Fri, 26 Apr 2024 19:36:34 +0300 Subject: [PATCH] one header --- array_core/array_core.h | 8 +- common_utils/common_constants.cpp | 2 + common_utils/common_constants.h | 2 + common_utils/common_utils.cpp | 17 +- common_utils/common_utils.h | 9 +- davis_files.txt | 9 + davis_one/davis.cpp | 502 ++++++++++++++++++++++++++++++ davis_one/davis.h | 263 ++++++++++++++++ davis_one_maker.py | 138 ++++++++ plotly_maker/html_parts.cpp | 5 +- plotly_maker/html_parts.h | 2 + plotly_maker/plotly_maker.cpp | 14 +- plotly_maker/plotly_maker.h | 8 +- 13 files changed, 957 insertions(+), 22 deletions(-) create mode 100644 davis_files.txt create mode 100644 davis_one/davis.cpp create mode 100644 davis_one/davis.h create mode 100644 davis_one_maker.py diff --git a/array_core/array_core.h b/array_core/array_core.h index 111ab3f..d8825d8 100644 --- a/array_core/array_core.h +++ b/array_core/array_core.h @@ -1,13 +1,15 @@ #ifndef ARRAY_CORE_ARRAY_CORE_H_ #define ARRAY_CORE_ARRAY_CORE_H_ - +//#START_GRAB_TO_INCLUDES_LIST #include #include +//#STOP_GRAB_TO_INCLUDES_LIST #include "plotly_maker/plotly_maker.h" #include "common_utils/common_utils.h" #include "common_utils/common_constants.h" namespace davis { +//#START_GRAB_TO_DAVIS_NAMESPACE using std::vector; using std::string; @@ -137,7 +139,7 @@ bool show(const T* data, uint64_t count, const string& title, res = davis::showLineChartInBrowser(dblRow, title, static_cast(settings)); return res; } - -} // namespace davis +//#STOP_GRAB_TO_DAVIS_NAMESPACE +} // end namespace davis #endif //ARRAY_CORE_ARRAY_CORE_H_ diff --git a/common_utils/common_constants.cpp b/common_utils/common_constants.cpp index 5015964..e9d0434 100644 --- a/common_utils/common_constants.cpp +++ b/common_utils/common_constants.cpp @@ -1,9 +1,11 @@ #include "common_constants.h" namespace davis { +//#START_GRAB_TO_DAVIS_NAMESPACE const char kAppName[] = "davis"; const char kOutFolderName[] = "davis_htmls/"; const char kPlotlyJsWorkPath[] = "./davis_htmls/plotly-2.27.0.min.js"; const char kPlotlyJsName[] = "plotly-2.27.0.min.js"; const char kPlotlyJsResourcePath[] = "plotly_maker/plotly-2.27.0.min.js"; +//#STOP_GRAB_TO_DAVIS_NAMESPACE } diff --git a/common_utils/common_constants.h b/common_utils/common_constants.h index 1c1e5a9..0d8a1e7 100644 --- a/common_utils/common_constants.h +++ b/common_utils/common_constants.h @@ -2,11 +2,13 @@ #define COMMON_UTILS_COMMON_CONSTANTS_H namespace davis { +//#START_GRAB_TO_DAVIS_NAMESPACE extern const char kAppName[]; extern const char kOutFolderName[]; extern const char kPlotlyJsName[]; extern const char kPlotlyJsResourcePath[]; extern const char kPlotlyJsWorkPath[]; +//#STOP_GRAB_TO_DAVIS_NAMESPACE } #endif // COMMON_UTILS_COMMON_CONSTANTS_H diff --git a/common_utils/common_utils.cpp b/common_utils/common_utils.cpp index 78cae89..45838b5 100644 --- a/common_utils/common_utils.cpp +++ b/common_utils/common_utils.cpp @@ -1,12 +1,15 @@ #include "common_utils.h" -#include "fstream" +#include "common_constants.h" + +//#START_GRAB_TO_INCLUDES_LIST +#include #include #include -#include "common_constants.h" #include +//#STOP_GRAB_TO_INCLUDES_LIST namespace { - +//#START_GRAB_TO_NAMESPACE using std::string; #ifdef _WIN32 @@ -39,11 +42,11 @@ void openFileBySystem(const string& file_name) { command.append(file_name); system(command.c_str()); } - +//#STOP_GRAB_TO_NAMESPACE } // namespace namespace davis { - +//#START_GRAB_TO_DAVIS_NAMESPACE string getCurrentPath() { #if defined (_WIN32) || (__linux__) char buffer[1024]; @@ -157,7 +160,7 @@ bool readMatrix(vector>& outMatrix, const std::string& path, char } } -std::vector split(const std::string& target, char c) { +vector split(const string& target, char c) { std::string temp; std::stringstream stringstream { target }; std::vector result; @@ -167,5 +170,5 @@ std::vector split(const std::string& target, char c) { return result; } - +//#STOP_GRAB_TO_DAVIS_NAMESPACE }; // namespace davis diff --git a/common_utils/common_utils.h b/common_utils/common_utils.h index 0c9c955..3e6b23e 100644 --- a/common_utils/common_utils.h +++ b/common_utils/common_utils.h @@ -1,14 +1,16 @@ #ifndef COMMON_UTILS_COMMON_UTILS_H_ #define COMMON_UTILS_COMMON_UTILS_H_ +//#START_GRAB_TO_INCLUDES_LIST #include #include +//#STOP_GRAB_TO_INCLUDES_LIST namespace davis { - +//#START_GRAB_TO_DAVIS_NAMESPACE using std::string; using std::vector; -std::string getCurrentPath(); +string getCurrentPath(); bool isPlotlyScriptExists(); @@ -23,7 +25,7 @@ void openPlotlyHtml(const string& file_name); bool getDataFromFile(const string& path, string& result); -std::vector split(const std::string& target, char c); +vector split(const string& target, char c); bool readMatrix(vector>& outMatrix, const string& path, char dlmtr); @@ -31,6 +33,7 @@ bool readMatrix(vector>& outMatrix, const string& path, char dlmt // Now it doesn't work. bool deleteFolder(const char* fname); +//#STOP_GRAB_TO_DAVIS_NAMESPACE }; // namespace davis #endif // COMMON_UTILS_COMMON_UTILS_H_ diff --git a/davis_files.txt b/davis_files.txt new file mode 100644 index 0000000..02d14f1 --- /dev/null +++ b/davis_files.txt @@ -0,0 +1,9 @@ +common_utils/common_constants.cpp +common_utils/common_constants.h +plotly_maker/html_parts.h +plotly_maker/html_parts.cpp +common_utils/common_utils.cpp +common_utils/common_utils.h +plotly_maker/plotly_maker.h +plotly_maker/plotly_maker.cpp +array_core/array_core.h diff --git a/davis_one/davis.cpp b/davis_one/davis.cpp new file mode 100644 index 0000000..622c2d3 --- /dev/null +++ b/davis_one/davis.cpp @@ -0,0 +1,502 @@ + +//This file was generated by davis_one_maker.py script. +//Use davis_files.txt and special comments to change generated davis.h and davis.cpp + +#include "davis.h" + +#include +#include +#include +#include +#include +namespace { +using std::string; + +#ifdef _WIN32 + #include + #include + #define getcwd _getcwd // stupid MSFT "deprecation" warning +#elif __linux__ + #include +#endif + +inline bool is_file_exists(const string& file_name) { + std::ifstream file(file_name.c_str()); + if (!file) { + return false; + } + return true; +} + +void openFileBySystem(const string& file_name) { + string command; +#ifdef _WIN32 + command = "start "; +#elif __APPLE__ + command = "open "; +#elif __linux__ + command = "xdg-open "; +#else +#error "Unknown compiler" +#endif + command.append(file_name); + system(command.c_str()); +} +using std::vector; +using std::string; + + + +bool checkThatSizesAreTheSame(const vector>& values) { + size_t size = 0; + if (!values.empty()) { + size = values[0].size(); + }; + for (size_t i = 0; i < values.size(); ++i) { + + if (values[i].size() != size) + return false; + } + return true; +} + +bool createStringHeatMapValues(const vector>& values, + string& str_values) { + if (!checkThatSizesAreTheSame(values)) + return false; + if (!str_values.empty()) + str_values.clear(); + str_values.append(R"(var data = [{z: )"); + str_values.append(R"([)"); + for (size_t i = 0; i < values.size(); ++i) { + str_values.append("["); + for (size_t j = 0; j < values[i].size(); ++j) { + str_values.append(std::to_string(values[i][j])); + if (j != values[i].size() - 1) { + str_values.append(","); + } + } + str_values.append("]"); + if (i != values.size() - 1) { + str_values.append(","); + } + } + str_values.append(R"(],)"); + return true; +} + + +bool createStringLineChartValues(const vector& values, + string& str_values) { + if (!str_values.empty()) { + str_values.clear(); + } + str_values = R"(var trace = {x: [)"; + for (size_t i = 0; i < values.size(); ++i) { + str_values.append(std::to_string(i)); + if (i != values.size() - 1) { + str_values.append(","); + } + } + str_values.append("], y: ["); + for (size_t j = 0; j < values.size(); ++j) { + str_values.append(std::to_string(values[j])); + if (j != values.size() - 1) { + str_values.append(","); + } + } + str_values.append("], mode: 'lines+markers'};var data = [trace];"); + return true; +} + +inline bool heatmap_and_surface(const vector>& values, + const std::string& title, + const davis::visualizationTypes& visualType, + const davis::colorscales& colorscale) { + std::string page; + if (!createHtmlPageWithPlotlyJS(values, page, visualType, colorscale)) { + return false; + } + string pageName; + davis::mayBeCreateJsWorkingFolder(); + pageName.append("./").append(davis::kOutFolderName).append(title).append(".html"); + davis::saveStringToFile(pageName, page); + davis::openPlotlyHtml(pageName); + return true;// TODO handle different exceptions +}; + +}// namespace end +namespace davis { +const char kAppName[] = "davis"; +const char kOutFolderName[] = "davis_htmls/"; +const char kPlotlyJsWorkPath[] = "./davis_htmls/plotly-2.27.0.min.js"; +const char kPlotlyJsName[] = "plotly-2.27.0.min.js"; +const char kPlotlyJsResourcePath[] = "plotly_maker/plotly-2.27.0.min.js"; +// *INDENT-OFF* + const char kDivSizePart[] = R"(
+ + +)"; + + const char kColorMapDefaultPart[] = R"( + colorscale: [ + ['0.0', 'rgb(165,0,38)'], + ['0.111111111111', 'rgb(215,48,39)'], + ['0.222222222222', 'rgb(244,109,67)'], + ['0.333333333333', 'rgb(253,174,97)'], + ['0.444444444444', 'rgb(254,224,144)'], + ['0.555555555556', 'rgb(224,243,248)'], + ['0.666666666667', 'rgb(171,217,233)'], + ['0.777777777778', 'rgb(116,173,209)'], + ['0.888888888889', 'rgb(69,117,180)'], + ['1.0', 'rgb(49,54,149)'] + ],)"; + + + const char kColorMapSunnyPart[] = R"( + colorscale: [ + ['0.0', 'rgb(250, 134, 7)'], + ['0.2', 'rgb(250, 150, 27)'], + ['0.4', 'rgb(252, 176, 51)'], + ['0.6', 'rgb(254, 204, 81)'], + ['0.8', 'rgb(255, 228, 105)'], + ['1.0', 'rgb(255, 245, 123)'] + ],)"; + + const char kColorMapGlamourPart[] = R"( + colorscale: [ + ['0.0', 'rgb(17,63,93)'], + ['0.2', 'rgb(88,80,143)'], + ['0.4', 'rgb(182,79,145)'], + ['0.6', 'rgb(245,97,94)'], + ['0.8', 'rgb(248,165,0)'], + ['1.0', 'rgb(235,248,0)'] + ],)"; + + const char kColorMapThermalPart[] = R"( + colorscale: [ + ['0.0', 'rgb(0,0,5)'], + ['0.2', 'rgb(12,0,44)'], + ['0.4', 'rgb(41,0,148)'], + ['0.6', 'rgb(196,0,123)'], + ['0.8', 'rgb(230,61,63)'], + ['1.0', 'rgb(255,213,0)'] + ],)"; + + + const char kHeatMapTypePart[] = R"( +type: 'heatmap' +}];)"; + + const char kSurfaceTypePart[]=R"( +type: 'surface' +}];)"; + + const char kCommonLastPart[] = R"( +Plotly.newPlot('gd', data); + +)"; + +// *INDENT-ON* +string getCurrentPath() { +#if defined (_WIN32) || (__linux__) + char buffer[1024]; + char* answer = getcwd(buffer, sizeof(buffer)); + string s_cwd; + if (answer) { + s_cwd = answer; + } + return s_cwd; +#elif __APPLE__ + //TODO macos get current path implementation + return ""; +#endif +} + +bool isPlotlyScriptExists() { + return is_file_exists(kPlotlyJsWorkPath); +} + +bool saveStringToFile(const string& file_name, + const string& data) { + std::ofstream out(file_name); + if (out.is_open()) { + out << data.c_str(); + out.close(); + return true; + } + return false; +} + + +void openPlotlyHtml(const string& file_name) { + openFileBySystem(file_name); +} + +void sleepMs(unsigned long milisec) { +#ifdef _WIN32 + Sleep(milisec); +#elif __linux__ + usleep(milisec * 1000); +#endif +} + +void mayBeCreateJsWorkingFolder() { + struct stat sb; + if (stat(kOutFolderName, &sb) != 0) { +#ifdef _WIN32 + _mkdir(kOutFolderName); +#elif __linux__ + mode_t mode = 0755; + mkdir(kOutFolderName, mode); +#endif + } +} + +bool deleteFolder(const char* fname) { + struct stat sb; + if (stat(fname, &sb) == 0) { + //rmdir(fname); + return true; + } else { + return false; + } +} + +bool getDataFromFile(const string& path, string& result) { + + //TODO different scenarious and sanitizing + if (!is_file_exists(path)) { + return false; + } + if (!result.empty()) { + result.clear(); + } + std::fstream file; + file.open(path, std::ios::in); + if (file.is_open()) { + string temp; + while (std::getline(file, temp)) { + result.append(temp).append(";"); + } + } else { + return false; + } + return true; +} + +bool readMatrix(vector>& outMatrix, const std::string& path, char dlmtr) { + outMatrix.clear(); + std::ifstream ifs; + std::string str; + ifs.open(path, std::ios::in); + if (ifs) { + while (!ifs.eof()) { + std::getline(ifs, str); + if (str.size() == 0) //if exist empty line + continue; + std::vector parts = split(str, dlmtr); + vector doubleLine; + doubleLine.resize(parts.size()); + for (size_t i = 0; i < parts.size(); ++i) { + doubleLine[i] = std::stod(parts.at(i)); + } + outMatrix.push_back(doubleLine); + } + ifs.close(); + return true; + } else { + std:: cout << "Unable to open file to read: " << path << std::endl; + return false; + } +} + +vector split(const string& target, char c) { + std::string temp; + std::stringstream stringstream { target }; + std::vector result; + while (std::getline(stringstream, temp, c)) { + result.push_back(temp); + } + + return result; +} +using std::string; +using std::vector; +using std::istringstream; + +bool getMatrixValuesFromString(const string& in_values, + vector>& out_values) { + istringstream f_lines(in_values); + string lines; + while (std::getline(f_lines, lines, ';')) { + vectorvals; + istringstream f_values(lines); + string str_value; + while (std::getline(f_values, str_value, ',')) { + vals.push_back(std::stod(str_value)); + } + out_values.push_back(vals); + } + return true; +}; + +bool createHtmlPageWithPlotlyJS(const std::vector>& values, + std::string& page, + const visualizationTypes& visualType, + const colorscales& colorscale) { + page = kCommonHeadPart; + page.append(kDivSizePart); + string str_values = ""; + if (!checkThatSizesAreTheSame(values)) { + return false; + } + createStringHeatMapValues(values, str_values); + page.append(str_values); + switch (colorscale) { + case colorscales::DEFAULT: + page.append(kColorMapDefaultPart); + break; + case colorscales::SUNNY: + page.append(kColorMapSunnyPart); + break; + case colorscales::GLAMOUR: + page.append(kColorMapGlamourPart); + break; + case colorscales::THERMAL: + page.append(kColorMapThermalPart); + break; + } + switch (visualType) { + case visualizationTypes::CHART: + break; + case visualizationTypes::HEATMAP: + page.append(kHeatMapTypePart); + break; + case visualizationTypes::SURFACE: + page.append(kSurfaceTypePart); + break; + } + page.append(kCommonLastPart); + return true; +} + +bool showHeatMapInBrowser(const vector>& values, + const std::string& title, + const ShowSettingsHeatMap* settings) { + return heatmap_and_surface(values, title, settings->getVisualType(), settings->colorScale); +} + +bool showHeatMapInBrowser(const std::string& values, + const std::string& title, + const ShowSettingsHeatMap* settings) { + vector>heat_map_values; + getMatrixValuesFromString(values, heat_map_values); + showHeatMapInBrowser(heat_map_values, title, settings); + return true; +}; + +bool showLineChartInBrowser(const vector& values, + const string& title, + const ShowSettings& settings) { + string page = kCommonHeadPart; + page.append(kDivSizePart); + string str_values = ""; + createStringLineChartValues(values, str_values); + page.append(str_values); + page.append(kCommonLastPart); + string pageName; + mayBeCreateJsWorkingFolder(); + pageName.append("./").append(kOutFolderName).append(title).append(".html"); + davis::saveStringToFile(pageName, page); + openPlotlyHtml(pageName); + return true; +} + +bool showLineChartInBrowser(const string& values, + const string& title, + const ShowSettings& settings) { + vectorvals; + istringstream f(values); + string s; + while (std::getline(f, s, ',')) { + vals.push_back(std::stod(s)); + } + showLineChartInBrowser(vals, title, settings); + return true; +}; + +bool showSurfaceInBrowser(const vector>& values, + const string& title, + const ShowSettingsSurface* settings) { + return heatmap_and_surface(values, title, settings->getVisualType(), settings->colorScale); +} + +bool showSurfaceInBrowser(const std::string& values, + const string& title, + const ShowSettingsSurface* settings) { + vector>surface_values; + getMatrixValuesFromString(values, surface_values); + showSurfaceInBrowser(surface_values, title, settings); + return true; +} + +bool showLineChartInBrowser(const vector& values, + const std::string& title, + const ShowSettingsChart* settings) { + std::string page = kCommonHeadPart; + page.append(kDivSizePart); + string str_values = ""; + createStringLineChartValues(values, str_values); + page.append(str_values); + page.append(kCommonLastPart); + std::string pageName; + mayBeCreateJsWorkingFolder(); + pageName.append("./").append(kOutFolderName).append(title).append(".html"); + davis::saveStringToFile(pageName, page); + openPlotlyHtml(pageName); + return true; +} + +bool showLineChartInBrowser(const std::string& values, + const std::string& title, + const ShowSettingsChart* settings) { + std::vectorvals; + std::istringstream f(values); + std::string s; + while (std::getline(f, s, ',')) { + vals.push_back(std::stod(s)); + } + showLineChartInBrowser(vals, title, settings); + return true; +}; + +std::unique_ptr testF() { + return std::make_unique(); +} + + +std::unique_ptr createShowSettingsHeatMap(colorscales color) { + return std::make_unique(color); +} + +std::unique_ptr createShowSettingsSurface(colorscales color) { + return std::make_unique(color); +} + +std::unique_ptr createShowSettingsChart() { + return std::make_unique(); +} + +visualizationTypes ShowSettings::getVisualType() const { + return visualType; +} + +} // namespace davis end \ No newline at end of file diff --git a/davis_one/davis.h b/davis_one/davis.h new file mode 100644 index 0000000..72382b4 --- /dev/null +++ b/davis_one/davis.h @@ -0,0 +1,263 @@ + +//This file was generated by davis_one_maker.py script. +//Use davis_files.txt and special comments to change generated davis.h and davis.cpp + +#ifndef DAVIS_H_ +#define DAVIS_H_ + +#include +#include +#include +#include +namespace { + +}// namespace end +namespace davis { +extern const char kAppName[]; +extern const char kOutFolderName[]; +extern const char kPlotlyJsName[]; +extern const char kPlotlyJsResourcePath[]; +extern const char kPlotlyJsWorkPath[]; +extern const char kCommonHeadPart[]; +extern const char kDivSizePart[]; +extern const char kColorMapDefaultPart[]; +extern const char kColorMapSunnyPart[]; +extern const char kColorMapGlamourPart[]; +extern const char kColorMapThermalPart[]; +extern const char kHeatMapTypePart[]; +extern const char kSurfaceTypePart[]; +extern const char kCommonLastPart[]; +using std::string; +using std::vector; + +string getCurrentPath(); + +bool isPlotlyScriptExists(); + +bool saveStringToFile(const string& file_name, + const string& data); + +void mayBeCreateJsWorkingFolder(); + +void sleepMs(unsigned long milisec); + +void openPlotlyHtml(const string& file_name); + +bool getDataFromFile(const string& path, string& result); + +vector split(const string& target, char c); + +bool readMatrix(vector>& outMatrix, const string& path, char dlmtr); + + +// Now it doesn't work. +bool deleteFolder(const char* fname); + + +using std::vector; + +enum class visualizationTypes { + CHART, + HEATMAP, + SURFACE +}; + +enum class colorscales { + DEFAULT, + SUNNY, + GLAMOUR, + THERMAL +}; + +class ShowSettings { + public: + virtual ~ShowSettings() {} + visualizationTypes getVisualType() const; + + protected: + visualizationTypes visualType; +}; + +class ShowSettingsHeatMap : public ShowSettings { + public: + ShowSettingsHeatMap(colorscales color = colorscales::DEFAULT) { + visualType = visualizationTypes::HEATMAP; + colorScale = color; + } + colorscales colorScale; +}; + +class ShowSettingsSurface : public ShowSettings { + public: + ShowSettingsSurface(colorscales color = colorscales::DEFAULT) { + visualType = visualizationTypes::SURFACE; + colorScale = color; + } + colorscales colorScale; +}; + +class ShowSettingsChart : public ShowSettings { + public: + ShowSettingsChart() { + visualType = visualizationTypes::CHART; + } +}; + +std::unique_ptr createShowSettingsHeatMap(colorscales color = colorscales::DEFAULT); +std::unique_ptr createShowSettingsSurface(colorscales color = colorscales::DEFAULT); +std::unique_ptr createShowSettingsChart(); + +bool createHtmlPageWithPlotlyJS(const vector>& values, + std::string& page, + const visualizationTypes& visualType, + const colorscales& colorscale); + +bool showHeatMapInBrowser(const vector>& values, const std::string& title, + const ShowSettingsHeatMap* settings); + +bool showHeatMapInBrowser(const std::string& values, const std::string& title, + const ShowSettingsHeatMap* settings); + +bool showLineChartInBrowser(const vector& values, const std::string& title, + const ShowSettingsChart* settings); + +bool showLineChartInBrowser(const std::string& values, const std::string& title, + const ShowSettingsChart* settings); + +bool showSurfaceInBrowser(const vector>& values, const std::string& title, + const ShowSettingsSurface* settings); + +bool showSurfaceInBrowser(const std::string& values, const std::string& title, + const ShowSettingsSurface* settings); + +using std::vector; +using std::string; + + +//! two-dimensional vector +template +bool show(const vector>& data, const string& title = kAppName, + ShowSettings* settings = nullptr); + +//! two-dimensional array +template +bool show(T** data, uint64_t arrRows, uint64_t arrCols, + const string& title = kAppName, ShowSettings* settings = nullptr); + +//! a one-dimensional array that simulates a two-dimensional one (element access [i*cols+j]) +template +bool show(const T* data, uint64_t arrRows, uint64_t arrCols, + const string& title = kAppName, ShowSettings* settings = nullptr); + +//! one-dimensional vector +template +bool show(const vector& data, const string& title = kAppName, + ShowSettings* settings = nullptr); + +//! one-dimensional array +template +bool show(const T* data, uint64_t count, const string& title = kAppName, + ShowSettings* settings = nullptr); + +// *********************************** +// template functions implementations: +// *********************************** + +template +bool show(const vector>& data, const string& title, + ShowSettings* settings) { + vector> vecVecDbl; + vecVecDbl.reserve(data.size()); + for (vector row : data) { + vector dblRow(row.begin(), row.end()); + vecVecDbl.emplace_back(dblRow); + } + bool res = false; + std::unique_ptr smartP; //Smart pointer for auto deleting settings if it will be created bellow via NEW + if (settings == nullptr) { + settings = new ShowSettingsHeatMap(); + smartP.reset(settings); + } + if (settings->getVisualType() == davis::visualizationTypes::HEATMAP) + res = davis::showHeatMapInBrowser(vecVecDbl, title, static_cast(settings)); + else if (settings->getVisualType() == davis::visualizationTypes::SURFACE) + res = davis::showSurfaceInBrowser(vecVecDbl, title, static_cast(settings)); + return res; +} + +template +bool show(T** data, uint64_t arrRows, uint64_t arrCols, const string& title, + ShowSettings* settings) { + vector> vecVecDbl; + vecVecDbl.reserve(arrRows); + for (uint64_t i = 0; i < arrRows; ++i) { + vector dblRow(&data[i][0], &data[i][0] + arrCols); + vecVecDbl.emplace_back(dblRow); + } + bool res = false; + std::unique_ptr smartP; //Smart pointer for auto deleting settings if it will be created bellow via NEW + if (settings == nullptr) { + settings = new ShowSettingsHeatMap(); + smartP.reset(settings); + } + if (settings->getVisualType() == davis::visualizationTypes::HEATMAP) + res = davis::showHeatMapInBrowser(vecVecDbl, title, static_cast(settings)); + else if (settings->getVisualType() == davis::visualizationTypes::SURFACE) + res = davis::showSurfaceInBrowser(vecVecDbl, title, static_cast(settings)); + return res; +} + +template +bool show(const T* data, uint64_t arrRows, uint64_t arrCols, const string& title, + ShowSettings* settings) { + vector> vecVecDbl; + vecVecDbl.reserve(arrRows); + for (uint64_t i = 0; i < arrRows; ++i) { + vector dblRow(&data[i * arrCols], &data[i * arrCols] + arrCols); + vecVecDbl.emplace_back(dblRow); + } + bool res = false; + std::unique_ptr smartP; //Smart pointer for auto deleting settings if it will be created bellow via NEW + if (settings == nullptr) { + settings = new ShowSettingsHeatMap(); + smartP.reset(settings); + } + if (settings->getVisualType() == davis::visualizationTypes::HEATMAP) + res = davis::showHeatMapInBrowser(vecVecDbl, title, static_cast(settings)); + else if (settings->getVisualType() == davis::visualizationTypes::SURFACE) + res = davis::showSurfaceInBrowser(vecVecDbl, title, static_cast(settings)); + return res; +} + +template +bool show(const vector& data, const string& title, + ShowSettings* settings) { + vector dblRow(data.begin(), data.end()); + bool res = false; + std::unique_ptr smartP; //Smart pointer for auto deleting settings if it will be created bellow via NEW + if (settings == nullptr) { + settings = new ShowSettingsChart(); + smartP.reset(settings); + } + if (settings->getVisualType() == davis::visualizationTypes::CHART) + res = davis::showLineChartInBrowser(dblRow, title, static_cast(settings)); + return res; +} + +template +bool show(const T* data, uint64_t count, const string& title, + ShowSettings* settings) { + vector dblRow(data, data + count); + bool res = false; + std::unique_ptr smartP; //Smart pointer for auto deleting settings if it will be created bellow via NEW + if (settings == nullptr) { + settings = new ShowSettingsChart(); + smartP.reset(settings); + } + if (settings->getVisualType() == davis::visualizationTypes::CHART) + res = davis::showLineChartInBrowser(dblRow, title, static_cast(settings)); + return res; +} + +} // namespace davis end +#endif // DAVIS_H_ \ No newline at end of file diff --git a/davis_one_maker.py b/davis_one_maker.py new file mode 100644 index 0000000..336837d --- /dev/null +++ b/davis_one_maker.py @@ -0,0 +1,138 @@ +import os +################################################# +########### CONSTANTS ########### +################################################# +GENERATED_INFO_HEADER = """ +//This file was generated by davis_one_maker.py script. +//Use davis_files.txt and special comments to change generated davis.h and davis.cpp + +""" +START_DAVIS_H = """#ifndef DAVIS_H_ +#define DAVIS_H_ + +""" +END_DAVIS_H = """ +#endif // DAVIS_H_""" + +START_DAVIS_CPP = """#include "davis.h" + +""" +START_NAME_SPACE_DAVIS = """namespace davis { +""" +END_NAME_SPACE_DAVIS = """ +} // namespace davis end""" + +START_NAME_SPACE = """namespace { +""" +END_NAME_SPACE = """ +}// namespace end +""" +OUTPUT_H_FILE_NAME = "davis_one/davis.h" +OUTPUT_CPP_FILE_NAME = "davis_one/davis.cpp" +SRC_LIST_FILE = "davis_files.txt" +START_GRAB_INCLUDES = """#START_GRAB_TO_INCLUDES_LIST""" +STOP_GRAB_INCLUDES = """#STOP_GRAB_TO_INCLUDES_LIST""" +START_GRAB_DAVIS_NAMESPACE = """#START_GRAB_TO_DAVIS_NAMESPACE""" +STOP_GRAB_DAVIS_NAMESPACE = """#STOP_GRAB_TO_DAVIS_NAMESPACE""" +START_GRAB_NAMESPACE = """#START_GRAB_TO_NAMESPACE""" +STOP_GRAB_NAMESPACE = """#STOP_GRAB_TO_NAMESPACE""" + +################################################# +########### FUNCTIONS ########### +################################################# +def delete_file_if_exists(file_name): + if os.path.exists(file_name): + os.remove(file_name) + +def grab_namespace_code(namespace_text_start, + namespace_text_end, + start_grab_text, + stop_grab_text, + files): + file_h.write(namespace_text_start) + file_cpp.write(namespace_text_start) + is_grab_davis = False + for el in files: + file_src = open(el).read().splitlines() + for f in file_src: + if start_grab_text in f: + is_grab_davis = True + continue + if stop_grab_text in f: + is_grab_davis = False + continue + if is_grab_davis: + if '.h' in el: + file_h.write(f + "\n") + if '.cpp' in el: + file_cpp.write(f + "\n") + file_h.write(namespace_text_end) + file_cpp.write(namespace_text_end) + +print(""" +######################################## +######## DAVIS MAKER SCRIPT ######## +######################################## +""") + +includes_h_set = set("") +includes_cpp_set = set("") + +delete_file_if_exists(OUTPUT_H_FILE_NAME) +delete_file_if_exists(OUTPUT_CPP_FILE_NAME) + +file_h = open(OUTPUT_H_FILE_NAME, 'a') +file_h.write(GENERATED_INFO_HEADER) +file_h.write(START_DAVIS_H) +file_cpp = open(OUTPUT_CPP_FILE_NAME, 'a') +file_cpp.write(GENERATED_INFO_HEADER) +file_cpp.write(START_DAVIS_CPP) +is_grab_includes = False + +file_src_list = open(SRC_LIST_FILE,'r').read().splitlines() +for el in file_src_list: + #print(el) + file_src = open(el).read().splitlines() + for f in file_src: + if START_GRAB_INCLUDES in f: + is_grab_includes = True + continue + if STOP_GRAB_INCLUDES in f: + is_grab_includes = False + continue + if is_grab_includes: + if '.h' in el: + includes_h_set.add(f) + #print('h: -->',f) + if '.cpp' in el: + includes_cpp_set.add(f) + #print('cpp: -->',f) + + +print("ALL H INCLUDES: ") +for hs in includes_h_set: + print(hs) + file_h.write(hs+"\n") + +print("ALL CPP INCLUDES: ") +for cpps in includes_cpp_set: + print(cpps) + file_cpp.write(cpps+"\n") + +grab_namespace_code(START_NAME_SPACE, + END_NAME_SPACE, + START_GRAB_NAMESPACE, + STOP_GRAB_NAMESPACE, + file_src_list) + +grab_namespace_code(START_NAME_SPACE_DAVIS, + END_NAME_SPACE_DAVIS, + START_GRAB_DAVIS_NAMESPACE, + STOP_GRAB_DAVIS_NAMESPACE, + file_src_list) + + +file_h.write(END_DAVIS_H) #DAVIS HEADER_GUARD END +file_h.close() +file_cpp.close() + diff --git a/plotly_maker/html_parts.cpp b/plotly_maker/html_parts.cpp index 8ccd128..bd3d353 100644 --- a/plotly_maker/html_parts.cpp +++ b/plotly_maker/html_parts.cpp @@ -1,8 +1,8 @@ #include "html_parts.h" namespace davis { +//#START_GRAB_TO_DAVIS_NAMESPACE // *INDENT-OFF* - const char kDivSizePart[] = R"(
)"; - // *INDENT-ON* +// *INDENT-ON* +//#STOP_GRAB_TO_DAVIS_NAMESPACE } // namespace davis diff --git a/plotly_maker/html_parts.h b/plotly_maker/html_parts.h index e837cda..c11ff1c 100644 --- a/plotly_maker/html_parts.h +++ b/plotly_maker/html_parts.h @@ -2,6 +2,7 @@ #define HTML_PARTS_H namespace davis { +//#START_GRAB_TO_DAVIS_NAMESPACE extern const char kCommonHeadPart[]; extern const char kDivSizePart[]; extern const char kColorMapDefaultPart[]; @@ -11,6 +12,7 @@ extern const char kColorMapThermalPart[]; extern const char kHeatMapTypePart[]; extern const char kSurfaceTypePart[]; extern const char kCommonLastPart[]; +//#STOP_GRAB_TO_DAVIS_NAMESPACE } #endif // HTML_PARTS_H diff --git a/plotly_maker/plotly_maker.cpp b/plotly_maker/plotly_maker.cpp index ec1947e..485a447 100644 --- a/plotly_maker/plotly_maker.cpp +++ b/plotly_maker/plotly_maker.cpp @@ -1,17 +1,20 @@ +//#START_GRAB_TO_INCLUDES_LIST #include #include #include #include #include +#include +#include +//#STOP_GRAB_TO_INCLUDES_LIST + #include "html_parts.h" #include "common_utils/common_utils.h" #include "common_utils/common_constants.h" #include "plotly_maker.h" -#include -#include namespace { - +//#START_GRAB_TO_NAMESPACE using std::vector; using std::string; @@ -94,10 +97,11 @@ inline bool heatmap_and_surface(const vector>& values, davis::openPlotlyHtml(pageName); return true;// TODO handle different exceptions }; - +//#STOP_GRAB_TO_NAMESPACE } // namespace namespace davis { +//#START_GRAB_TO_DAVIS_NAMESPACE using std::string; using std::vector; using std::istringstream; @@ -268,6 +272,6 @@ std::unique_ptr createShowSettingsChart() { visualizationTypes ShowSettings::getVisualType() const { return visualType; } - +//#STOP_GRAB_TO_DAVIS_NAMESPACE }; // namespace davis diff --git a/plotly_maker/plotly_maker.h b/plotly_maker/plotly_maker.h index 2c4627b..d4d4f46 100644 --- a/plotly_maker/plotly_maker.h +++ b/plotly_maker/plotly_maker.h @@ -1,12 +1,15 @@ #ifndef PLOTLY_MAKER_PLOTLY_MAKER_H_ #define PLOTLY_MAKER_PLOTLY_MAKER_H_ -#include "vector" -#include "string" +//#START_GRAB_TO_INCLUDES_LIST +#include +#include #include #include +//#STOP_GRAB_TO_INCLUDES_LIST namespace davis { +//#START_GRAB_TO_DAVIS_NAMESPACE using std::vector; @@ -83,6 +86,7 @@ bool showSurfaceInBrowser(const vector>& values, const std::strin bool showSurfaceInBrowser(const std::string& values, const std::string& title, const ShowSettingsSurface* settings); +//#STOP_GRAB_TO_DAVIS_NAMESPACE }; // namespace davis #endif // PLOTLY_MAKER_PLOTLY_MAKER_H_