gctl/lib/poly/vertex.h

224 lines
6.6 KiB
C
Raw Normal View History

2024-09-10 15:45:07 +08:00
/********************************************************
*
*
*
*
*
*
* 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_VERTEX_H
#define _GCTL_VERTEX_H
#include "point2c.h"
#include "point2p.h"
#include "point3c.h"
#include "point3s.h"
namespace gctl
{
2025-04-23 12:39:44 +08:00
template <typename T>
gctl::point2p<T> c2p(const gctl::point2c<T>& c)
{
point2p<T> 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 <typename T>
gctl::point2c<T> p2c(const gctl::point2p<T>& p)
{
point2c<T> outc;
outc.x = p.rad * cos(p.arc);
outc.y = p.rad * sin(p.arc);
return outc;
}
template <typename T>
gctl::point3s<T> c2s(const gctl::point3c<T>& c)
{
point3s<T> 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 <typename T>
gctl::point3c<T> s2c(const gctl::point3s<T>& s)
{
//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 = 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;
}
2024-09-10 15:45:07 +08:00
// this variable is only valid in this source file to control
// the output of a 2D or 3D vertex.
static char vertex_delimiter = ' ';
template <typename T, typename A = void> struct vertex;
typedef vertex<point2dc> vertex2dc;
typedef vertex<point2dp> vertex2dp;
typedef vertex<point2fc> vertex2fc;
typedef vertex<point2fp> vertex2fp;
typedef vertex<point3dc> vertex3dc;
typedef vertex<point3ds> vertex3ds;
typedef vertex<point3fc> vertex3fc;
typedef vertex<point3fs> vertex3fs;
template <typename T, typename A>
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<T, A> &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<T, A> &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 <typename T, typename A>
vertex<T, A>::vertex()
{
id = 0;
att = nullptr;
}
template <typename T, typename A>
vertex<T, A>::vertex(const T &p, int idx, A* att_ptr)
{
set(p, idx, att_ptr);
}
template <typename T, typename A>
vertex<T, A>::vertex(const vertex<T, A> &b)
{
set(b);
}
template <typename T, typename A>
vertex<T, A>::~vertex(){}
template <typename T, typename A>
void vertex<T, A>::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 <typename T, typename A>
void vertex<T, A>::set(const vertex<T, A> &b)
{
id = b.id;
att = b.att;
T::set(b); // call T's set function by force
}
template <typename T, typename A>
T vertex<T, A>::get_loc() const
{
return T::get_loc();
}
template <typename T, typename A>
void vertex<T, A>::set_delimiter(char deli)
{
vertex_delimiter = deli;
return;
}
template <typename T, typename A>
std::ostream &operator <<(std::ostream & os, const vertex<T, A> &a)
{
os << a.id << vertex_delimiter; a.out_loc(os, vertex_delimiter);
return os;
}
template <typename T, typename A>
std::istream &operator >>(std::istream & os, vertex<T, A> &a)
{
os >> a.id; a.in_loc(os);
return os;
}
2025-02-06 21:17:24 +08:00
};
2024-09-10 15:45:07 +08:00
#endif // _GCTL_VERTEX_H