gctl/lib/math/geometry2d.h
2025-04-24 12:57:06 +08:00

238 lines
9.1 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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_GEOMETRY2D_H
#define _GCTL_GEOMETRY2D_H
#include "../poly/triangle2d.h"
#include "../poly/edge2d.h"
namespace gctl
{
namespace geometry2d
{
/**
* @brief 计算点t是否在直线ab上
*
* @param[in] a 点a
* @param[in] b 点b
* @param[in] t 待检测点t
* @param[in] cut_off 检测精度
*
* @return 点t是否在直线ab上
*/
bool collinear(const point2dc &a, const point2dc &b, const point2dc &t,
double cut_off = GCTL_ZERO);
/**
* @brief 计算直线(射线)外一点在线上的投影点
*
* @note。 射线方向为 p1->p2
*
* @param[in] p1 直线(射线)上的第一点
* @param[in] p2 直线(射线)上的第二点
* @param[in] out_p 线外的一点
* @param on_p out_p 在线上的投影点
*
* @return 投影点在射线的负方向为假,否则为真
*/
bool dot_on_line(const point2dc &p1, const point2dc &p2, const point2dc &out_p, point2dc &on_p);
// 求两条直线的交点。
/**
* @brief 求两条直线的交点
*
* @note 如果两条直线平行则无交点out值将无变化
*
* @param[in] l1_p1 直线1上的第一点
* @param[in] l1_p2 直线1上的第二点
* @param[in] l2_p1 直线2上的第一点
* @param[in] l2_p2 直线2上的第二点
* @param out 交点
*/
void line_intersect(const point2dc &l1_p1, const point2dc &l1_p2, const point2dc &l2_p1, const point2dc &l2_p2, point2dc &out);
/**
* @brief 求两条直线的交点。
*
* @param[in] l1_p1 直线1上的第一个点
* @param[in] l1_p2 直线1上的第二个点
* @param[in] l2_p1 直线2上的第一个点
* @param[in] l2_p2 直线2上的第二个点
*
* @return 交点坐标,如果两条直线平行则返回(NaN, NaN)
*/
point2dc line_intersect(const point2dc &l1_p1, const point2dc &l1_p2,
const point2dc &l2_p1, const point2dc &l2_p2);
/**
* @brief 计算三角形内一点到三条边的垂线将三角形分割所得的三个菱形区域的面积
*
* @param t_p1 三角形的顶点1
* @param t_p2 三角形的顶点2
* @param t_p3 三角形的顶点3
* @param in_p 三角形内的一点
* @param a1 顶点1所在的菱形区域的面积
* @param a2 顶点2所在的菱形区域的面积
* @param a3 顶点3所在的菱形区域的面积
*/
void tri_split_area(const point2dc &t_p1, const point2dc &t_p2, const point2dc &t_p3, const point2dc &in_p,
double &a1, double &a2, double &a3);
/**
* @brief 距离反比形式的三角形内插值函数
*
* @param[in] p 待插值点坐标
* @param[in] p1 三角形顶点坐标1
* @param[in] p2 三角形顶点坐标2
* @param[in] p3 三角形顶点坐标3
* @param[in] d1 顶点1的值
* @param[in] d2 顶点2的值
* @param[in] d3 顶点3的值
* @param[in] w 距离反比阶次默认为2
*
* @return 插值点的值
*/
double tri_interp_dist(const point2dc &p, const point2dc &p1, const point2dc &p2,
const point2dc &p3, double d1, double d2, double d3, double w = 2.0);
/**
* @brief 细分三角形面积比形式的三角形内插值函数
*
* @param[in] p 待插值点坐标
* @param[in] p1 三角形顶点坐标1
* @param[in] p2 三角形顶点坐标2
* @param[in] p3 三角形顶点坐标3
* @param[in] d1 顶点1的值
* @param[in] d2 顶点2的值
* @param[in] d3 顶点3的值
*
* @return 插值点的值
*/
double tri_interp_area(const point2dc &p, const point2dc &p1, const point2dc &p2,
const point2dc &p3, double d1, double d2, double d3);
/**
* @brief 利用平面方程计算二维坐标上三点数据所确定的方向导数
*
* @param p1 三角形顶点坐标1
* @param p2 三角形顶点坐标2
* @param p3 三角形顶点坐标3
* @param d1 顶点1的值
* @param d2 顶点2的值
* @param d3 顶点3的值
* @param dx 计算所得的x方向导数
* @param dy 计算所得的y方向导数
*/
void tri_plane_gradient(const point2dc &p1, const point2dc &p2, const point2dc &p3,
const double &d1, const double &d2, const double &d3, double &dx, double &dy);
/**
* @brief 判断一个点是否在三角形内(二维)
*
* @param test_p 待检测点
* @param p1 三角形的第一个点
* @param p2 三角形的第二个点
* @param p3 三角形的第三个点
*
* @return 待检测点是否在三角形内
*/
bool node_in_triangle(const point2dc &test_p, const point2dc &p1,
const point2dc &p2, const point2dc &p3, bool on_boundary = true);
/**
* @brief 判断一个点是否在多边形内
*
* @param[in] poly_ps 多边形的边数组
* @param[in] one_p 待检测点
* @param on_boundary 是否包含点落在多边形边上的情况
*
* @return 在多边形为真,否则返回假
*/
bool node_in_polygon(const array<point2dc> &poly_ps, const point2dc &one_p, bool on_boundary = true);
/**
* @brief Get common edges of a triangular mesh.
*
* @param[in] input_ele The input elements
* @param out_edge The output edges
*/
void get_common_edges(const array<triangle2d> &input_ele, array<edge2d> &out_edge);
/**
* @brief Get number of vertex of a group of triangles
*
* @param[in] input_ele The input elements
*
* @return Number of vertex
*/
int sort_node_number(const array<triangle2d> &input_ele);
/**
* @brief Sort triangular neighbors for the mesh's vertex
*
* @param[in] input_ele The input element
* @param out_node_neigh The output node's neighbors of triangle
*/
void sort_node_neighbor(const array<triangle2d> &input_ele, std::vector<std::vector<triangle2d*> > &out_node_neigh, int node_num);
/**
* @brief Sort neighbors for triangle elements
*
* @param[in] input_ele The input elements
*/
void sort_triangle_neighbor(array<triangle2d> &input_ele, const std::vector<std::vector<triangle2d*> > &node_neighs);
/**
* @brief 使用直线切割二维三角网络
*
* @note 直线左侧的网络将被保留直线的方向为p12的射线方向
*
* @param[in] in_eles 输入的二维三角形数组(一般为成片的三角网)
* @param[in] p1 直线上的第一点
* @param[in] p2 直线上的第二点
* @param out_nodes 切割后的新网络顶点集
* @param out_eles 切割后的新网络三角形元素集
*/
void cut_triangular_mesh_2d(const array<triangle2d> &in_eles, const point2dc &p1, const point2dc &p2,
array<vertex2dc> &out_nodes, array<triangle2d> &out_eles);
/**
* @brief 提取二维三角网络的自网络(一部分)
*
* @param[in] in_eles 输入的二维三角形数组(一般为成片的三角网
* @param[in] out_list 长度等于三角形个数的布尔数组,元素为真则相应的三角形将被提取
* @param out_nodes 提取后的新网络顶点集
* @param out_eles 提取后的新网络三角形元素集
*/
void extract_triangular_mesh_2d(const array<triangle2d> &in_eles, const array<bool> &out_list,
array<vertex2dc> &out_nodes, array<triangle2d> &out_eles);
}
};
#endif // _GCTL_GEOMETRY2D_H