/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ * ██║ ██║██║ ██║ ██║ * ╚██████╔╝╚██████╗ ██║ ███████╗ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ * 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_TRIANGLE2D_H #define _GCTL_TRIANGLE2D_H #include "vertex.h" #include "entity.h" namespace gctl { // Declaration of the basic triangle2d type template struct type_triangle2d; // triangle2d type of attribute type of void typedef type_triangle2d triangle2d; /** * @brief Structure of a 2D triangle under the Cartesian coordinates * */ template struct type_triangle2d : public entity { type_triangle2d *neigh[3]; /** * @brief Constructor */ type_triangle2d(); /** * @brief Set object from parameters * * @warning This function will locate memories to store vertice * * @param[in] p0 The first point * @param[in] p1 The second point * @param[in] p2 The third point * @param[in] index The element index */ type_triangle2d(const point2dc &p0, const point2dc &p1, const point2dc &p2, int index = 0); /** * @brief Set object parameters * * @param vert0 The vertex 0 * @param vert1 The vertex 1 * @param vert2 The vertex 2 * @param[in] index The index */ type_triangle2d(vertex2dc &vert0, vertex2dc &vert1, vertex2dc &vert2, int index = 0); /** * @brief De-constructor */ virtual ~type_triangle2d(){} /** * @brief Set object from parameters * * @warning This function will locate memories to store vertice * * @param[in] p0 The first point * @param[in] p1 The second point * @param[in] p2 The third point * @param[in] index The element index */ void set(const point2dc &p0, const point2dc &p1, const point2dc &p2, int index = 0); /** * @brief Set object parameters * * @param vert0 The vertex 0 * @param vert1 The vertex 1 * @param vert2 The vertex 2 * @param[in] index The index */ void set(vertex2dc &vert0, vertex2dc &vert1, vertex2dc &vert2, int index = 0); /** * @brief Reset the structure to initial state */ void reset(); /** * @brief Calculate the triangle's area * * @return area */ double area(); /** * @brief Return the centroid of the 2D triangle * * @return point location */ point2dc centroid(); }; template type_triangle2d::type_triangle2d() : entity::entity() { neigh[0] = neigh[1] = neigh[2] = nullptr; } template type_triangle2d::type_triangle2d(const point2dc &p0, const point2dc &p1, const point2dc &p2, int index) : type_triangle2d() { set(p0, p1, p2, index); } template type_triangle2d::type_triangle2d(vertex2dc &vert0, vertex2dc &vert1, vertex2dc &vert2, int index) : type_triangle2d() { set(vert0, vert1, vert2, index); } template void type_triangle2d::set(const point2dc &p0, const point2dc &p1, const point2dc &p2, int index) { if (index < 0) { throw out_of_range("Invalid index number, From type_triangle2d::set(...)"); } for (int i = 0; i < 3; ++i) { this->vert[i] = new vertex2dc; } this->self_host = true; this->id = index; this->vert[0]->set(p0, 0); this->vert[1]->set(p1, 1); this->vert[2]->set(p2, 2); return; } template void type_triangle2d::set(vertex2dc &vert0, vertex2dc &vert1, vertex2dc &vert2, int index) { if (index < 0) { throw out_of_range("Invalid index, From type_triangle2d::set(...)"); } this->id = index; this->vert[0] = &vert0; this->vert[1] = &vert1; this->vert[2] = &vert2; return; } template void type_triangle2d::reset() { entity::reset(); neigh[0] = neigh[1] = neigh[2] = nullptr; return; } template double type_triangle2d::area() { double edge_len[3] = {0.0, 0.0, 0.0}; edge_len[0] = distance(*this->vert[0], *this->vert[1]); edge_len[1] = distance(*this->vert[1], *this->vert[2]); edge_len[2] = distance(*this->vert[2], *this->vert[0]); // calculate element's area double p = 0.5*(edge_len[0] + edge_len[1] + edge_len[2]); return sqrt(p*(p-edge_len[0])*(p-edge_len[1])*(p-edge_len[2])); } template point2dc type_triangle2d::centroid() { point2dc out; out.x = (this->vert[0]->x + this->vert[1]->x + this->vert[2]->x) / 3.0; out.y = (this->vert[0]->y + this->vert[1]->y + this->vert[2]->y) / 3.0; return out; } template void copy_type_triangle2d(type_triangle2d *tar, const type_triangle2d *src) { copy_entity(tar, src); for (size_t i = 0; i < 3; i++) { tar->neigh[i] = src->neigh[i]; } return; } template void copy_type_triangle2d(type_triangle2d *tar, const type_triangle2d *src) { copy_entity(tar, src); return; } }; #endif // _GCTL_TRIANGLE2D_H