Shinya Kitaoka 810553
#pragma once
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#ifndef T_SEGMENTADJUSTER_H
Toshihiro Shimizu 890ddd
#define T_SEGMENTADJUSTER_H
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tgeometry.h"
Toshihiro Shimizu 890ddd
#include "tstroke.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#undef DVAPI
Toshihiro Shimizu 890ddd
#undef DVVAR
Toshihiro Shimizu 890ddd
#ifdef TVECTORIMAGE_EXPORTS
Toshihiro Shimizu 890ddd
#define DVAPI DV_EXPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_EXPORT_VAR
Toshihiro Shimizu 890ddd
#else
Toshihiro Shimizu 890ddd
#define DVAPI DV_IMPORT_API
Toshihiro Shimizu 890ddd
#define DVVAR DV_IMPORT_VAR
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Shinya Kitaoka 9f5a1b
#ifdef _WIN32
Toshihiro Shimizu 890ddd
#pragma warning(disable : 4251)
Toshihiro Shimizu 890ddd
#endif
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*!
Toshihiro Shimizu 890ddd
  The TSegmentAdjuster minimizes the distance between
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
     stroke0->getPoint(w0) and stroke1->getPoint(w1)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  changing the values of w0 and w1 (in a defined range).
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  Usage:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  TSegmentAdjuster::End p0(stroke0, w0, 0, 1);
Toshihiro Shimizu 890ddd
  TSegmentAdjuster::End p1(stroke1, w1, 0, 1);
Toshihiro Shimizu 890ddd
  TSegmentAdjuster adjuster;
Toshihiro Shimizu 890ddd
  adjuster.compute(p0,p1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  // the adjusted segment is p0.getPoint(), p1.getPoint()
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  if you want to adjust the segment only in the w-growing-direction:
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  TSegmentAdjuster::End p0(stroke0, w0, w0, 1);
Toshihiro Shimizu 890ddd
  TSegmentAdjuster::End p1(stroke1, w1, w1, 1);
Toshihiro Shimizu 890ddd
  TSegmentAdjuster adjuster;
Toshihiro Shimizu 890ddd
  adjuster.compute(p0,p1);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
 */
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class DVAPI TSegmentAdjuster
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	class End
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	public:
Toshihiro Shimizu 890ddd
		TStroke *m_stroke;
Toshihiro Shimizu 890ddd
		double m_w;
Toshihiro Shimizu 890ddd
		double m_wmin, m_wmax;
Toshihiro Shimizu 890ddd
		End() : m_stroke(0), m_w(0), m_wmin(0), m_wmax(1) {}
Toshihiro Shimizu 890ddd
		End(TStroke *stroke, double w) : m_stroke(stroke), m_w(w), m_wmin(0), m_wmax(1) {}
Toshihiro Shimizu 890ddd
		End(TStroke *stroke, double w, double wmin, double wmax)
Toshihiro Shimizu 890ddd
			: m_stroke(stroke), m_w(w), m_wmin(wmin), m_wmax(wmax) {}
Toshihiro Shimizu 890ddd
		TPointD getPoint() const { return m_stroke->getPoint(m_w); }
Toshihiro Shimizu 890ddd
		TPointD getSpeed() const { return m_stroke->getSpeed(m_w); }
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TSegmentAdjuster();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	// debug
Toshihiro Shimizu 890ddd
	void enableTrace(bool enabled) { m_traceEnabled = enabled; }
Toshihiro Shimizu 890ddd
	void draw();
Toshihiro Shimizu 890ddd
	void clear() { m_links.clear(); }
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void compute(End &a, End &b);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	End m_a, m_b, m_c, m_d;
Toshihiro Shimizu 890ddd
	std::vector<std::pair<tpointd, tpointd="">> m_links;</std::pair<tpointd,>
Toshihiro Shimizu 890ddd
	bool m_traceEnabled;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	inline double fun(double u, double v) const
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		return norm2(m_a.m_stroke->getPoint(u) - m_b.m_stroke->getPoint(v));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	inline void gradient(double &dfdu, double &dfdv, double u, double v)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		const double h = 0.0001;
Toshihiro Shimizu 890ddd
		dfdu = (fun(u + h, v) - fun(u - h, v)) / (2 * h);
Toshihiro Shimizu 890ddd
		dfdv = (fun(u, v + h) - fun(u, v - h)) / (2 * h);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	inline static double dsda(TStroke *stroke, double w, double dwda)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
		const double h = 0.0001;
Toshihiro Shimizu 890ddd
		return norm(stroke->getPoint(w + dwda * h) - stroke->getPoint(w - dwda * h)) / (2 * h);
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#endif // T_SEGMENTADJUSTER_H