Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
#include "toonz/autoclose.h"
Toshihiro Shimizu 890ddd
#include "trastercm.h"
Toshihiro Shimizu 890ddd
#include "skeletonlut.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define AUT_SPOT_SAMPLES 10
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
using namespace SkeletonLut;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TAutocloser::Imp
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	struct Seed {
Toshihiro Shimizu 890ddd
		UCHAR *m_ptr;
Toshihiro Shimizu 890ddd
		UCHAR m_preseed;
Toshihiro Shimizu 890ddd
		Seed(UCHAR *ptr, UCHAR preseed) : m_ptr(ptr), m_preseed(preseed) {}
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int m_closingDistance;
Toshihiro Shimizu 890ddd
	double m_spotAngle;
Toshihiro Shimizu 890ddd
	int m_inkIndex;
Toshihiro Shimizu 890ddd
	int m_opacity;
Toshihiro Shimizu 890ddd
	TRasterP m_raster;
Toshihiro Shimizu 890ddd
	TRasterGR8P m_bRaster;
Toshihiro Shimizu 890ddd
	UCHAR *m_br;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int m_bWrap;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int m_displaceVector[8];
Toshihiro Shimizu 890ddd
	TPointD m_displAverage;
Toshihiro Shimizu 890ddd
	int m_visited;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double m_csp, m_snp, m_csm, m_snm, m_csa, m_sna, m_csb, m_snb;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Imp(const TRasterP &r, int distance = 10, double angle = (180 * TConsts::pi) / 360.0, int index = 0, int opacity = 0)
Toshihiro Shimizu 890ddd
		: m_raster(r), m_spotAngle(angle), m_closingDistance(distance), m_inkIndex(index), m_opacity(opacity)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	~Imp() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool inline isInk(UCHAR *br) { return (*br) & 0x1; }
Toshihiro Shimizu 890ddd
	inline void eraseInk(UCHAR *br) { *(br) &= 0xfe; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UCHAR inline ePix(UCHAR *br) { return (*(br + 1)); }
Toshihiro Shimizu 890ddd
	UCHAR inline wPix(UCHAR *br) { return (*(br - 1)); }
Toshihiro Shimizu 890ddd
	UCHAR inline nPix(UCHAR *br) { return (*(br + m_bWrap)); }
Toshihiro Shimizu 890ddd
	UCHAR inline sPix(UCHAR *br) { return (*(br - m_bWrap)); }
Toshihiro Shimizu 890ddd
	UCHAR inline swPix(UCHAR *br) { return (*(br - m_bWrap - 1)); }
Toshihiro Shimizu 890ddd
	UCHAR inline nwPix(UCHAR *br) { return (*(br + m_bWrap - 1)); }
Toshihiro Shimizu 890ddd
	UCHAR inline nePix(UCHAR *br) { return (*(br + m_bWrap + 1)); }
Toshihiro Shimizu 890ddd
	UCHAR inline sePix(UCHAR *br) { return (*(br - m_bWrap + 1)); }
Toshihiro Shimizu 890ddd
	UCHAR inline neighboursCode(UCHAR *seed)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return ((swPix(seed) & 0x1) | ((sPix(seed) & 0x1) << 1) |
Toshihiro Shimizu 890ddd
				((sePix(seed) & 0x1) << 2) | ((wPix(seed) & 0x1) << 3) |
Toshihiro Shimizu 890ddd
				((ePix(seed) & 0x1) << 4) | ((nwPix(seed) & 0x1) << 5) |
Toshihiro Shimizu 890ddd
				((nPix(seed) & 0x1) << 6) | ((nePix(seed) & 0x1) << 7));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//.......................
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	inline bool notMarkedBorderInk(UCHAR *br)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return ((((*br) & 0x5) == 1) && (ePix(br) == 0 || wPix(br) == 0 || nPix(br) == 0 || sPix(br) == 0));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//.......................
Toshihiro Shimizu 890ddd
	UCHAR *getPtr(int x, int y) { return m_br + m_bWrap * y + x; }
Toshihiro Shimizu 890ddd
	UCHAR *getPtr(const TPoint &p) { return m_br + m_bWrap * p.y + p.x; }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPoint getCoordinates(UCHAR *br)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		TPoint p;
Toshihiro Shimizu 890ddd
		int pixelCount = br - m_bRaster->getRawData();
Toshihiro Shimizu 890ddd
		p.y = pixelCount / m_bWrap;
Toshihiro Shimizu 890ddd
		p.x = pixelCount - p.y * m_bWrap;
Toshihiro Shimizu 890ddd
		return p;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//.......................
Shinya Kitaoka 3bfa54
	void compute(std::vector<segment> &closingSegmentArray);</segment>
Shinya Kitaoka 3bfa54
	void draw(const std::vector<segment> &closingSegmentArray);</segment>
Shinya Kitaoka 3bfa54
	void skeletonize(std::vector<tpoint> &endpoints);</tpoint>
Shinya Kitaoka 3bfa54
	void findSeeds(std::vector<seed> &seeds, std::vector<tpoint> &endpoints);</tpoint></seed>
Shinya Kitaoka 3bfa54
	void erase(std::vector<seed> &seeds, std::vector<tpoint> &endpoints);</tpoint></seed>
Toshihiro Shimizu 890ddd
	void circuitAndMark(UCHAR *seed, UCHAR preseed);
Shinya Kitaoka 3bfa54
	bool circuitAndCancel(UCHAR *seed, UCHAR preseed, std::vector<tpoint> &endpoints);</tpoint>
Shinya Kitaoka 3bfa54
	void findMeetingPoints(std::vector<tpoint> &endpoints, std::vector<segment> &closingSegments);</segment></tpoint>
Shinya Kitaoka 3bfa54
	void calculateWeightAndDirection(std::vector<segment> &orientedEndpoints);</segment>
Shinya Kitaoka 3bfa54
	bool spotResearchTwoPoints(std::vector<segment> &endpoints, std::vector<segment> &closingSegments);</segment></segment>
Shinya Kitaoka 3bfa54
	bool spotResearchOnePoint(std::vector<segment> &endpoints, std::vector<segment> &closingSegments);</segment></segment>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void copy(const TRasterGR8P &braux, TRaster32P &raux);
Toshihiro Shimizu 890ddd
	int exploreTwoSpots(const TAutocloser::Segment &s0, const TAutocloser::Segment &s1);
Toshihiro Shimizu 890ddd
	int notInsidePath(const TPoint &p, const TPoint &q);
Toshihiro Shimizu 890ddd
	void drawInByteRaster(const TPoint &p0, const TPoint &p1);
Toshihiro Shimizu 890ddd
	TPoint visitEndpoint(UCHAR *br);
Toshihiro Shimizu 890ddd
	bool exploreSpot(const Segment &s, TPoint &p);
Toshihiro Shimizu 890ddd
	bool exploreRay(UCHAR *br, Segment s, TPoint &p);
Toshihiro Shimizu 890ddd
	void visitPix(UCHAR *br, int toVisit, const TPoint &dis);
Toshihiro Shimizu 890ddd
	void cancelMarks(UCHAR *br);
Shinya Kitaoka 3bfa54
	void cancelFromArray(std::vector<segment> &array, TPoint p, int &count);</segment>
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define DRAW_SEGMENT(a, b, da, db, istr1, istr2, block) \
Toshihiro Shimizu 890ddd
	{                                                   \
Toshihiro Shimizu 890ddd
		d = 2 * db - da;                                \
Toshihiro Shimizu 890ddd
		incr_1 = 2 * db;                                \
Toshihiro Shimizu 890ddd
		incr_2 = 2 * (db - da);                         \
Toshihiro Shimizu 890ddd
		while (a < da) {                                \
Toshihiro Shimizu 890ddd
			if (d <= 0) {                               \
Toshihiro Shimizu 890ddd
				d += incr_1;                            \
Toshihiro Shimizu 890ddd
				a++;                                    \
Toshihiro Shimizu 890ddd
				istr1;                                  \
Toshihiro Shimizu 890ddd
			} else {                                    \
Toshihiro Shimizu 890ddd
				d += incr_2;                            \
Toshihiro Shimizu 890ddd
				a++;                                    \
Toshihiro Shimizu 890ddd
				b++;                                    \
Toshihiro Shimizu 890ddd
				istr2;                                  \
Toshihiro Shimizu 890ddd
			}                                           \
Toshihiro Shimizu 890ddd
			block;                                      \
Toshihiro Shimizu 890ddd
		}                                               \
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define EXPLORE_RAY_ISTR(istr)                                              \
Toshihiro Shimizu 890ddd
	if (!inside_ink) {                                                      \
Toshihiro Shimizu 890ddd
		if (((*br) & 0x1) && !((*br) & 0x80)) {                             \
Toshihiro Shimizu 890ddd
			p.x = istr;                                                     \
Toshihiro Shimizu 890ddd
			p.y = (s.first.y < s.second.y) ? s.first.y + y : s.first.y - y; \
Toshihiro Shimizu 890ddd
			return true;                                                    \
Toshihiro Shimizu 890ddd
		}                                                                   \
Toshihiro Shimizu 890ddd
	} else if (inside_ink && !((*br) & 0x1))                                \
Toshihiro Shimizu 890ddd
		inside_ink = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline bool isInk(const TPixel32 &pix)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return pix.r < 80;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TRasterGR8P fillByteRaster(const TRasterCM32P &r, TRasterGR8P &bRaster)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	int lx = r->getLx();
Toshihiro Shimizu 890ddd
	int ly = r->getLy();
Toshihiro Shimizu 890ddd
	//bRaster->create(lx+4, ly+4);
Toshihiro Shimizu 890ddd
	UCHAR *br = bRaster->getRawData();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < lx + 4; i++)
Toshihiro Shimizu 890ddd
		*(br++) = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < lx + 4; i++)
Toshihiro Shimizu 890ddd
		*(br++) = 131;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < ly; i++) {
Toshihiro Shimizu 890ddd
		*(br++) = 0;
Toshihiro Shimizu 890ddd
		*(br++) = 131;
Toshihiro Shimizu 890ddd
		TPixelCM32 *pix = r->pixels(i);
Toshihiro Shimizu 890ddd
		for (j = 0; j < lx; j++, pix++) {
Toshihiro Shimizu 890ddd
			if (pix->getTone() != pix->getMaxTone())
Toshihiro Shimizu 890ddd
				*(br++) = 3;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				*(br++) = 0;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		*(br++) = 131;
Toshihiro Shimizu 890ddd
		*(br++) = 0;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < lx + 4; i++)
Toshihiro Shimizu 890ddd
		*(br++) = 131;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < lx + 4; i++)
Toshihiro Shimizu 890ddd
		*(br++) = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return bRaster;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#define SET_INK                              \
Toshihiro Shimizu 890ddd
	if (buf->getTone() == buf->getMaxTone()) \
Toshihiro Shimizu 890ddd
		*buf = TPixelCM32(inkIndex, 0, 255 - opacity);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void drawSegment(TRasterCM32P &r, const TAutocloser::Segment &s, USHORT inkIndex, USHORT opacity)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int wrap = r->getWrap();
Toshihiro Shimizu 890ddd
	TPixelCM32 *buf = r->pixels();
Toshihiro Shimizu 890ddd
	/*
Toshihiro Shimizu 890ddd
int i, j;
Toshihiro Shimizu 890ddd
for (i=0; i<r->getLy();i++)</r->
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
	for (j=0; j<r->getLx();j++, buf++)</r->
Toshihiro Shimizu 890ddd
    *buf = (1<<4)|0xf;
Toshihiro Shimizu 890ddd
  buf += wrap-r->getLx();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
return;
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int x, y, dx, dy, d, incr_1, incr_2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int x1 = s.first.x;
Toshihiro Shimizu 890ddd
	int y1 = s.first.y;
Toshihiro Shimizu 890ddd
	int x2 = s.second.x;
Toshihiro Shimizu 890ddd
	int y2 = s.second.y;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (x1 > x2) {
Toshihiro Shimizu 890ddd
		tswap(x1, x2);
Toshihiro Shimizu 890ddd
		tswap(y1, y2);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	buf += y1 * wrap + x1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dx = x2 - x1;
Toshihiro Shimizu 890ddd
	dy = y2 - y1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x = y = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dy >= 0) {
Toshihiro Shimizu 890ddd
		if (dy <= dx)
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(x, y, dx, dy, (buf++), (buf += wrap + 1), SET_INK)
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(y, x, dy, dx, (buf += wrap), (buf += wrap + 1), SET_INK)
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		dy = -dy;
Toshihiro Shimizu 890ddd
		if (dy <= dx)
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(x, y, dx, dy, (buf++), (buf -= (wrap - 1)), SET_INK)
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(y, x, dy, dx, (buf -= wrap), (buf -= (wrap - 1)), SET_INK)
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::Imp::compute(std::vector<segment> &closingSegmentArray)</segment>
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<tpoint> endpoints;</tpoint>
Toshihiro Shimizu 890ddd
	try {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		assert(closingSegmentArray.empty());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRasterCM32P raux;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!(raux = (TRasterCM32P)m_raster))
Toshihiro Shimizu 890ddd
			throw TException("Unable to autoclose a not CM32 image.");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_raster->getLx() == 0 || m_raster->getLy() == 0)
Toshihiro Shimizu 890ddd
			throw TException("Autoclose error: bad image size");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//Lx = r->lx;
Toshihiro Shimizu 890ddd
		//Ly = r->ly;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRasterGR8P braux(raux->getLx() + 4, raux->getLy() + 4);
Toshihiro Shimizu 890ddd
		braux->lock();
Toshihiro Shimizu 890ddd
		fillByteRaster(raux, braux);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		TRect r(2, 2, braux->getLx() - 3, braux->getLy() - 3);
Toshihiro Shimizu 890ddd
		m_bRaster = braux->extract(r);
Toshihiro Shimizu 890ddd
		m_br = m_bRaster->getRawData();
Toshihiro Shimizu 890ddd
		m_bWrap = m_bRaster->getWrap();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		m_displaceVector[0] = -m_bWrap - 1;
Toshihiro Shimizu 890ddd
		m_displaceVector[1] = -m_bWrap;
Toshihiro Shimizu 890ddd
		m_displaceVector[2] = -m_bWrap + 1;
Toshihiro Shimizu 890ddd
		m_displaceVector[3] = -1;
Toshihiro Shimizu 890ddd
		m_displaceVector[4] = +1;
Toshihiro Shimizu 890ddd
		m_displaceVector[5] = m_bWrap - 1;
Toshihiro Shimizu 890ddd
		m_displaceVector[6] = m_bWrap;
Toshihiro Shimizu 890ddd
		m_displaceVector[7] = m_bWrap + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		skeletonize(endpoints);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		findMeetingPoints(endpoints, closingSegmentArray);
Toshihiro Shimizu 890ddd
		//copy(m_bRaster, raux);
Toshihiro Shimizu 890ddd
		braux->unlock();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	catch (TException &e) {
Toshihiro Shimizu 890ddd
		throw e;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::Imp::draw(const std::vector<segment> &closingSegmentArray)</segment>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRasterCM32P raux;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (!(raux = (TRasterCM32P)m_raster))
Toshihiro Shimizu 890ddd
		throw TException("Unable to autoclose a not CM32 image.");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (m_raster->getLx() == 0 || m_raster->getLy() == 0)
Toshihiro Shimizu 890ddd
		throw TException("Autoclose error: bad image size");
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (int i = 0; i < (int)closingSegmentArray.size(); i++)
Toshihiro Shimizu 890ddd
		drawSegment(raux, closingSegmentArray[i], m_inkIndex, m_opacity);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TAutocloser::Imp::copy(const TRasterGR8P &br, TRaster32P &r)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(r->getLx() == br->getLx() && r->getLy() == br->getLy());
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int lx = r->getLx();
Toshihiro Shimizu 890ddd
	int ly = r->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UCHAR *bbuf = br->getRawData();
Toshihiro Shimizu 890ddd
	TPixel *buf = (TPixel *)r->getRawData();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < ly; i++) {
Toshihiro Shimizu 890ddd
		for (j = 0; j < lx; j++, buf++, bbuf++) {
Toshihiro Shimizu 890ddd
			buf->m = 255;
Toshihiro Shimizu 890ddd
			if ((*bbuf) & 0x40)
Toshihiro Shimizu 890ddd
				buf->r = 255, buf->g = buf->b = 0;
Toshihiro Shimizu 890ddd
			else if (isInk(bbuf))
Toshihiro Shimizu 890ddd
				buf->r = buf->g = buf->b = 0;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				buf->r = buf->g = buf->b = 255;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		buf += r->getWrap() - lx;
Toshihiro Shimizu 890ddd
		bbuf += br->getWrap() - lx;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int intersect_segment(int x1, int y1, int x2, int y2, int i, double *ris)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if ((i < tmin(y1, y2)) || (i > tmax(y1, y2)) || (y1 == y2))
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*ris = ((double)((x1 - x2) * (i - y2)) / (double)(y1 - y2) + x2);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 1;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
inline int distance2(const TPoint p0, const TPoint p1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (p0.x - p1.x) * (p0.x - p1.x) + (p0.y - p1.y) * (p0.y - p1.y);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
int closerPoint(const std::vector<tautocloser::segment> &points, std::vector<bool> &marks, int index)</bool></tautocloser::segment>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	assert(points.size() == marks.size());
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int min, curr;
Toshihiro Shimizu 890ddd
	int minval = (std::numeric_limits<int>::max)();</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	min = index + 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (curr = index + 1; curr < (int)points.size(); curr++)
Toshihiro Shimizu 890ddd
		if (!(marks[curr])) {
Toshihiro Shimizu 890ddd
			int distance = distance2(points[index].first, points[curr].first);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (distance < minval) {
Toshihiro Shimizu 890ddd
				minval = distance;
Toshihiro Shimizu 890ddd
				min = curr;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	marks[min] = true;
Toshihiro Shimizu 890ddd
	return min;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int intersect_triangle(int x1a, int y1a, int x2a, int y2a, int x3a, int y3a,
Toshihiro Shimizu 890ddd
					   int x1b, int y1b, int x2b, int y2b, int x3b, int y3b)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int minx, maxx, miny, maxy, i;
Toshihiro Shimizu 890ddd
	double xamin, xamax, xbmin, xbmax, val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	miny = tmax(tmin(y1a, y2a, y3a), tmin(y1b, y2b, y3b));
Toshihiro Shimizu 890ddd
	maxy = tmin(tmax(y1a, y2a, y3a), tmax(y1b, y2b, y3b));
Toshihiro Shimizu 890ddd
	if (maxy < miny)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	minx = tmax(tmin(x1a, x2a, x3a), tmin(x1b, x2b, x3b));
Toshihiro Shimizu 890ddd
	maxx = tmin(tmax(x1a, x2a, x3a), tmax(x1b, x2b, x3b));
Toshihiro Shimizu 890ddd
	if (maxx < minx)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = miny; i <= maxy; i++) {
Toshihiro Shimizu 890ddd
		xamin = xamax = xbmin = xbmax = 0.0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		intersect_segment(x1a, y1a, x2a, y2a, i, &xamin);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (intersect_segment(x1a, y1a, x3a, y3a, i, &val))
Toshihiro Shimizu 890ddd
			if (xamin)
Toshihiro Shimizu 890ddd
				xamax = val;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				xamin = val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!xamax)
Toshihiro Shimizu 890ddd
			intersect_segment(x2a, y2a, x3a, y3a, i, &xamax);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (xamax < xamin) {
Toshihiro Shimizu 890ddd
			val = xamin, xamin = xamax, xamax = val;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		intersect_segment(x1b, y1b, x2b, y2b, i, &xbmin);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (intersect_segment(x1b, y1b, x3b, y3b, i, &val))
Toshihiro Shimizu 890ddd
			if (xbmin)
Toshihiro Shimizu 890ddd
				xbmax = val;
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				xbmin = val;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!xbmax)
Toshihiro Shimizu 890ddd
			intersect_segment(x2b, y2b, x3b, y3b, i, &xbmax);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (xbmax < xbmin) {
Toshihiro Shimizu 890ddd
			val = xbmin, xbmin = xbmax, xbmax = val;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (!((tceil(xamax) < tfloor(xbmin)) || (tceil(xbmax) < tfloor(xamin))))
Toshihiro Shimizu 890ddd
			return 1;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} //namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TAutocloser::Imp::notInsidePath(const TPoint &p, const TPoint &q)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int tmp, x, y, dx, dy, d, incr_1, incr_2;
Toshihiro Shimizu 890ddd
	int x1, y1, x2, y2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x1 = p.x;
Toshihiro Shimizu 890ddd
	y1 = p.y;
Toshihiro Shimizu 890ddd
	x2 = q.x;
Toshihiro Shimizu 890ddd
	y2 = q.y;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (x1 > x2) {
Toshihiro Shimizu 890ddd
		tmp = x1, x1 = x2, x2 = tmp;
Toshihiro Shimizu 890ddd
		tmp = y1, y1 = y2, y2 = tmp;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	UCHAR *br = getPtr(x1, y1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	dx = x2 - x1;
Toshihiro Shimizu 890ddd
	dy = y2 - y1;
Toshihiro Shimizu 890ddd
	x = y = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dy >= 0) {
Toshihiro Shimizu 890ddd
		if (dy <= dx)
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(x, y, dx, dy, (br++), (br += m_bWrap + 1), if (!((*br) & 0x2)) return true)
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(y, x, dy, dx, (br += m_bWrap), (br += m_bWrap + 1), if (!((*br) & 0x2)) return true)
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		dy = -dy;
Toshihiro Shimizu 890ddd
		if (dy <= dx)
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(x, y, dx, dy, (br++), (br -= m_bWrap - 1), if (!((*br) & 0x2)) return true)
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(y, x, dy, dx, (br -= m_bWrap), (br -= m_bWrap - 1), if (!((*br) & 0x2)) return true)
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TAutocloser::Imp::exploreTwoSpots(const TAutocloser::Segment &s0, const TAutocloser::Segment &s1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x1a, y1a, x2a, y2a, x3a, y3a, x1b, y1b, x2b, y2b, x3b, y3b;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x1a = s0.first.x;
Toshihiro Shimizu 890ddd
	y1a = s0.first.y;
Toshihiro Shimizu 890ddd
	x1b = s1.first.x;
Toshihiro Shimizu 890ddd
	y1b = s1.first.y;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPoint p0aux = s0.second;
Toshihiro Shimizu 890ddd
	TPoint p1aux = s1.second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (x1a == p0aux.x && y1a == p0aux.y)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
	if (x1b == p1aux.x && y1b == p1aux.y)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x2a = tround(x1a + (p0aux.x - x1a) * m_csp - (p0aux.y - y1a) * m_snp);
Toshihiro Shimizu 890ddd
	y2a = tround(y1a + (p0aux.x - x1a) * m_snp + (p0aux.y - y1a) * m_csp);
Toshihiro Shimizu 890ddd
	x3a = tround(x1a + (p0aux.x - x1a) * m_csm - (p0aux.y - y1a) * m_snm);
Toshihiro Shimizu 890ddd
	y3a = tround(y1a + (p0aux.x - x1a) * m_snm + (p0aux.y - y1a) * m_csm);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x2b = tround(x1b + (p1aux.x - x1b) * m_csp - (p1aux.y - y1b) * m_snp);
Toshihiro Shimizu 890ddd
	y2b = tround(y1b + (p1aux.x - x1b) * m_snp + (p1aux.y - y1b) * m_csp);
Toshihiro Shimizu 890ddd
	x3b = tround(x1b + (p1aux.x - x1b) * m_csm - (p1aux.y - y1b) * m_snm);
Toshihiro Shimizu 890ddd
	y3b = tround(y1b + (p1aux.x - x1b) * m_snm + (p1aux.y - y1b) * m_csm);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return (intersect_triangle(x1a, y1a, p0aux.x, p0aux.y, x2a, y2a,
Toshihiro Shimizu 890ddd
							   x1b, y1b, p1aux.x, p1aux.y, x2b, y2b) ||
Toshihiro Shimizu 890ddd
			intersect_triangle(x1a, y1a, p0aux.x, p0aux.y, x3a, y3a,
Toshihiro Shimizu 890ddd
							   x1b, y1b, p1aux.x, p1aux.y, x2b, y2b) ||
Toshihiro Shimizu 890ddd
			intersect_triangle(x1a, y1a, p0aux.x, p0aux.y, x2a, y2a,
Toshihiro Shimizu 890ddd
							   x1b, y1b, p1aux.x, p1aux.y, x3b, y3b) ||
Toshihiro Shimizu 890ddd
			intersect_triangle(x1a, y1a, p0aux.x, p0aux.y, x3a, y3a,
Toshihiro Shimizu 890ddd
							   x1b, y1b, p1aux.x, p1aux.y, x3b, y3b));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::Imp::findMeetingPoints(std::vector<tpoint> &endpoints, std::vector<segment> &closingSegments)</segment></tpoint>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
	double alfa;
Toshihiro Shimizu 890ddd
	alfa = m_spotAngle / AUT_SPOT_SAMPLES;
Toshihiro Shimizu 890ddd
	m_csp = cos(m_spotAngle / 5);
Toshihiro Shimizu 890ddd
	m_snp = sin(m_spotAngle / 5);
Toshihiro Shimizu 890ddd
	m_csm = cos(-m_spotAngle / 5);
Toshihiro Shimizu 890ddd
	m_snm = sin(-m_spotAngle / 5);
Toshihiro Shimizu 890ddd
	m_csa = cos(alfa);
Toshihiro Shimizu 890ddd
	m_sna = sin(alfa);
Toshihiro Shimizu 890ddd
	m_csb = cos(-alfa);
Toshihiro Shimizu 890ddd
	m_snb = sin(-alfa);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
	std::vector<segment> orientedEndpoints(endpoints.size());</segment>
Toshihiro Shimizu 890ddd
	for (i = 0; i < (int)endpoints.size(); i++)
Toshihiro Shimizu 890ddd
		orientedEndpoints[i].first = endpoints[i];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int size = -1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while ((int)closingSegments.size() > size && !orientedEndpoints.empty()) {
Toshihiro Shimizu 890ddd
		size = closingSegments.size();
Toshihiro Shimizu 890ddd
		do
Toshihiro Shimizu 890ddd
			calculateWeightAndDirection(orientedEndpoints);
Toshihiro Shimizu 890ddd
		while (spotResearchTwoPoints(orientedEndpoints, closingSegments));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		do
Toshihiro Shimizu 890ddd
			calculateWeightAndDirection(orientedEndpoints);
Toshihiro Shimizu 890ddd
		while (spotResearchOnePoint(orientedEndpoints, closingSegments));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
bool allMarked(const std::vector<bool> &marks, int index)</bool>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = index + 1; i < (int)marks.size(); i++)
Toshihiro Shimizu 890ddd
		if (!marks[i])
Toshihiro Shimizu 890ddd
			return false;
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
bool TAutocloser::Imp::spotResearchTwoPoints(std::vector<segment> &endpoints, std::vector<segment> &closingSegments)</segment></segment>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, distance, current = 0, closerIndex;
Toshihiro Shimizu 890ddd
	int sqrDistance = m_closingDistance * m_closingDistance;
Toshihiro Shimizu 890ddd
	bool found = 0;
Shinya Kitaoka 3bfa54
	std::vector<bool> marks(endpoints.size());</bool>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (current < (int)endpoints.size() - 1) {
Toshihiro Shimizu 890ddd
		found = 0;
Toshihiro Shimizu 890ddd
		for (i = current + 1; i < (int)marks.size(); i++)
Toshihiro Shimizu 890ddd
			marks[i] = false;
Toshihiro Shimizu 890ddd
		distance = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		while (!found && (distance <= sqrDistance) && !allMarked(marks, current)) {
Toshihiro Shimizu 890ddd
			closerIndex = closerPoint(endpoints, marks, current);
Toshihiro Shimizu 890ddd
			if (exploreTwoSpots(endpoints[current], endpoints[closerIndex]) &&
Toshihiro Shimizu 890ddd
				notInsidePath(endpoints[current].first, endpoints[closerIndex].first)) {
Toshihiro Shimizu 890ddd
				drawInByteRaster(endpoints[current].first, endpoints[closerIndex].first);
Toshihiro Shimizu 890ddd
				closingSegments.push_back(Segment(endpoints[current].first, endpoints[closerIndex].first));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (!EndpointTable[neighboursCode(getPtr(endpoints[closerIndex].first))]) {
Toshihiro Shimizu 890ddd
					std::vector<segment>::iterator it = endpoints.begin();</segment>
Toshihiro Shimizu 890ddd
					std::advance(it, closerIndex);
Toshihiro Shimizu 890ddd
					endpoints.erase(it);
Toshihiro Shimizu 890ddd
					std::vector<bool>::iterator it1 = marks.begin();</bool>
Toshihiro Shimizu 890ddd
					std::advance(it1, closerIndex);
Toshihiro Shimizu 890ddd
					marks.erase(it1);
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				found = true;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (found) {
Toshihiro Shimizu 890ddd
			std::vector<segment>::iterator it = endpoints.begin();</segment>
Toshihiro Shimizu 890ddd
			std::advance(it, current);
Toshihiro Shimizu 890ddd
			endpoints.erase(it);
Toshihiro Shimizu 890ddd
			std::vector<bool>::iterator it1 = marks.begin();</bool>
Toshihiro Shimizu 890ddd
			std::advance(it1, current);
Toshihiro Shimizu 890ddd
			marks.erase(it1);
Toshihiro Shimizu 890ddd
		} else
Toshihiro Shimizu 890ddd
			current++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return found;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
static void clear_marks(POINT *p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
while (p)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  p->mark = 0;
Toshihiro Shimizu 890ddd
  p = p->next;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
static int there_are_unmarked(POINT *p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
while (p)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  if (!p->mark) return 1;
Toshihiro Shimizu 890ddd
  p = p->next;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::Imp::calculateWeightAndDirection(std::vector<segment> &orientedEndpoints)</segment>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//UCHAR *br;
Toshihiro Shimizu 890ddd
	int lx = m_raster->getLx();
Toshihiro Shimizu 890ddd
	int ly = m_raster->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	std::vector<segment>::iterator it = orientedEndpoints.begin();</segment>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (it != orientedEndpoints.end()) {
Toshihiro Shimizu 890ddd
		TPoint p0 = it->first;
Toshihiro Shimizu 890ddd
		TPoint &p1 = it->second;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		//br = (UCHAR *)m_bRaster->pixels(p0.y)+p0.x;
Toshihiro Shimizu 890ddd
		//code = neighboursCode(br);
Toshihiro Shimizu 890ddd
		/*if (!EndpointTable[code])
Toshihiro Shimizu 890ddd
	  {
Toshihiro Shimizu 890ddd
	  it = orientedEndpoints.erase(it);
Toshihiro Shimizu 890ddd
    continue;
Toshihiro Shimizu 890ddd
    }*/
Toshihiro Shimizu 890ddd
		TPoint displAverage = visitEndpoint(getPtr(p0));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		p1 = p0 - displAverage;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		/*if ((point->x2<0 && point->y2<0) || (point->x2>Lx && point->y2>Ly)) printf("che palle!!!!!!\n");*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (p1.x < 0) {
Toshihiro Shimizu 890ddd
			p1.y = tround(p0.y - (float)((p0.y - p1.y) * p0.x) / (p0.x - p1.x));
Toshihiro Shimizu 890ddd
			p1.x = 0;
Toshihiro Shimizu 890ddd
		} else if (p1.x > lx) {
Toshihiro Shimizu 890ddd
			p1.y = tround(p0.y - (float)((p0.y - p1.y) * (p0.x - lx)) / (p0.x - p1.x));
Toshihiro Shimizu 890ddd
			p1.x = lx;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (p1.y < 0) {
Toshihiro Shimizu 890ddd
			p1.x = tround(p0.x - (float)((p0.x - p1.x) * p0.y) / (p0.y - p1.y));
Toshihiro Shimizu 890ddd
			p1.y = 0;
Toshihiro Shimizu 890ddd
		} else if (p1.y > ly) {
Toshihiro Shimizu 890ddd
			p1.x = tround(p0.x - (float)((p0.x - p1.x) * (p0.y - ly)) / (p0.y - p1.y));
Toshihiro Shimizu 890ddd
			p1.y = ly;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		it++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
bool TAutocloser::Imp::spotResearchOnePoint(std::vector<segment> &endpoints, std::vector<segment> &closingSegments)</segment></segment>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int count = 0;
Toshihiro Shimizu 890ddd
	bool ret = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (count < (int)endpoints.size()) {
Toshihiro Shimizu 890ddd
		TPoint p;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (exploreSpot(endpoints[count], p)) {
Toshihiro Shimizu 890ddd
			Segment segment(endpoints[count].first, p);
Shinya Kitaoka 3bfa54
			std::vector<segment>::iterator it = std::find(closingSegments.begin(), closingSegments.end(), segment);</segment>
Toshihiro Shimizu 890ddd
			if (it == closingSegments.end()) {
Toshihiro Shimizu 890ddd
				ret = true;
Toshihiro Shimizu 890ddd
				drawInByteRaster(endpoints[count].first, p);
Toshihiro Shimizu 890ddd
				closingSegments.push_back(Segment(endpoints[count].first, p));
Toshihiro Shimizu 890ddd
				cancelFromArray(endpoints, p, count);
Toshihiro Shimizu 890ddd
				if (!EndpointTable[neighboursCode(getPtr(endpoints[count].first))]) {
Toshihiro Shimizu 890ddd
					std::vector<segment>::iterator it = endpoints.begin();</segment>
Toshihiro Shimizu 890ddd
					std::advance(it, count);
Toshihiro Shimizu 890ddd
					endpoints.erase(it);
Toshihiro Shimizu 890ddd
					continue;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		count++;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TAutocloser::Imp::exploreSpot(const Segment &s, TPoint &p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x1, y1, x2, y2, x3, y3, i;
Toshihiro Shimizu 890ddd
	double x2a, y2a, x2b, y2b, xnewa, ynewa, xnewb, ynewb;
Toshihiro Shimizu 890ddd
	int lx = m_raster->getLx();
Toshihiro Shimizu 890ddd
	int ly = m_raster->getLy();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x1 = s.first.x;
Toshihiro Shimizu 890ddd
	y1 = s.first.y;
Toshihiro Shimizu 890ddd
	x2 = s.second.x;
Toshihiro Shimizu 890ddd
	y2 = s.second.y;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (x1 == x2 && y1 == y2)
Toshihiro Shimizu 890ddd
		return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (exploreRay(getPtr(x1, y1), s, p))
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x2a = x2b = (double)x2;
Toshihiro Shimizu 890ddd
	y2a = y2b = (double)y2;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < AUT_SPOT_SAMPLES; i++) {
Toshihiro Shimizu 890ddd
		xnewa = x1 + (x2a - x1) * m_csa - (y2a - y1) * m_sna;
Toshihiro Shimizu 890ddd
		ynewa = y1 + (y2a - y1) * m_csa + (x2a - x1) * m_sna;
Toshihiro Shimizu 890ddd
		x3 = tround(xnewa);
Toshihiro Shimizu 890ddd
		y3 = tround(ynewa);
Toshihiro Shimizu 890ddd
		if ((x3 != tround(x2a) || y3 != tround(y2a)) && x3 > 0 && x3 < lx && y3 > 0 && y3 < ly &&
Toshihiro Shimizu 890ddd
			exploreRay(getPtr(x1, y1), Segment(TPoint(x1, y1), TPoint(tround(xnewa), tround(ynewa))), p))
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		x2a = xnewa;
Toshihiro Shimizu 890ddd
		y2a = ynewa;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		xnewb = x1 + (x2b - x1) * m_csb - (y2b - y1) * m_snb;
Toshihiro Shimizu 890ddd
		ynewb = y1 + (y2b - y1) * m_csb + (x2b - x1) * m_snb;
Toshihiro Shimizu 890ddd
		x3 = tround(xnewb);
Toshihiro Shimizu 890ddd
		y3 = tround(ynewb);
Toshihiro Shimizu 890ddd
		if ((x3 != tround(x2b) || y3 != tround(y2b)) && x3 > 0 && x3 < lx && y3 > 0 && y3 < ly &&
Toshihiro Shimizu 890ddd
			exploreRay(getPtr(x1, y1), Segment(TPoint(x1, y1), TPoint(tround(xnewb), tround(ynewb))), p))
Toshihiro Shimizu 890ddd
			return true;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		x2b = xnewb;
Toshihiro Shimizu 890ddd
		y2b = ynewb;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TAutocloser::Imp::exploreRay(UCHAR *br, Segment s, TPoint &p)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x, y, dx, dy, d, incr_1, incr_2, inside_ink;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	inside_ink = 1;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x = 0;
Toshihiro Shimizu 890ddd
	y = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (s.first.x < s.second.x) {
Toshihiro Shimizu 890ddd
		dx = s.second.x - s.first.x;
Toshihiro Shimizu 890ddd
		dy = s.second.y - s.first.y;
Toshihiro Shimizu 890ddd
		if (dy >= 0)
Toshihiro Shimizu 890ddd
			if (dy <= dx)
Toshihiro Shimizu 890ddd
				DRAW_SEGMENT(x, y, dx, dy, (br++), (br += m_bWrap + 1),
Toshihiro Shimizu 890ddd
							 EXPLORE_RAY_ISTR((s.first.x + x)))
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				DRAW_SEGMENT(y, x, dy, dx, (br += m_bWrap), (br += m_bWrap + 1),
Toshihiro Shimizu 890ddd
							 EXPLORE_RAY_ISTR((s.first.x + x)))
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			dy = -dy;
Toshihiro Shimizu 890ddd
			if (dy <= dx)
Toshihiro Shimizu 890ddd
				DRAW_SEGMENT(x, y, dx, dy, (br++), (br -= m_bWrap - 1),
Toshihiro Shimizu 890ddd
							 EXPLORE_RAY_ISTR((s.first.x + x)))
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				DRAW_SEGMENT(y, x, dy, dx, (br -= m_bWrap), (br -= m_bWrap - 1),
Toshihiro Shimizu 890ddd
							 EXPLORE_RAY_ISTR((s.first.x + x)))
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		dx = s.first.x - s.second.x;
Toshihiro Shimizu 890ddd
		dy = s.second.y - s.first.y;
Toshihiro Shimizu 890ddd
		if (dy >= 0)
Toshihiro Shimizu 890ddd
			if (dy <= dx)
Toshihiro Shimizu 890ddd
				DRAW_SEGMENT(x, y, dx, dy, (br--), (br += m_bWrap - 1),
Toshihiro Shimizu 890ddd
							 EXPLORE_RAY_ISTR((s.first.x - x)))
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				DRAW_SEGMENT(y, x, dy, dx, (br += m_bWrap), (br += m_bWrap - 1),
Toshihiro Shimizu 890ddd
							 EXPLORE_RAY_ISTR((s.first.x - x)))
Toshihiro Shimizu 890ddd
		else {
Toshihiro Shimizu 890ddd
			dy = -dy;
Toshihiro Shimizu 890ddd
			if (dy <= dx)
Toshihiro Shimizu 890ddd
				DRAW_SEGMENT(x, y, dx, dy, (br--), (br -= m_bWrap + 1),
Toshihiro Shimizu 890ddd
							 EXPLORE_RAY_ISTR((s.first.x - x)))
Toshihiro Shimizu 890ddd
			else
Toshihiro Shimizu 890ddd
				DRAW_SEGMENT(y, x, dy, dx, (br -= m_bWrap), (br -= m_bWrap + 1),
Toshihiro Shimizu 890ddd
							 EXPLORE_RAY_ISTR((s.first.x - x)))
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TAutocloser::Imp::drawInByteRaster(const TPoint &p0, const TPoint &p1)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int x, y, dx, dy, d, incr_1, incr_2;
Toshihiro Shimizu 890ddd
	UCHAR *br;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (p0.x > p1.x) {
Toshihiro Shimizu 890ddd
		br = getPtr(p1);
Toshihiro Shimizu 890ddd
		dx = p0.x - p1.x;
Toshihiro Shimizu 890ddd
		dy = p0.y - p1.y;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		br = getPtr(p0);
Toshihiro Shimizu 890ddd
		dx = p1.x - p0.x;
Toshihiro Shimizu 890ddd
		dy = p1.y - p0.y;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	x = y = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (dy >= 0) {
Toshihiro Shimizu 890ddd
		if (dy <= dx)
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(x, y, dx, dy, (br++), (br += m_bWrap + 1), ((*br) |= 0x41))
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(y, x, dy, dx, (br += m_bWrap), (br += m_bWrap + 1), ((*br) |= 0x41))
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		dy = -dy;
Toshihiro Shimizu 890ddd
		if (dy <= dx)
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(x, y, dx, dy, (br++), (br -= m_bWrap - 1), ((*br) |= 0x41))
Toshihiro Shimizu 890ddd
		else
Toshihiro Shimizu 890ddd
			DRAW_SEGMENT(y, x, dy, dx, (br -= m_bWrap), (br -= m_bWrap - 1), ((*br) |= 0x41))
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TPoint TAutocloser::Imp::visitEndpoint(UCHAR *br)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_displAverage = TPointD();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_visited = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	visitPix(br, m_closingDistance, TPoint());
Toshihiro Shimizu 890ddd
	cancelMarks(br);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TPoint(convert((1.0 / m_visited) * m_displAverage));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TAutocloser::Imp::visitPix(UCHAR *br, int toVisit, const TPoint &dis)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR b = 0;
Toshihiro Shimizu 890ddd
	int i, pixToVisit = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*br |= 0x10;
Toshihiro Shimizu 890ddd
	m_visited++;
Toshihiro Shimizu 890ddd
	m_displAverage.x += dis.x;
Toshihiro Shimizu 890ddd
	m_displAverage.y += dis.y;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	toVisit--;
Toshihiro Shimizu 890ddd
	if (toVisit == 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < 8; i++) {
Toshihiro Shimizu 890ddd
		UCHAR *v = br + m_displaceVector[i];
Toshihiro Shimizu 890ddd
		if (isInk(v) && !((*v) & 0x10)) {
Toshihiro Shimizu 890ddd
			b |= (1 << i);
Toshihiro Shimizu 890ddd
			pixToVisit++;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (pixToVisit == 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (pixToVisit <= 4)
Toshihiro Shimizu 890ddd
		toVisit = troundp(toVisit / (double)pixToVisit);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (toVisit == 0)
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	int x[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
Toshihiro Shimizu 890ddd
	int y[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < 8; i++)
Toshihiro Shimizu 890ddd
		if (b & (1 << i))
Toshihiro Shimizu 890ddd
			visitPix(br + m_displaceVector[i], toVisit, dis + TPoint(x[i], y[i]));
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TAutocloser::Imp::cancelMarks(UCHAR *br)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	*br &= 0xef;
Toshihiro Shimizu 890ddd
	int i;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < 8; i++) {
Toshihiro Shimizu 890ddd
		UCHAR *v = br + m_displaceVector[i];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (isInk(v) && (*v) & 0x10)
Toshihiro Shimizu 890ddd
			cancelMarks(v);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::Imp::skeletonize(std::vector<tpoint> &endpoints)</tpoint>
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<seed> seeds;</seed>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	findSeeds(seeds, endpoints);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	erase(seeds, endpoints);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::Imp::findSeeds(std::vector<seed> &seeds, std::vector<tpoint> &endpoints)</tpoint></seed>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, j;
Toshihiro Shimizu 890ddd
	UCHAR preseed;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	UCHAR *br = m_br;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (i = 0; i < m_bRaster->getLy(); i++) {
Toshihiro Shimizu 890ddd
		for (j = 0; j < m_bRaster->getLx(); j++, br++) {
Toshihiro Shimizu 890ddd
			if (notMarkedBorderInk(br)) {
Toshihiro Shimizu 890ddd
				preseed = FirstPreseedTable[neighboursCode(br)];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				if (preseed != 8) /*non e' un pixel isolato*/
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					seeds.push_back(Seed(br, preseed));
Toshihiro Shimizu 890ddd
					circuitAndMark(br, preseed);
Toshihiro Shimizu 890ddd
				} else {
Toshihiro Shimizu 890ddd
					(*br) |= 0x8;
Toshihiro Shimizu 890ddd
					endpoints.push_back(getCoordinates(br));
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		br += m_bWrap - m_bRaster->getLx();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TAutocloser::Imp::circuitAndMark(UCHAR *seed, UCHAR preseed)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *walker;
Toshihiro Shimizu 890ddd
	UCHAR displ, prewalker;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	*seed |= 0x4;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	displ = NextPointTable[(neighboursCode(seed) << 3) | preseed];
Toshihiro Shimizu 890ddd
	//assert(displ>=0 && displ<8);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	walker = seed + m_displaceVector[displ];
Toshihiro Shimizu 890ddd
	prewalker = displ ^ 0x7;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while ((walker != seed) || (preseed != prewalker)) {
Toshihiro Shimizu 890ddd
		*walker |= 0x4; /* metto la marca di passaggio */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		displ = NextPointTable[(neighboursCode(walker) << 3) | prewalker];
Toshihiro Shimizu 890ddd
		//  assert(displ>=0 && displ<8);
Toshihiro Shimizu 890ddd
		walker += m_displaceVector[displ];
Toshihiro Shimizu 890ddd
		prewalker = displ ^ 0x7;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::Imp::erase(std::vector<seed> &seeds, std::vector<tpoint> &endpoints)</tpoint></seed>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i, size = 0, oldSize;
Toshihiro Shimizu 890ddd
	UCHAR *seed, preseed, code, displ;
Toshihiro Shimizu 890ddd
	oldSize = seeds.size();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while (oldSize != size) {
Toshihiro Shimizu 890ddd
		oldSize = size;
Toshihiro Shimizu 890ddd
		size = seeds.size();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		for (i = oldSize; i < size; i++) {
Toshihiro Shimizu 890ddd
			seed = seeds[i].m_ptr;
Toshihiro Shimizu 890ddd
			preseed = seeds[i].m_preseed;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (!isInk(seed)) {
Toshihiro Shimizu 890ddd
				code = NextSeedTable[neighboursCode(seed)];
Toshihiro Shimizu 890ddd
				seed += m_displaceVector[code & 0x7];
Toshihiro Shimizu 890ddd
				preseed = (code & 0x38) >> 3;
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
			if (circuitAndCancel(seed, preseed, endpoints)) {
Toshihiro Shimizu 890ddd
				if (isInk(seed)) {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
					displ = NextPointTable[(neighboursCode(seed) << 3) | preseed];
Toshihiro Shimizu 890ddd
					//				assert(displ>=0 && displ<8);
Toshihiro Shimizu 890ddd
					seeds.push_back(Seed(seed + m_displaceVector[displ], displ ^ 0x7));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
				} else /* il seed e' stato cancellato */
Toshihiro Shimizu 890ddd
				{
Toshihiro Shimizu 890ddd
					code = NextSeedTable[neighboursCode(seed)];
Toshihiro Shimizu 890ddd
					seeds.push_back(Seed(seed + m_displaceVector[code & 0x7], (code & 0x38) >> 3));
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
bool TAutocloser::Imp::circuitAndCancel(UCHAR *seed, UCHAR preseed, std::vector<tpoint> &endpoints)</tpoint>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	UCHAR *walker, *previous;
Toshihiro Shimizu 890ddd
	UCHAR displ, prewalker;
Toshihiro Shimizu 890ddd
	bool ret = false;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	displ = NextPointTable[(neighboursCode(seed) << 3) | preseed];
Toshihiro Shimizu 890ddd
	//assert(displ>=0 && displ<8);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if ((displ == preseed) && !((*seed) & 0x8)) {
Toshihiro Shimizu 890ddd
		endpoints.push_back(getCoordinates(seed));
Toshihiro Shimizu 890ddd
		*seed |= 0x8;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	walker = seed + m_displaceVector[displ];
Toshihiro Shimizu 890ddd
	prewalker = displ ^ 0x7;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	while ((walker != seed) || (preseed != prewalker)) {
Toshihiro Shimizu 890ddd
		//	assert(prewalker>=0 && prewalker<8);
Toshihiro Shimizu 890ddd
		displ = NextPointTable[(neighboursCode(walker) << 3) | prewalker];
Toshihiro Shimizu 890ddd
		//  assert(displ>=0 && displ<8);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if ((displ == prewalker) && !((*walker) & 0x8)) {
Toshihiro Shimizu 890ddd
			endpoints.push_back(getCoordinates(walker));
Toshihiro Shimizu 890ddd
			*walker |= 0x8;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		previous = walker + m_displaceVector[prewalker];
Toshihiro Shimizu 890ddd
		if (ConnectionTable[neighboursCode(previous)]) {
Toshihiro Shimizu 890ddd
			ret = true;
Toshihiro Shimizu 890ddd
			if (previous != seed)
Toshihiro Shimizu 890ddd
				eraseInk(previous);
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		walker += m_displaceVector[displ];
Toshihiro Shimizu 890ddd
		prewalker = displ ^ 0x7;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	displ = NextPointTable[(neighboursCode(walker) << 3) | prewalker];
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if ((displ == preseed) && !((*seed) & 0x8)) {
Toshihiro Shimizu 890ddd
		endpoints.push_back(getCoordinates(seed));
Toshihiro Shimizu 890ddd
		*seed |= 0x8;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (ConnectionTable[neighboursCode(seed + m_displaceVector[preseed])]) {
Toshihiro Shimizu 890ddd
		ret = true;
Toshihiro Shimizu 890ddd
		eraseInk(seed + m_displaceVector[preseed]);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (ConnectionTable[neighboursCode(seed)]) {
Toshihiro Shimizu 890ddd
		ret = true;
Toshihiro Shimizu 890ddd
		eraseInk(seed);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return ret;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::Imp::cancelFromArray(std::vector<segment> &array, TPoint p, int &count)</segment>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	std::vector<segment>::iterator it = array.begin();</segment>
Toshihiro Shimizu 890ddd
	int i = 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	for (; it != array.end(); ++it, i++)
Toshihiro Shimizu 890ddd
		if (it->first == p) {
Toshihiro Shimizu 890ddd
			if (!EndpointTable[neighboursCode(getPtr(p))]) {
Toshihiro Shimizu 890ddd
				assert(i != count);
Toshihiro Shimizu 890ddd
				if (i < count)
Toshihiro Shimizu 890ddd
					count--;
Toshihiro Shimizu 890ddd
				array.erase(it);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
			return;
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
int is_in_list(LIST list, UCHAR *br)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
POINT *aux;
Toshihiro Shimizu 890ddd
aux = list.head;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
while(aux)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  if (aux->p == br) return 1;
Toshihiro Shimizu 890ddd
  aux = aux->next;
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
return 0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*=============================================================================*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TAutocloser::TAutocloser(const TRasterP &r, int distance, double angle, int index, int opacity)
Shinya Kitaoka 262a92
	: m_imp(new Imp(r, distance, angle, index, opacity))
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//...............................
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TAutocloser::exec()
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 3bfa54
	std::vector<tautocloser::segment> segments;</tautocloser::segment>
Toshihiro Shimizu 890ddd
	compute(segments);
Toshihiro Shimizu 890ddd
	draw(segments);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//...............................
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TAutocloser::~TAutocloser()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::compute(std::vector<segment> &closingSegmentArray)</segment>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_imp->compute(closingSegmentArray);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 3bfa54
void TAutocloser::draw(const std::vector<segment> &closingSegmentArray)</segment>
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_imp->draw(closingSegmentArray);
Toshihiro Shimizu 890ddd
}