karbon

vsegment.h
1/* This file is part of the KDE project
2 Copyright (C) 2001, 2002, 2003 The Karbon Developers
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#ifndef __VSEGMENT_H__
21#define __VSEGMENT_H__
22
23#include <tqptrlist.h>
24#include <tqvaluelist.h>
25
26#include <KoPoint.h>
27#include <KoRect.h>
28
29#include "vglobal.h"
30#include <koffice_export.h>
31
32class TQDomElement;
33class VPainter;
34
42class KARBONBASE_EXPORT VSegment
43{
44 friend class VSubpath;
45 friend class VSubpathIterator;
46
47public:
54 {
55 none = 0,
56 first = 1,
57 second = 2
58 };
59
60 enum VState
61 {
62 normal,
63 deleted
64 };
65
66
67 VSegment( unsigned short deg = 3 );
68
69 VSegment( const VSegment& segment );
70
71 ~VSegment();
72
77 unsigned short degree() const
78 {
79 return m_degree;
80 }
81
86 void setDegree( unsigned short deg );
87
91 bool isBegin() const { return (degree() == 1) && !prev(); }
92 bool isLine() const { return (degree() == 1) && prev(); }
93 bool isCurve() const { return degree() > 1; }
94
98 VState state() const
99 {
100 return m_state;
101 }
102
106 void setState( VState state )
107 {
108 m_state = state;
109 }
110
111
115 const KoPoint& point( int i ) const
116 {
117 return m_nodes[ i ].m_vector;
118 }
119
125 const KoPoint& p( int i ) const
126 {
127 return i == 0
128 ? prev()->knot()
129 : m_nodes[ --i ].m_vector;
130 }
131
135 const KoPoint& knot() const
136 {
137 return point( degree() - 1 );
138 }
139
143 void setPoint( int i, const KoPoint& p )
144 {
145 m_nodes[ i ].m_vector = p;
146 }
147
153 void setP( int i, const KoPoint& p )
154 {
155 if( i == 0 )
156 prev()->setKnot( p );
157 else
158 m_nodes[ --i ].m_vector = p;
159 }
160
164 void setKnot( const KoPoint& p )
165 {
166 m_nodes[ degree() - 1 ].m_vector = p;
167 }
168
169
173 bool pointIsSelected( int i ) const
174 {
175 return m_nodes[ i ].m_isSelected;
176 }
177
181 bool knotIsSelected() const
182 {
183 return m_nodes[ degree() - 1 ].m_isSelected;
184 }
185
189 void selectPoint( int i, bool select = true )
190 {
191 m_nodes[ i ].m_isSelected = select;
192 }
193
197 void selectKnot( bool select = true )
198 {
199 m_nodes[ degree() - 1 ].m_isSelected = select;
200 }
201
202
207 // TODO: Move this function into "userland"
208 uint nodeNear( const KoPoint& p,
209 double isNearRange = VGlobal::isNearRange ) const;
210
211
216 VSegment* prev() const;
217
222 VSegment* next() const;
223
228 bool isFlat( double flatness = VGlobal::flatnessTolerance ) const;
229
230
235 KoPoint pointAt( double t ) const;
236
241 void pointDerivativesAt( double t, KoPoint* p = 0L,
242 KoPoint* d1 = 0L, KoPoint* d2 = 0L ) const;
243
244
252 KoPoint tangentAt( double t ) const;
253
259 void pointTangentNormalAt( double t, KoPoint* p = 0L,
260 KoPoint* tn = 0L, KoPoint* n = 0L ) const;
261
262
267 double length( double t = 1.0 ) const;
268
273 double chordLength() const;
274
278 double polyLength() const;
279
280
285 double lengthParam( double len ) const;
286
287
292 double nearestPointParam( const KoPoint& p ) const;
293
294
300 bool isSmooth( const VSegment& next ) const;
301
302 bool isSmooth() const
303 {
304 return next()
305 ? isSmooth( *next() )
306 : false;
307 }
308
309
315 VSegment* revert() const;
316
317
323 VSegment* splitAt( double t );
324
325
329 static double height(
330 const KoPoint& a,
331 const KoPoint& p,
332 const KoPoint& b );
333
334
338 static bool linesIntersect(
339 const KoPoint& a0,
340 const KoPoint& a1,
341 const KoPoint& b0,
342 const KoPoint& b1 );
343
347 bool intersects( const VSegment& segment ) const;
348
349
355 double pointIsLeft( const KoPoint& p ) const
356 {
357 return
358 ( knot().x() - prev()->knot().x() ) *
359 ( p.y() - prev()->knot().y() )
360 -
361 ( p.x() - prev()->knot().x() ) *
362 ( knot().y() - prev()->knot().y() );
363 }
364
368 KoRect boundingBox() const;
369
370
371 void draw( VPainter* painter ) const;
372
373 // TODO: remove this backward compatibility function after koffice 1.3.x.
374 void load( const TQDomElement& element );
375
376
380 VSegment* clone() const;
381
382private:
387 void rootParams( TQValueList<double>& params ) const;
388
392 int controlPolygonZeros() const;
393
394
398 unsigned short m_degree : 6;
399
403 VState m_state : 2;
404
408 struct VNodeData
409 {
410 KoPoint m_vector;
411 bool m_isSelected;
412 };
413
417 VNodeData* m_nodes;
418
419
423 VSegment* m_prev;
424
428 VSegment* m_next;
429};
430
431#endif
432
A class representing lines and beziers.
Definition vsegment.h:43
bool pointIsSelected(int i) const
Returns true if the point with index 0 <= i < degree() is selected.
Definition vsegment.h:173
bool knotIsSelected() const
Returns true if the knot is selected.
Definition vsegment.h:181
bool isBegin() const
Tests for the segment type ("begin", "line" or "curve").
Definition vsegment.h:91
double pointIsLeft(const KoPoint &p) 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 infin...
Definition vsegment.h:355
void setKnot(const KoPoint &p)
Sets the knot.
Definition vsegment.h:164
void setState(VState state)
Sets the segment state.
Definition vsegment.h:106
void setPoint(int i, const KoPoint &p)
Sets the segment's point with index 0 <= i < degree() to "p".
Definition vsegment.h:143
void selectKnot(bool select=true)
Selects/deselects the knot of this segment.
Definition vsegment.h:197
const KoPoint & p(int i) const
This is a convenience function.
Definition vsegment.h:125
void selectPoint(int i, bool select=true)
Selects the point with index 0 <= i < degree().
Definition vsegment.h:189
unsigned short degree() const
Returns the segment's degree, which is identical to the number of nodes.
Definition vsegment.h:77
const KoPoint & point(int i) const
Returns the segment's point with index 0 <= i < degree().
Definition vsegment.h:115
const KoPoint & knot() const
Returns the knot.
Definition vsegment.h:135
VCtrlPointFixing
Tells which control point is "fixed" i.e.
Definition vsegment.h:54
VState state() const
Returns the segment state.
Definition vsegment.h:98
void setP(int i, const KoPoint &p)
This is a convenience function.
Definition vsegment.h:153
VSubpathIterator provides an iterator class for highlevel path access.
Definition vpath.h:180
VSubpath provides a sophisticated list of VSegment.
Definition vpath.h:43