Program Listing for File Vertex.h

Return to documentation for file (include/gamer/Vertex.h)

/*
 * ***************************************************************************
 * This file is part of the GAMer software.
 * Copyright (C) 2016-2019
 * by Christopher Lee, John Moody, Rommie Amaro, J. Andrew McCammon,
 *    and Michael Holst
 *
 * This library is free software; 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.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 * ***************************************************************************
 */

#pragma once

#include <iostream>
#include <sstream>
#include <stdexcept>

#include "gamer/gamer.h"
#include "gamer/tensor.h"

namespace gamer
{
struct Vertex
{
    Vector position;
    int    marker   = 0;
    bool   selected = false;
    Vertex() : Vertex(0, 0, 0) {}

    Vertex(REAL x, REAL y, REAL z) : Vertex(x, y, z, -1, false){}

    Vertex(REAL x, REAL y, REAL z, int m, bool sel)
    {
        position[0] = x;
        position[1] = y;
        position[2] = z;
        marker   = m;
        selected = sel;
    }

    Vertex(Vector &v) : position(v), marker(-1), selected(false){}

    Vertex(Vector &&v) : position(std::move(v)), marker(-1), selected(false) {}

    Vertex(const Vertex &x) : position(x.position), marker(x.marker), selected(x.selected){}

    Vertex(const Vertex &&x) : position(std::move(x.position)), marker(std::move(x.marker)), selected(std::move(x.selected)) {}

    operator Vector() const {
        return position;
    }

    friend std::ostream &operator<<(std::ostream &output, const Vertex &v)
    {
        output << "Vertex(x:" << v[0]
               << ",y:" << v[1]
               << ",z:" << v[2]
               << ";m:" << v.marker
               << ";sel:" << v.selected
               << ")";
        return output;
    }

    std::string to_string() const
    {
        std::ostringstream output;
        output << *this;
        return output.str();
    }

    const REAL &operator[](std::size_t index) const
    {
        return position[index];
    }

    REAL &operator[](std::size_t index)
    {
        return position[index];
    }

    void operator=(const Vertex &v)
    {
        position = v.position;
        marker   = v.marker;
        selected = v.selected;
    }

    bool operator==(const Vertex &rhs) const
    {
        Vertex temp(rhs);
        if (position != temp.position) return false;
        if (marker != temp.marker) return false;
        if (selected != temp.selected) return false;
        return true;
    }

    bool operator!=(const Vertex &rhs) const
    {
        return !(*this == rhs);
    }

    Vertex &operator+=(const Vector &rhs)
    {
        // retains the marker of the lhs
        position += rhs;
        return *this;
    }

    Vertex &operator-=(const Vector &rhs)
    {
        // retains the marker of the lhs
        position -= rhs;
        return *this;
    }

    Vertex &operator*=(const REAL x)
    {
        position *= x;
        return *this;
    }

    Vertex &operator/=(const REAL x)
    {
        position /= x;
        return *this;
    }
};

Vertex operator+(const Vertex &A, const Vector &B);

Vertex operator-(const Vertex &A, const Vector &B);

Vector operator-(const Vertex &A, const Vertex &B);

Vertex operator*(REAL x, const Vertex &A);

Vertex operator*(const Vertex &A, REAL x);

Vertex operator/(const Vertex &A, REAL x);

REAL distance(const Vertex &A, const Vertex &B);

REAL angle(const Vertex &A, const Vertex &B, const Vertex &C);

REAL angle(const Vector &AB, const Vector &CB);

// REAL signed_angle(const Vertex &A, const Vertex &B, const Vertex &C);
REAL signed_angle(const Vector &v1, const Vector &v2, const Vector &reference);

REAL angleDeg(const Vertex &A, const Vertex &B, const Vertex &C);

REAL angleDeg(const Vector &AB, const Vector &CB);


inline REAL length(const Vector &A)
{
    return std::sqrt(A|A);
}

inline void normalize(Vector &A)
{
    REAL mag = length(A);
    if (mag == 0)
        throw std::runtime_error("Cannot normalize a vector with length of 0.");
    A /= mag;
}

} //end namespace gamer