Untitled

mail@pastecode.io avatar
unknown
plain_text
4 years ago
3.4 kB
4
Indexable
Never
//
//  GPSLab1.cpp
//
//  Copyright © 2017 CGIS. All rights reserved.
//

#include "GPSLab1.hpp"
#include <math.h>   
#define PI 3.14159265
#include "iostream""

namespace gps {
    glm::vec4 TransformPoint(const glm::vec4 &point)
    {
        glm::mat4 matrix(1.0f);
        matrix = glm::rotate(matrix, glm::radians(90.0f), glm::vec3(1.0f,0.0f,0.0f));
        matrix = glm::translate(matrix, glm::vec3(2.0f, 0.0f, 1.0f));
        return matrix * point;
    }
    
    float ComputeAngle(const glm::vec3 &v1, const glm::vec3 &v2)
    {
        glm::vec3 a = glm::normalize(v1);
        glm::vec3 b = glm::normalize(v2);
        float len = glm::length(v1) * glm::length(v2);
        float dot = glm::dot(v1, v2);
                    //return glm::acos(glm::dot(a,b)/glm::length(v1)*glm::length(v2));
        return acos(dot / len) * 180.0 / PI;
    }
    
    glm::vec3 transf(glm::vec2 a, glm::vec2 b) {
        glm::vec3 rez;
        rez.x = b.x - a.x;
        rez.y = b.y - a.y;
        rez.z = 0;
        return rez;
    }

    bool IsConvex(const std::vector<glm::vec2> &vertices)
    {
        std::vector<glm::vec3> edges;
        for (int i = 0; i < vertices.size(); i++) {
            if (i == vertices.size() - 1) {
                glm::vec3 aux2 = transf(vertices.at(i), vertices.at(0));
                edges.push_back(aux2);
                break;
            }
            glm::vec3 aux1 = transf(vertices.at(i), vertices.at(i + 1));
            edges.push_back(aux1);
        }

        for (int i = 0; i < edges.size(); i++) {
            glm::vec3 rezultatul, rezultatul2;
            rezultatul = glm::cross(edges.at(i), edges.at(i + 1));
            rezultatul2 = glm::cross(edges.at(i+1), edges.at(i + 2));
            if (i + 2 == edges.size() - 1) {
                rezultatul = glm::cross(edges.at(i), edges.at(i + 1));
                rezultatul2 = glm::cross(edges.at(i + 1), edges.at(0));
            }

            if (i + 1 == edges.size() - 1) {
                rezultatul = glm::cross(edges.at(i), edges.at(0));
                rezultatul2 = glm::cross(edges.at(0), edges.at(1));
            }

            if (rezultatul.z < 0 && rezultatul2.z > 0) {
                return false;
            }
            else {
                if (rezultatul.z > 0 && rezultatul2.z < 0)
                    return false;
            }
        }
            return true;
    }
    
    std::vector<glm::vec2> ComputeNormals(const std::vector<glm::vec2> &vertices)
    {
        std::vector<glm::vec2> normalsList;
        std::vector<glm::vec3> edges;
        glm::vec2 aux;
        for (int i = 0; i < vertices.size(); i++) {
            if (i == vertices.size() - 1) {
                glm::vec3 aux2 = transf(vertices.at(i), vertices.at(0));
                edges.push_back(aux2);
                break;
            }
            glm::vec3 aux1 = transf(vertices.at(i), vertices.at(i + 1));
            edges.push_back(aux1);
        }

        glm::vec3 normalaPlan = glm::cross(edges.at(0), edges.at(1));
        for (int i = 0; i < vertices.size(); i++) {
            glm::vec3 normalaLat;
            printf("am ajuns la edges %f\n",edges.at(i).y);
            normalaLat = glm::cross(edges.at(i),normalaPlan);
            glm::vec2 aux;
            aux.x = normalaLat.x;
            aux.y = normalaLat.y;
            normalsList.push_back(aux);
        }
        return normalsList;
    }
}