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 ){
34 if ( face.contributes() ) {
35 out.push_back( new Brush( brush ) );
36 Face* newFace = out.back()->addFace( face );
38 newFace->flipWinding();
39 newFace->getPlane().offset( offset );
40 newFace->planeChanged();
46 void Face_makeBrush( Face& face, const Brush& brush, brush_vector_t& out, float offset ){
47 if ( face.contributes() ) {
48 out.push_back( new Brush( brush ) );
49 //face.getPlane().offset( -offset );
50 //face.planeChanged();
51 std::shared_ptr<Face> newFace = out.back()->addFace( face );
53 newFace->flipWinding();
54 newFace->getPlane().offset( offset );
55 newFace->planeChanged();
60 void Face_makeRoom( Face &face, const Brush &brush, brush_vector_t &out, float offset ){
61 if ( face.contributes() ) {
62 face.getPlane().offset( offset );
63 out.push_back( new Brush( brush ) );
64 face.getPlane().offset( -offset );
65 std::shared_ptr<Face> newFace = out.back()->addFace( face );
67 newFace->flipWinding();
68 newFace->planeChanged();
73 void Brush_makeHollow( const Brush &brush, brush_vector_t &out, float offset ){
74 Brush_forEachFace( brush, [&]( Face &face ) {
75 Face_makeBrush( face, brush, out, offset );
79 void Brush_makeRoom( const Brush &brush, brush_vector_t &out, float offset ){
80 Brush_forEachFace( brush, [&]( Face &face ) {
81 Face_makeRoom( face, brush, out, offset );
85 class BrushHollowSelectedWalker : public scene::Graph::Walker
90 BrushHollowSelectedWalker( float offset, bool makeRoom )
91 : m_offset( offset ), m_makeRoom( makeRoom ){
94 bool pre( const scene::Path& path, scene::Instance& instance ) const {
95 if ( path.top().get().visible() ) {
96 Brush* brush = Node_getBrush( path.top() );
98 && Instance_getSelectable( instance )->isSelected()
99 && path.size() > 1 ) {
103 Brush_makeRoom(* brush, out, m_offset );
107 Brush_makeHollow(* brush, out, m_offset );
110 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
112 ( *i )->removeEmptyFaces();
113 NodeSmartReference node( ( new BrushNode() )->node() );
114 Node_getBrush( node )->copy( *( *i ) );
116 Node_getTraversable( path.parent() )->insert( node );
124 typedef std::list<Brush*> brushlist_t;
126 class BrushGatherSelected : public scene::Graph::Walker
128 brush_vector_t& m_brushlist;
130 BrushGatherSelected( brush_vector_t& brushlist )
131 : m_brushlist( brushlist ){
134 bool pre( const scene::Path& path, scene::Instance& instance ) const {
135 if ( path.top().get().visible() ) {
136 Brush* brush = Node_getBrush( path.top() );
138 && Instance_getSelectable( instance )->isSelected() ) {
139 m_brushlist.push_back( brush );
146 class BrushDeleteSelected : public scene::Graph::Walker
149 bool pre( const scene::Path& path, scene::Instance& instance ) const {
152 void post( const scene::Path& path, scene::Instance& instance ) const {
153 if ( path.top().get().visible() ) {
154 Brush* brush = Node_getBrush( path.top() );
156 && Instance_getSelectable( instance )->isSelected()
157 && path.size() > 1 ) {
158 Path_deleteTop( path );
164 void Scene_BrushMakeHollow_Selected( scene::Graph& graph, bool makeRoom ){
165 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( GetGridSize(), makeRoom ) );
166 GlobalSceneGraph().traverse( BrushDeleteSelected() );
175 void CSG_MakeHollow( void ){
176 UndoableCommand undo( "brushHollow" );
178 Scene_BrushMakeHollow_Selected( GlobalSceneGraph(), false );
183 void CSG_MakeRoom( void ){
184 UndoableCommand undo( "brushRoom" );
186 Scene_BrushMakeHollow_Selected( GlobalSceneGraph(), true );
191 template<typename Type>
192 class RemoveReference
198 template<typename Type>
199 class RemoveReference<Type&>
205 template<typename Functor>
208 const Functor& functor;
210 Dereference( const Functor& functor ) : functor( functor ){
212 get_result_type<Functor> operator()( typename RemoveReference<get_argument<Functor, 0>>::type *firstArgument ) const {
213 return functor( *firstArgument );
217 template<typename Functor>
218 inline Dereference<Functor> makeDereference( const Functor& functor ){
219 return Dereference<Functor>( functor );
222 template<typename Caller>
225 typedef get_argument<Caller, 1> FirstBound;
226 FirstBound firstBound;
228 BindArguments1( FirstBound firstBound )
229 : firstBound( firstBound ){
232 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
233 return Caller::call( firstArgument, firstBound );
237 template<typename Caller>
240 typedef get_argument<Caller, 1> FirstBound;
241 typedef get_argument<Caller, 2> SecondBound;
242 FirstBound firstBound;
243 SecondBound secondBound;
245 BindArguments2( FirstBound firstBound, SecondBound secondBound )
246 : firstBound( firstBound ), secondBound( secondBound ){
249 get_result_type<Caller> operator()( get_argument<Caller, 0> 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 ){
256 return BindArguments2<Caller>( firstBound, secondBound );
259 inline bool Face_testPlane( const Face& face, const Plane3& plane, bool flipped ){
260 return face.contributes() && !Winding_TestPlane( face.getWinding(), plane, flipped );
263 typedef Function<bool ( const Face &, const Plane3 &, bool ), Face_testPlane> FaceTestPlane;
266 /// \brief Returns true if
267 /// \li !flipped && brush is BACK or ON
268 /// \li flipped && brush is FRONT or ON
269 bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
270 brush.evaluateBRep();
271 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
273 if ( Face_testPlane( *( *i ), plane, flipped ) ) {
280 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
281 brush.evaluateBRep();
283 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
285 if ( ( *i )->contributes() ) {
286 split += Winding_ClassifyPlane( ( *i )->getWinding(), plane );
292 bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret_fragments ){
293 if ( aabb_intersects_aabb( brush.localAABB(), other.localAABB() ) ) {
294 brush_vector_t fragments;
295 fragments.reserve( other.size() );
298 for ( const std::shared_ptr<Face>& b : other )
300 if ( b->contributes() ) {
301 brushsplit_t split = Brush_classifyPlane( back, b->plane3() );
302 if ( split.counts[ePlaneFront] != 0
303 && split.counts[ePlaneBack] != 0 ) {
304 fragments.push_back( new Brush( back ) );
305 std::shared_ptr<Face> newFace = fragments.back()->addFace( *b );
306 if ( newFace != nullptr ) {
307 newFace->flipWinding();
311 else if ( split.counts[ePlaneBack] == 0 ) {
312 for ( Brush *i : fragments ) {
320 ret_fragments.insert( ret_fragments.end(), fragments.begin(), fragments.end() );
326 class SubtractBrushesFromUnselected : public scene::Graph::Walker
328 const brush_vector_t& m_brushlist;
329 std::size_t& m_before;
330 std::size_t& m_after;
332 SubtractBrushesFromUnselected( const brush_vector_t& brushlist, std::size_t& before, std::size_t& after )
333 : m_brushlist( brushlist ), m_before( before ), m_after( after ){
336 bool pre( const scene::Path& path, scene::Instance& instance ) const {
340 void post( const scene::Path& path, scene::Instance& instance ) const {
341 if ( path.top().get().visible() ) {
342 Brush* brush = Node_getBrush( path.top() );
344 && !Instance_getSelectable( instance )->isSelected() ) {
345 brush_vector_t buffer[2];
347 Brush* original = new Brush( *brush );
348 buffer[static_cast<std::size_t>( swap )].push_back( original );
351 for ( brush_vector_t::const_iterator i( m_brushlist.begin() ); i != m_brushlist.end(); ++i )
353 for ( brush_vector_t::iterator j( buffer[static_cast<std::size_t>( swap )].begin() ); j != buffer[static_cast<std::size_t>( swap )].end(); ++j )
355 if ( Brush_subtract( *( *j ), *( *i ), buffer[static_cast<std::size_t>( !swap )] ) ) {
360 buffer[static_cast<std::size_t>( !swap )].push_back( ( *j ) );
363 buffer[static_cast<std::size_t>( swap )].clear();
368 brush_vector_t& out = buffer[static_cast<std::size_t>( swap )];
370 if ( out.size() == 1 && out.back() == original ) {
376 for ( Brush *b : out ) {
378 b->removeEmptyFaces();
380 NodeSmartReference node( ( new BrushNode() )->node() );
381 Node_getBrush( node )->copy( *b );
382 Node_getTraversable( path.parent() )->insert( node );
386 Path_deleteTop( path );
394 brush_vector_t selected_brushes;
395 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
397 if ( selected_brushes.empty() ) {
398 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
400 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned( selected_brushes.size() ) << " brushes.\n";
402 UndoableCommand undo( "brushSubtract" );
404 // subtract selected from unselected
405 std::size_t before = 0;
406 std::size_t after = 0;
407 GlobalSceneGraph().traverse( SubtractBrushesFromUnselected( selected_brushes, before, after ) );
408 globalOutputStream() << "CSG Subtract: Result: "
409 << Unsigned( after ) << " fragment" << ( after == 1 ? "" : "s" )
410 << " from " << Unsigned( before ) << " brush" << ( before == 1 ? "" : "es" ) << ".\n";
416 class BrushSplitByPlaneSelected : public scene::Graph::Walker
421 const char* m_shader;
422 const TextureProjection& m_projection;
425 BrushSplitByPlaneSelected( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split )
426 : m_p0( p0 ), m_p1( p1 ), m_p2( p2 ), m_shader( shader ), m_projection( projection ), m_split( split ){
429 bool pre( const scene::Path& path, scene::Instance& instance ) const {
433 void post( const scene::Path& path, scene::Instance& instance ) const {
434 if ( !path.top().get().visible() ) {
438 Brush* brush = Node_getBrush( path.top() );
439 if ( brush == nullptr || !Instance_getSelectable( instance )->isSelected() ) {
443 Plane3 plane( plane3_for_points( m_p0, m_p1, m_p2 ) );
444 if ( !plane3_valid( plane ) ) {
448 brushsplit_t split = Brush_classifyPlane( *brush, m_split == eFront ? plane3_flipped( plane ) : plane );
449 if ( split.counts[ePlaneBack] && split.counts[ePlaneFront] ) {
450 // the plane intersects this brush
451 if ( m_split == eFrontAndBack ) {
452 NodeSmartReference node( ( new BrushNode() )->node() );
453 Brush* fragment = Node_getBrush( node );
454 fragment->copy( *brush );
455 std::shared_ptr<Face> newFace =
456 fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
457 if ( newFace != 0 && m_split != eFront ) {
458 newFace->flipWinding();
460 fragment->removeEmptyFaces();
461 ASSERT_MESSAGE( !fragment->empty(), "brush left with no faces after split" );
463 Node_getTraversable( path.parent() )->insert( node );
465 scene::Path fragmentPath = path;
466 fragmentPath.top() = makeReference( node.get() );
467 selectPath( fragmentPath, true );
471 std::shared_ptr<Face> newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
472 if ( newFace != 0 && m_split == eFront ) {
473 newFace->flipWinding();
475 brush->removeEmptyFaces();
476 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after split" );
479 // the plane does not intersect this brush
480 if ( m_split != eFrontAndBack && split.counts[ePlaneBack] != 0 ) {
481 // the brush is "behind" the plane
482 Path_deleteTop( path );
487 void Scene_BrushSplitByPlane( scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split ){
488 TextureProjection projection;
489 TexDef_Construct_Default( projection );
490 graph.traverse( BrushSplitByPlaneSelected( p0, p1, p2, shader, projection, split ) );
495 class BrushInstanceSetClipPlane : public scene::Graph::Walker
499 BrushInstanceSetClipPlane( const Plane3& plane )
503 bool pre( const scene::Path& path, scene::Instance& instance ) const {
504 BrushInstance* brush = Instance_getBrush( instance );
506 && path.top().get().visible()
507 && brush->isSelected() ) {
508 BrushInstance& brushInstance = *brush;
509 brushInstance.setClipPlane( m_plane );
515 void Scene_BrushSetClipPlane( scene::Graph& graph, const Plane3& plane ){
516 graph.traverse( BrushInstanceSetClipPlane( plane ) );
524 bool Brush_merge( Brush& brush, const brush_vector_t& in, bool onlyshape ){
525 // gather potential outer faces
528 typedef std::vector<const Face*> Faces;
530 for ( brush_vector_t::const_iterator i( in.begin() ); i != in.end(); ++i )
532 ( *i )->evaluateBRep();
533 for ( Brush::const_iterator j( ( *i )->begin() ); j != ( *i )->end(); ++j )
535 if ( !( *j )->contributes() ) {
539 const Face& face1 = *( *j );
542 // test faces of all input brushes
543 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
544 for ( brush_vector_t::const_iterator k( in.begin() ); !skip && k != in.end(); ++k )
546 if ( k != i ) { // don't test a brush against itself
547 for ( Brush::const_iterator l( ( *k )->begin() ); !skip && l != ( *k )->end(); ++l )
549 const Face& face2 = *( *l );
551 // face opposes another face
552 if ( plane3_opposing( face1.plane3(), face2.plane3() ) ) {
553 // skip opposing planes
561 // check faces already stored
562 for ( Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m )
564 const Face& face2 = *( *m );
566 // face equals another face
567 if ( plane3_equal( face1.plane3(), face2.plane3() ) ) {
568 //if the texture/shader references should be the same but are not
569 if ( !onlyshape && !shader_equal( face1.getShader().getShader(), face2.getShader().getShader() ) ) {
572 // skip duplicate planes
577 // face1 plane intersects face2 winding or vice versa
578 if ( Winding_PlanesConcave( face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3() ) ) {
579 // result would not be convex
585 faces.push_back( &face1 );
589 for ( Faces::const_iterator i = faces.begin(); i != faces.end(); ++i )
591 if ( !brush.addFace( *( *i ) ) ) {
592 // result would have too many sides
598 brush.removeEmptyFaces();
603 void CSG_Merge( void ){
604 brush_vector_t selected_brushes;
607 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
609 if ( selected_brushes.empty() ) {
610 globalOutputStream() << "CSG Merge: No brushes selected.\n";
614 if ( selected_brushes.size() < 2 ) {
615 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
619 globalOutputStream() << "CSG Merge: Merging " << Unsigned( selected_brushes.size() ) << " brushes.\n";
621 UndoableCommand undo( "brushMerge" );
623 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
625 NodeSmartReference node( ( new BrushNode() )->node() );
626 Brush* brush = Node_getBrush( node );
627 // if the new brush would not be convex
628 if ( !Brush_merge( *brush, selected_brushes, true ) ) {
629 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
633 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after merge" );
635 // free the original brushes
636 GlobalSceneGraph().traverse( BrushDeleteSelected() );
639 Node_getTraversable( merged_path.top() )->insert( node );
640 merged_path.push( makeReference( node.get() ) );
642 selectPath( merged_path, true );
644 globalOutputStream() << "CSG Merge: Succeeded.\n";