From 951aa17bd9245a1418fa7c84ee7f887d53aa1083 Mon Sep 17 00:00:00 2001 From: Isaak Lim Date: Wed, 27 Feb 2013 13:25:42 +0000 Subject: [PATCH] - added color_cast from Vec4f to Vec4i and Vec4ui - added colori and colorAi functions to BaseExporter which return Vec3i and Vec4i respectively - adjusted the PLYWriter to use colori and colorAi for writing ascii files refs #1405 git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@815 fdac6126-5c0c-442c-9429-916003d36597 --- src/OpenMesh/Core/IO/exporter/BaseExporter.hh | 28 ++-- src/OpenMesh/Core/IO/exporter/ExporterT.hh | 127 ++++++++++++------ src/OpenMesh/Core/IO/writer/PLYWriter.cc | 12 +- src/OpenMesh/Core/Utils/color_cast.hh | 28 ++++ 4 files changed, 134 insertions(+), 61 deletions(-) diff --git a/src/OpenMesh/Core/IO/exporter/BaseExporter.hh b/src/OpenMesh/Core/IO/exporter/BaseExporter.hh index 6973a8b8..4b50943c 100644 --- a/src/OpenMesh/Core/IO/exporter/BaseExporter.hh +++ b/src/OpenMesh/Core/IO/exporter/BaseExporter.hh @@ -4,10 +4,10 @@ * Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -76,7 +76,7 @@ namespace IO { /** Base class for exporter modules. The exporter modules provide an interface between the writer modules and - the target data structure. + the target data structure. */ class OPENMESHDLLEXPORT BaseExporter @@ -84,27 +84,31 @@ class OPENMESHDLLEXPORT BaseExporter public: virtual ~BaseExporter() { } - + // get vertex data virtual Vec3f point(VertexHandle _vh) const = 0; virtual Vec3f normal(VertexHandle _vh) const = 0; virtual Vec3uc color(VertexHandle _vh) const = 0; virtual Vec4uc colorA(VertexHandle _vh) const = 0; + virtual Vec3ui colori(VertexHandle _vh) const = 0; + virtual Vec4ui colorAi(VertexHandle _vh) const = 0; virtual Vec2f texcoord(VertexHandle _vh) const = 0; - + // get face data - virtual unsigned int - get_vhandles(FaceHandle _fh, + virtual unsigned int + get_vhandles(FaceHandle _fh, std::vector& _vhandles) const=0; virtual Vec3f normal(FaceHandle _fh) const = 0; virtual Vec3uc color (FaceHandle _fh) const = 0; - virtual Vec4uc colorA(FaceHandle _fh) const = 0; - + virtual Vec4uc colorA(FaceHandle _fh) const = 0; + // get edge data virtual Vec3uc color(EdgeHandle _eh) const = 0; virtual Vec4uc colorA(EdgeHandle _eh) const = 0; + virtual Vec3ui colori(EdgeHandle _vh) const = 0; + virtual Vec4ui colorAi(EdgeHandle _vh) const = 0; // get reference to base kernel virtual const BaseKernel* kernel() { return 0; } @@ -121,7 +125,7 @@ public: virtual bool has_vertex_normals() const { return false; } virtual bool has_vertex_colors() const { return false; } virtual bool has_vertex_texcoords() const { return false; } - virtual bool has_edge_colors() const { return false; } + virtual bool has_edge_colors() const { return false; } virtual bool has_face_normals() const { return false; } virtual bool has_face_colors() const { return false; } }; diff --git a/src/OpenMesh/Core/IO/exporter/ExporterT.hh b/src/OpenMesh/Core/IO/exporter/ExporterT.hh index b4b653e3..b221d7aa 100644 --- a/src/OpenMesh/Core/IO/exporter/ExporterT.hh +++ b/src/OpenMesh/Core/IO/exporter/ExporterT.hh @@ -4,10 +4,10 @@ * Copyright (C) 2001-2012 by Computer Graphics Group, RWTH Aachen * * www.openmesh.org * * * - *---------------------------------------------------------------------------* + *---------------------------------------------------------------------------* * This file is part of OpenMesh. * * * - * OpenMesh is free software: you can redistribute it and/or modify * + * OpenMesh is free software: you can redistribute it and/or modify * * it under the terms of the GNU Lesser General Public License as * * published by the Free Software Foundation, either version 3 of * * the License, or (at your option) any later version with the * @@ -30,10 +30,10 @@ * License along with OpenMesh. If not, * * see . * * * -\*===========================================================================*/ +\*===========================================================================*/ /*===========================================================================*\ - * * + * * * $Revision$ * * $Date$ * * * @@ -83,72 +83,99 @@ public: // Constructor ExporterT(const Mesh& _mesh) : mesh_(_mesh) {} - + // get vertex data - Vec3f point(VertexHandle _vh) const - { - return vector_cast(mesh_.point(_vh)); + Vec3f point(VertexHandle _vh) const + { + return vector_cast(mesh_.point(_vh)); } - Vec3f normal(VertexHandle _vh) const - { - return (mesh_.has_vertex_normals() - ? vector_cast(mesh_.normal(_vh)) + Vec3f normal(VertexHandle _vh) const + { + return (mesh_.has_vertex_normals() + ? vector_cast(mesh_.normal(_vh)) : Vec3f(0.0f, 0.0f, 0.0f)); } Vec3uc color(VertexHandle _vh) const { - return (mesh_.has_vertex_colors() - ? color_cast(mesh_.color(_vh)) + return (mesh_.has_vertex_colors() + ? color_cast(mesh_.color(_vh)) : Vec3uc(0, 0, 0)); } Vec4uc colorA(VertexHandle _vh) const { - return (mesh_.has_vertex_colors() - ? color_cast(mesh_.color(_vh)) + return (mesh_.has_vertex_colors() + ? color_cast(mesh_.color(_vh)) : Vec4uc(0, 0, 0, 0)); } + Vec3ui colori(VertexHandle _vh) const + { + return (mesh_.has_vertex_colors() + ? color_cast(mesh_.color(_vh)) + : Vec3ui(0, 0, 0)); + } + + Vec4ui colorAi(VertexHandle _vh) const + { + return (mesh_.has_vertex_colors() + ? color_cast(mesh_.color(_vh)) + : Vec4ui(0, 0, 0, 0)); + } + Vec2f texcoord(VertexHandle _vh) const { #if defined(OM_CC_GCC) && (OM_CC_VERSION<30000) - // Workaround! + // Workaround! // gcc 2.95.3 exits with internal compiler error at the // code below!??? **) if (mesh_.has_vertex_texcoords2D()) return vector_cast(mesh_.texcoord2D(_vh)); return Vec2f(0.0f, 0.0f); #else // **) - return (mesh_.has_vertex_texcoords2D() - ? vector_cast(mesh_.texcoord2D(_vh)) + return (mesh_.has_vertex_texcoords2D() + ? vector_cast(mesh_.texcoord2D(_vh)) : Vec2f(0.0f, 0.0f)); #endif } - + // get edge data - + Vec3uc color(EdgeHandle _eh) const { - return (mesh_.has_edge_colors() - ? color_cast(mesh_.color(_eh)) + return (mesh_.has_edge_colors() + ? color_cast(mesh_.color(_eh)) : Vec3uc(0, 0, 0)); } - + Vec4uc colorA(EdgeHandle _eh) const { - return (mesh_.has_edge_colors() - ? color_cast(mesh_.color(_eh)) + return (mesh_.has_edge_colors() + ? color_cast(mesh_.color(_eh)) : Vec4uc(0, 0, 0, 0)); } - + Vec3ui colori(EdgeHandle _eh) const + { + return (mesh_.has_edge_colors() + ? color_cast(mesh_.color(_eh)) + : Vec3ui(0, 0, 0)); + } + + Vec4ui colorAi(EdgeHandle _eh) const + { + return (mesh_.has_edge_colors() + ? color_cast(mesh_.color(_eh)) + : Vec4ui(0, 0, 0, 0)); + } + // get face data - unsigned int get_vhandles(FaceHandle _fh, + unsigned int get_vhandles(FaceHandle _fh, std::vector& _vhandles) const { unsigned int count(0); @@ -161,32 +188,46 @@ public: return count; } - Vec3f normal(FaceHandle _fh) const - { - return (mesh_.has_face_normals() - ? vector_cast(mesh_.normal(_fh)) + Vec3f normal(FaceHandle _fh) const + { + return (mesh_.has_face_normals() + ? vector_cast(mesh_.normal(_fh)) : Vec3f(0.0f, 0.0f, 0.0f)); } - Vec3uc color(FaceHandle _fh) const - { - return (mesh_.has_face_colors() - ? color_cast(mesh_.color(_fh)) + Vec3uc color(FaceHandle _fh) const + { + return (mesh_.has_face_colors() + ? color_cast(mesh_.color(_fh)) : Vec3uc(0, 0, 0)); } - Vec4uc colorA(FaceHandle _fh) const - { - return (mesh_.has_face_colors() - ? color_cast(mesh_.color(_fh)) + Vec4uc colorA(FaceHandle _fh) const + { + return (mesh_.has_face_colors() + ? color_cast(mesh_.color(_fh)) : Vec4uc(0, 0, 0, 0)); } + Vec3ui colori(FaceHandle _fh) const + { + return (mesh_.has_face_colors() + ? color_cast(mesh_.color(_fh)) + : Vec3ui(0, 0, 0)); + } + + Vec4ui colorAi(FaceHandle _fh) const + { + return (mesh_.has_face_colors() + ? color_cast(mesh_.color(_fh)) + : Vec4ui(0, 0, 0, 0)); + } + virtual const BaseKernel* kernel() { return &mesh_; } // query number of faces, vertices, normals, texcoords - size_t n_vertices() const { return mesh_.n_vertices(); } + size_t n_vertices() const { return mesh_.n_vertices(); } size_t n_faces() const { return mesh_.n_faces(); } size_t n_edges() const { return mesh_.n_edges(); } @@ -194,7 +235,7 @@ public: // property information bool is_triangle_mesh() const { return Mesh::is_triangles(); } - + bool has_vertex_normals() const { return mesh_.has_vertex_normals(); } bool has_vertex_colors() const { return mesh_.has_vertex_colors(); } bool has_vertex_texcoords() const { return mesh_.has_vertex_texcoords2D(); } @@ -203,7 +244,7 @@ public: bool has_face_colors() const { return mesh_.has_face_colors(); } private: - + const Mesh& mesh_; }; diff --git a/src/OpenMesh/Core/IO/writer/PLYWriter.cc b/src/OpenMesh/Core/IO/writer/PLYWriter.cc index d829171a..989ec89a 100644 --- a/src/OpenMesh/Core/IO/writer/PLYWriter.cc +++ b/src/OpenMesh/Core/IO/writer/PLYWriter.cc @@ -178,8 +178,8 @@ write_ascii(std::ostream& _out, BaseExporter& _be, Options _opt) const unsigned int i, j, nV, nF; Vec3f v, n; - OpenMesh::Vec3uc c; - OpenMesh::Vec4uc cA; + OpenMesh::Vec3ui c; + OpenMesh::Vec4ui cA; OpenMesh::Vec2f t; VertexHandle vh; std::vector vhandles; @@ -242,12 +242,12 @@ write_ascii(std::ostream& _out, BaseExporter& _be, Options _opt) const if ( _opt.vertex_has_color() ) { //with alpha if ( _opt.color_has_alpha() ){ - cA = _be.colorA(vh); - _out << " " << (unsigned int)cA[0] << " " << (unsigned int)cA[1] << " " << (unsigned int)cA[2] << " " << (unsigned int)cA[3]; + cA = _be.colorAi(vh); + _out << " " << cA; }else{ //without alpha - c = _be.color(vh); - _out << " " << (unsigned int)c[0] << " " << (unsigned int)c[1] << " " << (unsigned int)c[2]; + c = _be.colori(vh); + _out << " " << c; } } diff --git a/src/OpenMesh/Core/Utils/color_cast.hh b/src/OpenMesh/Core/Utils/color_cast.hh index a1461733..846e05f6 100644 --- a/src/OpenMesh/Core/Utils/color_cast.hh +++ b/src/OpenMesh/Core/Utils/color_cast.hh @@ -140,6 +140,20 @@ struct color_caster } }; +template <> +struct color_caster +{ + typedef Vec4i return_type; + + inline static return_type cast(const Vec4f& _src) + { + return Vec4i( (int)(_src[0]* 255.0f + 0.5f), + (int)(_src[1]* 255.0f + 0.5f), + (int)(_src[2]* 255.0f + 0.5f), + (int)(_src[3]* 255.0f + 0.5f) ); + } +}; + template <> struct color_caster { @@ -166,6 +180,20 @@ struct color_caster } }; +template <> +struct color_caster +{ + typedef Vec4ui return_type; + + inline static return_type cast(const Vec4f& _src) + { + return Vec4ui( (unsigned int)(_src[0]* 255.0f + 0.5f), + (unsigned int)(_src[1]* 255.0f + 0.5f), + (unsigned int)(_src[2]* 255.0f + 0.5f), + (unsigned int)(_src[3]* 255.0f + 0.5f) ); + } +}; + template <> struct color_caster {