/********************************************************
 *  ██████╗  ██████╗████████╗██╗
 * ██╔════╝ ██╔════╝╚══██╔══╝██║
 * ██║  ███╗██║        ██║   ██║
 * ██║   ██║██║        ██║   ██║
 * ╚██████╔╝╚██████╗   ██║   ███████╗
 *  ╚═════╝  ╚═════╝   ╚═╝   ╚══════╝
 * 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_EDGE_H
#define _GCTL_EDGE_H

#include "vertex.h"
#include "entity.h"

namespace gctl
{
    // Declaration of the basic edge type
    template <typename A> struct type_edge;
    // edge type of attribute type of void
    typedef type_edge<void> edge;

	/**
	 * @brief      Structure of a edge.
	 * 
	 * 边结构体。包含边的编号与顶点的指针。
	 */
    template <typename A>
	struct type_edge : public entity<vertex3dc, 2, A>
	{
        /**
         * 构造函数
         */
		type_edge();
        /**
         * @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]  index  The element index
         */
        type_edge(const point3dc &p0, const point3dc &p1, int index = 0);
        /**
         * @brief      构造函数重载
         *
         * @param[in]  index     索引值
         * @param      vert0  顶点0的指针
         * @param      vert1  顶点1的指针
         */
		type_edge(vertex3dc &vert0, vertex3dc &vert1, int index = 0);
        /**
         * @brief      析构函数
         */
		virtual ~type_edge(){}
        /**
         * @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]  index  The element index
         */
        void set(const point3dc &p0, const point3dc &p1, int index = 0);
        /**
         * @brief      设置三角形的索引与顶点列表
         *
         * @param[in]  index     索引值
         * @param      vert0  顶点0的指针
         * @param      vert1  顶点1的指针
         */
		void set(vertex3dc &vert0, vertex3dc &vert1, int index = 0);
	};

    template <typename A>
    type_edge<A>::type_edge() : entity<vertex3dc, 2, A>::entity(){}

    template <typename A>
    type_edge<A>::type_edge(const point3dc &p0, const point3dc &p1, int index) : type_edge()
    {
        set(p0, p1, index);
    }

    template <typename A>
    type_edge<A>::type_edge(vertex3dc &vert0, vertex3dc &vert1, int index) : type_edge()
    {
        set(vert0, vert1, index);
    }

    template <typename A>
    void type_edge<A>::set(const point3dc &p0, const point3dc &p1, int index)
    {
        if (index < 0)
        {
            throw out_of_range("Invalid index number, From type_edge::set(...)");
        }

        for (int i = 0; i < 2; ++i)
        {
            this->vert[i] = new vertex3dc;
        }
        this->self_host = true;

        this->id = index;
        this->vert[0]->set(p0, 2*index);
        this->vert[1]->set(p1, 2*index + 1);
        return;
    }

    template <typename A>
    void type_edge<A>::set(vertex3dc &vert0, vertex3dc &vert1, int index)
    {
        if (index < 0)
        {
            throw out_of_range("Invalid index number, From type_edge::set(...)");
        }

        this->id = index;
        if (vert0.id < vert1.id)
        {
            this->vert[0] = &vert0;
            this->vert[1] = &vert1;
        }
        else
        {
            this->vert[0] = &vert1;
            this->vert[1] = &vert0;
        }
        return;
    }
};

#endif // _GCTL_EDGE_H