Test volume

This commit is contained in:
hodasemi 2019-01-07 21:41:52 +01:00
parent e470aef3d5
commit fd494ffddc

View file

@ -2,11 +2,14 @@
#include "util/OpenMeshUtils.h" #include "util/OpenMeshUtils.h"
struct SimpleFace { struct SimpleFace
{
std::vector<OpenMesh::HalfedgeHandle> he_handles; std::vector<OpenMesh::HalfedgeHandle> he_handles;
bool is_triangulated() { bool is_triangulated()
if (he_handles.size() != 3) { {
if (he_handles.size() != 3)
{
return false; return false;
} }
@ -14,13 +17,17 @@ struct SimpleFace {
} }
}; };
struct Triangle { struct Triangle
Triangle(std::vector<OpenMesh::Vec3f> vpoints) { {
if (vpoints.size() != 3) { Triangle(std::vector<OpenMesh::Vec3f> vpoints)
{
if (vpoints.size() != 3)
{
abort(); abort();
} }
for (int i = 0; i < 3; i++) { for (int i = 0; i < 3; i++)
{
points[i] = vpoints[i]; points[i] = vpoints[i];
} }
} }
@ -28,30 +35,37 @@ struct Triangle {
OpenMesh::Vec3f points[3]; OpenMesh::Vec3f points[3];
}; };
class Triangles { class Triangles
public: {
Triangles(HEMesh private_mesh) { public:
std::vector<SimpleFace> simple_faces; Triangles(HEMesh private_mesh)
{
std::vector<SimpleFace> simple_faces;
if (!gather_faces(private_mesh, simple_faces)) { if (!gather_faces(private_mesh, simple_faces))
simple_faces.clear(); {
private_mesh.triangulate(); simple_faces.clear();
private_mesh.triangulate();
if (!gather_faces(private_mesh, simple_faces)) { if (!gather_faces(private_mesh, simple_faces))
abort(); {
} abort();
} }
}
for (auto simple_face: simple_faces) { for (auto simple_face : simple_faces)
{
std::vector<OpenMesh::Vec3f> points = face_points(simple_face, private_mesh); std::vector<OpenMesh::Vec3f> points = face_points(simple_face, private_mesh);
triangles.emplace_back(points); triangles.emplace_back(points);
} }
} }
float surface_area() { float surface_area()
{
float surface_area = 0.0f; float surface_area = 0.0f;
for (Triangle& triangle : triangles) { for (Triangle &triangle : triangles)
{
OpenMesh::Vec3f p1 = triangle.points[0]; OpenMesh::Vec3f p1 = triangle.points[0];
OpenMesh::Vec3f p2 = triangle.points[1]; OpenMesh::Vec3f p2 = triangle.points[1];
OpenMesh::Vec3f p3 = triangle.points[2]; OpenMesh::Vec3f p3 = triangle.points[2];
@ -67,33 +81,55 @@ public:
return surface_area; return surface_area;
} }
private: float volume()
bool gather_faces(const HEMesh& m, std::vector<SimpleFace>& simple_faces) { {
auto faces = m.all_faces(); float volume = 0.0f;
for (auto face_handle : faces) { for (Triangle &triangle : triangles)
HEMesh::ConstFaceHalfedgeIter fh_it = m.cfh_iter(face_handle); {
OpenMesh::Vec3f p1 = triangle.points[0];
OpenMesh::Vec3f p2 = triangle.points[1];
OpenMesh::Vec3f p3 = triangle.points[2];
SimpleFace simple_face; volume = OpenMesh::dot(p1, OpenMesh::cross(p2, p3));
}
for (; fh_it.is_valid(); ++fh_it) { return volume;
simple_face.he_handles.emplace_back(*fh_it); }
}
if (!simple_face.is_triangulated()) { private:
return false; bool gather_faces(const HEMesh &m, std::vector<SimpleFace> &simple_faces)
} {
auto faces = m.all_faces();
simple_faces.emplace_back(simple_face); for (auto face_handle : faces)
} {
HEMesh::ConstFaceHalfedgeIter fh_it = m.cfh_iter(face_handle);
return true; SimpleFace simple_face;
}
std::vector<OpenMesh::Vec3f> face_points(SimpleFace& simple_face, const HEMesh& m) { 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; std::vector<OpenMesh::Vec3f> points;
for (auto he : simple_face.he_handles) { for (auto he : simple_face.he_handles)
{
OpenMesh::VertexHandle vertex_handle = m.from_vertex_handle(he); OpenMesh::VertexHandle vertex_handle = m.from_vertex_handle(he);
points.emplace_back(m.point(vertex_handle)); points.emplace_back(m.point(vertex_handle));