diff --git a/davis_one/davis.cpp b/davis_one/davis.cpp index 31082d6..0164b47 100644 --- a/davis_one/davis.cpp +++ b/davis_one/davis.cpp @@ -1,492 +1,498 @@ -#include "davis.h" - -#include -#include -#include -#include -#include - -namespace { - -using std::vector; -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()); -} - - -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 - -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* - -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; -} - -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 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; - } -} - -std::vector split(const std::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; -} - -} \ No newline at end of file +#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 index 6fdac4f..1aa99fb 100644 --- a/davis_one/davis.h +++ b/davis_one/davis.h @@ -1,251 +1,259 @@ -#ifndef DAVIS_H_ -#define DAVIS_H_ - -#include -#include - - -namespace davis { - -using std::vector; -using std::string; - -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[]; - - - -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); - - -//! 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; -} - -std::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); - -std::vector split(const std::string& target, char c); - -bool readMatrix(vector>& outMatrix, const string& path, char dlmtr); - -} // namespace davis - -#endif // DAVIS_H_ +#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/davis_.cpp b/davis_one/davis_.cpp deleted file mode 100644 index 7ff66bd..0000000 --- a/davis_one/davis_.cpp +++ /dev/null @@ -1,498 +0,0 @@ -#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 deleted file mode 100644 index 422cc7b..0000000 --- a/davis_one/davis_.h +++ /dev/null @@ -1,259 +0,0 @@ -#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 index 6eb65ef..bc52e48 100644 --- a/davis_one_maker.py +++ b/davis_one_maker.py @@ -22,8 +22,8 @@ END_NAME_SPACE = """ }// namespace end """ -OUTPUT_H_FILE_NAME = "davis_one/davis_.h" -OUTPUT_CPP_FILE_NAME = "davis_one/davis_.cpp" +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""" @@ -112,30 +112,20 @@ def grab_namespace_code(namespace_text_start, print(cpps) file_cpp.write(cpps+"\n") -file_h.write(START_NAME_SPACE) -file_cpp.write(START_NAME_SPACE) -is_grab_davis = False -for el in file_src_list: - file_src = open(el).read().splitlines() - for f in file_src: - if START_GRAB_NAMESPACE in f: - is_grab_davis = True - continue - if STOP_GRAB_NAMESPACE 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(END_NAME_SPACE) -file_cpp.write(END_NAME_SPACE) +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) +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() +