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
|
// Copyright (C) 2003 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 "locus_imp.h"
#include "bogus_imp.h"
#include "point_imp.h"
#include "../misc/object_hierarchy.h"
#include "../misc/kigpainter.h"
#include "../misc/coordinate.h"
#include "../misc/common.h"
#include "../kig/kig_view.h"
#include <tdelocale.h>
#include <cmath>
using namespace std;
static double cachedparam = 0.0;
LocusImp::~LocusImp()
{
delete mcurve;
}
ObjectImp* LocusImp::transform( const Transformation& t ) const
{
return new LocusImp( mcurve->copy(), mhier.transformFinalObject( t ) );
}
void LocusImp::draw( KigPainter& p ) const
{
p.drawCurve( this );
}
bool LocusImp::contains( const Coordinate& p, int width, const KigWidget& w ) const
{
return internalContainsPoint( p, w.screenInfo().normalMiss( width ), w.document() );
}
bool LocusImp::inRect( const Rect&, int, const KigWidget& ) const
{
// TODO ?
return false;
}
const Coordinate LocusImp::getPoint( double param, const KigDocument& doc ) const
{
Coordinate arg = mcurve->getPoint( param, doc );
if ( ! arg.valid() ) return arg;
PointImp argimp( arg );
Args args;
args.push_back( &argimp );
vector<ObjectImp*> calcret = mhier.calc( args, doc );
assert( calcret.size() == 1 );
ObjectImp* imp = calcret.front();
Coordinate ret;
if ( imp->inherits( PointImp::stype() ) )
{
cachedparam = param;
ret = static_cast<PointImp*>( imp )->coordinate();
}
else
ret = Coordinate::invalidCoord();
delete imp;
return ret;
}
LocusImp::LocusImp( CurveImp* curve, const ObjectHierarchy& hier )
: mcurve( curve ), mhier( hier )
{
}
const uint LocusImp::numberOfProperties() const
{
return Parent::numberOfProperties();
}
const QCStringList LocusImp::propertiesInternalNames() const
{
return Parent::propertiesInternalNames();
}
const QCStringList LocusImp::properties() const
{
return Parent::properties();
}
const ObjectImpType* LocusImp::impRequirementForProperty( uint which ) const
{
return Parent::impRequirementForProperty( which );
}
const char* LocusImp::iconForProperty( uint which ) const
{
return Parent::iconForProperty( which );
}
ObjectImp* LocusImp::property( uint which, const KigDocument& w ) const
{
return Parent::property( which, w );
}
LocusImp* LocusImp::copy() const
{
return new LocusImp( mcurve->copy(), mhier );
}
const CurveImp* LocusImp::curve() const
{
return mcurve;
}
const ObjectHierarchy& LocusImp::hierarchy() const
{
return mhier;
}
/**
* This function returns the distance between the point with parameter
* param and point p. param is allowed to not be between 0 and 1, in
* which case we consider only the decimal part.
*/
double LocusImp::getDist(double param, const Coordinate& p, const KigDocument& doc) const
{
param = fmod( param, 1 );
if( param < 0 ) param += 1.;
Coordinate p1 = getPoint( param, doc );
// i don't think the p1.valid() switch is really necessary, but I
// prefer to not take any chances :)
return p1.valid() ? ( p1 - p ).length() : +double_inf;
}
/**
* This function searches starting from x1 for the first interval in
* which the function of the distance from the point at coordinate x
* starts to increase. The range found is returned in the parameters
* x1 and x2: [x1,x2].
*/
void LocusImp::getInterval( double& x1, double& x2,
double incr,const Coordinate& p,
const KigDocument& doc) const
{
double mm = getDist( x1, p, doc);
double mm1 = getDist( x2, p, doc);
if( mm <= mm1 ) return;
else
{
double x3 = x2 + incr;
double mm2 = getDist (x3, p, doc);
while( mm > mm1 & mm1 > mm2 )
{
x1 = x2;
x2 = x3;
x3 = x2 + incr;
mm = mm1;
mm1 = mm2;
mm2 = getDist (x3, p, doc);
}
x2=x3;
}
}
double LocusImp::getParam( const Coordinate& p, const KigDocument& doc ) const
{
// this function ( and related functions like getInterval etc. ) is
// written by Franco Pasquarelli <pasqui@dmf.bs.unicatt.it>.
// I ( domi ) have adapted and documented it a bit.
if ( cachedparam >= 0. && cachedparam <= 1. &&
getPoint ( cachedparam, doc ) == p ) return cachedparam;
// consider the function that returns the distance for a point at
// parameter x to the locus for a given parameter x. What we do
// here is look for the global minimum of this function. We do that
// by dividing the range ( [0,1] ) into N parts, and start looking
// for a local minimum from there on. If we find one, we keep it if
// it is the lowest of all the ones we've already found..
const int N = 50;
const double incr = 1. / (double) N;
// xm is the best parameter we've found so far, fxm is the distance
// to the locus from that point. We start with some
// pseudo-values.
// (mp) note that if the distance is actually increasing in the
// whole interval [0,1] this value will be returned in the end.
double xm = 0.;
double fxm = getDist( xm, p, doc );
int j = 0;
double mm = fxm;
while( j < N )
{
// [x1,x2] is the range we're currently considering..
double x1 = j * incr;
double x2 = x1 + incr;
// check the range x1,x2 for the first local maximum..
double mm1 = getDist( x2, p, doc);
double mm2;
j++;
if( mm < mm1 )
mm = mm1;
else
{
if ( mm > mm1 )
{
double x3 = x2 + incr;
mm2 = getDist (x3, p, doc);
j++;
while( mm1 > mm2 & j <= N )
{
x1 = x2;
x2 = x3;
x3 = x2 + incr;
mm = mm1;
mm1 = mm2;
mm2 = getDist (x3, p, doc);
j++;
}
x2 = x3;
}
else
mm2 = mm1;
if ( mm1 <= mm2 )
{
mm = mm2;
double xm1 = getParamofmin( x1, x2, p, doc);
double fxm1 = getDist( xm1, p, doc );
if( fxm1 < fxm )
{
// we found a new minimum, save it..
xm=xm1;
fxm=fxm1;
}
}
}
}
return xm;
}
/**
* This function calculates the parameter of the point that realizes the
* minimum in [a,b] of the distance between the points of the locus and
* the point of coordinate p, using the golden ration method.
*/
double LocusImp::getParamofmin( double a, double b,
const Coordinate& p,
const KigDocument& doc ) const
{
double epsilons = 1.e-08;
double epsilonl = 2.e-02;
//assert( a < b && a >= 0. && b <= 1.0);
assert( a < b && a >= 0.);
double r2 = ( sqrt( 5. ) - 1 ) / 2.; // golden ratio
double r1 = 1. - r2;
double t2 = a + r2 * ( b - a );
double t1 = a + r1 * ( b - a );
Coordinate p1 = getPoint( fmod( t1, 1. ), doc);
double f1 = (p1 - p).length();
Coordinate p2 = getPoint( fmod( t2, 1. ), doc);
double f2 = (p2 - p).length();
double fmin, tmin;
if (f1 < f2)
{
b = t2;
fmin = f1;
tmin = t1;
}
else
{
a = t1;
fmin = f2;
tmin = t2;
}
while ( ( b - a ) > epsilons &&
( (p1 - p2).length() > 0.4 * fmin
|| (b - a) > epsilonl) &&
fmin > 1.e-8 )
{
if ( f1 < f2 )
{
t2 = t1;
t1 = a + r1*(b - a);
f2 = f1;
p1 = getPoint( fmod( t1, 1. ), doc);
f1 = (p1 - p).length();
}
else
{
t1 = t2;
t2 = a + r2*(b - a);
f1 = f2;
p2 = getPoint( fmod( t2, 1. ), doc);
f2 = (p2 - p).length();
}
if ( f1 < f2 )
{
b = t2;
fmin = f1;
tmin = t1;
}
else
{
a = t1;
fmin = f2;
tmin = t2;
}
}
return(tmin);
}
void LocusImp::visit( ObjectImpVisitor* vtor ) const
{
vtor->visit( this );
}
bool LocusImp::equals( const ObjectImp& rhs ) const
{
return rhs.inherits( LocusImp::stype() ) &&
static_cast<const LocusImp&>( rhs ).curve()->equals( *curve() ) &&
static_cast<const LocusImp&>( rhs ).hierarchy() == hierarchy();
}
const ObjectImpType* LocusImp::stype()
{
static const ObjectImpType t(
Parent::stype(), "locus",
I18N_NOOP( "locus" ),
I18N_NOOP( "Select this locus" ),
I18N_NOOP( "Select locus %1" ),
I18N_NOOP( "Remove a Locus" ),
I18N_NOOP( "Add a Locus" ),
I18N_NOOP( "Move a Locus" ),
I18N_NOOP( "Attach to this locus" ),
I18N_NOOP( "Show a Locus" ),
I18N_NOOP( "Hide a Locus" )
);
return &t;
}
const ObjectImpType* LocusImp::type() const
{
return LocusImp::stype();
}
bool LocusImp::containsPoint( const Coordinate& p, const KigDocument& doc ) const
{
return internalContainsPoint( p, test_threshold, doc );
}
bool LocusImp::internalContainsPoint( const Coordinate& p, double threshold, const KigDocument& doc ) const
{
double param = getParam( p, doc );
double dist = getDist( param, p, doc );
return fabs( dist ) <= threshold;
}
bool LocusImp::isPropertyDefinedOnOrThroughThisImp( uint which ) const
{
return Parent::isPropertyDefinedOnOrThroughThisImp( which );
}
Rect LocusImp::surroundingRect() const
{
// it's probably possible to calculate this, if it exists, but we
// don't for now.
return Rect::invalidRect();
}
|