Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_TRIANGULATE_HPP
Toshihiro Shimizu 890ddd
#define TCG_TRIANGULATE_HPP
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// tcg includes
Toshihiro Shimizu 890ddd
#include "../triangulate.h"
Toshihiro Shimizu 890ddd
#include "../traits.h"
Toshihiro Shimizu 890ddd
#include "../point_ops.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// OS-specific includes
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#include "windows.h"
Toshihiro Shimizu 890ddd
#include <gl glu.h=""></gl>
Toshihiro Shimizu 890ddd
#elif MACOSX
Toshihiro Shimizu 890ddd
#include <glut glut.h=""></glut>
Campbell Barton 107701
#elif LINUX
Campbell Barton 107701
#include <gl glut.h=""></gl>
Campbell Barton 40cabe
#include <cstring></cstring>
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef TCG_GLU_CALLBACK
Toshihiro Shimizu 890ddd
#ifdef WIN32
Toshihiro Shimizu 890ddd
#define TCG_GLU_CALLBACK void(CALLBACK *)()
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define TCG_GLU_CALLBACK void (*)()
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef CALLBACK
Toshihiro Shimizu 890ddd
#define CALLBACK
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace tcg {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
//    GLU Tessellator Callbacks
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace detail {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Toshihiro Shimizu 890ddd
struct CBackData {
Shinya Kitaoka 120a6e
  mesh_type *m_mesh;
Shinya Kitaoka 120a6e
  int m_triangle[3];
Shinya Kitaoka 120a6e
  int m_i;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
// NOTE: must be declared with CALLBACK directive
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Shinya Kitaoka 120a6e
void CALLBACK tessBegin(GLenum type, void *polygon_data) {
Shinya Kitaoka 120a6e
  assert(type == GL_TRIANGLES);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  CBackData<mesh_type> *data = (CBackData<mesh_type> *)polygon_data;</mesh_type></mesh_type>
Shinya Kitaoka 120a6e
  data->m_i                  = 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Shinya Kitaoka 120a6e
void CALLBACK tessEnd(void *polygon_data) {
Shinya Kitaoka 120a6e
  CBackData<mesh_type> *data = (CBackData<mesh_type> *)polygon_data;</mesh_type></mesh_type>
Shinya Kitaoka 120a6e
  assert(data->m_i == 0);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh_type,="" typename="" vertex_type=""></typename>
Shinya Kitaoka 120a6e
void CALLBACK tessVertex(void *vertex_data, void *polygon_data) {
Shinya Kitaoka 120a6e
  typedef typename mesh_type::vertex_type::point_type point_type;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  CBackData<mesh_type> *data = (CBackData<mesh_type> *)polygon_data;</mesh_type></mesh_type>
Shinya Kitaoka 120a6e
  vertex_type *vData         = (vertex_type *)vertex_data;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  GLdouble(&pos)[3] =
Shinya Kitaoka 120a6e
      TriMeshStuff::glu_vertex_traits<vertex_type>::vertex3d(*vData);</vertex_type>
Shinya Kitaoka 120a6e
  int &idx = TriMeshStuff::glu_vertex_traits<vertex_type>::index(*vData);</vertex_type>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (idx < 0) {
Shinya Kitaoka 120a6e
    data->m_mesh->addVertex(
Shinya Kitaoka 120a6e
        typename mesh_type::vertex_type(point_type(pos[0], pos[1])));
Shinya Kitaoka 120a6e
    idx = data->m_mesh->verticesCount() - 1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  data->m_triangle[data->m_i] = idx;
Shinya Kitaoka 120a6e
  data->m_i                   = (data->m_i + 1) % 3;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (data->m_i == 0)
Shinya Kitaoka 120a6e
    data->m_mesh->addFace(data->m_triangle[0], data->m_triangle[1],
Shinya Kitaoka 120a6e
                          data->m_triangle[2]);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// Supplied to ensure that triangle primitives are always of type GL_TRIANGLE
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Shinya Kitaoka 120a6e
void CALLBACK edgeFlag(GLboolean flag) {}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg::detail
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
//    Polygon triangulation
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace detail {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename func=""></typename>
Shinya Kitaoka 120a6e
void gluRegister(GLUtesselator *tess, GLenum which, Func *func) {
Shinya Kitaoka 120a6e
  gluTessCallback(tess, which, (TCG_GLU_CALLBACK)func);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg::detail
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename containersreader="" forit,="" typename=""></typename>
Shinya Kitaoka 120a6e
void gluTriangulate(ForIt tribeBegin, ForIt tribeEnd,
Shinya Kitaoka 120a6e
                    ContainersReader &meshes_reader) {
Shinya Kitaoka 120a6e
  using namespace detail;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  typedef typename tcg::traits<typename forit::value_type="">::pointed_type</typename>
Shinya Kitaoka 120a6e
      family_type;
Shinya Kitaoka 120a6e
  typedef typename tcg::traits<typename family_type::value_type="">::pointed_type</typename>
Shinya Kitaoka 120a6e
      polygon_type;
Shinya Kitaoka 120a6e
  typedef typename polygon_type::value_type vertex_type;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  typedef typename tcg::container_reader_traits<containersreader> output;</containersreader>
Shinya Kitaoka 120a6e
  typedef
Shinya Kitaoka 120a6e
      typename tcg::traits<typename output::value_type="">::pointed_type mesh_type;</typename>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  GLUtesselator *tess = gluNewTess();  // create a tessellator
Shinya Kitaoka 120a6e
  assert(tess);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Declare callbacks
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // NOTE: On g++, it seems that at this point each of the callbacks still have
Shinya Kitaoka 120a6e
  // undefined type.
Shinya Kitaoka 120a6e
  // We use the above gluRegister template to force the compiler to generate
Shinya Kitaoka 120a6e
  // these types.
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  gluRegister(tess, GLU_TESS_BEGIN_DATA, tessBegin<mesh_type>);</mesh_type>
Shinya Kitaoka 120a6e
  gluRegister(tess, GLU_TESS_END_DATA, tessEnd<mesh_type>);</mesh_type>
Shinya Kitaoka 120a6e
  gluRegister(tess, GLU_TESS_VERTEX_DATA, tessVertex<mesh_type, vertex_type="">);</mesh_type,>
Shinya Kitaoka 120a6e
  gluRegister(tess, GLU_TESS_EDGE_FLAG, edgeFlag<mesh_type>);</mesh_type>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  output::openContainer(meshes_reader);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Iterate the tribe. For every polygons family, associate one output mesh
Shinya Kitaoka 120a6e
  for (ForIt it = tribeBegin; it != tribeEnd; ++it) {
Shinya Kitaoka 120a6e
    // Build the output mesh and initialize stuff
Shinya Kitaoka 120a6e
    mesh_type *mesh = new mesh_type;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    CBackData<mesh_type> cbData;  // Callback Data about the mesh</mesh_type>
Shinya Kitaoka 120a6e
    cbData.m_mesh = mesh;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    // Tessellate the family
Shinya Kitaoka 120a6e
    gluTessBeginPolygon(tess, (void *)&cbData);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    typename family_type::iterator ft, fEnd = (*it)->end();
Shinya Kitaoka 120a6e
    for (ft = (*it)->begin(); ft != fEnd; ++ft) {
Shinya Kitaoka 120a6e
      gluTessBeginContour(tess);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      typename polygon_type::iterator pt, pEnd = (*ft)->end();
Shinya Kitaoka 120a6e
      for (pt = (*ft)->begin(); pt != pEnd; ++pt)
Shinya Kitaoka 120a6e
        gluTessVertex(
Shinya Kitaoka 120a6e
            tess, TriMeshStuff::glu_vertex_traits<vertex_type>::vertex3d(*pt),</vertex_type>
Shinya Kitaoka 120a6e
            (void *)&*pt);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      gluTessEndContour(tess);
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    gluTessEndPolygon(tess);  // Invokes the family tessellation
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    output::addElement(meshes_reader, mesh);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  gluDeleteTess(tess);  // delete after tessellation
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  output::closeContainer(meshes_reader);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
//    Mesh refinement
Toshihiro Shimizu 890ddd
//**************************************************************************************
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace detail {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Shinya Kitaoka 120a6e
inline void touchEdge(std::vector<uchar> &buildEdge, mesh_type &mesh, int e) {</uchar>
Shinya Kitaoka 120a6e
  typename mesh_type::edge_type &ed = mesh.edge(e);
Shinya Kitaoka 120a6e
  int f1 = ed.face(0), f2 = ed.face(1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (f1 >= 0) {
Shinya Kitaoka 120a6e
    typename mesh_type::face_type &f = mesh.face(f1);
Shinya Kitaoka 120a6e
    buildEdge[f.edge(0)] = 1, buildEdge[f.edge(1)] = 1,
Shinya Kitaoka 120a6e
    buildEdge[f.edge(2)] = 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  if (f2 >= 0) {
Shinya Kitaoka 120a6e
    typename mesh_type::face_type &f = mesh.face(f2);
Shinya Kitaoka 120a6e
    buildEdge[f.edge(0)] = 1, buildEdge[f.edge(1)] = 1,
Shinya Kitaoka 120a6e
    buildEdge[f.edge(2)] = 1;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Shinya Kitaoka 120a6e
inline void touchVertex(std::vector<uchar> &buildEdge, mesh_type &mesh, int v) {</uchar>
Shinya Kitaoka 120a6e
  // Sign all adjacent edges and adjacent faces' edges to v
Shinya Kitaoka 120a6e
  typename mesh_type::vertex_type &vx = mesh.vertex(v);
Shinya Kitaoka 120a6e
  const tcg::list<int> &incidentEdges = vx.edges();</int>
Shinya Kitaoka 120a6e
  tcg::list<int>::const_iterator it;</int>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (it = incidentEdges.begin(); it != incidentEdges.end(); ++it)
Shinya Kitaoka 120a6e
    touchEdge(buildEdge, mesh, *it);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//================================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Shinya Kitaoka 120a6e
class BoundaryEdges {
Shinya Kitaoka 120a6e
  std::vector<uchar> m_boundaryVertices;</uchar>
Shinya Kitaoka 120a6e
  const mesh_type &m_mesh;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  BoundaryEdges(const mesh_type &mesh) : m_mesh(mesh) {
Shinya Kitaoka 120a6e
    const tcg::list<typename mesh_type::edge_type=""> &edges = mesh.edges();</typename>
Shinya Kitaoka 120a6e
    const tcg::list<typename mesh_type::vertex_type=""> &vertices =</typename>
Shinya Kitaoka 120a6e
        mesh.vertices();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_boundaryVertices.resize(vertices.nodesCount(), 0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    typename tcg::list<typename mesh_type::edge_type="">::const_iterator it;</typename>
Shinya Kitaoka 120a6e
    for (it = edges.begin(); it != edges.end(); ++it) {
Shinya Kitaoka 120a6e
      if (it->face(0) < 0 || it->face(1) < 0) {
Shinya Kitaoka 120a6e
        m_boundaryVertices[it->vertex(0)] = true;
Shinya Kitaoka 120a6e
        m_boundaryVertices[it->vertex(1)] = true;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ~BoundaryEdges() {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  void setBoundaryVertex(int v) {
Shinya Kitaoka 120a6e
    m_boundaryVertices.resize(m_mesh.vertices().nodesCount(), 0);
Shinya Kitaoka 120a6e
    m_boundaryVertices[v] = 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool isBoundaryVertex(int v) const {
Shinya Kitaoka 120a6e
    return v < int(m_boundaryVertices.size()) && m_boundaryVertices[v];
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isBoundaryEdge(int e) const {
Shinya Kitaoka 120a6e
    const typename mesh_type::edge_type &ed = m_mesh.edge(e);
Shinya Kitaoka 120a6e
    return ed.face(0) < 0 || ed.face(1) < 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg::detail
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Toshihiro Shimizu 890ddd
void TriMeshStuff::DefaultEvaluator<mesh_type>::actionSort(</mesh_type>
Shinya Kitaoka 120a6e
    const mesh_type &mesh, int e,
Shinya Kitaoka 120a6e
    typename ActionEvaluator<mesh_type>::Action *actionSequence) {</mesh_type>
Shinya Kitaoka 120a6e
  typedef ActionEvaluator<mesh_type> ActionEvaluator;</mesh_type>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int count = 0;
Shinya Kitaoka 120a6e
  memset(actionSequence, ActionEvaluator::NONE,
Shinya Kitaoka 120a6e
         3 * sizeof(typename ActionEvaluator::Action));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Try to minimize the edge length deviation in e's neighbourhood
Shinya Kitaoka 120a6e
  const typename mesh_type::edge_type &ed = mesh.edge(e);
Shinya Kitaoka 120a6e
  int f1 = ed.face(0), f2 = ed.face(1);
Shinya Kitaoka 120a6e
  const TPointD *v1, *v2, *v3, *v4;
Shinya Kitaoka 120a6e
  double length[6];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  v1 = &mesh.vertex(ed.vertex(0)).P();
Shinya Kitaoka 120a6e
  v2 = &mesh.vertex(ed.vertex(1)).P();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  length[0]        = norm(*v2 - *v1);
Shinya Kitaoka 120a6e
  double lengthMax = length[0], lengthMin = length[0];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (f1 >= 0) {
Shinya Kitaoka 120a6e
    v3        = &mesh.vertex(mesh.otherFaceVertex(f1, e)).P();
Shinya Kitaoka 120a6e
    length[1] = norm(*v3 - *v1);
Shinya Kitaoka 120a6e
    length[2] = norm(*v3 - *v2);
Shinya Kitaoka 120a6e
    lengthMax = std::max({lengthMax, length[1], length[2]});
Shinya Kitaoka 120a6e
    lengthMin = std::min({lengthMin, length[1], length[2]});
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (f2 >= 0) {
Shinya Kitaoka 120a6e
    v4        = &mesh.vertex(mesh.otherFaceVertex(f2, e)).P();
Shinya Kitaoka 120a6e
    length[3] = norm(*v4 - *v1);
Shinya Kitaoka 120a6e
    length[4] = norm(*v4 - *v2);
Shinya Kitaoka 120a6e
    lengthMax = std::max({lengthMax, length[3], length[4]});
Shinya Kitaoka 120a6e
    lengthMin = std::min({lengthMin, length[3], length[4]});
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (f1 >= 0 && f2 >= 0) {
Shinya Kitaoka 120a6e
    // Build the edge lengths
Shinya Kitaoka 120a6e
    length[5] = norm(*v4 - *v3);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Evaluate swap - take the triangles with least maximum mean boundary edge
Shinya Kitaoka 120a6e
    double m1 = (length[0] + length[1] + length[2]) / 3.0;
Shinya Kitaoka 120a6e
    double m2 = (length[0] + length[3] + length[4]) / 3.0;
Shinya Kitaoka 120a6e
    double m3 = (length[5] + length[1] + length[3]) / 3.0;
Shinya Kitaoka 120a6e
    double m4 = (length[5] + length[2] + length[4]) / 3.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (std::max(m3, m4) < std::max(m1, m2) - 1e-5)
Shinya Kitaoka 120a6e
      actionSequence[count++] = ActionEvaluator::SWAP;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // NOTE: The original swap evaluation was about maximizing the minimal face
Shinya Kitaoka 120a6e
    // angle.
Shinya Kitaoka 120a6e
    // However, this requires quite some cross products - the above test is
Shinya Kitaoka 120a6e
    // sufficiently
Shinya Kitaoka 120a6e
    // simple and has a similar behaviour.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Evaluate collapse
Shinya Kitaoka 120a6e
    if (length[0] < m_collapseValue)
Shinya Kitaoka 120a6e
      actionSequence[count++] = ActionEvaluator::COLLAPSE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Evaluate split
Shinya Kitaoka 120a6e
  if (length[0] > m_splitValue)
Shinya Kitaoka 120a6e
    actionSequence[count++] = ActionEvaluator::SPLIT;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace detail {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Shinya Kitaoka 120a6e
inline bool testSwap(const mesh_type &mesh, int e) {
Shinya Kitaoka 120a6e
  // Retrieve adjacent faces
Shinya Kitaoka 120a6e
  const typename mesh_type::edge_type &ed = mesh.edge(e);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int f1 = ed.face(0), f2 = ed.face(1);
Shinya Kitaoka 120a6e
  if (f1 < 0 || f2 < 0) return false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Retrieve the 4 adjacent vertices
Shinya Kitaoka 120a6e
  const typename mesh_type::vertex_type &v1 = mesh.vertex(ed.vertex(0));
Shinya Kitaoka 120a6e
  const typename mesh_type::vertex_type &v2 = mesh.vertex(ed.vertex(1));
Shinya Kitaoka 120a6e
  const typename mesh_type::vertex_type &v3 =
Shinya Kitaoka 120a6e
      mesh.vertex(mesh.otherFaceVertex(f1, ed.getIndex()));
Shinya Kitaoka 120a6e
  const typename mesh_type::vertex_type &v4 =
Shinya Kitaoka 120a6e
      mesh.vertex(mesh.otherFaceVertex(f2, ed.getIndex()));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Make sure that vertex v4 lies between the semiplane generated by v3v1 and
Shinya Kitaoka 120a6e
  // v3v2
Shinya Kitaoka 120a6e
  TPointD a(v1.P() - v3.P()), b(v2.P() - v3.P());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double normA = norm(a), normB = norm(b);
Shinya Kitaoka 120a6e
  if (normA < 1e-5 || normB < 1e-5) return false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  a = a * (1.0 / normA);
Shinya Kitaoka 120a6e
  b = b * (1.0 / normB);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD c(v4.P() - v1.P()), d(v4.P() - v2.P());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double normC = norm(c), normD = norm(d);
Shinya Kitaoka 120a6e
  if (normC < 1e-5 || normD < 1e-5) return false;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  c = c * (1.0 / normC);
Shinya Kitaoka 120a6e
  d = d * (1.0 / normD);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double crossAC = point_ops::cross(a, c);
Shinya Kitaoka 120a6e
  int signAC     = crossAC < -1e-5 ? -1 : crossAC > 1e-5 ? 1 : 0;
Shinya Kitaoka 120a6e
  double crossBD = point_ops::cross(b, d);
Shinya Kitaoka 120a6e
  int signBD     = crossBD < -1e-5 ? -1 : crossBD > 1e-5 ? 1 : 0;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return signAC == -signBD;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// Tests edge e for admissibility of ad edge collapse. Edge e must not have
Shinya Kitaoka 120a6e
// adjacent
Shinya Kitaoka 120a6e
// faces with boundary components.
Shinya Kitaoka 120a6e
// Furthermore, we must test that faces adjacent to f1 and f2 keep e on the same
Shinya Kitaoka 120a6e
// side of
Shinya Kitaoka 120a6e
// the line passing through v3v1 and v3v2.
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Shinya Kitaoka 120a6e
inline bool testCollapse(const mesh_type &mesh, int e,
Shinya Kitaoka 120a6e
                         const BoundaryEdges<mesh_type> &boundary) {</mesh_type>
Shinya Kitaoka 120a6e
  // Any face adjacent to e must have no boundary edge
Shinya Kitaoka 120a6e
  const typename mesh_type::edge_type &ed = mesh.edge(e);
Shinya Kitaoka 120a6e
  int f1 = ed.face(0), f2 = ed.face(1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (f1 < 0 || f2 < 0) return false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int v1 = mesh.edge(e).vertex(0), v2 = mesh.edge(e).vertex(1);
Shinya Kitaoka 120a6e
  if (boundary.isBoundaryVertex(v1) || boundary.isBoundaryVertex(v2))
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Test faces adjacent to v1 or v2. Since one of their vertices will change,
Shinya Kitaoka 120a6e
  // we must make sure that their
Shinya Kitaoka 120a6e
  //'side' does not change too.
Shinya Kitaoka 120a6e
  int v = mesh.otherFaceVertex(f1, e);
Shinya Kitaoka 120a6e
  int l = mesh.edgeInciding(v1, v);
Shinya Kitaoka 120a6e
  int f =
Shinya Kitaoka 120a6e
      mesh.edge(l).face(0) == f1 ? mesh.edge(l).face(1) : mesh.edge(l).face(0);
Shinya Kitaoka 120a6e
  int vNext = mesh.otherFaceVertex(f, l);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (f != f2) {
Shinya Kitaoka 120a6e
    // Test face f
Shinya Kitaoka 120a6e
    if (tsign(point_ops::cross(mesh.vertex(vNext).P() - mesh.vertex(v).P(),
Shinya Kitaoka 120a6e
                               mesh.vertex(v2).P() - mesh.vertex(v).P())) !=
Shinya Kitaoka 120a6e
        tsign(point_ops::cross(mesh.vertex(vNext).P() - mesh.vertex(v).P(),
Shinya Kitaoka 120a6e
                               mesh.vertex(v1).P() - mesh.vertex(v).P())))
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Update vars
Shinya Kitaoka 120a6e
    v = vNext;
Shinya Kitaoka 120a6e
    l = mesh.edgeInciding(v1, v);
Shinya Kitaoka 120a6e
    f = mesh.edge(l).face(0) == f ? mesh.edge(l).face(1) : mesh.edge(l).face(0);
Shinya Kitaoka 120a6e
    vNext = mesh.otherFaceVertex(f, l);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // Same with respect to v2
Shinya Kitaoka 120a6e
  v = mesh.otherFaceVertex(f1, e);
Shinya Kitaoka 120a6e
  l = mesh.edgeInciding(v2, v);
Shinya Kitaoka 120a6e
  f = mesh.edge(l).face(0) == f1 ? mesh.edge(l).face(1) : mesh.edge(l).face(0);
Shinya Kitaoka 120a6e
  vNext = mesh.otherFaceVertex(f, l);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  while (f != f2) {
Shinya Kitaoka 120a6e
    // Test face f
Shinya Kitaoka 120a6e
    if (tsign(point_ops::cross(mesh.vertex(vNext).P() - mesh.vertex(v).P(),
Shinya Kitaoka 120a6e
                               mesh.vertex(v2).P() - mesh.vertex(v).P())) !=
Shinya Kitaoka 120a6e
        tsign(point_ops::cross(mesh.vertex(vNext).P() - mesh.vertex(v).P(),
Shinya Kitaoka 120a6e
                               mesh.vertex(v1).P() - mesh.vertex(v).P())))
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // Update vars
Shinya Kitaoka 120a6e
    v = vNext;
Shinya Kitaoka 120a6e
    l = mesh.edgeInciding(v2, v);
Shinya Kitaoka 120a6e
    f = mesh.edge(l).face(0) == f ? mesh.edge(l).face(1) : mesh.edge(l).face(0);
Shinya Kitaoka 120a6e
    vNext = mesh.otherFaceVertex(f, l);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg::detail
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename mesh_type=""></typename>
Shinya Kitaoka 120a6e
void refineMesh(mesh_type &mesh, TriMeshStuff::ActionEvaluator<mesh_type> &eval,</mesh_type>
Shinya Kitaoka 120a6e
                unsigned long maxActions) {
Shinya Kitaoka 120a6e
  using namespace detail;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  typedef TriMeshStuff::ActionEvaluator<mesh_type> Evaluator;</mesh_type>
Shinya Kitaoka 120a6e
  typedef typename Evaluator::Action Action;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // DIAGNOSTICS_TIMER("simplifyMesh");
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Build boundary edges. They will not be altered by the simplification
Shinya Kitaoka 120a6e
  // procedure.
Shinya Kitaoka 120a6e
  detail::BoundaryEdges<mesh_type> boundary(mesh);</mesh_type>
Shinya Kitaoka 120a6e
  Action actions[3], *act, *actEnd = actions + 3;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  tcg::list<edge> &edges = mesh.edges();</edge>
Shinya Kitaoka 120a6e
  tcg::list<edge>::iterator it;</edge>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // DIAGNOSTICS_SET("Simplify | Vertex count (before simplify)",
Shinya Kitaoka 120a6e
  // mesh.vertexCount());
Shinya Kitaoka 120a6e
  // DIAGNOSTICS_SET("Simplify | Edges count (before simplify)", edges.size());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Build a vector of the edges to be analyzed
Shinya Kitaoka 120a6e
  std::vector<uchar> buildEdge(edges.nodesCount(), 1);</uchar>
Shinya Kitaoka 120a6e
  int touchedIdx;
Shinya Kitaoka 120a6e
  bool boundaryEdge;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
cycle:
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (maxActions-- == 0) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // Analyze mesh for possible updates. Perform the first one.
Shinya Kitaoka 120a6e
  for (it = edges.begin(); it != edges.end(); ++it) {
Shinya Kitaoka 120a6e
    if (!buildEdge[it.m_idx]) continue;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    boundaryEdge = boundary.isBoundaryEdge(it.m_idx);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    eval.actionSort(mesh, it.m_idx, actions);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    for (act = actions; act < actEnd; ++act) {
Shinya Kitaoka 120a6e
      // Try to perform the i-th action
Shinya Kitaoka 120a6e
      if (*act == Evaluator::NONE)
Shinya Kitaoka 120a6e
        break;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      else if (!boundaryEdge && *act == Evaluator::SWAP &&
Shinya Kitaoka 120a6e
               testSwap(mesh, it.m_idx)) {
Shinya Kitaoka 120a6e
        touchedIdx = mesh.swapEdge(it.m_idx);
Shinya Kitaoka 120a6e
        touchEdge(buildEdge, mesh, touchedIdx);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        goto cycle;
Shinya Kitaoka 120a6e
      } else if (!boundaryEdge && *act == Evaluator::COLLAPSE &&
Shinya Kitaoka 120a6e
                 testCollapse(mesh, it.m_idx, boundary)) {
Shinya Kitaoka 120a6e
        touchedIdx = mesh.collapseEdge(it.m_idx);
Shinya Kitaoka 120a6e
        touchVertex(buildEdge, mesh, touchedIdx);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        goto cycle;
Shinya Kitaoka 120a6e
      } else if (*act == Evaluator::SPLIT) {
Shinya Kitaoka 120a6e
        Edge &ed = mesh.edge(it.m_idx);
Shinya Kitaoka 120a6e
        touchVertex(buildEdge, mesh, ed.vertex(0));
Shinya Kitaoka 120a6e
        touchVertex(buildEdge, mesh, ed.vertex(1));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        touchedIdx = mesh.splitEdge(it.m_idx);
Shinya Kitaoka 120a6e
        if (buildEdge.size() < edges.size()) buildEdge.resize(edges.size());
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        touchVertex(buildEdge, mesh, touchedIdx);
Shinya Kitaoka 120a6e
        if (boundaryEdge) boundary.setBoundaryVertex(touchedIdx);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        goto cycle;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    buildEdge[it.m_idx] = 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  // DIAGNOSTICS_SET("Simplify | Vertex count (after simplify)",
Shinya Kitaoka 120a6e
  // mesh.vertexCount());
Shinya Kitaoka 120a6e
  // DIAGNOSTICS_SET("Simplify | Edges count (after simplify)", edges.size());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace tcg
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
#endif  // TCG_TRIANGULATE_HPP