summaryrefslogtreecommitdiffstats
path: root/kig/objects/tests_type.cc
blob: e85c111eceac55b724cef66733c1eea996e8d09c (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
// Copyright (C)  2004  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 "tests_type.h"

#include "line_imp.h"
#include "polygon_imp.h"
#include "point_imp.h"
#include "bogus_imp.h"
#include "other_imp.h"

#include <cmath>

static const ArgsParser::spec argsspecAreParallel[] =
{
  { AbstractLineImp::stype(), I18N_NOOP( "Is this line parallel?" ),
    I18N_NOOP( "Select the first of the two possibly parallel lines..." ), false },
  { AbstractLineImp::stype(), I18N_NOOP( "Parallel to this line?" ),
    I18N_NOOP( "Select the other of the two possibly parallel lines..." ), false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( AreParallelType )

AreParallelType::AreParallelType()
  : ArgsParserObjectType( "AreParallel",
                         argsspecAreParallel, 2 )
{
}

AreParallelType::~AreParallelType()
{
}

const AreParallelType* AreParallelType::instance()
{
  static const AreParallelType t;
  return &t;
}

ObjectImp* AreParallelType::calc( const Args& parents, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( parents ) ) return new InvalidImp;
  const LineData& l1 = static_cast<const AbstractLineImp*>( parents[0] )->data();
  const LineData& l2 = static_cast<const AbstractLineImp*>( parents[1] )->data();

  if ( l1.isParallelTo( l2 ) )
    return new TestResultImp( i18n( "These lines are parallel." ) );
  else
    return new TestResultImp( i18n( "These lines are not parallel." ) );

}

const ObjectImpType* AreParallelType::resultId() const
{
  return TestResultImp::stype();
}

static const ArgsParser::spec argsspecAreOrthogonal[] =
{
  { AbstractLineImp::stype(), I18N_NOOP( "Is this line orthogonal?" ),
    I18N_NOOP( "Select the first of the two possibly orthogonal lines..." ), false },
  { AbstractLineImp::stype(), I18N_NOOP( "Orthogonal to this line?" ),
    I18N_NOOP( "Select the other of the two possibly orthogonal lines..." ), false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( AreOrthogonalType )

AreOrthogonalType::AreOrthogonalType()
  : ArgsParserObjectType( "AreOrthogonal",
                         argsspecAreOrthogonal, 2 )
{
}

AreOrthogonalType::~AreOrthogonalType()
{
}

const AreOrthogonalType* AreOrthogonalType::instance()
{
  static const AreOrthogonalType t;
  return &t;
}

ObjectImp* AreOrthogonalType::calc( const Args& parents, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( parents ) ) return new InvalidImp;
  const LineData& l1 = static_cast<const AbstractLineImp*>( parents[0] )->data();
  const LineData& l2 = static_cast<const AbstractLineImp*>( parents[1] )->data();

  if ( l1.isOrthogonalTo( l2 ) )
    return new TestResultImp( i18n( "These lines are orthogonal." ) );
  else
    return new TestResultImp( i18n( "These lines are not orthogonal." ) );

}

const ObjectImpType* AreOrthogonalType::resultId() const
{
  return TestResultImp::stype();
}

static const ArgsParser::spec argsspecAreCollinear[] =
{
  { PointImp::stype(), I18N_NOOP( "Check collinearity of this point" ),
    I18N_NOOP( "Select the first of the three possibly collinear points..." ), false },
  { PointImp::stype(), I18N_NOOP( "and this second point" ),
    I18N_NOOP( "Select the second of the three possibly collinear points..." ), false },
  { PointImp::stype(), I18N_NOOP( "with this third point" ),
    I18N_NOOP( "Select the last of the three possibly collinear points..." ), false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( AreCollinearType )

AreCollinearType::AreCollinearType()
  : ArgsParserObjectType( "AreCollinear",
                         argsspecAreCollinear, 3 )
{
}

AreCollinearType::~AreCollinearType()
{
}

const AreCollinearType* AreCollinearType::instance()
{
  static const AreCollinearType t;
  return &t;
}

ObjectImp* AreCollinearType::calc( const Args& parents, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( parents ) ) return new InvalidImp;
  const Coordinate& p1 = static_cast<const PointImp*>( parents[0] )->coordinate();
  const Coordinate& p2 = static_cast<const PointImp*>( parents[1] )->coordinate();
  const Coordinate& p3 = static_cast<const PointImp*>( parents[2] )->coordinate();

  if ( areCollinear( p1, p2, p3 ) )
    return new TestResultImp( i18n( "These points are collinear." ) );
  else
    return new TestResultImp( i18n( "These points are not collinear." ) );
}

const ObjectImpType* AreCollinearType::resultId() const
{
  return TestResultImp::stype();
}

static const ArgsParser::spec containsTestArgsSpec[] =
{
  { PointImp::stype(), I18N_NOOP( "Check whether this point is on a curve" ),
    I18N_NOOP( "Select the point you want to test..." ), false },
  { CurveImp::stype(), I18N_NOOP( "Check whether the point is on this curve" ),
    I18N_NOOP( "Select the curve that the point might be on..." ), false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( ContainsTestType )

ContainsTestType::ContainsTestType()
  : ArgsParserObjectType( "ContainsTest", containsTestArgsSpec, 2 )
{
}

ContainsTestType::~ContainsTestType()
{
}

const ContainsTestType* ContainsTestType::instance()
{
  static const ContainsTestType t;
  return &t;
}

ObjectImp* ContainsTestType::calc( const Args& parents, const KigDocument& doc ) const
{
  if ( ! margsparser.checkArgs( parents ) ) return new InvalidImp;
  const Coordinate& p = static_cast<const PointImp*>( parents[0] )->coordinate();
  const CurveImp* c = static_cast<const CurveImp*>( parents[1] );

  if ( c->containsPoint( p, doc ) )
    return new TestResultImp( i18n( "This curve contains the point." ) );
  else
    return new TestResultImp( i18n( "This curve does not contain the point." ) );
}

const ObjectImpType* ContainsTestType::resultId() const
{
  return TestResultImp::stype();
}

/*
 * containment test of a point in a polygon
 */

static const ArgsParser::spec InPolygonTestArgsSpec[] =
{
  { PointImp::stype(), I18N_NOOP( "Check whether this point is in a polygon" ),
    I18N_NOOP( "Select the point you want to test..." ), false },
  { PolygonImp::stype(), I18N_NOOP( "Check whether the point is in this polygon" ),
    I18N_NOOP( "Select the polygon that the point might be in..." ), false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( InPolygonTestType )

InPolygonTestType::InPolygonTestType()
  : ArgsParserObjectType( "InPolygonTest", InPolygonTestArgsSpec, 2 )
{
}

InPolygonTestType::~InPolygonTestType()
{
}

const InPolygonTestType* InPolygonTestType::instance()
{
  static const InPolygonTestType t;
  return &t;
}

ObjectImp* InPolygonTestType::calc( const Args& parents, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( parents ) ) return new InvalidImp;
  const Coordinate& p = static_cast<const PointImp*>( parents[0] )->coordinate();
  const PolygonImp* pol = static_cast<const PolygonImp*>( parents[1] );

  if ( pol->isInPolygon( p ) )
    return new TestResultImp( i18n( "This polygon contains the point." ) );
  else
    return new TestResultImp( i18n( "This polygon does not contain the point." ) );
}

const ObjectImpType* InPolygonTestType::resultId() const
{
  return TestResultImp::stype();
}

/*
 * test if a polygon is convex
 */

static const ArgsParser::spec ConvexPolygonTestArgsSpec[] =
{
  { PolygonImp::stype(), I18N_NOOP( "Check whether this polygon is convex" ),
    I18N_NOOP( "Select the polygon you want to test for convexity..." ), false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( ConvexPolygonTestType )

ConvexPolygonTestType::ConvexPolygonTestType()
  : ArgsParserObjectType( "ConvexPolygonTest", ConvexPolygonTestArgsSpec, 1 )
{
}

ConvexPolygonTestType::~ConvexPolygonTestType()
{
}

const ConvexPolygonTestType* ConvexPolygonTestType::instance()
{
  static const ConvexPolygonTestType t;
  return &t;
}

ObjectImp* ConvexPolygonTestType::calc( const Args& parents, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( parents ) ) return new InvalidImp;
  const PolygonImp* pol = static_cast<const PolygonImp*>( parents[0] );

  if ( pol->isConvex() )
    return new TestResultImp( i18n( "This polygon is convex." ) );
  else
    return new TestResultImp( i18n( "This polygon is not convex." ) );
}

const ObjectImpType* ConvexPolygonTestType::resultId() const
{
  return TestResultImp::stype();
}

/*
 * same distance test
 */

static const ArgsParser::spec argsspecSameDistanceType[] =
{
  { PointImp::stype(), I18N_NOOP( "Check if this point has the same distance" ),
    I18N_NOOP( "Select the point which might have the same distance from two other points..." ), false },
  { PointImp::stype(), I18N_NOOP( "from this point" ),
    I18N_NOOP( "Select the first of the two other points..." ), false },
  { PointImp::stype(), I18N_NOOP( "and from this second point" ),
    I18N_NOOP( "Select the other of the two other points..." ), false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( SameDistanceType )

SameDistanceType::SameDistanceType()
  : ArgsParserObjectType( "SameDistanceType", argsspecSameDistanceType, 3 )
{
}

SameDistanceType::~SameDistanceType()
{
}

const SameDistanceType* SameDistanceType::instance()
{
  static const SameDistanceType t;
  return &t;
}

ObjectImp* SameDistanceType::calc( const Args& parents, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( parents ) ) return new InvalidImp;
  const Coordinate& p1 = static_cast<const PointImp*>( parents[0] )->coordinate();
  const Coordinate& p2 = static_cast<const PointImp*>( parents[1] )->coordinate();
  const Coordinate& p3 = static_cast<const PointImp*>( parents[2] )->coordinate();

  if ( fabs( ( p1 - p2 ).length() - ( p1 - p3 ).length() ) < 10e-5  )
    return new TestResultImp( i18n( "The two distances are the same." ) );
  else
    return new TestResultImp( i18n( "The two distances are not the same." ) );
}

const ObjectImpType* SameDistanceType::resultId() const
{
  return TestResultImp::stype();
}

static const ArgsParser::spec vectorEqualityArgsSpec[] =
{
  { VectorImp::stype(), I18N_NOOP( "Check whether this vector is equal to another vector" ),
    I18N_NOOP( "Select the first of the two possibly equal vectors..." ), false },
  { VectorImp::stype(), I18N_NOOP( "Check whether this vector is equal to the other vector" ),
    I18N_NOOP( "Select the other of the two possibly equal vectors..." ), false }
};

KIG_INSTANTIATE_OBJECT_TYPE_INSTANCE( VectorEqualityTestType )

VectorEqualityTestType::VectorEqualityTestType()
  : ArgsParserObjectType( "VectorEquality", vectorEqualityArgsSpec, 2 )
{
}

VectorEqualityTestType::~VectorEqualityTestType()
{
}

const VectorEqualityTestType* VectorEqualityTestType::instance()
{
  static const VectorEqualityTestType t;
  return &t;
}

ObjectImp* VectorEqualityTestType::calc( const Args& parents, const KigDocument& ) const
{
  if ( ! margsparser.checkArgs( parents ) ) return new InvalidImp;
  const Coordinate& v1 = static_cast<const VectorImp*>( parents[0] )->dir();
  const Coordinate& v2 = static_cast<const VectorImp*>( parents[1] )->dir();

  if ( ( v1 - v2 ).length() < 10e-5  )
    return new TestResultImp( i18n( "The two vectors are the same." ) );
  else
    return new TestResultImp( i18n( "The two vectors are not the same." ) );
}

const ObjectImpType* VectorEqualityTestType::resultId() const
{
  return TestResultImp::stype();
}