From db7394d19b4c7f3c59aec9249adc17d3ffa27af4 Mon Sep 17 00:00:00 2001 From: Paolo Cignoni Date: Tue, 13 Feb 2018 19:44:49 +0100 Subject: [PATCH] Added sample to show how to save in PLY a given PerVertex or PerFace attribute --- apps/sample/sample.pro | 1 + .../trimesh_attribute_saving.cpp | 70 +++++++++++++++++++ .../trimesh_attribute_saving.pro | 3 + 3 files changed, 74 insertions(+) create mode 100644 apps/sample/trimesh_attribute_saving/trimesh_attribute_saving.cpp create mode 100644 apps/sample/trimesh_attribute_saving/trimesh_attribute_saving.pro diff --git a/apps/sample/sample.pro b/apps/sample/sample.pro index 3b590577..10d6ff49 100644 --- a/apps/sample/sample.pro +++ b/apps/sample/sample.pro @@ -2,6 +2,7 @@ TEMPLATE = subdirs SUBDIRS = trimesh_allocate \ trimesh_attribute \ + trimesh_attribute_saving \ trimesh_ball_pivoting \ trimesh_base \ trimesh_closest \ diff --git a/apps/sample/trimesh_attribute_saving/trimesh_attribute_saving.cpp b/apps/sample/trimesh_attribute_saving/trimesh_attribute_saving.cpp new file mode 100644 index 00000000..2fcabd6f --- /dev/null +++ b/apps/sample/trimesh_attribute_saving/trimesh_attribute_saving.cpp @@ -0,0 +1,70 @@ +/**************************************************************************** +* VCGLib o o * +* Visual and Computer Graphics Library o o * +* _ O _ * +* Copyright(C) 2004-2016 \/)\/ * +* Visual Computing Lab /\/| * +* ISTI - Italian National Research Council | * +* \ * +* All rights reserved. * +* * +* This program is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program 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 (http://www.gnu.org/licenses/gpl.txt) * +* for more details. * +* * +****************************************************************************/ +/*! \file trimesh_attribute.cpp +\ingroup code_sample + +\brief the minimal example of using the attributes + +Attributes are a simple mechanism to associate user-defined 'attributes' to the simplicies and to the mesh. +See the page '\ref attributes' for more details. +*/ + +#include +#include +#include +class MyEdge; +class MyFace; +class MyVertex; +struct MyUsedTypes : public vcg::UsedTypes< vcg::Use::AsVertexType, + vcg::Use ::AsFaceType>{}; + +class MyVertex : public vcg::Vertex< MyUsedTypes, vcg::vertex::Coord3f,vcg::vertex::Normal3f, vcg::vertex::BitFlags>{}; +class MyFace : public vcg::Face< MyUsedTypes, vcg::face::VertexRef, vcg::face::Normal3f, vcg::face::BitFlags> {}; + +class MyMesh : public vcg::tri::TriMesh< std::vector, std::vector > {}; +int main() +{ + MyMesh m; + Torus(m, 3.0f, 1.0f); + //! [Adding an attribute] + // add a per-vertex attribute with type float named "GaussianCurvature" + MyMesh::PerVertexAttributeHandle + hv = vcg::tri::Allocator:: GetPerVertexAttribute (m,std::string("GaussianCurvature")); + // add a per-face attribute with type float named "FaceArea" + MyMesh::PerFaceAttributeHandle + hf = vcg::tri::Allocator:: GetPerFaceAttribute (m,std::string("FaceArea")); + //! [filling the attribute] + vcg::tri::Allocator::ClearPerVertexAttribute(m,hv, float(M_PI*2)); + + ForEachFace(m, [&](MyFace &f){ + hf[&f]=vcg::DoubleArea(f)*0.5f; + for(int i=0;i<3;++i) + hv[f.V(i)] -= vcg::Angle(f.P1(i)-f.P0(i),f.P2(i)-f.P0(i)); + }); + + //! [Saving two attributes in ply, one of the two disguised as quality] + vcg::tri::io::PlyInfo pi; + pi.AddPerVertexFloatAttribute("GaussianCurvature","quality"); + pi.AddPerFaceFloatAttribute("FaceArea"); + vcg::tri::io::ExporterPLY::Save(m,"MeshWithCurvature.ply",true,pi); +} diff --git a/apps/sample/trimesh_attribute_saving/trimesh_attribute_saving.pro b/apps/sample/trimesh_attribute_saving/trimesh_attribute_saving.pro new file mode 100644 index 00000000..78228a1e --- /dev/null +++ b/apps/sample/trimesh_attribute_saving/trimesh_attribute_saving.pro @@ -0,0 +1,3 @@ +include(../common.pri) +TARGET = trimesh_attribute_saving +SOURCES += trimesh_attribute_saving.cpp ../../../wrap/ply/plylib.cpp