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();
51 FaceMakeBrush( const Brush& brush, brush_vector_t& out, float offset )
52 : brush( brush ), out( out ), offset( offset ){
54 void operator()( Face& face ) const {
55 Face_makeBrush( face, brush, out, offset );
59 void Brush_makeHollow( const Brush& brush, brush_vector_t& out, float offset ){
60 Brush_forEachFace( brush, FaceMakeBrush( brush, out, offset ) );
63 class BrushHollowSelectedWalker : public scene::Graph::Walker
67 BrushHollowSelectedWalker( float offset )
70 bool pre( const scene::Path& path, scene::Instance& instance ) const {
71 if ( path.top().get().visible() ) {
72 Brush* brush = Node_getBrush( path.top() );
74 && Instance_getSelectable( instance )->isSelected()
75 && path.size() > 1 ) {
77 Brush_makeHollow( *brush, out, m_offset );
78 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
80 ( *i )->removeEmptyFaces();
81 NodeSmartReference node( ( new BrushNode() )->node() );
82 Node_getBrush( node )->copy( *( *i ) );
84 Node_getTraversable( path.parent() )->insert( node );
92 typedef std::list<Brush*> brushlist_t;
94 class BrushGatherSelected : public scene::Graph::Walker
96 brush_vector_t& m_brushlist;
98 BrushGatherSelected( brush_vector_t& brushlist )
99 : m_brushlist( brushlist ){
101 bool pre( const scene::Path& path, scene::Instance& instance ) const {
102 if ( path.top().get().visible() ) {
103 Brush* brush = Node_getBrush( path.top() );
105 && Instance_getSelectable( instance )->isSelected() ) {
106 m_brushlist.push_back( brush );
113 class BrushDeleteSelected : public scene::Graph::Walker
116 bool pre( const scene::Path& path, scene::Instance& instance ) const {
119 void post( 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 && path.size() > 1 ) {
125 Path_deleteTop( path );
131 void Scene_BrushMakeHollow_Selected( scene::Graph& graph ){
132 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( GetGridSize() ) );
133 GlobalSceneGraph().traverse( BrushDeleteSelected() );
142 void CSG_MakeHollow( void ){
143 UndoableCommand undo( "brushHollow" );
145 Scene_BrushMakeHollow_Selected( GlobalSceneGraph() );
150 template<typename Type>
151 class RemoveReference
157 template<typename Type>
158 class RemoveReference<Type&>
164 template<typename Functor>
167 const Functor& functor;
169 Dereference( const Functor& functor ) : functor( functor ){
171 get_result_type<Functor> operator()( typename RemoveReference<get_argument<Functor, 0>>::type *firstArgument ) const {
172 return functor( *firstArgument );
176 template<typename Functor>
177 inline Dereference<Functor> makeDereference( const Functor& functor ){
178 return Dereference<Functor>( functor );
181 typedef Face* FacePointer;
182 const FacePointer c_nullFacePointer = 0;
184 template<typename Predicate>
185 Face* Brush_findIf( const Brush& brush, const Predicate& predicate ){
186 Brush::const_iterator i = std::find_if( brush.begin(), brush.end(), makeDereference( predicate ) );
187 return i == brush.end() ? c_nullFacePointer : *i; // uses c_nullFacePointer instead of 0 because otherwise gcc 4.1 attempts conversion to int
190 template<typename Caller>
193 typedef get_argument<Caller, 1> FirstBound;
194 FirstBound firstBound;
196 BindArguments1( FirstBound firstBound )
197 : firstBound( firstBound ){
199 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
200 return Caller::call( firstArgument, firstBound );
204 template<typename Caller>
207 typedef get_argument<Caller, 1> FirstBound;
208 typedef get_argument<Caller, 2> SecondBound;
209 FirstBound firstBound;
210 SecondBound secondBound;
212 BindArguments2( FirstBound firstBound, SecondBound secondBound )
213 : firstBound( firstBound ), secondBound( secondBound ){
215 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
216 return Caller::call( firstArgument, firstBound, secondBound );
220 template<typename Caller, typename FirstBound, typename SecondBound>
221 BindArguments2<Caller> bindArguments( const Caller& caller, FirstBound firstBound, SecondBound secondBound ){
222 return BindArguments2<Caller>( firstBound, secondBound );
225 inline bool Face_testPlane( const Face& face, const Plane3& plane, bool flipped ){
226 return face.contributes() && !Winding_TestPlane( face.getWinding(), plane, flipped );
228 typedef Function3<const Face&, const Plane3&, bool, bool, Face_testPlane> FaceTestPlane;
232 /// \brief Returns true if
233 /// \li !flipped && brush is BACK or ON
234 /// \li flipped && brush is FRONT or ON
235 bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
236 brush.evaluateBRep();
238 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
240 if ( Face_testPlane( *( *i ), plane, flipped ) ) {
246 return Brush_findIf( brush, bindArguments( FaceTestPlane(), makeReference( plane ), flipped ) ) == 0;
250 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
251 brush.evaluateBRep();
253 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
255 if ( ( *i )->contributes() ) {
256 split += Winding_ClassifyPlane( ( *i )->getWinding(), plane );
262 bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret_fragments ){
263 if ( aabb_intersects_aabb( brush.localAABB(), other.localAABB() ) ) {
264 brush_vector_t fragments;
265 fragments.reserve( other.size() );
268 for ( Brush::const_iterator i( other.begin() ); i != other.end(); ++i )
270 if ( ( *i )->contributes() ) {
271 brushsplit_t split = Brush_classifyPlane( back, ( *i )->plane3() );
272 if ( split.counts[ePlaneFront] != 0
273 && split.counts[ePlaneBack] != 0 ) {
274 fragments.push_back( new Brush( back ) );
275 Face* newFace = fragments.back()->addFace( *( *i ) );
276 if ( newFace != 0 ) {
277 newFace->flipWinding();
279 back.addFace( *( *i ) );
281 else if ( split.counts[ePlaneBack] == 0 ) {
282 for ( brush_vector_t::iterator i = fragments.begin(); i != fragments.end(); ++i )
290 ret_fragments.insert( ret_fragments.end(), fragments.begin(), fragments.end() );
296 class SubtractBrushesFromUnselected : public scene::Graph::Walker
298 const brush_vector_t& m_brushlist;
299 std::size_t& m_before;
300 std::size_t& m_after;
302 SubtractBrushesFromUnselected( const brush_vector_t& brushlist, std::size_t& before, std::size_t& after )
303 : m_brushlist( brushlist ), m_before( before ), m_after( after ){
305 bool pre( const scene::Path& path, scene::Instance& instance ) const {
308 void post( const scene::Path& path, scene::Instance& instance ) const {
309 if ( path.top().get().visible() ) {
310 Brush* brush = Node_getBrush( path.top() );
312 && !Instance_getSelectable( instance )->isSelected() ) {
313 brush_vector_t buffer[2];
315 Brush* original = new Brush( *brush );
316 buffer[static_cast<std::size_t>( swap )].push_back( original );
319 for ( brush_vector_t::const_iterator i( m_brushlist.begin() ); i != m_brushlist.end(); ++i )
321 for ( brush_vector_t::iterator j( buffer[static_cast<std::size_t>( swap )].begin() ); j != buffer[static_cast<std::size_t>( swap )].end(); ++j )
323 if ( Brush_subtract( *( *j ), *( *i ), buffer[static_cast<std::size_t>( !swap )] ) ) {
328 buffer[static_cast<std::size_t>( !swap )].push_back( ( *j ) );
331 buffer[static_cast<std::size_t>( swap )].clear();
336 brush_vector_t& out = buffer[static_cast<std::size_t>( swap )];
338 if ( out.size() == 1 && out.back() == original ) {
344 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
347 ( *i )->removeEmptyFaces();
348 if ( !( *i )->empty() ) {
349 NodeSmartReference node( ( new BrushNode() )->node() );
350 Node_getBrush( node )->copy( *( *i ) );
352 Node_getTraversable( path.parent() )->insert( node );
358 Path_deleteTop( path );
366 brush_vector_t selected_brushes;
367 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
369 if ( selected_brushes.empty() ) {
370 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
374 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned( selected_brushes.size() ) << " brushes.\n";
376 UndoableCommand undo( "brushSubtract" );
378 // subtract selected from unselected
379 std::size_t before = 0;
380 std::size_t after = 0;
381 GlobalSceneGraph().traverse( SubtractBrushesFromUnselected( selected_brushes, before, after ) );
382 globalOutputStream() << "CSG Subtract: Result: "
383 << Unsigned( after ) << " fragment" << ( after == 1 ? "" : "s" )
384 << " from " << Unsigned( before ) << " brush" << ( before == 1 ? "" : "es" ) << ".\n";
390 class BrushSplitByPlaneSelected : public scene::Graph::Walker
395 const char* m_shader;
396 const TextureProjection& m_projection;
399 BrushSplitByPlaneSelected( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split )
400 : m_p0( p0 ), m_p1( p1 ), m_p2( p2 ), m_shader( shader ), m_projection( projection ), m_split( split ){
402 bool pre( const scene::Path& path, scene::Instance& instance ) const {
405 void post( const scene::Path& path, scene::Instance& instance ) const {
406 if ( path.top().get().visible() ) {
407 Brush* brush = Node_getBrush( path.top() );
409 && Instance_getSelectable( instance )->isSelected() ) {
410 Plane3 plane( plane3_for_points( m_p0, m_p1, m_p2 ) );
411 if ( plane3_valid( plane ) ) {
412 brushsplit_t split = Brush_classifyPlane( *brush, m_split == eFront ? plane3_flipped( plane ) : plane );
413 if ( split.counts[ePlaneBack] && split.counts[ePlaneFront] ) {
414 // the plane intersects this brush
415 if ( m_split == eFrontAndBack ) {
416 NodeSmartReference node( ( new BrushNode() )->node() );
417 Brush* fragment = Node_getBrush( node );
418 fragment->copy( *brush );
419 Face* newFace = fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
420 if ( newFace != 0 && m_split != eFront ) {
421 newFace->flipWinding();
423 fragment->removeEmptyFaces();
424 ASSERT_MESSAGE( !fragment->empty(), "brush left with no faces after split" );
426 Node_getTraversable( path.parent() )->insert( node );
428 scene::Path fragmentPath = path;
429 fragmentPath.top() = makeReference( node.get() );
430 selectPath( fragmentPath, true );
434 Face* newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
435 if ( newFace != 0 && m_split == eFront ) {
436 newFace->flipWinding();
438 brush->removeEmptyFaces();
439 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after split" );
442 // the plane does not intersect this brush
443 if ( m_split != eFrontAndBack && split.counts[ePlaneBack] != 0 ) {
444 // the brush is "behind" the plane
445 Path_deleteTop( path );
453 void Scene_BrushSplitByPlane( scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split ){
454 TextureProjection projection;
455 TexDef_Construct_Default( projection );
456 graph.traverse( BrushSplitByPlaneSelected( p0, p1, p2, shader, projection, split ) );
461 class BrushInstanceSetClipPlane : public scene::Graph::Walker
465 BrushInstanceSetClipPlane( const Plane3& plane )
468 bool pre( const scene::Path& path, scene::Instance& instance ) const {
469 BrushInstance* brush = Instance_getBrush( instance );
471 && path.top().get().visible()
472 && brush->isSelected() ) {
473 BrushInstance& brushInstance = *brush;
474 brushInstance.setClipPlane( m_plane );
480 void Scene_BrushSetClipPlane( scene::Graph& graph, const Plane3& plane ){
481 graph.traverse( BrushInstanceSetClipPlane( plane ) );
489 bool Brush_merge( Brush& brush, const brush_vector_t& in, bool onlyshape ){
490 // gather potential outer faces
493 typedef std::vector<const Face*> Faces;
495 for ( brush_vector_t::const_iterator i( in.begin() ); i != in.end(); ++i )
497 ( *i )->evaluateBRep();
498 for ( Brush::const_iterator j( ( *i )->begin() ); j != ( *i )->end(); ++j )
500 if ( !( *j )->contributes() ) {
504 const Face& face1 = *( *j );
507 // test faces of all input brushes
508 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
509 for ( brush_vector_t::const_iterator k( in.begin() ); !skip && k != in.end(); ++k )
511 if ( k != i ) { // don't test a brush against itself
512 for ( Brush::const_iterator l( ( *k )->begin() ); !skip && l != ( *k )->end(); ++l )
514 const Face& face2 = *( *l );
516 // face opposes another face
517 if ( plane3_opposing( face1.plane3(), face2.plane3() ) ) {
518 // skip opposing planes
526 // check faces already stored
527 for ( Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m )
529 const Face& face2 = *( *m );
531 // face equals another face
532 if ( plane3_equal( face1.plane3(), face2.plane3() ) ) {
533 //if the texture/shader references should be the same but are not
534 if ( !onlyshape && !shader_equal( face1.getShader().getShader(), face2.getShader().getShader() ) ) {
537 // skip duplicate planes
542 // face1 plane intersects face2 winding or vice versa
543 if ( Winding_PlanesConcave( face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3() ) ) {
544 // result would not be convex
550 faces.push_back( &face1 );
554 for ( Faces::const_iterator i = faces.begin(); i != faces.end(); ++i )
556 if ( !brush.addFace( *( *i ) ) ) {
557 // result would have too many sides
563 brush.removeEmptyFaces();
568 void CSG_Merge( void ){
569 brush_vector_t selected_brushes;
572 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
574 if ( selected_brushes.empty() ) {
575 globalOutputStream() << "CSG Merge: No brushes selected.\n";
579 if ( selected_brushes.size() < 2 ) {
580 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
584 globalOutputStream() << "CSG Merge: Merging " << Unsigned( selected_brushes.size() ) << " brushes.\n";
586 UndoableCommand undo( "brushMerge" );
588 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
590 NodeSmartReference node( ( new BrushNode() )->node() );
591 Brush* brush = Node_getBrush( node );
592 // if the new brush would not be convex
593 if ( !Brush_merge( *brush, selected_brushes, true ) ) {
594 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
598 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after merge" );
600 // free the original brushes
601 GlobalSceneGraph().traverse( BrushDeleteSelected() );
604 Node_getTraversable( merged_path.top() )->insert( node );
605 merged_path.push( makeReference( node.get() ) );
607 selectPath( merged_path, true );
609 globalOutputStream() << "CSG Merge: Succeeded.\n";