/
CSG.cpp
502 lines (388 loc) · 12.3 KB
/
CSG.cpp
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#include "CSG.h"
#include <map>
#include "i18n.h"
#include "itextstream.h"
#include "iundo.h"
#include "igrid.h"
#include "iselection.h"
#include "scenelib.h"
#include "shaderlib.h"
#include "registry/registry.h"
#include "brush/Face.h"
#include "brush/Brush.h"
#include "brush/BrushNode.h"
#include "brush/BrushVisit.h"
#include "selection/algorithm/Primitives.h"
#include "messages/NotificationMessage.h"
#include "command/ExecutionNotPossible.h"
namespace brush
{
namespace algorithm
{
const std::string RKEY_EMIT_CSG_SUBTRACT_WARNING("user/ui/brush/emitCSGSubtractWarning");
void hollowBrush(const BrushNodePtr& sourceBrush, bool makeRoom)
{
// Hollow the brush using the current grid size
sourceBrush->getBrush().forEachFace([&] (Face& face)
{
if (!face.contributes())
{
return;
}
scene::INodePtr parent = sourceBrush->getParent();
scene::INodePtr newNode = GlobalBrushCreator().createBrush();
BrushNodePtr brushNode = std::dynamic_pointer_cast<BrushNode>(newNode);
assert(brushNode);
float offset = GlobalGrid().getGridSize();
if (makeRoom)
{
face.getPlane().offset(offset);
}
// Add the child to the same parent as the source brush
parent->addChildNode(brushNode);
// Move the child brushes to the same layer as their source
brushNode->assignToLayers(sourceBrush->getLayers());
// Copy all faces from the source brush
brushNode->getBrush().copy(sourceBrush->getBrush());
if (makeRoom)
{
face.getPlane().offset(-offset);
}
Node_setSelected(brushNode, true);
FacePtr newFace = brushNode->getBrush().addFace(face);
if (newFace != 0)
{
newFace->flipWinding();
if (!makeRoom)
{
newFace->getPlane().offset(offset);
}
newFace->planeChanged();
}
brushNode->getBrush().removeEmptyFaces();
});
// Now unselect and remove the source brush from the scene
scene::removeNodeFromParent(sourceBrush);
}
void hollowSelectedBrushes(const cmd::ArgumentList& args) {
UndoableCommand undo("hollowSelectedBrushes");
// Find all brushes
BrushPtrVector brushes = selection::algorithm::getSelectedBrushes();
// Cycle through the brushes and hollow them
// We assume that all these selected brushes are visible as well.
for (const BrushNodePtr& brush : brushes)
{
hollowBrush(brush, false);
}
SceneChangeNotify();
}
void makeRoomForSelectedBrushes(const cmd::ArgumentList& args) {
UndoableCommand undo("brushMakeRoom");
// Find all brushes
BrushPtrVector brushes = selection::algorithm::getSelectedBrushes();
// Cycle through the brushes and hollow them
// We assume that all these selected brushes are visible as well.
for (std::size_t i = 0; i < brushes.size(); ++i)
{
hollowBrush(brushes[i], true);
}
SceneChangeNotify();
}
// Returns true if fragments have been inserted into the given ret_fragments list
bool Brush_subtract(const BrushNodePtr& brush, const Brush& other, BrushPtrVector& ret_fragments)
{
if (brush->getBrush().localAABB().intersects(other.localAABB()))
{
BrushPtrVector fragments;
fragments.reserve(other.getNumFaces());
BrushNodePtr back = std::dynamic_pointer_cast<BrushNode>(brush->clone());
for (Brush::const_iterator i(other.begin()); i != other.end(); ++i)
{
const Face& face = *(*i);
if (!face.contributes()) continue;
BrushSplitType split = back->getBrush().classifyPlane(face.plane3());
if (split.counts[ePlaneFront] != 0 && split.counts[ePlaneBack] != 0)
{
fragments.push_back(std::dynamic_pointer_cast<BrushNode>(back->clone()));
FacePtr newFace = fragments.back()->getBrush().addFace(face);
if (newFace != 0)
{
newFace->flipWinding();
}
back->getBrush().addFace(face);
}
else if (split.counts[ePlaneBack] == 0)
{
return false;
}
}
ret_fragments.insert(ret_fragments.end(), fragments.begin(), fragments.end());
return true;
}
return false;
}
class SubtractBrushesFromUnselected :
public scene::NodeVisitor
{
const BrushPtrVector& _brushlist;
std::size_t& _before;
std::size_t& _after;
BrushPtrVector _unselectedBrushes;
public:
SubtractBrushesFromUnselected(const BrushPtrVector& brushlist, std::size_t& before, std::size_t& after) :
_brushlist(brushlist),
_before(before),
_after(after)
{}
bool pre(const scene::INodePtr& node) override
{
if (!node->visible())
{
return false;
}
if (Node_isBrush(node) && !Node_isSelected(node))
{
_unselectedBrushes.emplace_back(std::dynamic_pointer_cast<BrushNode>(node));
}
return true;
}
void processUnselectedBrushes()
{
for (const auto& node : _unselectedBrushes)
{
processNode(node);
}
}
private:
void processNode(const BrushNodePtr& brushNode)
{
// Get the parent of this brush
scene::INodePtr parent = brushNode->getParent();
assert(parent); // parent must not be NULL
BrushPtrVector buffer[2];
std::size_t swap = 0;
BrushNodePtr original = std::dynamic_pointer_cast<BrushNode>(brushNode->clone());
//Brush* original = new Brush(*brush);
buffer[swap].push_back(original);
// Iterate over all selected brushes
for (const auto& selectedBrush : _brushlist)
{
for (const auto& target : buffer[swap])
{
if (Brush_subtract(target, selectedBrush->getBrush(), buffer[1 - swap]))
{
// greebo: Delete not necessary, nodes get deleted automatically by clear() below
// delete (*j);
}
else
{
buffer[1 - swap].push_back(target);
}
}
buffer[swap].clear();
swap = 1 - swap;
}
BrushPtrVector& out = buffer[swap];
if (out.size() == 1 && out.back() == original)
{
// greebo: shared_ptr is taking care of this
//delete original;
}
else
{
_before++;
for (BrushPtrVector::const_iterator i = out.begin(); i != out.end(); ++i)
{
_after++;
scene::INodePtr newBrush = GlobalBrushCreator().createBrush();
parent->addChildNode(newBrush);
// Move the new Brush to the same layers as the source node
newBrush->assignToLayers(brushNode->getLayers());
(*i)->getBrush().removeEmptyFaces();
ASSERT_MESSAGE(!(*i)->getBrush().empty(), "brush left with no faces after subtract");
Node_getBrush(newBrush)->copy((*i)->getBrush());
}
scene::removeNodeFromParent(brushNode);
}
}
};
void subtractBrushesFromUnselected(const cmd::ArgumentList& args)
{
if (registry::getValue<bool>(RKEY_EMIT_CSG_SUBTRACT_WARNING))
{
radiant::NotificationMessage::SendInformation(
_("Note: be careful when using the CSG tool, as you might end up\n"
"with an unnecessary number of tiny brushes and/or leaks.\n"
"This popup will not be shown again."),
_("This Is Not Dromed Warning"));
// Disable this warning
registry::setValue(RKEY_EMIT_CSG_SUBTRACT_WARNING, false);
}
// Collect all selected brushes
BrushPtrVector brushes = selection::algorithm::getSelectedBrushes();
if (brushes.empty())
{
throw cmd::ExecutionNotPossible(_("CSG Subtract: No brushes selected."));
}
rMessage() << "CSG Subtract: Subtracting " << brushes.size() << " brushes.\n";
UndoableCommand undo("brushSubtract");
// subtract selected from unselected
std::size_t before = 0;
std::size_t after = 0;
SubtractBrushesFromUnselected walker(brushes, before, after);
GlobalSceneGraph().root()->traverse(walker);
walker.processUnselectedBrushes();
rMessage() << "CSG Subtract: Result: "
<< after << " fragment" << (after == 1 ? "" : "s")
<< " from " << before << " brush" << (before == 1 ? "" : "es") << ".\n";
SceneChangeNotify();
}
// greebo: TODO: Make this a member method of the Brush class
bool Brush_merge(Brush& brush, const BrushPtrVector& in, bool onlyshape) {
// gather potential outer faces
typedef std::vector<const Face*> FaceList;
FaceList faces;
for (BrushPtrVector::const_iterator i(in.begin()); i != in.end(); ++i) {
(*i)->getBrush().evaluateBRep();
for (Brush::const_iterator j((*i)->getBrush().begin()); j != (*i)->getBrush().end(); ++j) {
if (!(*j)->contributes()) {
continue;
}
const Face& face1 = *(*j);
bool skip = false;
// test faces of all input brushes
//!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
for (BrushPtrVector::const_iterator k(in.begin()); !skip && k != in.end(); ++k) {
if (k != i) { // don't test a brush against itself
for (Brush::const_iterator l((*k)->getBrush().begin()); !skip && l != (*k)->getBrush().end(); ++l) {
const Face& face2 = *(*l);
// face opposes another face
if (face1.plane3() == -face2.plane3()) {
// skip opposing planes
skip = true;
break;
}
}
}
}
// check faces already stored
for (FaceList::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m) {
const Face& face2 = *(*m);
// face equals another face
if (face1.plane3() == face2.plane3()) {
// if the texture/shader references should be the same but are not
if (!onlyshape && !shader_equal(
face1.getFaceShader().getMaterialName(),
face2.getFaceShader().getMaterialName()
))
{
return false;
}
// skip duplicate planes
skip = true;
break;
}
// face1 plane intersects face2 winding or vice versa
if (Winding::planesConcave(face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3())) {
// result would not be convex
return false;
}
}
if (!skip) {
faces.push_back(&face1);
}
}
}
for (FaceList::const_iterator i = faces.begin(); i != faces.end(); ++i) {
if (!brush.addFace(*(*i))) {
// result would have too many sides
return false;
}
}
brush.removeEmptyFaces();
return true;
}
void mergeSelectedBrushes(const cmd::ArgumentList& args)
{
// Get the current selection
auto brushes = selection::algorithm::getSelectedBrushes();
if (brushes.empty())
{
throw cmd::ExecutionNotPossible(_("CSG Merge: No brushes selected."));
}
// Group the brushes by their parents
std::map<scene::INodePtr, BrushPtrVector> brushesByEntity;
for (const auto& brushNode : brushes)
{
auto parent = brushNode->getParent();
if (brushesByEntity.find(parent) == brushesByEntity.end())
{
brushesByEntity[parent] = BrushPtrVector();
}
brushesByEntity[parent].emplace_back(brushNode);
}
bool selectionIsSuitable = false;
// At least one group should have more than two members
for (const auto& pair : brushesByEntity)
{
if (pair.second.size() >= 2)
{
selectionIsSuitable = true;
break;
}
}
if (!selectionIsSuitable)
{
throw cmd::ExecutionNotPossible(_("CSG Merge: At least two brushes sharing of the same entity have to be selected."));
}
UndoableCommand undo("mergeSelectedBrushes");
bool anythingMerged = false;
for (const auto& pair : brushesByEntity)
{
if (pair.second.size() < 2)
{
continue;
}
// Take the last selected node as reference for layers and parent
auto lastBrush = pair.second.back();
auto parent = lastBrush->getParent();
assert(Node_isEntity(parent));
// Create a new BrushNode
auto newBrush = GlobalBrushCreator().createBrush();
// Insert the newly created brush into the same parent entity
parent->addChildNode(newBrush);
// Move the new brush to the same layers as the merged one
newBrush->assignToLayers(lastBrush->getLayers());
// Get the contained brush
Brush* brush = Node_getBrush(newBrush);
// Attempt to merge the selected brushes into the new one
if (!Brush_merge(*brush, pair.second, true))
{
continue;
}
anythingMerged = true;
ASSERT_MESSAGE(!brush->empty(), "brush left with no faces after merge");
// Remove the original brushes
for (const auto& brush : pair.second)
{
scene::removeNodeFromParent(brush);
}
// Select the new brush
Node_setSelected(newBrush, true);
}
if (!anythingMerged)
{
throw cmd::ExecutionFailure(_("CSG Merge: Failed - result would not be convex"));
}
rMessage() << "CSG Merge: Succeeded." << std::endl;
SceneChangeNotify();
}
void registerCommands()
{
GlobalCommandSystem().addCommand("CSGSubtract", subtractBrushesFromUnselected);
GlobalCommandSystem().addCommand("CSGMerge", mergeSelectedBrushes);
GlobalCommandSystem().addCommand("CSGHollow", hollowSelectedBrushes);
GlobalCommandSystem().addCommand("CSGRoom", makeRoomForSelectedBrushes);
}
} // namespace algorithm
} // namespace brush