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"
34 void Face_makeBrush( Face& face, const Brush& brush, brush_vector_t& out, float offset ){
35 if ( face.contributes() ) {
36 out.push_back( new Brush( brush ) );
37 Face* newFace = out.back()->addFace( face );
38 face.getPlane().offset( -offset );
41 newFace->flipWinding();
42 newFace->getPlane().offset( offset );
43 newFace->planeChanged();
48 void Face_extrude( Face& face, const Brush& brush, brush_vector_t& out, float offset ){
49 if ( face.contributes() ) {
50 face.getPlane().offset( offset );
51 out.push_back( new Brush( brush ) );
52 face.getPlane().offset( -offset );
53 Face* newFace = out.back()->addFace( face );
55 newFace->flipWinding();
56 newFace->planeChanged();
61 #include "preferences.h"
62 #include "texwindow.h"
64 typedef std::vector<DoubleVector3> doublevector_vector_t;
75 const char* getCaulkShader(){
76 const char* gotShader = g_pGameDescription->getKeyValue( "shader_caulk" );
77 if ( gotShader && *gotShader ){
80 return "textures/common/caulk";
85 DoubleVector3 ExclusionAxis;
88 doublevector_vector_t &exclude_vec;
90 CaulkFace( DoubleVector3 ExclusionAxis,
93 doublevector_vector_t &exclude_vec ):
94 ExclusionAxis( ExclusionAxis ),
97 exclude_vec( exclude_vec ){}
98 void operator()( Face& face ) const {
99 double dot = vector3_dot( face.getPlane().plane3().normal(), ExclusionAxis );
100 if( dot == 0 || ( dot > mindot + 0.005 && dot < maxdot - 0.005 ) ){
101 if( !exclude_vec.empty() ){
102 for ( doublevector_vector_t::const_iterator i = exclude_vec.begin(); i != exclude_vec.end(); ++i ){
103 if( ( *i ) == face.getPlane().plane3().normal() ){
108 face.SetShader( getCaulkShader() );
118 eHollowType HollowType;
119 DoubleVector3 ExclusionAxis;
122 doublevector_vector_t &exclude_vec;
126 FaceMakeBrush( const Brush& brush,
129 eHollowType HollowType,
130 DoubleVector3 ExclusionAxis,
133 doublevector_vector_t &exclude_vec,
139 HollowType( HollowType ),
140 ExclusionAxis( ExclusionAxis ),
143 exclude_vec( exclude_vec ),
145 RemoveInner( RemoveInner ){
147 void operator()( Face& face ) const {
148 double dot = vector3_dot( face.getPlane().plane3().normal(), ExclusionAxis );
149 if( dot == 0 || ( dot > mindot + 0.005 && dot < maxdot - 0.005 ) ){
150 if( !exclude_vec.empty() ){
151 for ( doublevector_vector_t::const_iterator i = exclude_vec.begin(); i != exclude_vec.end(); ++i ){
152 if( ( *i ) == face.getPlane().plane3().normal() ){
158 if( HollowType == pull ){
159 if ( face.contributes() ) {
160 face.getPlane().offset( offset );
162 out.push_back( new Brush( brush ) );
163 face.getPlane().offset( -offset );
167 Brush_forEachFace( *out.back(), CaulkFace( ExclusionAxis, mindot, maxdot, exclude_vec ) );
169 Face* newFace = out.back()->addFace( face );
170 if ( newFace != 0 ) {
171 newFace->flipWinding();
175 else if( HollowType == wrap ){
176 //Face_makeBrush( face, brush, out, offset );
177 if ( face.contributes() ) {
179 out.push_back( new Brush( brush ) );
180 if( !RemoveInner && caulk )
181 face.SetShader( getCaulkShader() );
182 Face* newFace = out.back()->addFace( face );
183 face.getPlane().offset( -offset );
186 face.SetShader( getCaulkShader() );
187 if ( newFace != 0 ) {
188 newFace->flipWinding();
189 newFace->getPlane().offset( offset );
190 newFace->planeChanged();
194 else if( HollowType == extrude ){
195 if ( face.contributes() ) {
197 out.push_back( new Brush( brush ) );
200 Face* newFace = out.back()->addFace( face );
201 if ( newFace != 0 ) {
202 newFace->getPlane().offset( offset );
203 newFace->planeChanged();
206 if( !RemoveInner && caulk )
207 face.SetShader( getCaulkShader() );
208 newFace = out.back()->addFace( face );
209 if ( newFace != 0 ) {
210 newFace->flipWinding();
212 Winding& winding = face.getWinding();
213 TextureProjection projection;
214 TexDef_Construct_Default( projection );
215 for ( Winding::iterator j = winding.begin(); j != winding.end(); ++j ){
216 std::size_t index = std::distance( winding.begin(), j );
217 std::size_t next = Winding_next( winding, index );
219 out.back()->addPlane( winding[index].vertex, winding[next].vertex, winding[next].vertex + face.getPlane().plane3().normal() * offset, TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), projection );
223 else if( HollowType == diag ){
224 if ( face.contributes() ) {
225 out.push_back( new Brush( brush ) );
228 Face* newFace = out.back()->addFace( face );
229 if ( newFace != 0 ) {
231 newFace->planeChanged();
233 newFace = out.back()->addFace( face );
235 if ( newFace != 0 ) {
236 if( !RemoveInner && caulk )
237 newFace->SetShader( getCaulkShader() );
238 newFace->flipWinding();
239 newFace->getPlane().offset( offset );
240 newFace->planeChanged();
243 Winding& winding = face.getWinding();
244 TextureProjection projection;
245 TexDef_Construct_Default( projection );
246 for ( Winding::iterator i = winding.begin(); i != winding.end(); ++i ){
247 std::size_t index = std::distance( winding.begin(), i );
248 std::size_t next = Winding_next( winding, index );
250 float bestdist = 999999;
252 for( Brush::const_iterator j = brush.begin(); j != brush.end(); ++j ){
253 Winding& winding2 = ( *j )->getWinding();
254 for ( Winding::iterator k = winding2.begin(); k != winding2.end(); ++k ){
255 std::size_t index2 = std::distance( winding2.begin(), k );
256 float testdist = vector3_length( winding[index].vertex - winding2[index2].vertex );
257 if( testdist < bestdist ){
259 BestPoint = winding2[index2].vertex;
263 out.back()->addPlane( winding[next].vertex, winding[index].vertex, BestPoint, caulk? getCaulkShader() : TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), projection );
273 DoubleVector3 ExclusionAxis;
277 FaceExclude( DoubleVector3 ExclusionAxis, double &mindot, double &maxdot )
278 : ExclusionAxis( ExclusionAxis ), mindot( mindot ), maxdot( maxdot ){
280 void operator()( Face& face ) const {
281 if( vector3_length_squared( ExclusionAxis ) != 0 ){
282 double dot = vector3_dot( face.getPlane().plane3().normal(), ExclusionAxis );
286 else if( dot > maxdot ){
296 DoubleVector3 ExclusionAxis;
299 doublevector_vector_t &exclude_vec;
301 FaceOffset( float offset, DoubleVector3 ExclusionAxis, double &mindot, double &maxdot, doublevector_vector_t &exclude_vec )
302 : offset( offset ), ExclusionAxis( ExclusionAxis ), mindot( mindot ), maxdot( maxdot ), exclude_vec( exclude_vec ){
304 void operator()( Face& face ) const {
305 double dot = vector3_dot( face.getPlane().plane3().normal(), ExclusionAxis );
306 if( dot == 0 || ( dot > mindot + 0.005 && dot < maxdot - 0.005 ) ){
307 if( !exclude_vec.empty() ){
308 for ( doublevector_vector_t::const_iterator i = exclude_vec.begin(); i != exclude_vec.end(); ++i ){
309 if( ( *i ) == face.getPlane().plane3().normal() ){
315 face.getPlane().offset( offset );
321 class FaceExcludeSelected
323 doublevector_vector_t &outvec;
325 FaceExcludeSelected( doublevector_vector_t &outvec ): outvec( outvec ){
327 void operator()( FaceInstance& face ) const {
328 if( face.isSelected() ){
329 outvec.push_back( face.getFace().getPlane().plane3().normal() );
335 DoubleVector3 getExclusion();
337 bool getRemoveInner();
339 class BrushHollowSelectedWalker : public scene::Graph::Walker
342 eHollowType HollowType;
344 BrushHollowSelectedWalker( float offset, eHollowType HollowType )
345 : offset( offset ), HollowType( HollowType ){
347 bool pre( const scene::Path& path, scene::Instance& instance ) const {
348 if ( path.top().get().visible() ) {
349 Brush* brush = Node_getBrush( path.top() );
351 && Instance_getSelectable( instance )->isSelected()
352 && path.size() > 1 ) {
354 doublevector_vector_t exclude_vec;
357 if( HollowType != room ){
358 Brush_forEachFace( *brush, FaceExclude( getExclusion(), mindot, maxdot ) );
359 if( mindot == 0 && maxdot == 0 ){
360 Brush_ForEachFaceInstance( *Instance_getBrush( instance ), FaceExcludeSelected( exclude_vec ) );
363 if( HollowType == room ){
364 Brush* tmpbrush = new Brush( *brush );
365 tmpbrush->removeEmptyFaces();
366 Brush_forEachFace( *brush, FaceMakeBrush( *brush, out, offset, pull, DoubleVector3( 0, 0, 0 ), mindot, maxdot, exclude_vec, true, true ) );
369 else if( HollowType == pull ){
370 if( !getRemoveInner() && getCaulk() ){
371 Brush_forEachFace( *brush, CaulkFace( getExclusion(), mindot, maxdot, exclude_vec ) );
373 Brush* tmpbrush = new Brush( *brush );
374 tmpbrush->removeEmptyFaces();
375 Brush_forEachFace( *tmpbrush, FaceMakeBrush( *tmpbrush, out, offset, HollowType, getExclusion(), mindot, maxdot, exclude_vec, getCaulk(), getRemoveInner() ) );
378 else if( HollowType == diag ){
379 Brush* tmpbrush = new Brush( *brush );
380 Brush_forEachFace( *tmpbrush, FaceOffset( offset, getExclusion(), mindot, maxdot, exclude_vec ) );
381 tmpbrush->removeEmptyFaces();
382 Brush_forEachFace( *tmpbrush, FaceMakeBrush( *brush, out, offset, HollowType, getExclusion(), mindot, maxdot, exclude_vec, getCaulk(), getRemoveInner() ) );
384 if( !getRemoveInner() && getCaulk() ){
385 Brush_forEachFace( *brush, CaulkFace( getExclusion(), mindot, maxdot, exclude_vec ) );
389 Brush_forEachFace( *brush, FaceMakeBrush( *brush, out, offset, HollowType, getExclusion(), mindot, maxdot, exclude_vec, getCaulk(), getRemoveInner() ) );
391 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
393 ( *i )->removeEmptyFaces();
394 if( ( *i )->hasContributingFaces() ){
395 NodeSmartReference node( ( new BrushNode() )->node() );
396 Node_getBrush( node )->copy( *( *i ) );
398 Node_getTraversable( path.parent() )->insert( node );
399 //path.push( makeReference( node.get() ) );
400 //selectPath( path, true );
401 //Instance_getSelectable( *GlobalSceneGraph().find( path ) )->setSelected( true );
402 //Path_deleteTop( path );
411 typedef std::list<Brush*> brushlist_t;
413 class BrushGatherSelected : public scene::Graph::Walker
415 brush_vector_t& m_brushlist;
417 BrushGatherSelected( brush_vector_t& brushlist )
418 : m_brushlist( brushlist ){
421 bool pre( const scene::Path& path, scene::Instance& instance ) const {
422 if ( path.top().get().visible() ) {
423 Brush* brush = Node_getBrush( path.top() );
425 && Instance_getSelectable( instance )->isSelected() ) {
426 m_brushlist.push_back( brush );
433 class BrushDeleteSelected : public scene::Graph::Walker
436 bool pre( const scene::Path& path, scene::Instance& instance ) const {
439 void post( const scene::Path& path, scene::Instance& instance ) const {
440 if ( path.top().get().visible() ) {
441 Brush* brush = Node_getBrush( path.top() );
443 && Instance_getSelectable( instance )->isSelected()
444 && path.size() > 1 ) {
445 Path_deleteTop( path );
453 class BrushDeleteSelected : public scene::Graph::Walker
455 scene::Node* m_keepNode;
456 mutable bool m_eraseParent;
458 BrushDeleteSelected( scene::Node* keepNode ): m_keepNode( keepNode ), m_eraseParent( false ){
460 BrushDeleteSelected(): m_keepNode( NULL ), m_eraseParent( false ){
462 bool pre( const scene::Path& path, scene::Instance& instance ) const {
465 void post( const scene::Path& path, scene::Instance& instance ) const {
466 //globalOutputStream() << path.size() << "\n";
467 if ( path.top().get().visible() ) {
468 Brush* brush = Node_getBrush( path.top() );
470 && Instance_getSelectable( instance )->isSelected()
471 && path.size() > 1 ) {
472 scene::Node& parent = path.parent();
473 Path_deleteTop( path );
474 if( Node_getTraversable( parent )->empty() ){
475 m_eraseParent = true;
476 //globalOutputStream() << "Empty node?!.\n";
480 if( m_eraseParent && !Node_isPrimitive( path.top() ) && path.size() > 1 ){
481 //globalOutputStream() << "about to Delete empty node!.\n";
482 m_eraseParent = false;
483 Entity* entity = Node_getEntity( path.top() );
484 if ( entity != 0 && path.top().get_pointer() != Map_FindWorldspawn( g_map )
485 && Node_getTraversable( path.top() )->empty() && path.top().get_pointer() != m_keepNode ) {
486 //globalOutputStream() << "now Deleting empty node!.\n";
487 Path_deleteTop( path );
498 void CSG_MakeRoom( void ){
499 UndoableCommand undo( "makeRoom" );
500 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( GetGridSize(), room ) );
501 GlobalSceneGraph().traverse( BrushDeleteSelected() );
505 template<typename Type>
506 class RemoveReference
512 template<typename Type>
513 class RemoveReference<Type&>
519 template<typename Functor>
522 const Functor& functor;
524 Dereference( const Functor& functor ) : functor( functor ){
526 get_result_type<Functor> operator()( typename RemoveReference<get_argument<Functor, 0>>::type *firstArgument ) const {
527 return functor( *firstArgument );
531 template<typename Functor>
532 inline Dereference<Functor> makeDereference( const Functor& functor ){
533 return Dereference<Functor>( functor );
536 typedef Face* FacePointer;
537 const FacePointer c_nullFacePointer = 0;
539 template<typename Predicate>
540 Face* Brush_findIf( const Brush& brush, const Predicate& predicate ){
541 Brush::const_iterator i = std::find_if( brush.begin(), brush.end(), makeDereference( predicate ) );
542 return i == brush.end() ? c_nullFacePointer : *i; // uses c_nullFacePointer instead of 0 because otherwise gcc 4.1 attempts conversion to int
545 template<typename Caller>
548 typedef get_argument<Caller, 1> FirstBound;
549 FirstBound firstBound;
551 BindArguments1( FirstBound firstBound )
552 : firstBound( firstBound ){
555 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
556 return Caller::call( firstArgument, firstBound );
560 template<typename Caller>
563 typedef get_argument<Caller, 1> FirstBound;
564 typedef get_argument<Caller, 2> SecondBound;
565 FirstBound firstBound;
566 SecondBound secondBound;
568 BindArguments2( FirstBound firstBound, SecondBound secondBound )
569 : firstBound( firstBound ), secondBound( secondBound ){
572 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
573 return Caller::call( firstArgument, firstBound, secondBound );
577 template<typename Caller, typename FirstBound, typename SecondBound>
578 BindArguments2<Caller> bindArguments( const Caller& caller, FirstBound firstBound, SecondBound secondBound ){
579 return BindArguments2<Caller>( firstBound, secondBound );
582 inline bool Face_testPlane( const Face& face, const Plane3& plane, bool flipped ){
583 return face.contributes() && !Winding_TestPlane( face.getWinding(), plane, flipped );
586 typedef Function<bool ( const Face &, const Plane3 &, bool ), Face_testPlane> FaceTestPlane;
589 /// \brief Returns true if
590 /// \li !flipped && brush is BACK or ON
591 /// \li flipped && brush is FRONT or ON
592 bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
593 brush.evaluateBRep();
595 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
597 if ( Face_testPlane( *( *i ), plane, flipped ) ) {
603 return Brush_findIf( brush, bindArguments( FaceTestPlane(), makeReference( plane ), flipped ) ) == 0;
607 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
608 brush.evaluateBRep();
610 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
612 if ( ( *i )->contributes() ) {
613 split += Winding_ClassifyPlane( ( *i )->getWinding(), plane );
619 bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret_fragments ){
620 if ( aabb_intersects_aabb( brush.localAABB(), other.localAABB() ) ) {
621 brush_vector_t fragments;
622 fragments.reserve( other.size() );
625 for ( Brush::const_iterator i( other.begin() ); i != other.end(); ++i )
627 if ( ( *i )->contributes() ) {
628 brushsplit_t split = Brush_classifyPlane( back, ( *i )->plane3() );
629 if ( split.counts[ePlaneFront] != 0
630 && split.counts[ePlaneBack] != 0 ) {
631 fragments.push_back( new Brush( back ) );
632 Face* newFace = fragments.back()->addFace( *( *i ) );
633 if ( newFace != 0 ) {
634 newFace->flipWinding();
636 back.addFace( *( *i ) );
638 else if ( split.counts[ePlaneBack] == 0 ) {
639 for ( brush_vector_t::iterator i = fragments.begin(); i != fragments.end(); ++i )
647 ret_fragments.insert( ret_fragments.end(), fragments.begin(), fragments.end() );
653 class SubtractBrushesFromUnselected : public scene::Graph::Walker
655 const brush_vector_t& m_brushlist;
656 std::size_t& m_before;
657 std::size_t& m_after;
658 mutable bool m_eraseParent;
660 SubtractBrushesFromUnselected( const brush_vector_t& brushlist, std::size_t& before, std::size_t& after )
661 : m_brushlist( brushlist ), m_before( before ), m_after( after ), m_eraseParent( false ){
664 bool pre( const scene::Path& path, scene::Instance& instance ) const {
665 if ( path.top().get().visible() ) {
671 void post( const scene::Path& path, scene::Instance& instance ) const {
672 if ( path.top().get().visible() ) {
673 Brush* brush = Node_getBrush( path.top() );
675 && !Instance_getSelectable( instance )->isSelected() ) {
676 brush_vector_t buffer[2];
678 Brush* original = new Brush( *brush );
679 buffer[static_cast<std::size_t>( swap )].push_back( original );
682 for ( brush_vector_t::const_iterator i( m_brushlist.begin() ); i != m_brushlist.end(); ++i )
684 for ( brush_vector_t::iterator j( buffer[static_cast<std::size_t>( swap )].begin() ); j != buffer[static_cast<std::size_t>( swap )].end(); ++j )
686 if ( Brush_subtract( *( *j ), *( *i ), buffer[static_cast<std::size_t>( !swap )] ) ) {
691 buffer[static_cast<std::size_t>( !swap )].push_back( ( *j ) );
694 buffer[static_cast<std::size_t>( swap )].clear();
699 brush_vector_t& out = buffer[static_cast<std::size_t>( swap )];
701 if ( out.size() == 1 && out.back() == original ) {
707 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
710 ( *i )->removeEmptyFaces();
711 if ( !( *i )->empty() ) {
712 NodeSmartReference node( ( new BrushNode() )->node() );
713 Node_getBrush( node )->copy( *( *i ) );
715 Node_getTraversable( path.parent() )->insert( node );
721 scene::Node& parent = path.parent();
722 Path_deleteTop( path );
723 if( Node_getTraversable( parent )->empty() ){
724 m_eraseParent = true;
729 if( m_eraseParent && !Node_isPrimitive( path.top() ) && path.size() > 1 ){
730 m_eraseParent = false;
731 Entity* entity = Node_getEntity( path.top() );
732 if ( entity != 0 && path.top().get_pointer() != Map_FindWorldspawn( g_map )
733 && Node_getTraversable( path.top() )->empty() ) {
734 Path_deleteTop( path );
741 brush_vector_t selected_brushes;
742 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
744 if ( selected_brushes.empty() ) {
745 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
749 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned( selected_brushes.size() ) << " brushes.\n";
751 UndoableCommand undo( "brushSubtract" );
753 // subtract selected from unselected
754 std::size_t before = 0;
755 std::size_t after = 0;
756 GlobalSceneGraph().traverse( SubtractBrushesFromUnselected( selected_brushes, before, after ) );
757 globalOutputStream() << "CSG Subtract: Result: "
758 << Unsigned( after ) << " fragment" << ( after == 1 ? "" : "s" )
759 << " from " << Unsigned( before ) << " brush" << ( before == 1 ? "" : "es" ) << ".\n";
765 class BrushSplitByPlaneSelected : public scene::Graph::Walker
770 const char* m_shader;
771 const TextureProjection& m_projection;
774 BrushSplitByPlaneSelected( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split )
775 : m_p0( p0 ), m_p1( p1 ), m_p2( p2 ), m_shader( shader ), m_projection( projection ), m_split( split ){
778 bool pre( const scene::Path& path, scene::Instance& instance ) const {
782 void post( const scene::Path& path, scene::Instance& instance ) const {
783 if ( path.top().get().visible() ) {
784 Brush* brush = Node_getBrush( path.top() );
786 && Instance_getSelectable( instance )->isSelected() ) {
787 Plane3 plane( plane3_for_points( m_p0, m_p1, m_p2 ) );
788 if ( plane3_valid( plane ) ) {
789 brushsplit_t split = Brush_classifyPlane( *brush, m_split == eFront ? plane3_flipped( plane ) : plane );
790 if ( split.counts[ePlaneBack] && split.counts[ePlaneFront] ) {
791 // the plane intersects this brush
792 if ( m_split == eFrontAndBack ) {
793 NodeSmartReference node( ( new BrushNode() )->node() );
794 Brush* fragment = Node_getBrush( node );
795 fragment->copy( *brush );
796 Face* newFace = fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
797 if ( newFace != 0 && m_split != eFront ) {
798 newFace->flipWinding();
800 fragment->removeEmptyFaces();
801 ASSERT_MESSAGE( !fragment->empty(), "brush left with no faces after split" );
803 Node_getTraversable( path.parent() )->insert( node );
805 scene::Path fragmentPath = path;
806 fragmentPath.top() = makeReference( node.get() );
807 selectPath( fragmentPath, true );
811 Face* newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
812 if ( newFace != 0 && m_split == eFront ) {
813 newFace->flipWinding();
815 brush->removeEmptyFaces();
816 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after split" );
819 // the plane does not intersect this brush
820 if ( m_split != eFrontAndBack && split.counts[ePlaneBack] != 0 ) {
821 // the brush is "behind" the plane
822 Path_deleteTop( path );
830 void Scene_BrushSplitByPlane( scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split ){
831 TextureProjection projection;
832 TexDef_Construct_Default( projection );
833 graph.traverse( BrushSplitByPlaneSelected( p0, p1, p2, shader, projection, split ) );
838 class BrushInstanceSetClipPlane : public scene::Graph::Walker
842 BrushInstanceSetClipPlane( const Plane3& plane )
846 bool pre( const scene::Path& path, scene::Instance& instance ) const {
847 BrushInstance* brush = Instance_getBrush( instance );
849 && path.top().get().visible()
850 && brush->isSelected() ) {
851 BrushInstance& brushInstance = *brush;
852 brushInstance.setClipPlane( m_plane );
858 void Scene_BrushSetClipPlane( scene::Graph& graph, const Plane3& plane ){
859 graph.traverse( BrushInstanceSetClipPlane( plane ) );
867 bool Brush_merge( Brush& brush, const brush_vector_t& in, bool onlyshape ){
868 // gather potential outer faces
871 typedef std::vector<const Face*> Faces;
873 for ( brush_vector_t::const_iterator i( in.begin() ); i != in.end(); ++i )
875 ( *i )->evaluateBRep();
876 for ( Brush::const_iterator j( ( *i )->begin() ); j != ( *i )->end(); ++j )
878 if ( !( *j )->contributes() ) {
882 const Face& face1 = *( *j );
885 // test faces of all input brushes
886 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
887 for ( brush_vector_t::const_iterator k( in.begin() ); !skip && k != in.end(); ++k )
889 if ( k != i ) { // don't test a brush against itself
890 for ( Brush::const_iterator l( ( *k )->begin() ); !skip && l != ( *k )->end(); ++l )
892 const Face& face2 = *( *l );
894 // face opposes another face
895 if ( plane3_opposing( face1.plane3(), face2.plane3() ) ) {
896 // skip opposing planes
904 // check faces already stored
905 for ( Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m )
907 const Face& face2 = *( *m );
909 // face equals another face
910 if ( plane3_equal( face1.plane3(), face2.plane3() ) ) {
911 //if the texture/shader references should be the same but are not
912 if ( !onlyshape && !shader_equal( face1.getShader().getShader(), face2.getShader().getShader() ) ) {
915 // skip duplicate planes
920 // face1 plane intersects face2 winding or vice versa
921 if ( Winding_PlanesConcave( face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3() ) ) {
922 // result would not be convex
928 faces.push_back( &face1 );
932 for ( Faces::const_iterator i = faces.begin(); i != faces.end(); ++i )
934 if ( !brush.addFace( *( *i ) ) ) {
935 // result would have too many sides
941 brush.removeEmptyFaces();
946 void CSG_Merge( void ){
947 brush_vector_t selected_brushes;
950 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
952 if ( selected_brushes.empty() ) {
953 globalOutputStream() << "CSG Merge: No brushes selected.\n";
957 if ( selected_brushes.size() < 2 ) {
958 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
962 globalOutputStream() << "CSG Merge: Merging " << Unsigned( selected_brushes.size() ) << " brushes.\n";
964 UndoableCommand undo( "brushMerge" );
966 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
968 NodeSmartReference node( ( new BrushNode() )->node() );
969 Brush* brush = Node_getBrush( node );
970 // if the new brush would not be convex
971 if ( !Brush_merge( *brush, selected_brushes, true ) ) {
972 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
976 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after merge" );
978 // free the original brushes
979 GlobalSceneGraph().traverse( BrushDeleteSelected( merged_path.parent().get_pointer() ) );
982 Node_getTraversable( merged_path.top() )->insert( node );
983 merged_path.push( makeReference( node.get() ) );
985 selectPath( merged_path, true );
987 globalOutputStream() << "CSG Merge: Succeeded.\n";
1002 #include "mainframe.h"
1003 #include <gtk/gtk.h>
1004 #include "gtkutil/dialog.h"
1005 #include "gtkutil/button.h"
1006 #include "gtkutil/accelerator.h"
1007 #include "xywindow.h"
1008 #include "camwindow.h"
1010 struct CSGToolDialog
1012 GtkSpinButton* spin;
1013 bool allocated{false};
1014 ui::Window window{ui::null};
1015 GtkToggleButton *radFaces, *radProj, *radCam, *caulk, *removeInner;
1018 CSGToolDialog g_csgtool_dialog;
1020 DoubleVector3 getExclusion(){
1021 if( gtk_toggle_button_get_active( g_csgtool_dialog.radProj ) ){
1022 if( GlobalXYWnd_getCurrentViewType() == YZ ){
1023 return DoubleVector3( 1, 0, 0 );
1025 else if( GlobalXYWnd_getCurrentViewType() == XZ ){
1026 return DoubleVector3( 0, 1, 0 );
1028 else if( GlobalXYWnd_getCurrentViewType() == XY ){
1029 return DoubleVector3( 0, 0, 1 );
1032 if( gtk_toggle_button_get_active( g_csgtool_dialog.radCam ) ){
1033 Vector3 angles( Camera_getAngles( *g_pParentWnd->GetCamWnd() ) );
1034 // globalOutputStream() << angles << " angles\n";
1035 DoubleVector3 radangles( degrees_to_radians( angles[0] ), degrees_to_radians( angles[1] ), degrees_to_radians( angles[2] ) );
1036 // globalOutputStream() << radangles << " radangles\n";
1037 // x = cos(yaw)*cos(pitch)
1038 // y = sin(yaw)*cos(pitch)
1040 DoubleVector3 viewvector;
1041 viewvector[0] = cos( radangles[1] ) * cos( radangles[0] );
1042 viewvector[1] = sin( radangles[1] ) * cos( radangles[0] );
1043 viewvector[2] = sin( radangles[0] );
1044 // globalOutputStream() << viewvector << " viewvector\n";
1047 return DoubleVector3( 0, 0, 0 );
1051 if( gtk_toggle_button_get_active( g_csgtool_dialog.caulk ) ){
1057 bool getRemoveInner(){
1058 if( gtk_toggle_button_get_active( g_csgtool_dialog.removeInner ) ){
1064 class BrushFaceOffset
1068 BrushFaceOffset( float offset )
1071 void operator()( BrushInstance& brush ) const {
1074 doublevector_vector_t exclude_vec;
1075 Brush_forEachFace( brush, FaceExclude( getExclusion(), mindot, maxdot ) );
1076 if( mindot == 0 && maxdot == 0 ){
1077 Brush_ForEachFaceInstance( brush, FaceExcludeSelected( exclude_vec ) );
1079 Brush_forEachFace( brush, FaceOffset( offset, getExclusion(), mindot, maxdot, exclude_vec ) );
1083 //=================DLG
1085 static gboolean CSGdlg_HollowDiag( GtkWidget *widget, CSGToolDialog* dialog ){
1086 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
1087 UndoableCommand undo( "brushHollow::Diag" );
1088 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( offset, diag ) );
1089 if( getRemoveInner() )
1090 GlobalSceneGraph().traverse( BrushDeleteSelected() );
1091 SceneChangeNotify();
1095 static gboolean CSGdlg_HollowWrap( GtkWidget *widget, CSGToolDialog* dialog ){
1096 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
1097 UndoableCommand undo( "brushHollow::Wrap" );
1098 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( offset, wrap ) );
1099 if( getRemoveInner() )
1100 GlobalSceneGraph().traverse( BrushDeleteSelected() );
1101 SceneChangeNotify();
1105 static gboolean CSGdlg_HollowExtrude( GtkWidget *widget, CSGToolDialog* dialog ){
1106 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
1107 UndoableCommand undo( "brushHollow::Extrude" );
1108 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( offset, extrude ) );
1109 if( getRemoveInner() )
1110 GlobalSceneGraph().traverse( BrushDeleteSelected() );
1111 SceneChangeNotify();
1115 static gboolean CSGdlg_HollowPull( GtkWidget *widget, CSGToolDialog* dialog ){
1116 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
1117 UndoableCommand undo( "brushHollow::Pull" );
1118 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( offset, pull ) );
1119 if( getRemoveInner() )
1120 GlobalSceneGraph().traverse( BrushDeleteSelected() );
1121 SceneChangeNotify();
1125 static gboolean CSGdlg_BrushShrink( GtkWidget *widget, CSGToolDialog* dialog ){
1126 gtk_spin_button_update ( dialog->spin );
1127 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
1129 UndoableCommand undo( "Shrink brush" );
1130 // GlobalSceneGraph().traverse( OffsetBrushFacesSelectedWalker( offset ) );
1131 //Scene_ForEachSelectedBrush_ForEachFace( GlobalSceneGraph(), BrushFaceOffset( offset ) );
1132 Scene_forEachSelectedBrush( BrushFaceOffset( offset ) );
1133 SceneChangeNotify();
1137 static gboolean CSGdlg_BrushExpand( GtkWidget *widget, CSGToolDialog* dialog ){
1138 gtk_spin_button_update ( dialog->spin );
1139 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
1140 UndoableCommand undo( "Expand brush" );
1141 // GlobalSceneGraph().traverse( OffsetBrushFacesSelectedWalker( offset ) );
1142 //Scene_ForEachSelectedBrush_ForEachFace( GlobalSceneGraph(), BrushFaceOffset( offset ) );
1143 Scene_forEachSelectedBrush( BrushFaceOffset( offset ) );
1144 SceneChangeNotify();
1148 static gboolean CSGdlg_grid2spin( GtkWidget *widget, CSGToolDialog* dialog ){
1149 gtk_spin_button_set_value( dialog->spin, GetGridSize() );
1153 static gboolean CSGdlg_delete( GtkWidget *widget, GdkEventAny *event, CSGToolDialog* dialog ){
1154 gtk_widget_hide( GTK_WIDGET( dialog->window ) );
1159 // FIXME: there is probably improvements to do less raw GTK stuff, more GTK wrapper
1160 if ( !g_csgtool_dialog.allocated ) {
1161 g_csgtool_dialog.allocated = true;
1162 g_csgtool_dialog.window = MainFrame_getWindow().create_dialog_window( "CSG Tool", G_CALLBACK( CSGdlg_delete ), &g_csgtool_dialog );
1163 gtk_window_set_type_hint( g_csgtool_dialog.window, GDK_WINDOW_TYPE_HINT_UTILITY );
1165 //GtkAccelGroup* accel = gtk_accel_group_new();
1166 //gtk_window_add_accel_group( g_csgtool_dialog.window, accel );
1167 global_accel_connect_window( g_csgtool_dialog.window );
1170 auto hbox = create_dialog_hbox( 4, 4 );
1171 gtk_container_add( GTK_CONTAINER( g_csgtool_dialog.window ), GTK_WIDGET( hbox ) );
1173 auto table = create_dialog_table( 3, 8, 4, 4 );
1174 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1176 //GtkWidget* label = gtk_label_new( "<->" );
1177 //gtk_widget_show( label );
1178 auto button = ui::Button( "Grid->" );
1179 table.attach( button, {0, 1, 0, 1}, {0, 0} );
1181 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_grid2spin ), &g_csgtool_dialog );
1184 GtkAdjustment* adj = GTK_ADJUSTMENT( gtk_adjustment_new( 16, 0, 9999, 1, 10, 0 ) );
1185 GtkSpinButton* spin = GTK_SPIN_BUTTON( gtk_spin_button_new( adj, 1, 3 ) );
1186 gtk_widget_show( GTK_WIDGET( spin ) );
1187 gtk_widget_set_tooltip_text( GTK_WIDGET( spin ), "Thickness" );
1188 gtk_table_attach( table, GTK_WIDGET( spin ), 1, 2, 0, 1,
1189 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1190 (GtkAttachOptions) ( 0 ), 0, 0 );
1191 gtk_widget_set_size_request( GTK_WIDGET( spin ), 64, -1 );
1192 gtk_spin_button_set_numeric( spin, TRUE );
1194 g_csgtool_dialog.spin = spin;
1197 //radio button group for choosing the exclude axis
1198 GtkWidget* radFaces = gtk_radio_button_new_with_label( NULL, "-faces" );
1199 gtk_widget_set_tooltip_text( radFaces, "Exclude selected faces" );
1200 GtkWidget* radProj = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(radFaces), "-proj" );
1201 gtk_widget_set_tooltip_text( radProj, "Exclude faces, most orthogonal to active projection" );
1202 GtkWidget* radCam = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(radFaces), "-cam" );
1203 gtk_widget_set_tooltip_text( radCam, "Exclude faces, most orthogonal to camera view" );
1205 gtk_widget_show( radFaces );
1206 gtk_widget_show( radProj );
1207 gtk_widget_show( radCam );
1209 gtk_table_attach( table, radFaces, 2, 3, 0, 1,
1210 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1211 (GtkAttachOptions) ( 0 ), 0, 0 );
1212 gtk_table_attach( table, radProj, 3, 4, 0, 1,
1213 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1214 (GtkAttachOptions) ( 0 ), 0, 0 );
1215 gtk_table_attach( table, radCam, 4, 5, 0, 1,
1216 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1217 (GtkAttachOptions) ( 0 ), 0, 0 );
1219 g_csgtool_dialog.radFaces = GTK_TOGGLE_BUTTON( radFaces );
1220 g_csgtool_dialog.radProj = GTK_TOGGLE_BUTTON( radProj );
1221 g_csgtool_dialog.radCam = GTK_TOGGLE_BUTTON( radCam );
1224 GtkWidget* button = gtk_toggle_button_new();
1225 auto ubutton = ui::Button::from( button );
1226 button_set_icon( ubutton, "f-caulk.png" );
1227 gtk_button_set_relief( GTK_BUTTON( button ), GTK_RELIEF_NONE );
1228 table.attach( ubutton, { 6, 7, 0, 1 }, { GTK_EXPAND, 0 } );
1229 gtk_widget_set_tooltip_text( button, "Caulk some faces" );
1230 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), TRUE );
1232 g_csgtool_dialog.caulk = GTK_TOGGLE_BUTTON( button );
1235 GtkWidget* button = gtk_toggle_button_new();
1236 auto ubutton = ui::Button::from( button );
1237 button_set_icon( ubutton, "csgtool_removeinner.png" );
1238 gtk_button_set_relief( GTK_BUTTON( button ), GTK_RELIEF_NONE );
1239 table.attach( ubutton, { 7, 8, 0, 1 }, { GTK_EXPAND, 0 } );
1240 gtk_widget_set_tooltip_text( button, "Remove inner brush" );
1241 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), TRUE );
1243 g_csgtool_dialog.removeInner = GTK_TOGGLE_BUTTON( button );
1246 GtkWidget* sep = gtk_hseparator_new();
1247 gtk_widget_show( sep );
1248 gtk_table_attach( table, sep, 0, 8, 1, 2,
1249 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1250 (GtkAttachOptions) ( 0 ), 0, 0 );
1253 GtkWidget* button = gtk_button_new();
1254 auto ubutton = ui::Button::from( button );
1255 button_set_icon( ubutton, "csgtool_shrink.png" );
1256 table.attach( ubutton, { 0, 1, 2, 3 }, { GTK_EXPAND, 0 } );
1257 gtk_widget_set_tooltip_text( button, "Shrink brush" );
1259 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_BrushShrink ), &g_csgtool_dialog );
1262 GtkWidget* button = gtk_button_new();
1263 auto ubutton = ui::Button::from( button );
1264 button_set_icon( ubutton, "csgtool_expand.png" );
1265 table.attach( ubutton, { 1, 2, 2, 3 }, { GTK_EXPAND, 0 } );
1266 gtk_widget_set_tooltip_text( button, "Expand brush" );
1268 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_BrushExpand ), &g_csgtool_dialog );
1271 GtkWidget* button = gtk_button_new();
1272 auto ubutton = ui::Button::from( button );
1273 button_set_icon( ubutton, "csgtool_diagonal.png" );
1274 table.attach( ubutton, { 3, 4, 2, 3 }, { GTK_EXPAND, 0 } );
1275 gtk_widget_set_tooltip_text( button, "Hollow::diagonal joints" );
1277 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_HollowDiag ), &g_csgtool_dialog );
1280 GtkWidget* button = gtk_button_new();
1281 auto ubutton = ui::Button::from( button );
1282 button_set_icon( ubutton, "csgtool_wrap.png" );
1283 table.attach( ubutton, { 4, 5, 2, 3 }, { GTK_EXPAND, 0 } );
1284 gtk_widget_set_tooltip_text( button, "Hollow::warp" );
1286 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_HollowWrap ), &g_csgtool_dialog );
1289 GtkWidget* button = gtk_button_new();
1290 auto ubutton = ui::Button::from( button );
1291 button_set_icon( ubutton, "csgtool_extrude.png" );
1292 table.attach( ubutton, { 5, 6, 2, 3 }, { GTK_EXPAND, 0 } );
1293 gtk_widget_set_tooltip_text( button, "Hollow::extrude faces" );
1295 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_HollowExtrude ), &g_csgtool_dialog );
1298 GtkWidget* button = gtk_button_new();
1299 auto ubutton = ui::Button::from( button );
1300 button_set_icon( ubutton, "csgtool_pull.png" );
1301 table.attach( ubutton, { 6, 7, 2, 3 }, { GTK_EXPAND, 0 } );
1302 gtk_widget_set_tooltip_text( button, "Hollow::pull faces" );
1304 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_HollowPull ), &g_csgtool_dialog );
1311 gtk_widget_show( GTK_WIDGET( g_csgtool_dialog.window ) );
1312 gtk_window_present( g_csgtool_dialog.window );