diff --git a/CMakeLists.txt b/CMakeLists.txt index 4f78279..c13dbbc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.15.2) # 设置项目名称与语言 -project(GCTL VERSION 1.0) +project(GCTL VERSION 1.1) # 添加配置配件编写的函数 include(CMakePackageConfigHelpers) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 27a7353..8c65443 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -5,11 +5,10 @@ set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3") set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) # 设定库源文件文件夹 -aux_source_directory(geometry/ GCTL_GEOMETRY_SRC) -aux_source_directory(algorithms/ GCTL_ALGORITHMS_SRC) +aux_source_directory(core/ GCTL_CORE_SRC) aux_source_directory(io/ GCTL_IO_SRC) aux_source_directory(utility/ GCTL_UTILITY_SRC) -aux_source_directory(maths/ GCTL_MATHS_SRC) +aux_source_directory(math/ GCTL_MATH_SRC) aux_source_directory(graphic/ GCTL_GRAPHIC_SRC) # 在windows系统下使用改编过的getopt_win源文件替代GNU getopt # 若在linux系统下则不适用此源文件 @@ -24,11 +23,11 @@ endif() # 以下部分为库的编译 # 注意目标名必须唯一 所以不能直接生成相同名称的动态库与静态库 # 注意此处不必为目标名称添加lib前缀和相应后缀,cmake会自行添加 -add_library(gctl SHARED ${GCTL_UTILITY_SRC} ${GCTL_ALGORITHMS_SRC} ${GCTL_GRAPHIC_SRC} - ${GCTL_GEOMETRY_SRC} ${GCTL_IO_SRC} ${GCTL_MATHS_SRC}) +add_library(gctl SHARED ${GCTL_CORE_SRC} ${GCTL_UTILITY_SRC} + ${GCTL_GRAPHIC_SRC} ${GCTL_IO_SRC} ${GCTL_MATH_SRC}) # 首先添加静态库的生成命令 -add_library(gctl_static STATIC ${GCTL_UTILITY_SRC} ${GCTL_ALGORITHMS_SRC} ${GCTL_GRAPHIC_SRC} - ${GCTL_GEOMETRY_SRC} ${GCTL_IO_SRC} ${GCTL_MATHS_SRC}) +add_library(gctl_static STATIC ${GCTL_CORE_SRC} ${GCTL_UTILITY_SRC} + ${GCTL_GRAPHIC_SRC} ${GCTL_IO_SRC} ${GCTL_MATH_SRC}) # 设置静态库的输出名称从而获得与动态库名称相同的静态库 set_target_properties(gctl_static PROPERTIES OUTPUT_NAME "gctl") # 设置输出目标属性以同时输出动态库与静态库 diff --git a/lib/algorithms.h b/lib/algorithms.h deleted file mode 100644 index 221d8a0..0000000 --- a/lib/algorithms.h +++ /dev/null @@ -1,48 +0,0 @@ -/******************************************************** - * ██████╗ ██████╗████████╗██╗ - * ██╔════╝ ██╔════╝╚══██╔══╝██║ - * ██║ ███╗██║ ██║ ██║ - * ██║ ██║██║ ██║ ██║ - * ╚██████╔╝╚██████╗ ██║ ███████╗ - * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ - * Geophysical Computational Tools & Library (GCTL) - * - * Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn) - * - * GCTL is distributed under a dual licensing scheme. You can redistribute - * it and/or modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either version 2 - * of the License, or (at your option) any later version. You should have - * received a copy of the GNU Lesser General Public License along with this - * program. If not, see . - * - * If the terms and conditions of the LGPL v.2. would prevent you from using - * the GCTL, please consider the option to obtain a commercial license for a - * fee. These licenses are offered by the GCTL's original author. As a rule, - * licenses are provided "as-is", unlimited in time for a one time fee. Please - * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget - * to include some description of your company and the realm of its activities. - * Also add information on how to contact you by electronic and paper mail. - ******************************************************/ - -#ifndef _GCTL_ALGORITHM_H -#define _GCTL_ALGORITHM_H - -#include "algorithms/algorithm_func.h" -#include "algorithms/glni.h" -#include "algorithms/interpolate.h" -#include "algorithms/extrapolate.h" -#include "algorithms/heap_sort.h" -#include "algorithms/boxsort2d.h" -#include "algorithms/boxsort_sph.h" -#include "algorithms/variogram.h" -#include "algorithms/fir_filter.h" -#include "algorithms/space_filter.h" -#include "algorithms/sinkhorn.h" -#include "algorithms/kde.h" -#include "algorithms/autodiff.h" -#include "algorithms/multinary.h" -#include "algorithms/emd.h" -#include "algorithms/fft_filter.h" - -#endif // _GCTL_ALGORITHM_H \ No newline at end of file diff --git a/lib/core.h b/lib/core.h index 82cf29d..324a028 100644 --- a/lib/core.h +++ b/lib/core.h @@ -37,5 +37,6 @@ #include "core/matrix.h" #include "core/spmat.h" #include "core/sparray.h" +#include "core/str.h" #endif // _GCTL_CORE_H \ No newline at end of file diff --git a/lib/core/str.cpp b/lib/core/str.cpp new file mode 100644 index 0000000..d18d82c --- /dev/null +++ b/lib/core/str.cpp @@ -0,0 +1,207 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * Geophysical Computational Tools & Library (GCTL) + * + * Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn) + * + * GCTL is distributed under a dual licensing scheme. You can redistribute + * it and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either version 2 + * of the License, or (at your option) any later version. You should have + * received a copy of the GNU Lesser General Public License along with this + * program. If not, see . + * + * If the terms and conditions of the LGPL v.2. would prevent you from using + * the GCTL, please consider the option to obtain a commercial license for a + * fee. These licenses are offered by the GCTL's original author. As a rule, + * licenses are provided "as-is", unlimited in time for a one time fee. Please + * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget + * to include some description of your company and the realm of its activities. + * Also add information on how to contact you by electronic and paper mail. + ******************************************************/ + +#include "str.h" + +//替换str中所有lod_value为new_value,返回被替换的old_value的个数 +int gctl::replace_all(std::string& new_str, const std::string& old_str,const std::string& old_value,const std::string& new_value) +{ + int count = 0; + new_str = old_str; + for(std::string::size_type pos(0);pos!=std::string::npos;pos+=new_value.length()) + { + if((pos=new_str.find(old_value,pos))!=std::string::npos) + { + new_str.replace(pos,old_value.length(),new_value); + count++; + } + else break; + } + return count; +} + +//在输入字符串末尾添加一段字符串,如果输入字符串的尾端与待添加的字符串一致则不添加并返回原字符串 +std::string gctl::patch_string(std::string in_str, std::string patch_str) +{ + int inlen = in_str.length(); + int exlen = patch_str.length(); + + std::string out_str = in_str; + + if (exlen > inlen) + { + out_str += patch_str; + return out_str; + } + + if (exlen == inlen && in_str != patch_str) + { + out_str += patch_str; + return out_str; + } + + if (in_str.substr(inlen - exlen, inlen) != patch_str) + { + out_str += patch_str; + return out_str; + } + + return out_str; +} + +//转换string对象为stringstream对象 +void gctl::str2ss(std::string in_str, std::stringstream &out_ss, std::string delimiter) +{ + if (delimiter != "") + { + std::string replace_str; + replace_all(replace_str, in_str, delimiter, " "); + out_ss.clear(); + out_ss.str(replace_str); + } + else + { + out_ss.clear(); + out_ss.str(in_str); + } + return; +} + +/** + * @brief 转换string字符串为double类型的数值 + * + * 这个函数的主要作用是检查输入字符串是否为nan或者inf等表示无效值的符号。有的编译器 + * 可以在>>输入符中完成此检测,但为了函数功能的稳定,所以在此处自定了这个函数。 + * + * @param[in] instr 输入字符串 + * + * @return 返回的double类型的数值 + */ +double gctl::str2double(std::string instr) +{ + if (instr == "NAN" || instr == "nan" || instr == "NaN") + return NAN; + else if (instr == "INF" || instr == "inf" || instr == "Inf") + return INFINITY; + else + { + auto e(instr.find_first_of("dD")); + if (e != std::string::npos) instr[e] = 'e'; + + return atof(instr.c_str()); + } +} + +/** + * @brief 转换double类型数值为string类型字符串 + * + * @param[in] in_d 输入数值 + * + * @return 输出字符串 + */ +std::string gctl::double2str(double in_d) +{ + std::string tmp_str; + std::stringstream tmp_ss; + + tmp_ss.clear(); + if (std::isnan(in_d)) + tmp_ss.str("nan"); + else if (std::isinf(in_d)) + tmp_ss.str("inf"); + else tmp_ss << in_d; + + tmp_ss >> tmp_str; + return tmp_str; +} + +//返回指定长度的随机字符串 +void gctl::random_char(unsigned int length, char* out) +{ + char str[76] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*+_-=?"; + int i,lstr; + char ss[2] = {0}; + lstr = strlen(str);//计算字符串长度 + srand((unsigned int)time((time_t *)NULL));//使用系统时间来初始化随机数发生器 + //按指定大小返回相应的字符串 + for(i = 1; i <= length; i++) + { + snprintf(ss, 2, "%c",str[(rand()%lstr)]);//rand()%lstr 可随机返回0-61之间的整数, str[0-61]可随机得到其中的字符 + strcat(out,ss);//将随机生成的字符串连接到指定数组后面 + } + return; +} + +void gctl::random_str(unsigned int length, std::string &out_str) +{ + char *out_char = new char [length]; + random_char(length, out_char); + out_str = out_char; + delete[] out_char; + return; +} + +void gctl::parse_string_with_quotes(std::string in_str, std::vector &str_vec) +{ + std::vector tmp_units; + parse_string_to_vector(in_str, ' ', tmp_units); + + // Combine strings into one if they are enclosed by quotes + std::string tmp_str = ""; + int i = 0; + while (i < tmp_units.size()) + { + if (tmp_units[i].front() == '"' && tmp_units[i].back() == '"') + { + str_vec.push_back(tmp_units[i].substr(1, tmp_units[i].length() - 1)); + i++; + } + else if (tmp_units[i].front() == '"') + { + tmp_str = tmp_units[i].substr(1, tmp_units[i].length()); + i++; + + while (tmp_units[i].back() != '"') + { + tmp_str += " " + tmp_units[i]; + i++; + } + + tmp_str += " " + tmp_units[i].substr(0, tmp_units[i].length() - 1); + i++; + + str_vec.push_back(tmp_str); + tmp_str = ""; + } + else + { + str_vec.push_back(tmp_units[i]); + i++; + } + } + return; +} \ No newline at end of file diff --git a/lib/utility/stream_t.h b/lib/core/str.h similarity index 73% rename from lib/utility/stream_t.h rename to lib/core/str.h index 4e5afd9..3eb1d5e 100644 --- a/lib/utility/stream_t.h +++ b/lib/core/str.h @@ -1,4 +1,4 @@ -/******************************************************** +/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ @@ -25,32 +25,17 @@ * Also add information on how to contact you by electronic and paper mail. ******************************************************/ -#ifndef _GCTL_STREAM_TEMPLATE_H -#define _GCTL_STREAM_TEMPLATE_H +#ifndef _GCTL_STR_H +#define _GCTL_STR_H -#include +#include "exceptions.h" + +#include +#include +#include +#include #include - -#include "../core/exceptions.h" -#include "../core/vector_t.h" - -/** - * terminal control symbols - */ -#define GCTL_BOLD "\033[1m" ///< 设置终端字体与颜色为粗体 -#define GCTL_BOLDRED "\033[1m\033[31m" ///< 设置终端字体与颜色为粗体红色 -#define GCTL_BOLDGREEN "\033[1m\033[32m" ///< 设置终端字体与颜色为粗体绿色 -#define GCTL_BOLDBLUE "\033[1m\033[34m" ///< 设置终端字体与颜色为粗体蓝色 -#define GCTL_BOLDYELLOW "\033[1m\033[33m" ///< 设置后续字符字体为黄色加粗 -#define GCTL_UNDERLINE "\033[1m\033[4m" ///< 设置终端字体为下划线 -#define GCTL_RESET "\033[0m" ///< 重置字体与颜色设定 -#define GCTL_CLEARLINE "\033[K" ///< 清空终端当前行 -#define GCTL_CLEARALL "\033[2J" ///< 清空终端窗口 -#define GCTL_MOVEUP(os, x) do {os << "\033[" << x << "A";} while (0); ///< 终端光标上移x行 -#define GCTL_MOVEDOWN(os, x) do {os << "\033[" << x << "B";} while (0); ///< 终端光标下移x行 -#define GCTL_MOVERIGHT(os, x) do {os << "\033[" << x << "C";} while (0); ///< 终端光标右移x列 -#define GCTL_MOVELEFT(os, x) do {os << "\033[" << x << "D";} while (0); ///< 终端光标左移x列 -#define GCTL_MOVETO(os, r, c) do {os << "\033[" << c << ";" << r << "H";} while (0); ///< 终端光标移动至r行c列位置 +#include namespace gctl { @@ -198,52 +183,6 @@ namespace gctl return; } - /** - * @brief Parse a string argument into an element array - * - * @param[in] val_str The input value string - * @param out_vec The output array - * @param[in] separator The separator - * - * @tparam ValueType Runtime value type - * - * @return 0 for success, -1 for failure - */ - template - int parse_string_to_vector(std::string val_str, char separator, std::vector &out_vec) - { - val_str.erase(0, val_str.find_first_not_of(" \t")); - val_str.erase(val_str.find_last_not_of(" \t") + 1); - - int pos; - bool bk = false; - ValueType val; - std::string l_str; - while (1) - { - pos = val_str.find(separator); - if (pos == val_str.npos) - { - l_str = val_str; - bk = true; - } - else - { - l_str = val_str.substr(0, pos); - val_str = val_str.substr(pos+1, val_str.length()); - val_str.erase(0, val_str.find_first_not_of(" \t")); - } - - // 如果输出也是字符串 就直接赋值即可(避免l_str中含有空格会出现bug) - if constexpr (std::is_same::value) val = l_str; - else str2type(l_str, val); - - out_vec.push_back(val); - if (bk) break; - } - return 0; - } - /** * @brief Parse a string argument into separated elements. * @@ -318,6 +257,132 @@ namespace gctl return parse_string_to_value(tmp_str, separator, throw_err, rest...) + 1; } -} -#endif // _GCTL_STREAM_TEMPLATE_H \ No newline at end of file + /** + * @brief 替换字符串中的指定子串 + * + * @param new_str 替换后的字符串 + * @param old_str 需要操作的字符串 + * @param old_value 需要被替换的字串 + * @param new_value 替换的字串 + * + * @return 替换的次数 + */ + int replace_all(std::string& new_str, const std::string& old_str, const std::string& old_value, + const std::string& new_value); + + /** + * @brief 在输入字符串末尾添加一段字符串,如果输入字符串的尾端与待添加的字符串一致则不添加并返回原字符串 + * + * @param in_str 输入字符串 + * @param patch_str 待匹配的字符串 + * @return 输出字符串 + */ + std::string patch_string(std::string in_str, std::string patch_str); + + /** + * @brief 转换string对象为stringstream对象 + * + * @param[in] in_str 输入的string字符串 + * @param out_ss 引用返回的stringstreams对象 + * @param[in] delimiter 分割string对象时的分隔符。如果不为空, + * 函数会将delimiter转换为空格 + */ + void str2ss(std::string in_str, std::stringstream &out_ss, std::string delimiter = ""); + + /** + * @brief 转换string字符串为double类型的数值 + * + * 这个函数的主要作用是检查输入字符串是否为nan或者inf等表示无效值的符号。有的编译器 + * 可以在>>输入符中完成此检测,但为了函数功能的稳定,所以在此处自定了这个函数。这个函数 + * 还可以将fortran输出文件中以D标注的科学数字转换为浮点类型 + * + * @param[in] instr 输入字符串 + * + * @return 返回的double类型的数值 + */ + double str2double(std::string instr); + + /** + * @brief 转换double类型数值为string类型字符串 标准库中有to_string函数,以后就不用这个了。 + * + * @param[in] in_d 输入数值 + * + * @return 输出字符串 + */ + std::string double2str(double in_d); + + /** + * @brief 返回一定长度的随机字符串 + * + * @param[in] length 字符串的长度 + * @param out 输出字符串 + */ + void random_char(unsigned int length, char* out); + + /** + * @brief 返回一定长度的随机字符串 + * + * @param[in] length 字符串的长度 + * @param out_str 输出字符串 + */ + void random_str(unsigned int lenght, std::string &out_str); + + /** + * @brief Parse a string argument into an element array + * + * @param[in] val_str The input value string + * @param out_vec The output array + * @param[in] separator The separator + * + * @tparam ValueType Runtime value type + * + * @return 0 for success, -1 for failure + */ + template + int parse_string_to_vector(std::string val_str, char separator, std::vector &out_vec) + { + val_str.erase(0, val_str.find_first_not_of(" \t")); + val_str.erase(val_str.find_last_not_of(" \t") + 1); + + int pos; + bool bk = false; + ValueType val; + std::string l_str; + while (1) + { + pos = val_str.find(separator); + if (pos == val_str.npos) + { + l_str = val_str; + bk = true; + } + else + { + l_str = val_str.substr(0, pos); + val_str = val_str.substr(pos+1, val_str.length()); + val_str.erase(0, val_str.find_first_not_of(" \t")); + } + + // 如果输出也是字符串 就直接赋值即可(避免l_str中含有空格会出现bug) + if constexpr (std::is_same::value) val = l_str; + else str2type(l_str, val); + + out_vec.push_back(val); + if (bk) break; + } + return 0; + } + + /** + * @brief 解析字符串并返回以空格分割的字符串向量 + * + * @note 双引号括起来的部分将解析为一个字符串 + * + * @param in_str 输入字符串 + * @param str_vec 输出字符串向量 + */ + void parse_string_with_quotes(std::string in_str, std::vector &str_vec); +}; + +#endif // _GCTL_STR_H \ No newline at end of file diff --git a/lib/geometry.h b/lib/geometry.h deleted file mode 100644 index d8fbe64..0000000 --- a/lib/geometry.h +++ /dev/null @@ -1,35 +0,0 @@ -/******************************************************** - * ██████╗ ██████╗████████╗██╗ - * ██╔════╝ ██╔════╝╚══██╔══╝██║ - * ██║ ███╗██║ ██║ ██║ - * ██║ ██║██║ ██║ ██║ - * ╚██████╔╝╚██████╗ ██║ ███████╗ - * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ - * Geophysical Computational Tools & Library (GCTL) - * - * Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn) - * - * GCTL is distributed under a dual licensing scheme. You can redistribute - * it and/or modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either version 2 - * of the License, or (at your option) any later version. You should have - * received a copy of the GNU Lesser General Public License along with this - * program. If not, see . - * - * If the terms and conditions of the LGPL v.2. would prevent you from using - * the GCTL, please consider the option to obtain a commercial license for a - * fee. These licenses are offered by the GCTL's original author. As a rule, - * licenses are provided "as-is", unlimited in time for a one time fee. Please - * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget - * to include some description of your company and the realm of its activities. - * Also add information on how to contact you by electronic and paper mail. - ******************************************************/ - -#ifndef _GCTL_GEOMETRY_H -#define _GCTL_GEOMETRY_H - -#include "geometry/refellipsoid.h" -#include "geometry/geometry2d.h" -#include "geometry/geometry3d.h" - -#endif // _GCTL_GEOMETRY_H \ No newline at end of file diff --git a/lib/graphic/cliplot.h b/lib/graphic/cliplot.h index e919978..277d594 100644 --- a/lib/graphic/cliplot.h +++ b/lib/graphic/cliplot.h @@ -36,7 +36,7 @@ #include #include -#include "../utility/stream_t.h" +#include "../io/term_io.h" namespace gctl { diff --git a/lib/graphic/gmt.h b/lib/graphic/gmt.h index 816d743..acbb4bd 100644 --- a/lib/graphic/gmt.h +++ b/lib/graphic/gmt.h @@ -34,8 +34,8 @@ #include #include "gmt/gmt.h" -#include "../utility/stream.h" #include "../core/array.h" +#include "../io/file_io.h" #include "../io/netcdf_io.h" #ifndef GMT_VF_LEN diff --git a/lib/graphic/gnuplot.h b/lib/graphic/gnuplot.h index 73b9f32..56ece74 100644 --- a/lib/graphic/gnuplot.h +++ b/lib/graphic/gnuplot.h @@ -33,7 +33,7 @@ #include #include -#include "../utility/stream.h" +#include "../io/file_io.h" namespace gctl { diff --git a/lib/io.h b/lib/io.h index 2777008..de841e6 100644 --- a/lib/io.h +++ b/lib/io.h @@ -28,18 +28,22 @@ #ifndef _GCTL_IO_H #define _GCTL_IO_H -#include "io/netcdf_io.h" -#include "io/mesh_io.h" +#include "io/term_io.h" +#include "io/file_io.h" #include "io/native_io.h" -#include "io/text_io.h" -#include "io/dsv_io.h" -#include "io/gmsh_io.h" + #include "io/surfer_io.h" -#include "io/tetgen_io.h" -#include "io/triangle_io.h" +#include "io/netcdf_io.h" + #include "io/off_io.h" #include "io/stl_io.h" #include "io/ply_io.h" -#include "io/cli_viewer.h" +#include "io/tetgen_io.h" +#include "io/triangle_io.h" +#include "io/gmsh_io.h" +#include "io/mesh_io.h" + +#include "io/text_io.h" +#include "io/dsv_io.h" #endif // _GCTL_IO_H \ No newline at end of file diff --git a/lib/io/dsv_io.cpp b/lib/io/dsv_io.cpp index 4428acb..e3357cb 100644 --- a/lib/io/dsv_io.cpp +++ b/lib/io/dsv_io.cpp @@ -26,7 +26,6 @@ ******************************************************/ #include "dsv_io.h" -#include "cli_viewer.h" gctl::dsv_io::dsv_io() { diff --git a/lib/io/dsv_io.h b/lib/io/dsv_io.h index 9f09b77..789916c 100644 --- a/lib/io/dsv_io.h +++ b/lib/io/dsv_io.h @@ -28,20 +28,20 @@ #ifndef _GCTL_DSV_IO_H #define _GCTL_DSV_IO_H -#include "../gctl_config.h" +#include "regex.h" + +#include "../core/matrix.h" +#include "../poly/point2c.h" +#include "../poly/point2p.h" +#include "../poly/point3c.h" +#include "../poly/point3s.h" +#include "file_io.h" +#include "term_io.h" + #ifdef GCTL_EXPRTK #include "exprtk.hpp" #endif // GCTL_EXPRTK -#include "regex.h" - -#include "../core.h" -#include "../utility/stream.h" -#include "../geometry/point2c.h" -#include "../geometry/point2p.h" -#include "../geometry/point3c.h" -#include "../geometry/point3s.h" - namespace gctl { enum table_cell_type diff --git a/lib/utility/stream.cpp b/lib/io/file_io.cpp similarity index 50% rename from lib/utility/stream.cpp rename to lib/io/file_io.cpp index 3988607..7bfcca2 100644 --- a/lib/utility/stream.cpp +++ b/lib/io/file_io.cpp @@ -1,4 +1,4 @@ -/******************************************************** +/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ @@ -25,166 +25,7 @@ * Also add information on how to contact you by electronic and paper mail. ******************************************************/ -#include - -#if defined _WINDOWS || __WIN32__ -#include -// Test for file existence -#define F_OK 0 -#endif - -#include "stream.h" - -//在终端显示一个简易的GCTL图标 -void gctl::display_logo(std::ostream &sout) -{ - sout << " ___ ___ _____ __\n"; - sout << " / _ \\ / __\\/__ \\ / /\n"; - sout << " / /_\\// / / /\\// /\n"; - sout << "/ /_\\\\/ /___ / / / /___\n"; - sout << "\\____/\\____/ \\/ \\____/\n"; - sout << "Geophysical Computational Tools & Library\n"; - sout << "Author: Dr. Yi Zhang (yizhang-geo@zju.edu.cn)\n\n"; - return; -} - -//替换str中所有lod_value为new_value,返回被替换的old_value的个数 -int gctl::replace_all(std::string& new_str, const std::string& old_str,const std::string& old_value,const std::string& new_value) -{ - int count = 0; - new_str = old_str; - for(std::string::size_type pos(0);pos!=std::string::npos;pos+=new_value.length()) - { - if((pos=new_str.find(old_value,pos))!=std::string::npos) - { - new_str.replace(pos,old_value.length(),new_value); - count++; - } - else break; - } - return count; -} - -//在输入字符串末尾添加一段字符串,如果输入字符串的尾端与待添加的字符串一致则不添加并返回原字符串 -std::string gctl::patch_string(std::string in_str, std::string patch_str) -{ - int inlen = in_str.length(); - int exlen = patch_str.length(); - - std::string out_str = in_str; - - if (exlen > inlen) - { - out_str += patch_str; - return out_str; - } - - if (exlen == inlen && in_str != patch_str) - { - out_str += patch_str; - return out_str; - } - - if (in_str.substr(inlen - exlen, inlen) != patch_str) - { - out_str += patch_str; - return out_str; - } - - return out_str; -} - -//转换string对象为stringstream对象 -void gctl::str2ss(std::string in_str, std::stringstream &out_ss, std::string delimiter) -{ - if (delimiter != "") - { - std::string replace_str; - replace_all(replace_str, in_str, delimiter, " "); - out_ss.clear(); - out_ss.str(replace_str); - } - else - { - out_ss.clear(); - out_ss.str(in_str); - } - return; -} - -/** - * @brief 转换string字符串为double类型的数值 - * - * 这个函数的主要作用是检查输入字符串是否为nan或者inf等表示无效值的符号。有的编译器 - * 可以在>>输入符中完成此检测,但为了函数功能的稳定,所以在此处自定了这个函数。 - * - * @param[in] instr 输入字符串 - * - * @return 返回的double类型的数值 - */ -double gctl::str2double(std::string instr) -{ - if (instr == "NAN" || instr == "nan" || instr == "NaN") - return NAN; - else if (instr == "INF" || instr == "inf" || instr == "Inf") - return INFINITY; - else - { - auto e(instr.find_first_of("dD")); - if (e != std::string::npos) instr[e] = 'e'; - - return atof(instr.c_str()); - } -} - -/** - * @brief 转换double类型数值为string类型字符串 - * - * @param[in] in_d 输入数值 - * - * @return 输出字符串 - */ -std::string gctl::double2str(double in_d) -{ - std::string tmp_str; - std::stringstream tmp_ss; - - tmp_ss.clear(); - if (std::isnan(in_d)) - tmp_ss.str("nan"); - else if (std::isinf(in_d)) - tmp_ss.str("inf"); - else tmp_ss << in_d; - - tmp_ss >> tmp_str; - return tmp_str; -} - -//返回指定长度的随机字符串 -void gctl::random_char(unsigned int length, char* out) -{ - char str[76] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz!@#$%^&*+_-=?"; - int i,lstr; - char ss[2] = {0}; - lstr = strlen(str);//计算字符串长度 - srand((unsigned int)time((time_t *)NULL));//使用系统时间来初始化随机数发生器 - //按指定大小返回相应的字符串 - for(i = 1; i <= length; i++) - { - snprintf(ss, 2, "%c",str[(rand()%lstr)]);//rand()%lstr 可随机返回0-61之间的整数, str[0-61]可随机得到其中的字符 - strcat(out,ss);//将随机生成的字符串连接到指定数组后面 - } - return; -} - -void gctl::random_str(unsigned int length, std::string &out_str) -{ - char *out_char = new char [length]; - random_char(length, out_char); - out_str = out_char; - delete[] out_char; - return; -} +#include "file_io.h" //测试打开输入文件 如果成功则返回0并输出信息 否则返回1 void gctl::open_infile(std::ifstream &infile, std::string filename, std::string extension, @@ -192,7 +33,7 @@ void gctl::open_infile(std::ifstream &infile, std::string filename, std::string { if (filename == "") { - throw domain_error("Empty file name. From open_infile(...)"); + throw std::domain_error("Empty file name. From open_infile(...)"); } // 文件流开启则先关闭文件 @@ -209,7 +50,7 @@ void gctl::open_infile(std::ifstream &infile, std::string filename, std::string infile.open(filename+extension, mode); if (!infile) { - throw domain_error("Fail to open file: " + filename + extension + ". From open_infile(...)"); + throw gctl::domain_error("Fail to open file: " + filename + extension + ". From open_infile(...)"); } return; } @@ -219,7 +60,7 @@ void gctl::open_infile(std::ifstream &infile, std::string filename, std::string infile.open(filename, mode); if (!infile) { - throw domain_error("Fail to open file: " + filename + ". From open_infile(...)"); + throw gctl::domain_error("Fail to open file: " + filename + ". From open_infile(...)"); } return; } @@ -229,7 +70,7 @@ void gctl::open_matched_infile(std::ifstream &infile, std::string filename, { if (exten_pattern == "" || filename == "") { - throw domain_error("Empty file name or extension(s). From open_matched_infile(...)"); + throw std::domain_error("Empty file name or extension(s). From open_matched_infile(...)"); } if (infile.is_open()) infile.close(); @@ -354,81 +195,6 @@ void gctl::open_matched_outfile(std::ofstream &outfile, std::string filename, return; } -int gctl::terminal_width() -{ - int width; - //获取终端窗口的行列数 -#ifdef _WINDOWS - CONSOLE_SCREEN_BUFFER_INFO csbi; - GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); - width = csbi.srWindow.Right - csbi.srWindow.Left + 1; -#else - struct winsize w; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - width = w.ws_col; -#endif - - return width; -} - -int gctl::terminal_height() -{ - int height; - //获取终端窗口的行列数 -#ifdef _WINDOWS - CONSOLE_SCREEN_BUFFER_INFO csbi; - GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); - height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; -#else - struct winsize w; - ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); - height = w.ws_row; -#endif - - return height; -} - -void gctl::parse_string_with_quotes(std::string in_str, std::vector &str_vec) -{ - std::vector tmp_units; - parse_string_to_vector(in_str, ' ', tmp_units); - - // Combine strings into one if they are enclosed by quotes - std::string tmp_str = ""; - int i = 0; - while (i < tmp_units.size()) - { - if (tmp_units[i].front() == '"' && tmp_units[i].back() == '"') - { - str_vec.push_back(tmp_units[i].substr(1, tmp_units[i].length() - 1)); - i++; - } - else if (tmp_units[i].front() == '"') - { - tmp_str = tmp_units[i].substr(1, tmp_units[i].length()); - i++; - - while (tmp_units[i].back() != '"') - { - tmp_str += " " + tmp_units[i]; - i++; - } - - tmp_str += " " + tmp_units[i].substr(0, tmp_units[i].length() - 1); - i++; - - str_vec.push_back(tmp_str); - tmp_str = ""; - } - else - { - str_vec.push_back(tmp_units[i]); - i++; - } - } - return; -} - void gctl::parse_filename(std::string filename, std::string &naked_name, std::string &exten_name) { naked_name = filename.substr(0, filename.rfind(".")); diff --git a/lib/utility/stream.h b/lib/io/file_io.h similarity index 51% rename from lib/utility/stream.h rename to lib/io/file_io.h index 6778305..fdeba67 100644 --- a/lib/utility/stream.h +++ b/lib/io/file_io.h @@ -1,4 +1,4 @@ -/******************************************************** +/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ @@ -24,104 +24,15 @@ * to include some description of your company and the realm of its activities. * Also add information on how to contact you by electronic and paper mail. ******************************************************/ -#ifndef _GCTL_STREAM_H -#define _GCTL_STREAM_H -// system's head files -#include -#include -#include -#include -#include -#include +#ifndef _GCTL_FILE_IO_H +#define _GCTL_FILE_IO_H -#ifdef _WINDOWS -#include -#else -#include -#endif - -// library's head files -#include "stream_t.h" +#include "../core/str.h" namespace gctl { - /** - * @brief 在终端显示一个简易的GCTL图标 - */ - void display_logo(std::ostream &sout = std::cout); - - /** - * @brief 替换字符串中的指定子串 - * - * @param new_str 替换后的字符串 - * @param old_str 需要操作的字符串 - * @param old_value 需要被替换的字串 - * @param new_value 替换的字串 - * - * @return 替换的次数 - */ - int replace_all(std::string& new_str, const std::string& old_str, const std::string& old_value, - const std::string& new_value); - - /** - * @brief 在输入字符串末尾添加一段字符串,如果输入字符串的尾端与待添加的字符串一致则不添加并返回原字符串 - * - * @param in_str 输入字符串 - * @param patch_str 待匹配的字符串 - * @return 输出字符串 - */ - std::string patch_string(std::string in_str, std::string patch_str); - - /** - * @brief 转换string对象为stringstream对象 - * - * @param[in] in_str 输入的string字符串 - * @param out_ss 引用返回的stringstreams对象 - * @param[in] delimiter 分割string对象时的分隔符。如果不为空, - * 函数会将delimiter转换为空格 - */ - void str2ss(std::string in_str, std::stringstream &out_ss, std::string delimiter = ""); - - /** - * @brief 转换string字符串为double类型的数值 - * - * 这个函数的主要作用是检查输入字符串是否为nan或者inf等表示无效值的符号。有的编译器 - * 可以在>>输入符中完成此检测,但为了函数功能的稳定,所以在此处自定了这个函数。这个函数 - * 还可以将fortran输出文件中以D标注的科学数字转换为浮点类型 - * - * @param[in] instr 输入字符串 - * - * @return 返回的double类型的数值 - */ - double str2double(std::string instr); - - /** - * @brief 转换double类型数值为string类型字符串 标准库中有to_string函数,以后就不用这个了。 - * - * @param[in] in_d 输入数值 - * - * @return 输出字符串 - */ - std::string double2str(double in_d); - - /** - * @brief 返回一定长度的随机字符串 - * - * @param[in] length 字符串的长度 - * @param out 输出字符串 - */ - void random_char(unsigned int length, char* out); - - /** - * @brief 返回一定长度的随机字符串 - * - * @param[in] length 字符串的长度 - * @param out_str 输出字符串 - */ - void random_str(unsigned int lenght, std::string &out_str); - - /** + /** * @brief 打开输入文件 * * @param infile ifstream对象 @@ -165,30 +76,6 @@ namespace gctl void open_matched_outfile(std::ofstream &outfile, std::string filename, std::string exten_pattern, std::ios_base::openmode mode = std::ios_base::out); - /** - * @brief 返回当前终端窗口的字符宽度 - * - * @return 宽度 - */ - int terminal_width(); - - /** - * @brief 返回当前终端窗口的字符高度 - * - * @return 高度 - */ - int terminal_height(); - - /** - * @brief 解析字符串并返回以空格分割的字符串向量 - * - * @note 双引号括起来的部分将解析为一个字符串 - * - * @param in_str 输入字符串 - * @param str_vec 输出字符串向量 - */ - void parse_string_with_quotes(std::string in_str, std::vector &str_vec); - /** * @brief 解析文件路径 * @@ -199,4 +86,4 @@ namespace gctl void parse_filename(std::string filename, std::string &naked_name, std::string &exten_name); }; -#endif // _GCTL_STREAM_H \ No newline at end of file +#endif // _GCTL_FILE_IO_H \ No newline at end of file diff --git a/lib/io/gmsh_io.h b/lib/io/gmsh_io.h index 99cb155..0a1c6ec 100644 --- a/lib/io/gmsh_io.h +++ b/lib/io/gmsh_io.h @@ -29,9 +29,21 @@ #define _GCTL_GMSH_IO_H // library's head files -#include "../core.h" -#include "../geometry.h" -#include "../utility.h" +#include "../core/matrix.h" +#include "../poly/node.h" +#include "../poly/sphere.h" +#include "../poly/triangle2d.h" +#include "../poly/triangle2d2o.h" +#include "../poly/rectangle2d.h" +#include "../poly/edge2d.h" +#include "../poly/block.h" +#include "../poly/prism.h" +#include "../poly/tesseroid.h" +#include "../poly/tetrahedron.h" +#include "../poly/tri_cone.h" +#include "../poly/triangle.h" +#include "../poly/edge.h" +#include "file_io.h" #ifdef GCTL_EIGEN #include "Eigen/Dense" diff --git a/lib/io/mesh_io.cpp b/lib/io/mesh_io.cpp index 687b4c2..fcac492 100644 --- a/lib/io/mesh_io.cpp +++ b/lib/io/mesh_io.cpp @@ -1821,7 +1821,7 @@ void gctl::mesh_io::save_data_to_xyz(std::string filename, std::string dataname, { if (out_coor == Spherical) { - ps = vptr->c2s(); + ps = c2s(*vptr); ps.rad -= ellipse_radius_2d(refR, refr, ps.lat*GCTL_Pi/180.0); ofile << ps.lon << " " << ps.lat << " " << ps.rad << " " << datas_[i].val[n] << "\n"; @@ -1852,7 +1852,7 @@ void gctl::mesh_io::save_data_to_xyz(std::string filename, std::string dataname, if (out_coor == Spherical) { - ps = pc.c2s(); + ps = c2s(pc); ps.rad -= ellipse_radius_2d(refR, refr, ps.lat*GCTL_Pi/180.0); ofile << ps.lon << " " << ps.lat << " " << ps.rad << " " << datas_[i].val[e] << "\n"; diff --git a/lib/io/mesh_io.h b/lib/io/mesh_io.h index 5832001..2698039 100644 --- a/lib/io/mesh_io.h +++ b/lib/io/mesh_io.h @@ -29,16 +29,12 @@ #define _GCTL_MESH_IO_H #include +#include -// library's head files -#include "../core.h" -#include "../geometry.h" -#include "../utility.h" - +#include "../math/refellipsoid.h" #include "triangle_io.h" #include "tetgen_io.h" #include "gmsh_io.h" -#include "map" namespace gctl { diff --git a/lib/io/native_io.h b/lib/io/native_io.h index 91fcbb8..1e3ed29 100644 --- a/lib/io/native_io.h +++ b/lib/io/native_io.h @@ -28,8 +28,8 @@ #ifndef _GCTL_NATIVE_IO_H #define _GCTL_NATIVE_IO_H -#include "../core.h" -#include "../utility.h" +#include "../core/spmat.h" +#include "file_io.h" namespace gctl { diff --git a/lib/io/netcdf_io.h b/lib/io/netcdf_io.h index 3e0e009..6d978b8 100644 --- a/lib/io/netcdf_io.h +++ b/lib/io/netcdf_io.h @@ -28,18 +28,14 @@ #ifndef _GCTL_NETCDF_IO_H #define _GCTL_NETCDF_IO_H -// get library configuration -#include "../gctl_config.h" +#include "../core/matrix.h" + #ifdef GCTL_NETCDF // netcdf cpp head file #include "netcdfcxx_legacy/netcdfcpp.h" #include "typeinfo" -#include "../core/array.h" -#include "../core/matrix.h" -#include "../utility/stream.h" - namespace gctl { /** diff --git a/lib/io/off_io.h b/lib/io/off_io.h index b9cb56f..28afe79 100644 --- a/lib/io/off_io.h +++ b/lib/io/off_io.h @@ -28,10 +28,8 @@ #ifndef _GCTL_OFF_IO_H #define _GCTL_OFF_IO_H -// library's head file -#include "../core.h" -#include "../utility.h" -#include "../geometry.h" +#include "../poly/triangle.h" +#include "file_io.h" namespace gctl { diff --git a/lib/io/ply_io.h b/lib/io/ply_io.h index be70d0c..9d869b6 100644 --- a/lib/io/ply_io.h +++ b/lib/io/ply_io.h @@ -28,10 +28,8 @@ #ifndef _GCTL_PLY_IO_H #define _GCTL_PLY_IO_H -// library's head file -#include "../core.h" -#include "../utility.h" -#include "../geometry.h" +#include "../poly/triangle.h" +#include "file_io.h" namespace gctl { diff --git a/lib/io/stl_io.h b/lib/io/stl_io.h index a976954..045da35 100644 --- a/lib/io/stl_io.h +++ b/lib/io/stl_io.h @@ -28,10 +28,8 @@ #ifndef _GCTL_STL_IO_H #define _GCTL_STL_IO_H -// library's head file -#include "../core.h" -#include "../utility.h" -#include "../geometry.h" +#include "../poly/triangle.h" +#include "file_io.h" namespace gctl { diff --git a/lib/io/surfer_io.h b/lib/io/surfer_io.h index a125495..33332f9 100644 --- a/lib/io/surfer_io.h +++ b/lib/io/surfer_io.h @@ -29,8 +29,8 @@ #define _GCTL_SURFER_IO_H // library's head files -#include "../core.h" -#include "../utility.h" +#include "../core/matrix.h" +#include "file_io.h" namespace gctl { diff --git a/lib/io/cli_viewer.cpp b/lib/io/term_io.cpp similarity index 76% rename from lib/io/cli_viewer.cpp rename to lib/io/term_io.cpp index 09f5afe..52a3330 100644 --- a/lib/io/cli_viewer.cpp +++ b/lib/io/term_io.cpp @@ -25,7 +25,60 @@ * Also add information on how to contact you by electronic and paper mail. ******************************************************/ -#include "cli_viewer.h" +#if defined _WINDOWS || __WIN32__ +#include +// Test for file existence +#define F_OK 0 +#endif + +#include "term_io.h" + +//在终端显示一个简易的GCTL图标 +void gctl::display_logo(std::ostream &sout) +{ + sout << " ___ ___ _____ __\n"; + sout << " / _ \\ / __\\/__ \\ / /\n"; + sout << " / /_\\// / / /\\// /\n"; + sout << "/ /_\\\\/ /___ / / / /___\n"; + sout << "\\____/\\____/ \\/ \\____/\n"; + sout << "Geophysical Computational Tools & Library\n"; + sout << "Author: Dr. Yi Zhang (yizhang-geo@zju.edu.cn)\n\n"; + return; +} + +int gctl::terminal_width() +{ + int width; + //获取终端窗口的行列数 +#ifdef _WINDOWS + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + width = csbi.srWindow.Right - csbi.srWindow.Left + 1; +#else + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + width = w.ws_col; +#endif + + return width; +} + +int gctl::terminal_height() +{ + int height; + //获取终端窗口的行列数 +#ifdef _WINDOWS + CONSOLE_SCREEN_BUFFER_INFO csbi; + GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi); + height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1; +#else + struct winsize w; + ioctl(STDOUT_FILENO, TIOCGWINSZ, &w); + height = w.ws_row; +#endif + + return height; +} gctl::cli_viewer::cli_viewer() { diff --git a/lib/io/term_io.h b/lib/io/term_io.h new file mode 100644 index 0000000..cf70a32 --- /dev/null +++ b/lib/io/term_io.h @@ -0,0 +1,103 @@ +/******************************************************** + * ██████╗ ██████╗████████╗██╗ + * ██╔════╝ ██╔════╝╚══██╔══╝██║ + * ██║ ███╗██║ ██║ ██║ + * ██║ ██║██║ ██║ ██║ + * ╚██████╔╝╚██████╗ ██║ ███████╗ + * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ + * Geophysical Computational Tools & Library (GCTL) + * + * Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn) + * + * GCTL is distributed under a dual licensing scheme. You can redistribute + * it and/or modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation, either version 2 + * of the License, or (at your option) any later version. You should have + * received a copy of the GNU Lesser General Public License along with this + * program. If not, see . + * + * If the terms and conditions of the LGPL v.2. would prevent you from using + * the GCTL, please consider the option to obtain a commercial license for a + * fee. These licenses are offered by the GCTL's original author. As a rule, + * licenses are provided "as-is", unlimited in time for a one time fee. Please + * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget + * to include some description of your company and the realm of its activities. + * Also add information on how to contact you by electronic and paper mail. + ******************************************************/ + +#ifndef _GCTL_TERM_IO_H +#define _GCTL_TERM_IO_H + +#include +#include +#include +#include +#include +#include + +#ifdef _WINDOWS +#include +#else +#include +#endif + +/** + * terminal control symbols + */ +#define GCTL_BOLD "\033[1m" ///< 设置终端字体与颜色为粗体 +#define GCTL_BOLDRED "\033[1m\033[31m" ///< 设置终端字体与颜色为粗体红色 +#define GCTL_BOLDGREEN "\033[1m\033[32m" ///< 设置终端字体与颜色为粗体绿色 +#define GCTL_BOLDBLUE "\033[1m\033[34m" ///< 设置终端字体与颜色为粗体蓝色 +#define GCTL_BOLDYELLOW "\033[1m\033[33m" ///< 设置后续字符字体为黄色加粗 +#define GCTL_UNDERLINE "\033[1m\033[4m" ///< 设置终端字体为下划线 +#define GCTL_RESET "\033[0m" ///< 重置字体与颜色设定 +#define GCTL_CLEARLINE "\033[K" ///< 清空终端当前行 +#define GCTL_CLEARALL "\033[2J" ///< 清空终端窗口 +#define GCTL_MOVEUP(os, x) do {os << "\033[" << x << "A";} while (0); ///< 终端光标上移x行 +#define GCTL_MOVEDOWN(os, x) do {os << "\033[" << x << "B";} while (0); ///< 终端光标下移x行 +#define GCTL_MOVERIGHT(os, x) do {os << "\033[" << x << "C";} while (0); ///< 终端光标右移x列 +#define GCTL_MOVELEFT(os, x) do {os << "\033[" << x << "D";} while (0); ///< 终端光标左移x列 +#define GCTL_MOVETO(os, r, c) do {os << "\033[" << c << ";" << r << "H";} while (0); ///< 终端光标移动至r行c列位置 + +namespace gctl +{ + /** + * @brief 在终端显示一个简易的GCTL图标 + */ + void display_logo(std::ostream &sout = std::cout); + + /** + * @brief 返回当前终端窗口的字符宽度 + * + * @return 宽度 + */ + int terminal_width(); + + /** + * @brief 返回当前终端窗口的字符高度 + * + * @return 高度 + */ + int terminal_height(); + + // 命令行查看器,用于在终端内查看文本文件内容,支持上下左右鼠标滚动或按键导航和退出查看 + class cli_viewer + { + public: + // 构造函数 + cli_viewer(); + // 析构函数 + ~cli_viewer(); + // 设置显示内容 + void setData(const std::vector& data); + // 添加显示内容 + void addData(const std::string& l); + // 显示文件内容并进入查看循环 + void display(); + + private: + std::vector lines; // 存储文件内容 + }; +}; + +#endif // _GCTL_TERM_IO_H \ No newline at end of file diff --git a/lib/io/tetgen_io.h b/lib/io/tetgen_io.h index 8eb9b1c..fa347a9 100644 --- a/lib/io/tetgen_io.h +++ b/lib/io/tetgen_io.h @@ -28,10 +28,11 @@ #ifndef _GCTL_TETGEN_IO_H #define _GCTL_TETGEN_IO_H -// library's head file -#include "../core.h" -#include "../utility.h" -#include "../geometry.h" +#include "../core/matrix.h" +#include "../poly/triangle.h" +#include "../poly/tetrahedron.h" +#include "../poly/edge.h" +#include "file_io.h" namespace gctl { diff --git a/lib/io/text_io.h b/lib/io/text_io.h index 4103222..53700d7 100644 --- a/lib/io/text_io.h +++ b/lib/io/text_io.h @@ -28,11 +28,11 @@ #ifndef _GCTL_TEXT_IO_H #define _GCTL_TEXT_IO_H -// library's head file -#include "../core.h" -#include "../utility.h" -#include "../geometry.h" -// system head file +#include "../core/matrix.h" +#include "../poly/point3c.h" +#include "../poly/point3s.h" +#include "file_io.h" + #include "initializer_list" namespace gctl diff --git a/lib/io/triangle_io.h b/lib/io/triangle_io.h index 78000a2..e19255e 100644 --- a/lib/io/triangle_io.h +++ b/lib/io/triangle_io.h @@ -29,9 +29,11 @@ #define _GCTL_TRIANGLE_IO_H // library's head file -#include "../core.h" -#include "../utility.h" -#include "../geometry.h" +#include "../core/matrix.h" +#include "../poly/triangle2d.h" +#include "../poly/triangle2d2o.h" +#include "../poly/edge2d.h" +#include "file_io.h" namespace gctl { diff --git a/lib/io/cli_viewer.h b/lib/math.h similarity index 67% rename from lib/io/cli_viewer.h rename to lib/math.h index 6c694db..8988383 100644 --- a/lib/io/cli_viewer.h +++ b/lib/math.h @@ -25,34 +25,44 @@ * Also add information on how to contact you by electronic and paper mail. ******************************************************/ -#ifndef _GCTL_CLI_VIEWER_H -#define _GCTL_CLI_VIEWER_H +#ifndef _GCTL_MATH_H +#define _GCTL_MATH_H -#include -#include -#include -#include +#include "math/mathfunc.h" +#include "math/algorithm_func.h" -namespace gctl -{ - // 命令行查看器,用于在终端内查看文本文件内容,支持上下左右鼠标滚动或按键导航和退出查看 - class cli_viewer - { - public: - // 构造函数 - cli_viewer(); - // 析构函数 - ~cli_viewer(); - // 设置显示内容 - void setData(const std::vector& data); - // 添加显示内容 - void addData(const std::string& l); - // 显示文件内容并进入查看循环 - void display(); +#include "math/legendre.h" +#include "math/linear_algebra.h" - private: - std::vector lines; // 存储文件内容 - }; -}; +#include "math/heap_sort.h" +#include "math/boxsort2d.h" +#include "math/boxsort_sph.h" +#include "math/variogram.h" -#endif // _GCTL_CLI_VIEWER_H \ No newline at end of file +#include "math/interpolate.h" +#include "math/extrapolate.h" + +#include "math/gaussfunc.h" +#include "math/sinkhorn.h" +#include "math/kde.h" +#include "math/autodiff.h" +#include "math/multinary.h" +#include "math/emd.h" + +#include "math/geometry2d.h" +#include "math/geometry3d.h" +#include "math/refellipsoid.h" + +#include "math/mathfunc_ext.h" +#include "math/space_filter.h" + +#include "math/windowfunc.h" +#include "math/fir_filter.h" + +#include "math/fft.h" +#include "math/fft_filter.h" + +#include "math/shapefunc.h" +#include "math/glni.h" + +#endif // _GCTL_MATH_H \ No newline at end of file diff --git a/lib/algorithms/algorithm_func.cpp b/lib/math/algorithm_func.cpp similarity index 100% rename from lib/algorithms/algorithm_func.cpp rename to lib/math/algorithm_func.cpp diff --git a/lib/algorithms/algorithm_func.h b/lib/math/algorithm_func.h similarity index 98% rename from lib/algorithms/algorithm_func.h rename to lib/math/algorithm_func.h index bc39580..3f5909b 100644 --- a/lib/algorithms/algorithm_func.h +++ b/lib/math/algorithm_func.h @@ -28,9 +28,7 @@ #ifndef _GCTL_ALGORITHM_FUNC_H #define _GCTL_ALGORITHM_FUNC_H -#include "../core/array.h" -#include "../core/matrix.h" -#include "../maths/mathfunc.h" +#include "mathfunc.h" namespace gctl { diff --git a/lib/algorithms/autodiff.cpp b/lib/math/autodiff.cpp similarity index 100% rename from lib/algorithms/autodiff.cpp rename to lib/math/autodiff.cpp diff --git a/lib/algorithms/autodiff.h b/lib/math/autodiff.h similarity index 100% rename from lib/algorithms/autodiff.h rename to lib/math/autodiff.h diff --git a/lib/algorithms/boxsort2d.h b/lib/math/boxsort2d.h similarity index 99% rename from lib/algorithms/boxsort2d.h rename to lib/math/boxsort2d.h index 933ea46..3e03aa4 100644 --- a/lib/algorithms/boxsort2d.h +++ b/lib/math/boxsort2d.h @@ -28,7 +28,6 @@ #ifndef _BOXSORT2D_H #define _BOXSORT2D_H -#include "../core.h" #include "heap_sort.h" namespace gctl diff --git a/lib/algorithms/boxsort_sph.h b/lib/math/boxsort_sph.h similarity index 99% rename from lib/algorithms/boxsort_sph.h rename to lib/math/boxsort_sph.h index 78a5fe3..3fd1266 100644 --- a/lib/algorithms/boxsort_sph.h +++ b/lib/math/boxsort_sph.h @@ -28,7 +28,6 @@ #ifndef _BOXSORT_SPH_H #define _BOXSORT_SPH_H -#include "../core.h" #include "heap_sort.h" namespace gctl diff --git a/lib/algorithms/emd.cpp b/lib/math/emd.cpp similarity index 100% rename from lib/algorithms/emd.cpp rename to lib/math/emd.cpp diff --git a/lib/algorithms/emd.h b/lib/math/emd.h similarity index 97% rename from lib/algorithms/emd.h rename to lib/math/emd.h index db1134c..a54b961 100644 --- a/lib/algorithms/emd.h +++ b/lib/math/emd.h @@ -29,8 +29,6 @@ #define _GCTL_EMD_H // library head file -#include "../gctl_config.h" -#include "../core/array.h" #include "../core/matrix.h" #ifdef GCTL_EEMD diff --git a/lib/algorithms/extrapolate.cpp b/lib/math/extrapolate.cpp similarity index 100% rename from lib/algorithms/extrapolate.cpp rename to lib/math/extrapolate.cpp diff --git a/lib/algorithms/extrapolate.h b/lib/math/extrapolate.h similarity index 98% rename from lib/algorithms/extrapolate.h rename to lib/math/extrapolate.h index b597d0a..8c2b439 100644 --- a/lib/algorithms/extrapolate.h +++ b/lib/math/extrapolate.h @@ -28,9 +28,7 @@ #ifndef _GCTL_EXTRAPOLATE_H #define _GCTL_EXTRAPOLATE_H -#include "../core/array.h" -#include "../core/matrix.h" -#include "../maths/mathfunc_t.h" +#include "mathfunc.h" namespace gctl { diff --git a/lib/maths/fft.cpp b/lib/math/fft.cpp similarity index 100% rename from lib/maths/fft.cpp rename to lib/math/fft.cpp diff --git a/lib/maths/fft.h b/lib/math/fft.h similarity index 100% rename from lib/maths/fft.h rename to lib/math/fft.h diff --git a/lib/algorithms/fft_filter.cpp b/lib/math/fft_filter.cpp similarity index 100% rename from lib/algorithms/fft_filter.cpp rename to lib/math/fft_filter.cpp diff --git a/lib/algorithms/fft_filter.h b/lib/math/fft_filter.h similarity index 98% rename from lib/algorithms/fft_filter.h rename to lib/math/fft_filter.h index 63bff6a..8604428 100644 --- a/lib/algorithms/fft_filter.h +++ b/lib/math/fft_filter.h @@ -28,9 +28,8 @@ #ifndef _FFT_FILTER_H #define _FFT_FILTER_H -#include "../gctl_config.h" #include "../core/array.h" -#include "../maths/fft.h" +#include "fft.h" #include "extrapolate.h" #ifdef GCTL_FFTW3 diff --git a/lib/algorithms/fir_filter.cpp b/lib/math/fir_filter.cpp similarity index 100% rename from lib/algorithms/fir_filter.cpp rename to lib/math/fir_filter.cpp diff --git a/lib/algorithms/fir_filter.h b/lib/math/fir_filter.h similarity index 99% rename from lib/algorithms/fir_filter.h rename to lib/math/fir_filter.h index 011e37a..7a564d9 100644 --- a/lib/algorithms/fir_filter.h +++ b/lib/math/fir_filter.h @@ -28,7 +28,6 @@ #ifndef _RIF_FILTER_H #define _RIF_FILTER_H -#include "../core.h" #include "windowfunc.h" namespace gctl diff --git a/lib/maths/gaussfunc.cpp b/lib/math/gaussfunc.cpp similarity index 100% rename from lib/maths/gaussfunc.cpp rename to lib/math/gaussfunc.cpp diff --git a/lib/maths/gaussfunc.h b/lib/math/gaussfunc.h similarity index 100% rename from lib/maths/gaussfunc.h rename to lib/math/gaussfunc.h diff --git a/lib/geometry/geometry2d.cpp b/lib/math/geometry2d.cpp similarity index 100% rename from lib/geometry/geometry2d.cpp rename to lib/math/geometry2d.cpp diff --git a/lib/geometry/geometry2d.h b/lib/math/geometry2d.h similarity index 98% rename from lib/geometry/geometry2d.h rename to lib/math/geometry2d.h index a8a7fd2..59e316a 100644 --- a/lib/geometry/geometry2d.h +++ b/lib/math/geometry2d.h @@ -28,14 +28,7 @@ #ifndef _GCTL_GEOMETRY2D_H #define _GCTL_GEOMETRY2D_H -// library's head files -#include "../core/array.h" -#include "../maths.h" - -#include "triangle2d.h" -#include "triangle2d2o.h" -#include "edge2d.h" -#include "rectangle2d.h" +#include "../poly/edge2d.h" namespace gctl { diff --git a/lib/geometry/geometry3d.cpp b/lib/math/geometry3d.cpp similarity index 99% rename from lib/geometry/geometry3d.cpp rename to lib/math/geometry3d.cpp index bc87f46..c70c091 100644 --- a/lib/geometry/geometry3d.cpp +++ b/lib/math/geometry3d.cpp @@ -111,7 +111,7 @@ void gctl::geometry3d::get_plane_coeff(double x1, double x2, double x3, double y void gctl::geometry3d::get_plane_coeff(const point3ds &p1, const point3ds &p2, double &A, double &B, double &C, double &D) { - gctl::point3dc c1 = p1.s2c(), c2 = p2.s2c(); + gctl::point3dc c1 = s2c(p1), c2 = s2c(p2); get_plane_coeff(c1.x, c2.x, 0.0, c1.y, c2.y, 0.0, c1.z, c2.z, 0.0, A, B, C, D); return; } @@ -136,11 +136,11 @@ gctl::point3ds gctl::geometry3d::track_sphere_arc(const point3ds &v1, const poin throw std::runtime_error("[gctl::geometry3d::track_sphere_arc] Invalid radius."); } - point3dc p1 = v1.s2c(), p2 = v2.s2c(); + point3dc p1 = s2c(v1), p2 = s2c(v2); double Phi = angle(p1, p2); // Phi为弧度表示 point3dc pc = cos(arc)*p1 + sin(arc)/sin(Phi)*(p2 - cos(Phi)*p1); - return pc.c2s(); + return c2s(pc); } gctl::point3dc gctl::geometry3d::dot_on_plane(const point3dc &face_dot, const point3dc &face_nor, diff --git a/lib/geometry/geometry3d.h b/lib/math/geometry3d.h similarity index 97% rename from lib/geometry/geometry3d.h rename to lib/math/geometry3d.h index 70d02a9..21e3fe6 100644 --- a/lib/geometry/geometry3d.h +++ b/lib/math/geometry3d.h @@ -28,21 +28,9 @@ #ifndef _GCTL_GEOMETRY3D_H #define _GCTL_GEOMETRY3D_H -// library's head file -#include "../core/array.h" -#include "../core/matrix.h" -#include "../maths.h" - -#include "tensor.h" -#include "node.h" -#include "edge.h" -#include "tetrahedron.h" -#include "triangle.h" -#include "block.h" -#include "tri_cone.h" -#include "sphere.h" -#include "tesseroid.h" -#include "prism.h" +#include "../poly/triangle.h" +#include "../poly/tetrahedron.h" +#include "mathfunc.h" namespace gctl { diff --git a/lib/algorithms/glni.cpp b/lib/math/glni.cpp similarity index 93% rename from lib/algorithms/glni.cpp rename to lib/math/glni.cpp index f728f51..68f4670 100644 --- a/lib/algorithms/glni.cpp +++ b/lib/math/glni.cpp @@ -66,7 +66,7 @@ void glni::coefficients::initiate(size_t size) #ifdef GCTL_CHECK_SIZE if (size == 0 || size > 20) { - throw invalid_argument("Invalid initiating order. From glni::coefficients::initiate(...)."); + throw std::invalid_argument("Invalid initiating order. From glni::coefficients::initiate(...)."); } #endif @@ -355,7 +355,7 @@ double glni::coefficients::node(size_t idx) #ifdef GCTL_CHECK_SIZE if (idx >= size_) { - throw out_of_range("Invalid index. From gctl::glni::coefficients::node(...)"); + throw std::out_of_range("Invalid index. From gctl::glni::coefficients::node(...)"); } #endif @@ -367,7 +367,7 @@ double glni::coefficients::weight(size_t idx) #ifdef GCTL_CHECK_SIZE if (idx >= size_) { - throw out_of_range("Invalid index. From gctl::glni::coefficients::weight(...)"); + throw std::out_of_range("Invalid index. From gctl::glni::coefficients::weight(...)"); } #endif @@ -404,7 +404,7 @@ void glni::line::initiate(size_t size) #ifdef GCTL_CHECK_SIZE if (size == 0 || size > 20) { - throw invalid_argument("Invalid initiating order. From glni::line::initiate(...)"); + throw std::invalid_argument("Invalid initiating order. From glni::line::initiate(...)"); } #endif @@ -419,13 +419,13 @@ double glni::line::integral(double low, double hig, void *att) #ifdef GCTL_CHECK_BOUNDER if (hig <= low) { - throw length_error("Invalid integration range. From glni::line::integral(...)"); + throw std::length_error("Invalid integration range. From glni::line::integral(...)"); } #endif if (!initialized_) { - throw runtime_error("The object is not initialized. From gctl::glni::line::integral(...)."); + throw std::runtime_error("The object is not initialized. From gctl::glni::line::integral(...)."); } double scl = (hig - low)/2.0; @@ -468,7 +468,7 @@ void glni::quadrilateral::initiate(size_t size) #ifdef GCTL_CHECK_SIZE if (size == 0 || size > 20) { - throw invalid_argument("Invalid initiating order. From glni::quadrilateral::initiate(...)"); + throw std::invalid_argument("Invalid initiating order. From glni::quadrilateral::initiate(...)"); } #endif @@ -483,13 +483,13 @@ double glni::quadrilateral::integral(double xlow, double xhig, double ylow, doub #ifdef GCTL_CHECK_BOUNDER if (xhig <= xlow || yhig <= ylow) { - throw length_error("Invalid integration range. From glni::quadrilateral::integral(...)"); + throw std::length_error("Invalid integration range. From glni::quadrilateral::integral(...)"); } #endif if (!initialized_) { - throw runtime_error("Un-initialized. From glni::quadrilateral::integral(...)"); + throw std::runtime_error("Un-initialized. From glni::quadrilateral::integral(...)"); } double scl = (xhig-xlow)*(yhig-ylow)/4.0; @@ -536,7 +536,7 @@ void glni::triangle::initiate(size_t size) #ifdef GCTL_CHECK_SIZE if (size == 0 || size > 20) { - throw invalid_argument("Invalid initiating order. From glni::triangle::initiate(...)"); + throw std::invalid_argument("Invalid initiating order. From glni::triangle::initiate(...)"); } #endif @@ -552,13 +552,13 @@ double glni::triangle::integral(double x1, double x2, double x3, double y1, #ifdef GCTL_CHECK_BOUNDER if ((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1) <= GCTL_ZERO) // 这里没有设置为0是为了检测三个点是否共线 { - throw length_error("Invalid integration range. From glni::triangle::integral(...)"); + throw std::length_error("Invalid integration range. From glni::triangle::integral(...)"); } #endif if (!initialized_) { - throw runtime_error("Un-initialized. From glni::triangle::integral(...)"); + throw std::runtime_error("Un-initialized. From glni::triangle::integral(...)"); } double ksi, eta; @@ -609,7 +609,7 @@ void glni::triangle_c::initiate(size_t size) #ifdef GCTL_CHECK_SIZE if (size == 0 || size > 20) { - throw invalid_argument("Invalid initiating order. From glni::triangle_c::initiate(...)"); + throw std::invalid_argument("Invalid initiating order. From glni::triangle_c::initiate(...)"); } #endif @@ -623,15 +623,15 @@ std::complex glni::triangle_c::integral(double x1, double x2, double x3, double y2, double y3, void *att) { #ifndef GCTL_CHECK_BOUNDER - if ((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1) <= GCTL_ZERO) // 这里没有设置为0是为了检测三个点是否共线 + if ((x2-x1)*(y3-y1)-(y2-y1)*(x3-x1) <= 1e-30) // 这里没有设置为0是为了检测三个点是否共线 { - throw length_error("Invalid integration range. From glni::triangle::integral(...)"); + throw std::length_error("Invalid integration range. From glni::triangle::integral(...)"); } #endif if (!initialized_) { - throw runtime_error("Un-initialized. From glni::triangle::integral(...)"); + throw std::runtime_error("Un-initialized. From glni::triangle::integral(...)"); } double ksi, eta; @@ -683,7 +683,7 @@ void glni::cube::initiate(size_t size) #ifdef GCTL_CHECK_SIZE if (size == 0 || size > 20) { - throw invalid_argument("Invalid initiating order. From glni::cube::initiate(...)"); + throw std::invalid_argument("Invalid initiating order. From glni::cube::initiate(...)"); } #endif @@ -699,13 +699,13 @@ double glni::cube::integral(double x1, double x2, double y1, double y2, #ifdef GCTL_CHECK_BOUNDER if (x2 <= x1 || y2 <= y1 || z2 <= z1) { - throw length_error("Invalid integration range. From glni::cube::integral(...)"); + throw std::length_error("Invalid integration range. From glni::cube::integral(...)"); } #endif if (!initialized_) { - throw runtime_error("Un-initialized. From glni::cube::integral(...)"); + throw std::runtime_error("Un-initialized. From glni::cube::integral(...)"); } double gx, gy, gz, inte = 0.0; @@ -758,7 +758,7 @@ void glni::tetrahedron::initiate(size_t size) #ifdef GCTL_CHECK_SIZE if (size == 0 || size > 20) { - throw invalid_argument("Invalid initiating order. From glni::tetrahedron::initiate(...)"); + throw std::invalid_argument("Invalid initiating order. From glni::tetrahedron::initiate(...)"); } #endif @@ -775,14 +775,14 @@ double glni::tetrahedron::integral(double x1, double x2, double x3, double x4, { if (!initialized_) { - throw runtime_error("Un-initialized. From glni::tetrahedron::integral(...)"); + throw std::runtime_error("Un-initialized. From glni::tetrahedron::integral(...)"); } double ksi, eta, zta; double inte = 0.0; double scl = 6.0*tet_volume(x1, x2, x3, x4, y1, y2, y3, y4, z1, z2, z3, z4); - if (scl < GCTL_ZERO) + if (scl < 1e-30) { throw std::invalid_argument("glni::tetrahedron::integral(...) got an invalid integration range."); } @@ -835,7 +835,7 @@ void glni::triprism::initiate(size_t size) #ifdef GCTL_CHECK_SIZE if (size == 0 || size > 20) { - throw std::invalid_argument("glni::triprism::initiate(...) got an invalid initiating order."); + throw std::std::invalid_argument("glni::triprism::initiate(...) got an invalid initiating order."); } #endif // GCTL_CHECK_BOUNDER diff --git a/lib/algorithms/glni.h b/lib/math/glni.h similarity index 99% rename from lib/algorithms/glni.h rename to lib/math/glni.h index 78b517f..331eb30 100644 --- a/lib/algorithms/glni.h +++ b/lib/math/glni.h @@ -28,7 +28,7 @@ #ifndef _GCTL_GLNI_H #define _GCTL_GLNI_H -#include "../core.h" +#include #include "../gctl_config.h" namespace gctl diff --git a/lib/algorithms/heap_sort.h b/lib/math/heap_sort.h similarity index 99% rename from lib/algorithms/heap_sort.h rename to lib/math/heap_sort.h index 11f9678..fb16370 100644 --- a/lib/algorithms/heap_sort.h +++ b/lib/math/heap_sort.h @@ -29,7 +29,7 @@ #define _GCTL_HEAPSORT_H // library's head file -#include "../core.h" +#include "../core/array.h" namespace gctl { diff --git a/lib/algorithms/interpolate.cpp b/lib/math/interpolate.cpp similarity index 99% rename from lib/algorithms/interpolate.cpp rename to lib/math/interpolate.cpp index 0888f47..a931e56 100644 --- a/lib/algorithms/interpolate.cpp +++ b/lib/math/interpolate.cpp @@ -509,8 +509,8 @@ gctl::array *gctl::p2p_dist_sph(point3ds * out_posi, int out_num, point3 { tmp_id = box_list[search_id][b]; // 如果点到节点的距离小于椭球的半径则添加到向量中 - tmp_pc1 = out_posi[i].s2c(); - tmp_pc2 = in_posi[tmp_id].s2c(); + tmp_pc1 = s2c(out_posi[i]); + tmp_pc2 = s2c(in_posi[tmp_id]); tmp_dist = distance(tmp_pc1, tmp_pc2); if (geometry3d::angle(tmp_pc1, tmp_pc2) <= search_deg && diff --git a/lib/algorithms/interpolate.h b/lib/math/interpolate.h similarity index 98% rename from lib/algorithms/interpolate.h rename to lib/math/interpolate.h index 1746b7b..cf0ba3f 100644 --- a/lib/algorithms/interpolate.h +++ b/lib/math/interpolate.h @@ -28,12 +28,7 @@ #ifndef _GCTL_INTERPOLATE_H #define _GCTL_INTERPOLATE_H -#include "../core/array.h" -#include "../maths/mathfunc_t.h" -#include "../maths/mathfunc.h" -#include "../geometry/point2c.h" -#include "../geometry/point3c.h" -#include "../geometry/geometry3d.h" +#include "geometry3d.h" #include "algorithm_func.h" namespace gctl diff --git a/lib/algorithms/kde.cpp b/lib/math/kde.cpp similarity index 100% rename from lib/algorithms/kde.cpp rename to lib/math/kde.cpp diff --git a/lib/algorithms/kde.h b/lib/math/kde.h similarity index 99% rename from lib/algorithms/kde.h rename to lib/math/kde.h index 6ba1424..4c1c9c4 100644 --- a/lib/algorithms/kde.h +++ b/lib/math/kde.h @@ -28,8 +28,7 @@ #ifndef _GCTL_KERNEL_DENSITY_ESTIMATION_H #define _GCTL_KERNEL_DENSITY_ESTIMATION_H -#include "../core.h" -#include "../maths.h" +#include "../core/array.h" namespace gctl { diff --git a/lib/maths/legendre.cpp b/lib/math/legendre.cpp similarity index 100% rename from lib/maths/legendre.cpp rename to lib/math/legendre.cpp diff --git a/lib/maths/legendre.h b/lib/math/legendre.h similarity index 100% rename from lib/maths/legendre.h rename to lib/math/legendre.h diff --git a/lib/maths/linear_algebra.cpp b/lib/math/linear_algebra.cpp similarity index 100% rename from lib/maths/linear_algebra.cpp rename to lib/math/linear_algebra.cpp diff --git a/lib/maths/linear_algebra.h b/lib/math/linear_algebra.h similarity index 100% rename from lib/maths/linear_algebra.h rename to lib/math/linear_algebra.h diff --git a/lib/maths/mathfunc.cpp b/lib/math/mathfunc.cpp similarity index 100% rename from lib/maths/mathfunc.cpp rename to lib/math/mathfunc.cpp diff --git a/lib/maths/mathfunc.h b/lib/math/mathfunc.h similarity index 73% rename from lib/maths/mathfunc.h rename to lib/math/mathfunc.h index 89674c4..fdf8281 100644 --- a/lib/maths/mathfunc.h +++ b/lib/math/mathfunc.h @@ -36,10 +36,136 @@ // library's head files #include "../core/macro.h" #include "../core/spmat.h" -#include "mathfunc_t.h" namespace gctl { + template + inline int sign(T d) + { + return (T(0) < d) - (d < T(0)); + } + + template + inline T arc(T deg) + { + return deg*GCTL_Pi/180.0L; + } + + template + inline T deg(T arc) + { + return arc*180.0L/GCTL_Pi; + } + + template + inline T sind(T deg) + { + return sin(deg*GCTL_Pi/180.0L); + } + + template + inline T cosd(T deg) + { + return cos(deg*GCTL_Pi/180.0L); + } + + template + inline T tand(T deg) + { + return tan(deg*GCTL_Pi/180.0L); + } + + template + inline T power2(T in) + { + return (in)*(in); + } + + template + inline T power3(T in) + { + return (in)*(in)*(in); + } + + template + inline T power4(T in) + { + return (in)*(in)*(in)*(in); + } + + template + inline T power5(T in) + { + return (in)*(in)*(in)*(in)*(in); + } + + template + inline T jacoby2(T x00, T x01, T x10, T x11) + { + return x00*x11-x01*x10; + } + + template + inline T jacoby3(T x00, T x01, T x02, T x10, T x11, + T x12, T x20, T x21, T x22) + { + return x00*x11*x22+x01*x12*x20+x10*x21*x02 + -x02*x11*x20-x01*x10*x22-x00*x12*x21; + } + + /** + * @brief Calculate the inverse matrix of a 3x3 matrix + * + * @tparam T Type name + * @param A Pointer of the input matrix, must be stored in an array of the nine coefficients in a row-major fashion + * @param invA Pointer of the output matrix, must be stored in an array of the nine coefficients in a row-major fashion + * @return true Success + * @return false Fail + */ + template + bool inverse3x3(T *A, T *invA) + { + T det = jacoby3(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8]); + if (det <= GCTL_ZERO && det >= -1*GCTL_ZERO) return false; + + invA[0] = jacoby2(A[4], A[7], A[5], A[8])/det; + invA[1] = -1.0*jacoby2(A[1], A[7], A[2], A[8])/det; + invA[2] = jacoby2(A[1], A[4], A[2], A[5])/det; + invA[3] = -1.0*jacoby2(A[3], A[6], A[5], A[8])/det; + invA[4] = jacoby2(A[0], A[6], A[2], A[8])/det; + invA[5] = -1.0*jacoby2(A[0], A[3], A[2], A[5])/det; + invA[6] = jacoby2(A[3], A[6], A[4], A[7])/det; + invA[7] = -1.0*jacoby2(A[0], A[6], A[1], A[7])/det; + invA[8] = jacoby2(A[0], A[3], A[1], A[4])/det; + return true; + } + + template + T arctg(T v) + { + T ang; + if(v>=0) ang=atan(v); + else ang=atan(v)+GCTL_Pi; + return ang; + } + + template + T arctg2(T v, T f) + { + T ang; + if(f>=0) + { + if(atan(v)>0) ang=atan(v); + else ang=atan(v) + GCTL_Pi; + } + else if(f<0) + { + if(atan(v)<0) ang=atan(v); + else ang=atan(v) - GCTL_Pi; + } + return ang; + } + /** * @brief Return a random number in the range [low, hig] * diff --git a/lib/maths/mathfunc_ext.cpp b/lib/math/mathfunc_ext.cpp similarity index 100% rename from lib/maths/mathfunc_ext.cpp rename to lib/math/mathfunc_ext.cpp diff --git a/lib/maths/mathfunc_ext.h b/lib/math/mathfunc_ext.h similarity index 100% rename from lib/maths/mathfunc_ext.h rename to lib/math/mathfunc_ext.h diff --git a/lib/algorithms/multinary.cpp b/lib/math/multinary.cpp similarity index 100% rename from lib/algorithms/multinary.cpp rename to lib/math/multinary.cpp diff --git a/lib/algorithms/multinary.h b/lib/math/multinary.h similarity index 100% rename from lib/algorithms/multinary.h rename to lib/math/multinary.h diff --git a/lib/geometry/refellipsoid.cpp b/lib/math/refellipsoid.cpp similarity index 99% rename from lib/geometry/refellipsoid.cpp rename to lib/math/refellipsoid.cpp index 5ca1d37..03f86de 100644 --- a/lib/geometry/refellipsoid.cpp +++ b/lib/math/refellipsoid.cpp @@ -100,7 +100,7 @@ void gctl::refellipsoid::geodetic2spherical(double geodetic_lati, void gctl::refellipsoid::spherical2geodetic(const point3ds& ps, double& geodetic_lon, double& geodetic_lati, double& geodetic_hei, double eps, int cnt) { - point3dc pc = ps.s2c(); + point3dc pc = s2c(ps); xyz2geodetic(pc, geodetic_lon, geodetic_lati, geodetic_hei, eps, cnt); return; } diff --git a/lib/geometry/refellipsoid.h b/lib/math/refellipsoid.h similarity index 97% rename from lib/geometry/refellipsoid.h rename to lib/math/refellipsoid.h index 8e7ab0e..ef56f82 100644 --- a/lib/geometry/refellipsoid.h +++ b/lib/math/refellipsoid.h @@ -28,11 +28,8 @@ #ifndef _GCTL_REFELLIPSOID_H #define _GCTL_REFELLIPSOID_H -#include "../core/macro.h" -#include "../core/exceptions.h" -#include "../maths/mathfunc.h" -#include "point3c.h" -#include "point3s.h" +#include "../poly/vertex.h" +#include "mathfunc.h" namespace gctl { diff --git a/lib/maths/shapefunc.cpp b/lib/math/shapefunc.cpp similarity index 100% rename from lib/maths/shapefunc.cpp rename to lib/math/shapefunc.cpp diff --git a/lib/maths/shapefunc.h b/lib/math/shapefunc.h similarity index 99% rename from lib/maths/shapefunc.h rename to lib/math/shapefunc.h index 83a10c9..dbb5cfe 100644 --- a/lib/maths/shapefunc.h +++ b/lib/math/shapefunc.h @@ -31,7 +31,7 @@ #include "../gctl_config.h" #include "../core/enum.h" #include "../core/exceptions.h" -#include "../maths/mathfunc.h" +#include "mathfunc.h" namespace gctl { diff --git a/lib/algorithms/sinkhorn.cpp b/lib/math/sinkhorn.cpp similarity index 100% rename from lib/algorithms/sinkhorn.cpp rename to lib/math/sinkhorn.cpp diff --git a/lib/algorithms/sinkhorn.h b/lib/math/sinkhorn.h similarity index 98% rename from lib/algorithms/sinkhorn.h rename to lib/math/sinkhorn.h index 79c8c21..10c8b52 100644 --- a/lib/algorithms/sinkhorn.h +++ b/lib/math/sinkhorn.h @@ -28,8 +28,6 @@ #ifndef _GCTL_SINKHORN_H #define _GCTL_SINKHORN_H -#include "../core/array.h" -#include "algorithm_func.h" #include "interpolate.h" namespace gctl diff --git a/lib/algorithms/space_filter.cpp b/lib/math/space_filter.cpp similarity index 100% rename from lib/algorithms/space_filter.cpp rename to lib/math/space_filter.cpp diff --git a/lib/algorithms/space_filter.h b/lib/math/space_filter.h similarity index 99% rename from lib/algorithms/space_filter.h rename to lib/math/space_filter.h index c72588d..04db649 100644 --- a/lib/algorithms/space_filter.h +++ b/lib/math/space_filter.h @@ -28,8 +28,8 @@ #ifndef _GCTL_SPACE_FILTER_H #define _GCTL_SPACE_FILTER_H -#include "../core.h" -#include "../maths.h" +#include "../core/matrix.h" +#include "mathfunc_ext.h" namespace gctl { diff --git a/lib/algorithms/variogram.cpp b/lib/math/variogram.cpp similarity index 100% rename from lib/algorithms/variogram.cpp rename to lib/math/variogram.cpp diff --git a/lib/algorithms/variogram.h b/lib/math/variogram.h similarity index 100% rename from lib/algorithms/variogram.h rename to lib/math/variogram.h diff --git a/lib/algorithms/windowfunc.cpp b/lib/math/windowfunc.cpp similarity index 100% rename from lib/algorithms/windowfunc.cpp rename to lib/math/windowfunc.cpp diff --git a/lib/algorithms/windowfunc.h b/lib/math/windowfunc.h similarity index 98% rename from lib/algorithms/windowfunc.h rename to lib/math/windowfunc.h index b195a41..3e5eb35 100644 --- a/lib/algorithms/windowfunc.h +++ b/lib/math/windowfunc.h @@ -28,7 +28,7 @@ #ifndef _WINDOWFUNC_H #define _WINDOWFUNC_H -#include "../core.h" +#include "../core/array.h" namespace gctl { diff --git a/lib/maths.h b/lib/maths.h deleted file mode 100644 index d6db690..0000000 --- a/lib/maths.h +++ /dev/null @@ -1,40 +0,0 @@ -/******************************************************** - * ██████╗ ██████╗████████╗██╗ - * ██╔════╝ ██╔════╝╚══██╔══╝██║ - * ██║ ███╗██║ ██║ ██║ - * ██║ ██║██║ ██║ ██║ - * ╚██████╔╝╚██████╗ ██║ ███████╗ - * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ - * Geophysical Computational Tools & Library (GCTL) - * - * Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn) - * - * GCTL is distributed under a dual licensing scheme. You can redistribute - * it and/or modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either version 2 - * of the License, or (at your option) any later version. You should have - * received a copy of the GNU Lesser General Public License along with this - * program. If not, see . - * - * If the terms and conditions of the LGPL v.2. would prevent you from using - * the GCTL, please consider the option to obtain a commercial license for a - * fee. These licenses are offered by the GCTL's original author. As a rule, - * licenses are provided "as-is", unlimited in time for a one time fee. Please - * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget - * to include some description of your company and the realm of its activities. - * Also add information on how to contact you by electronic and paper mail. - ******************************************************/ - -#ifndef _GCTL_MATHS_H -#define _GCTL_MATHS_H - -#include "maths/mathfunc_t.h" -#include "maths/mathfunc.h" -#include "maths/mathfunc_ext.h" -#include "maths/shapefunc.h" -#include "maths/legendre.h" -#include "maths/linear_algebra.h" -#include "maths/gaussfunc.h" -#include "maths/fft.h" - -#endif // _GCTL_MATHS_H \ No newline at end of file diff --git a/lib/maths/mathfunc_t.h b/lib/maths/mathfunc_t.h deleted file mode 100644 index 1d7f9dd..0000000 --- a/lib/maths/mathfunc_t.h +++ /dev/null @@ -1,482 +0,0 @@ -/******************************************************** - * ██████╗ ██████╗████████╗██╗ - * ██╔════╝ ██╔════╝╚══██╔══╝██║ - * ██║ ███╗██║ ██║ ██║ - * ██║ ██║██║ ██║ ██║ - * ╚██████╔╝╚██████╗ ██║ ███████╗ - * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ - * Geophysical Computational Tools & Library (GCTL) - * - * Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn) - * - * GCTL is distributed under a dual licensing scheme. You can redistribute - * it and/or modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation, either version 2 - * of the License, or (at your option) any later version. You should have - * received a copy of the GNU Lesser General Public License along with this - * program. If not, see . - * - * If the terms and conditions of the LGPL v.2. would prevent you from using - * the GCTL, please consider the option to obtain a commercial license for a - * fee. These licenses are offered by the GCTL's original author. As a rule, - * licenses are provided "as-is", unlimited in time for a one time fee. Please - * send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget - * to include some description of your company and the realm of its activities. - * Also add information on how to contact you by electronic and paper mail. - ******************************************************/ - -#ifndef _GCTL_MATHFUNC_TEMPLATE_H -#define _GCTL_MATHFUNC_TEMPLATE_H - -#include -#include "../core/matrix.h" - -namespace gctl -{ - template - inline int sign(T d) - { - return (T(0) < d) - (d < T(0)); - } - - template - inline T arc(T deg) - { - return deg*GCTL_Pi/180.0L; - } - - template - inline T deg(T arc) - { - return arc*180.0L/GCTL_Pi; - } - - template - inline T sind(T deg) - { - return sin(deg*GCTL_Pi/180.0L); - } - - template - inline T cosd(T deg) - { - return cos(deg*GCTL_Pi/180.0L); - } - - template - inline T tand(T deg) - { - return tan(deg*GCTL_Pi/180.0L); - } - - template - inline T power2(T in) - { - return (in)*(in); - } - - template - inline T power3(T in) - { - return (in)*(in)*(in); - } - - template - inline T power4(T in) - { - return (in)*(in)*(in)*(in); - } - - template - inline T power5(T in) - { - return (in)*(in)*(in)*(in)*(in); - } - - template - inline T jacoby2(T x00, T x01, T x10, T x11) - { - return x00*x11-x01*x10; - } - - template - inline T jacoby3(T x00, T x01, T x02, T x10, T x11, - T x12, T x20, T x21, T x22) - { - return x00*x11*x22+x01*x12*x20+x10*x21*x02 - -x02*x11*x20-x01*x10*x22-x00*x12*x21; - } - - /** - * @brief Calculate the inverse matrix of a 3x3 matrix - * - * @tparam T Type name - * @param A Pointer of the input matrix, must be stored in an array of the nine coefficients in a row-major fashion - * @param invA Pointer of the output matrix, must be stored in an array of the nine coefficients in a row-major fashion - * @return true Success - * @return false Fail - */ - template - bool inverse3x3(T *A, T *invA) - { - T det = jacoby3(A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8]); - if (det <= GCTL_ZERO && det >= -1*GCTL_ZERO) return false; - - invA[0] = jacoby2(A[4], A[7], A[5], A[8])/det; - invA[1] = -1.0*jacoby2(A[1], A[7], A[2], A[8])/det; - invA[2] = jacoby2(A[1], A[4], A[2], A[5])/det; - invA[3] = -1.0*jacoby2(A[3], A[6], A[5], A[8])/det; - invA[4] = jacoby2(A[0], A[6], A[2], A[8])/det; - invA[5] = -1.0*jacoby2(A[0], A[3], A[2], A[5])/det; - invA[6] = jacoby2(A[3], A[6], A[4], A[7])/det; - invA[7] = -1.0*jacoby2(A[0], A[6], A[1], A[7])/det; - invA[8] = jacoby2(A[0], A[3], A[1], A[4])/det; - return true; - } - - template - T arctg(T v) - { - T ang; - if(v>=0) ang=atan(v); - else ang=atan(v)+GCTL_Pi; - return ang; - } - - template - T arctg2(T v, T f) - { - T ang; - if(f>=0) - { - if(atan(v)>0) ang=atan(v); - else ang=atan(v) + GCTL_Pi; - } - else if(f<0) - { - if(atan(v)<0) ang=atan(v); - else ang=atan(v) - GCTL_Pi; - } - return ang; - } -}; - -#endif //_GCTL_MATHFUNC_TEMPLATE_H - -/* - template - void matrix_vector(const matrix &mat, const array &vec, array &ret, T zero = 0, matrix_layout_e layout = NoTrans) - { - static_assert(std::is_arithmetic::value, "gctl::matrix_vector(...) could only be used with an arithmetic type."); - - if (mat.empty() || vec.empty()) - { - throw runtime_error("Empty matrix or vector. From matrix_vector(...)"); - } - - if ((layout == NoTrans && mat.col_size() != vec.size()) || (layout == Trans && mat.row_size() != vec.size())) - { - throw runtime_error("Incompatible sizes of the matrix and the vector. From matrix_vector(...)"); - } - - if (layout == Trans) - { - ret.resize(mat.col_size(), zero); - for (int j = 0; j < mat.col_size(); ++j) - { - for (int i = 0; i < mat.row_size(); ++i) - { - ret[j] = ret[j] + mat[i][j]*vec[i]; - } - } - return; - } - - ret.resize(mat.row_size(), zero); - for (int i = 0; i < mat.row_size(); ++i) - { - for (int j = 0; j < mat.col_size(); ++j) - { - ret[i] = ret[i] + mat[i][j]*vec[j]; - } - } - return; - } - - template - void matrix_matrix(const matrix &mat, const matrix &mat2, matrix &ret, T zero = 0) - { - static_assert(std::is_arithmetic::value, "gctl::matrix_matrix(...) could only be used with an arithmetic type."); - - if (mat.empty() || mat2.empty()) - { - throw runtime_error("Empty matrix(s). From matrix_matrix(...)"); - } - - if (mat.col_size() != mat2.row_size()) - { - throw runtime_error("Incompatible matrix sizes. From matrix_vector(...)"); - } - - ret.resize(mat.row_size(), mat2.col_size(), zero); - for (int i = 0; i < mat.row_size(); ++i) - { - for (int j = 0; j < mat2.col_size(); ++j) - { - for (int k = 0; k < mat.col_size(); ++k) - { - ret[i][j] = ret[i][j] + mat[i][k]*mat2[k][j]; - } - } - } - return; - } - - template - void linespace(const T &start, const T &end, unsigned int size, std::vector &out_vec) - { - if (size < 1) - throw invalid_argument("Invalid vector size. From linespace(...)"); - - out_vec.resize(size); - - if (size == 1) - { - out_vec[0] = 0.5*(start + end); - return; - } - - T space = 1.0/(size-1)*(end - start); - for (int i = 0; i < size; i++) - { - out_vec[i] = start + i*space; - } - return; - } - - template - void gridspace(const T &xs, const T &xe, const T &ys, const T &ye, unsigned int xn, - unsigned int yn, std::vector &out_vec) - { - if (xn < 1 || yn < 1) - throw invalid_argument("Invalid grid size. From gridspace(...)"); - - std::vector out_x, out_y; - linespace(xs, xe, xn, out_x); - linespace(ys, ye, yn, out_y); - - out_vec.resize(xn*yn); - for (int i = 0; i < yn; ++i) - { - for (int j = 0; j < xn; ++j) - { - out_vec[j+i*xn] = out_x[j] + out_y[i]; - } - } - return; - } - - template - void meshspace(const T &xs, const T &xe, const T &ys, const T &ye, const T &zs, const T &ze, - unsigned int xn, unsigned int yn, unsigned int zn, std::vector &out_vec) - { - if (xn < 1 || yn < 1 || zn < 1) - throw invalid_argument("Invalid grid size. From meshspace(...)"); - - array out_x, out_y, out_z; - linespace(xs, xe, xn, out_x); - linespace(ys, ye, yn, out_y); - linespace(zs, ze, zn, out_z); - - out_vec.resize(xn*yn*zn); - for (int i = 0; i < zn; ++i) - { - for (int j = 0; j < yn; ++j) - { - for (int k = 0; k < xn; ++k) - { - out_vec[k+j*xn+i*xn*yn] = out_x[k] + out_y[j] + out_z[i]; - } - } - } - return; - } - - template - T average(T *val_ptr, int size, const T &zero = 0) - { - if (val_ptr == nullptr) - throw domain_error("Invalid pointer. From average(...)"); - - if (size <= 0) - throw invalid_argument("Invalid object size. From average(...)"); - - T mn = zero; - for (int i = 0; i < size; i++) - { - mn = mn + val_ptr[i]; - } - - return 1.0/size*mn; - } - - template - T average(const std::vector &val_arr, const T &zero = 0) - { - if (val_arr.empty()) - throw domain_error("Invalid object size. From average(...)"); - - int size = val_arr.size(); - - T mn = zero; - for (int i = 0; i < size; i++) - { - mn = mn + val_arr[i]; - } - - return 1.0/size*mn; - } - - template - T deviation(T *val_ptr, int size, const T &zero = 0, bool STD = false) - { - T mn = average(val_ptr, size); - - T deviation = zero; - for (int i = 0; i < size; i++) - { - deviation = deviation + (val_ptr[i] - mn)*(val_ptr[i] - mn); - } - deviation = 1.0/size*deviation; - - if (STD) return sqrt(deviation); - else return deviation; - } - - template - T deviation(const std::vector &val_arr, const T &zero = 0, bool STD = false) - { - T mn = average(val_arr); - int size = val_arr.size(); - - T deviation = zero; - for (int i = 0; i < size; i++) - { - deviation = deviation + (val_arr[i] - mn)*(val_arr[i] - mn); - } - deviation = 1.0/size*deviation; - - if (STD) return sqrt(deviation); - else return deviation; - } - - template - T root_mn_square(T *val_ptr, int size, const T &zero = 0) - { - if (val_ptr == nullptr) - throw domain_error("Invalid pointer. From root_mn_square(...)"); - - if (size <= 0) - throw invalid_argument("Invalid object size. From root_mn_square(...)"); - - T mn = zero; - for (int i = 0; i < size; i++) - { - mn = mn + val_ptr[i]*val_ptr[i]; - } - return sqrt(1.0/size*mn); - } - - template - T root_mn_square(const std::vector &val_arr, const T &zero = 0) - { - if (val_arr.empty()) - throw domain_error("Invalid object size. From root_mn_square(...)"); - - int size = val_arr.size(); - - T mn = zero; - for (int i = 0; i < size; i++) - { - mn = mn + val_arr[i]*val_arr[i]; - } - return sqrt(1.0/size*mn); - } - - template - void random(T p1, T p2, T *val_ptr, int size, random_type_e mode = RdNormal) - { - if (val_ptr == nullptr) - throw domain_error("Invalid pointer. From random(...)"); - - if (size <= 0) - throw invalid_argument("Invalid size. From random(...)"); - - unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); - - std::default_random_engine generator(seed); - if (mode == RdNormal) - { - //添加高斯分布的随机值 - std::normal_distribution dist(p1, p2); - for (int i = 0; i < size; i++) - { - val_ptr[i] = dist(generator); - } - return; - } - - //添加均匀分布的随机值 - std::uniform_real_distribution dist(p1, p2); - for (int i = 0; i < size; i++) - { - val_ptr[i] = dist(generator); - } - return; - } - - template - void random(T p1, T p2, std::vector &val_vec, random_type_e mode = RdNormal) - { - size_t size = val_vec.size(); - - if (size <= 0) - throw invalid_argument("Invalid size. From random(...)"); - - unsigned seed = std::chrono::system_clock::now().time_since_epoch().count(); - - std::default_random_engine generator(seed); - if (mode == RdNormal) - { - //添加高斯分布的随机值 - std::normal_distribution dist(p1, p2); - for (int i = 0; i < size; i++) - { - val_vec[i] = dist(generator); - } - return; - } - - //添加均匀分布的随机值 - std::uniform_real_distribution dist(p1, p2); - for (int i = 0; i < size; i++) - { - val_vec[i] = dist(generator); - } - return; - } - - template - T normalize(array &in_arr, T eps = 1e-8) - { - T norm_val = norm(in_arr, L2); - if (norm_val < eps) - return 0.0; - - for (int i = 0; i < in_arr.size(); i++) - { - in_arr[i] /= norm_val; - } - return norm_val; - } -*/ \ No newline at end of file diff --git a/lib/geometry/point.h b/lib/poly.h similarity index 74% rename from lib/geometry/point.h rename to lib/poly.h index 7eff0ff..0b6d11e 100644 --- a/lib/geometry/point.h +++ b/lib/poly.h @@ -25,31 +25,32 @@ * Also add information on how to contact you by electronic and paper mail. ******************************************************/ -#ifndef _GCTL_POINT_H -#define _GCTL_POINT_H +#ifndef _GCTL_POLY_H +#define _GCTL_POLY_H + +#include "poly/point2c.h" +#include "poly/point2p.h" +#include "poly/point3c.h" +#include "poly/point3s.h" +#include "poly/vertex.h" +#include "poly/tensor.h" -#include "iostream" -#include "string" -#include "cmath" -#include "iomanip" -#include "regex" +#include "poly/entity.h" +#include "poly/node.h" +#include "poly/sphere.h" -#include "../core/macro.h" -#include "../core/exceptions.h" +#include "poly/triangle2d.h" +#include "poly/triangle2d2o.h" +#include "poly/rectangle2d.h" +#include "poly/edge2d.h" -namespace gctl -{ - template struct point2c; - template struct point2p; - template struct point3c; - template struct point3s; +#include "poly/block.h" +#include "poly/prism.h" +#include "poly/tesseroid.h" +#include "poly/tetrahedron.h" +#include "poly/tri_cone.h" +#include "poly/triangle.h" +#include "poly/edge.h" -#ifndef IO_PSN -#define IO_PSN - // static variable for controlling the IO process - static int io_psn = 6; -#endif // IO_PSN -}; - -#endif // _GCTL_POINT_H \ No newline at end of file +#endif // _GCTL_POLY_H \ No newline at end of file diff --git a/lib/geometry/block.h b/lib/poly/block.h similarity index 100% rename from lib/geometry/block.h rename to lib/poly/block.h diff --git a/lib/geometry/edge.h b/lib/poly/edge.h similarity index 99% rename from lib/geometry/edge.h rename to lib/poly/edge.h index 395ca01..1333f94 100644 --- a/lib/geometry/edge.h +++ b/lib/poly/edge.h @@ -28,7 +28,6 @@ #ifndef _GCTL_EDGE_H #define _GCTL_EDGE_H -#include "point3c.h" #include "vertex.h" #include "entity.h" diff --git a/lib/geometry/edge2d.h b/lib/poly/edge2d.h similarity index 99% rename from lib/geometry/edge2d.h rename to lib/poly/edge2d.h index ba4b185..2786ce6 100644 --- a/lib/geometry/edge2d.h +++ b/lib/poly/edge2d.h @@ -28,9 +28,6 @@ #ifndef _GCTL_EDGE2D_H #define _GCTL_EDGE2D_H -#include "point2c.h" -#include "vertex.h" -#include "entity.h" #include "triangle2d.h" namespace gctl diff --git a/lib/geometry/entity.h b/lib/poly/entity.h similarity index 99% rename from lib/geometry/entity.h rename to lib/poly/entity.h index 3c30108..f48cce3 100644 --- a/lib/geometry/entity.h +++ b/lib/poly/entity.h @@ -33,7 +33,6 @@ // library head file #include "../core/array.h" - namespace gctl { // this variable is only valid in this source file to control diff --git a/lib/geometry/node.h b/lib/poly/node.h similarity index 99% rename from lib/geometry/node.h rename to lib/poly/node.h index a15da5e..df7a0ea 100644 --- a/lib/geometry/node.h +++ b/lib/poly/node.h @@ -28,7 +28,6 @@ #ifndef _GCTL_NODE_H #define _GCTL_NODE_H -#include "point3c.h" #include "vertex.h" #include "entity.h" diff --git a/lib/geometry/point2c.h b/lib/poly/point2c.h similarity index 96% rename from lib/geometry/point2c.h rename to lib/poly/point2c.h index ad20676..59d1bd5 100644 --- a/lib/geometry/point2c.h +++ b/lib/poly/point2c.h @@ -28,10 +28,20 @@ #ifndef _GCTL_POINT2C_H #define _GCTL_POINT2C_H -#include "point.h" +#include "iostream" +#include "string" +#include "cmath" +#include "iomanip" +#include "regex" + +#include "../core/array.h" namespace gctl { + // static variable for controlling the IO process + static int p2c_io_psn = 6; + + template class point2c; typedef point2c point2dc; typedef point2c point2fc; @@ -108,7 +118,7 @@ namespace gctl * * @return The point2dp object. */ - point2p c2p() const; + //point2p c2p() const; /** * @brief Set a point2c object from a string. The accepted format: (x, y) * @@ -236,7 +246,7 @@ namespace gctl { return point2c(x/module(), y/module()); } - +/* template gctl::point2p gctl::point2c::c2p() const { @@ -247,7 +257,7 @@ namespace gctl else outp.arc = atan2(y, x) + 2.0*GCTL_Pi; return outp; } - +*/ template void gctl::point2c::str(std::string str) { @@ -272,7 +282,7 @@ namespace gctl throw invalid_argument("Invalid precision. From point2c::set_io_precision(...)"); } - io_psn = psn; + p2c_io_psn = psn; return; } @@ -288,7 +298,7 @@ namespace gctl template void gctl::point2c::out_loc(std::ostream &os, char deli) const { - os << std::setprecision(io_psn) << x << deli << y; + os << std::setprecision(p2c_io_psn) << x << deli << y; return; } @@ -402,7 +412,7 @@ namespace gctl template std::ostream &operator <<(std::ostream & os, const point2c &a) { - os << std::setprecision(io_psn) << a.x << " " << a.y; + os << std::setprecision(p2c_io_psn) << a.x << " " << a.y; return os; } diff --git a/lib/geometry/point2p.h b/lib/poly/point2p.h similarity index 95% rename from lib/geometry/point2p.h rename to lib/poly/point2p.h index 4b46817..403e2a9 100644 --- a/lib/geometry/point2p.h +++ b/lib/poly/point2p.h @@ -28,10 +28,21 @@ #ifndef _GCTL_POINT2P_H #define _GCTL_POINT2P_H -#include "point.h" +#include "iostream" +#include "string" +#include "cmath" +#include "iomanip" +#include "regex" + +#include "../core/macro.h" +#include "../core/exceptions.h" namespace gctl { + // static variable for controlling the IO process + static int p2p_io_psn = 6; + + template class point2p; typedef point2p point2dp; typedef point2p point2fp; @@ -99,7 +110,7 @@ namespace gctl * * @return The point2c object. */ - point2c p2c() const; + //point2c p2c() const; /** * @brief Parse the point's parameters from a string * @@ -207,7 +218,7 @@ namespace gctl else if (arc < 0.0) arc = fmod(arc, (2.0*GCTL_Pi)) + 2.0*GCTL_Pi; return; } - + /* template gctl::point2c gctl::point2p::p2c() const { @@ -216,7 +227,7 @@ namespace gctl outc.y = rad * sin(arc); return outc; } - + */ template void gctl::point2p::str(std::string str) { @@ -242,14 +253,14 @@ namespace gctl throw invalid_argument("Invalid precision. From point2p::set_io_precision(...)"); } - io_psn = psn; + p2p_io_psn = psn; return; } template void gctl::point2p::out_loc(std::ostream &os, char deli) const { - os << std::setprecision(io_psn) << rad << deli << arc; + os << std::setprecision(p2p_io_psn) << rad << deli << arc; return; } @@ -291,7 +302,7 @@ namespace gctl template std::ostream &operator <<(std::ostream & os, const point2p &a) { - os << std::setprecision(io_psn) << a.rad << " " << a.arc; + os << std::setprecision(p2p_io_psn) << a.rad << " " << a.arc; return os; } diff --git a/lib/geometry/point3c.h b/lib/poly/point3c.h similarity index 97% rename from lib/geometry/point3c.h rename to lib/poly/point3c.h index 2e20767..a013b5a 100644 --- a/lib/geometry/point3c.h +++ b/lib/poly/point3c.h @@ -28,11 +28,20 @@ #ifndef _GCTL_POINT3C_H #define _GCTL_POINT3C_H -#include "point.h" +#include "iostream" +#include "string" +#include "cmath" +#include "iomanip" +#include "regex" + #include "../core/array.h" namespace gctl { + // static variable for controlling the IO process + static int p3c_io_psn = 6; + + template class point3c; typedef point3c point3dc; typedef point3c point3fc; @@ -103,7 +112,7 @@ namespace gctl * * @return spherical coordinates point */ - point3s c2s() const; + //point3s c2s() const; /** * @brief Return the normal vector * @@ -254,7 +263,7 @@ namespace gctl { return sqrt(x*x + y*y + z*z); } - +/* template gctl::point3s gctl::point3c::c2s() const { @@ -269,7 +278,7 @@ namespace gctl outs.lon = atan2(y, x)*180.0/GCTL_Pi; return outs; } - +*/ template gctl::point3c gctl::point3c::normal() const { @@ -311,7 +320,7 @@ namespace gctl throw invalid_argument("Invalid precision. From point3c::set_io_precision(...)"); } - io_psn = psn; + p3c_io_psn = psn; return; } @@ -386,7 +395,7 @@ namespace gctl template void gctl::point3c::out_loc(std::ostream &os, char deli) const { - os << std::setprecision(io_psn) << x << deli << y << deli << z; + os << std::setprecision(p3c_io_psn) << x << deli << y << deli << z; return; } @@ -510,7 +519,7 @@ namespace gctl template std::ostream &operator <<(std::ostream & os, const point3c &a) { - os << std::setprecision(io_psn) << a.x << " " << a.y << " " << a.z; + os << std::setprecision(p3c_io_psn) << a.x << " " << a.y << " " << a.z; return os; } diff --git a/lib/geometry/point3s.h b/lib/poly/point3s.h similarity index 94% rename from lib/geometry/point3s.h rename to lib/poly/point3s.h index 91826ee..ff69800 100644 --- a/lib/geometry/point3s.h +++ b/lib/poly/point3s.h @@ -28,10 +28,21 @@ #ifndef _GCTL_POINT3S_H #define _GCTL_POINT3S_H -#include "point.h" +#include "iostream" +#include "string" +#include "cmath" +#include "iomanip" +#include "regex" + +#include "../core/macro.h" +#include "../core/exceptions.h" namespace gctl { + // static variable for controlling the IO process + static int p3s_io_psn = 6; + + template class point3s; typedef point3s point3ds; typedef point3s point3fs; @@ -53,7 +64,7 @@ namespace gctl void set(T r, T ln, T lt); void set(const point3s &b); double module() const; - point3c s2c() const; // 转换为球坐标点 + //point3c s2c() const; // 转换为球坐标点 void str(std::string str); /** * @brief Sets the i/o precision of the type. @@ -161,17 +172,15 @@ namespace gctl { return rad; } - +/* template gctl::point3c gctl::point3s::s2c() const { - /* - point3c outc; - outc.x = rad*sin((0.5 - lat/180.0)*GCTL_Pi)*cos((2.0 + lon/180.0)*GCTL_Pi); - outc.y = rad*sin((0.5 - lat/180.0)*GCTL_Pi)*sin((2.0 + lon/180.0)*GCTL_Pi); - outc.z = rad*cos((0.5 - lat/180.0)*GCTL_Pi); - return outc; - */ + //point3c outc; + //outc.x = rad*sin((0.5 - lat/180.0)*GCTL_Pi)*cos((2.0 + lon/180.0)*GCTL_Pi); + //outc.y = rad*sin((0.5 - lat/180.0)*GCTL_Pi)*sin((2.0 + lon/180.0)*GCTL_Pi); + //outc.z = rad*cos((0.5 - lat/180.0)*GCTL_Pi); + //return outc; point3c v; v.x = rad*cos(GCTL_Pi*lat/180.0)*cos(GCTL_Pi*lon/180.0); @@ -179,7 +188,7 @@ namespace gctl v.z = rad*sin(GCTL_Pi*lat/180.0); return v; } - +*/ template void gctl::point3s::str(std::string str) { @@ -205,14 +214,14 @@ namespace gctl throw invalid_argument("Invalid precision. From point3s::set_io_precision(...)"); } - io_psn = psn; + p3s_io_psn = psn; return; } template void gctl::point3s::out_loc(std::ostream &os, char deli) const { - os << std::setprecision(io_psn) << rad << deli << lon << deli << lat; + os << std::setprecision(p3s_io_psn) << rad << deli << lon << deli << lat; return; } @@ -389,7 +398,7 @@ namespace gctl template std::ostream &operator <<(std::ostream & os, const point3s &a) { - os << std::setprecision(io_psn) << a.rad << " " << a.lon << " " << a.lat; + os << std::setprecision(p3s_io_psn) << a.rad << " " << a.lon << " " << a.lat; return os; } diff --git a/lib/geometry/prism.h b/lib/poly/prism.h similarity index 100% rename from lib/geometry/prism.h rename to lib/poly/prism.h diff --git a/lib/geometry/rectangle2d.h b/lib/poly/rectangle2d.h similarity index 100% rename from lib/geometry/rectangle2d.h rename to lib/poly/rectangle2d.h diff --git a/lib/geometry/sphere.h b/lib/poly/sphere.h similarity index 99% rename from lib/geometry/sphere.h rename to lib/poly/sphere.h index a6031a4..378ead4 100644 --- a/lib/geometry/sphere.h +++ b/lib/poly/sphere.h @@ -28,7 +28,6 @@ #ifndef _GCTL_SPHERE_H #define _GCTL_SPHERE_H -#include "point3c.h" #include "vertex.h" #include "entity.h" diff --git a/lib/geometry/tensor.h b/lib/poly/tensor.h similarity index 99% rename from lib/geometry/tensor.h rename to lib/poly/tensor.h index 66420e2..ceec1e9 100644 --- a/lib/geometry/tensor.h +++ b/lib/poly/tensor.h @@ -28,8 +28,7 @@ #ifndef _GCTL_TENSOR_H #define _GCTL_TENSOR_H -#include "../core.h" - +#include "../core/matrix.h" #include "point3c.h" namespace gctl diff --git a/lib/geometry/tesseroid.h b/lib/poly/tesseroid.h similarity index 100% rename from lib/geometry/tesseroid.h rename to lib/poly/tesseroid.h diff --git a/lib/geometry/tetrahedron.h b/lib/poly/tetrahedron.h similarity index 99% rename from lib/geometry/tetrahedron.h rename to lib/poly/tetrahedron.h index 2a44739..01fe96b 100644 --- a/lib/geometry/tetrahedron.h +++ b/lib/poly/tetrahedron.h @@ -28,7 +28,6 @@ #ifndef _GCTL_TETRAHEDRON_H #define _GCTL_TETRAHEDRON_H -#include "point3c.h" #include "vertex.h" #include "entity.h" @@ -337,10 +336,10 @@ namespace gctl } this->self_host = true; - point3dc p0 = ps0.s2c(); - point3dc p1 = ps1.s2c(); - point3dc p2 = ps2.s2c(); - point3dc p3 = ps3.s2c(); + point3dc p0 = s2c(ps0); + point3dc p1 = s2c(ps1); + point3dc p2 = s2c(ps2); + point3dc p3 = s2c(ps3); this->id = index; this->vert[0]->set(p0, 4*index + 0); diff --git a/lib/geometry/tri_cone.h b/lib/poly/tri_cone.h similarity index 98% rename from lib/geometry/tri_cone.h rename to lib/poly/tri_cone.h index 4e678c6..347dfe8 100644 --- a/lib/geometry/tri_cone.h +++ b/lib/poly/tri_cone.h @@ -28,7 +28,6 @@ #ifndef _GCTL_TRI_CONE_H #define _GCTL_TRI_CONE_H -#include "point3c.h" #include "vertex.h" #include "entity.h" @@ -180,9 +179,9 @@ namespace gctl } this->self_host = true; - point3dc p0 = ps0.s2c(); - point3dc p1 = ps1.s2c(); - point3dc p2 = ps2.s2c(); + point3dc p0 = s2c(ps0); + point3dc p1 = s2c(ps1); + point3dc p2 = s2c(ps2); // 重新检查顶点的排序 初始化为逆时针排序 point3dc tmp_p; diff --git a/lib/geometry/triangle.h b/lib/poly/triangle.h similarity index 99% rename from lib/geometry/triangle.h rename to lib/poly/triangle.h index 8f87881..c472b0b 100644 --- a/lib/geometry/triangle.h +++ b/lib/poly/triangle.h @@ -28,7 +28,6 @@ #ifndef _GCTL_TRIANGLE_H #define _GCTL_TRIANGLE_H -#include "point3c.h" #include "vertex.h" #include "entity.h" diff --git a/lib/geometry/triangle2d.h b/lib/poly/triangle2d.h similarity index 99% rename from lib/geometry/triangle2d.h rename to lib/poly/triangle2d.h index 1953911..dc18cfa 100644 --- a/lib/geometry/triangle2d.h +++ b/lib/poly/triangle2d.h @@ -28,7 +28,6 @@ #ifndef _GCTL_TRIANGLE2D_H #define _GCTL_TRIANGLE2D_H -#include "point2c.h" #include "vertex.h" #include "entity.h" diff --git a/lib/geometry/triangle2d2o.h b/lib/poly/triangle2d2o.h similarity index 99% rename from lib/geometry/triangle2d2o.h rename to lib/poly/triangle2d2o.h index 5b1c5d0..1bef192 100644 --- a/lib/geometry/triangle2d2o.h +++ b/lib/poly/triangle2d2o.h @@ -28,7 +28,6 @@ #ifndef _GCTL_TRIANGLE2D2O_H #define _GCTL_TRIANGLE2D2O_H -#include "point2c.h" #include "vertex.h" #include "entity.h" diff --git a/lib/geometry/vertex.h b/lib/poly/vertex.h similarity index 80% rename from lib/geometry/vertex.h rename to lib/poly/vertex.h index 2d07c6a..25f54df 100644 --- a/lib/geometry/vertex.h +++ b/lib/poly/vertex.h @@ -35,6 +35,57 @@ namespace gctl { + template + gctl::point2p c2p(const gctl::point2c& c) + { + point2p outp; + outp.rad = c.module(); + if (c.y >= 0.0) + outp.arc= atan2(c.y, c.x); + else outp.arc = atan2(c.y, c.x) + 2.0*GCTL_Pi; + return outp; + } + + template + gctl::point2c p2c(const gctl::point2p& p) + { + point2c outc; + outc.x = p.rad * cos(p.arc); + outc.y = p.rad * sin(p.arc); + return outc; + } + + template + gctl::point3s c2s(const gctl::point3c& c) + { + point3s outs; + outs.rad = c.module(); + if (outs.rad <= GCTL_ZERO) //点距离原点极近 将点置于原点 + { + throw runtime_error("The point is at the origin. From point3c::c2s()"); + } + + outs.lat = 90.0 - acos(c.z/outs.rad)*180.0/GCTL_Pi; + outs.lon = atan2(c.y, c.x)*180.0/GCTL_Pi; + return outs; + } + + template + gctl::point3c s2c(const gctl::point3s& s) + { + //point3c outc; + //outc.x = rad*sin((0.5 - lat/180.0)*GCTL_Pi)*cos((2.0 + lon/180.0)*GCTL_Pi); + //outc.y = rad*sin((0.5 - lat/180.0)*GCTL_Pi)*sin((2.0 + lon/180.0)*GCTL_Pi); + //outc.z = rad*cos((0.5 - lat/180.0)*GCTL_Pi); + //return outc; + + point3c v; + v.x = s.rad*cos(GCTL_Pi*s.lat/180.0)*cos(GCTL_Pi*s.lon/180.0); + v.y = s.rad*cos(GCTL_Pi*s.lat/180.0)*sin(GCTL_Pi*s.lon/180.0); + v.z = s.rad*sin(GCTL_Pi*s.lat/180.0); + return v; + } + // this variable is only valid in this source file to control // the output of a 2D or 3D vertex. static char vertex_delimiter = ' '; diff --git a/lib/utility.h b/lib/utility.h index c550d65..03a1df1 100644 --- a/lib/utility.h +++ b/lib/utility.h @@ -28,13 +28,11 @@ #ifndef _GCTL_UTILITY_H #define _GCTL_UTILITY_H -#include "utility/stream_t.h" -#include "utility/stream.h" +#include "utility/assess.h" #include "utility/utc_time.h" #include "utility/get_option.h" #include "utility/progress_bar.h" #include "utility/getopt_help.h" -#include "utility/assess.h" #include "utility/process_monitor.h" #endif // _GCTL_UTILITY_H \ No newline at end of file diff --git a/lib/utility/get_option.cpp b/lib/utility/get_option.cpp index 3c3db1f..18552f3 100644 --- a/lib/utility/get_option.cpp +++ b/lib/utility/get_option.cpp @@ -27,7 +27,6 @@ #include "get_option.h" - void gctl::parse_key_value(std::string in_str, std::string separator, std::string &key, std::string &value) { diff --git a/lib/utility/get_option.h b/lib/utility/get_option.h index b4b3fa1..b35c64d 100644 --- a/lib/utility/get_option.h +++ b/lib/utility/get_option.h @@ -28,15 +28,8 @@ #ifndef _GCTL_GETOPTION_H #define _GCTL_GETOPTION_H -// system's head file -#include -#include -#include -#include - -// library's head file #include "../core/array.h" -#include "stream.h" +#include "../io/file_io.h" namespace gctl { diff --git a/lib/utility/getopt_help.h b/lib/utility/getopt_help.h index f80a9cf..e6d65fe 100644 --- a/lib/utility/getopt_help.h +++ b/lib/utility/getopt_help.h @@ -36,7 +36,8 @@ #include "getopt.h" #endif -#include "stream.h" +#include "../core/str.h" +#include "../io/term_io.h" namespace gctl { diff --git a/lib/utility/progress_bar.h b/lib/utility/progress_bar.h index 18ad670..e4b5ec4 100644 --- a/lib/utility/progress_bar.h +++ b/lib/utility/progress_bar.h @@ -40,7 +40,8 @@ #include #include -#include "stream_t.h" +#include "../core/exceptions.h" +#include "../io/term_io.h" //进度条宏定义 #define TOTAL_PERCENTAGE 100.0 ///< 设置总的百分比为100%。