File: CSG.cpp

package info (click to toggle)
darkradiant 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 41,080 kB
  • sloc: cpp: 264,743; ansic: 10,659; python: 1,852; xml: 1,650; sh: 92; makefile: 21
file content (227 lines) | stat: -rw-r--r-- 7,724 bytes parent folder | download | duplicates (4)
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
#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());
}

}