tahoma2d/toonz/sources/include/trop_borders.h

278 lines
9.2 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
// #pragma once // could not use by INCLUDE_HPP
2016-03-19 06:57:51 +13:00
#ifndef TROP_BORDERS_H
#define TROP_BORDERS_H
2016-04-14 22:15:09 +12:00
#include <memory>
2016-03-19 06:57:51 +13:00
// TnzCore includes
#include "tsmartpointer.h"
#include "tgeometry.h"
#include "traster.h"
#include "trastercm.h"
// tcg includes
#include "tcg_wrap.h"
#include "tcg/tcg_vertex.h"
#include "tcg/tcg_edge.h"
#include "tcg/tcg_face.h"
#include "tcg/tcg_mesh.h"
// TRop::borders includes
#include "../common/trop/pixelselectors.h"
#include "../common/trop/raster_edge_iterator.h"
#include "../common/trop/borders_extractor.h"
#undef DVAPI
#undef DVVAR
#ifdef TROP_EXPORTS
#define DVAPI DV_EXPORT_API
#define DVVAR DV_EXPORT_VAR
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
#endif
//==============================================================================
/*!
The TRop::borders namespace contains functions and classes that perform
contours extraction of a specified raster (either greyscale, fullcolor
or colormap).
Image contours are currently extracted in a not-hierarchycal fashion
(ie in succession) and supplied through a reimplementable reader class.
*/
//==============================================================================
2016-06-15 18:43:10 +12:00
namespace TRop {
namespace borders {
2016-03-19 06:57:51 +13:00
// Standard type instantiations
template class DVAPI RasterEdgeIterator<PixelSelector<TPixel32>>;
template class DVAPI RasterEdgeIterator<PixelSelector<TPixel64>>;
template class DVAPI RasterEdgeIterator<PixelSelector<TPixelGR8>>;
template class DVAPI RasterEdgeIterator<PixelSelector<TPixelGR16>>;
template class DVAPI RasterEdgeIterator<PixelSelector<TPixelCM32>>;
2016-03-19 06:57:51 +13:00
//**********************************************************************
// Borders Extraction Reader (callbacks container)
//**********************************************************************
/*!
The BordersReader class defines the supported callbacks fired from the
borders extraction routines.
*/
2016-06-15 18:43:10 +12:00
class DVAPI BordersReader {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
virtual void openContainer(const TPoint &pos, const TPoint &dir,
const TPixel32 &innerColor,
const TPixel32 &outerColor) {}
virtual void openContainer(const TPoint &pos, const TPoint &dir,
const TPixel64 &innerColor,
const TPixel64 &outerColor) {}
virtual void openContainer(const TPoint &pos, const TPoint &dir,
const TPixelGR8 &innerColor,
const TPixelGR8 &outerColor) {}
virtual void openContainer(const TPoint &pos, const TPoint &dir,
const TPixelGR16 &innerColor,
const TPixelGR16 &outerColor) {}
virtual void openContainer(const TPoint &pos, const TPoint &dir,
TUINT32 innerColorIdx, TUINT32 outerColorIdx) {
} // colormap
virtual void addElement(const TPoint &pos, const TPoint &dir,
const TPixel32 &outerColor) {}
virtual void addElement(const TPoint &pos, const TPoint &dir,
const TPixel64 &outerColor) {}
virtual void addElement(const TPoint &pos, const TPoint &dir,
const TPixelGR8 &outerColor) {}
virtual void addElement(const TPoint &pos, const TPoint &dir,
const TPixelGR16 &outerColor) {}
virtual void addElement(const TPoint &pos, const TPoint &dir,
TUINT32 outerColorIdx) {}
virtual void closeContainer() {}
2016-03-19 06:57:51 +13:00
};
//**********************************************************************
// Borders Extraction functions
//**********************************************************************
2016-06-15 18:43:10 +12:00
// Generic readBorders
void DVAPI readBorders_simple(const TRasterP &raster, BordersReader &reader,
bool onlyCorners = true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Greymap readBorders with transparency color
2016-03-19 06:57:51 +13:00
void DVAPI readBorders_simple(const TRasterGR8P &raster, BordersReader &reader,
2016-06-15 18:43:10 +12:00
const TPixelGR8 &transparencyColor,
bool onlyCorners = true);
2016-03-19 06:57:51 +13:00
void DVAPI readBorders_simple(const TRasterGR16P &raster, BordersReader &reader,
2016-06-15 18:43:10 +12:00
const TPixelGR16 &transparencyColor,
bool onlyCorners = true);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Colormap readBorders with ink/paint antialias chooser
2016-03-19 06:57:51 +13:00
void DVAPI readBorders_simple(const TRasterCM32P &raster, BordersReader &reader,
2016-06-15 18:43:10 +12:00
bool onlyCorners = true, int toneThreshold = 128);
2016-03-19 06:57:51 +13:00
//**********************************************************************
// Mesh Extraction (classes)
//**********************************************************************
typedef tcg::Vertex<TPoint> Vertex;
//--------------------------------------------------------------------------------
class Edge final : public tcg::Edge {
2016-06-15 18:43:10 +12:00
TPoint m_directions[2];
int m_imageIndex;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
const TPoint &direction(int i) const { return m_directions[i]; }
TPoint &direction(int i) { return m_directions[i]; }
//! The edge index in the overall image extraction procedure. This value can
//! be used to
//! univocally associate external data to an image edge.
int imageIndex() const { return m_imageIndex; }
int &imageIndex() { return m_imageIndex; }
2016-03-19 06:57:51 +13:00
};
//--------------------------------------------------------------------------------
class Face final : public tcg::Face {
2016-06-15 18:43:10 +12:00
tcg::list<int> m_meshes;
int m_imageIndex;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
const tcg::list<int> &meshes() const { return m_meshes; }
tcg::list<int> &meshes() { return m_meshes; }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
int meshesCount() const { return m_meshes.size(); }
int mesh(int m) const { return m_meshes[m]; }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
//! The face index in the overall image extraction procedure. This value can
//! be used to
//! univocally associate external data to an image face.
int imageIndex() const { return m_imageIndex; }
int &imageIndex() { return m_imageIndex; }
2016-03-19 06:57:51 +13:00
};
//--------------------------------------------------------------------------------
class ImageMesh final : public TSmartObject,
public tcg::Mesh<Vertex, Edge, Face> {};
2016-03-19 06:57:51 +13:00
//--------------------------------------------------------------------------------
}
} // namespace TRop::borders
2016-04-15 17:11:23 +12:00
#ifdef _WIN32
template class DVAPI TSmartPointerT<TRop::borders::ImageMesh>;
2016-03-19 06:57:51 +13:00
#endif
namespace TRop {
namespace borders {
2016-03-19 06:57:51 +13:00
typedef TSmartPointerT<ImageMesh> ImageMeshP;
//**********************************************************************
// Mesh Extraction (reader)
//**********************************************************************
2016-06-15 18:43:10 +12:00
class DVAPI ImageMeshesReader {
class Imp;
std::unique_ptr<Imp> m_imp;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
ImageMeshesReader();
virtual ~ImageMeshesReader();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const Face &outerFace() const;
Face &outerFace();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const tcg::list<ImageMeshP> &meshes() const;
tcg::list<ImageMeshP> &meshes();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void clear();
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
// Face reader functions. Nested meshes are stored by default in a
// global list of meshes retrievable with the meshes() accessor.
// Each Mesh Face will be filled with the list of its sub-meshes in
// the form of indices to the global list.
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void openFace(ImageMesh *mesh, int faceIdx);
void addMesh(ImageMesh *mesh);
void closeFace();
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
void closeEdge(ImageMesh *mesh, int edgeIdx);
2016-03-19 06:57:51 +13:00
private:
2016-06-15 18:43:10 +12:00
// Not implemented
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
ImageMeshesReader(const ImageMeshesReader &);
ImageMeshesReader &operator=(const ImageMeshesReader &);
2016-03-19 06:57:51 +13:00
};
//--------------------------------------------------------------------------------
template <typename Pixel>
2016-06-15 18:43:10 +12:00
class ImageMeshesReaderT : public ImageMeshesReader {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
typedef PixelSelector<Pixel> pixel_selector_type;
typedef typename pixel_selector_type::value_type value_type;
typedef RasterEdgeIterator<pixel_selector_type> raster_edge_iterator;
2016-03-19 06:57:51 +13:00
protected:
2016-06-15 18:43:10 +12:00
pixel_selector_type m_selector;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
ImageMeshesReaderT() : m_selector(false) {}
ImageMeshesReaderT(const pixel_selector_type &selector)
: m_selector(selector) {}
~ImageMeshesReaderT() {}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const pixel_selector_type &pixelSelector() const { return m_selector; }
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
virtual void openFace(ImageMesh *mesh, int faceIdx,
const value_type &colorValue) {
ImageMeshesReader::openFace(mesh, faceIdx);
}
virtual void addMesh(ImageMesh *mesh) { ImageMeshesReader::addMesh(mesh); }
virtual void closeFace() { ImageMeshesReader::closeFace(); }
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
// Edge reader functions. Edges are not stored by default.
// Users should reimplement these methods to store or process edge
// data for the required purpose.
virtual void openEdge(const raster_edge_iterator &it) {}
virtual void addVertex(const raster_edge_iterator &it) {}
virtual void closeEdge(ImageMesh *mesh, int edgeIdx) {
ImageMeshesReader::closeEdge(mesh, edgeIdx);
}
2016-03-19 06:57:51 +13:00
};
//**********************************************************************
// Mesh Extraction (functions)
//**********************************************************************
template <typename Pixel>
2016-06-15 18:43:10 +12:00
void readMeshes(const TRasterPT<Pixel> &raster,
ImageMeshesReaderT<Pixel> &reader);
//--------------------------------------------------------------------------------
2016-03-19 06:57:51 +13:00
}
2016-06-15 18:43:10 +12:00
} // namespace TRop::borders
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
#endif // TROP_BORDERS_H
2016-03-19 06:57:51 +13:00
#ifdef INCLUDE_HPP
#include "../common/trop/borders_extractor.hpp"
#include "../common/trop/raster_edge_iterator.hpp"
#endif