|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#ifndef TCG_POLYLINE_OPS_HPP
|
|
Toshihiro Shimizu |
890ddd |
#define TCG_POLYLINE_OPS_HPP
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// tcg includes
|
|
Toshihiro Shimizu |
890ddd |
#include "../polyline_ops.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "../iterator_ops.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "../sequence_ops.h"
|
|
Toshihiro Shimizu |
890ddd |
#include "../point_ops.h"
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
// STD includes
|
|
Toshihiro Shimizu |
890ddd |
#include <assert.h></assert.h>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
namespace tcg
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
namespace polyline_ops
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
using tcg::point_ops::operator/;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//***********************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// Standard Deviation Evaluator
|
|
Toshihiro Shimizu |
890ddd |
//***********************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename ranit=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
StandardDeviationEvaluator<ranit>::StandardDeviationEvaluator(const RanIt &begin, const RanIt &end)</ranit>
|
|
Toshihiro Shimizu |
890ddd |
: m_begin(begin), m_end(end)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
//Let m_sum[i] and m_sum2[i] be respectively the sums of vertex coordinates
|
|
Toshihiro Shimizu |
890ddd |
//(relative to begin is sufficient) from 0 to i, and the sums of their squares;
|
|
Toshihiro Shimizu |
890ddd |
//m_sumsMix contain sums of xy terms.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
diff_type i, n = m_end - m_begin;
|
|
Toshihiro Shimizu |
890ddd |
diff_type n2 = n * 2;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_sums_x.resize(n);
|
|
Toshihiro Shimizu |
890ddd |
m_sums_y.resize(n);
|
|
Toshihiro Shimizu |
890ddd |
m_sums2_x.resize(n);
|
|
Toshihiro Shimizu |
890ddd |
m_sums2_y.resize(n);
|
|
Toshihiro Shimizu |
890ddd |
m_sums_xy.resize(n);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_sums_x[0] = m_sums_y[0] = m_sums2_x[0] = m_sums2_y[0] = m_sums_xy[0] = 0.0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Build sums following the path
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
point_type posToBegin;
|
|
Toshihiro Shimizu |
890ddd |
i = 0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
iterator_type a = m_begin;
|
|
Toshihiro Shimizu |
890ddd |
for (a = m_begin, ++a; a != m_end; ++a, ++i) {
|
|
Toshihiro Shimizu |
890ddd |
posToBegin = point_type(a->x - m_begin->x, a->y - m_begin->y);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_sums_x[i + 1] = m_sums_x[i] + posToBegin.x;
|
|
Toshihiro Shimizu |
890ddd |
m_sums_y[i + 1] = m_sums_y[i] + posToBegin.y;
|
|
Toshihiro Shimizu |
890ddd |
m_sums2_x[i + 1] = m_sums2_x[i] + sq(posToBegin.x);
|
|
Toshihiro Shimizu |
890ddd |
m_sums2_y[i + 1] = m_sums2_y[i] + sq(posToBegin.y);
|
|
Toshihiro Shimizu |
890ddd |
m_sums_xy[i + 1] = m_sums_xy[i] + posToBegin.x * posToBegin.y;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//------------------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename ranit=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
typename StandardDeviationEvaluator<ranit>::penalty_type</ranit>
|
|
Toshihiro Shimizu |
890ddd |
StandardDeviationEvaluator<ranit>::penalty(const iterator_type &a, const iterator_type &b)</ranit>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
diff_type aIdx = a - m_begin, bIdx = b - m_begin;
|
|
Toshihiro Shimizu |
890ddd |
point_type v(b->x - a->x, b->y - a->y), a_(a->x - m_begin->x, a->y - m_begin->y);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double n = b - a; //Needs to be of higher precision than diff_type
|
|
Toshihiro Shimizu |
890ddd |
double sumX = m_sums_x[bIdx] - m_sums_x[aIdx];
|
|
Toshihiro Shimizu |
890ddd |
double sumY = m_sums_y[bIdx] - m_sums_y[aIdx];
|
|
Toshihiro Shimizu |
890ddd |
double sum2X = m_sums2_x[bIdx] - m_sums2_x[aIdx];
|
|
Toshihiro Shimizu |
890ddd |
double sum2Y = m_sums2_y[bIdx] - m_sums2_y[aIdx];
|
|
Toshihiro Shimizu |
890ddd |
double sumMix = m_sums_xy[bIdx] - m_sums_xy[aIdx];
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (bIdx < aIdx) {
|
|
Toshihiro Shimizu |
890ddd |
int count = m_end - m_begin, count_1 = count - 1;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
n += count;
|
|
Toshihiro Shimizu |
890ddd |
sumX += m_sums_x[count_1];
|
|
Toshihiro Shimizu |
890ddd |
sumY += m_sums_y[count_1];
|
|
Toshihiro Shimizu |
890ddd |
sum2X += m_sums2_x[count_1];
|
|
Toshihiro Shimizu |
890ddd |
sum2Y += m_sums2_y[count_1];
|
|
Toshihiro Shimizu |
890ddd |
sumMix += m_sums_xy[count_1];
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
double A = sum2Y - 2.0 * sumY * a_.y + n * sq(a_.y);
|
|
Toshihiro Shimizu |
890ddd |
double B = sum2X - 2.0 * sumX * a_.x + n * sq(a_.x);
|
|
Toshihiro Shimizu |
890ddd |
double C = sumMix - sumX * a_.y - sumY * a_.x + n * a_.x * a_.y;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return sqrt((v.x * v.x * A + v.y * v.y * B - 2 * v.x * v.y * C) / n);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//***********************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// Quadratics approximation Evaluator
|
|
Toshihiro Shimizu |
890ddd |
//***********************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename point=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
class _QuadraticsEdgeEvaluator
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
typedef Point point_type;
|
|
Toshihiro Shimizu |
890ddd |
typedef typename tcg::point_traits<point_type>::value_type value_type;</point_type>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::vector<point>::iterator cp_iterator;</point>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename tcg::step_iterator<cp_iterator> quad_iterator;</cp_iterator>
|
|
Toshihiro Shimizu |
890ddd |
typedef double penalty_type;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
private:
|
|
Toshihiro Shimizu |
890ddd |
quad_iterator m_begin, m_end;
|
|
Toshihiro Shimizu |
890ddd |
penalty_type m_tol;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
_QuadraticsEdgeEvaluator(const quad_iterator &begin, const quad_iterator &end,
|
|
Toshihiro Shimizu |
890ddd |
penalty_type tol);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
quad_iterator furthestFrom(const quad_iterator &a);
|
|
Toshihiro Shimizu |
890ddd |
penalty_type penalty(const quad_iterator &a, const quad_iterator &b);
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename point=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
_QuadraticsEdgeEvaluator<point>::_QuadraticsEdgeEvaluator(</point>
|
|
Toshihiro Shimizu |
890ddd |
const quad_iterator &begin, const quad_iterator &end, penalty_type tol)
|
|
Toshihiro Shimizu |
890ddd |
: m_begin(begin), m_end(end), m_tol(tol)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename point=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
typename _QuadraticsEdgeEvaluator<point>::quad_iterator</point>
|
|
Toshihiro Shimizu |
890ddd |
_QuadraticsEdgeEvaluator<point>::furthestFrom(const quad_iterator &at)</point>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
const point_type &A = *at;
|
|
Toshihiro Shimizu |
890ddd |
const point_type &A1 = *(at.it() + 1);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Build at (opposite) side
|
|
Toshihiro Shimizu |
890ddd |
int atSide_ = -tcg::numeric_ops::sign(cross(A - A1, *(at + 1) - A1));
|
|
Toshihiro Shimizu |
890ddd |
bool atSideNotZero = (atSide_ != 0);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
quad_iterator bt, last = this->m_end - 1; //Don't do the last (it's a dummy quad)
|
|
Toshihiro Shimizu |
890ddd |
for (bt = at + 1; bt != last; ++bt) //Always allow 1 step
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
//Trying to reach (bt + 1) from at
|
|
Toshihiro Shimizu |
890ddd |
const point_type &C = *(bt + 1);
|
|
Toshihiro Shimizu |
890ddd |
const point_type &C1 = *(bt.it() + 1);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Ensure that bt is not a corner
|
|
Toshihiro Shimizu |
890ddd |
if (abs(tcg::point_ops::cross(*(bt.it() - 1) - *bt, *(bt.it() + 1) - *bt)) > 1e-3)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Ensure there is no sign inversion
|
|
Toshihiro Shimizu |
890ddd |
int btSide = tcg::numeric_ops::sign(tcg::point_ops::cross(*bt - C1, C - C1));
|
|
Toshihiro Shimizu |
890ddd |
if (atSideNotZero && btSide != 0 && btSide == atSide_)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Build the approximating new quad if any
|
|
Toshihiro Shimizu |
890ddd |
value_type s, t;
|
|
Toshihiro Shimizu |
890ddd |
tcg::point_ops::intersectionSegCoords(A, A1, C, C1, s, t, 1e-4);
|
|
Toshihiro Shimizu |
890ddd |
if (s == tcg::numeric_ops::NaN<value_type>()) {</value_type>
|
|
Toshihiro Shimizu |
890ddd |
//A-A1 and C1-C are parallel. There are 2 cases:
|
|
Toshihiro Shimizu |
890ddd |
if ((A1 - A) * (C - C1) >= 0)
|
|
Toshihiro Shimizu |
890ddd |
//Either we're still on a straight line
|
|
Toshihiro Shimizu |
890ddd |
continue;
|
|
Toshihiro Shimizu |
890ddd |
else
|
|
Toshihiro Shimizu |
890ddd |
//Or, we just can't build the new quad
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
point_type B(A + s * (A1 - A));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
point_type A_B(A - B);
|
|
Toshihiro Shimizu |
890ddd |
point_type AC_2B(A_B + C - B);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Now, for each quadratic between at and bt, build the 'distance' from our new
|
|
Toshihiro Shimizu |
890ddd |
//approximating quad (ABC)
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
quad_iterator qt, end = bt + 1;
|
|
Toshihiro Shimizu |
890ddd |
for (qt = at; qt != end; ++qt) {
|
|
Toshihiro Shimizu |
890ddd |
const point_type &Q_A(*qt);
|
|
Toshihiro Shimizu |
890ddd |
const point_type &Q_B(*(qt.it() + 1));
|
|
Toshihiro Shimizu |
890ddd |
const point_type &Q_C(*(qt + 1));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Check the distance of Q_B from the ABC tangent whose direction
|
|
Toshihiro Shimizu |
890ddd |
//is the same as Q'_B - ie, Q_A -> Q_C.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
point_type dir(Q_C - Q_A);
|
|
Toshihiro Shimizu |
890ddd |
value_type dirNorm = tcg::point_ops::norm(dir);
|
|
Toshihiro Shimizu |
890ddd |
if (dirNorm < 1e-4)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
dir = dir / dirNorm;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
value_type den = tcg::point_ops::cross(AC_2B, dir);
|
|
Toshihiro Shimizu |
890ddd |
if (den < 1e-4 && den > -1e-4)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
value_type t = tcg::point_ops::cross(A_B, dir) / den;
|
|
Toshihiro Shimizu |
890ddd |
if (t < 0.0 || t > 1.0)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
value_type t1 = 1.0 - t;
|
|
Toshihiro Shimizu |
890ddd |
point_type P(sq(t1) * A + 2.0 * t * t1 * B + sq(t) * C);
|
|
Toshihiro Shimizu |
890ddd |
point_type Q(0.25 * Q_A + 0.5 * Q_B + 0.25 * Q_C);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (tcg::point_ops::lineDist(Q, P, dir) > m_tol)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
value_type pos = ((P - Q_A) * dir);
|
|
Toshihiro Shimizu |
890ddd |
if (pos < 0.0 || pos > dirNorm)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
/*if(pos < -m_tol || pos > dirNorm + m_tol) //Should this be relaxed too?
|
|
Toshihiro Shimizu |
890ddd |
break;*/
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (qt == bt)
|
|
Toshihiro Shimizu |
890ddd |
continue;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Check the distance of Q_C from the ABC tangent whose direction
|
|
Toshihiro Shimizu |
890ddd |
//is the same as Q'_C.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
dir = tcg::point_ops::direction(Q_B, Q_C);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
den = tcg::point_ops::cross(AC_2B, dir);
|
|
Toshihiro Shimizu |
890ddd |
if (den < 1e-4 && den > -1e-4)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
t = tcg::point_ops::cross(A_B, dir) / den;
|
|
Toshihiro Shimizu |
890ddd |
if (t < 0.0 || t > 1.0)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
t1 = 1.0 - t;
|
|
Toshihiro Shimizu |
890ddd |
P = sq(t1) * A + 2.0 * t * t1 * B + sq(t) * C;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (tcg::point_ops::lineDist(Q_C, P, dir) > m_tol)
|
|
Toshihiro Shimizu |
890ddd |
break;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (qt != end)
|
|
Toshihiro Shimizu |
890ddd |
break; //Constraints were violated
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return std::min(bt, this->m_end - 1);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename point=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
typename _QuadraticsEdgeEvaluator<point>::penalty_type</point>
|
|
Toshihiro Shimizu |
890ddd |
_QuadraticsEdgeEvaluator<point>::penalty(const quad_iterator &at, const quad_iterator &bt)</point>
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (bt == at + 1)
|
|
Toshihiro Shimizu |
890ddd |
return 0.0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
penalty_type penalty = 0.0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
const point_type &A(*at);
|
|
Toshihiro Shimizu |
890ddd |
const point_type &A1(*(at.it() + 1));
|
|
Toshihiro Shimizu |
890ddd |
const point_type &C(*bt);
|
|
Toshihiro Shimizu |
890ddd |
const point_type &C1(*(bt.it() - 1));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Build B
|
|
Toshihiro Shimizu |
890ddd |
value_type s, t;
|
|
Toshihiro Shimizu |
890ddd |
tcg::point_ops::intersectionSegCoords(A, A1, C, C1, s, t, 1e-4);
|
|
Toshihiro Shimizu |
890ddd |
if (s == tcg::numeric_ops::NaN<value_type>())</value_type>
|
|
Toshihiro Shimizu |
890ddd |
return 0.0;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
point_type B(A + s * (A1 - A));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Iterate and build penalties
|
|
Toshihiro Shimizu |
890ddd |
point_type A_B(A - B);
|
|
Toshihiro Shimizu |
890ddd |
point_type AC_2B(A_B + C - B);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
quad_iterator qt, bt_1 = bt - 1;
|
|
Toshihiro Shimizu |
890ddd |
for (qt = at; qt != bt; ++qt) {
|
|
Toshihiro Shimizu |
890ddd |
const point_type &Q_A(*qt);
|
|
Toshihiro Shimizu |
890ddd |
const point_type &Q_B(*(qt.it() + 1));
|
|
Toshihiro Shimizu |
890ddd |
const point_type &Q_C(*(qt + 1));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//point_type dir(tcg::point_ops::direction(Q_A, Q_C));
|
|
Toshihiro Shimizu |
890ddd |
point_type dir(Q_C - Q_A);
|
|
Toshihiro Shimizu |
890ddd |
dir = dir / tcg::point_ops::norm(dir);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
value_type t = tcg::point_ops::cross(A_B, dir) / tcg::point_ops::cross(AC_2B, dir);
|
|
Toshihiro Shimizu |
890ddd |
assert(t >= 0.0 && t <= 1.0);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
value_type t1 = 1.0 - t;
|
|
Toshihiro Shimizu |
890ddd |
point_type P(sq(t1) * A + 2.0 * t * t1 * B + sq(t) * C);
|
|
Toshihiro Shimizu |
890ddd |
point_type Q(0.25 * Q_A + 0.5 * Q_B + 0.25 * Q_C);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
penalty += tcg::point_ops::lineDist(Q, P, dir);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (qt == bt_1)
|
|
Toshihiro Shimizu |
890ddd |
continue;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
dir = tcg::point_ops::direction(Q_B, Q_C);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
t = tcg::point_ops::cross(A_B, dir) / tcg::point_ops::cross(AC_2B, dir);
|
|
Toshihiro Shimizu |
890ddd |
assert(t >= 0.0 && t <= 1.0);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
t1 = 1.0 - t;
|
|
Toshihiro Shimizu |
890ddd |
P = sq(t1) * A + 2.0 * t * t1 * B + sq(t) * C;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
penalty += tcg::point_ops::lineDist(Q_C, P, dir);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return penalty;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//***********************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
// Conversion to Quadratics functions
|
|
Toshihiro Shimizu |
890ddd |
//***********************************************************************************
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename cps_reader=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
class _QuadReader
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
typedef typename cps_reader::value_type point_type;
|
|
Toshihiro Shimizu |
890ddd |
typedef typename tcg::point_traits<point_type>::value_type value_type;</point_type>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::vector<point_type>::iterator cps_iterator;</point_type>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename tcg::step_iterator<cps_iterator> quad_iterator;</cps_iterator>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
private:
|
|
Toshihiro Shimizu |
890ddd |
cps_reader &m_reader;
|
|
Toshihiro Shimizu |
890ddd |
quad_iterator m_it;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
public:
|
|
Toshihiro Shimizu |
890ddd |
_QuadReader(cps_reader &reader) : m_reader(reader) {}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void openContainer(const quad_iterator &it)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
m_reader.openContainer(*it);
|
|
Toshihiro Shimizu |
890ddd |
m_it = it;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void addElement(const quad_iterator &it)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
if (it == m_it + 1) {
|
|
Toshihiro Shimizu |
890ddd |
m_reader.addElement(*(it.it() - 1));
|
|
Toshihiro Shimizu |
890ddd |
m_reader.addElement(*it);
|
|
Toshihiro Shimizu |
890ddd |
} else {
|
|
Toshihiro Shimizu |
890ddd |
const point_type &A(*m_it);
|
|
Toshihiro Shimizu |
890ddd |
const point_type &A1(*(m_it.it() + 1));
|
|
Toshihiro Shimizu |
890ddd |
const point_type &C(*it);
|
|
Toshihiro Shimizu |
890ddd |
const point_type &C1(*(it.it() - 1));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Build B
|
|
Toshihiro Shimizu |
890ddd |
value_type s, t;
|
|
Toshihiro Shimizu |
890ddd |
tcg::point_ops::intersectionSegCoords(A, A1, C, C1, s, t, 1e-4);
|
|
Toshihiro Shimizu |
890ddd |
point_type B((s == tcg::numeric_ops::NaN<value_type>()) ? 0.5 * (A + C) : A + s * (A1 - A));</value_type>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_reader.addElement(B);
|
|
Toshihiro Shimizu |
890ddd |
m_reader.addElement(C);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
m_it = it;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
void closeContainer() { m_reader.closeContainer(); }
|
|
Toshihiro Shimizu |
890ddd |
};
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename iter_type,="" reader,="" tripletoquadsfunc="" typename=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
void _naiveQuadraticConversion(const iter_type &begin, const iter_type &end,
|
|
Toshihiro Shimizu |
890ddd |
Reader &reader, tripleToQuadsFunc &tripleToQuadsF)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::iterator_traits<iter_type>::value_type point_type;</iter_type>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
point_type a, c;
|
|
Toshihiro Shimizu |
890ddd |
iter_type it, jt;
|
|
Toshihiro Shimizu |
890ddd |
iter_type last(end);
|
|
Toshihiro Shimizu |
890ddd |
--last;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (*begin != *last) {
|
|
Toshihiro Shimizu |
890ddd |
reader.openContainer();
|
|
Toshihiro Shimizu |
890ddd |
reader.addElement(*begin);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
++(it = begin);
|
|
Toshihiro Shimizu |
890ddd |
a = 0.5 * (*begin + *it);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
reader.addElement(0.5 * (*begin + a));
|
|
Toshihiro Shimizu |
890ddd |
reader.addElement(a);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Work out each quadratic
|
|
Toshihiro Shimizu |
890ddd |
for (++(jt = it); jt != end; it = jt, ++jt) {
|
|
Toshihiro Shimizu |
890ddd |
c = 0.5 * (*it + *jt);
|
|
Toshihiro Shimizu |
890ddd |
tripleToQuadsF(a, it, c, reader);
|
|
Toshihiro Shimizu |
890ddd |
a = c;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
reader.addElement(0.5 * (a + *it));
|
|
Toshihiro Shimizu |
890ddd |
reader.addElement(*it);
|
|
Toshihiro Shimizu |
890ddd |
} else {
|
|
Toshihiro Shimizu |
890ddd |
++(it = begin);
|
|
Toshihiro Shimizu |
890ddd |
point_type first = a = 0.5 * (*begin + *it);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
reader.openContainer();
|
|
Toshihiro Shimizu |
890ddd |
reader.addElement(a);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
for (++(jt = it); jt != end; it = jt, ++jt) {
|
|
Toshihiro Shimizu |
890ddd |
c = 0.5 * (*it + *jt);
|
|
Toshihiro Shimizu |
890ddd |
tripleToQuadsF(a, it, c, reader);
|
|
Toshihiro Shimizu |
890ddd |
a = c;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
tripleToQuadsF(a, last, first, reader);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
reader.closeContainer();
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//---------------------------------------------------------------------------
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
template <typename containers_reader,="" iter_type,="" toquadsfunc="" typename=""></typename>
|
|
Toshihiro Shimizu |
890ddd |
void toQuadratics(iter_type begin, iter_type end, containers_reader &output,
|
|
Toshihiro Shimizu |
890ddd |
toQuadsFunc &toQuadsF, double reductionTol)
|
|
Toshihiro Shimizu |
890ddd |
{
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::iterator_traits<iter_type>::difference_type diff_type;</iter_type>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename std::iterator_traits<iter_type>::value_type point_type;</iter_type>
|
|
Toshihiro Shimizu |
890ddd |
typedef typename tcg::point_traits<point_type>::value_type value_type;</point_type>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (begin == end)
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
diff_type count = std::distance(begin, end);
|
|
Toshihiro Shimizu |
890ddd |
if (count < 2) {
|
|
Toshihiro Shimizu |
890ddd |
//Single point - add 2 points on top of it and quit.
|
|
Toshihiro Shimizu |
890ddd |
output.openContainer(*begin);
|
|
Toshihiro Shimizu |
890ddd |
output.addElement(*begin), output.addElement(*begin);
|
|
Toshihiro Shimizu |
890ddd |
output.closeContainer();
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (count == 2) {
|
|
Toshihiro Shimizu |
890ddd |
//Segment case
|
|
Toshihiro Shimizu |
890ddd |
iter_type it = begin;
|
|
Toshihiro Shimizu |
890ddd |
++it;
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
output.openContainer(*begin);
|
|
Toshihiro Shimizu |
890ddd |
output.addElement(0.5 * (*begin + *it)), output.addElement(*it);
|
|
Toshihiro Shimizu |
890ddd |
output.closeContainer();
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Build an intermediate vector of points containing the naive quadratic
|
|
Toshihiro Shimizu |
890ddd |
//conversion.
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
std::vector<point_type> cps;</point_type>
|
|
Toshihiro Shimizu |
890ddd |
tcg::sequential_reader<std::vector<point_type>> cpsReader(&cps);</std::vector<point_type>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
_naiveQuadraticConversion(begin, end, cpsReader, toQuadsF);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
if (reductionTol <= 0) {
|
|
Toshihiro Shimizu |
890ddd |
output.openContainer(*cps.begin());
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Directly output the naive conversion
|
|
Toshihiro Shimizu |
890ddd |
typename std::vector<point_type>::iterator it, end = cps.end();</point_type>
|
|
Toshihiro Shimizu |
890ddd |
for (it = ++cps.begin(); it != end; ++it)
|
|
Toshihiro Shimizu |
890ddd |
output.addElement(*it);
|
|
Toshihiro Shimizu |
890ddd |
output.closeContainer();
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
return;
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Resize the cps to cover a multiple of 2
|
|
Toshihiro Shimizu |
890ddd |
cps.resize(cps.size() + 2 - (cps.size() % 2));
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
//Now, launch the quadratics reduction procedure
|
|
Toshihiro Shimizu |
890ddd |
tcg::step_iterator<typename std::vector<point_type="">::iterator></typename>
|
|
Toshihiro Shimizu |
890ddd |
bt(cps.begin(), 2), et(cps.end(), 2);
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
_QuadraticsEdgeEvaluator<point_type> eval(bt, et, reductionTol);</point_type>
|
|
Toshihiro Shimizu |
890ddd |
_QuadReader<containers_reader> quadReader(output);</containers_reader>
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
bool ret = tcg::sequence_ops::minimalPath(bt, et, eval, quadReader);
|
|
Toshihiro Shimizu |
890ddd |
assert(ret);
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
}
|
|
Toshihiro Shimizu |
890ddd |
} // namespace tcg::polyline_ops
|
|
Toshihiro Shimizu |
890ddd |
|
|
Toshihiro Shimizu |
890ddd |
#endif // TCG_POLYLINE_OPS_HPP
|