tahoma2d/toonz/sources/include/tcurveutil.h

163 lines
4.5 KiB
C
Raw Normal View History

2016-05-17 03:04:11 +12:00
#pragma once
2016-03-19 06:57:51 +13:00
#ifndef TCURVES_UTIL_INCLUDED
#define TCURVES_UTIL_INCLUDED
//#include "tutil.h"
#include "tgeometry.h"
#undef DVAPI
#undef DVVAR
#ifdef TGEOMETRY_EXPORTS
#define DVAPI DV_EXPORT_API
#define DVVAR DV_EXPORT_VAR
#else
#define DVAPI DV_IMPORT_API
#define DVVAR DV_IMPORT_VAR
#endif
//=============================================================================
2016-06-15 18:43:10 +12:00
// forwards declarations
2016-03-19 06:57:51 +13:00
class TSegment;
class TQuadratic;
class TThickQuadratic;
//=============================================================================
/*! area (orientata) del trapeziode limitato dalla curva
e dall'asse delle ascisse. L'area e' positiva se p(0),...p(t)...,p(1),p(0)
2016-06-15 18:43:10 +12:00
viene percorso in senso antiorario
2016-03-19 06:57:51 +13:00
DVAPI double getArea(const TQuadratic &curve);
*/
2016-06-15 18:43:10 +12:00
/*! Returns true if the min distance between \b point an \b segment is less o
* equal to \b distance
2016-03-19 06:57:51 +13:00
*/
2016-06-15 18:43:10 +12:00
DVAPI bool isCloseToSegment(const TPointD &point, const TSegment &segment,
double distance);
2016-03-19 06:57:51 +13:00
/*!
Compute min distance between a segment and a point
*/
DVAPI double tdistance(const TSegment &segment, const TPointD &point);
2016-06-15 18:43:10 +12:00
inline double tdistance(const TPointD &point, const TSegment &segment) {
return tdistance(segment, point);
2016-03-19 06:57:51 +13:00
}
/*!
2016-06-15 18:43:10 +12:00
Compute intersection between segments;
2016-03-19 06:57:51 +13:00
return the number of intersections (0/1/2/-1) and add them
(as a param couple) to the vector 'intersections'
2016-06-15 18:43:10 +12:00
\note
2016-03-19 06:57:51 +13:00
if the segment intersections is larger than one point
2016-06-15 18:43:10 +12:00
(i.e. the segments share a sub-segment) return 2 and
2016-03-19 06:57:51 +13:00
in vector there are extremes of sub-segment.
*/
2016-06-15 18:43:10 +12:00
DVAPI int intersect(const TPointD &seg1p0, const TPointD &seg1p1,
const TPointD &seg2p0, const TPointD &seg2p1,
std::vector<DoublePair> &intersections);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
DVAPI int intersect(const TSegment &first, const TSegment &second,
std::vector<DoublePair> &intersections);
2016-03-19 06:57:51 +13:00
/*!
2016-06-15 18:43:10 +12:00
Compute intersection between quadratics;
2016-03-19 06:57:51 +13:00
return the number of intersections (0-4) and add them
(as a param couple) to the vector 'intersections'
*/
2016-06-15 18:43:10 +12:00
DVAPI int intersect(const TQuadratic &q0, const TQuadratic &q1,
std::vector<DoublePair> &intersections,
bool checksegments = true);
2016-03-19 06:57:51 +13:00
/*!
2016-06-15 18:43:10 +12:00
Compute intersection between and a segment;
2016-03-19 06:57:51 +13:00
return the number of intersections [0,2] and add them
(as a param couple) to the vector 'intersections'.
Remark:
In pair "first" is for the first object and "second"
its for the second.
*/
2016-06-15 18:43:10 +12:00
DVAPI int intersect(const TQuadratic &q, const TSegment &s,
std::vector<DoublePair> &intersections,
bool firstQuad = true);
inline int intersect(const TSegment &s, const TQuadratic &q,
std::vector<DoublePair> &intersections) {
return intersect(q, s, intersections, false);
2016-03-19 06:57:51 +13:00
}
template <class T>
2016-06-15 18:43:10 +12:00
void split(const T &tq, const std::vector<double> &pars, std::vector<T *> &v) {
if (pars.empty()) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
T *q1, q2;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
UINT i;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
q1 = new T();
tq.split(pars[0], *q1, q2);
v.push_back(q1);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
for (i = 1; i < pars.size(); ++i) {
double newPar = (pars[i] - pars[i - 1]) / (1.0 - pars[i - 1]);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
q1 = new T();
q2.split(newPar, *q1, q2);
v.push_back(q1);
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
v.push_back(new T(q2));
2016-03-19 06:57:51 +13:00
}
template <class T>
2016-06-15 18:43:10 +12:00
void split(const T &tq, double w0, double w1, T &qOut) {
T q2;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
assert(w0 <= w1);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if ((w1 - w0 == 0.0) && w0 == 1.0) {
tq.split(w0, q2, qOut);
return;
}
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
tq.split(w0, qOut, q2);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
double newPar = (w1 - w0) / (1.0 - w0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
q2.split(newPar, qOut, q2);
2016-03-19 06:57:51 +13:00
}
DVAPI double computeStep(const TQuadratic &quad, double pixelSize);
DVAPI double computeStep(const TThickQuadratic &quad, double pixelSize);
//=============================================================================
/*!
TQuadraticLengthEvaulator is an explicit length builder that for a specified
quadratic.
The purpose of a dedicated evaluator for the length of a quadratic is that of
minimizing its computational cost.
Both assigning a quadratic to the evaluator and retrieving its length up
to a given parameter cost 1 sqrt and 1 log.
*/
2016-06-15 18:43:10 +12:00
class TQuadraticLengthEvaluator {
double m_c, m_e, m_f, m_sqrt_a_div_2, m_tRef, m_primitive_0;
bool m_constantSpeed, m_noSpeed0, m_squareIntegrand;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TQuadraticLengthEvaluator() {}
TQuadraticLengthEvaluator(const TQuadratic &quad) { setQuad(quad); }
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
void setQuad(const TQuadratic &quad);
double getLengthAt(double t) const;
2016-03-19 06:57:51 +13:00
};
#endif
//-----------------------------------------------------------------------------
// End Of File
//-----------------------------------------------------------------------------