This commit is contained in:
张壹 2025-04-23 12:39:44 +08:00
parent 0e1f69c3d3
commit 11d29c3dd4
109 changed files with 966 additions and 1320 deletions

View File

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.15.2)
#
project(GCTL VERSION 1.0)
project(GCTL VERSION 1.1)
#
include(CMakePackageConfigHelpers)

View File

@ -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_winGNU getopt
# linux
@ -24,11 +23,11 @@ endif()
#
#
# libcmake
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")
#

View File

@ -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 <http://www.gnu.org/licenses/>.
*
* 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

View File

@ -37,5 +37,6 @@
#include "core/matrix.h"
#include "core/spmat.h"
#include "core/sparray.h"
#include "core/str.h"
#endif // _GCTL_CORE_H

207
lib/core/str.cpp Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
* 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<std::string> &str_vec)
{
std::vector<std::string> 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;
}

View File

@ -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 <string>
#include "exceptions.h"
#include <cmath>
#include <cstring>
#include <iostream>
#include <fstream>
#include <sstream>
#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 <vector>
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 <typename ValueType>
int parse_string_to_vector(std::string val_str, char separator, std::vector<ValueType> &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<ValueType, std::string>::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
/**
* @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 <typename ValueType>
int parse_string_to_vector(std::string val_str, char separator, std::vector<ValueType> &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<ValueType, std::string>::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<std::string> &str_vec);
};
#endif // _GCTL_STR_H

View File

@ -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 <http://www.gnu.org/licenses/>.
*
* 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

View File

@ -36,7 +36,7 @@
#include <random>
#include <algorithm>
#include "../utility/stream_t.h"
#include "../io/term_io.h"
namespace gctl
{

View File

@ -34,8 +34,8 @@
#include <ctime>
#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

View File

@ -33,7 +33,7 @@
#include <stdexcept>
#include <ctime>
#include "../utility/stream.h"
#include "../io/file_io.h"
namespace gctl
{

View File

@ -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

View File

@ -26,7 +26,6 @@
******************************************************/
#include "dsv_io.h"
#include "cli_viewer.h"
gctl::dsv_io::dsv_io()
{

View File

@ -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

View File

@ -1,4 +1,4 @@
/********************************************************
/********************************************************
*
*
*
@ -25,166 +25,7 @@
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#include <cmath>
#if defined _WINDOWS || __WIN32__
#include <io.h>
// 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<std::string> &str_vec)
{
std::vector<std::string> 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("."));

View File

@ -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 <cstring>
#include <iostream>
#include <fstream>
#include <iomanip>
#include <ctime>
#include <stdio.h>
#ifndef _GCTL_FILE_IO_H
#define _GCTL_FILE_IO_H
#ifdef _WINDOWS
#include <windows.h>
#else
#include <sys/ioctl.h>
#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<std::string> &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
#endif // _GCTL_FILE_IO_H

View File

@ -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"

View File

@ -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";

View File

@ -29,16 +29,12 @@
#define _GCTL_MESH_IO_H
#include <unordered_set>
#include <map>
// 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
{

View File

@ -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
{

View File

@ -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
{
/**

View File

@ -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
{

View File

@ -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
{

View File

@ -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
{

View File

@ -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
{

View File

@ -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 <io.h>
// 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()
{

103
lib/io/term_io.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*
* 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 <iostream>
#include <fstream>
#include <vector>
#include <unistd.h>
#include <stdio.h>
#include <ncurses.h>
#ifdef _WINDOWS
#include <windows.h>
#else
#include <sys/ioctl.h>
#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<std::string>& data);
// 添加显示内容
void addData(const std::string& l);
// 显示文件内容并进入查看循环
void display();
private:
std::vector<std::string> lines; // 存储文件内容
};
};
#endif // _GCTL_TERM_IO_H

View File

@ -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
{

View File

@ -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

View File

@ -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
{

View File

@ -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 <fstream>
#include <iostream>
#include <vector>
#include <ncurses.h>
#include "math/mathfunc.h"
#include "math/algorithm_func.h"
namespace gctl
{
// 命令行查看器,用于在终端内查看文本文件内容,支持上下左右鼠标滚动或按键导航和退出查看
class cli_viewer
{
public:
// 构造函数
cli_viewer();
// 析构函数
~cli_viewer();
// 设置显示内容
void setData(const std::vector<std::string>& data);
// 添加显示内容
void addData(const std::string& l);
// 显示文件内容并进入查看循环
void display();
#include "math/legendre.h"
#include "math/linear_algebra.h"
private:
std::vector<std::string> lines; // 存储文件内容
};
};
#include "math/heap_sort.h"
#include "math/boxsort2d.h"
#include "math/boxsort_sph.h"
#include "math/variogram.h"
#endif // _GCTL_CLI_VIEWER_H
#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

View File

@ -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
{

View File

@ -28,7 +28,6 @@
#ifndef _BOXSORT2D_H
#define _BOXSORT2D_H
#include "../core.h"
#include "heap_sort.h"
namespace gctl

View File

@ -28,7 +28,6 @@
#ifndef _BOXSORT_SPH_H
#define _BOXSORT_SPH_H
#include "../core.h"
#include "heap_sort.h"
namespace gctl

View File

@ -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

View File

@ -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
{

View File

@ -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

View File

@ -28,7 +28,6 @@
#ifndef _RIF_FILTER_H
#define _RIF_FILTER_H
#include "../core.h"
#include "windowfunc.h"
namespace gctl

View File

@ -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
{

View File

@ -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,

View File

@ -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
{

View File

@ -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<double> 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

View File

@ -28,7 +28,7 @@
#ifndef _GCTL_GLNI_H
#define _GCTL_GLNI_H
#include "../core.h"
#include <complex>
#include "../gctl_config.h"
namespace gctl

View File

@ -29,7 +29,7 @@
#define _GCTL_HEAPSORT_H
// library's head file
#include "../core.h"
#include "../core/array.h"
namespace gctl
{

View File

@ -509,8 +509,8 @@ gctl::array<double> *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 &&

View File

@ -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

View File

@ -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
{

View File

@ -36,10 +36,136 @@
// library's head files
#include "../core/macro.h"
#include "../core/spmat.h"
#include "mathfunc_t.h"
namespace gctl
{
template <typename T>
inline int sign(T d)
{
return (T(0) < d) - (d < T(0));
}
template <typename T>
inline T arc(T deg)
{
return deg*GCTL_Pi/180.0L;
}
template <typename T>
inline T deg(T arc)
{
return arc*180.0L/GCTL_Pi;
}
template <typename T>
inline T sind(T deg)
{
return sin(deg*GCTL_Pi/180.0L);
}
template <typename T>
inline T cosd(T deg)
{
return cos(deg*GCTL_Pi/180.0L);
}
template <typename T>
inline T tand(T deg)
{
return tan(deg*GCTL_Pi/180.0L);
}
template <typename T>
inline T power2(T in)
{
return (in)*(in);
}
template <typename T>
inline T power3(T in)
{
return (in)*(in)*(in);
}
template <typename T>
inline T power4(T in)
{
return (in)*(in)*(in)*(in);
}
template <typename T>
inline T power5(T in)
{
return (in)*(in)*(in)*(in)*(in);
}
template <typename T>
inline T jacoby2(T x00, T x01, T x10, T x11)
{
return x00*x11-x01*x10;
}
template <typename T>
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 <typename T>
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 <typename T>
T arctg(T v)
{
T ang;
if(v>=0) ang=atan(v);
else ang=atan(v)+GCTL_Pi;
return ang;
}
template <typename T>
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]
*

View File

@ -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;
}

View File

@ -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
{

View File

@ -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
{

View File

@ -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

View File

@ -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
{

View File

@ -28,7 +28,7 @@
#ifndef _WINDOWFUNC_H
#define _WINDOWFUNC_H
#include "../core.h"
#include "../core/array.h"
namespace gctl
{

View File

@ -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 <http://www.gnu.org/licenses/>.
*
* 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

View File

@ -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 <http://www.gnu.org/licenses/>.
*
* 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 <cmath>
#include "../core/matrix.h"
namespace gctl
{
template <typename T>
inline int sign(T d)
{
return (T(0) < d) - (d < T(0));
}
template <typename T>
inline T arc(T deg)
{
return deg*GCTL_Pi/180.0L;
}
template <typename T>
inline T deg(T arc)
{
return arc*180.0L/GCTL_Pi;
}
template <typename T>
inline T sind(T deg)
{
return sin(deg*GCTL_Pi/180.0L);
}
template <typename T>
inline T cosd(T deg)
{
return cos(deg*GCTL_Pi/180.0L);
}
template <typename T>
inline T tand(T deg)
{
return tan(deg*GCTL_Pi/180.0L);
}
template <typename T>
inline T power2(T in)
{
return (in)*(in);
}
template <typename T>
inline T power3(T in)
{
return (in)*(in)*(in);
}
template <typename T>
inline T power4(T in)
{
return (in)*(in)*(in)*(in);
}
template <typename T>
inline T power5(T in)
{
return (in)*(in)*(in)*(in)*(in);
}
template <typename T>
inline T jacoby2(T x00, T x01, T x10, T x11)
{
return x00*x11-x01*x10;
}
template <typename T>
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 <typename T>
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 <typename T>
T arctg(T v)
{
T ang;
if(v>=0) ang=atan(v);
else ang=atan(v)+GCTL_Pi;
return ang;
}
template <typename T>
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 <typename T>
void matrix_vector(const matrix<T> &mat, const array<T> &vec, array<T> &ret, T zero = 0, matrix_layout_e layout = NoTrans)
{
static_assert(std::is_arithmetic<T>::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 <typename T>
void matrix_matrix(const matrix<T> &mat, const matrix<T> &mat2, matrix<T> &ret, T zero = 0)
{
static_assert(std::is_arithmetic<T>::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 <typename T>
void linespace(const T &start, const T &end, unsigned int size, std::vector<T> &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 <typename T>
void gridspace(const T &xs, const T &xe, const T &ys, const T &ye, unsigned int xn,
unsigned int yn, std::vector<T> &out_vec)
{
if (xn < 1 || yn < 1)
throw invalid_argument("Invalid grid size. From gridspace(...)");
std::vector<T> 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 <typename T>
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<T> &out_vec)
{
if (xn < 1 || yn < 1 || zn < 1)
throw invalid_argument("Invalid grid size. From meshspace(...)");
array<T> 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 <typename T>
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 <typename T>
T average(const std::vector<T> &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 <typename T>
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 <typename T>
T deviation(const std::vector<T> &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 <typename T>
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 <typename T>
T root_mn_square(const std::vector<T> &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 <typename T>
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<T> dist(p1, p2);
for (int i = 0; i < size; i++)
{
val_ptr[i] = dist(generator);
}
return;
}
//添加均匀分布的随机值
std::uniform_real_distribution<T> dist(p1, p2);
for (int i = 0; i < size; i++)
{
val_ptr[i] = dist(generator);
}
return;
}
template <typename T>
void random(T p1, T p2, std::vector<T> &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<T> dist(p1, p2);
for (int i = 0; i < size; i++)
{
val_vec[i] = dist(generator);
}
return;
}
//添加均匀分布的随机值
std::uniform_real_distribution<T> dist(p1, p2);
for (int i = 0; i < size; i++)
{
val_vec[i] = dist(generator);
}
return;
}
template <typename T>
T normalize(array<T> &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;
}
*/

View File

@ -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 <typename T> struct point2c;
template <typename T> struct point2p;
template <typename T> struct point3c;
template <typename T> 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
#endif // _GCTL_POLY_H

View File

@ -28,7 +28,6 @@
#ifndef _GCTL_EDGE_H
#define _GCTL_EDGE_H
#include "point3c.h"
#include "vertex.h"
#include "entity.h"

View File

@ -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

View File

@ -33,7 +33,6 @@
// library head file
#include "../core/array.h"
namespace gctl
{
// this variable is only valid in this source file to control

View File

@ -28,7 +28,6 @@
#ifndef _GCTL_NODE_H
#define _GCTL_NODE_H
#include "point3c.h"
#include "vertex.h"
#include "entity.h"

View File

@ -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 <typename T> class point2c;
typedef point2c<double> point2dc;
typedef point2c<float> point2fc;
@ -108,7 +118,7 @@ namespace gctl
*
* @return The point2dp object.
*/
point2p<T> c2p() const;
//point2p<T> c2p() const;
/**
* @brief Set a point2c object from a string. The accepted format: (x, y)
*
@ -236,7 +246,7 @@ namespace gctl
{
return point2c<T>(x/module(), y/module());
}
/*
template <typename T>
gctl::point2p<T> gctl::point2c<T>::c2p() const
{
@ -247,7 +257,7 @@ namespace gctl
else outp.arc = atan2(y, x) + 2.0*GCTL_Pi;
return outp;
}
*/
template <typename T>
void gctl::point2c<T>::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 <typename T>
void gctl::point2c<T>::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 <typename T>
std::ostream &operator <<(std::ostream & os, const point2c<T> &a)
{
os << std::setprecision(io_psn) << a.x << " " << a.y;
os << std::setprecision(p2c_io_psn) << a.x << " " << a.y;
return os;
}

View File

@ -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 <typename T> class point2p;
typedef point2p<double> point2dp;
typedef point2p<float> point2fp;
@ -99,7 +110,7 @@ namespace gctl
*
* @return The point2c object.
*/
point2c<T> p2c() const;
//point2c<T> 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 <typename T>
gctl::point2c<T> gctl::point2p<T>::p2c() const
{
@ -216,7 +227,7 @@ namespace gctl
outc.y = rad * sin(arc);
return outc;
}
*/
template <typename T>
void gctl::point2p<T>::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 <typename T>
void gctl::point2p<T>::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 <typename T>
std::ostream &operator <<(std::ostream & os, const point2p<T> &a)
{
os << std::setprecision(io_psn) << a.rad << " " << a.arc;
os << std::setprecision(p2p_io_psn) << a.rad << " " << a.arc;
return os;
}

View File

@ -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 <typename T> class point3c;
typedef point3c<double> point3dc;
typedef point3c<float> point3fc;
@ -103,7 +112,7 @@ namespace gctl
*
* @return spherical coordinates point
*/
point3s<T> c2s() const;
//point3s<T> c2s() const;
/**
* @brief Return the normal vector
*
@ -254,7 +263,7 @@ namespace gctl
{
return sqrt(x*x + y*y + z*z);
}
/*
template <typename T>
gctl::point3s<T> gctl::point3c<T>::c2s() const
{
@ -269,7 +278,7 @@ namespace gctl
outs.lon = atan2(y, x)*180.0/GCTL_Pi;
return outs;
}
*/
template <typename T>
gctl::point3c<T> gctl::point3c<T>::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 <typename T>
void gctl::point3c<T>::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 <typename T>
std::ostream &operator <<(std::ostream & os, const point3c<T> &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;
}

View File

@ -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 <typename T> class point3s;
typedef point3s<double> point3ds;
typedef point3s<float> point3fs;
@ -53,7 +64,7 @@ namespace gctl
void set(T r, T ln, T lt);
void set(const point3s<T> &b);
double module() const;
point3c<T> s2c() const; // 转换为球坐标点
//point3c<T> 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 <typename T>
gctl::point3c<T> gctl::point3s<T>::s2c() const
{
/*
point3c<T> 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<T> 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<T> 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 <typename T>
void gctl::point3s<T>::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 <typename T>
void gctl::point3s<T>::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 <typename T>
std::ostream &operator <<(std::ostream & os, const point3s<T> &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;
}

View File

@ -28,7 +28,6 @@
#ifndef _GCTL_SPHERE_H
#define _GCTL_SPHERE_H
#include "point3c.h"
#include "vertex.h"
#include "entity.h"

View File

@ -28,8 +28,7 @@
#ifndef _GCTL_TENSOR_H
#define _GCTL_TENSOR_H
#include "../core.h"
#include "../core/matrix.h"
#include "point3c.h"
namespace gctl

View File

@ -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);

View File

@ -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;

Some files were not shown because too many files have changed in this diff Show More