/usr/include/mia-2.0/mia/mesh/triangulate.hh is in libmia-2.0-dev 2.0.13-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | /* -*- mia-c++ -*-
*
* This file is part of MIA - a toolbox for medical image analysis
* Copyright (c) Leipzig, Madrid 1999-2013 Gert Wollny
*
* MIA 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 3 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with MIA; if not, see <http://www.gnu.org/licenses/>.
*
*/
#include <mia/mesh/clist.hh>
#include <mia/core/msgstream.hh>
#include <iostream>
NS_MIA_BEGIN
/**
\brief class to make a triangle mesh from a closed polygon
This class is used to convert a closed polygon into a mesh defined
by triangles.
\tparam VertexVector type of the vector that contains the vertices
\tparam Polygon The type of the Polygon
*/
template <class VertexVector, class Polygon>
class TPolyTriangulator {
public:
/**
Constructor to be initializes with a vector of vertices that used when defining the polygon
\param vv the vertices
*/
TPolyTriangulator(const VertexVector& vv);
/**
Evaluate the triangulation of the polygon that is defined by the vertices given above and
their ordering by indices as given in poly.
\param [out] output the list of triangles as list of index triples into the vertex arrey given in the
constructor
\param [in] poly the index array into the vertex array describing the polygon
\returns true of the triangulation was successfull and false if the polygon only consisted only
of two corners.
*/
template <class TriangleList>
bool triangulate(TriangleList& output, const Polygon& poly) const;
private:
typedef clist<typename Polygon::value_type> CPoly;
typedef typename VertexVector::value_type Vector;
Vector eval_orientation(const Polygon& poly) const;
bool is_convex(const typename CPoly::const_iterator& i, bool debug = false) const;
bool is_ear(const typename CPoly::const_iterator& p, const CPoly& cpoly, bool debug = false) const;
bool is_inside(
const typename VertexVector::value_type& a,
const typename VertexVector::value_type& b,
const typename VertexVector::value_type& c,
const typename VertexVector::value_type& p,
bool debug = false ) const;
const VertexVector& m_vv;
mutable Vector m_orientation;
};
template <class VertexVector, class Polygon>
TPolyTriangulator<VertexVector,Polygon>::TPolyTriangulator(const VertexVector& vv):
m_vv(vv)
{
}
template <class VertexVector, class Polygon>
template <class TriangleList>
bool
TPolyTriangulator<VertexVector,Polygon>::triangulate(TriangleList& output, const Polygon& poly) const
{
size_t poly_size = poly.size();
if ( poly_size < 3) // no triangles at all
return false;
typedef clist<typename Polygon::value_type> CPoly;
CPoly cpoly;
m_orientation = eval_orientation(poly);
typename Polygon::const_iterator pi = poly.begin();
typename Polygon::const_iterator pe = poly.end();
while (pi != pe)
cpoly.push_back(*pi++);
typename CPoly::iterator p_i = cpoly.begin();
typename CPoly::iterator p_e = cpoly.end();
p_i = p_i->succ;
while ( (p_i != p_e) && (poly_size > 3)) {
if (is_ear(p_i, cpoly, false)) {
// we have a valid triangle, store it
output.push_back(typename TriangleList::value_type(**p_i->succ, **p_i, **p_i->prev));
// set the current middle node
typename CPoly::iterator p_r = p_i;
p_i = (p_i->prev != cpoly.begin()) ? p_i->prev : p_i->succ;
cpoly.remove(p_r);
--poly_size;
}else
p_i = p_i->succ;
}
if ((p_i == p_e) && (poly_size > 3)) {
cvdebug() <<"gotcha\n";
p_i = cpoly.begin();
p_i = p_i->succ;
while ( (p_i != p_e) && (poly_size > 3)) {
if (is_ear(p_i, cpoly, true)) {
// we have a valid triangle, store it
output.push_back(typename TriangleList::value_type(**p_i->succ, **p_i, **p_i->prev));
// set the current middle node
typename CPoly::iterator p_r = p_i;
p_i = (p_i->prev != cpoly.begin()) ? p_i->prev : p_i->succ;
cpoly.remove(p_r);
--poly_size;
}else
p_i = p_i->succ;
}
}
output.push_back(typename TriangleList::value_type(**p_i->succ, **p_i, **p_i->prev));
return true;
}
template <class VertexVector, class Polygon>
typename TPolyTriangulator<VertexVector,Polygon>::Vector
TPolyTriangulator<VertexVector,Polygon>::eval_orientation(const Polygon& poly) const
{
typename VertexVector::value_type result(0,0,0);
typename Polygon::const_iterator pb = poly.begin();
typename Polygon::const_iterator be = poly.end();
typename Polygon::const_iterator c1 = pb;
++c1;
typename Polygon::const_iterator c2 = c1;
++c2;
typename VertexVector::value_type a = m_vv[*pb];
while (c1 != be && c2 != be) {
result += (m_vv[*c1++] - a) ^ (m_vv[*c2++] - a);
}
return result;
}
template <class VertexVector, class Polygon>
bool TPolyTriangulator<VertexVector,Polygon>::is_convex(const typename CPoly::const_iterator& i, bool /*debug*/) const
{
typename VertexVector::value_type a = m_vv[**i->prev];
typename VertexVector::value_type b = m_vv[**i];
typename VertexVector::value_type c = m_vv[**i->succ];
typename VertexVector::value_type ab = a - b;
typename VertexVector::value_type cb = c - b;
typename VertexVector::value_type cross = ab ^ cb;
const bool result = dot(cross, m_orientation) > 0;
return result;
}
template <class VertexVector, class Polygon>
bool TPolyTriangulator<VertexVector,Polygon>::is_ear(const typename CPoly::const_iterator& p, const CPoly& cpoly, bool debug) const
{
if (!is_convex(p,debug)) {
cvdebug() << "corner is concave\n";
return false;
}
typename VertexVector::value_type a = m_vv[**p->prev];
typename VertexVector::value_type b = m_vv[**p];
typename VertexVector::value_type c = m_vv[**p->succ];
cvdebug() << "check triangle" << a << b << c << " = (" << **p->prev << "," << **p << "," << **p->succ << "\n";
typename CPoly::const_iterator i = cpoly.begin();
i = i->succ;
while (i != cpoly.end()) {
if (i != p && i != p->prev && i != p->succ)
if (!is_convex(i, debug) && is_inside(a,b,c,m_vv[**i], debug)) {
cvdebug() << "point " << **i << ":" << m_vv[**i] << " is concave and inside\n";
return false;
}
i = i->succ;
}
return true;
}
template <class VertexVector, class Polygon>
bool TPolyTriangulator<VertexVector,Polygon>::is_inside(
const typename VertexVector::value_type& a,
const typename VertexVector::value_type& b,
const typename VertexVector::value_type& c,
const typename VertexVector::value_type& p,
bool /*debug*/) const
{
double abc = ((a-b)^(c-b)).norm() * 0.5;
double abp = ((a-p)^(b-p)).norm() * 0.5;
double acp = ((a-p)^(c-p)).norm() * 0.5;
double bcp = ((b-p)^(c-p)).norm() * 0.5;
const bool result = (abc >= abp + acp + bcp);
return result;
}
NS_MIA_END
|