src/magicWand.cc

changeset 823
1a2f593f0c02
parent 821
a67b1201942a
child 824
6add2126e7ff
equal deleted inserted replaced
821:a67b1201942a 823:1a2f593f0c02
1 /*
2 * LDForge: LDraw parts authoring CAD
3 * Copyright (C) 2013, 2014 Santeri 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 "main.h"
20 #include "magicWand.h"
21 #include "ldDocument.h"
22 #include "mainWindow.h"
23
24 MagicWand::MagicWand()
25 {
26 // Get vertex<->object data
27 for (LDObjectPtr obj : getCurrentDocument()->objects())
28 {
29 // Note: this deliberately only takes vertex-objects into account.
30 // The magic wand does not process subparts.
31 for (int i = 0; i < obj->numVertices(); ++i)
32 _vertices[obj->vertex (i)] << obj;
33 }
34 }
35
36 void MagicWand::fillBoundaries (LDObjectPtr obj, QVector<BoundaryType>& boundaries, QVector<LDObjectPtr>& candidates)
37 {
38 // All boundaries obviously share vertices with the object, therefore they're all in the list
39 // of candidates.
40 for (auto it = candidates.begin(); it != candidates.end(); ++it)
41 {
42 if ((*it)->type() != OBJ_Line || (*it)->vertex (0) == (*it)->vertex (1))
43 continue;
44
45 int matches = 0;
46
47 for (int i = 0; i < obj->numVertices(); ++i)
48 {
49 if (not eq (obj->vertex (i), (*it)->vertex (0), (*it)->vertex (1)))
50 continue;
51
52 if (++matches == 2)
53 {
54 // Boundary found. Add to boundaries list and get it off the candidates list.
55 boundaries.append (std::make_tuple ((*it)->vertex (0), (*it)->vertex (1)));
56 break;
57 }
58 }
59 }
60 }
61
62 void MagicWand::doMagic (LDObjectPtr obj, MagicWand::MagicType type)
63 {
64 if (obj == null)
65 {
66 if (type == Set)
67 {
68 getCurrentDocument()->clearSelection();
69 g_win->buildObjList();
70 }
71
72 return;
73 }
74
75 int matchesneeded = 0;
76 QVector<BoundaryType> boundaries;
77 LDObjectType objtype = obj->type();
78
79 if (type != InternalRecursion)
80 {
81 _selection.clear();
82 _selection.append (obj);
83 }
84
85 switch (obj->type())
86 {
87 case OBJ_Line:
88 case OBJ_CondLine:
89 matchesneeded = 1;
90 break;
91
92 case OBJ_Triangle:
93 case OBJ_Quad:
94 matchesneeded = 2;
95 break;
96
97 default:
98 return;
99 }
100
101 QVector<LDObjectPtr> candidates;
102
103 // Get the list of objects that touch this object, i.e. share a vertex
104 // with this.
105 for (int i = 0; i < obj->numVertices(); ++i)
106 candidates += _vertices[obj->vertex (i)];
107
108 removeDuplicates (candidates);
109
110 // If we're dealing with surfaces, get a list of boundaries.
111 if (matchesneeded > 1)
112 fillBoundaries (obj, boundaries, candidates);
113
114 for (LDObjectPtr candidate : candidates)
115 {
116 try
117 {
118 // If we're doing this on lines, we need exact type match. Surface types (quads and
119 // triangles) can be mixed. Also don't consider self a candidate, and don't consider
120 // objects we have already processed.
121 if ((candidate == obj) ||
122 (candidate->color() != obj->color()) ||
123 (_selection.contains (candidate)) ||
124 (matchesneeded == 1 && (candidate->type() != objtype)) ||
125 ((candidate->numVertices() > 2) ^ (matchesneeded == 2)))
126 {
127 throw 0;
128 }
129
130 // Now ensure the two objects share enough vertices.
131 QVector<Vertex> matches;
132
133 for (int i = 0; i < obj->numVertices(); ++i)
134 {
135 for (int j = 0; j < candidate->numVertices(); ++j)
136 {
137 if (obj->vertex(i) == candidate->vertex(j))
138 {
139 matches << obj->vertex(i);
140 break;
141 }
142 }
143 }
144
145 if (matches.size() < matchesneeded)
146 throw 0; // Not enough matches.
147
148 // Check if a boundary gets in between the objects.
149 for (auto boundary : boundaries)
150 {
151 if (eq (matches[0], std::get<0> (boundary), std::get<1> (boundary)) &&
152 eq (matches[1], std::get<0> (boundary), std::get<1> (boundary)))
153 {
154 throw 0;
155 }
156 }
157
158 _selection.append (candidate);
159 doMagic (candidate, InternalRecursion);
160 }
161 catch (int&)
162 {
163 continue;
164 }
165 }
166
167 switch (type)
168 {
169 case Set:
170 getCurrentDocument()->clearSelection();
171 case Additive:
172 for (LDObjectPtr obj : _selection)
173 obj->select();
174 break;
175
176 case Subtractive:
177 for (LDObjectPtr obj : _selection)
178 obj->deselect();
179 break;
180
181 case InternalRecursion:
182 break;
183 }
184
185 if (type != InternalRecursion)
186 g_win->buildObjList();
187 }

mercurial