Newer
Older
cg / hw04 / src / Vertex.cpp
// Copyright (c) 2021. Pascal Syma <pascal@syma.dev> and Antonio Martinez Casadesus <acasadesus@stud.hs-bremen.de>.
// All rights reserved.

//
// Created by Pascal on 17.05.2021.
//

#include <algorithm>
#include "Vertex.h"
using namespace std;

Vertex::Vertex() { // empty constructor

    this->valence = 0;
}

Vertex::Vertex(Mesh* mesh) : Vertex() {
    this->mesh = mesh;
}

Vertex::Vertex(Mesh* mesh, float x, float y, float z) : Vertex(mesh) { // constructor with initialization
    p[0] = x;
    p[1] = y;
    p[2] = z;
}

Vertex::Vertex(Mesh* mesh, float *point) : Vertex(mesh) {
    copy_n(point, 3, p);
}

Vertex operator+( Vertex a, Vertex b) { // add pts or vectors
    return *new Vertex(a.mesh, a.p[0] + b.p[0], a.p[1] + b.p[1],a.p[2] + b.p[2]);
}
Vertex operator-( Vertex a, Vertex b) { // subtract pts or vectors
    return *new Vertex(a.mesh, a.p[0] - b.p[0], a.p[1] - b.p[1],a.p[2] - b.p[2]);
}
Vertex operator*( float a, Vertex b) { // product between scalar and vector
    return *new Vertex(b.mesh, a*b.p[0], a*b.p[1], a*b.p[2]);
}
float operator*( Vertex a, Vertex b) { // scalar product
    return a.p[0] * b.p[0] + a.p[1] * b.p[1] + a.p[2] * b.p[2];
}
Vertex operator%( Vertex a, Vertex b) { // cross product
    return *new Vertex(a.mesh, a.p[1]*b.p[2] - a.p[2]*b.p[1], a.p[2]*b.p[0] - a.p[0]*b.p[2], a.p[0]*b.p[1] - a.p[1]*b.p[0]);
}
bool operator==(Vertex a, Vertex b) { // check if coords are the same
    return (a.p[0] == b.p[0]) && (a.p[1] == b.p[1]) && (a.p[2] == b.p[2]);
}
void Vertex::operator*=( float a) {
    this->p[0] *= a;
    this->p[1] *= a;
    this->p[2] *= a;
}
void Vertex::operator+=( Vertex a) {
    this->p[0] += a.p[0];
    this->p[1] += a.p[1];
    this->p[2] += a.p[2];
}

Vertex *Vertex::copy(Mesh *mesh) {
    auto *copy = new Vertex(mesh);
    copy->valence = this->valence;
    for (int i = 0; i < 3; ++i) {
        copy->p[i] = this->p[i];
    }

    return copy;
}