aboutsummaryrefslogtreecommitdiff
path: root/anglerange.cpp
blob: c62f760115a5cd0d90dc09e0f23f385db976e878 (plain) (blame)
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
/*
 * LatticeMatch calculator - class used for angles
 * Copyright (C) 2015 Andreas Grois
 *
 * 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.
 *
 *
 * A small class that deals with ranges of angles. Its members are two angles, maximum and minimum.
 * The main purpose of this class is to provide an easy way to determine the overlap of two ranges.
 * There is no support for disjoint ranges (yet).
 * Probably that would better be left for a separate class anyhow. Think: vector of anglerange objects
 *
 * There is an important convention with this class:
 * Ranges are always clockwise from the lower to the upper bound.
 * They include the lower and the upper bound, as this is required to be able to contain individual points.
 *
 * This class is part of the LatticeMatch program.
 *
 * To contact the author either use electronic mail: Andreas Grois <andreas.grois@jku.at>
 * or write to:
 * Andreas Grois, Institute for Semiconductor and SolidState physics, Johannes Kepler University,
 * Altenbergerstraße 69, 4040 Linz, AUSTRIA
 */

#include "anglerange.h"
#include <cmath>

anglerange::anglerange()
{
    lowerborder = angleclass(0.0);
    upperborder = angleclass(0.0);
    upperset = false;
    lowerset = false;
    fullcircle= false;
    sortby = SRT_SIZE;
}

anglerange::anglerange(const angleclass &lower, const angleclass &upper)
{
    lowerborder = lower;
    upperborder = upper;
    upperset = true;
    lowerset = true;
    fullcircle = false;
    sortby = SRT_SIZE;
}

bool anglerange::isempty() const
{
    return(!upperset || !lowerset);
}

angleclass anglerange::getupper() const
{
    return(upperborder);
}

angleclass anglerange::getlower() const
{
    return(lowerborder);
}

void anglerange::setupper(const angleclass &newupper)
{
    upperborder=newupper;
    upperset = true;
}

void anglerange::setlower(const angleclass &newlower)
{
    lowerborder=newlower;
    lowerset = true;
}

void anglerange::setempty()
{
    upperset = false;
    lowerset = false;
    fullcircle = false;
//    lowerborder = 0.0;
//    upperborder = 0.0;
//commented out, as users (I) should not rely on this.
}

void anglerange::setsorttype(const anglerange::sorttype value)
{
    sortby=value;
}

const anglerange::sorttype anglerange::getsorttype()
{
    return(sortby);
}

bool anglerange::iscircle() const
{
    return(fullcircle && !isempty());
}

void anglerange::setcircle(bool value)
{
    if(value)
    {
        lowerset=true;
        upperset=true;
        lowerborder=0.0;
        upperborder=0.0;
    }
    fullcircle=value;
}

bool anglerange::isinside(const angleclass &val) const
{
    if(isempty())
    {
        return(false);
    }
    else
    {
        if(fullcircle)
        {
            return(true);
        }
        else if(lowerborder>upperborder)
        {
            return((val>=lowerborder || val<=upperborder));
        }
        else
        {
            return(val>=lowerborder && val<=upperborder);
        }
    }
}

anglerange anglerange::overlap(const anglerange &other)
{
    //storage for the return value:
    anglerange retval; //anglerange constructor without arguments: emtpy range [0:0]
    retval.setsorttype(sortby); //return value inherits current sorttype.
    //first: if one of the input ranges is empty, don't do anything, leave retval empty!

    if(!(isempty()) && !(other.isempty()))
    {
        //special treatment if one of the ranges is a full circle:
        if(fullcircle)
        {
            retval = other;
            retval.setsorttype(sortby);
        }
        else if(other.fullcircle)
        {
            retval = *this;
        }
        //Here we need to take care to always follow the counter-clockwise convention.
        //This leaves us with four possibilities:
        //a) Both ranges contain the zero-line
        //b) This range contains the zero-line, the other doesn't
        //c) Vice versa
        //d) Both ranges are regular.

        //first check if this range contains the zero-line
        else if(lowerborder>upperborder)
        {
            //ok, this range is around zero. Let's check the other one too:
            if(other.getlower()>other.getupper())
            {
                //ok, both ranges contain the zero-line. This makes some things easier, as we know there's
                //an overlap
                retval.setlower(fmax(lowerborder.getval(),other.getlower().getval()));
                retval.setupper(fmin(upperborder.getval(),other.getupper().getval()));
            }
            else
            {
                //this contains zero, other doesn't.
                //This cannot fully lie within other, but other can lie fully within this.
                //The result does obviously not contain zero.
                //Let's check if other is within this.
                if(other.getupper()<=upperborder || other.getlower()>=lowerborder){
                    retval.setlower(other.getlower());
                    retval.setupper(other.getupper());
                }
                else if(other.getupper()>=lowerborder) //not >, as per definition upperborder is inside range
                {
                    retval.setlower(lowerborder);
                    retval.setupper(other.getupper());
                }
                else if(other.getlower()<=upperborder) //not <, as again upperborder is inside.
                {
                    retval.setlower(other.getlower());
                    retval.setupper(upperborder);
                }
            }
        }
        else
        {
            //this is regular, is other as well?
            if(other.getlower()>other.getupper())
            {
                //so, other cannot lie in this, but this can be in other.
                //Let's check for that.
                if(upperborder<=other.getupper() || lowerborder>=other.getlower()){
                    retval.setlower(lowerborder);
                    retval.setupper(upperborder);
                }
                else if(lowerborder <= other.getupper())
                {
                    retval.setlower(lowerborder);
                    retval.setupper(other.getupper());
                }
                else if(upperborder >= other.getlower())
                {
                    retval.setlower(other.getlower());
                    retval.setupper(upperborder);
                }
            }
            else //both ranges regular
            {
                double curmax = fmin(other.getupper().getval(),upperborder.getval());
                double curmin = fmax(other.getlower().getval(),lowerborder.getval());
                if(curmax>=curmin){
                    retval.setlower(curmin);
                    retval.setupper(curmax);
                }
            }
        }
    }
    return(retval);
}

anglerange anglerange::combine(const anglerange &other)
{
    anglerange retval;
    retval.setsorttype(sortby); //return value inherits current sorttype.
    //check if any of the ranges is empty. If yes: return an empty range as well.
    if(!(isempty()) && !(other.isempty())){
        //again: special treatment if one of the ranges is a full circle:
        if(fullcircle)
        {
            retval = *this;
        }
        if(other.fullcircle)
        {
            retval = other;
            retval.setsorttype(sortby);
        }
        //Here we need to take care to always follow the counter-clockwise convention.
        //This leaves us with four possibilities:
        //a) Both ranges contain the zero-line
        //b) This range contains the zero-line, the other doesn't
        //c) Vice versa
        //d) Both ranges are regular.

        //first check if this range contains the zero-line
        else if(lowerborder>upperborder)
        {
            //ok, this range is around zero. Let's check the other one too:
            if(other.getlower()>other.getupper())
            {
                //ok, both ranges contain the zero-line.
                //let's see if the result is a full circle
                if(lowerborder<=other.upperborder || other.lowerborder <=upperborder)
                {
                    //full circle
                    retval.setcircle(true);
                }
                else
                {
                    retval.setlower(fmin(lowerborder.getval(),other.getlower().getval()));
                    retval.setupper(fmax(upperborder.getval(),other.getupper().getval()));
                }
            }
            else
            {
                //this contains zero, other doesn't.
                //This cannot fully lie within other, but other can lie fully within this.
                //The result does obviously contain zero.
                //Let's check if other is within this.
                if(other.getupper()<=upperborder || other.getlower()>=lowerborder){
                    retval.setlower(lowerborder);
                    retval.setupper(upperborder);
                }
                //again: check if result is a full circle
                else if(other.lowerborder<=upperborder && other.upperborder>=lowerborder)
                {
                    retval.setcircle(true);
                }
                else if(other.getupper()>=lowerborder) //not >, as per definition upperborder is inside range
                {
                    retval.setlower(other.getlower());
                    retval.setupper(upperborder);
                }
                else if(other.getlower()<=upperborder) //not <, as again upperborder is inside.
                {
                    retval.setlower(lowerborder);
                    retval.setupper(other.getupper());
                }
            }
        }
        else
        {
            //this is regular, is other as well?
            if(other.getlower()>other.getupper())
            {
                //so, other cannot lie in this, but this can be in other.
                //Let's check for that.
                if(upperborder<=other.getupper() || lowerborder>=other.getlower()){
                    retval.setlower(other.getlower());
                    retval.setupper(other.getupper());
                }
                //full circle?
                else if(lowerborder <= other.getupper() && upperborder >= other.getlower())
                {
                    retval.setcircle(true);
                }
                else if(lowerborder <= other.getupper())
                {
                    retval.setlower(other.getlower());
                    retval.setupper(upperborder);
                }
                else if(upperborder >= other.getlower())
                {
                    retval.setlower(lowerborder);
                    retval.setupper(other.getupper());
                }
            }
            else //both ranges regular
            {
                //this also means: none of them contains zero, the result cannot be a full circle
                //check if there's an overlap
                double curmax = fmin(other.getupper().getval(),upperborder.getval());
                double curmin = fmax(other.getlower().getval(),lowerborder.getval());
                if(curmax>=curmin){
                    //there is an overlap - set limits
                    retval.setlower(fmin(other.getlower().getval(),lowerborder.getval()));
                    retval.setupper(fmax(other.getupper().getval(),upperborder.getval()));
                }
            }
        }
    }
    return(retval);
}

bool anglerange::operator<(const anglerange &other) const
{
    if(isempty() || other.isempty())
    {
        return(false);
    }
    else
    {
        switch(sortby)
        {
        case SRT_LOWER:
            return(lowerborder < other.lowerborder);
            break;
        case SRT_UPPER:
            return(upperborder < other.upperborder);
            break;
        case SRT_SIZE:
            //I know that this if elseif thing can be written as a single statement. This is for readability.
            if(other.fullcircle)
            {
                return(!fullcircle);
            }
            else if(fullcircle)
            {
                return(false);
            }
            else
            {
                //angleclass takes care of zero crossing. C++ is awesome.
                return(upperborder - lowerborder < other.upperborder - other.lowerborder);
            }
            break;
        default:
            throw std::out_of_range("Invalid sort field set for anglerange! Go, debug.\n");
        }
    }
}

bool anglerange::operator>(const anglerange &other) const
{
    //as == doesn't care about sort order but fully compares, we have to implement two sort operators fully...
    if(isempty() || other.isempty())
    {
        return(false);
    }
    else
    {
        switch(sortby)
        {
        case SRT_LOWER:
            return(lowerborder > other.lowerborder);
            break;
        case SRT_UPPER:
            return(upperborder > other.upperborder);
            break;
        case SRT_SIZE:
            if(fullcircle)
            {
                return(!other.fullcircle);
            }
            else if(other.fullcircle)
            {
                return(false);
            }
            else
            {
                //angleclass takes care of zero crossing. C++ is awesome.
                return(upperborder - lowerborder > other.upperborder - other.lowerborder);
            }
            break;
        default:
            throw std::out_of_range("Invalid sort field set for anglerange! Go, debug.\n");
        }
    }
}

bool anglerange::operator<=(const anglerange &other) const
{
    if(isempty() || other.isempty())
    {
        return(false);
    }
    else
    {
        return(!(operator>(other)));
    }
}
bool anglerange::operator>=(const anglerange &other) const
{
    if(isempty() || other.isempty())
    {
        return(false);
    }
    else
    {
        return(!(operator<(other)));
    }
}


bool anglerange::operator==(const anglerange &other) const
{
    if(!(isempty()) && !(other.isempty()))
    {
        //both ranges set, we need to compare field by field
        return( (lowerborder == other.lowerborder) &&  (upperborder == other.upperborder) );
    }
    else
        return(isempty() && other.isempty());
}
bool anglerange::operator!=(const anglerange &other) const
{
    //needn't check for isempty here, as == already checks for it.
    return(!operator==(other));
}