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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
|
/* This file is part of the KDE project
Copyright (C) 2001, 2002, 2003 The Karbon Developers
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#ifndef __VSEGMENT_H__
#define __VSEGMENT_H__
#include <qptrlist.h>
#include <qvaluelist.h>
#include <KoPoint.h>
#include <KoRect.h>
#include "vglobal.h"
#include <koffice_export.h>
class QDomElement;
class VPainter;
/**
* A class representing lines and beziers. We waste some KoPoints, if we
* would use only lines, but this makes it easy to convert the segment types
* into each other. Make sure yourself, that you pass values to functions within
* proper ranges.
*/
class KARBONBASE_EXPORT VSegment
{
friend class VSubpath;
friend class VSubpathIterator;
public:
/**
* Tells which control point is "fixed" i.e. located at the
* corresponding knot and invisible. This flag makes no sense for
* line segments.
*/
enum VCtrlPointFixing
{
none = 0,
first = 1,
second = 2
};
enum VState
{
normal,
deleted
};
VSegment( unsigned short deg = 3 );
VSegment( const VSegment& segment );
~VSegment();
/**
* Returns the segment's degree, which is identical to the number of nodes.
* For cubic beziers it is "three" and "one" for lines.
*/
unsigned short degree() const
{
return m_degree;
}
/**
* Sets the segment's degree and thus resizes the array of node data.
* The node data is copied from the old knot "backwards".
*/
void setDegree( unsigned short deg );
/**
* Tests for the segment type ("begin", "line" or "curve").
*/
bool isBegin() const { return (degree() == 1) && !prev(); }
bool isLine() const { return (degree() == 1) && prev(); }
bool isCurve() const { return degree() > 1; }
/**
* Returns the segment state.
*/
VState state() const
{
return m_state;
}
/**
* Sets the segment state.
*/
void setState( VState state )
{
m_state = state;
}
/**
* Returns the segment's point with index 0 <= i < degree().
*/
const KoPoint& point( int i ) const
{
return m_nodes[ i ].m_vector;
}
/**
* This is a convenience function. It returns the point with index
* 0 <= i <= degree() while p( 0 ) is the knot of the previous
* segment.
*/
const KoPoint& p( int i ) const
{
return i == 0
? prev()->knot()
: m_nodes[ --i ].m_vector;
}
/**
* Returns the knot. This is a convenience function using point().
*/
const KoPoint& knot() const
{
return point( degree() - 1 );
}
/**
* Sets the segment's point with index 0 <= i < degree() to "p".
*/
void setPoint( int i, const KoPoint& p )
{
m_nodes[ i ].m_vector = p;
}
/**
* This is a convenience function. It sets the point with index
* 0 <= i <= degree() to "p" while setP( 0 ) sets the knot of the
* previous segment.
*/
void setP( int i, const KoPoint& p )
{
if( i == 0 )
prev()->setKnot( p );
else
m_nodes[ --i ].m_vector = p;
}
/**
* Sets the knot. This is a convenience function.
*/
void setKnot( const KoPoint& p )
{
m_nodes[ degree() - 1 ].m_vector = p;
}
/**
* Returns true if the point with index 0 <= i < degree() is selected.
*/
bool pointIsSelected( int i ) const
{
return m_nodes[ i ].m_isSelected;
}
/**
* Returns true if the knot is selected. This is a convenience function.
*/
bool knotIsSelected() const
{
return m_nodes[ degree() - 1 ].m_isSelected;
}
/**
* Selects the point with index 0 <= i < degree().
*/
void selectPoint( int i, bool select = true )
{
m_nodes[ i ].m_isSelected = select;
}
/**
* Selects/deselects the knot of this segment.
*/
void selectKnot( bool select = true )
{
m_nodes[ degree() - 1 ].m_isSelected = select;
}
/**
* Returns index of the node at point p. Returns 0 if no
* segment point matches point p.
*/
// TODO: Move this function into "userland"
uint nodeNear( const KoPoint& p,
double isNearRange = VGlobal::isNearRange ) const;
/**
* Returns a pointer to the previous not deleted segment, if
* stored in a VSubpath.
*/
VSegment* prev() const;
/**
* Returns a pointer to the next not deleted segment, if
* stored in a VSubpath.
*/
VSegment* next() const;
/**
* Returns true if the segment is flat. That means it's height
* is smaller than flatness.
*/
bool isFlat( double flatness = VGlobal::flatnessTolerance ) const;
/**
* Calculates the point on this segment at parameter 0 <= t <= 1.
* This is a convenience wrapper for pointDerivativesAt().
*/
KoPoint pointAt( double t ) const;
/**
* Calculates the point and the derivatives of first and
* second order for 0 <= t <= 1.
*/
void pointDerivativesAt( double t, KoPoint* p = 0L,
KoPoint* d1 = 0L, KoPoint* d2 = 0L ) const;
/**
* Calculates the normalized tangent vector (length=1) at the point
* parameterized by 0 <= t <= 1. This is a convenience wrapper
* for pointTangentNormalAt(). Use the latter function directly if you
* need to calculate the point and normal vector or tangent vector
* at once.
*/
KoPoint tangentAt( double t ) const;
/**
* Calculates the point, the tangent vector and the normal vector for
* 0 <= t <= 1. The tangent vector and the normal vector are
* normalized (length=1).
*/
void pointTangentNormalAt( double t, KoPoint* p = 0L,
KoPoint* tn = 0L, KoPoint* n = 0L ) const;
/**
* Calculates the arclength from p0 to the point parametrized
* by 0 <= t <= 1. For beziers this function is a bit expensive.
*/
double length( double t = 1.0 ) const;
/**
* Calculates the chord length (the distance from the previous
* knot to the current knot).
*/
double chordLength() const;
/**
* Calculates the length of the control polygon.
*/
double polyLength() const;
/**
* Calculates the parameter of a point located at arclength len.
* This is the exact inverse operation of length( t ).
*/
double lengthParam( double len ) const;
/**
* Calculates the parameter of the nearest point on this segment
* to the point p. This function is pretty expensive.
*/
double nearestPointParam( const KoPoint& p ) const;
/**
* Calculates wether the tangent at the knot is exactly parallel to
* the tangent at p0 of the next segment. Returns false if the
* current segment is a "begin".
*/
bool isSmooth( const VSegment& next ) const;
bool isSmooth() const
{
return next()
? isSmooth( *next() )
: false;
}
/**
* Creates a reverted version of this segment. For example:
* if this segment is a line from A to B, the result is a
* line from B to A.
*/
VSegment* revert() const;
/**
* Splits the segment at parameter 0 <= t <= 1. Returns a pointer
* to the first segment and modifies the current one to
* be the second segment.
*/
VSegment* splitAt( double t );
/**
* Calculates height of point p above line AB.
*/
static double height(
const KoPoint& a,
const KoPoint& p,
const KoPoint& b );
/**
* Calculates whether lines A0A1 and B0B1 intersect.
*/
static bool linesIntersect(
const KoPoint& a0,
const KoPoint& a1,
const KoPoint& b0,
const KoPoint& b1 );
/**
* Returns true, if this segment intersects the other segment.
*/
bool intersects( const VSegment& segment ) const;
/**
* Returns a number > 0 if the point p is left, 0 if it's on and
* a number < 0 if it's right of the infinite line through the
* previous segment's knot and the current knot.
*/
double pointIsLeft( const KoPoint& p ) const
{
return
( knot().x() - prev()->knot().x() ) *
( p.y() - prev()->knot().y() )
-
( p.x() - prev()->knot().x() ) *
( knot().y() - prev()->knot().y() );
}
/**
* Calculates the bounding box.
*/
KoRect boundingBox() const;
void draw( VPainter* painter ) const;
// TODO: remove this backward compatibility function after koffice 1.3.x.
void load( const QDomElement& element );
/**
* Returns a pointer to a copy of this segment.
*/
VSegment* clone() const;
private:
/**
* Calculates the solutions of y(x) = 0 where 0 <= x <= 1. The
* returned parameters are not ordered.
*/
void rootParams( QValueList<double>& params ) const;
/**
* Calculates how often the control polygon crosses the x-axis.
*/
int controlPolygonZeros() const;
/**
* The segment degree. For (cubic) beziers "three", "one" for lines.
*/
unsigned short m_degree : 6;
/**
* The segment state.
*/
VState m_state : 2;
/**
* Node data.
*/
struct VNodeData
{
KoPoint m_vector;
bool m_isSelected;
};
/**
* A pointer to an array of node data.
*/
VNodeData* m_nodes;
/**
* Pointer to the previous segment.
*/
VSegment* m_prev;
/**
* Pointer to the next segment.
*/
VSegment* m_next;
};
#endif
|