1 #pragma once
2
3 #ifndef TCURVES_UTIL_INCLUDED
4 #define TCURVES_UTIL_INCLUDED
5
6 //#include "tutil.h"
7 #include "tgeometry.h"
8
9 #undef DVAPI
10 #undef DVVAR
11 #ifdef TGEOMETRY_EXPORTS
12 #define DVAPI DV_EXPORT_API
13 #define DVVAR DV_EXPORT_VAR
14 #else
15 #define DVAPI DV_IMPORT_API
16 #define DVVAR DV_IMPORT_VAR
17 #endif
18
19 //=============================================================================
20 // forwards declarations
21
22 class TSegment;
23 class TQuadratic;
24 class TThickQuadratic;
25
26 //=============================================================================
27
28 /*! area (orientata) del trapeziode limitato dalla curva
29 e dall'asse delle ascisse. L'area e' positiva se p(0),...p(t)...,p(1),p(0)
30 viene percorso in senso antiorario
31 DVAPI double getArea(const TQuadratic &curve);
32 */
33
34 /*! Returns true if the min distance between \b point an \b segment is less o
35 * equal to \b distance
36 */
37 DVAPI bool isCloseToSegment(const TPointD &point, const TSegment &segment,
38 double distance);
39
40 /*!
41 Compute min distance between a segment and a point
42 */
43 DVAPI double tdistance(const TSegment &segment, const TPointD &point);
tdistance(const TPointD & point,const TSegment & segment)44 inline double tdistance(const TPointD &point, const TSegment &segment) {
45 return tdistance(segment, point);
46 }
47
48 /*!
49 Compute intersection between segments;
50 return the number of intersections (0/1/2/-1) and add them
51 (as a param couple) to the vector 'intersections'
52 \note
53 if the segment intersections is larger than one point
54 (i.e. the segments share a sub-segment) return 2 and
55 in vector there are extremes of sub-segment.
56 */
57
58 DVAPI int intersect(const TPointD &seg1p0, const TPointD &seg1p1,
59 const TPointD &seg2p0, const TPointD &seg2p1,
60 std::vector<DoublePair> &intersections);
61
62 DVAPI int intersect(const TSegment &first, const TSegment &second,
63 std::vector<DoublePair> &intersections);
64
65 /*!
66 Compute intersection between quadratics;
67 return the number of intersections (0-4) and add them
68 (as a param couple) to the vector 'intersections'
69 */
70 DVAPI int intersect(const TQuadratic &q0, const TQuadratic &q1,
71 std::vector<DoublePair> &intersections,
72 bool checksegments = true);
73
74 /*!
75 Compute intersection between and a segment;
76 return the number of intersections [0,2] and add them
77 (as a param couple) to the vector 'intersections'.
78 Remark:
79 In pair "first" is for the first object and "second"
80 its for the second.
81 */
82 DVAPI int intersect(const TQuadratic &q, const TSegment &s,
83 std::vector<DoublePair> &intersections,
84 bool firstQuad = true);
85
intersect(const TSegment & s,const TQuadratic & q,std::vector<DoublePair> & intersections)86 inline int intersect(const TSegment &s, const TQuadratic &q,
87 std::vector<DoublePair> &intersections) {
88 return intersect(q, s, intersections, false);
89 }
90
91 template <class T>
split(const T & tq,const std::vector<double> & pars,std::vector<T * > & v)92 void split(const T &tq, const std::vector<double> &pars, std::vector<T *> &v) {
93 if (pars.empty()) return;
94
95 T *q1, q2;
96
97 UINT i;
98
99 q1 = new T();
100 tq.split(pars[0], *q1, q2);
101 v.push_back(q1);
102
103 for (i = 1; i < pars.size(); ++i) {
104 double newPar = (pars[i] - pars[i - 1]) / (1.0 - pars[i - 1]);
105
106 q1 = new T();
107 q2.split(newPar, *q1, q2);
108 v.push_back(q1);
109 }
110
111 v.push_back(new T(q2));
112 }
113
114 template <class T>
split(const T & tq,double w0,double w1,T & qOut)115 void split(const T &tq, double w0, double w1, T &qOut) {
116 T q2;
117
118 assert(w0 <= w1);
119
120 if ((w1 - w0 == 0.0) && w0 == 1.0) {
121 tq.split(w0, q2, qOut);
122 return;
123 }
124
125 tq.split(w0, qOut, q2);
126
127 double newPar = (w1 - w0) / (1.0 - w0);
128
129 q2.split(newPar, qOut, q2);
130 }
131
132 DVAPI double computeStep(const TQuadratic &quad, double pixelSize);
133
134 DVAPI double computeStep(const TThickQuadratic &quad, double pixelSize);
135
136 //=============================================================================
137
138 /*!
139 TQuadraticLengthEvaulator is an explicit length builder that for a specified
140 quadratic.
141
142 The purpose of a dedicated evaluator for the length of a quadratic is that of
143 minimizing its computational cost.
144 Both assigning a quadratic to the evaluator and retrieving its length up
145 to a given parameter cost 1 sqrt and 1 log.
146 */
147 class TQuadraticLengthEvaluator {
148 double m_c, m_e, m_f, m_sqrt_a_div_2, m_tRef, m_primitive_0;
149 bool m_constantSpeed, m_noSpeed0, m_squareIntegrand;
150
151 public:
TQuadraticLengthEvaluator()152 TQuadraticLengthEvaluator() {}
TQuadraticLengthEvaluator(const TQuadratic & quad)153 TQuadraticLengthEvaluator(const TQuadratic &quad) { setQuad(quad); }
154
155 void setQuad(const TQuadratic &quad);
156 double getLengthAt(double t) const;
157 };
158
159 #endif
160 //-----------------------------------------------------------------------------
161 // End Of File
162 //-----------------------------------------------------------------------------
163