1
// -*- Mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; tab-width: 4; -*-
2
/* This file is part of the KDE project
3
Copyright (C) 2001 Laurent MONTEL <lmontel@mandrakesoft.com>
5
This library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public
7
License as published by the Free Software Foundation; either
8
version 2 of the License, or (at your option) any later version.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
* Boston, MA 02110-1301, USA.
21
#include "KoPointArray.h"
24
#include <KoZoomHandler.h>
26
void KoPointArray::translate( double dx, double dy )
28
register KoPoint *p = data();
29
register int i = size();
37
void KoPointArray::point( uint index, double *x, double *y ) const
39
KoPoint p = QMemArray<KoPoint>::at( index );
46
KoPoint KoPointArray::point( uint index ) const
47
{ // #### index out of bounds
48
return QMemArray<KoPoint>::at( index );
51
void KoPointArray::setPoint( uint index, double x, double y )
52
{ // #### index out of bounds
53
QMemArray<KoPoint>::at( index ) = KoPoint( x, y );
58
bool KoPointArray::putPoints( int index, int nPoints, double firstx, double firsty,
62
if ( index + nPoints > (int)size() ) { // extend array
63
if ( !resize(index + nPoints) )
68
setPoint( index, firstx, firsty ); // set first point
72
va_start( ap, firsty );
74
x = va_arg( ap, double );
75
y = va_arg( ap, double );
76
setPoint( i++, x, y );
82
void split(const double *p, double *l, double *r)
92
l[2] = (p[0]+ p[2])/2;
93
l[3] = (p[1]+ p[3])/2;
94
tmpx = (p[2]+ p[4])/2;
95
tmpy = (p[3]+ p[5])/2;
96
r[4] = (p[4]+ p[6])/2;
97
r[5] = (p[5]+ p[7])/2;
99
l[4] = (l[2]+ tmpx)/2;
100
l[5] = (l[3]+ tmpy)/2;
101
r[2] = (tmpx + r[4])/2;
102
r[3] = (tmpy + r[5])/2;
104
l[6] = (l[4]+ r[2])/2;
105
l[7] = (l[5]+ r[3])/2;
112
// A Fast 2D Point-On-Line Test
114
// from "Graphics Gems", Academic Press, 1990
116
int pnt_on_line( const int* p, const int* q, const int* t )
119
* given a line through P:(px,py) Q:(qx,qy) and T:(tx,ty)
120
* return 0 if T is not on the line through <--P--Q-->
121
* 1 if T is on the open ray ending at P: <--P
122
* 2 if T is on the closed interior along: P--Q
123
* 3 if T is on the open ray beginning at Q: Q-->
125
* Example: consider the line P = (3,2), Q = (17,7). A plot
126
* of the test points T(x,y) (with 0 mapped onto '.') yields:
128
* 8| . . . . . . . . . . . . . . . . . 3 3
129
* Y 7| . . . . . . . . . . . . . . 2 2 Q 3 3 Q = 2
130
* 6| . . . . . . . . . . . 2 2 2 2 2 . . .
131
* a 5| . . . . . . . . 2 2 2 2 2 2 . . . . .
132
* x 4| . . . . . 2 2 2 2 2 2 . . . . . . . .
133
* i 3| . . . 2 2 2 2 2 . . . . . . . . . . .
134
* s 2| 1 1 P 2 2 . . . . . . . . . . . . . . P = 2
135
* 1| 1 1 . . . . . . . . . . . . . . . . .
136
* +--------------------------------------
137
* 1 2 3 4 5 X-axis 10 15 19
139
* Point-Line distance is normalized with the Infinity Norm
140
* avoiding square-root code and tightening the test vs the
141
* Manhattan Norm. All math is done on the field of integers.
142
* The latter replaces the initial ">= MAX(...)" test with
143
* "> (ABS(qx-px) + ABS(qy-py))" loosening both inequality
144
* and norm, yielding a broader target line for selection.
145
* The tightest test is employed here for best discrimination
146
* in merging collinear (to grid coordinates) vertex chains
147
* into a larger, spanning vectors within the Lemming editor.
150
// if all points are coincident, return condition 2 (on line)
151
if(q[0]==p[0] && q[1]==p[1] && q[0]==t[0] && q[1]==t[1]) {
155
if ( QABS((q[1]-p[1])*(t[0]-p[0])-(t[1]-p[1])*(q[0]-p[0])) >=
156
(QMAX(QABS(q[0]-p[0]), QABS(q[1]-p[1])))) return 0;
158
if (((q[0]<p[0])&&(p[0]<t[0])) || ((q[1]<p[1])&&(p[1]<t[1])))
160
if (((t[0]<p[0])&&(p[0]<q[0])) || ((t[1]<p[1])&&(p[1]<q[1])))
162
if (((p[0]<q[0])&&(q[0]<t[0])) || ((p[1]<q[1])&&(q[1]<t[1])))
164
if (((t[0]<q[0])&&(q[0]<p[0])) || ((t[1]<q[1])&&(q[1]<p[1])))
171
void polygonizeQBezier( double* acc, int& accsize, const double ctrl[],
174
if ( accsize > maxsize / 2 )
176
// This never happens in practice.
178
if ( accsize >= maxsize-4 )
180
// Running out of space - approximate by a line.
181
acc[accsize++] = ctrl[0];
182
acc[accsize++] = ctrl[1];
183
acc[accsize++] = ctrl[6];
184
acc[accsize++] = ctrl[7];
193
// convert to integers for line condition check
194
int c0[2]; c0[0] = int(ctrl[0]); c0[1] = int(ctrl[1]);
195
int c1[2]; c1[0] = int(ctrl[2]); c1[1] = int(ctrl[3]);
196
int c2[2]; c2[0] = int(ctrl[4]); c2[1] = int(ctrl[5]);
197
int c3[2]; c3[0] = int(ctrl[6]); c3[1] = int(ctrl[7]);
199
// #### Duplication needed?
200
if ( QABS(c1[0]-c0[0]) <= 1 && QABS(c1[1]-c0[1]) <= 1
201
&& QABS(c2[0]-c0[0]) <= 1 && QABS(c2[1]-c0[1]) <= 1
202
&& QABS(c3[0]-c1[0]) <= 1 && QABS(c3[1]-c0[1]) <= 1 )
204
// Approximate by one line.
205
// Dont need to write last pt as it is the same as first pt
206
// on the next segment
207
acc[accsize++] = l[0];
208
acc[accsize++] = l[1];
212
if ( ( pnt_on_line( c0, c3, c1 ) == 2 && pnt_on_line( c0, c3, c2 ) == 2 )
213
|| ( QABS(c1[0]-c0[0]) <= 1 && QABS(c1[1]-c0[1]) <= 1
214
&& QABS(c2[0]-c0[0]) <= 1 && QABS(c2[1]-c0[1]) <= 1
215
&& QABS(c3[0]-c1[0]) <= 1 && QABS(c3[1]-c0[1]) <= 1 ) )
217
// Approximate by one line.
218
// Dont need to write last pt as it is the same as first pt
219
// on the next segment
220
acc[accsize++] = l[0];
221
acc[accsize++] = l[1];
225
// Too big and too curved - recusively subdivide.
226
polygonizeQBezier( acc, accsize, l, maxsize );
227
polygonizeQBezier( acc, accsize, r, maxsize );
231
KoRect KoPointArray::boundingRect() const
234
return KoRect( 0, 0, 0, 0 ); // null rectangle
235
register KoPoint *pd = data();
236
double minx, maxx, miny, maxy;
237
minx = maxx = pd->x();
238
miny = maxy = pd->y();
240
for ( int i=1; i<(int)size(); i++ ) { // find min+max x and y
241
if ( pd->x() < minx )
243
else if ( pd->x() > maxx )
245
if ( pd->y() < miny )
247
else if ( pd->y() > maxy )
251
return KoRect( KoPoint(minx,miny), KoPoint(maxx,maxy) );
255
KoPointArray KoPointArray::cubicBezier() const
258
#if defined(QT_CHECK_RANGE)
259
qWarning( "QPointArray::bezier: The array must have 4 control points" );
264
KoRect r = boundingRect();
265
int m = (int)(4+2*QMAX(r.width(),r.height()));
266
double *p = new double[m];
269
for (i=0; i<4; i++) {
270
ctrl[i*2] = at(i).x();
271
ctrl[i*2+1] = at(i).y();
274
polygonizeQBezier( p, len, ctrl, m );
275
KoPointArray pa((len/2)+1); // one extra point for last point on line
277
for (i=0; j<len; i++) {
278
double x = qRound(p[j++]);
279
double y = qRound(p[j++]);
280
pa[i] = KoPoint(x,y);
282
// add last pt on the line, which will be at the last control pt
283
pa[(int)pa.size()-1] = at(3);
290
QPointArray KoPointArray::zoomPointArray( const KoZoomHandler* zoomHandler ) const
292
QPointArray tmpPoints(size());
293
for ( uint i= 0; i<size();i++ ) {
295
tmpPoints.putPoints( i, 1, zoomHandler->zoomItX(p.x()),zoomHandler->zoomItY(p.y()) );
300
QPointArray KoPointArray::zoomPointArray( const KoZoomHandler* zoomHandler, int penWidth ) const
304
KoSize ext = boundingRect().size();
305
int pw = zoomHandler->zoomItX( penWidth ) / 2;
307
fx = (double)( zoomHandler->zoomItX(ext.width()) - 2 * pw ) / ext.width();
308
fy = (double)( zoomHandler->zoomItY(ext.height()) - 2 * pw ) / ext.height();
310
unsigned int index = 0;
311
QPointArray tmpPoints;
312
KoPointArray::ConstIterator it;
313
for ( it = begin(); it != end(); ++it, ++index ) {
314
int tmpX = qRound((*it).x() * fx + pw);
315
int tmpY = qRound((*it).y() * fy + pw);
317
tmpPoints.putPoints( index, 1, tmpX, tmpY );