/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ * ██║ ██║██║ ██║ ██║ * ╚██████╔╝╚██████╗ ██║ ███████╗ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ * 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_TRIANGLE2D2O_H #define _GCTL_TRIANGLE2D2O_H #include "vertex.h" #include "entity.h" namespace gctl { // Declaration of the basic triangle2d2o type template struct type_triangle2d2o; // triangle2d type of attribute type of void typedef type_triangle2d2o triangle2d2o; /** * @brief Structure of a second order 2D triangle under the Cartesian coordinates * * Vertex's order (Same as Triangle, We also use this order) * 2 * / \ * / \ * / \ * 4 3 * / \ * / \ * / \ * 0-------5-------1 * * Vertex's order (Same as Gmsh) * 2 * / \ * / \ * / \ * 5 4 * / \ * / \ * / \ * 0-------3-------1 * */ template struct type_triangle2d2o : public entity { type_triangle2d2o *neigh[3]; /** * @brief Constructor */ type_triangle2d2o(); /** * @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_triangle2d2o(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 vert3 The vertex 3 * @param vert4 The vertex 4 * @param vert5 The vertex 5 * @param[in] index The index */ type_triangle2d2o(vertex2dc &vert0, vertex2dc &vert1, vertex2dc &vert2, vertex2dc &vert3, vertex2dc &vert4, vertex2dc &vert5, int index = 0); /** * @brief De-constructor */ virtual ~type_triangle2d2o(){} /** * @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 vert3 The vertex 3 * @param vert4 The vertex 4 * @param vert5 The vertex 5 * @param[in] index The index */ void set(vertex2dc &vert0, vertex2dc &vert1, vertex2dc &vert2, vertex2dc &vert3, vertex2dc &vert4, vertex2dc &vert5, 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(); /** * @brief Check if the object is valid * * @return true Valid * @return false Not valid */ bool validate(); /** * @brief Return vertex pointer in Gmsh's order * * @param id index of the object's vertex from 0 to 5 * @return vertex2dc* Vertex pointer */ vertex2dc* out_in_gmsh_order(size_t id); }; template type_triangle2d2o::type_triangle2d2o() : entity::entity() { neigh[0] = neigh[1] = neigh[2] = nullptr; } template type_triangle2d2o::type_triangle2d2o(const point2dc &p0, const point2dc &p1, const point2dc &p2, int index) : type_triangle2d2o() { set(p0, p1, p2, index); } template type_triangle2d2o::type_triangle2d2o(vertex2dc &vert0, vertex2dc &vert1, vertex2dc &vert2, vertex2dc &vert3, vertex2dc &vert4, vertex2dc &vert5, int index) : type_triangle2d2o() { set(vert0, vert1, vert2, vert3, vert4, vert5, index); } template void type_triangle2d2o::set(const point2dc &p0, const point2dc &p1, const point2dc &p2, int index) { if (index < 0) { throw out_of_range("Invalid index number, From type_triangle2d2o::set(...)"); } for (int i = 0; i < 6; ++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); this->vert[3]->set(0.5*(p1+p2), 3); this->vert[4]->set(0.5*(p2+p0), 4); this->vert[5]->set(0.5*(p0+p1), 5); return; } template void type_triangle2d2o::set(vertex2dc &vert0, vertex2dc &vert1, vertex2dc &vert2, vertex2dc &vert3, vertex2dc &vert4, vertex2dc &vert5, int index) { if (index < 0) { throw out_of_range("Invalid index, From type_triangle2d2o::set(...)"); } this->id = index; this->vert[0] = &vert0; this->vert[1] = &vert1; this->vert[2] = &vert2; this->vert[3] = &vert3; this->vert[4] = &vert4; this->vert[5] = &vert5; if (!validate()) { throw invalid_argument("Invalid object setup. From type_triangle2d2o::set(...)"); } return; } template void type_triangle2d2o::reset() { entity::reset(); neigh[0] = neigh[1] = neigh[2] = nullptr; return; } template double type_triangle2d2o::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_triangle2d2o::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 bool type_triangle2d2o::validate() { if (*this->vert[3] != 0.5*(*this->vert[1] + *this->vert[2]) || *this->vert[4] != 0.5*(*this->vert[2] + *this->vert[0]) || *this->vert[5] != 0.5*(*this->vert[0] + *this->vert[1])) { return false; } return true; } template vertex2dc* type_triangle2d2o::out_in_gmsh_order(size_t id) { if (id > 5) { throw invalid_argument("The local index is from 0 to 5. From type_triangle2d2o::out_in_gmsh_order(...)"); } if (id < 3) return this->vert[id]; return this->vert[3+(id+2)%3]; } template void copy_type_triangle2d2o(type_triangle2d2o *tar, const type_triangle2d2o *src) { copy_entity(tar, src); for (size_t i = 0; i < 3; i++) { tar->neigh[i] = src->neigh[i]; } return; } template void copy_type_triangle2d2o(type_triangle2d2o *tar, const type_triangle2d2o *src) { copy_entity(tar, src); return; } }; #endif // _GCTL_TRIANGLE2D2O_H