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();
45 void Face_makeRoom( Face &face, const Brush &brush, brush_vector_t &out, float offset ){
46 if ( face.contributes() ) {
47 face.getPlane().offset( offset );
48 out.push_back( new Brush( brush ) );
49 face.getPlane().offset( -offset );
50 Face* newFace = out.back()->addFace( face );
52 newFace->flipWinding();
53 newFace->planeChanged();
58 void Brush_makeHollow( const Brush &brush, brush_vector_t &out, float offset ){
59 Brush_forEachFace( brush, [&]( Face &face ) {
60 Face_makeBrush( face, brush, out, offset );
64 void Brush_makeRoom( const Brush &brush, brush_vector_t &out, float offset ){
65 Brush_forEachFace( brush, [&]( Face &face ) {
66 Face_makeRoom( face, brush, out, offset );
70 class BrushHollowSelectedWalker : public scene::Graph::Walker
75 BrushHollowSelectedWalker( float offset, bool makeRoom )
76 : m_offset( offset ), m_makeRoom( makeRoom ){
79 bool pre( const scene::Path& path, scene::Instance& instance ) const {
80 if ( path.top().get().visible() ) {
81 Brush* brush = Node_getBrush( path.top() );
83 && Instance_getSelectable( instance )->isSelected()
84 && path.size() > 1 ) {
88 Brush_makeRoom(* brush, out, m_offset );
92 Brush_makeHollow(* brush, out, m_offset );
95 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
97 ( *i )->removeEmptyFaces();
98 NodeSmartReference node( ( new BrushNode() )->node() );
99 Node_getBrush( node )->copy( *( *i ) );
101 Node_getTraversable( path.parent() )->insert( node );
109 typedef std::list<Brush*> brushlist_t;
111 class BrushGatherSelected : public scene::Graph::Walker
113 brush_vector_t& m_brushlist;
115 BrushGatherSelected( brush_vector_t& brushlist )
116 : m_brushlist( brushlist ){
119 bool pre( const scene::Path& path, scene::Instance& instance ) const {
120 if ( path.top().get().visible() ) {
121 Brush* brush = Node_getBrush( path.top() );
123 && Instance_getSelectable( instance )->isSelected() ) {
124 m_brushlist.push_back( brush );
131 class BrushDeleteSelected : public scene::Graph::Walker
134 bool pre( const scene::Path& path, scene::Instance& instance ) const {
137 void post( const scene::Path& path, scene::Instance& instance ) const {
138 if ( path.top().get().visible() ) {
139 Brush* brush = Node_getBrush( path.top() );
141 && Instance_getSelectable( instance )->isSelected()
142 && path.size() > 1 ) {
143 Path_deleteTop( path );
149 void Scene_BrushMakeHollow_Selected( scene::Graph& graph, bool makeRoom ){
150 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( GetGridSize(), makeRoom ) );
151 GlobalSceneGraph().traverse( BrushDeleteSelected() );
160 void CSG_MakeHollow( void ){
161 UndoableCommand undo( "brushHollow" );
163 Scene_BrushMakeHollow_Selected( GlobalSceneGraph(), false );
168 void CSG_MakeRoom( void ){
169 UndoableCommand undo( "brushRoom" );
171 Scene_BrushMakeHollow_Selected( GlobalSceneGraph(), true );
176 template<typename Type>
177 class RemoveReference
183 template<typename Type>
184 class RemoveReference<Type&>
190 template<typename Functor>
193 const Functor& functor;
195 Dereference( const Functor& functor ) : functor( functor ){
197 get_result_type<Functor> operator()( typename RemoveReference<get_argument<Functor, 0>>::type *firstArgument ) const {
198 return functor( *firstArgument );
202 template<typename Functor>
203 inline Dereference<Functor> makeDereference( const Functor& functor ){
204 return Dereference<Functor>( functor );
207 typedef Face* FacePointer;
208 const FacePointer c_nullFacePointer = 0;
210 template<typename Predicate>
211 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 get_argument<Caller, 1> FirstBound;
220 FirstBound firstBound;
222 BindArguments1( FirstBound firstBound )
223 : firstBound( firstBound ){
226 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
227 return Caller::call( firstArgument, firstBound );
231 template<typename Caller>
234 typedef get_argument<Caller, 1> FirstBound;
235 typedef get_argument<Caller, 2> SecondBound;
236 FirstBound firstBound;
237 SecondBound secondBound;
239 BindArguments2( FirstBound firstBound, SecondBound secondBound )
240 : firstBound( firstBound ), secondBound( secondBound ){
243 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
244 return Caller::call( firstArgument, firstBound, secondBound );
248 template<typename Caller, typename FirstBound, typename SecondBound>
249 BindArguments2<Caller> bindArguments( const Caller& caller, FirstBound firstBound, SecondBound secondBound ){
250 return BindArguments2<Caller>( firstBound, secondBound );
253 inline bool Face_testPlane( const Face& face, const Plane3& plane, bool flipped ){
254 return face.contributes() && !Winding_TestPlane( face.getWinding(), plane, flipped );
257 typedef Function<bool ( const Face &, const Plane3 &, bool ), Face_testPlane> FaceTestPlane;
260 /// \brief Returns true if
261 /// \li !flipped && brush is BACK or ON
262 /// \li flipped && brush is FRONT or ON
263 bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
264 brush.evaluateBRep();
266 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
268 if ( Face_testPlane( *( *i ), plane, flipped ) ) {
274 return Brush_findIf( brush, bindArguments( FaceTestPlane(), makeReference( plane ), flipped ) ) == 0;
278 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
279 brush.evaluateBRep();
281 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
283 if ( ( *i )->contributes() ) {
284 split += Winding_ClassifyPlane( ( *i )->getWinding(), plane );
290 bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret_fragments ){
291 if ( aabb_intersects_aabb( brush.localAABB(), other.localAABB() ) ) {
292 brush_vector_t fragments;
293 fragments.reserve( other.size() );
296 for ( Brush::const_iterator i( other.begin() ); i != other.end(); ++i )
298 if ( ( *i )->contributes() ) {
299 brushsplit_t split = Brush_classifyPlane( back, ( *i )->plane3() );
300 if ( split.counts[ePlaneFront] != 0
301 && split.counts[ePlaneBack] != 0 ) {
302 fragments.push_back( new Brush( back ) );
303 Face* newFace = fragments.back()->addFace( *( *i ) );
304 if ( newFace != 0 ) {
305 newFace->flipWinding();
307 back.addFace( *( *i ) );
309 else if ( split.counts[ePlaneBack] == 0 ) {
310 for ( brush_vector_t::iterator i = fragments.begin(); i != fragments.end(); ++i )
318 ret_fragments.insert( ret_fragments.end(), fragments.begin(), fragments.end() );
324 class SubtractBrushesFromUnselected : public scene::Graph::Walker
326 const brush_vector_t& m_brushlist;
327 std::size_t& m_before;
328 std::size_t& m_after;
330 SubtractBrushesFromUnselected( const brush_vector_t& brushlist, std::size_t& before, std::size_t& after )
331 : m_brushlist( brushlist ), m_before( before ), m_after( after ){
334 bool pre( const scene::Path& path, scene::Instance& instance ) const {
338 void post( const scene::Path& path, scene::Instance& instance ) const {
339 if ( path.top().get().visible() ) {
340 Brush* brush = Node_getBrush( path.top() );
342 && !Instance_getSelectable( instance )->isSelected() ) {
343 brush_vector_t buffer[2];
345 Brush* original = new Brush( *brush );
346 buffer[static_cast<std::size_t>( swap )].push_back( original );
349 for ( brush_vector_t::const_iterator i( m_brushlist.begin() ); i != m_brushlist.end(); ++i )
351 for ( brush_vector_t::iterator j( buffer[static_cast<std::size_t>( swap )].begin() ); j != buffer[static_cast<std::size_t>( swap )].end(); ++j )
353 if ( Brush_subtract( *( *j ), *( *i ), buffer[static_cast<std::size_t>( !swap )] ) ) {
358 buffer[static_cast<std::size_t>( !swap )].push_back( ( *j ) );
361 buffer[static_cast<std::size_t>( swap )].clear();
366 brush_vector_t& out = buffer[static_cast<std::size_t>( swap )];
368 if ( out.size() == 1 && out.back() == original ) {
374 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
377 ( *i )->removeEmptyFaces();
378 if ( !( *i )->empty() ) {
379 NodeSmartReference node( ( new BrushNode() )->node() );
380 Node_getBrush( node )->copy( *( *i ) );
382 Node_getTraversable( path.parent() )->insert( node );
388 Path_deleteTop( path );
396 brush_vector_t selected_brushes;
397 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
399 if ( selected_brushes.empty() ) {
400 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
404 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned( selected_brushes.size() ) << " brushes.\n";
406 UndoableCommand undo( "brushSubtract" );
408 // subtract selected from unselected
409 std::size_t before = 0;
410 std::size_t after = 0;
411 GlobalSceneGraph().traverse( SubtractBrushesFromUnselected( selected_brushes, before, after ) );
412 globalOutputStream() << "CSG Subtract: Result: "
413 << Unsigned( after ) << " fragment" << ( after == 1 ? "" : "s" )
414 << " from " << Unsigned( before ) << " brush" << ( before == 1 ? "" : "es" ) << ".\n";
420 class BrushSplitByPlaneSelected : public scene::Graph::Walker
425 const char* m_shader;
426 const TextureProjection& m_projection;
429 BrushSplitByPlaneSelected( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split )
430 : m_p0( p0 ), m_p1( p1 ), m_p2( p2 ), m_shader( shader ), m_projection( projection ), m_split( split ){
433 bool pre( const scene::Path& path, scene::Instance& instance ) const {
437 void post( const scene::Path& path, scene::Instance& instance ) const {
438 if ( path.top().get().visible() ) {
439 Brush* brush = Node_getBrush( path.top() );
441 && Instance_getSelectable( instance )->isSelected() ) {
442 Plane3 plane( plane3_for_points( m_p0, m_p1, m_p2 ) );
443 if ( plane3_valid( plane ) ) {
444 brushsplit_t split = Brush_classifyPlane( *brush, m_split == eFront ? plane3_flipped( plane ) : plane );
445 if ( split.counts[ePlaneBack] && split.counts[ePlaneFront] ) {
446 // the plane intersects this brush
447 if ( m_split == eFrontAndBack ) {
448 NodeSmartReference node( ( new BrushNode() )->node() );
449 Brush* fragment = Node_getBrush( node );
450 fragment->copy( *brush );
451 Face* newFace = fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
452 if ( newFace != 0 && m_split != eFront ) {
453 newFace->flipWinding();
455 fragment->removeEmptyFaces();
456 ASSERT_MESSAGE( !fragment->empty(), "brush left with no faces after split" );
458 Node_getTraversable( path.parent() )->insert( node );
460 scene::Path fragmentPath = path;
461 fragmentPath.top() = makeReference( node.get() );
462 selectPath( fragmentPath, true );
466 Face* newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
467 if ( newFace != 0 && m_split == eFront ) {
468 newFace->flipWinding();
470 brush->removeEmptyFaces();
471 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after split" );
474 // the plane does not intersect this brush
475 if ( m_split != eFrontAndBack && split.counts[ePlaneBack] != 0 ) {
476 // the brush is "behind" the plane
477 Path_deleteTop( path );
485 void Scene_BrushSplitByPlane( scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split ){
486 TextureProjection projection;
487 TexDef_Construct_Default( projection );
488 graph.traverse( BrushSplitByPlaneSelected( p0, p1, p2, shader, projection, split ) );
493 class BrushInstanceSetClipPlane : public scene::Graph::Walker
497 BrushInstanceSetClipPlane( const Plane3& plane )
501 bool pre( const scene::Path& path, scene::Instance& instance ) const {
502 BrushInstance* brush = Instance_getBrush( instance );
504 && path.top().get().visible()
505 && brush->isSelected() ) {
506 BrushInstance& brushInstance = *brush;
507 brushInstance.setClipPlane( m_plane );
513 void Scene_BrushSetClipPlane( scene::Graph& graph, const Plane3& plane ){
514 graph.traverse( BrushInstanceSetClipPlane( plane ) );
522 bool Brush_merge( Brush& brush, const brush_vector_t& in, bool onlyshape ){
523 // gather potential outer faces
526 typedef std::vector<const Face*> Faces;
528 for ( brush_vector_t::const_iterator i( in.begin() ); i != in.end(); ++i )
530 ( *i )->evaluateBRep();
531 for ( Brush::const_iterator j( ( *i )->begin() ); j != ( *i )->end(); ++j )
533 if ( !( *j )->contributes() ) {
537 const Face& face1 = *( *j );
540 // test faces of all input brushes
541 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
542 for ( brush_vector_t::const_iterator k( in.begin() ); !skip && k != in.end(); ++k )
544 if ( k != i ) { // don't test a brush against itself
545 for ( Brush::const_iterator l( ( *k )->begin() ); !skip && l != ( *k )->end(); ++l )
547 const Face& face2 = *( *l );
549 // face opposes another face
550 if ( plane3_opposing( face1.plane3(), face2.plane3() ) ) {
551 // skip opposing planes
559 // check faces already stored
560 for ( Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m )
562 const Face& face2 = *( *m );
564 // face equals another face
565 if ( plane3_equal( face1.plane3(), face2.plane3() ) ) {
566 //if the texture/shader references should be the same but are not
567 if ( !onlyshape && !shader_equal( face1.getShader().getShader(), face2.getShader().getShader() ) ) {
570 // skip duplicate planes
575 // face1 plane intersects face2 winding or vice versa
576 if ( Winding_PlanesConcave( face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3() ) ) {
577 // result would not be convex
583 faces.push_back( &face1 );
587 for ( Faces::const_iterator i = faces.begin(); i != faces.end(); ++i )
589 if ( !brush.addFace( *( *i ) ) ) {
590 // result would have too many sides
596 brush.removeEmptyFaces();
601 void CSG_Merge( void ){
602 brush_vector_t selected_brushes;
605 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
607 if ( selected_brushes.empty() ) {
608 globalOutputStream() << "CSG Merge: No brushes selected.\n";
612 if ( selected_brushes.size() < 2 ) {
613 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
617 globalOutputStream() << "CSG Merge: Merging " << Unsigned( selected_brushes.size() ) << " brushes.\n";
619 UndoableCommand undo( "brushMerge" );
621 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
623 NodeSmartReference node( ( new BrushNode() )->node() );
624 Brush* brush = Node_getBrush( node );
625 // if the new brush would not be convex
626 if ( !Brush_merge( *brush, selected_brushes, true ) ) {
627 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
631 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after merge" );
633 // free the original brushes
634 GlobalSceneGraph().traverse( BrushDeleteSelected() );
637 Node_getTraversable( merged_path.top() )->insert( node );
638 merged_path.push( makeReference( node.get() ) );
640 selectPath( merged_path, true );
642 globalOutputStream() << "CSG Merge: Succeeded.\n";