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 );
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 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 typedef Face* FacePointer;
214 const FacePointer c_nullFacePointer = 0;
216 template<typename Predicate>
217 Face* Brush_findIf( const Brush& brush, const Predicate& predicate ){
218 Brush::const_iterator i = std::find_if( brush.begin(), brush.end(), makeDereference( predicate ) );
219 return i == brush.end() ? c_nullFacePointer : *i; // uses c_nullFacePointer instead of 0 because otherwise gcc 4.1 attempts conversion to int
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();
272 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
274 if ( Face_testPlane( *( *i ), plane, flipped ) ) {
280 return Brush_findIf( brush, bindArguments( FaceTestPlane(), makeReference( plane ), flipped ) ) == 0;
284 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
285 brush.evaluateBRep();
287 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
289 if ( ( *i )->contributes() ) {
290 split += Winding_ClassifyPlane( ( *i )->getWinding(), plane );
296 bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret_fragments ){
297 if ( aabb_intersects_aabb( brush.localAABB(), other.localAABB() ) ) {
298 brush_vector_t fragments;
299 fragments.reserve( other.size() );
302 for ( Brush::const_iterator i( other.begin() ); i != other.end(); ++i )
304 if ( ( *i )->contributes() ) {
305 brushsplit_t split = Brush_classifyPlane( back, ( *i )->plane3() );
306 if ( split.counts[ePlaneFront] != 0
307 && split.counts[ePlaneBack] != 0 ) {
308 fragments.push_back( new Brush( back ) );
309 Face* newFace = fragments.back()->addFace( *( *i ) );
310 if ( newFace != 0 ) {
311 newFace->flipWinding();
313 back.addFace( *( *i ) );
315 else if ( split.counts[ePlaneBack] == 0 ) {
316 for ( brush_vector_t::iterator i = fragments.begin(); i != fragments.end(); ++i )
324 ret_fragments.insert( ret_fragments.end(), fragments.begin(), fragments.end() );
330 class SubtractBrushesFromUnselected : public scene::Graph::Walker
332 const brush_vector_t& m_brushlist;
333 std::size_t& m_before;
334 std::size_t& m_after;
336 SubtractBrushesFromUnselected( const brush_vector_t& brushlist, std::size_t& before, std::size_t& after )
337 : m_brushlist( brushlist ), m_before( before ), m_after( after ){
340 bool pre( const scene::Path& path, scene::Instance& instance ) const {
344 void post( const scene::Path& path, scene::Instance& instance ) const {
345 if ( path.top().get().visible() ) {
346 Brush* brush = Node_getBrush( path.top() );
348 && !Instance_getSelectable( instance )->isSelected() ) {
349 brush_vector_t buffer[2];
351 Brush* original = new Brush( *brush );
352 buffer[static_cast<std::size_t>( swap )].push_back( original );
355 for ( brush_vector_t::const_iterator i( m_brushlist.begin() ); i != m_brushlist.end(); ++i )
357 for ( brush_vector_t::iterator j( buffer[static_cast<std::size_t>( swap )].begin() ); j != buffer[static_cast<std::size_t>( swap )].end(); ++j )
359 if ( Brush_subtract( *( *j ), *( *i ), buffer[static_cast<std::size_t>( !swap )] ) ) {
364 buffer[static_cast<std::size_t>( !swap )].push_back( ( *j ) );
367 buffer[static_cast<std::size_t>( swap )].clear();
372 brush_vector_t& out = buffer[static_cast<std::size_t>( swap )];
374 if ( out.size() == 1 && out.back() == original ) {
380 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
383 ( *i )->removeEmptyFaces();
384 if ( !( *i )->empty() ) {
385 NodeSmartReference node( ( new BrushNode() )->node() );
386 Node_getBrush( node )->copy( *( *i ) );
388 Node_getTraversable( path.parent() )->insert( node );
394 Path_deleteTop( path );
402 brush_vector_t selected_brushes;
403 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
405 if ( selected_brushes.empty() ) {
406 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
410 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned( selected_brushes.size() ) << " brushes.\n";
412 UndoableCommand undo( "brushSubtract" );
414 // subtract selected from unselected
415 std::size_t before = 0;
416 std::size_t after = 0;
417 GlobalSceneGraph().traverse( SubtractBrushesFromUnselected( selected_brushes, before, after ) );
418 globalOutputStream() << "CSG Subtract: Result: "
419 << Unsigned( after ) << " fragment" << ( after == 1 ? "" : "s" )
420 << " from " << Unsigned( before ) << " brush" << ( before == 1 ? "" : "es" ) << ".\n";
426 class BrushSplitByPlaneSelected : public scene::Graph::Walker
431 const char* m_shader;
432 const TextureProjection& m_projection;
435 BrushSplitByPlaneSelected( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split )
436 : m_p0( p0 ), m_p1( p1 ), m_p2( p2 ), m_shader( shader ), m_projection( projection ), m_split( split ){
439 bool pre( const scene::Path& path, scene::Instance& instance ) const {
443 void post( const scene::Path& path, scene::Instance& instance ) const {
444 if ( path.top().get().visible() ) {
445 Brush* brush = Node_getBrush( path.top() );
447 && Instance_getSelectable( instance )->isSelected() ) {
448 Plane3 plane( plane3_for_points( m_p0, m_p1, m_p2 ) );
449 if ( plane3_valid( plane ) ) {
450 brushsplit_t split = Brush_classifyPlane( *brush, m_split == eFront ? plane3_flipped( plane ) : plane );
451 if ( split.counts[ePlaneBack] && split.counts[ePlaneFront] ) {
452 // the plane intersects this brush
453 if ( m_split == eFrontAndBack ) {
454 NodeSmartReference node( ( new BrushNode() )->node() );
455 Brush* fragment = Node_getBrush( node );
456 fragment->copy( *brush );
457 Face* newFace = fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
458 if ( newFace != 0 && m_split != eFront ) {
459 newFace->flipWinding();
461 fragment->removeEmptyFaces();
462 ASSERT_MESSAGE( !fragment->empty(), "brush left with no faces after split" );
464 Node_getTraversable( path.parent() )->insert( node );
466 scene::Path fragmentPath = path;
467 fragmentPath.top() = makeReference( node.get() );
468 selectPath( fragmentPath, true );
472 Face* newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
473 if ( newFace != 0 && m_split == eFront ) {
474 newFace->flipWinding();
476 brush->removeEmptyFaces();
477 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after split" );
480 // the plane does not intersect this brush
481 if ( m_split != eFrontAndBack && split.counts[ePlaneBack] != 0 ) {
482 // the brush is "behind" the plane
483 Path_deleteTop( path );
491 void Scene_BrushSplitByPlane( scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split ){
492 TextureProjection projection;
493 TexDef_Construct_Default( projection );
494 graph.traverse( BrushSplitByPlaneSelected( p0, p1, p2, shader, projection, split ) );
499 class BrushInstanceSetClipPlane : public scene::Graph::Walker
503 BrushInstanceSetClipPlane( const Plane3& plane )
507 bool pre( const scene::Path& path, scene::Instance& instance ) const {
508 BrushInstance* brush = Instance_getBrush( instance );
510 && path.top().get().visible()
511 && brush->isSelected() ) {
512 BrushInstance& brushInstance = *brush;
513 brushInstance.setClipPlane( m_plane );
519 void Scene_BrushSetClipPlane( scene::Graph& graph, const Plane3& plane ){
520 graph.traverse( BrushInstanceSetClipPlane( plane ) );
528 bool Brush_merge( Brush& brush, const brush_vector_t& in, bool onlyshape ){
529 // gather potential outer faces
532 typedef std::vector<const Face*> Faces;
534 for ( brush_vector_t::const_iterator i( in.begin() ); i != in.end(); ++i )
536 ( *i )->evaluateBRep();
537 for ( Brush::const_iterator j( ( *i )->begin() ); j != ( *i )->end(); ++j )
539 if ( !( *j )->contributes() ) {
543 const Face& face1 = *( *j );
546 // test faces of all input brushes
547 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
548 for ( brush_vector_t::const_iterator k( in.begin() ); !skip && k != in.end(); ++k )
550 if ( k != i ) { // don't test a brush against itself
551 for ( Brush::const_iterator l( ( *k )->begin() ); !skip && l != ( *k )->end(); ++l )
553 const Face& face2 = *( *l );
555 // face opposes another face
556 if ( plane3_opposing( face1.plane3(), face2.plane3() ) ) {
557 // skip opposing planes
565 // check faces already stored
566 for ( Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m )
568 const Face& face2 = *( *m );
570 // face equals another face
571 if ( plane3_equal( face1.plane3(), face2.plane3() ) ) {
572 //if the texture/shader references should be the same but are not
573 if ( !onlyshape && !shader_equal( face1.getShader().getShader(), face2.getShader().getShader() ) ) {
576 // skip duplicate planes
581 // face1 plane intersects face2 winding or vice versa
582 if ( Winding_PlanesConcave( face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3() ) ) {
583 // result would not be convex
589 faces.push_back( &face1 );
593 for ( Faces::const_iterator i = faces.begin(); i != faces.end(); ++i )
595 if ( !brush.addFace( *( *i ) ) ) {
596 // result would have too many sides
602 brush.removeEmptyFaces();
607 void CSG_Merge( void ){
608 brush_vector_t selected_brushes;
611 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
613 if ( selected_brushes.empty() ) {
614 globalOutputStream() << "CSG Merge: No brushes selected.\n";
618 if ( selected_brushes.size() < 2 ) {
619 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
623 globalOutputStream() << "CSG Merge: Merging " << Unsigned( selected_brushes.size() ) << " brushes.\n";
625 UndoableCommand undo( "brushMerge" );
627 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
629 NodeSmartReference node( ( new BrushNode() )->node() );
630 Brush* brush = Node_getBrush( node );
631 // if the new brush would not be convex
632 if ( !Brush_merge( *brush, selected_brushes, true ) ) {
633 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
637 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after merge" );
639 // free the original brushes
640 GlobalSceneGraph().traverse( BrushDeleteSelected() );
643 Node_getTraversable( merged_path.top() )->insert( node );
644 merged_path.push( makeReference( node.get() ) );
646 selectPath( merged_path, true );
648 globalOutputStream() << "CSG Merge: Succeeded.\n";