Create triangle header

This commit is contained in:
hodasemi 2019-01-05 18:00:14 +01:00
parent ce1042f3b1
commit e470aef3d5
2 changed files with 110 additions and 129 deletions

View file

@ -0,0 +1,106 @@
#pragma once
#include "util/OpenMeshUtils.h"
struct SimpleFace {
std::vector<OpenMesh::HalfedgeHandle> he_handles;
bool is_triangulated() {
if (he_handles.size() != 3) {
return false;
}
return true;
}
};
struct Triangle {
Triangle(std::vector<OpenMesh::Vec3f> vpoints) {
if (vpoints.size() != 3) {
abort();
}
for (int i = 0; i < 3; i++) {
points[i] = vpoints[i];
}
}
OpenMesh::Vec3f points[3];
};
class Triangles {
public:
Triangles(HEMesh private_mesh) {
std::vector<SimpleFace> simple_faces;
if (!gather_faces(private_mesh, simple_faces)) {
simple_faces.clear();
private_mesh.triangulate();
if (!gather_faces(private_mesh, simple_faces)) {
abort();
}
}
for (auto simple_face: simple_faces) {
std::vector<OpenMesh::Vec3f> points = face_points(simple_face, private_mesh);
triangles.emplace_back(points);
}
}
float surface_area() {
float surface_area = 0.0f;
for (Triangle& triangle : triangles) {
OpenMesh::Vec3f p1 = triangle.points[0];
OpenMesh::Vec3f p2 = triangle.points[1];
OpenMesh::Vec3f p3 = triangle.points[2];
OpenMesh::Vec3f p12_vec = p1 - p2;
OpenMesh::Vec3f p23_vec = p1 - p3;
OpenMesh::Vec3f cross = OpenMesh::cross(p12_vec, p23_vec);
surface_area += cross.length() / 2;
}
return surface_area;
}
private:
bool gather_faces(const HEMesh& m, std::vector<SimpleFace>& simple_faces) {
auto faces = m.all_faces();
for (auto face_handle : faces) {
HEMesh::ConstFaceHalfedgeIter fh_it = m.cfh_iter(face_handle);
SimpleFace simple_face;
for (; fh_it.is_valid(); ++fh_it) {
simple_face.he_handles.emplace_back(*fh_it);
}
if (!simple_face.is_triangulated()) {
return false;
}
simple_faces.emplace_back(simple_face);
}
return true;
}
std::vector<OpenMesh::Vec3f> face_points(SimpleFace& simple_face, const HEMesh& m) {
std::vector<OpenMesh::Vec3f> points;
for (auto he : simple_face.he_handles) {
OpenMesh::VertexHandle vertex_handle = m.from_vertex_handle(he);
points.emplace_back(m.point(vertex_handle));
}
return points;
}
std::vector<Triangle> triangles;
};

View file

@ -3,140 +3,15 @@
// Copyright (C) CGV TU Dresden - All Rights Reserved
#include "SurfaceArea.h"
#include "triangles.h"
#include <iostream>
struct SimpleFace {
std::vector<OpenMesh::HalfedgeHandle> he_handles;
bool is_triangulated() {
if (he_handles.size() != 3) {
return false;
}
return true;
}
};
struct Triangle {
Triangle(std::vector<OpenMesh::Vec3f> vpoints) {
if (vpoints.size() != 3) {
abort();
}
for (int i = 0; i < 3; i++) {
points[i] = vpoints[i];
}
}
OpenMesh::Vec3f points[3];
};
struct Triangles {
Triangles(std::vector<SimpleFace> simple_faces, const HEMesh& m) {
for (auto simple_face: simple_faces) {
std::vector<OpenMesh::Vec3f> points = face_points(simple_face, m);
triangles.emplace_back(points);
}
}
std::vector<OpenMesh::Vec3f> face_points(SimpleFace& simple_face, const HEMesh& m) {
std::vector<OpenMesh::Vec3f> points;
for (auto he : simple_face.he_handles) {
OpenMesh::VertexHandle vertex_handle = m.from_vertex_handle(he);
points.emplace_back(m.point(vertex_handle));
}
return points;
}
float calculate_h(OpenMesh::Vec3f& vector, OpenMesh::Vec3f& p) {
float x = vector[0];
float y = vector[1];
float z = vector[2];
float x1 = p[0];
float y1 = p[1];
float z1 = p[2];
float r = - ( (-x*x1 - y*y1 - z*z1) / (x*x + y*y + z*z) );
OpenMesh::Vec3f s_vec(r * vector);
OpenMesh::Vec3f h_vec = s_vec - p;
float h = h_vec.length();
return h;
}
float surface_area() {
float surface_area = 0.0f;
for (Triangle& triangle : triangles) {
OpenMesh::Vec3f p1 = triangle.points[0];
OpenMesh::Vec3f p2 = triangle.points[1];
OpenMesh::Vec3f p3 = triangle.points[2];
OpenMesh::Vec3f p12_vec = p1 - p2;
OpenMesh::Vec3f p23_vec = p1 - p3;
OpenMesh::Vec3f cross = OpenMesh::cross(p12_vec, p23_vec);
surface_area += cross.length() / 2;
}
return surface_area;
}
std::vector<Triangle> triangles;
};
bool gather_faces(const HEMesh& m, std::vector<SimpleFace>& simple_faces) {
auto faces = m.all_faces();
for (auto face_handle : faces) {
HEMesh::ConstFaceHalfedgeIter fh_it = m.cfh_iter(face_handle);
SimpleFace simple_face;
for (; fh_it.is_valid(); ++fh_it) {
simple_face.he_handles.emplace_back(*fh_it);
}
if (!simple_face.is_triangulated()) {
return false;
}
simple_faces.emplace_back(simple_face);
}
return true;
}
float ComputeSurfaceArea(const HEMesh& _m)
float ComputeSurfaceArea(const HEMesh& m)
{
float area = 0;
/* Task 2.2.2 */
std::cout << "Area computation is not implemented." << std::endl;
Triangles triangles(m);
// copy mesh for mutability
HEMesh private_mesh = _m;
std::vector<SimpleFace> simple_faces;
if (!gather_faces(private_mesh, simple_faces)) {
simple_faces.clear();
private_mesh.triangulate();
if (!gather_faces(private_mesh, simple_faces)) {
abort();
}
}
Triangles triangles(simple_faces, private_mesh);
float area = 0.0f;
area = triangles.surface_area();