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"
33 void Face_makeBrush(Face& face, const Brush& brush, brush_vector_t& out, float offset)
35 if(face.contributes())
37 out.push_back(new Brush(brush));
38 Face* newFace = out.back()->addFace(face);
41 newFace->flipWinding();
42 newFace->getPlane().offset(offset);
43 newFace->planeChanged();
54 FaceMakeBrush(const Brush& brush, brush_vector_t& out, float offset)
55 : brush(brush), out(out), offset(offset)
58 void operator()(Face& face) const
60 Face_makeBrush(face, brush, out, offset);
64 void Brush_makeHollow(const Brush& brush, brush_vector_t& out, float offset)
66 Brush_forEachFace(brush, FaceMakeBrush(brush, out, offset));
69 class BrushHollowSelectedWalker : public scene::Graph::Walker
73 BrushHollowSelectedWalker(float offset)
77 bool pre(const scene::Path& path, scene::Instance& instance) const
79 if(path.top().get().visible())
81 Brush* brush = Node_getBrush(path.top());
83 && Instance_getSelectable(instance)->isSelected()
87 Brush_makeHollow(*brush, out, m_offset);
88 for(brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i)
90 (*i)->removeEmptyFaces();
91 NodeSmartReference node((new BrushNode())->node());
92 Node_getBrush(node)->copy(*(*i));
94 Node_getTraversable(path.parent())->insert(node);
102 typedef std::list<Brush*> brushlist_t;
104 class BrushGatherSelected : public scene::Graph::Walker
106 brush_vector_t& m_brushlist;
108 BrushGatherSelected(brush_vector_t& brushlist)
109 : m_brushlist(brushlist)
112 bool pre(const scene::Path& path, scene::Instance& instance) const
114 if(path.top().get().visible())
116 Brush* brush = Node_getBrush(path.top());
118 && Instance_getSelectable(instance)->isSelected())
120 m_brushlist.push_back(brush);
127 class BrushDeleteSelected : public scene::Graph::Walker
130 bool pre(const scene::Path& path, scene::Instance& instance) const
134 void post(const scene::Path& path, scene::Instance& instance) const
136 if(path.top().get().visible())
138 Brush* brush = Node_getBrush(path.top());
140 && Instance_getSelectable(instance)->isSelected()
143 Path_deleteTop(path);
149 void Scene_BrushMakeHollow_Selected(scene::Graph& graph)
151 GlobalSceneGraph().traverse(BrushHollowSelectedWalker(GetGridSize()));
152 GlobalSceneGraph().traverse(BrushDeleteSelected());
161 void CSG_MakeHollow (void)
163 UndoableCommand undo("brushHollow");
165 Scene_BrushMakeHollow_Selected(GlobalSceneGraph());
170 template<typename Type>
171 class RemoveReference
177 template<typename Type>
178 class RemoveReference<Type&>
184 template<typename Functor>
187 const Functor& functor;
189 typedef typename RemoveReference<typename Functor::first_argument_type>::type* first_argument_type;
190 typedef typename Functor::result_type result_type;
191 Dereference(const Functor& functor) : functor(functor)
194 result_type operator()(first_argument_type firstArgument) const
196 return functor(*firstArgument);
200 template<typename Functor>
201 inline Dereference<Functor> makeDereference(const Functor& functor)
203 return Dereference<Functor>(functor);
206 typedef Face* FacePointer;
207 const FacePointer c_nullFacePointer = 0;
209 template<typename Predicate>
210 Face* Brush_findIf(const Brush& brush, const Predicate& predicate)
212 Brush::const_iterator i = std::find_if(brush.begin(), brush.end(), makeDereference(predicate));
213 return i == brush.end() ? c_nullFacePointer : *i; // uses c_nullFacePointer instead of 0 because otherwise gcc 4.1 attempts conversion to int
216 template<typename Caller>
219 typedef typename Caller::second_argument_type FirstBound;
220 FirstBound firstBound;
222 typedef typename Caller::result_type result_type;
223 typedef typename Caller::first_argument_type first_argument_type;
224 BindArguments1(FirstBound firstBound)
225 : firstBound(firstBound)
228 result_type operator()(first_argument_type firstArgument) const
230 return Caller::call(firstArgument, firstBound);
234 template<typename Caller>
237 typedef typename Caller::second_argument_type FirstBound;
238 typedef typename Caller::third_argument_type SecondBound;
239 FirstBound firstBound;
240 SecondBound secondBound;
242 typedef typename Caller::result_type result_type;
243 typedef typename Caller::first_argument_type first_argument_type;
244 BindArguments2(FirstBound firstBound, SecondBound secondBound)
245 : firstBound(firstBound), secondBound(secondBound)
248 result_type operator()(first_argument_type firstArgument) const
250 return Caller::call(firstArgument, firstBound, secondBound);
254 template<typename Caller, typename FirstBound, typename SecondBound>
255 BindArguments2<Caller> bindArguments(const Caller& caller, FirstBound firstBound, SecondBound secondBound)
257 return BindArguments2<Caller>(firstBound, secondBound);
260 inline bool Face_testPlane(const Face& face, const Plane3& plane, bool flipped)
262 return face.contributes() && !Winding_TestPlane(face.getWinding(), plane, flipped);
264 typedef Function3<const Face&, const Plane3&, bool, bool, Face_testPlane> FaceTestPlane;
268 /// \brief Returns true if
269 /// \li !flipped && brush is BACK or ON
270 /// \li flipped && brush is FRONT or ON
271 bool Brush_testPlane(const Brush& brush, const Plane3& plane, bool flipped)
273 brush.evaluateBRep();
275 for(Brush::const_iterator i(brush.begin()); i != brush.end(); ++i)
277 if(Face_testPlane(*(*i), plane, flipped))
284 return Brush_findIf(brush, bindArguments(FaceTestPlane(), makeReference(plane), flipped)) == 0;
288 brushsplit_t Brush_classifyPlane(const Brush& brush, const Plane3& plane)
290 brush.evaluateBRep();
292 for(Brush::const_iterator i(brush.begin()); i != brush.end(); ++i)
294 if((*i)->contributes())
296 split += Winding_ClassifyPlane((*i)->getWinding(), plane);
302 bool Brush_subtract(const Brush& brush, const Brush& other, brush_vector_t& ret_fragments)
304 if(aabb_intersects_aabb(brush.localAABB(), other.localAABB()))
306 brush_vector_t fragments;
307 fragments.reserve(other.size());
310 for(Brush::const_iterator i(other.begin()); i != other.end(); ++i)
312 if((*i)->contributes())
314 brushsplit_t split = Brush_classifyPlane(back, (*i)->plane3());
315 if(split.counts[ePlaneFront] != 0
316 && split.counts[ePlaneBack] != 0)
318 fragments.push_back(new Brush(back));
319 Face* newFace = fragments.back()->addFace(*(*i));
322 newFace->flipWinding();
326 else if(split.counts[ePlaneBack] == 0)
328 for(brush_vector_t::iterator i = fragments.begin(); i != fragments.end(); ++i)
336 ret_fragments.insert(ret_fragments.end(), fragments.begin(), fragments.end());
342 class SubtractBrushesFromUnselected : public scene::Graph::Walker
344 const brush_vector_t& m_brushlist;
345 std::size_t& m_before;
346 std::size_t& m_after;
348 SubtractBrushesFromUnselected(const brush_vector_t& brushlist, std::size_t& before, std::size_t& after)
349 : m_brushlist(brushlist), m_before(before), m_after(after)
352 bool pre(const scene::Path& path, scene::Instance& instance) const
356 void post(const scene::Path& path, scene::Instance& instance) const
358 if(path.top().get().visible())
360 Brush* brush = Node_getBrush(path.top());
362 && !Instance_getSelectable(instance)->isSelected())
364 brush_vector_t buffer[2];
366 Brush* original = new Brush(*brush);
367 buffer[static_cast<std::size_t>(swap)].push_back(original);
370 for(brush_vector_t::const_iterator i(m_brushlist.begin()); i != m_brushlist.end(); ++i)
372 for(brush_vector_t::iterator j(buffer[static_cast<std::size_t>(swap)].begin()); j != buffer[static_cast<std::size_t>(swap)].end(); ++j)
374 if(Brush_subtract(*(*j), *(*i), buffer[static_cast<std::size_t>(!swap)]))
380 buffer[static_cast<std::size_t>(!swap)].push_back((*j));
383 buffer[static_cast<std::size_t>(swap)].clear();
388 brush_vector_t& out = buffer[static_cast<std::size_t>(swap)];
390 if(out.size() == 1 && out.back() == original)
397 for(brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i)
400 NodeSmartReference node((new BrushNode())->node());
401 (*i)->removeEmptyFaces();
402 ASSERT_MESSAGE(!(*i)->empty(), "brush left with no faces after subtract");
403 Node_getBrush(node)->copy(*(*i));
405 Node_getTraversable(path.parent())->insert(node);
407 Path_deleteTop(path);
416 brush_vector_t selected_brushes;
417 GlobalSceneGraph().traverse(BrushGatherSelected(selected_brushes));
419 if (selected_brushes.empty())
421 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
425 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned(selected_brushes.size()) << " brushes.\n";
427 UndoableCommand undo("brushSubtract");
429 // subtract selected from unselected
430 std::size_t before = 0;
431 std::size_t after = 0;
432 GlobalSceneGraph().traverse(SubtractBrushesFromUnselected(selected_brushes, before, after));
433 globalOutputStream() << "CSG Subtract: Result: "
434 << Unsigned(after) << " fragment" << (after == 1 ? "" : "s")
435 << " from " << Unsigned(before) << " brush" << (before == 1? "" : "es") << ".\n";
441 class BrushSplitByPlaneSelected : public scene::Graph::Walker
446 const char* m_shader;
447 const TextureProjection& m_projection;
450 BrushSplitByPlaneSelected(const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split)
451 : m_p0(p0), m_p1(p1), m_p2(p2), m_shader(shader), m_projection(projection), m_split(split)
454 bool pre(const scene::Path& path, scene::Instance& instance) const
458 void post(const scene::Path& path, scene::Instance& instance) const
460 if(path.top().get().visible())
462 Brush* brush = Node_getBrush(path.top());
464 && Instance_getSelectable(instance)->isSelected())
466 Plane3 plane(plane3_for_points(m_p0, m_p1, m_p2));
467 if(plane3_valid(plane))
469 brushsplit_t split = Brush_classifyPlane(*brush, m_split == eFront ? plane3_flipped(plane) : plane);
470 if(split.counts[ePlaneBack] && split.counts[ePlaneFront])
472 // the plane intersects this brush
473 if(m_split == eFrontAndBack)
475 NodeSmartReference node((new BrushNode())->node());
476 Brush* fragment = Node_getBrush(node);
477 fragment->copy(*brush);
478 Face* newFace = fragment->addPlane(m_p0, m_p1, m_p2, m_shader, m_projection);
479 if(newFace != 0 && m_split != eFront)
481 newFace->flipWinding();
483 fragment->removeEmptyFaces();
484 ASSERT_MESSAGE(!fragment->empty(), "brush left with no faces after split");
486 Node_getTraversable(path.parent())->insert(node);
488 scene::Path fragmentPath = path;
489 fragmentPath.top() = makeReference(node.get());
490 selectPath(fragmentPath, true);
494 Face* newFace = brush->addPlane(m_p0, m_p1, m_p2, m_shader, m_projection);
495 if(newFace != 0 && m_split == eFront)
497 newFace->flipWinding();
499 brush->removeEmptyFaces();
500 ASSERT_MESSAGE(!brush->empty(), "brush left with no faces after split");
503 // the plane does not intersect this brush
504 if(m_split != eFrontAndBack && split.counts[ePlaneBack] != 0)
506 // the brush is "behind" the plane
507 Path_deleteTop(path);
515 void Scene_BrushSplitByPlane(scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split)
517 TextureProjection projection;
518 TexDef_Construct_Default(projection);
519 graph.traverse(BrushSplitByPlaneSelected(p0, p1, p2, shader, projection, split));
524 class BrushInstanceSetClipPlane : public scene::Graph::Walker
528 BrushInstanceSetClipPlane(const Plane3& plane)
532 bool pre(const scene::Path& path, scene::Instance& instance) const
534 BrushInstance* brush = Instance_getBrush(instance);
536 && path.top().get().visible()
537 && brush->isSelected())
539 BrushInstance& brushInstance = *brush;
540 brushInstance.setClipPlane(m_plane);
546 void Scene_BrushSetClipPlane(scene::Graph& graph, const Plane3& plane)
548 graph.traverse(BrushInstanceSetClipPlane(plane));
556 bool Brush_merge(Brush& brush, const brush_vector_t& in, bool onlyshape)
558 // gather potential outer faces
561 typedef std::vector<const Face*> Faces;
563 for(brush_vector_t::const_iterator i(in.begin()); i != in.end(); ++i)
565 (*i)->evaluateBRep();
566 for(Brush::const_iterator j((*i)->begin()); j != (*i)->end(); ++j)
568 if(!(*j)->contributes())
573 const Face& face1 = *(*j);
576 // test faces of all input brushes
577 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
578 for(brush_vector_t::const_iterator k(in.begin()); !skip && k != in.end(); ++k)
580 if(k != i) // don't test a brush against itself
582 for(Brush::const_iterator l((*k)->begin()); !skip && l != (*k)->end(); ++l)
584 const Face& face2 = *(*l);
586 // face opposes another face
587 if(plane3_opposing(face1.plane3(), face2.plane3()))
589 // skip opposing planes
597 // check faces already stored
598 for(Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m)
600 const Face& face2 = *(*m);
602 // face equals another face
603 if (plane3_equal(face1.plane3(), face2.plane3()))
605 //if the texture/shader references should be the same but are not
606 if (!onlyshape && !shader_equal(face1.getShader().getShader(), face2.getShader().getShader()))
610 // skip duplicate planes
615 // face1 plane intersects face2 winding or vice versa
616 if (Winding_PlanesConcave(face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3()))
618 // result would not be convex
625 faces.push_back(&face1);
629 for(Faces::const_iterator i = faces.begin(); i != faces.end(); ++i)
631 if(!brush.addFace(*(*i)))
633 // result would have too many sides
639 brush.removeEmptyFaces();
646 brush_vector_t selected_brushes;
649 GlobalSceneGraph().traverse(BrushGatherSelected(selected_brushes));
651 if (selected_brushes.empty())
653 globalOutputStream() << "CSG Merge: No brushes selected.\n";
657 if (selected_brushes.size() < 2)
659 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
663 globalOutputStream() << "CSG Merge: Merging " << Unsigned(selected_brushes.size()) << " brushes.\n";
665 UndoableCommand undo("brushMerge");
667 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
669 NodeSmartReference node((new BrushNode())->node());
670 Brush* brush = Node_getBrush(node);
671 // if the new brush would not be convex
672 if(!Brush_merge(*brush, selected_brushes, true))
674 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
678 ASSERT_MESSAGE(!brush->empty(), "brush left with no faces after merge");
680 // free the original brushes
681 GlobalSceneGraph().traverse(BrushDeleteSelected());
684 Node_getTraversable(merged_path.top())->insert(node);
685 merged_path.push(makeReference(node.get()));
687 selectPath(merged_path, true);
689 globalOutputStream() << "CSG Merge: Succeeded.\n";