/*
 * Software License Agreement (BSD License)
 *
 * Point Cloud Library (PCL) - www.pointclouds.org
 * Copyright (c) 2009-2012, Willow Garage, Inc.
 * Copyright (c) 2012-, Open Perception, Inc.
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *  * Neither the name of the copyright holder(s) nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id$
 *
 */

#ifndef PCL_GEOMETRY_MESH_CONVERSION_H
#define PCL_GEOMETRY_MESH_CONVERSION_H

#include <pcl/PolygonMesh.h>
#include <pcl/conversions.h>

namespace pcl
{
  namespace geometry
  {
    /** \brief Convert a half-edge mesh to a face-vertex mesh.
      * \param[in] half_edge_mesh The input mesh.
      * \param[out] face_vertex_mesh The output mesh.
      * \author Martin Saelzle
      * \ingroup geometry
      */
    template <class HalfEdgeMeshT> void
    toFaceVertexMesh (const HalfEdgeMeshT& half_edge_mesh, pcl::PolygonMesh& face_vertex_mesh)
    {
      typedef HalfEdgeMeshT HalfEdgeMesh;
      typedef typename HalfEdgeMesh::VertexAroundFaceCirculator VAFC;
      typedef typename HalfEdgeMesh::FaceIndex FaceIndex;

      pcl::Vertices polygon;
      pcl::toPCLPointCloud2 (half_edge_mesh.getVertexDataCloud (), face_vertex_mesh.cloud);

      face_vertex_mesh.polygons.reserve (half_edge_mesh.sizeFaces ());
      for (size_t i=0; i<half_edge_mesh.sizeFaces (); ++i)
      {
        VAFC       circ     = half_edge_mesh.getVertexAroundFaceCirculator (FaceIndex (i));
        const VAFC circ_end = circ;
        polygon.vertices.clear ();
        do
        {
          polygon.vertices.push_back (circ.getTargetIndex ().get ());
        } while (++circ != circ_end);
        face_vertex_mesh.polygons.push_back (polygon);
      }
    }

    /** \brief Convert a face-vertex mesh to a half-edge mesh.
      * \param[in] face_vertex_mesh The input mesh.
      * \param[out] half_edge_mesh The output mesh. It must have data associated with the vertices.
      * \return The number of faces that could NOT be added to the half-edge mesh.
      * \author Martin Saelzle
      * \ingroup geometry
      */
    template <class HalfEdgeMeshT> int
    toHalfEdgeMesh (const pcl::PolygonMesh& face_vertex_mesh, HalfEdgeMeshT& half_edge_mesh)
    {
      typedef HalfEdgeMeshT                          HalfEdgeMesh;
      typedef typename HalfEdgeMesh::VertexDataCloud VertexDataCloud;
      typedef typename HalfEdgeMesh::VertexIndex     VertexIndex;
      typedef typename HalfEdgeMesh::VertexIndices   VertexIndices;

      BOOST_STATIC_ASSERT (HalfEdgeMesh::HasVertexData::value); // Output mesh must have data associated with the vertices!

      VertexDataCloud vertices;
      pcl::fromPCLPointCloud2 (face_vertex_mesh.cloud, vertices);

      half_edge_mesh.reserveVertices (vertices.size ());
      half_edge_mesh.reserveEdges (3 * face_vertex_mesh.polygons.size ());
      half_edge_mesh.reserveFaces (    face_vertex_mesh.polygons.size ());

      for (typename VertexDataCloud::const_iterator it=vertices.begin (); it!=vertices.end (); ++it)
      {
        half_edge_mesh.addVertex (*it);
      }

      assert (half_edge_mesh.sizeVertices () == vertices.size ());

      int count_not_added = 0;
      VertexIndices vi;
      vi.reserve (3); // Minimum number (triangle)
      for (size_t i=0; i<face_vertex_mesh.polygons.size (); ++i)
      {
        vi.clear ();
        for (size_t j=0; j<face_vertex_mesh.polygons [i].vertices.size (); ++j)
        {
          vi.push_back (VertexIndex (face_vertex_mesh.polygons [i].vertices [j]));
        }

        if (!half_edge_mesh.addFace (vi).isValid ())
        {
          ++count_not_added;
        }
      }

      return (count_not_added);
    }
  } // End namespace geometry
} // End namespace pcl

#endif // PCL_GEOMETRY_MESH_CONVERSION_H
