* Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
* https://3d.bk.tudelft.nl/liangliang/
*
* This file is part of Easy3D. If it is useful in your research/work,
* I would be grateful if you show your appreciation by citing it:
* ------------------------------------------------------------------
* Liangliang Nan.
* Easy3D: a lightweight, easy-to-use, and efficient C++ library
* for processing and rendering 3D data.
* Journal of Open Source Software, 6(64), 3255, 2021.
* ------------------------------------------------------------------
*
* Easy3D is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License Version 3
* as published by the Free Software Foundation.
*
* Easy3D is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
********************************************************************/
#include <easy3d/core/surface_mesh.h>
#include <easy3d/core/surface_mesh_builder.h>
#include <easy3d/fileio/surface_mesh_io.h>
#include <easy3d/util/resource.h>
#include <easy3d/util/file_system.h>
using namespace easy3d;
int test_surface_mesh() {
const int option = 2;
// / | \
// / | \
// v1 /_ _|_ _\ v2
const std::vector<vec3> points = {
vec3(0, 0, 0),
vec3(1, 0, 0),
vec3(0, 1, 0),
vec3(0, 0, 1)
};
SurfaceMesh mesh;
{
if (option == 1) {
SurfaceMesh::Vertex v0 = mesh.add_vertex(points[0]);
SurfaceMesh::Vertex v1 = mesh.add_vertex(points[1]);
SurfaceMesh::Vertex v2 = mesh.add_vertex(points[2]);
SurfaceMesh::Vertex v3 = mesh.add_vertex(points[3]);
mesh.add_triangle(v0, v1, v3);
mesh.add_triangle(v1, v2, v3);
mesh.add_triangle(v2, v0, v3);
mesh.add_triangle(v0, v2, v1);
} else if (option == 2) {
SurfaceMeshBuilder builder(&mesh);
builder.begin_surface();
SurfaceMesh::Vertex v0 = builder.add_vertex(vec3(0, 0, 0));
SurfaceMesh::Vertex v1 = builder.add_vertex(vec3(1, 0, 0));
SurfaceMesh::Vertex v2 = builder.add_vertex(vec3(0, 1, 0));
SurfaceMesh::Vertex v3 = builder.add_vertex(vec3(0, 0, 1));
builder.add_triangle(v0, v1, v3);
builder.add_triangle(v1, v2, v3);
builder.add_triangle(v2, v0, v3);
builder.add_triangle(v0, v2, v1);
builder.end_surface(false);
} else
LOG(ERROR) << "option must be 1 or 2";
std::cout << "#face: " << mesh.n_faces() << std::endl;
std::cout << "#vertex: " << mesh.n_vertices() << std::endl;
std::cout << "#edge: " << mesh.n_edges() << std::endl;
}
#define USE_FOR_LOOP
{
std::cout << "----------------------------------------\n";
std::cout << "The incident vertices of each vertex" << std::endl;
std::cout << "----------------------------------------\n";
for (auto v : mesh.vertices()) {
std::cout << "incident vertices of vertex " << v << ": ";
#ifdef USE_FOR_LOOP
for (auto vv : mesh.vertices(v))
std::cout << vv << " ";
#else
SurfaceMesh::VertexAroundVertexCirculator cir = mesh.vertices(v);
SurfaceMesh::VertexAroundVertexCirculator end = cir;
do {
SurfaceMesh::Vertex vv = *cir;
std::cout << vv << " ";
++cir;
} while (cir != end);
#endif
std::cout << std::endl;
}
std::cout << "\n--------------------------------------\n";
std::cout << "The incident outgoing/ingoing edges of each vertex" << std::endl;
std::cout << "----------------------------------------\n";
for (auto v : mesh.vertices()) {
std::cout << "incident outgoing/ingoing edges of vertex " << v << ": ";
#ifdef USE_FOR_LOOP
for (auto h : mesh.halfedges(v))
std::cout << h << "/" << mesh.opposite(h) << " ";
#else
SurfaceMesh::HalfedgeAroundVertexCirculator cir = mesh.halfedges(v);
SurfaceMesh::HalfedgeAroundVertexCirculator end = cir;
do {
SurfaceMesh::Halfedge h = *cir;
std::cout << h << "/" << mesh.opposite(h) << " ";
++cir;
} while (cir != end);
#endif
std::cout << std::endl;
}
std::cout << "\n--------------------------------------\n";
std::cout << "The incident faces of each vertex" << std::endl;
std::cout << "----------------------------------------\n";
for (auto v : mesh.vertices()) {
std::cout << "incident faces of vertex " << v << ": ";
#ifdef USE_FOR_LOOP
for (auto f : mesh.faces(v))
std::cout << f << " ";
#else
SurfaceMesh::FaceAroundVertexCirculator cir = mesh.faces(v);
SurfaceMesh::FaceAroundVertexCirculator end = cir;
do {
SurfaceMesh::Face f = *cir;
std::cout << f << " ";
++cir;
} while (cir != end);
#endif
std::cout << std::endl;
}
std::cout << "\n--------------------------------------\n";
std::cout << "The incident vertices of each face" << std::endl;
std::cout << "----------------------------------------\n";
for (auto f : mesh.faces()) {
std::cout << "incident vertices of face " << f << ": ";
#ifdef USE_FOR_LOOP
for (auto v : mesh.vertices(f))
std::cout << v << " ";
#else
SurfaceMesh::VertexAroundFaceCirculator cir = mesh.vertices(f);
SurfaceMesh::VertexAroundFaceCirculator end = cir;
do {
SurfaceMesh::Vertex v = *cir;
std::cout << v << " ";
++cir;
} while (cir != end);
#endif
std::cout << std::endl;
}
std::cout << "\n--------------------------------------\n";
std::cout << "The incident half-edges of each face" << std::endl;
std::cout << "----------------------------------------\n";
for (auto f : mesh.faces()) {
std::cout << "half-edges around face " << f << ": ";
#ifdef USE_FOR_LOOP
for (auto h : mesh.halfedges(f))
std::cout << h << " ";
#else
SurfaceMesh::HalfedgeAroundFaceCirculator cir = mesh.halfedges(f);
SurfaceMesh::HalfedgeAroundFaceCirculator end = cir;
do {
SurfaceMesh::Halfedge h = *cir;
std::cout << h << " ";
++cir;
} while (cir != end);
#endif
std::cout << std::endl;
}
std::cout << "\n--------------------------------------\n";
std::cout << "The two end points of each edge" << std::endl;
std::cout << "----------------------------------------\n";
for (auto e : mesh.edges()) {
std::cout << "the two end points of edge " << e << ": ";
SurfaceMesh::Vertex vs = mesh.vertex(e, 0);
std::cout << vs << " ";
SurfaceMesh::Vertex vt = mesh.vertex(e, 1);
std::cout << vt << " " << std::endl;
}
std::cout << "\n--------------------------------------\n";
std::cout << "The two faces connected by each edge" << std::endl;
std::cout << "----------------------------------------\n";
for (auto e : mesh.edges()) {
std::cout << "the two faces connected by edge " << e << ": ";
SurfaceMesh::Halfedge h0 = mesh.halfedge(e, 0);
if (mesh.is_border(h0))
std::cout << "NULL" << " ";
else
std::cout << mesh.face(h0) << " ";
SurfaceMesh::Halfedge h1 = mesh.halfedge(e, 1);
if (mesh.is_border(h1))
std::cout << "NULL" << " ";
else
std::cout << mesh.face(h1) << " ";
std::cout << std::endl;
}
}
{
SurfaceMesh::FaceProperty<vec3> normals = mesh.add_face_property<vec3>("f:normal");
for (auto f : mesh.faces()) {
normals[f] = mesh.compute_face_normal(f);
std::cout << "normal of face " << f << ": " << normals[f] << std::endl;
}
}
{
const std::string file_name = resource::directory() + "/data/sphere.obj";
SurfaceMesh* mesh = SurfaceMeshIO::load(file_name);
if (!mesh) {
LOG(ERROR) << "failed to load model. Please make sure the file exists and format is correct.";
return EXIT_FAILURE;
}
std::cout << "mesh loaded. " << std::endl;
std::cout << "\tvertices: " << mesh->n_vertices() << std::endl;
std::cout << "\tedges: " << mesh->n_edges() << std::endl;
std::cout << "\tfaces: " << mesh->n_faces() << std::endl;
const std::string save_file_name = "./sphere-copy.obj";
if (SurfaceMeshIO::save(save_file_name, mesh))
std::cout << "mesh saved to \'" << save_file_name << "\'" << std::endl;
else
std::cerr << "failed create the new file" << std::endl;
if (file_system::delete_file(save_file_name))
std::cout << "the saved file has been deleted" << std::endl;
else
std::cerr << "failed to delete the saved file" << std::endl;
}
return EXIT_SUCCESS;
}