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 ){
124 bool pre( const scene::Path& path, scene::Instance& instance ) const {
125 if ( path.top().get().visible() ) {
126 Brush* brush = Node_getBrush( path.top() );
128 && Instance_getSelectable( instance )->isSelected() ) {
129 m_brushlist.push_back( brush );
136 class BrushDeleteSelected : public scene::Graph::Walker
139 bool pre( const scene::Path& path, scene::Instance& instance ) const {
142 void post( const scene::Path& path, scene::Instance& instance ) const {
143 if ( path.top().get().visible() ) {
144 Brush* brush = Node_getBrush( path.top() );
146 && Instance_getSelectable( instance )->isSelected()
147 && path.size() > 1 ) {
148 Path_deleteTop( path );
154 void Scene_BrushMakeHollow_Selected( scene::Graph& graph, bool room ){
155 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( GetGridSize(), room ) );
156 GlobalSceneGraph().traverse( BrushDeleteSelected() );
165 void CSG_MakeHollow( void ){
166 UndoableCommand undo( "brushHollow" );
168 Scene_BrushMakeHollow_Selected( GlobalSceneGraph(), false );
173 void CSG_MakeRoom( void ){
174 UndoableCommand undo( "makeRoom" );
176 Scene_BrushMakeHollow_Selected( GlobalSceneGraph(), true );
181 template<typename Type>
182 class RemoveReference
188 template<typename Type>
189 class RemoveReference<Type&>
195 template<typename Functor>
198 const Functor& functor;
200 typedef typename RemoveReference<typename Functor::first_argument_type>::type* first_argument_type;
201 typedef typename Functor::result_type result_type;
202 Dereference( const Functor& functor ) : functor( functor ){
204 result_type operator()( first_argument_type firstArgument ) const {
205 return functor( *firstArgument );
209 template<typename Functor>
210 inline Dereference<Functor> makeDereference( const Functor& functor ){
211 return Dereference<Functor>( functor );
214 typedef Face* FacePointer;
215 const FacePointer c_nullFacePointer = 0;
217 template<typename Predicate>
218 Face* Brush_findIf( const Brush& brush, const Predicate& predicate ){
219 Brush::const_iterator i = std::find_if( brush.begin(), brush.end(), makeDereference( predicate ) );
220 return i == brush.end() ? c_nullFacePointer : *i; // uses c_nullFacePointer instead of 0 because otherwise gcc 4.1 attempts conversion to int
223 template<typename Caller>
226 typedef typename Caller::second_argument_type FirstBound;
227 FirstBound firstBound;
229 typedef typename Caller::result_type result_type;
230 typedef typename Caller::first_argument_type first_argument_type;
231 BindArguments1( FirstBound firstBound )
232 : firstBound( firstBound ){
234 result_type operator()( first_argument_type firstArgument ) const {
235 return Caller::call( firstArgument, firstBound );
239 template<typename Caller>
242 typedef typename Caller::second_argument_type FirstBound;
243 typedef typename Caller::third_argument_type SecondBound;
244 FirstBound firstBound;
245 SecondBound secondBound;
247 typedef typename Caller::result_type result_type;
248 typedef typename Caller::first_argument_type first_argument_type;
249 BindArguments2( FirstBound firstBound, SecondBound secondBound )
250 : firstBound( firstBound ), secondBound( secondBound ){
252 result_type operator()( first_argument_type firstArgument ) const {
253 return Caller::call( firstArgument, firstBound, secondBound );
257 template<typename Caller, typename FirstBound, typename SecondBound>
258 BindArguments2<Caller> bindArguments( const Caller& caller, FirstBound firstBound, SecondBound secondBound ){
259 return BindArguments2<Caller>( firstBound, secondBound );
262 inline bool Face_testPlane( const Face& face, const Plane3& plane, bool flipped ){
263 return face.contributes() && !Winding_TestPlane( face.getWinding(), plane, flipped );
265 typedef Function3<const Face&, const Plane3&, bool, bool, Face_testPlane> FaceTestPlane;
269 /// \brief Returns true if
270 /// \li !flipped && brush is BACK or ON
271 /// \li flipped && brush is FRONT or ON
272 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 ) ) {
283 return Brush_findIf( brush, bindArguments( FaceTestPlane(), makeReference( plane ), flipped ) ) == 0;
287 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
288 brush.evaluateBRep();
290 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
292 if ( ( *i )->contributes() ) {
293 split += Winding_ClassifyPlane( ( *i )->getWinding(), plane );
299 bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret_fragments ){
300 if ( aabb_intersects_aabb( brush.localAABB(), other.localAABB() ) ) {
301 brush_vector_t fragments;
302 fragments.reserve( other.size() );
305 for ( Brush::const_iterator i( other.begin() ); i != other.end(); ++i )
307 if ( ( *i )->contributes() ) {
308 brushsplit_t split = Brush_classifyPlane( back, ( *i )->plane3() );
309 if ( split.counts[ePlaneFront] != 0
310 && split.counts[ePlaneBack] != 0 ) {
311 fragments.push_back( new Brush( back ) );
312 Face* newFace = fragments.back()->addFace( *( *i ) );
313 if ( newFace != 0 ) {
314 newFace->flipWinding();
316 back.addFace( *( *i ) );
318 else if ( split.counts[ePlaneBack] == 0 ) {
319 for ( brush_vector_t::iterator i = fragments.begin(); i != fragments.end(); ++i )
327 ret_fragments.insert( ret_fragments.end(), fragments.begin(), fragments.end() );
333 class SubtractBrushesFromUnselected : public scene::Graph::Walker
335 const brush_vector_t& m_brushlist;
336 std::size_t& m_before;
337 std::size_t& m_after;
339 SubtractBrushesFromUnselected( const brush_vector_t& brushlist, std::size_t& before, std::size_t& after )
340 : m_brushlist( brushlist ), m_before( before ), m_after( after ){
342 bool pre( const scene::Path& path, scene::Instance& instance ) const {
345 void post( const scene::Path& path, scene::Instance& instance ) const {
346 if ( path.top().get().visible() ) {
347 Brush* brush = Node_getBrush( path.top() );
349 && !Instance_getSelectable( instance )->isSelected() ) {
350 brush_vector_t buffer[2];
352 Brush* original = new Brush( *brush );
353 buffer[static_cast<std::size_t>( swap )].push_back( original );
356 for ( brush_vector_t::const_iterator i( m_brushlist.begin() ); i != m_brushlist.end(); ++i )
358 for ( brush_vector_t::iterator j( buffer[static_cast<std::size_t>( swap )].begin() ); j != buffer[static_cast<std::size_t>( swap )].end(); ++j )
360 if ( Brush_subtract( *( *j ), *( *i ), buffer[static_cast<std::size_t>( !swap )] ) ) {
365 buffer[static_cast<std::size_t>( !swap )].push_back( ( *j ) );
368 buffer[static_cast<std::size_t>( swap )].clear();
373 brush_vector_t& out = buffer[static_cast<std::size_t>( swap )];
375 if ( out.size() == 1 && out.back() == original ) {
381 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
384 ( *i )->removeEmptyFaces();
385 if ( !( *i )->empty() ) {
386 NodeSmartReference node( ( new BrushNode() )->node() );
387 Node_getBrush( node )->copy( *( *i ) );
389 Node_getTraversable( path.parent() )->insert( node );
395 Path_deleteTop( path );
403 brush_vector_t selected_brushes;
404 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
406 if ( selected_brushes.empty() ) {
407 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
411 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned( selected_brushes.size() ) << " brushes.\n";
413 UndoableCommand undo( "brushSubtract" );
415 // subtract selected from unselected
416 std::size_t before = 0;
417 std::size_t after = 0;
418 GlobalSceneGraph().traverse( SubtractBrushesFromUnselected( selected_brushes, before, after ) );
419 globalOutputStream() << "CSG Subtract: Result: "
420 << Unsigned( after ) << " fragment" << ( after == 1 ? "" : "s" )
421 << " from " << Unsigned( before ) << " brush" << ( before == 1 ? "" : "es" ) << ".\n";
427 class BrushSplitByPlaneSelected : public scene::Graph::Walker
432 const char* m_shader;
433 const TextureProjection& m_projection;
436 BrushSplitByPlaneSelected( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split )
437 : 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 {
442 void post( const scene::Path& path, scene::Instance& instance ) const {
443 if ( path.top().get().visible() ) {
444 Brush* brush = Node_getBrush( path.top() );
446 && Instance_getSelectable( instance )->isSelected() ) {
447 Plane3 plane( plane3_for_points( m_p0, m_p1, m_p2 ) );
448 if ( plane3_valid( plane ) ) {
449 brushsplit_t split = Brush_classifyPlane( *brush, m_split == eFront ? plane3_flipped( plane ) : plane );
450 if ( split.counts[ePlaneBack] && split.counts[ePlaneFront] ) {
451 // the plane intersects this brush
452 if ( m_split == eFrontAndBack ) {
453 NodeSmartReference node( ( new BrushNode() )->node() );
454 Brush* fragment = Node_getBrush( node );
455 fragment->copy( *brush );
456 Face* newFace = 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 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 );
490 void Scene_BrushSplitByPlane( scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split ){
491 TextureProjection projection;
492 TexDef_Construct_Default( projection );
493 graph.traverse( BrushSplitByPlaneSelected( p0, p1, p2, shader, projection, split ) );
498 class BrushInstanceSetClipPlane : public scene::Graph::Walker
502 BrushInstanceSetClipPlane( const Plane3& plane )
505 bool pre( const scene::Path& path, scene::Instance& instance ) const {
506 BrushInstance* brush = Instance_getBrush( instance );
508 && path.top().get().visible()
509 && brush->isSelected() ) {
510 BrushInstance& brushInstance = *brush;
511 brushInstance.setClipPlane( m_plane );
517 void Scene_BrushSetClipPlane( scene::Graph& graph, const Plane3& plane ){
518 graph.traverse( BrushInstanceSetClipPlane( plane ) );
526 bool Brush_merge( Brush& brush, const brush_vector_t& in, bool onlyshape ){
527 // gather potential outer faces
530 typedef std::vector<const Face*> Faces;
532 for ( brush_vector_t::const_iterator i( in.begin() ); i != in.end(); ++i )
534 ( *i )->evaluateBRep();
535 for ( Brush::const_iterator j( ( *i )->begin() ); j != ( *i )->end(); ++j )
537 if ( !( *j )->contributes() ) {
541 const Face& face1 = *( *j );
544 // test faces of all input brushes
545 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
546 for ( brush_vector_t::const_iterator k( in.begin() ); !skip && k != in.end(); ++k )
548 if ( k != i ) { // don't test a brush against itself
549 for ( Brush::const_iterator l( ( *k )->begin() ); !skip && l != ( *k )->end(); ++l )
551 const Face& face2 = *( *l );
553 // face opposes another face
554 if ( plane3_opposing( face1.plane3(), face2.plane3() ) ) {
555 // skip opposing planes
563 // check faces already stored
564 for ( Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m )
566 const Face& face2 = *( *m );
568 // face equals another face
569 if ( plane3_equal( face1.plane3(), face2.plane3() ) ) {
570 //if the texture/shader references should be the same but are not
571 if ( !onlyshape && !shader_equal( face1.getShader().getShader(), face2.getShader().getShader() ) ) {
574 // skip duplicate planes
579 // face1 plane intersects face2 winding or vice versa
580 if ( Winding_PlanesConcave( face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3() ) ) {
581 // result would not be convex
587 faces.push_back( &face1 );
591 for ( Faces::const_iterator i = faces.begin(); i != faces.end(); ++i )
593 if ( !brush.addFace( *( *i ) ) ) {
594 // result would have too many sides
600 brush.removeEmptyFaces();
605 void CSG_Merge( void ){
606 brush_vector_t selected_brushes;
609 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
611 if ( selected_brushes.empty() ) {
612 globalOutputStream() << "CSG Merge: No brushes selected.\n";
616 if ( selected_brushes.size() < 2 ) {
617 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
621 globalOutputStream() << "CSG Merge: Merging " << Unsigned( selected_brushes.size() ) << " brushes.\n";
623 UndoableCommand undo( "brushMerge" );
625 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
627 NodeSmartReference node( ( new BrushNode() )->node() );
628 Brush* brush = Node_getBrush( node );
629 // if the new brush would not be convex
630 if ( !Brush_merge( *brush, selected_brushes, true ) ) {
631 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
635 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after merge" );
637 // free the original brushes
638 GlobalSceneGraph().traverse( BrushDeleteSelected() );
641 Node_getTraversable( merged_path.top() )->insert( node );
642 merged_path.push( makeReference( node.get() ) );
644 selectPath( merged_path, true );
646 globalOutputStream() << "CSG Merge: Succeeded.\n";