/******************************************************** * ██████╗ ██████╗████████╗██╗ * ██╔════╝ ██╔════╝╚══██╔══╝██║ * ██║ ███╗██║ ██║ ██║ * ██║ ██║██║ ██║ ██║ * ╚██████╔╝╚██████╗ ██║ ███████╗ * ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝ * 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_VERTEX_H #define _GCTL_VERTEX_H #include "point2c.h" #include "point2p.h" #include "point3c.h" #include "point3s.h" 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 = ' '; template struct vertex; typedef vertex vertex2dc; typedef vertex vertex2dp; typedef vertex vertex2fc; typedef vertex vertex2fp; typedef vertex vertex3dc; typedef vertex vertex3ds; typedef vertex vertex3fc; typedef vertex vertex3fs; template struct vertex : public T { int id; A *att; /** * Constructor */ vertex(); /** * @brief Construct an object from a 2D point * * @param[in] p Input point * @param[in] idx Element's index. Must be equal or greater than 0. */ vertex(const T &p, int idx = 0, A* att_ptr = nullptr); /** * @brief Copy constructor * * @param[in] b Input vertex */ vertex(const vertex &b); /** * @brief De-constructor */ virtual ~vertex(); /** * @brief Set an object from a 2D point * * @param[in] p Input point * @param[in] idx Element's index. Must be equal or greater than 0. */ void set(const T &p, int idx = -1, A* att_ptr = nullptr); /** * @brief Copy an object * * @param[in] b Input vertex */ void set(const vertex &b); /** * @brief Extract the vertex's location * * @return The location */ T get_loc() const; /** * @brief Set delimiter for the output stream * * @param[in] deli delimiter */ void set_delimiter(char deli); }; template vertex::vertex() { id = 0; att = nullptr; } template vertex::vertex(const T &p, int idx, A* att_ptr) { set(p, idx, att_ptr); } template vertex::vertex(const vertex &b) { set(b); } template vertex::~vertex(){} template void vertex::set(const T &p, int idx, A* att_ptr) { if (idx >= 0) id = idx; if (att_ptr != nullptr) att = att_ptr; T::set(p); return; } template void vertex::set(const vertex &b) { id = b.id; att = b.att; T::set(b); // call T's set function by force } template T vertex::get_loc() const { return T::get_loc(); } template void vertex::set_delimiter(char deli) { vertex_delimiter = deli; return; } template std::ostream &operator <<(std::ostream & os, const vertex &a) { os << a.id << vertex_delimiter; a.out_loc(os, vertex_delimiter); return os; } template std::istream &operator >>(std::istream & os, vertex &a) { os >> a.id; a.in_loc(os); return os; } }; #endif // _GCTL_VERTEX_H