From d20ff95a550581a9bf2c302fd0751f4b6aef70e4 Mon Sep 17 00:00:00 2001 From: alemuntoni Date: Fri, 9 Jul 2021 14:29:57 +0200 Subject: [PATCH] copy&swap idiom for CMeshO --- src/common/ml_document/cmesh.cpp | 36 ++++++++----------------- src/common/ml_document/cmesh.h | 38 ++++++++++++++++++++++++++- src/common/ml_document/mesh_model.cpp | 15 +++++++---- src/vcglib | 2 +- 4 files changed, 59 insertions(+), 32 deletions(-) diff --git a/src/common/ml_document/cmesh.cpp b/src/common/ml_document/cmesh.cpp index 3695e80aa..b9eea2fbc 100644 --- a/src/common/ml_document/cmesh.cpp +++ b/src/common/ml_document/cmesh.cpp @@ -37,35 +37,23 @@ CMeshO::CMeshO(const CMeshO& oth) : vcg::tri::Append::MeshAppendConst(*this, oth); textures = oth.textures; normalmaps = oth.normalmaps; + imark = oth.imark; } -/// TODO: make a proper implementation of a move constructor. -/// Even if almost never used, this is very inefficient. -CMeshO::CMeshO(CMeshO&& oth): - vcgTriMesh(), sfn(oth.sfn), svn(oth.svn), - pvn(oth.pvn), pfn(oth.pfn), Tr(oth.Tr) +CMeshO::CMeshO(CMeshO&& oth) + : CMeshO() { - enableComponentsFromOtherMesh(oth); - //I could take everything from oth and place it in - //this mesh - vcg::tri::Append::Mesh(*this, oth); - textures = oth.textures; - normalmaps = oth.normalmaps; + swap(*this, oth); } -/// TODO: change this and use the copy&swap idiom -CMeshO& CMeshO::operator=(const CMeshO& oth) +CMeshO::~CMeshO() { - Clear(); - enableComponentsFromOtherMesh(oth); - vcg::tri::Append::MeshCopyConst(*this, oth); - sfn = oth.sfn; - svn = oth.svn; - pvn = oth.pvn; - pfn = oth.pfn; - Tr = oth.Tr; - textures = oth.textures; - normalmaps = oth.normalmaps; + //no need to call base class destructor. It is called automatically +} + +CMeshO& CMeshO::operator=(CMeshO oth) +{ + swap(*this, oth); return *this; } @@ -130,5 +118,3 @@ void CMeshO::enableComponentsFromOtherMesh(const CMeshO& oth) for(const std::string& attr : perFPointAttrs) vcg::tri::Allocator::AddPerFaceAttribute(*this, attr); } - - diff --git a/src/common/ml_document/cmesh.h b/src/common/ml_document/cmesh.h index 71c703c65..d9654e459 100644 --- a/src/common/ml_document/cmesh.h +++ b/src/common/ml_document/cmesh.h @@ -139,8 +139,12 @@ public : CMeshO(const CMeshO& oth); CMeshO(CMeshO&& oth); + + virtual ~CMeshO(); - CMeshO& operator=(const CMeshO& oth); + CMeshO& operator=(CMeshO oth); + + friend void swap(CMeshO& m1, CMeshO& m2); Box3m trBB() const; @@ -156,4 +160,36 @@ private: void enableComponentsFromOtherMesh(const CMeshO& oth); }; +//must be inlined +inline void swap(CMeshO& m1, CMeshO& m2) +{ + using std::swap; + swap(m1.vn, m2.vn); + swap(m1.vert, m2.vert); + m1.vert._updateOVP(m1.vert.begin(), m1.vert.end()); + m2.vert._updateOVP(m2.vert.begin(), m2.vert.end()); + swap(m1.en, m2.en); + swap(m1.edge, m2.edge); + swap(m1.fn, m2.fn); + swap(m1.face, m2.face); + m1.face._updateOVP(m1.face.begin(), m1.face.end()); + m2.face._updateOVP(m2.face.begin(), m2.face.end()); + swap(m1.hn, m2.hn); + swap(m1.hedge, m2.hedge); + swap(m1.tn, m2.tn); + swap(m1.tetra, m2.tetra); + swap(m1.bbox, m2.bbox); + swap(m1.textures, m2.textures); + swap(m1.normalmaps, m2.normalmaps); + swap(m1.attrn, m2.attrn); + swap(m1.vert_attr, m2.vert_attr); + swap(m1.edge_attr, m2.edge_attr); + swap(m1.face_attr, m2.face_attr); + swap(m1.mesh_attr, m2.mesh_attr); + swap(m1.tetra_attr, m2.tetra_attr); + swap(m1.shot, m2.shot); + swap(m1.imark, m2.imark); +} + + #endif //CMESH_H diff --git a/src/common/ml_document/mesh_model.cpp b/src/common/ml_document/mesh_model.cpp index c132bbb25..e6eb97ad7 100644 --- a/src/common/ml_document/mesh_model.cpp +++ b/src/common/ml_document/mesh_model.cpp @@ -336,11 +336,16 @@ void MeshModel::enable(int openingFileMask) updateDataMask(MM_VERTCOLOR); if( openingFileMask & tri::io::Mask::IOM_FACECOLOR ) updateDataMask(MM_FACECOLOR); - if( openingFileMask & tri::io::Mask::IOM_VERTRADIUS ) updateDataMask(MM_VERTRADIUS); - if( openingFileMask & tri::io::Mask::IOM_CAMERA ) updateDataMask(MM_CAMERA); - if( openingFileMask & tri::io::Mask::IOM_VERTQUALITY ) updateDataMask(MM_VERTQUALITY); - if( openingFileMask & tri::io::Mask::IOM_FACEQUALITY ) updateDataMask(MM_FACEQUALITY); - if( openingFileMask & tri::io::Mask::IOM_BITPOLYGONAL ) updateDataMask(MM_POLYGONAL); + if( openingFileMask & tri::io::Mask::IOM_VERTRADIUS ) + updateDataMask(MM_VERTRADIUS); + if( openingFileMask & tri::io::Mask::IOM_CAMERA ) + updateDataMask(MM_CAMERA); + if( openingFileMask & tri::io::Mask::IOM_VERTQUALITY ) + updateDataMask(MM_VERTQUALITY); + if( openingFileMask & tri::io::Mask::IOM_FACEQUALITY ) + updateDataMask(MM_FACEQUALITY); + if( openingFileMask & tri::io::Mask::IOM_BITPOLYGONAL ) + updateDataMask(MM_POLYGONAL); } bool MeshModel::meshModified() const diff --git a/src/vcglib b/src/vcglib index 735f93c25..fdabe1aea 160000 --- a/src/vcglib +++ b/src/vcglib @@ -1 +1 @@ -Subproject commit 735f93c256f960aa50ce3a0559c10b8eb1f2782a +Subproject commit fdabe1aead524ce967bdca453fe36a94ab10c1a2