blob: c40a4f93fc8185e721eaf85795b49923c1ee6e91 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
#include "Spline.h"
namespace nvidia {
namespace CurveEditor {
Spline::Spline(long segmentCount)
: _needSample(true)
, _samplePoints()
, _edgeLengths()
, _totolLength(0.0f)
{
_segmentCount = segmentCount < 1 ? 1 : segmentCount;
}
Spline::~Spline(void)
{
}
bool Spline::getPercentPoint(float percent, QPointF& point)
{
long lPointCount = (long)_samplePoints.size();
if (lPointCount == 0)
{
return false;
}
else if (lPointCount == 1)
{
point = _samplePoints[0];
return true;
}
if (_needSample)
_sample();
if (percent > 1.0)
{
percent -= (int)percent;
}
if (percent < 0.0)
{
percent += (int)(-percent) + 1;
}
if (percent <= 0.0)
{
// return begin point
point = _samplePoints[0];
return true;
}
else if (percent >= 1.0)
{
// return end point
point = _samplePoints[_samplePoints.size() - 1];
return true;
}
float fCurPos = _totolLength * percent;
int index = 0;
{// get edge's index the point is on based on the percent value
long lEdgeCount = (long)_edgeLengths.size();
for ( ; index < lEdgeCount; index++)
{
if (fCurPos < _edgeLengths[index])
{
break;
}
fCurPos -= _edgeLengths[index];
}
if (index == lEdgeCount)
{
point = _samplePoints[_samplePoints.size() - 1];
return true;
}
}
QPointF v1 = _samplePoints[index];
QPointF v2 = _samplePoints[index + 1];
point = v1 + (v2 - v1) * (fCurPos / _edgeLengths[index]);
return true;
}
bool Spline::getPointByX(float x, QPointF& point)
{
if (_needSample)
_sample();
long lPointCount = (long)_samplePoints.size();
if(lPointCount < 2)
{
return false;
}
for (int i = 0; i < lPointCount - 1; i++)
{
if(_samplePoints[i].x() <= x && _samplePoints[i + 1].x() > x)
{
point.setX( x );
float fRate = (x - _samplePoints[i].x())/ (_samplePoints[i + 1].x() - _samplePoints[i].x());
point.setY(_samplePoints[i].y() + (_samplePoints[i+1].y() - _samplePoints[i].y()) * fRate);
return true;
}
}
return false;
}
void Spline::setSegmentCount(long segmentCount)
{
_segmentCount = segmentCount < 1 ? 1 : segmentCount;
_needSample = true;
}
std::vector<QPointF> Spline::getSamplePoints()
{
if (_needSample)
_sample();
return _samplePoints;
}
void Spline::_sample()
{
_samplePoints.clear();
_doSample();
// get all edges length and total length
_totolLength = 0;
_edgeLengths.clear();
float fEdgeLength;
long lPointCount = (long)_samplePoints.size();
for (int i = 1; i < lPointCount; i++)
{
QPointF dist = _samplePoints[i] - _samplePoints[i - 1];
fEdgeLength = (sqrt(dist.x()*dist.x() + dist.y() + dist.y()));
_edgeLengths.push_back(fEdgeLength);
_totolLength += fEdgeLength;
}
_needSample = false;
}
} // namespace CurveEditor
} // namespace nvidia
|