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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
|
// Copyright (C) 2002 Dominique Devriese <devriese@kde.org>
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
// This program 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 General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
// 02110-1301, USA.
#include "line_imp.h"
#include "bogus_imp.h"
#include "point_imp.h"
#include "../misc/rect.h"
#include "../misc/common.h"
#include "../misc/kigtransform.h"
#include "../misc/kigpainter.h"
#include "../kig/kig_view.h"
#include <klocale.h>
#include <cmath>
using namespace std;
AbstractLineImp::AbstractLineImp( const Coordinate& a, const Coordinate& b )
: mdata( a, b )
{
}
AbstractLineImp::~AbstractLineImp()
{
}
bool AbstractLineImp::inRect( const Rect& r, int width, const KigWidget& w ) const
{
return lineInRect( r, mdata.a, mdata.b, width, this, w );
}
const uint AbstractLineImp::numberOfProperties() const
{
return Parent::numberOfProperties() + 2;
}
const ObjectImpType* AbstractLineImp::impRequirementForProperty( uint which ) const
{
if ( which < Parent::numberOfProperties() )
return Parent::impRequirementForProperty( which );
else return AbstractLineImp::stype();
}
const char* AbstractLineImp::iconForProperty( uint which ) const
{
if ( which < Parent::numberOfProperties() )
return Parent::iconForProperty( which );
if ( which == Parent::numberOfProperties() )
return "slope"; // slope
if ( which == Parent::numberOfProperties() + 1 )
return "kig_text"; // equation
else assert( false );
return "";
}
ObjectImp* AbstractLineImp::property( uint which, const KigDocument& w ) const
{
if ( which < Parent::numberOfProperties() )
return Parent::property( which, w );
if ( which == Parent::numberOfProperties() )
return new DoubleImp( slope() );
if ( which == Parent::numberOfProperties() + 1 )
return new StringImp( equationString() );
else assert( false );
return new InvalidImp;
}
const QCStringList AbstractLineImp::propertiesInternalNames() const
{
QCStringList l = Parent::propertiesInternalNames();
l << "slope";
l << "equation";
assert( l.size() == AbstractLineImp::numberOfProperties() );
return l;
}
const QCStringList AbstractLineImp::properties() const
{
QCStringList l = Parent::properties();
l << I18N_NOOP( "Slope" );
l << I18N_NOOP( "Equation" );
assert( l.size() == AbstractLineImp::numberOfProperties() );
return l;
}
const uint SegmentImp::numberOfProperties() const
{
return Parent::numberOfProperties() + 4;
}
const QCStringList SegmentImp::propertiesInternalNames() const
{
QCStringList s = Parent::propertiesInternalNames();
s << "length";
s << "mid-point";
s << "end-point-A";
s << "end-point-B";
assert( s.size() == SegmentImp::numberOfProperties() );
return s;
}
const QCStringList SegmentImp::properties() const
{
QCStringList s = Parent::properties();
s << I18N_NOOP( "Length" );
s << I18N_NOOP( "Mid Point" );
s << I18N_NOOP( "First End Point" );
s << I18N_NOOP( "Second End Point" );
assert( s.size() == SegmentImp::numberOfProperties() );
return s;
}
const ObjectImpType* SegmentImp::impRequirementForProperty( uint which ) const
{
if ( which < Parent::numberOfProperties() )
return Parent::impRequirementForProperty( which );
else return SegmentImp::stype();
}
const char* SegmentImp::iconForProperty( uint which ) const
{
int pnum = 0;
if ( which < Parent::numberOfProperties() )
return Parent::iconForProperty( which );
else if ( which == Parent::numberOfProperties() + pnum++ )
return "distance"; // length
else if ( which == Parent::numberOfProperties() + pnum++ )
return "segment_midpoint"; // mid point
else if ( which == Parent::numberOfProperties() + pnum++ )
return "endpoint1"; // mid point
else if ( which == Parent::numberOfProperties() + pnum++ )
return "endpoint2"; // mid point
else assert( false );
return "";
}
ObjectImp* SegmentImp::property( uint which, const KigDocument& w ) const
{
int pnum = 0;
if ( which < Parent::numberOfProperties() )
return Parent::property( which, w );
else if ( which == Parent::numberOfProperties() + pnum++ )
return new DoubleImp( mdata.dir().length() );
else if ( which == Parent::numberOfProperties() + pnum++ )
return new PointImp( ( mdata.a + mdata.b ) / 2 );
else if ( which == Parent::numberOfProperties() + pnum++ )
return new PointImp( mdata.a );
else if ( which == Parent::numberOfProperties() + pnum++ )
return new PointImp( mdata.b );
else assert( false );
return new InvalidImp;
}
double AbstractLineImp::slope() const
{
Coordinate diff = mdata.dir();
return diff.y / diff.x;
}
const TQString AbstractLineImp::equationString() const
{
Coordinate p = mdata.a;
Coordinate q = mdata.b;
double m = ( q.y - p.y ) / ( q.x - p.x );
double r = - ( q.y - p.y ) * p.x / ( q.x - p.x ) + p.y;
TQString ret = TQString::fromUtf8( "y = %1x " ) +
TQString::fromUtf8( r > 0 ? "+" : "-" ) +
TQString::fromUtf8( " %2" );
ret = ret.arg( m, 0, 'g', 3 );
ret = ret.arg( abs( r ), 0, 'g', 3 );
return ret;
}
void SegmentImp::draw( KigPainter& p ) const
{
p.drawSegment( mdata );
}
bool SegmentImp::tqcontains( const Coordinate& p, int width, const KigWidget& w ) const
{
return internalContainsPoint( p, w.screenInfo().normalMiss( width ) );
}
void RayImp::draw( KigPainter& p ) const
{
p.drawRay( mdata );
}
bool RayImp::tqcontains( const Coordinate& p, int width, const KigWidget& w ) const
{
return internalContainsPoint( p, w.screenInfo().normalMiss( width ) );
}
void LineImp::draw( KigPainter& p ) const
{
p.drawLine( mdata );
}
bool LineImp::tqcontains( const Coordinate& p, int width, const KigWidget& w ) const
{
return internalContainsPoint( p, w.screenInfo().normalMiss( width ) );
}
SegmentImp::SegmentImp( const Coordinate& a, const Coordinate& b )
: AbstractLineImp( a, b )
{
}
RayImp::RayImp( const Coordinate& a, const Coordinate& b )
: AbstractLineImp( a, b )
{
}
LineImp::LineImp( const Coordinate& a, const Coordinate& b )
: AbstractLineImp( a, b )
{
}
SegmentImp* SegmentImp::copy() const
{
return new SegmentImp( mdata );
}
RayImp* RayImp::copy() const
{
return new RayImp( mdata );
}
LineImp* LineImp::copy() const
{
return new LineImp( mdata );
}
const Coordinate SegmentImp::getPoint( double param, const KigDocument& ) const
{
return mdata.a + mdata.dir()*param;
}
double SegmentImp::getParam( const Coordinate& p, const KigDocument& ) const
{
Coordinate pt = calcPointOnPerpend( data(), p );
pt = calcIntersectionPoint( data(), LineData( p, pt ) );
// if pt is over the end of the segment ( i.e. it's on the line
// which the segment is a part of, but not of the segment itself..;
// ) we set it to one of the end points of the segment...
if ((pt - mdata.a).length() > mdata.dir().length() )
pt = mdata.b;
else if ( (pt- mdata.b).length() > mdata.dir().length() )
pt = mdata.a;
if (mdata.b == mdata.a) return 0;
return ((pt - mdata.a).length())/(mdata.dir().length());
}
LineData AbstractLineImp::data() const
{
return mdata;
}
const Coordinate RayImp::getPoint( double param, const KigDocument& ) const
{
param = 1.0/param - 1.0;
return mdata.a + mdata.dir()*param;
}
double RayImp::getParam( const Coordinate& p, const KigDocument& ) const
{
const LineData ld = data();
Coordinate pt = calcPointOnPerpend( ld, p );
pt = calcIntersectionPoint( ld, LineData( p, pt ));
// if pt is over the end of the ray ( i.e. it's on the line
// which the ray is a part of, but not of the ray itself..;
// ) we set it to the start point of the ray...
Coordinate dir = ld.dir();
pt -= ld.a;
double param;
if ( dir.x != 0 ) param = pt.x / dir.x;
else if ( dir.y != 0 ) param = pt.y / dir.y;
else param = 0.;
if ( param < 0. ) param = 0.;
// mp: let's try with 1/(x+1), this reverses the mapping, but
// should allow to take advantage of the tightness of floating point
// numbers near zero, in order to get more possible positions near
// infinity
param = 1./( param + 1. );
assert( param >= 0. && param <= 1. );
return param;
}
const Coordinate LineImp::getPoint( double p, const KigDocument& ) const
{
// inspired upon KSeg
// we need to spread the points over the line, it should also come near
// the (infinite) end of the line, but most points should be near
// the two points we contain...
if ( p <= 0. ) p = 1e-6;
if ( p >= 1. ) p = 1 - 1e-6;
p = 2*p - 1;
if (p > 0) p = p/(1 - p);
else p = p/(1 + p);
// p *= 1024; // such multiplying factor could be useful in order to
// have more points near infinity, at the expense of
// points near ma and mb
return mdata.a + p*mdata.dir();
}
double LineImp::getParam( const Coordinate& point, const KigDocument& ) const
{
// somewhat the reverse of getPoint, although it also supports
// points not on the line...
Coordinate pa = point - mdata.a;
Coordinate ba = mdata.dir();
double balsq = ba.x*ba.x + ba.y*ba.y;
assert (balsq > 0);
double p = (pa.x*ba.x + pa.y*ba.y)/balsq;
// p /= 1024;
if (p > 0) p = p/(1+p);
else p = p/(1-p);
return 0.5*(p + 1);
}
ObjectImp* SegmentImp::transform( const Transformation& t ) const
{
if ( ! t.isAffine() ) /* we need to check the position of the two points */
{
if ( t.getProjectiveIndicator( mdata.a ) *
t.getProjectiveIndicator( mdata.b ) < 0 )
return new InvalidImp();
}
Coordinate na = t.apply( mdata.a );
Coordinate nb = t.apply( mdata.b );
if( na.valid() && nb.valid() ) return new SegmentImp( na, nb );
else return new InvalidImp();
}
ObjectImp* LineImp::transform( const Transformation& t ) const
{
Coordinate na = t.apply( mdata.a );
Coordinate nb = t.apply( mdata.b );
if ( na.valid() && nb.valid() ) return new LineImp( na, nb );
else return new InvalidImp();
}
ObjectImp* RayImp::transform( const Transformation& t ) const
{
if ( ! t.isAffine() ) /* we need to check the position of the two points */
{
double pa = t.getProjectiveIndicator( mdata.a );
double pb = t.getProjectiveIndicator( mdata.b );
if ( pa < 0 ) pb = -pb;
if ( pb < fabs (pa) ) return new InvalidImp();
Coordinate na = t.apply( mdata.a );
Coordinate nb = t.apply0( mdata.b - mdata.a );
if ( na.valid() && nb.valid() ) return new SegmentImp( na, nb );
else return new InvalidImp();
}
Coordinate na = t.apply( mdata.a );
Coordinate nb = t.apply( mdata.b );
if ( na.valid() && nb.valid() ) return new RayImp( na, nb );
else return new InvalidImp();
}
AbstractLineImp::AbstractLineImp( const LineData& d )
: mdata( d )
{
}
SegmentImp::SegmentImp( const LineData& d )
: AbstractLineImp( d )
{
}
RayImp::RayImp( const LineData& d )
: AbstractLineImp( d )
{
}
LineImp::LineImp( const LineData& d )
: AbstractLineImp( d )
{
}
double SegmentImp::length() const
{
return mdata.length();
}
void SegmentImp::visit( ObjectImpVisitor* vtor ) const
{
vtor->visit( this );
}
void RayImp::visit( ObjectImpVisitor* vtor ) const
{
vtor->visit( this );
}
void LineImp::visit( ObjectImpVisitor* vtor ) const
{
vtor->visit( this );
}
bool AbstractLineImp::equals( const ObjectImp& rhs ) const
{
return rhs.type() == type() &&
static_cast<const AbstractLineImp&>( rhs ).data() == data();
}
const ObjectImpType* AbstractLineImp::stype()
{
static const ObjectImpType t(
Parent::stype(), "line", I18N_NOOP( "line" ),
I18N_NOOP( "Select a Line" ), 0, 0, 0, 0, 0, 0, 0 );
return &t;
}
const ObjectImpType* LineImp::stype()
{
static const ObjectImpType t(
Parent::stype(), "line",
I18N_NOOP( "line" ),
I18N_NOOP( "Select this line" ),
I18N_NOOP( "Select line %1" ),
I18N_NOOP( "Remove a Line" ),
I18N_NOOP( "Add a Line" ),
I18N_NOOP( "Move a Line" ),
I18N_NOOP( "Attach to this line" ),
I18N_NOOP( "Show a Line" ),
I18N_NOOP( "Hide a Line" )
);
return &t;
}
const ObjectImpType* SegmentImp::stype()
{
static const ObjectImpType t(
Parent::stype(), "segment",
I18N_NOOP( "segment" ),
I18N_NOOP( "Select this segment" ),
I18N_NOOP( "Select segment %1" ),
I18N_NOOP( "Remove a Segment" ),
I18N_NOOP( "Add a Segment" ),
I18N_NOOP( "Move a Segment" ),
I18N_NOOP( "Attach to this segment" ),
I18N_NOOP( "Show a Segment" ),
I18N_NOOP( "Hide a Segment" )
);
return &t;
}
const ObjectImpType* RayImp::stype()
{
static const ObjectImpType t(
Parent::stype(), "ray",
I18N_NOOP( "half-line" ),
I18N_NOOP( "Select this half-line" ),
I18N_NOOP( "Select half-line %1" ),
I18N_NOOP( "Remove a Half-Line" ),
I18N_NOOP( "Add a Half-Line" ),
I18N_NOOP( "Move a Half-Line" ),
I18N_NOOP( "Attach to this half-line" ),
I18N_NOOP( "Show a Half-Line" ),
I18N_NOOP( "Hide a Half-Line" )
);
return &t;
}
const ObjectImpType* SegmentImp::type() const
{
return SegmentImp::stype();
}
const ObjectImpType* RayImp::type() const
{
return RayImp::stype();
}
const ObjectImpType* LineImp::type() const
{
return LineImp::stype();
}
bool SegmentImp::tqcontainsPoint( const Coordinate& p, const KigDocument& ) const
{
return internalContainsPoint( p, test_threshold );
}
bool SegmentImp::internalContainsPoint( const Coordinate& p, double threshold ) const
{
return isOnSegment( p, mdata.a, mdata.b, threshold );
}
bool RayImp::tqcontainsPoint( const Coordinate& p, const KigDocument& ) const
{
return internalContainsPoint( p, test_threshold );
}
bool RayImp::internalContainsPoint( const Coordinate& p, double threshold ) const
{
return isOnRay( p, mdata.a, mdata.b, threshold );
}
bool LineImp::tqcontainsPoint( const Coordinate& p, const KigDocument& ) const
{
return internalContainsPoint( p, test_threshold );
}
bool LineImp::internalContainsPoint( const Coordinate& p, double threshold ) const
{
return isOnLine( p, mdata.a, mdata.b, threshold );
}
bool AbstractLineImp::isPropertyDefinedOnOrThroughThisImp( uint which ) const
{
int pnum = 0;
if ( which < Parent::numberOfProperties() )
return Parent::isPropertyDefinedOnOrThroughThisImp( which );
else if ( which == Parent::numberOfProperties() + pnum++ )
return false;
else if ( which == Parent::numberOfProperties() + pnum++ )
return true;
else if ( which == Parent::numberOfProperties() + pnum++ )
return true;
else if ( which == Parent::numberOfProperties() + pnum++ )
return true;
else assert( false );
return false;
}
Rect SegmentImp::surroundingRect() const
{
return Rect( mdata.a, mdata.b );
}
Rect RayImp::surroundingRect() const
{
return Rect::invalidRect();
}
Rect LineImp::surroundingRect() const
{
return Rect::invalidRect();
}
|