CGI/exercise3/include/triangles.h

143 lines
2.4 KiB
C
Raw Normal View History

2019-01-05 17:00:14 +00:00
#pragma once
#include "util/OpenMeshUtils.h"
2019-01-07 20:41:52 +00:00
struct SimpleFace
{
2019-01-05 17:00:14 +00:00
std::vector<OpenMesh::HalfedgeHandle> he_handles;
2019-01-07 20:41:52 +00:00
bool is_triangulated()
{
if (he_handles.size() != 3)
{
2019-01-05 17:00:14 +00:00
return false;
}
return true;
}
};
2019-01-07 20:41:52 +00:00
struct Triangle
{
Triangle(std::vector<OpenMesh::Vec3f> vpoints)
{
if (vpoints.size() != 3)
{
2019-01-05 17:00:14 +00:00
abort();
}
2019-01-07 20:41:52 +00:00
for (int i = 0; i < 3; i++)
{
2019-01-05 17:00:14 +00:00
points[i] = vpoints[i];
}
}
OpenMesh::Vec3f points[3];
};
2019-01-07 20:41:52 +00:00
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();
}
}
2019-01-05 17:00:14 +00:00
2019-01-07 20:41:52 +00:00
for (auto simple_face : simple_faces)
{
2019-01-05 17:00:14 +00:00
std::vector<OpenMesh::Vec3f> points = face_points(simple_face, private_mesh);
triangles.emplace_back(points);
}
}
2019-01-07 20:41:52 +00:00
float surface_area()
{
2019-01-05 17:00:14 +00:00
float surface_area = 0.0f;
2019-01-07 20:41:52 +00:00
for (Triangle &triangle : triangles)
{
2019-01-05 17:00:14 +00:00
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;
}
2019-01-07 20:41:52 +00:00
float volume()
{
float volume = 0.0f;
2019-01-05 17:00:14 +00:00
2019-01-07 20:41:52 +00:00
for (Triangle &triangle : triangles)
{
OpenMesh::Vec3f p1 = triangle.points[0];
OpenMesh::Vec3f p2 = triangle.points[1];
OpenMesh::Vec3f p3 = triangle.points[2];
2019-01-05 17:00:14 +00:00
2019-01-07 21:10:54 +00:00
volume += OpenMesh::dot(p1, OpenMesh::cross(p2, p3)) / 6.0f;
2019-01-07 20:41:52 +00:00
}
2019-01-05 17:00:14 +00:00
2019-01-07 20:41:52 +00:00
return volume;
}
2019-01-05 17:00:14 +00:00
2019-01-07 20:41:52 +00:00
private:
bool gather_faces(const HEMesh &m, std::vector<SimpleFace> &simple_faces)
{
auto faces = m.all_faces();
2019-01-05 17:00:14 +00:00
2019-01-07 20:41:52 +00:00
for (auto face_handle : faces)
{
HEMesh::ConstFaceHalfedgeIter fh_it = m.cfh_iter(face_handle);
2019-01-05 17:00:14 +00:00
2019-01-07 20:41:52 +00:00
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;
}
2019-01-05 17:00:14 +00:00
2019-01-07 20:41:52 +00:00
std::vector<OpenMesh::Vec3f> face_points(SimpleFace &simple_face, const HEMesh &m)
{
2019-01-05 17:00:14 +00:00
std::vector<OpenMesh::Vec3f> points;
2019-01-07 20:41:52 +00:00
for (auto he : simple_face.he_handles)
{
2019-01-05 17:00:14 +00:00
OpenMesh::VertexHandle vertex_handle = m.from_vertex_handle(he);
points.emplace_back(m.point(vertex_handle));
}
return points;
}
std::vector<Triangle> triangles;
};