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 std::shared_ptr<Face> newFace = out.back()->addFace( face );
37 face.getPlane().offset( -offset );
40 newFace->flipWinding();
41 newFace->getPlane().offset( offset );
42 newFace->planeChanged();
47 void Face_extrude( Face& face, const Brush& brush, brush_vector_t& out, float offset ){
48 if ( face.contributes() ) {
49 face.getPlane().offset( offset );
50 out.push_back( new Brush( brush ) );
51 face.getPlane().offset( -offset );
52 std::shared_ptr<Face> newFace = out.back()->addFace( face );
54 newFace->flipWinding();
55 newFace->planeChanged();
68 FaceMakeBrush( const Brush& brush, brush_vector_t& out, float offset, bool room )
69 : brush( brush ), out( out ), offset( offset ), room( room ){
71 void operator()( Face& face ) const {
73 Face_extrude( face, brush, out, offset );
76 Face_makeBrush( face, brush, out, offset );
81 void Brush_makeHollow( const Brush& brush, brush_vector_t& out, float offset, bool room ){
82 Brush_forEachFace( brush, FaceMakeBrush( brush, out, offset, room ) );
85 class BrushHollowSelectedWalker : public scene::Graph::Walker
90 BrushHollowSelectedWalker( float offset, bool room )
91 : m_offset( offset ), room( room ){
93 bool pre( const scene::Path& path, scene::Instance& instance ) const {
94 if ( path.top().get().visible() ) {
95 Brush* brush = Node_getBrush( path.top() );
97 && Instance_getSelectable( instance )->isSelected()
98 && path.size() > 1 ) {
100 Brush_makeHollow( *brush, out, m_offset, room );
101 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
103 ( *i )->removeEmptyFaces();
104 NodeSmartReference node( ( new BrushNode() )->node() );
105 Node_getBrush( node )->copy( *( *i ) );
107 Node_getTraversable( path.parent() )->insert( node );
115 typedef std::list<Brush*> brushlist_t;
117 class BrushGatherSelected : public scene::Graph::Walker
119 brush_vector_t& m_brushlist;
121 BrushGatherSelected( brush_vector_t& brushlist )
122 : m_brushlist( brushlist ){
125 bool pre( const scene::Path& path, scene::Instance& instance ) const {
126 if ( path.top().get().visible() ) {
127 Brush* brush = Node_getBrush( path.top() );
129 && Instance_getSelectable( instance )->isSelected() ) {
130 m_brushlist.push_back( brush );
137 class BrushDeleteSelected : public scene::Graph::Walker
140 bool pre( const scene::Path& path, scene::Instance& instance ) const {
143 void post( const scene::Path& path, scene::Instance& instance ) const {
144 if ( path.top().get().visible() ) {
145 Brush* brush = Node_getBrush( path.top() );
147 && Instance_getSelectable( instance )->isSelected()
148 && path.size() > 1 ) {
149 Path_deleteTop( path );
155 void Scene_BrushMakeHollow_Selected( scene::Graph& graph, bool room ){
156 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( GetGridSize(), room ) );
157 GlobalSceneGraph().traverse( BrushDeleteSelected() );
166 void CSG_MakeHollow( void ){
167 UndoableCommand undo( "brushHollow" );
169 Scene_BrushMakeHollow_Selected( GlobalSceneGraph(), false );
174 void CSG_MakeRoom( void ){
175 UndoableCommand undo( "makeRoom" );
177 Scene_BrushMakeHollow_Selected( GlobalSceneGraph(), true );
182 template<typename Type>
183 class RemoveReference
189 template<typename Type>
190 class RemoveReference<Type&>
196 template<typename Functor>
199 const Functor& functor;
201 Dereference( const Functor& functor ) : functor( functor ){
203 get_result_type<Functor> operator()( typename RemoveReference<get_argument<Functor, 0>>::type *firstArgument ) const {
204 return functor( *firstArgument );
208 template<typename Functor>
209 inline Dereference<Functor> makeDereference( const Functor& functor ){
210 return Dereference<Functor>( functor );
213 template<typename Caller>
216 typedef get_argument<Caller, 1> FirstBound;
217 FirstBound firstBound;
219 BindArguments1( FirstBound firstBound )
220 : firstBound( firstBound ){
223 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
224 return Caller::call( firstArgument, firstBound );
228 template<typename Caller>
231 typedef get_argument<Caller, 1> FirstBound;
232 typedef get_argument<Caller, 2> SecondBound;
233 FirstBound firstBound;
234 SecondBound secondBound;
236 BindArguments2( FirstBound firstBound, SecondBound secondBound )
237 : firstBound( firstBound ), secondBound( secondBound ){
240 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
241 return Caller::call( firstArgument, firstBound, secondBound );
245 template<typename Caller, typename FirstBound, typename SecondBound>
246 BindArguments2<Caller> bindArguments( const Caller& caller, FirstBound firstBound, SecondBound secondBound ){
247 return BindArguments2<Caller>( firstBound, secondBound );
250 inline bool Face_testPlane( const Face& face, const Plane3& plane, bool flipped ){
251 return face.contributes() && !Winding_TestPlane( face.getWinding(), plane, flipped );
254 typedef Function<bool ( const Face &, const Plane3 &, bool ), Face_testPlane> FaceTestPlane;
257 /// \brief Returns true if
258 /// \li !flipped && brush is BACK or ON
259 /// \li flipped && brush is FRONT or ON
260 bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
261 brush.evaluateBRep();
262 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
264 if ( Face_testPlane( *( *i ), plane, flipped ) ) {
271 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
272 brush.evaluateBRep();
274 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
276 if ( ( *i )->contributes() ) {
277 split += Winding_ClassifyPlane( ( *i )->getWinding(), plane );
283 bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret_fragments ){
284 if ( aabb_intersects_aabb( brush.localAABB(), other.localAABB() ) ) {
285 brush_vector_t fragments;
286 fragments.reserve( other.size() );
289 for ( const std::shared_ptr<Face>& b : other )
291 if ( b->contributes() ) {
292 brushsplit_t split = Brush_classifyPlane( back, b->plane3() );
293 if ( split.counts[ePlaneFront] != 0
294 && split.counts[ePlaneBack] != 0 ) {
295 fragments.push_back( new Brush( back ) );
296 std::shared_ptr<Face> newFace = fragments.back()->addFace( *b );
297 if ( newFace != nullptr ) {
298 newFace->flipWinding();
302 else if ( split.counts[ePlaneBack] == 0 ) {
303 for ( Brush *i : fragments ) {
311 ret_fragments.insert( ret_fragments.end(), fragments.begin(), fragments.end() );
317 class SubtractBrushesFromUnselected : public scene::Graph::Walker
319 const brush_vector_t& m_brushlist;
320 std::size_t& m_before;
321 std::size_t& m_after;
323 SubtractBrushesFromUnselected( const brush_vector_t& brushlist, std::size_t& before, std::size_t& after )
324 : m_brushlist( brushlist ), m_before( before ), m_after( after ){
327 bool pre( const scene::Path& path, scene::Instance& instance ) const {
331 void post( const scene::Path& path, scene::Instance& instance ) const {
332 if ( path.top().get().visible() ) {
333 Brush* brush = Node_getBrush( path.top() );
335 && !Instance_getSelectable( instance )->isSelected() ) {
336 brush_vector_t buffer[2];
338 Brush* original = new Brush( *brush );
339 buffer[static_cast<std::size_t>( swap )].push_back( original );
342 for ( brush_vector_t::const_iterator i( m_brushlist.begin() ); i != m_brushlist.end(); ++i )
344 for ( brush_vector_t::iterator j( buffer[static_cast<std::size_t>( swap )].begin() ); j != buffer[static_cast<std::size_t>( swap )].end(); ++j )
346 if ( Brush_subtract( *( *j ), *( *i ), buffer[static_cast<std::size_t>( !swap )] ) ) {
351 buffer[static_cast<std::size_t>( !swap )].push_back( ( *j ) );
354 buffer[static_cast<std::size_t>( swap )].clear();
359 brush_vector_t& out = buffer[static_cast<std::size_t>( swap )];
361 if ( out.size() == 1 && out.back() == original ) {
367 for ( Brush *b : out ) {
369 b->removeEmptyFaces();
371 NodeSmartReference node( ( new BrushNode() )->node() );
372 Node_getBrush( node )->copy( *b );
373 Node_getTraversable( path.parent() )->insert( node );
377 Path_deleteTop( path );
385 brush_vector_t selected_brushes;
386 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
388 if ( selected_brushes.empty() ) {
389 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
391 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned( selected_brushes.size() ) << " brushes.\n";
393 UndoableCommand undo( "brushSubtract" );
395 // subtract selected from unselected
396 std::size_t before = 0;
397 std::size_t after = 0;
398 GlobalSceneGraph().traverse( SubtractBrushesFromUnselected( selected_brushes, before, after ) );
399 globalOutputStream() << "CSG Subtract: Result: "
400 << Unsigned( after ) << " fragment" << ( after == 1 ? "" : "s" )
401 << " from " << Unsigned( before ) << " brush" << ( before == 1 ? "" : "es" ) << ".\n";
407 class BrushSplitByPlaneSelected : public scene::Graph::Walker
412 const char* m_shader;
413 const TextureProjection& m_projection;
416 BrushSplitByPlaneSelected( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split )
417 : m_p0( p0 ), m_p1( p1 ), m_p2( p2 ), m_shader( shader ), m_projection( projection ), m_split( split ){
420 bool pre( const scene::Path& path, scene::Instance& instance ) const {
424 void post( const scene::Path& path, scene::Instance& instance ) const {
425 if ( !path.top().get().visible() ) {
429 Brush* brush = Node_getBrush( path.top() );
430 if ( brush == nullptr || !Instance_getSelectable( instance )->isSelected() ) {
434 Plane3 plane( plane3_for_points( m_p0, m_p1, m_p2 ) );
435 if ( !plane3_valid( plane ) ) {
439 brushsplit_t split = Brush_classifyPlane( *brush, m_split == eFront ? plane3_flipped( plane ) : plane );
440 if ( split.counts[ePlaneBack] && split.counts[ePlaneFront] ) {
441 // the plane intersects this brush
442 if ( m_split == eFrontAndBack ) {
443 NodeSmartReference node( ( new BrushNode() )->node() );
444 Brush* fragment = Node_getBrush( node );
445 fragment->copy( *brush );
446 std::shared_ptr<Face> newFace =
447 fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
448 if ( newFace != 0 && m_split != eFront ) {
449 newFace->flipWinding();
451 fragment->removeEmptyFaces();
452 ASSERT_MESSAGE( !fragment->empty(), "brush left with no faces after split" );
454 Node_getTraversable( path.parent() )->insert( node );
456 scene::Path fragmentPath = path;
457 fragmentPath.top() = makeReference( node.get() );
458 selectPath( fragmentPath, true );
462 std::shared_ptr<Face> newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
463 if ( newFace != 0 && m_split == eFront ) {
464 newFace->flipWinding();
466 brush->removeEmptyFaces();
467 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after split" );
470 // the plane does not intersect this brush
471 if ( m_split != eFrontAndBack && split.counts[ePlaneBack] != 0 ) {
472 // the brush is "behind" the plane
473 Path_deleteTop( path );
478 void Scene_BrushSplitByPlane( scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split ){
479 TextureProjection projection;
480 TexDef_Construct_Default( projection );
481 graph.traverse( BrushSplitByPlaneSelected( p0, p1, p2, shader, projection, split ) );
486 class BrushInstanceSetClipPlane : public scene::Graph::Walker
490 BrushInstanceSetClipPlane( const Plane3& plane )
494 bool pre( const scene::Path& path, scene::Instance& instance ) const {
495 BrushInstance* brush = Instance_getBrush( instance );
497 && path.top().get().visible()
498 && brush->isSelected() ) {
499 BrushInstance& brushInstance = *brush;
500 brushInstance.setClipPlane( m_plane );
506 void Scene_BrushSetClipPlane( scene::Graph& graph, const Plane3& plane ){
507 graph.traverse( BrushInstanceSetClipPlane( plane ) );
515 bool Brush_merge( Brush& brush, const brush_vector_t& in, bool onlyshape ){
516 // gather potential outer faces
519 typedef std::vector<const Face*> Faces;
521 for ( brush_vector_t::const_iterator i( in.begin() ); i != in.end(); ++i )
523 ( *i )->evaluateBRep();
524 for ( Brush::const_iterator j( ( *i )->begin() ); j != ( *i )->end(); ++j )
526 if ( !( *j )->contributes() ) {
530 const Face& face1 = *( *j );
533 // test faces of all input brushes
534 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
535 for ( brush_vector_t::const_iterator k( in.begin() ); !skip && k != in.end(); ++k )
537 if ( k != i ) { // don't test a brush against itself
538 for ( Brush::const_iterator l( ( *k )->begin() ); !skip && l != ( *k )->end(); ++l )
540 const Face& face2 = *( *l );
542 // face opposes another face
543 if ( plane3_opposing( face1.plane3(), face2.plane3() ) ) {
544 // skip opposing planes
552 // check faces already stored
553 for ( Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m )
555 const Face& face2 = *( *m );
557 // face equals another face
558 if ( plane3_equal( face1.plane3(), face2.plane3() ) ) {
559 //if the texture/shader references should be the same but are not
560 if ( !onlyshape && !shader_equal( face1.getShader().getShader(), face2.getShader().getShader() ) ) {
563 // skip duplicate planes
568 // face1 plane intersects face2 winding or vice versa
569 if ( Winding_PlanesConcave( face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3() ) ) {
570 // result would not be convex
576 faces.push_back( &face1 );
580 for ( Faces::const_iterator i = faces.begin(); i != faces.end(); ++i )
582 if ( !brush.addFace( *( *i ) ) ) {
583 // result would have too many sides
589 brush.removeEmptyFaces();
594 void CSG_Merge( void ){
595 brush_vector_t selected_brushes;
598 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
600 if ( selected_brushes.empty() ) {
601 globalOutputStream() << "CSG Merge: No brushes selected.\n";
605 if ( selected_brushes.size() < 2 ) {
606 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
610 globalOutputStream() << "CSG Merge: Merging " << Unsigned( selected_brushes.size() ) << " brushes.\n";
612 UndoableCommand undo( "brushMerge" );
614 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
616 NodeSmartReference node( ( new BrushNode() )->node() );
617 Brush* brush = Node_getBrush( node );
618 // if the new brush would not be convex
619 if ( !Brush_merge( *brush, selected_brushes, true ) ) {
620 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
624 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after merge" );
626 // free the original brushes
627 GlobalSceneGraph().traverse( BrushDeleteSelected() );
630 Node_getTraversable( merged_path.top() )->insert( node );
631 merged_path.push( makeReference( node.get() ) );
633 selectPath( merged_path, true );
635 globalOutputStream() << "CSG Merge: Succeeded.\n";