|
1 /* |
|
2 * LDForge: LDraw parts authoring CAD |
|
3 * Copyright (C) 2013 - 2017 Teemu Piippo |
|
4 * |
|
5 * This program is free software: you can redistribute it and/or modify |
|
6 * it under the terms of the GNU General Public License as published by |
|
7 * the Free Software Foundation, either version 3 of the License, or |
|
8 * (at your option) any later version. |
|
9 * |
|
10 * This program is distributed in the hope that it will be useful, |
|
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 * GNU General Public License for more details. |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License |
|
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
17 */ |
|
18 |
|
19 #include "linesegment.h" |
|
20 |
|
21 /* |
|
22 * Returns the vertices of this line segment as a QPair. |
|
23 */ |
|
24 QPair<Vertex, Vertex> LineSegment::toPair() const |
|
25 { |
|
26 return {this->v_1, this->v_2}; |
|
27 } |
|
28 |
|
29 /* |
|
30 * Possibly swaps the vertices of given line segment so that equivalent line segments become equal. |
|
31 */ |
|
32 LineSegment normalized(const LineSegment& segment) |
|
33 { |
|
34 LineSegment result = segment; |
|
35 |
|
36 if (result.v_2 < result.v_1) |
|
37 qSwap(result.v_1, result.v_2); |
|
38 |
|
39 return result; |
|
40 } |
|
41 |
|
42 /* |
|
43 * Overload of qHash for line segments. |
|
44 */ |
|
45 unsigned int qHash(const LineSegment& segment) |
|
46 { |
|
47 return qHash(normalized(segment).toPair()); |
|
48 } |
|
49 |
|
50 /* |
|
51 * Comparison operator definition to allow line segments to be used in QSets. |
|
52 */ |
|
53 bool operator<(const LineSegment& one, const LineSegment& other) |
|
54 { |
|
55 return normalized(one).toPair() < normalized(other).toPair(); |
|
56 } |
|
57 |
|
58 /* |
|
59 * Checks whether two line segments are equal. |
|
60 */ |
|
61 bool operator==(const LineSegment& one, const LineSegment& other) |
|
62 { |
|
63 return (one.v_1 == other.v_1 and one.v_2 == other.v_2) |
|
64 or (one.v_2 == other.v_1 and one.v_1 == other.v_2); |
|
65 } |