2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "debugging/debugging.h"
29 #include "brushmanip.h"
30 #include "brushnode.h"
35 void Brush_makeHollow(const Brush& brush, brush_vector_t& out, float offset)
37 for(Brush::const_iterator i(brush.begin()); i != brush.end(); ++i)
39 if((*i)->contributes())
41 out.push_back(new Brush(brush));
42 Face* newFace = out.back()->addFace(*(*i));
45 newFace->flipWinding();
46 newFace->getPlane().offset(offset);
47 newFace->planeChanged();
53 class BrushHollowSelectedWalker : public scene::Graph::Walker
57 BrushHollowSelectedWalker(float offset)
61 bool pre(const scene::Path& path, scene::Instance& instance) const
63 if(path.top().get().visible())
65 Brush* brush = Node_getBrush(path.top());
67 && Instance_getSelectable(instance)->isSelected()
71 Brush_makeHollow(*brush, out, m_offset);
72 for(brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i)
74 NodeSmartReference node((new BrushNode())->node());
75 (*i)->removeEmptyFaces();
76 Node_getBrush(node)->copy(*(*i));
78 Node_getTraversable(path.parent())->insert(node);
86 typedef std::list<Brush*> brushlist_t;
88 class BrushGatherSelected : public scene::Graph::Walker
90 brush_vector_t& m_brushlist;
92 BrushGatherSelected(brush_vector_t& brushlist)
93 : m_brushlist(brushlist)
96 bool pre(const scene::Path& path, scene::Instance& instance) const
98 if(path.top().get().visible())
100 Brush* brush = Node_getBrush(path.top());
102 && Instance_getSelectable(instance)->isSelected())
104 m_brushlist.push_back(brush);
111 class BrushDeleteSelected : public scene::Graph::Walker
114 bool pre(const scene::Path& path, scene::Instance& instance) const
118 void post(const scene::Path& path, scene::Instance& instance) const
120 if(path.top().get().visible())
122 Brush* brush = Node_getBrush(path.top());
124 && Instance_getSelectable(instance)->isSelected()
127 Path_deleteTop(path);
133 void Scene_BrushMakeHollow_Selected(scene::Graph& graph)
135 GlobalSceneGraph().traverse(BrushHollowSelectedWalker(GetGridSize()));
136 GlobalSceneGraph().traverse(BrushDeleteSelected());
145 void CSG_MakeHollow (void)
147 UndoableCommand undo("brushHollow");
149 Scene_BrushMakeHollow_Selected(GlobalSceneGraph());
155 /// \brief Returns true if
156 /// \li !flipped && brush is BACK or ON
157 /// \li flipped && brush is FRONT or ON
158 bool Brush_testPlane(const Brush& brush, const Plane3& plane, bool flipped)
160 brush.evaluateBRep();
161 for(Brush::const_iterator i(brush.begin()); i != brush.end(); ++i)
163 if((*i)->contributes() && !Winding_TestPlane((*i)->getWinding(), plane, flipped))
171 brushsplit_t Brush_classifyPlane(const Brush& brush, const Plane3& plane)
173 brush.evaluateBRep();
175 for(Brush::const_iterator i(brush.begin()); i != brush.end(); ++i)
177 if((*i)->contributes())
179 split += Winding_ClassifyPlane((*i)->getWinding(), plane);
185 bool Brush_subtract(const Brush& brush, const Brush& other, brush_vector_t& ret_fragments)
187 if(aabb_intersects_aabb(brush.localAABB(), other.localAABB()))
189 brush_vector_t fragments;
190 fragments.reserve(other.size());
193 for(Brush::const_iterator i(other.begin()); i != other.end(); ++i)
195 if((*i)->contributes())
197 brushsplit_t split = Brush_classifyPlane(back, (*i)->plane3());
198 if(split.counts[ePlaneFront] != 0
199 && split.counts[ePlaneBack] != 0)
201 fragments.push_back(new Brush(back));
202 Face* newFace = fragments.back()->addFace(*(*i));
205 newFace->flipWinding();
209 else if(split.counts[ePlaneBack] == 0)
211 for(brush_vector_t::iterator i = fragments.begin(); i != fragments.end(); ++i)
219 ret_fragments.insert(ret_fragments.end(), fragments.begin(), fragments.end());
225 class SubtractBrushesFromUnselected : public scene::Graph::Walker
227 const brush_vector_t& m_brushlist;
228 std::size_t& m_before;
229 std::size_t& m_after;
231 SubtractBrushesFromUnselected(const brush_vector_t& brushlist, std::size_t& before, std::size_t& after)
232 : m_brushlist(brushlist), m_before(before), m_after(after)
235 bool pre(const scene::Path& path, scene::Instance& instance) const
239 void post(const scene::Path& path, scene::Instance& instance) const
241 if(path.top().get().visible())
243 Brush* brush = Node_getBrush(path.top());
245 && !Instance_getSelectable(instance)->isSelected())
247 brush_vector_t buffer[2];
249 Brush* original = new Brush(*brush);
250 buffer[static_cast<std::size_t>(swap)].push_back(original);
253 for(brush_vector_t::const_iterator i(m_brushlist.begin()); i != m_brushlist.end(); ++i)
255 for(brush_vector_t::iterator j(buffer[static_cast<std::size_t>(swap)].begin()); j != buffer[static_cast<std::size_t>(swap)].end(); ++j)
257 if(Brush_subtract(*(*j), *(*i), buffer[static_cast<std::size_t>(!swap)]))
263 buffer[static_cast<std::size_t>(!swap)].push_back((*j));
266 buffer[static_cast<std::size_t>(swap)].clear();
271 brush_vector_t& out = buffer[static_cast<std::size_t>(swap)];
273 if(out.size() == 1 && out.back() == original)
280 for(brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i)
283 NodeSmartReference node((new BrushNode())->node());
284 (*i)->removeEmptyFaces();
285 ASSERT_MESSAGE(!(*i)->empty(), "brush left with no faces after subtract");
286 Node_getBrush(node)->copy(*(*i));
288 Node_getTraversable(path.parent())->insert(node);
290 Path_deleteTop(path);
299 brush_vector_t selected_brushes;
300 GlobalSceneGraph().traverse(BrushGatherSelected(selected_brushes));
302 if (selected_brushes.empty())
304 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
308 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned(selected_brushes.size()) << " brushes.\n";
310 UndoableCommand undo("brushSubtract");
312 // subtract selected from unselected
313 std::size_t before = 0;
314 std::size_t after = 0;
315 GlobalSceneGraph().traverse(SubtractBrushesFromUnselected(selected_brushes, before, after));
316 globalOutputStream() << "CSG Subtract: Result: "
317 << Unsigned(after) << " fragment" << (after == 1 ? "" : "s")
318 << " from " << Unsigned(before) << " brush" << (before == 1? "" : "es") << ".\n";
324 class BrushSplitByPlaneSelected : public scene::Graph::Walker
329 const char* m_shader;
330 const TextureProjection& m_projection;
333 BrushSplitByPlaneSelected(const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split)
334 : m_p0(p0), m_p1(p1), m_p2(p2), m_shader(shader), m_projection(projection), m_split(split)
337 bool pre(const scene::Path& path, scene::Instance& instance) const
341 void post(const scene::Path& path, scene::Instance& instance) const
343 if(path.top().get().visible())
345 Brush* brush = Node_getBrush(path.top());
347 && Instance_getSelectable(instance)->isSelected())
349 Plane3 plane(plane3_for_points(m_p0, m_p1, m_p2));
350 if(plane3_valid(plane))
352 brushsplit_t split = Brush_classifyPlane(*brush, m_split == eFront ? plane3_flipped(plane) : plane);
353 if(split.counts[ePlaneBack] && split.counts[ePlaneFront])
355 // the plane intersects this brush
356 if(m_split == eFrontAndBack)
358 NodeSmartReference node((new BrushNode())->node());
359 Brush* fragment = Node_getBrush(node);
360 fragment->copy(*brush);
361 Face* newFace = fragment->addPlane(m_p0, m_p1, m_p2, m_shader, m_projection);
362 if(newFace != 0 && m_split != eFront)
364 newFace->flipWinding();
366 fragment->removeEmptyFaces();
367 ASSERT_MESSAGE(!fragment->empty(), "brush left with no faces after split");
369 Node_getTraversable(path.parent())->insert(node);
371 scene::Path fragmentPath = path;
372 fragmentPath.top() = makeReference(node.get());
373 selectPath(fragmentPath, true);
377 Face* newFace = brush->addPlane(m_p0, m_p1, m_p2, m_shader, m_projection);
378 if(newFace != 0 && m_split == eFront)
380 newFace->flipWinding();
382 brush->removeEmptyFaces();
383 ASSERT_MESSAGE(!brush->empty(), "brush left with no faces after split");
386 // the plane does not intersect this brush
387 if(m_split != eFrontAndBack && split.counts[ePlaneBack] != 0)
389 // the brush is "behind" the plane
390 Path_deleteTop(path);
398 void Scene_BrushSplitByPlane(scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split)
400 TextureProjection projection;
401 TexDef_Construct_Default(projection);
402 graph.traverse(BrushSplitByPlaneSelected(p0, p1, p2, shader, projection, split));
407 class BrushInstanceSetClipPlane : public scene::Graph::Walker
411 BrushInstanceSetClipPlane(const Plane3& plane)
415 bool pre(const scene::Path& path, scene::Instance& instance) const
417 BrushInstance* brush = Instance_getBrush(instance);
419 && path.top().get().visible()
420 && brush->isSelected())
422 BrushInstance& brushInstance = *brush;
423 brushInstance.setClipPlane(m_plane);
429 void Scene_BrushSetClipPlane(scene::Graph& graph, const Plane3& plane)
431 graph.traverse(BrushInstanceSetClipPlane(plane));
439 bool Brush_merge(Brush& brush, const brush_vector_t& in, bool onlyshape)
441 // gather potential outer faces
444 typedef std::vector<const Face*> Faces;
446 for(brush_vector_t::const_iterator i(in.begin()); i != in.end(); ++i)
448 (*i)->evaluateBRep();
449 for(Brush::const_iterator j((*i)->begin()); j != (*i)->end(); ++j)
451 if(!(*j)->contributes())
456 const Face& face1 = *(*j);
459 // test faces of all input brushes
460 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
461 for(brush_vector_t::const_iterator k(in.begin()); !skip && k != in.end(); ++k)
463 if(k != i) // don't test a brush against itself
465 for(Brush::const_iterator l((*k)->begin()); !skip && l != (*k)->end(); ++l)
467 const Face& face2 = *(*l);
469 // face opposes another face
470 if(plane3_opposing(face1.plane3(), face2.plane3()))
472 // skip opposing planes
480 // check faces already stored
481 for(Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m)
483 const Face& face2 = *(*m);
485 // face equals another face
486 if (plane3_equal(face1.plane3(), face2.plane3()))
488 //if the texture/shader references should be the same but are not
489 if (!onlyshape && !shader_equal(face1.getShader().getShader(), face2.getShader().getShader()))
493 // skip duplicate planes
498 // face1 plane intersects face2 winding or vice versa
499 if (Winding_PlanesConcave(face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3()))
501 // result would not be convex
508 faces.push_back(&face1);
512 for(Faces::const_iterator i = faces.begin(); i != faces.end(); ++i)
514 if(!brush.addFace(*(*i)))
516 // result would have too many sides
522 brush.removeEmptyFaces();
529 brush_vector_t selected_brushes;
532 GlobalSceneGraph().traverse(BrushGatherSelected(selected_brushes));
534 if (selected_brushes.empty())
536 globalOutputStream() << "CSG Merge: No brushes selected.\n";
540 if (selected_brushes.size() < 2)
542 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
546 globalOutputStream() << "CSG Merge: Merging " << Unsigned(selected_brushes.size()) << " brushes.\n";
548 UndoableCommand undo("brushMerge");
550 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
552 NodeSmartReference node((new BrushNode())->node());
553 Brush* brush = Node_getBrush(node);
554 // if the new brush would not be convex
555 if(!Brush_merge(*brush, selected_brushes, true))
557 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
561 ASSERT_MESSAGE(!brush->empty(), "brush left with no faces after merge");
563 // free the original brushes
564 GlobalSceneGraph().traverse(BrushDeleteSelected());
567 Node_getTraversable(merged_path.top())->insert(node);
568 merged_path.push(makeReference(node.get()));
570 selectPath(merged_path, true);
572 globalOutputStream() << "CSG Merge: Succeeded.\n";