summaryrefslogtreecommitdiffstats
path: root/kig/objects/inversion_type.cpp
blob: 4f27058265b47a930371307cb035c129febadfec (plain)
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
// Copyright (C)  2005  Maurizio Paolini <paolini@dmf.unicatt.it>

// 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 "inversion_type.h"
#include "point_imp.h"
#include "line_imp.h"
#include "circle_imp.h"
#include "other_imp.h"
#include "bogus_imp.h"

#include "../misc/common.h"

#include <tdelocale.h>

static const char str1[] = I18N_NOOP( "Invert with respect to this circle" );
static const char str2[] = I18N_NOOP( "Select the circle we want to invert against..." );

static const ArgsParser::spec argsspecInvertPoint[] =
{
  { PointImp::stype(), I18N_NOOP( "Compute the inversion of this point" ),
    I18N_NOOP( "Select the point to invert..." ), false },
  { CircleImp::stype(), str1, str2, false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( InvertPointType )

InvertPointType::InvertPointType()
  : ArgsParserObjectType( "InvertPoint", argsspecInvertPoint, 2 )
{
}

InvertPointType::~InvertPointType()
{
}

const InvertPointType* InvertPointType::instance()
{
  static const InvertPointType s;
  return &s;
}

const ObjectImpType* InvertPointType::resultId() const
{
  return PointImp::stype();
}

ObjectImp* InvertPointType::calc( const Args& args, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;

  const CircleImp* c = static_cast<const CircleImp*>( args[1] );
  Coordinate center = c->center();
  Coordinate relp = static_cast<const PointImp*>( args[0] )->coordinate() - center;
  double radiussq = c->squareRadius();
  double normsq = relp.x*relp.x + relp.y*relp.y;
  if ( normsq == 0 ) return new InvalidImp;
  return new PointImp( center + (radiussq/normsq)*relp );
}

/*
 * inversion of a line
 */

static const ArgsParser::spec argsspecInvertLine[] =
{
  { LineImp::stype(), I18N_NOOP( "Compute the inversion of this line" ),
    I18N_NOOP( "Select the line to invert..." ), false },
  { CircleImp::stype(), str1, str2, false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( InvertLineType )

InvertLineType::InvertLineType()
  : ArgsParserObjectType( "InvertLine", argsspecInvertLine, 2 )
{
}

InvertLineType::~InvertLineType()
{
}

const InvertLineType* InvertLineType::instance()
{
  static const InvertLineType s;
  return &s;
}

const ObjectImpType* InvertLineType::resultId() const
{
  return CircleImp::stype();
}

ObjectImp* InvertLineType::calc( const Args& args, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;

  const CircleImp* c = static_cast<const CircleImp*>( args[1] );
  Coordinate center = c->center();
  double radiussq = c->squareRadius();
  const LineData line = static_cast<const AbstractLineImp*>( args[0] )->data();
  Coordinate relb = line.b - center;
  Coordinate ab = line.b - line.a;
  double t = (relb.x*ab.x + relb.y*ab.y)/(ab.x*ab.x + ab.y*ab.y);
  Coordinate relh = relb - t*ab;
  double normhsq = relh.x*relh.x + relh.y*relh.y;
  if ( normhsq < 1e-12*radiussq ) return new LineImp( line.a, line.b );
  Coordinate newcenter = center + 0.5*radiussq/normhsq*relh;
  double newradius = 0.5*radiussq/sqrt(normhsq);

  return new CircleImp( newcenter, newradius );
}

/*
 * inversion of a segment
 */

static const ArgsParser::spec argsspecInvertSegment[] =
{
  { SegmentImp::stype(), I18N_NOOP( "Compute the inversion of this segment" ),
    I18N_NOOP( "Select the segment to invert..." ), false },
  { CircleImp::stype(), str1, str2, false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( InvertSegmentType )

InvertSegmentType::InvertSegmentType()
  : ArgsParserObjectType( "InvertSegment", argsspecInvertSegment, 2 )
{
}

InvertSegmentType::~InvertSegmentType()
{
}

const InvertSegmentType* InvertSegmentType::instance()
{
  static const InvertSegmentType s;
  return &s;
}

const ObjectImpType* InvertSegmentType::resultId() const
{
  return ArcImp::stype();
}

ObjectImp* InvertSegmentType::calc( const Args& args, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;

  const CircleImp* c = static_cast<const CircleImp*>( args[1] );
  Coordinate center = c->center();
  double radiussq = c->squareRadius();
  const LineData line = static_cast<const AbstractLineImp*>( args[0] )->data();
  Coordinate rela = line.a - center;
  Coordinate relb = line.b - center;
  Coordinate ab = relb - rela;
  double t = (relb.x*ab.x + relb.y*ab.y)/(ab.x*ab.x + ab.y*ab.y);
  Coordinate relh = relb - t*ab;
  double normhsq = relh.x*relh.x + relh.y*relh.y;

  /*
   * compute the inversion of the two endpoints
   */

  Coordinate newcenterrel = 0.5*radiussq/normhsq*relh;
  Coordinate relainv = radiussq/rela.squareLength() * rela;
  Coordinate relbinv = radiussq/relb.squareLength() * relb;

  if ( normhsq < 1e-12*radiussq )
  {
    if ( rela.squareLength() < 1e-12 )
    {
      return new RayImp( relbinv + center, 2*relbinv + center );
    }
    if ( relb.squareLength() < 1e-12 )
    {
      return new RayImp( relainv + center, 2*relainv + center );
    }
    if ( relb.x*rela.x + relb.y*rela.y > 0 )
    {
      return new SegmentImp( relainv + center, relbinv + center );
    }
    return new InvalidImp();
  }
  double newradius = 0.5*radiussq/sqrt(normhsq);

  relainv -= newcenterrel;
  relbinv -= newcenterrel;
  double angle1 = atan2( relainv.y, relainv.x );
  double angle2 = atan2( relbinv.y, relbinv.x );
  double angle = angle2 - angle1;
  if ( ab.x*rela.y - ab.y*rela.x > 0 )
  {
    angle1 = angle2;
    angle = -angle;
  }
  while ( angle1 < 0 ) angle1 += 2*M_PI;
  while ( angle1 >= 2*M_PI ) angle1 -= 2*M_PI;
  while ( angle < 0 ) angle += 2*M_PI;
  while ( angle >= 2*M_PI ) angle -= 2*M_PI;
  return new ArcImp( newcenterrel + center, newradius, angle1, angle );
}

/*
 * inversion of a circle
 */

static const ArgsParser::spec argsspecInvertCircle[] =
{
  { CircleImp::stype(), I18N_NOOP( "Compute the inversion of this circle" ),
    I18N_NOOP( "Select the circle to invert..." ), false },
  { CircleImp::stype(), str1, str2, false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( InvertCircleType )

InvertCircleType::InvertCircleType()
  : ArgsParserObjectType( "InvertCircle", argsspecInvertCircle, 2 )
{
}

InvertCircleType::~InvertCircleType()
{
}

const InvertCircleType* InvertCircleType::instance()
{
  static const InvertCircleType s;
  return &s;
}

const ObjectImpType* InvertCircleType::resultId() const
{
  return CircleImp::stype();
}

ObjectImp* InvertCircleType::calc( const Args& args, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;

  const CircleImp* refcircle = static_cast<const CircleImp*>( args[1] );
  Coordinate refc = refcircle->center();
  double refrsq = refcircle->squareRadius();
  const CircleImp* circle = static_cast<const CircleImp*>( args[0] );
  Coordinate c = circle->center() - refc;
  double clength = c.length();
  Coordinate cnorm = Coordinate (1.,0.);
  if ( clength != 0.0 ) cnorm = c/clength;
  double r = circle->radius();
  Coordinate tc = r*cnorm;
  Coordinate b = c + tc;   //(1 + t)*c;
  double bsq = b.x*b.x + b.y*b.y;
  Coordinate bprime = refrsq*b/bsq;
  if ( std::fabs( clength - r ) < 1e-6*clength )       // circle through origin -> line
  {
    return new LineImp( bprime+refc, bprime+refc+Coordinate( -c.y, c.x ) );
  }

  Coordinate a = c - tc;
  double asq = a.x*a.x + a.y*a.y;
  Coordinate aprime = refrsq*a/asq;

  Coordinate cprime = 0.5*(aprime + bprime);
  double rprime = 0.5*( bprime - aprime ).length();

  return new CircleImp( cprime + refc, rprime );
}

/*
 * inversion of an arc
 */

static const ArgsParser::spec argsspecInvertArc[] =
{
  { ArcImp::stype(), I18N_NOOP( "Compute the inversion of this arc" ),
    I18N_NOOP( "Select the arc to invert..." ), false },
  { CircleImp::stype(), str1, str2, false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( InvertArcType )

InvertArcType::InvertArcType()
  : ArgsParserObjectType( "InvertArc", argsspecInvertArc, 2 )
{
}

InvertArcType::~InvertArcType()
{
}

const InvertArcType* InvertArcType::instance()
{
  static const InvertArcType s;
  return &s;
}

const ObjectImpType* InvertArcType::resultId() const
{
  return ArcImp::stype();
}

ObjectImp* InvertArcType::calc( const Args& args, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( args ) ) return new InvalidImp;

  const CircleImp* refcircle = static_cast<const CircleImp*>( args[1] );
  Coordinate refc = refcircle->center();
  double refrsq = refcircle->squareRadius();
  const ArcImp* arc = static_cast<const ArcImp*>( args[0] );
  Coordinate c = arc->center() - refc;
  double clength = c.length();
  Coordinate cnorm = Coordinate (1.,0.);
  if ( clength != 0.0 ) cnorm = c/clength;
  double r = arc->radius();
  /*
   * r > clength means center of inversion circle inside of circle supporting arc
   */
  Coordinate tc = r*cnorm;
  Coordinate b = c + tc;
  double bsq = b.x*b.x + b.y*b.y;
  Coordinate bprime = refrsq*b/bsq;
  if ( std::fabs( clength - r ) < 1e-6*clength )  // support circle through origin -> 
                                                  // segment, ray or invalid
                                                  // (reversed segment, union of two rays)
  {
    bool valid1 = false;
    bool valid2 = false;
    Coordinate ep1 = arc->firstEndPoint() - refc;
    Coordinate ep2 = arc->secondEndPoint() - refc;
    Coordinate ep1inv = Coordinate::invalidCoord();
    Coordinate ep2inv = Coordinate::invalidCoord();
    double ep1sq = ep1.squareLength();
    if ( ep1sq > 1e-12 )
    {
      valid1 = true;
      ep1inv = refrsq/ep1sq * ep1;
    }
    Coordinate rayendp = ep1inv;
    int sign = 1;
    double ep2sq = ep2.squareLength();
    if ( ep2sq > 1e-12 )
    {
      valid2 = true;
      ep2inv = refrsq/ep2sq * ep2;
      rayendp = ep2inv;
      sign = -1;
    }
    if ( valid1 || valid2 )
    {
      if ( valid1 && valid2 )
      {
        // this gives either a segment or the complement of a segment (relative
        // to its support line).  We return a segment in any case (fixme)
        double ang = atan2( -c.y, -c.x );
        double sa = arc->startAngle();
        if ( ang < sa ) ang += 2*M_PI;
        if ( ang - sa - arc->angle() < 0 ) return new InvalidImp();
        return new SegmentImp( ep1inv + refc, ep2inv + refc );
      } else
        return new RayImp ( rayendp + refc, 
                 rayendp + refc + sign*Coordinate( -c.y, c.x ) );  // this should give a Ray
    } else
      return new LineImp( bprime+refc, bprime+refc+Coordinate( -c.y, c.x ) );
  }

  Coordinate a = c - tc;
  double asq = a.x*a.x + a.y*a.y;
  Coordinate aprime = refrsq*a/asq;

  Coordinate cprime = 0.5*(aprime + bprime);
  double rprime = 0.5*( bprime - aprime ).length();

  Coordinate ep1 = arc->firstEndPoint() - refc;
  double ang1 = arc->startAngle();
  double newstartangle = 2*atan2(ep1.y,ep1.x) - ang1;
  Coordinate ep2 = arc->secondEndPoint() - refc;
  double ang2 = ang1 + arc->angle();
  double newendangle = 2*atan2(ep2.y,ep2.x) - ang2;
  double newangle = newendangle - newstartangle;

  /*
   * newstartangle and newendangle might have to be exchanged:
   * this is the case if the circle supporting our arc does not
   * contain the center of the inversion circle
   */
  if ( r < clength )
  {
    newstartangle = newendangle - M_PI;
    newangle = - newangle;
    // newendangle is no-longer valid
  }
  while ( newstartangle < 0 ) newstartangle += 2*M_PI;
  while ( newstartangle >= 2*M_PI ) newstartangle -= 2*M_PI;
  while ( newangle < 0 ) newangle += 2*M_PI;
  while ( newangle >= 2*M_PI ) newangle -= 2*M_PI;
  return new ArcImp( cprime + refc, rprime, newstartangle, newangle );
}