|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// TnzCore includes
|
|
Toshihiro Shimizu |
890ddd |
#include "trop.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "trop_borders.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tpixelutils.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// tcg includes
|
|
Toshihiro Shimizu |
890ddd |
#include "tcg_wrap.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "tcg/tcg_point.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tcg/tcg_cyclic.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "tcg/tcg_containers_reader.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#define INCLUDE_HPP
|
|
Toshihiro Shimizu |
890ddd |
#include "tcg/tcg_triangulate.h"
|
|
Toshihiro Shimizu |
890ddd |
#undef INCLUDE_HPP
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// TnzExt includes
|
|
Toshihiro Shimizu |
890ddd |
#define INCLUDE_HPP
|
|
Toshihiro Shimizu |
890ddd |
#include "../common/trop/raster_edge_evaluator.h"
|
|
Toshihiro Shimizu |
890ddd |
#undef INCLUDE_HPP
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#include "ext/meshbuilder.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// Local namespace
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
namespace
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
struct PolygonVertex {
|
|
Toshihiro Shimizu |
890ddd |
double m_pos[3];
|
|
Toshihiro Shimizu |
890ddd |
int m_idx;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PolygonVertex(const TPoint &p)
|
|
Toshihiro Shimizu |
890ddd |
: m_idx(-1) { m_pos[0] = p.x, m_pos[1] = p.y, m_pos[2] = 0.0; }
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
} // namespace
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// tcg stuff
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
namespace tcg
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <>
|
|
Toshihiro Shimizu |
890ddd |
struct traits<ttexturemeshp> {</ttexturemeshp>
|
|
Toshihiro Shimizu |
890ddd |
typedef TTextureMeshP *pointer_type;
|
|
Toshihiro Shimizu |
890ddd |
typedef TTextureMeshP *&reference_type;
|
|
Toshihiro Shimizu |
890ddd |
typedef TTextureMesh pointed_type;
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <>
|
|
Toshihiro Shimizu |
890ddd |
struct point_traits<polygonvertex> {</polygonvertex>
|
|
Toshihiro Shimizu |
890ddd |
typedef PolygonVertex point_type;
|
|
Toshihiro Shimizu |
890ddd |
typedef double value_type;
|
|
Toshihiro Shimizu |
890ddd |
typedef double float_type;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
inline static value_type x(const point_type &p) { return p.m_pos[0]; }
|
|
Toshihiro Shimizu |
890ddd |
inline static value_type y(const point_type &p) { return p.m_pos[1]; }
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
} // namespace tcg
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// MeshBuilder Locals
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
namespace
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//======================================================================================
|
|
Toshihiro Shimizu |
890ddd |
// Thresholding stuff
|
|
Toshihiro Shimizu |
890ddd |
//======================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename pix=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
void thresholdRaster(const TRasterPT<pix> &ras, TRasterGR8P &out, const Pix &transp)</pix>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
int lx = ras->getLx(), y, ly = ras->getLy();
|
|
Toshihiro Shimizu |
890ddd |
for (y = 0; y < ly; ++y) {
|
|
Toshihiro Shimizu |
890ddd |
Pix *pix, *line = ras->pixels(y), *lineEnd = line + lx;
|
|
Toshihiro Shimizu |
890ddd |
TPixelGR8 *gr, *grLine = out->pixels(y);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
for (pix = line, gr = grLine; pix != lineEnd; ++pix, ++gr)
|
|
Toshihiro Shimizu |
890ddd |
gr->value = (pix->m && *pix != transp) ? 0 : 255;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename pix=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
void thresholdRasterGr(const TRasterPT<pix> &ras, TRasterGR8P &out, const Pix &transp)</pix>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
int lx = ras->getLx(), y, ly = ras->getLy();
|
|
Toshihiro Shimizu |
890ddd |
for (y = 0; y < ly; ++y) {
|
|
Toshihiro Shimizu |
890ddd |
Pix *pix, *line = ras->pixels(y), *lineEnd = line + lx;
|
|
Toshihiro Shimizu |
890ddd |
TPixelGR8 *gr, *grLine = out->pixels(y);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
for (pix = line, gr = grLine; pix != lineEnd; ++pix, ++gr)
|
|
Toshihiro Shimizu |
890ddd |
gr->value = (*pix != transp) ? 0 : 255;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void thresholdRasterCM32(const TRasterCM32P &ras, TRasterGR8P &out)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
int lx = ras->getLx(), y, ly = ras->getLy();
|
|
Toshihiro Shimizu |
890ddd |
for (y = 0; y < ly; ++y) {
|
|
Toshihiro Shimizu |
890ddd |
TPixelCM32 *pix, *line = ras->pixels(y), *lineEnd = line + lx;
|
|
Toshihiro Shimizu |
890ddd |
TPixelGR8 *gr, *grLine = out->pixels(y);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
for (pix = line, gr = grLine; pix != lineEnd; ++pix, ++gr)
|
|
Toshihiro Shimizu |
890ddd |
gr->value = (pix->isPurePaint() && !pix->getPaint()) ? 255 : 0;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TRasterGR8P thresholdRaster(const TRasterP &ras, const MeshBuilderOptions &opts)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TRasterGR8P binaryRas(ras->getLx(), ras->getLy());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TRasterCM32P rasCM(ras);
|
|
Toshihiro Shimizu |
890ddd |
if (rasCM)
|
|
Toshihiro Shimizu |
890ddd |
thresholdRasterCM32(rasCM, binaryRas);
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
switch (ras->getPixelSize()) {
|
|
Toshihiro Shimizu |
890ddd |
case 1: {
|
|
Toshihiro Shimizu |
890ddd |
TRasterGR8P rasGR8(ras);
|
|
Toshihiro Shimizu |
890ddd |
thresholdRasterGr(rasGR8, binaryRas, TPixelGR8::from(toPixel32(opts.m_transparentColor)));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
CASE 2:
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TRasterGR16P rasGR16(ras);
|
|
Toshihiro Shimizu |
890ddd |
thresholdRasterGr(rasGR16, binaryRas, TPixelGR16::from(opts.m_transparentColor));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
CASE 4:
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TRaster32P ras32(ras);
|
|
Toshihiro Shimizu |
890ddd |
thresholdRaster(ras32, binaryRas, toPixel32(opts.m_transparentColor));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
CASE 8:
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
TRaster64P ras64(ras);
|
|
Toshihiro Shimizu |
890ddd |
thresholdRaster(ras64, binaryRas, opts.m_transparentColor);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
DEFAULT:
|
|
Toshihiro Shimizu |
890ddd |
assert(false);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Build an enlarged ras to preserve borders. 5 pixels should be fine.
|
|
Toshihiro Shimizu |
890ddd |
TRasterGR8P result(ras->getLx(), ras->getLy());
|
|
Toshihiro Shimizu |
890ddd |
TRop::blur(result, binaryRas, opts.m_margin, 0, 0);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
thresholdRasterGr(result, result, TPixelGR8::White);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return result;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//======================================================================================
|
|
Toshihiro Shimizu |
890ddd |
// Borders Extraction stuff
|
|
Toshihiro Shimizu |
890ddd |
//======================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
using namespace TRop::borders;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
inline void delete_(T t) { delete t; }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename t=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
struct Vector : public std::vector<t> {</t>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
Vector() : std::vector<t>() {}</t>
|
|
Toshihiro Shimizu |
890ddd |
~Vector() { std::for_each(this->begin(), this->end(), delete_<t>); }</t>
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
typedef std::vector<tpoint> RasterBorder;</tpoint>
|
|
Toshihiro Shimizu |
890ddd |
typedef std::vector<polygonvertex> Polygon;</polygonvertex>
|
|
Toshihiro Shimizu |
890ddd |
typedef Vector<polygon *=""> Family;</polygon>
|
|
Toshihiro Shimizu |
890ddd |
typedef Vector<family *=""> Tribe;</family>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
struct PolygonReader {
|
|
Toshihiro Shimizu |
890ddd |
Polygon *m_polygon;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
typedef tcg::cyclic_iterator<rasterborder::iterator> cyclic_iter;</rasterborder::iterator>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
PolygonReader() : m_polygon(0) {}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void openContainer(const cyclic_iter &ct)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_polygon = new Polygon;
|
|
Toshihiro Shimizu |
890ddd |
m_polygon->push_back(PolygonVertex(*ct));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void addElement(const cyclic_iter &ct) { m_polygon->push_back(PolygonVertex(*ct)); }
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void closeContainer() {}
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
class BordersReader : public ImageMeshesReaderT<tpixelgr8></tpixelgr8>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
Vector<rasterborder *=""> m_borders;</rasterborder>
|
|
Toshihiro Shimizu |
890ddd |
RasterBorder *m_current;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
BordersReader()
|
|
Toshihiro Shimizu |
890ddd |
: ImageMeshesReaderT<tpixelgr8>(PixelSelector<tpixelgr8>(false)), m_current(0) {}</tpixelgr8></tpixelgr8>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void openFace(ImageMesh *mesh, int faceIdx, const TPixelGR8 &color)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
ImageMeshesReader::openFace(mesh, faceIdx); // defines imageIndex
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (mesh) {
|
|
Toshihiro Shimizu |
890ddd |
ImageMesh::face_type &fc = mesh->face(faceIdx);
|
|
Toshihiro Shimizu |
890ddd |
fc.imageIndex() = (color.value) ? 0 : 1; // redefines iI
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void openEdge(const raster_edge_iterator &it)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_current = new RasterBorder;
|
|
Toshihiro Shimizu |
890ddd |
m_current->push_back(it.pos());
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void addVertex(const raster_edge_iterator &it)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_current->push_back(it.pos());
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void closeEdge(ImageMesh *mesh, int edgeIdx)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
ImageMesh::edge_type &ed = mesh->edge(edgeIdx);
|
|
Toshihiro Shimizu |
890ddd |
ed.imageIndex() = m_borders.size();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_borders.push_back(m_current);
|
|
Toshihiro Shimizu |
890ddd |
m_current = 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
ImageMeshesReader::closeEdge(mesh, edgeIdx);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
Polygon *reduceBorder(RasterBorder *border)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
typedef RasterBorder::iterator iter;
|
|
Toshihiro Shimizu |
890ddd |
typedef tcg::cyclic_iterator<iter> cyclic_iter;</iter>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
iter b(border->begin()), e(border->end());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
cyclic_iter cBegin(b, b, e - 1, 0),
|
|
Toshihiro Shimizu |
890ddd |
cEnd(b + 1, b, e - 1, 1);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
RasterEdgeEvaluator<cyclic_iter> eval(</cyclic_iter>
|
|
Toshihiro Shimizu |
890ddd |
cBegin - 1, cEnd + 1, 2.0, (std::numeric_limits<double>::max)());</double>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
PolygonReader reader;
|
|
Toshihiro Shimizu |
890ddd |
tcg::sequence_ops::minimalPath(cBegin, cEnd, eval, reader);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return reader.m_polygon;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void reduceBorders(Tribe *tribe, const ImageMeshesReader &reader, const Vector<rasterborder *=""> &borders,</rasterborder>
|
|
Toshihiro Shimizu |
890ddd |
int meshIdx, const ImageMesh::face_type &fc)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// Traverse the image structure. Each time a black face is encountered, add its associated family to the
|
|
Toshihiro Shimizu |
890ddd |
// resulting tribe.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
const tcg::list<imagemeshp> &meshes = reader.meshes();</imagemeshp>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (meshIdx >= 0 && fc.imageIndex()) {
|
|
Toshihiro Shimizu |
890ddd |
// Build a family. Start by extracting the face's contour
|
|
Toshihiro Shimizu |
890ddd |
Family *family = new Family;
|
|
Toshihiro Shimizu |
890ddd |
tribe->push_back(family);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
const ImageMeshP &mesh = meshes[meshIdx];
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
Polygon *outerBorder = reduceBorder(borders[mesh->edge(0).imageIndex()]);
|
|
Toshihiro Shimizu |
890ddd |
family->push_back(outerBorder);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Then, extract the contours of every sub-mesh
|
|
Toshihiro Shimizu |
890ddd |
int m, mCount = fc.meshesCount();
|
|
Toshihiro Shimizu |
890ddd |
for (m = 0; m < mCount; ++m) {
|
|
Toshihiro Shimizu |
890ddd |
int mIdx = fc.mesh(m);
|
|
Toshihiro Shimizu |
890ddd |
const ImageMeshP &childMesh = meshes[mIdx];
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
Polygon *innerBorder = reduceBorder(borders[childMesh->edge(0).imageIndex()]);
|
|
Toshihiro Shimizu |
890ddd |
family->push_back(innerBorder);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
reduceBorders(tribe, reader, borders, mIdx, childMesh->face(0));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Recursive on the face's sub-mesh faces
|
|
Toshihiro Shimizu |
890ddd |
int m, mCount = fc.meshesCount();
|
|
Toshihiro Shimizu |
890ddd |
for (m = 0; m < mCount; ++m) {
|
|
Toshihiro Shimizu |
890ddd |
int mIdx = fc.mesh(m);
|
|
Toshihiro Shimizu |
890ddd |
reduceBorders(tribe, reader, borders, mIdx, meshes[mIdx]->face(0));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//--------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
Tribe *extractBorders(const TRasterGR8P &ras)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
Tribe *result = new Tribe;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
BordersReader reader;
|
|
Toshihiro Shimizu |
890ddd |
TRop::borders::readMeshes(ras, reader);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
reduceBorders(result, reader, reader.m_borders, -1, reader.outerFace());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return result;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//======================================================================================
|
|
Toshihiro Shimizu |
890ddd |
// Maximum Edge Length stuff
|
|
Toshihiro Shimizu |
890ddd |
//======================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double buildMinimumEdgeLength(Tribe *tribe, int targetMaxVerticesCount)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// Calculate the tribe's total area
|
|
Toshihiro Shimizu |
890ddd |
double area = 0.0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
Tribe::iterator ft, fEnd(tribe->end());
|
|
Toshihiro Shimizu |
890ddd |
for (ft = tribe->begin(); ft != fEnd; ++ft) {
|
|
Toshihiro Shimizu |
890ddd |
Family *fam = *ft;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Add area corresponding to the external border
|
|
Toshihiro Shimizu |
890ddd |
area += fabs(tcg::polyline_ops::area(fam->front()->begin(), fam->front()->end()));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Then, subtract all the internal ones
|
|
Toshihiro Shimizu |
890ddd |
Family::iterator pt, pEnd(fam->end());
|
|
Toshihiro Shimizu |
890ddd |
for (pt = ++fam->begin(); pt != pEnd; ++pt)
|
|
Toshihiro Shimizu |
890ddd |
area -= fabs(tcg::polyline_ops::area((*pt)->begin(), (*pt)->end()));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Given the area, find the approximate edge length corresponding to the required
|
|
Toshihiro Shimizu |
890ddd |
// vertices count
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// The idea is: given a polygon, its uniform triangular mesh - made of, say, lots of equilateral
|
|
Toshihiro Shimizu |
890ddd |
// triangles, has vertices count proportional to ( polygon area / sq(mean mesh edge length) ).
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Under this assumption, take an equilater triangle as our polygon, and assume that its built
|
|
Toshihiro Shimizu |
890ddd |
// mesh is the regular mesh obtained by subdividing it multiple times, Sierpinsky-like.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Since it can easily be sheared to a corresponding half-square, it can be SEEN that in
|
|
Toshihiro Shimizu |
890ddd |
// this case its vertices count is EXACTLY (l / e_length)^2 / 2, l being the large
|
|
Toshihiro Shimizu |
890ddd |
// triangle's edge. By extension, if A is the area of the triangle, then we have:
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// A = l^2 * sqrt(3/2) / 2; => l = sqrt(2 * K * A), K = 1.0 / sqrt(3/2);
|
|
Toshihiro Shimizu |
890ddd |
// V = K * A / e_length^2; => e_length = sqrt(KA / V);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// And we just extend this from 'triangle' to 'polygon'.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return sqrt(area / (sqrt(1.5) * targetMaxVerticesCount));
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//======================================================================================
|
|
Toshihiro Shimizu |
890ddd |
// Mesh Refinement stuff
|
|
Toshihiro Shimizu |
890ddd |
//======================================================================================
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void refineMeshes(const TMeshImageP &mi, const MeshBuilderOptions &options)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
std::vector<ttexturemeshp> &meshes = mi->meshes();</ttexturemeshp>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Refine every mesh to achieve a target mesh density dependent on the image size
|
|
Toshihiro Shimizu |
890ddd |
/*TRectD bbox(mi->getBBox());
|
|
Toshihiro Shimizu |
890ddd |
double targetLength = sqrt(bbox.getLx() * bbox.getLy()) * relativeMeshDensity;*/
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double targetLength = options.m_targetEdgeLength;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
int m, mCount = meshes.size();
|
|
Toshihiro Shimizu |
890ddd |
for (m = 0; m < mCount; ++m) {
|
|
Toshihiro Shimizu |
890ddd |
const TTextureMeshP &mesh = meshes[m];
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
tcg::TriMeshStuff::DefaultEvaluator<ttexturemesh> eval(0.0, (std::numeric_limits<double>::max)());</double></ttexturemesh>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// First, perform edge swaps alone. This is useful since results from gluTriangulate
|
|
Toshihiro Shimizu |
890ddd |
// tend to be unbalanced to vertical - this is a good correction.
|
|
Toshihiro Shimizu |
890ddd |
tcg::refineMesh(*mesh, eval);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Now, launch a full-scale, finishing simplification
|
|
Toshihiro Shimizu |
890ddd |
eval.m_collapseValue = targetLength * 0.6;
|
|
Toshihiro Shimizu |
890ddd |
eval.m_splitValue = targetLength * 1.4;
|
|
Toshihiro Shimizu |
890ddd |
tcg::refineMesh(*mesh, eval, 20000); //Max 10000 iterations - to avoid loops
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Since we stopped at a max number of iterations, separate collapses from splits
|
|
Toshihiro Shimizu |
890ddd |
// and simplify until the procedure stops.
|
|
Toshihiro Shimizu |
890ddd |
eval.m_collapseValue = 0.0;
|
|
Toshihiro Shimizu |
890ddd |
eval.m_splitValue = 1.4 * targetLength;
|
|
Toshihiro Shimizu |
890ddd |
tcg::refineMesh(*mesh, eval);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
eval.m_splitValue = (std::numeric_limits<double>::max)();</double>
|
|
Toshihiro Shimizu |
890ddd |
eval.m_collapseValue = targetLength * 0.6;
|
|
Toshihiro Shimizu |
890ddd |
tcg::refineMesh(*mesh, eval);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Perform 1000 final iterations with uniform split and collapses
|
|
Toshihiro Shimizu |
890ddd |
eval.m_splitValue = targetLength * 1.4;
|
|
Toshihiro Shimizu |
890ddd |
tcg::refineMesh(*mesh, eval, 1000);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Finally, squeeze the mesh to ensure that VEF containers are tight
|
|
Toshihiro Shimizu |
890ddd |
mesh->squeeze();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
} // namespace
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// Mesh Builder function
|
|
Toshihiro Shimizu |
890ddd |
//**************************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
TMeshImageP buildMesh(const TRasterP &ras, const MeshBuilderOptions &options)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
// Convert the input image to a binary raster
|
|
Toshihiro Shimizu |
890ddd |
TRasterGR8P binaryRas = thresholdRaster(ras, options);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Extract the image borders
|
|
Toshihiro Shimizu |
890ddd |
Tribe *tribe = extractBorders(binaryRas);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Calculate maximum edge length
|
|
Toshihiro Shimizu |
890ddd |
double minEdgeLength = buildMinimumEdgeLength(tribe, options.m_targetMaxVerticesCount);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
MeshBuilderOptions opts(options);
|
|
Toshihiro Shimizu |
890ddd |
opts.m_targetEdgeLength = tmax(opts.m_targetEdgeLength, minEdgeLength);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Perform tessellation
|
|
Toshihiro Shimizu |
890ddd |
TMeshImageP meshImage(new TMeshImage);
|
|
Toshihiro Shimizu |
890ddd |
std::vector<ttexturemeshp> &meshes = meshImage->meshes();</ttexturemeshp>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
tcg::sequential_reader<std::vector<ttexturemeshp>> reader(&meshes);</std::vector<ttexturemeshp>
|
|
Toshihiro Shimizu |
890ddd |
tcg::gluTriangulate(tribe->begin(), tribe->end(), reader);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
delete tribe;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// Perform meshes refinement
|
|
Toshihiro Shimizu |
890ddd |
refineMeshes(meshImage, opts);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return meshImage;
|
|
Toshihiro Shimizu |
890ddd |
}
|