-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathCSG.cpp
More file actions
344 lines (260 loc) · 11.8 KB
/
CSG.cpp
File metadata and controls
344 lines (260 loc) · 11.8 KB
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
#include "RadiantTest.h"
#include "imap.h"
#include "ibrush.h"
#include "entitylib.h"
#include "algorithm/Scene.h"
namespace test
{
using CsgTest = RadiantTest;
TEST_F(CsgTest, CSGMergeTwoRegularWorldspawnBrushes)
{
loadMap("csg_merge.map");
// Locate the first worldspawn brush
auto worldspawn = GlobalMapModule().getWorldspawn();
// Try to merge the two brushes with the "1" and "2" materials
auto firstBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto secondBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "2");
ASSERT_TRUE(Node_getIBrush(firstBrush)->getNumFaces() == 5);
ASSERT_TRUE(Node_getIBrush(secondBrush)->getNumFaces() == 5);
// Select the brushes and merge them
GlobalSelectionSystem().setSelectedAll(false);
Node_setSelected(firstBrush, true);
Node_setSelected(secondBrush, true);
// CSG merge
GlobalCommandSystem().executeCommand("CSGMerge");
// The two brushes should be gone, replaced by a new one
ASSERT_TRUE(firstBrush->getParent() == nullptr);
ASSERT_TRUE(secondBrush->getParent() == nullptr);
// The merged brush will carry both materials
auto brushWithMaterial1 = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto brushWithMaterial2 = algorithm::findFirstBrushWithMaterial(worldspawn, "2");
ASSERT_TRUE(brushWithMaterial1 == brushWithMaterial2);
ASSERT_TRUE(Node_getIBrush(brushWithMaterial1)->getNumFaces() == 6);
}
TEST_F(CsgTest, CSGMergeFourRegularWorldspawnBrushes)
{
loadMap("csg_merge.map");
// Locate the first worldspawn brush
auto worldspawn = GlobalMapModule().getWorldspawn();
// Try to merge the two brushes with the "1" and "2" materials
std::vector<scene::INodePtr> brushes = {
algorithm::findFirstBrushWithMaterial(worldspawn, "1"),
algorithm::findFirstBrushWithMaterial(worldspawn, "2"),
algorithm::findFirstBrushWithMaterial(worldspawn, "3"),
algorithm::findFirstBrushWithMaterial(worldspawn, "4")
};
// Check the correct setup
for (const auto& brush : brushes)
{
ASSERT_TRUE(Node_getIBrush(brush)->getNumFaces() == 5);
}
// Select the brushes and merge them
GlobalSelectionSystem().setSelectedAll(false);
for (const auto& brush : brushes)
{
Node_setSelected(brush, true);
}
// CSG merge
GlobalCommandSystem().executeCommand("CSGMerge");
// All brushes should be gone, replaced by a new one
for (const auto& brush : brushes)
{
ASSERT_TRUE(brush->getParent() == nullptr);
}
// The combined brush should be a 6-sided cuboid
auto brushWithMaterial1 = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
ASSERT_TRUE(Node_getIBrush(brushWithMaterial1)->getNumFaces() == 6);
}
TEST_F(CsgTest, CSGMergeTwoFuncStaticBrushes)
{
loadMap("csg_merge.map");
// Locate the func_static in the map
EntityNodeFindByClassnameWalker walker("func_static");
GlobalSceneGraph().root()->traverse(walker);
auto entity = walker.getEntityNode();
// Try to merge the two brushes with the "1" and "2" materials
auto firstBrush = algorithm::findFirstBrushWithMaterial(entity, "1");
auto secondBrush = algorithm::findFirstBrushWithMaterial(entity, "2");
ASSERT_TRUE(Node_getIBrush(firstBrush)->getNumFaces() == 5);
ASSERT_TRUE(Node_getIBrush(secondBrush)->getNumFaces() == 5);
// Select the brushes and merge them
GlobalSelectionSystem().setSelectedAll(false);
Node_setSelected(firstBrush, true);
Node_setSelected(secondBrush, true);
// CSG merge
GlobalCommandSystem().executeCommand("CSGMerge");
// The two brushes should be gone, replaced by a new one
ASSERT_TRUE(firstBrush->getParent() == nullptr);
ASSERT_TRUE(secondBrush->getParent() == nullptr);
// The merged brush will carry both materials
auto brushWithMaterial1 = algorithm::findFirstBrushWithMaterial(entity, "1");
auto brushWithMaterial2 = algorithm::findFirstBrushWithMaterial(entity, "2");
ASSERT_TRUE(brushWithMaterial1 == brushWithMaterial2);
ASSERT_TRUE(Node_getIBrush(brushWithMaterial1)->getNumFaces() == 6);
// They should still be children of the same entity
ASSERT_TRUE(brushWithMaterial1->getParent() == entity);
ASSERT_TRUE(brushWithMaterial2->getParent() == entity);
}
// #5344: Check that selecting a couple of brushes will only merge those
// which share the same parent entity
TEST_F(CsgTest, CSGMergeBrushesOfMixedEntitySelection)
{
loadMap("csg_merge.map");
// Locate the func_static in the map
EntityNodeFindByClassnameWalker walker("func_static");
GlobalSceneGraph().root()->traverse(walker);
auto entity = walker.getEntityNode();
auto worldspawn = GlobalMapModule().getWorldspawn();
// Select the mergeable brushes of both entities carrying the "1" and "2" materials
std::vector<scene::INodePtr> brushes = {
algorithm::findFirstBrushWithMaterial(entity, "1"),
algorithm::findFirstBrushWithMaterial(entity, "2"),
algorithm::findFirstBrushWithMaterial(worldspawn, "1"),
algorithm::findFirstBrushWithMaterial(worldspawn, "2")
};
// Check the correct setup
for (const auto& brush : brushes)
{
ASSERT_TRUE(Node_getIBrush(brush)->getNumFaces() == 5);
}
// Select the brushes and merge them
GlobalSelectionSystem().setSelectedAll(false);
for (const auto& brush : brushes)
{
Node_setSelected(brush, true);
}
// CSG merge
GlobalCommandSystem().executeCommand("CSGMerge");
// All brushes should be gone, replaced by TWO new ones
for (const auto& brush : brushes)
{
ASSERT_TRUE(brush->getParent() == nullptr);
}
// The merged brush will carry both materials
auto funcBrush1 = algorithm::findFirstBrushWithMaterial(entity, "1");
auto funcBrush2 = algorithm::findFirstBrushWithMaterial(entity, "2");
ASSERT_TRUE(funcBrush1);
ASSERT_TRUE(funcBrush1 == funcBrush2);
ASSERT_TRUE(Node_getIBrush(funcBrush1)->getNumFaces() == 6);
// Same for the worldspawn entity
auto worldBrush1 = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto worldBrush2 = algorithm::findFirstBrushWithMaterial(worldspawn, "2");
ASSERT_TRUE(worldBrush1);
ASSERT_TRUE(worldBrush1 == worldBrush2);
ASSERT_TRUE(Node_getIBrush(worldBrush1)->getNumFaces() == 6);
}
// Issue #5336: Crash when using CSG Merge on brushes that are part of worldspawn and a func_static
TEST_F(CsgTest, CSGMergeWithFuncStatic)
{
loadMap("csg_merge_with_func_static.map");
// Locate the first worldspawn brush
auto firstBrush = algorithm::getNthChild(GlobalMapModule().getWorldspawn(), 0);
ASSERT_TRUE(firstBrush);
// Locate the func_static in the map
EntityNodeFindByClassnameWalker walker("func_static");
GlobalSceneGraph().root()->traverse(walker);
auto entityNode = walker.getEntityNode();
ASSERT_TRUE(entityNode);
ASSERT_TRUE(entityNode->hasChildNodes());
// Select both of them, the order is important
Node_setSelected(firstBrush, true);
Node_setSelected(entityNode, true);
// CSG merge
GlobalCommandSystem().executeCommand("CSGMerge");
// No merge should have happened since the brushes
// are not part of the same entity
// So assume the scene didn't change
ASSERT_TRUE(algorithm::getNthChild(GlobalMapModule().getWorldspawn(), 0) == firstBrush);
EntityNodeFindByClassnameWalker walker2("func_static");
GlobalSceneGraph().root()->traverse(walker2);
ASSERT_TRUE(walker.getEntityNode());
ASSERT_TRUE(walker.getEntityNode()->hasChildNodes());
}
TEST_F(CsgTest, CSGIntersectTwoOverlappingBrushes)
{
loadMap("csg_intersect.map");
auto worldspawn = GlobalMapModule().getWorldspawn();
// Find the two overlapping brushes with materials "1" and "2"
auto firstBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto secondBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "2");
ASSERT_TRUE(firstBrush != nullptr);
ASSERT_TRUE(secondBrush != nullptr);
ASSERT_TRUE(Node_getIBrush(firstBrush)->getNumFaces() == 6);
ASSERT_TRUE(Node_getIBrush(secondBrush)->getNumFaces() == 6);
// Select the brushes and intersect them
GlobalSelectionSystem().setSelectedAll(false);
Node_setSelected(firstBrush, true);
Node_setSelected(secondBrush, true);
// CSG intersect
GlobalCommandSystem().executeCommand("CSGIntersect");
// The two brushes should be gone, replaced by a new one
ASSERT_TRUE(firstBrush->getParent() == nullptr);
ASSERT_TRUE(secondBrush->getParent() == nullptr);
// The intersection should have created a new brush
// It should have materials from the first brush (since we started with that)
auto resultBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
ASSERT_TRUE(resultBrush != nullptr);
// The result should be a valid 6-sided brush (the intersection of two cubes is a cube)
ASSERT_TRUE(Node_getIBrush(resultBrush)->getNumFaces() == 6);
}
TEST_F(CsgTest, CSGIntersectNonOverlappingBrushes)
{
loadMap("csg_intersect.map");
auto worldspawn = GlobalMapModule().getWorldspawn();
// Find brush "1" and the non-overlapping brush "3"
auto firstBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto nonOverlappingBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "3");
ASSERT_TRUE(firstBrush != nullptr);
ASSERT_TRUE(nonOverlappingBrush != nullptr);
// Select the brushes
GlobalSelectionSystem().setSelectedAll(false);
Node_setSelected(firstBrush, true);
Node_setSelected(nonOverlappingBrush, true);
// CSG intersect - should fail silently because brushes don't overlap
GlobalCommandSystem().executeCommand("CSGIntersect");
// The original brushes should still exist (operation failed, no changes)
ASSERT_TRUE(firstBrush->getParent() != nullptr);
ASSERT_TRUE(nonOverlappingBrush->getParent() != nullptr);
}
TEST_F(CsgTest, CSGIntersectContainedBrush)
{
loadMap("csg_intersect.map");
auto worldspawn = GlobalMapModule().getWorldspawn();
// Find brush "1" (large) and brush "4" (small, contained within "1")
auto largeBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
auto smallBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "4");
ASSERT_TRUE(largeBrush != nullptr);
ASSERT_TRUE(smallBrush != nullptr);
ASSERT_TRUE(Node_getIBrush(largeBrush)->getNumFaces() == 6);
ASSERT_TRUE(Node_getIBrush(smallBrush)->getNumFaces() == 6);
// Select the brushes
GlobalSelectionSystem().setSelectedAll(false);
Node_setSelected(largeBrush, true);
Node_setSelected(smallBrush, true);
// CSG intersect
GlobalCommandSystem().executeCommand("CSGIntersect");
// The two brushes should be gone
ASSERT_TRUE(largeBrush->getParent() == nullptr);
ASSERT_TRUE(smallBrush->getParent() == nullptr);
// The result should be a brush equal in size to the small brush
// The result will have material "4" since those faces define the intersection volume
auto resultBrush = algorithm::findFirstBrushWithMaterial(worldspawn, "4");
ASSERT_TRUE(resultBrush != nullptr);
ASSERT_TRUE(Node_getIBrush(resultBrush)->getNumFaces() == 6);
}
TEST_F(CsgTest, CSGIntersectRequiresTwoBrushes)
{
loadMap("csg_intersect.map");
auto worldspawn = GlobalMapModule().getWorldspawn();
// Find just one brush
auto brush = algorithm::findFirstBrushWithMaterial(worldspawn, "1");
ASSERT_TRUE(brush != nullptr);
// Select only one brush
GlobalSelectionSystem().setSelectedAll(false);
Node_setSelected(brush, true);
// CSG intersect - should fail silently because we need at least 2 brushes
GlobalCommandSystem().executeCommand("CSGIntersect");
// The original brush should still exist (operation failed, no changes)
ASSERT_TRUE(brush->getParent() != nullptr);
}
}