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 std::shared_ptr<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 std::shared_ptr<Face> newFace = out.back()->addFace( face );
55 newFace->flipWinding();
56 newFace->planeChanged();
61 #include "preferences.h"
62 #include "texwindow.h"
74 const char* getCaulkShader(){
75 const char* gotShader = g_pGameDescription->getKeyValue( "shader_caulk" );
76 if ( gotShader && *gotShader ){
79 return "textures/common/caulk";
84 DoubleVector3 ExclusionAxis;
88 CaulkFace( DoubleVector3 ExclusionAxis,
91 ExclusionAxis( ExclusionAxis ),
94 void operator()( Face& face ) const {
95 double dot = vector3_dot( face.getPlane().plane3().normal(), ExclusionAxis );
96 if( dot == 0 || ( dot > mindot + 0.005 && dot < maxdot - 0.005 ) )
97 face.SetShader( getCaulkShader() );
106 eHollowType HollowType;
107 DoubleVector3 ExclusionAxis;
113 FaceMakeBrush( const Brush& brush,
116 eHollowType HollowType,
117 DoubleVector3 ExclusionAxis,
125 HollowType( HollowType ),
126 ExclusionAxis( ExclusionAxis ),
130 RemoveInner( RemoveInner ){
132 void operator()( Face& face ) const {
133 double dot = vector3_dot( face.getPlane().plane3().normal(), ExclusionAxis );
134 if( dot == 0 || ( dot > mindot + 0.005 && dot < maxdot - 0.005 ) ){
135 if( HollowType == pull ){
136 if ( face.contributes() ) {
137 face.getPlane().offset( offset );
139 out.push_back( new Brush( brush ) );
140 face.getPlane().offset( -offset );
144 Brush_forEachFace( *out.back(), CaulkFace( ExclusionAxis, mindot, maxdot ) );
146 std::shared_ptr<Face> newFace = out.back()->addFace( face );
147 if ( newFace != 0 ) {
148 newFace->flipWinding();
152 else if( HollowType == wrap ){
153 //Face_makeBrush( face, brush, out, offset );
154 if ( face.contributes() ) {
156 out.push_back( new Brush( brush ) );
157 if( !RemoveInner && caulk )
158 face.SetShader( getCaulkShader() );
159 std::shared_ptr<Face> newFace = out.back()->addFace( face );
160 face.getPlane().offset( -offset );
163 face.SetShader( getCaulkShader() );
164 if ( newFace != 0 ) {
165 newFace->flipWinding();
166 newFace->getPlane().offset( offset );
167 newFace->planeChanged();
171 else if( HollowType == extrude ){
172 if ( face.contributes() ) {
174 out.push_back( new Brush( brush ) );
177 std::shared_ptr<Face> newFace = out.back()->addFace( face );
178 if ( newFace != 0 ) {
179 newFace->getPlane().offset( offset );
180 newFace->planeChanged();
183 if( !RemoveInner && caulk )
184 face.SetShader( getCaulkShader() );
185 newFace = out.back()->addFace( face );
186 if ( newFace != 0 ) {
187 newFace->flipWinding();
189 Winding& winding = face.getWinding();
190 TextureProjection projection;
191 TexDef_Construct_Default( projection );
192 for ( Winding::iterator j = winding.begin(); j != winding.end(); ++j ){
193 std::size_t index = std::distance( winding.begin(), j );
194 std::size_t next = Winding_next( winding, index );
196 out.back()->addPlane( winding[index].vertex, winding[next].vertex, winding[next].vertex + face.getPlane().plane3().normal() * offset, TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), projection );
200 else if( HollowType == diag ){
201 if ( face.contributes() ) {
202 out.push_back( new Brush( brush ) );
205 std::shared_ptr<Face> newFace = out.back()->addFace( face );
206 if ( newFace != 0 ) {
208 newFace->planeChanged();
210 newFace = out.back()->addFace( face );
212 if ( newFace != 0 ) {
213 if( !RemoveInner && caulk )
214 newFace->SetShader( getCaulkShader() );
215 newFace->flipWinding();
216 newFace->getPlane().offset( offset );
217 newFace->planeChanged();
220 Winding& winding = face.getWinding();
221 TextureProjection projection;
222 TexDef_Construct_Default( projection );
223 for ( Winding::iterator i = winding.begin(); i != winding.end(); ++i ){
224 std::size_t index = std::distance( winding.begin(), i );
225 std::size_t next = Winding_next( winding, index );
227 float bestdist = 999999;
229 for( Brush::const_iterator j = brush.begin(); j != brush.end(); ++j ){
230 Winding& winding2 = ( *j )->getWinding();
231 for ( Winding::iterator k = winding2.begin(); k != winding2.end(); ++k ){
232 std::size_t index2 = std::distance( winding2.begin(), k );
233 float testdist = vector3_length( winding[index].vertex - winding2[index2].vertex );
234 if( testdist < bestdist ){
236 BestPoint = winding2[index2].vertex;
240 out.back()->addPlane( winding[next].vertex, winding[index].vertex, BestPoint, caulk? getCaulkShader() : TextureBrowser_GetSelectedShader( GlobalTextureBrowser() ), projection );
250 DoubleVector3 ExclusionAxis;
254 FaceExclude( DoubleVector3 ExclusionAxis, double &mindot, double &maxdot )
255 : ExclusionAxis( ExclusionAxis ), mindot( mindot ), maxdot( maxdot ){
257 void operator()( Face& face ) const {
258 if( vector3_length_squared( ExclusionAxis ) != 0 ){
259 double dot = vector3_dot( face.getPlane().plane3().normal(), ExclusionAxis );
263 else if( dot > maxdot ){
273 DoubleVector3 ExclusionAxis;
277 FaceOffset( float offset, DoubleVector3 ExclusionAxis, double &mindot, double &maxdot )
278 : offset( offset ), ExclusionAxis( ExclusionAxis ), mindot( mindot ), maxdot( maxdot ){
280 void operator()( Face& face ) const {
281 double dot = vector3_dot( face.getPlane().plane3().normal(), ExclusionAxis );
282 if( dot == 0 || ( dot > mindot + 0.005 && dot < maxdot - 0.005 ) ){
284 face.getPlane().offset( offset );
291 DoubleVector3 getExclusion();
293 bool getRemoveInner();
295 class BrushHollowSelectedWalker : public scene::Graph::Walker
298 eHollowType HollowType;
300 BrushHollowSelectedWalker( float offset, eHollowType HollowType )
301 : offset( offset ), HollowType( HollowType ){
303 bool pre( const scene::Path& path, scene::Instance& instance ) const {
304 if ( path.top().get().visible() ) {
305 Brush* brush = Node_getBrush( path.top() );
307 && Instance_getSelectable( instance )->isSelected()
308 && path.size() > 1 ) {
312 if( HollowType != room ){
313 Brush_forEachFace( *brush, FaceExclude( getExclusion(), mindot, maxdot ) );
315 if( HollowType == room ){
316 Brush* tmpbrush = new Brush( *brush );
317 tmpbrush->removeEmptyFaces();
318 Brush_forEachFace( *brush, FaceMakeBrush( *brush, out, offset, pull, DoubleVector3( 0, 0, 0 ), mindot, maxdot, true, true ) );
321 else if( HollowType == pull ){
322 if( !getRemoveInner() && getCaulk() ){
323 Brush_forEachFace( *brush, CaulkFace( getExclusion(), mindot, maxdot ) );
325 Brush* tmpbrush = new Brush( *brush );
326 tmpbrush->removeEmptyFaces();
327 Brush_forEachFace( *tmpbrush, FaceMakeBrush( *tmpbrush, out, offset, HollowType, getExclusion(), mindot, maxdot, getCaulk(), getRemoveInner() ) );
330 else if( HollowType == diag ){
331 Brush* tmpbrush = new Brush( *brush );
332 Brush_forEachFace( *tmpbrush, FaceOffset( offset, getExclusion(), mindot, maxdot ) );
333 tmpbrush->removeEmptyFaces();
334 Brush_forEachFace( *tmpbrush, FaceMakeBrush( *brush, out, offset, HollowType, getExclusion(), mindot, maxdot, getCaulk(), getRemoveInner() ) );
336 if( !getRemoveInner() && getCaulk() ){
337 Brush_forEachFace( *brush, CaulkFace( getExclusion(), mindot, maxdot ) );
341 Brush_forEachFace( *brush, FaceMakeBrush( *brush, out, offset, HollowType, getExclusion(), mindot, maxdot, getCaulk(), getRemoveInner() ) );
343 for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
345 ( *i )->removeEmptyFaces();
346 if( ( *i )->hasContributingFaces() ){
347 NodeSmartReference node( ( new BrushNode() )->node() );
348 Node_getBrush( node )->copy( *( *i ) );
350 Node_getTraversable( path.parent() )->insert( node );
351 //path.push( makeReference( node.get() ) );
352 //selectPath( path, true );
353 //Instance_getSelectable( *GlobalSceneGraph().find( path ) )->setSelected( true );
354 //Path_deleteTop( path );
363 typedef std::list<Brush*> brushlist_t;
365 class BrushGatherSelected : public scene::Graph::Walker
367 brush_vector_t& m_brushlist;
369 BrushGatherSelected( brush_vector_t& brushlist )
370 : m_brushlist( brushlist ){
373 bool pre( const scene::Path& path, scene::Instance& instance ) const {
374 if ( path.top().get().visible() ) {
375 Brush* brush = Node_getBrush( path.top() );
377 && Instance_getSelectable( instance )->isSelected() ) {
378 m_brushlist.push_back( brush );
385 class BrushDeleteSelected : public scene::Graph::Walker
388 bool pre( const scene::Path& path, scene::Instance& instance ) const {
391 void post( const scene::Path& path, scene::Instance& instance ) const {
392 if ( path.top().get().visible() ) {
393 Brush* brush = Node_getBrush( path.top() );
395 && Instance_getSelectable( instance )->isSelected()
396 && path.size() > 1 ) {
397 Path_deleteTop( path );
409 void CSG_MakeRoom( void ){
410 UndoableCommand undo( "makeRoom" );
411 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( GetGridSize(), room ) );
412 GlobalSceneGraph().traverse( BrushDeleteSelected() );
416 template<typename Type>
417 class RemoveReference
423 template<typename Type>
424 class RemoveReference<Type&>
430 template<typename Functor>
433 const Functor& functor;
435 Dereference( const Functor& functor ) : functor( functor ){
437 get_result_type<Functor> operator()( typename RemoveReference<get_argument<Functor, 0>>::type *firstArgument ) const {
438 return functor( *firstArgument );
442 template<typename Functor>
443 inline Dereference<Functor> makeDereference( const Functor& functor ){
444 return Dereference<Functor>( functor );
447 template<typename Caller>
450 typedef get_argument<Caller, 1> FirstBound;
451 FirstBound firstBound;
453 BindArguments1( FirstBound firstBound )
454 : firstBound( firstBound ){
457 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
458 return Caller::call( firstArgument, firstBound );
462 template<typename Caller>
465 typedef get_argument<Caller, 1> FirstBound;
466 typedef get_argument<Caller, 2> SecondBound;
467 FirstBound firstBound;
468 SecondBound secondBound;
470 BindArguments2( FirstBound firstBound, SecondBound secondBound )
471 : firstBound( firstBound ), secondBound( secondBound ){
474 get_result_type<Caller> operator()( get_argument<Caller, 0> firstArgument ) const {
475 return Caller::call( firstArgument, firstBound, secondBound );
479 template<typename Caller, typename FirstBound, typename SecondBound>
480 BindArguments2<Caller> bindArguments( const Caller& caller, FirstBound firstBound, SecondBound secondBound ){
481 return BindArguments2<Caller>( firstBound, secondBound );
484 inline bool Face_testPlane( const Face& face, const Plane3& plane, bool flipped ){
485 return face.contributes() && !Winding_TestPlane( face.getWinding(), plane, flipped );
488 typedef Function<bool ( const Face &, const Plane3 &, bool ), Face_testPlane> FaceTestPlane;
491 /// \brief Returns true if
492 /// \li !flipped && brush is BACK or ON
493 /// \li flipped && brush is FRONT or ON
494 bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
495 brush.evaluateBRep();
496 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
498 if ( Face_testPlane( *( *i ), plane, flipped ) ) {
505 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
506 brush.evaluateBRep();
508 for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
510 if ( ( *i )->contributes() ) {
511 split += Winding_ClassifyPlane( ( *i )->getWinding(), plane );
517 bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret_fragments ){
518 if ( aabb_intersects_aabb( brush.localAABB(), other.localAABB() ) ) {
519 brush_vector_t fragments;
520 fragments.reserve( other.size() );
523 for ( const std::shared_ptr<Face>& b : other )
525 if ( b->contributes() ) {
526 brushsplit_t split = Brush_classifyPlane( back, b->plane3() );
527 if ( split.counts[ePlaneFront] != 0
528 && split.counts[ePlaneBack] != 0 ) {
529 fragments.push_back( new Brush( back ) );
530 std::shared_ptr<Face> newFace = fragments.back()->addFace( *b );
531 if ( newFace != nullptr ) {
532 newFace->flipWinding();
536 else if ( split.counts[ePlaneBack] == 0 ) {
537 for ( Brush *i : fragments ) {
545 ret_fragments.insert( ret_fragments.end(), fragments.begin(), fragments.end() );
551 class SubtractBrushesFromUnselected : public scene::Graph::Walker
553 const brush_vector_t& m_brushlist;
554 std::size_t& m_before;
555 std::size_t& m_after;
557 SubtractBrushesFromUnselected( const brush_vector_t& brushlist, std::size_t& before, std::size_t& after )
558 : m_brushlist( brushlist ), m_before( before ), m_after( after ){
561 bool pre( const scene::Path& path, scene::Instance& instance ) const {
565 void post( const scene::Path& path, scene::Instance& instance ) const {
566 if ( path.top().get().visible() ) {
567 Brush* brush = Node_getBrush( path.top() );
569 && !Instance_getSelectable( instance )->isSelected() ) {
570 brush_vector_t buffer[2];
572 Brush* original = new Brush( *brush );
573 buffer[static_cast<std::size_t>( swap )].push_back( original );
576 for ( brush_vector_t::const_iterator i( m_brushlist.begin() ); i != m_brushlist.end(); ++i )
578 for ( brush_vector_t::iterator j( buffer[static_cast<std::size_t>( swap )].begin() ); j != buffer[static_cast<std::size_t>( swap )].end(); ++j )
580 if ( Brush_subtract( *( *j ), *( *i ), buffer[static_cast<std::size_t>( !swap )] ) ) {
585 buffer[static_cast<std::size_t>( !swap )].push_back( ( *j ) );
588 buffer[static_cast<std::size_t>( swap )].clear();
593 brush_vector_t& out = buffer[static_cast<std::size_t>( swap )];
595 if ( out.size() == 1 && out.back() == original ) {
601 for ( Brush *b : out ) {
603 b->removeEmptyFaces();
605 NodeSmartReference node( ( new BrushNode() )->node() );
606 Node_getBrush( node )->copy( *b );
607 Node_getTraversable( path.parent() )->insert( node );
611 Path_deleteTop( path );
619 brush_vector_t selected_brushes;
620 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
622 if ( selected_brushes.empty() ) {
623 globalOutputStream() << "CSG Subtract: No brushes selected.\n";
625 globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned( selected_brushes.size() ) << " brushes.\n";
627 UndoableCommand undo( "brushSubtract" );
629 // subtract selected from unselected
630 std::size_t before = 0;
631 std::size_t after = 0;
632 GlobalSceneGraph().traverse( SubtractBrushesFromUnselected( selected_brushes, before, after ) );
633 globalOutputStream() << "CSG Subtract: Result: "
634 << Unsigned( after ) << " fragment" << ( after == 1 ? "" : "s" )
635 << " from " << Unsigned( before ) << " brush" << ( before == 1 ? "" : "es" ) << ".\n";
641 class BrushSplitByPlaneSelected : public scene::Graph::Walker
646 const char* m_shader;
647 const TextureProjection& m_projection;
650 BrushSplitByPlaneSelected( const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, const TextureProjection& projection, EBrushSplit split )
651 : m_p0( p0 ), m_p1( p1 ), m_p2( p2 ), m_shader( shader ), m_projection( projection ), m_split( split ){
654 bool pre( const scene::Path& path, scene::Instance& instance ) const {
658 void post( const scene::Path& path, scene::Instance& instance ) const {
659 if ( !path.top().get().visible() ) {
663 Brush* brush = Node_getBrush( path.top() );
664 if ( brush == nullptr || !Instance_getSelectable( instance )->isSelected() ) {
668 Plane3 plane( plane3_for_points( m_p0, m_p1, m_p2 ) );
669 if ( !plane3_valid( plane ) ) {
673 brushsplit_t split = Brush_classifyPlane( *brush, m_split == eFront ? plane3_flipped( plane ) : plane );
674 if ( split.counts[ePlaneBack] && split.counts[ePlaneFront] ) {
675 // the plane intersects this brush
676 if ( m_split == eFrontAndBack ) {
677 NodeSmartReference node( ( new BrushNode() )->node() );
678 Brush* fragment = Node_getBrush( node );
679 fragment->copy( *brush );
680 std::shared_ptr<Face> newFace =
681 fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
682 if ( newFace != 0 && m_split != eFront ) {
683 newFace->flipWinding();
685 fragment->removeEmptyFaces();
686 ASSERT_MESSAGE( !fragment->empty(), "brush left with no faces after split" );
688 Node_getTraversable( path.parent() )->insert( node );
690 scene::Path fragmentPath = path;
691 fragmentPath.top() = makeReference( node.get() );
692 selectPath( fragmentPath, true );
696 std::shared_ptr<Face> newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
697 if ( newFace != 0 && m_split == eFront ) {
698 newFace->flipWinding();
700 brush->removeEmptyFaces();
701 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after split" );
704 // the plane does not intersect this brush
705 if ( m_split != eFrontAndBack && split.counts[ePlaneBack] != 0 ) {
706 // the brush is "behind" the plane
707 Path_deleteTop( path );
712 void Scene_BrushSplitByPlane( scene::Graph& graph, const Vector3& p0, const Vector3& p1, const Vector3& p2, const char* shader, EBrushSplit split ){
713 TextureProjection projection;
714 TexDef_Construct_Default( projection );
715 graph.traverse( BrushSplitByPlaneSelected( p0, p1, p2, shader, projection, split ) );
720 class BrushInstanceSetClipPlane : public scene::Graph::Walker
724 BrushInstanceSetClipPlane( const Plane3& plane )
728 bool pre( const scene::Path& path, scene::Instance& instance ) const {
729 BrushInstance* brush = Instance_getBrush( instance );
731 && path.top().get().visible()
732 && brush->isSelected() ) {
733 BrushInstance& brushInstance = *brush;
734 brushInstance.setClipPlane( m_plane );
740 void Scene_BrushSetClipPlane( scene::Graph& graph, const Plane3& plane ){
741 graph.traverse( BrushInstanceSetClipPlane( plane ) );
749 bool Brush_merge( Brush& brush, const brush_vector_t& in, bool onlyshape ){
750 // gather potential outer faces
753 typedef std::vector<const Face*> Faces;
755 for ( brush_vector_t::const_iterator i( in.begin() ); i != in.end(); ++i )
757 ( *i )->evaluateBRep();
758 for ( Brush::const_iterator j( ( *i )->begin() ); j != ( *i )->end(); ++j )
760 if ( !( *j )->contributes() ) {
764 const Face& face1 = *( *j );
767 // test faces of all input brushes
768 //!\todo SPEEDUP: Flag already-skip faces and only test brushes from i+1 upwards.
769 for ( brush_vector_t::const_iterator k( in.begin() ); !skip && k != in.end(); ++k )
771 if ( k != i ) { // don't test a brush against itself
772 for ( Brush::const_iterator l( ( *k )->begin() ); !skip && l != ( *k )->end(); ++l )
774 const Face& face2 = *( *l );
776 // face opposes another face
777 if ( plane3_opposing( face1.plane3(), face2.plane3() ) ) {
778 // skip opposing planes
786 // check faces already stored
787 for ( Faces::const_iterator m = faces.begin(); !skip && m != faces.end(); ++m )
789 const Face& face2 = *( *m );
791 // face equals another face
792 if ( plane3_equal( face1.plane3(), face2.plane3() ) ) {
793 //if the texture/shader references should be the same but are not
794 if ( !onlyshape && !shader_equal( face1.getShader().getShader(), face2.getShader().getShader() ) ) {
797 // skip duplicate planes
802 // face1 plane intersects face2 winding or vice versa
803 if ( Winding_PlanesConcave( face1.getWinding(), face2.getWinding(), face1.plane3(), face2.plane3() ) ) {
804 // result would not be convex
810 faces.push_back( &face1 );
814 for ( Faces::const_iterator i = faces.begin(); i != faces.end(); ++i )
816 if ( !brush.addFace( *( *i ) ) ) {
817 // result would have too many sides
823 brush.removeEmptyFaces();
828 void CSG_Merge( void ){
829 brush_vector_t selected_brushes;
832 GlobalSceneGraph().traverse( BrushGatherSelected( selected_brushes ) );
834 if ( selected_brushes.empty() ) {
835 globalOutputStream() << "CSG Merge: No brushes selected.\n";
839 if ( selected_brushes.size() < 2 ) {
840 globalOutputStream() << "CSG Merge: At least two brushes have to be selected.\n";
844 globalOutputStream() << "CSG Merge: Merging " << Unsigned( selected_brushes.size() ) << " brushes.\n";
846 UndoableCommand undo( "brushMerge" );
848 scene::Path merged_path = GlobalSelectionSystem().ultimateSelected().path();
850 NodeSmartReference node( ( new BrushNode() )->node() );
851 Brush* brush = Node_getBrush( node );
852 // if the new brush would not be convex
853 if ( !Brush_merge( *brush, selected_brushes, true ) ) {
854 globalOutputStream() << "CSG Merge: Failed - result would not be convex.\n";
858 ASSERT_MESSAGE( !brush->empty(), "brush left with no faces after merge" );
860 // free the original brushes
861 GlobalSceneGraph().traverse( BrushDeleteSelected() );
864 Node_getTraversable( merged_path.top() )->insert( node );
865 merged_path.push( makeReference( node.get() ) );
867 selectPath( merged_path, true );
869 globalOutputStream() << "CSG Merge: Succeeded.\n";
884 #include "mainframe.h"
886 #include "gtkutil/dialog.h"
887 #include "gtkutil/button.h"
888 #include "gtkutil/accelerator.h"
893 bool allocated{false};
894 ui::Window window{ui::null};
895 GtkToggleButton *radXYZ, *radX, *radY, *radZ, *caulk, *removeInner;
898 CSGToolDialog g_csgtool_dialog;
900 DoubleVector3 getExclusion(){
901 if( gtk_toggle_button_get_active( g_csgtool_dialog.radX ) ){
902 return DoubleVector3( 1, 0, 0 );
904 else if( gtk_toggle_button_get_active( g_csgtool_dialog.radY ) ){
905 return DoubleVector3( 0, 1, 0 );
907 else if( gtk_toggle_button_get_active( g_csgtool_dialog.radZ ) ){
908 return DoubleVector3( 0, 0, 1 );
910 return DoubleVector3( 0, 0, 0 );
914 if( gtk_toggle_button_get_active( g_csgtool_dialog.caulk ) ){
920 bool getRemoveInner(){
921 if( gtk_toggle_button_get_active( g_csgtool_dialog.removeInner ) ){
927 class BrushFaceOffset
931 BrushFaceOffset( float offset )
934 void operator()( BrushInstance& brush ) const {
937 Brush_forEachFace( brush, FaceExclude( getExclusion(), mindot, maxdot ) );
938 Brush_forEachFace( brush, FaceOffset( offset, getExclusion(), mindot, maxdot ) );
942 //=================DLG
944 static gboolean CSGdlg_HollowDiag( GtkWidget *widget, CSGToolDialog* dialog ){
945 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
946 UndoableCommand undo( "brushHollow::Diag" );
947 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( offset, diag ) );
948 if( getRemoveInner() )
949 GlobalSceneGraph().traverse( BrushDeleteSelected() );
954 static gboolean CSGdlg_HollowWrap( GtkWidget *widget, CSGToolDialog* dialog ){
955 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
956 UndoableCommand undo( "brushHollow::Wrap" );
957 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( offset, wrap ) );
958 if( getRemoveInner() )
959 GlobalSceneGraph().traverse( BrushDeleteSelected() );
964 static gboolean CSGdlg_HollowExtrude( GtkWidget *widget, CSGToolDialog* dialog ){
965 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
966 UndoableCommand undo( "brushHollow::Extrude" );
967 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( offset, extrude ) );
968 if( getRemoveInner() )
969 GlobalSceneGraph().traverse( BrushDeleteSelected() );
974 static gboolean CSGdlg_HollowPull( GtkWidget *widget, CSGToolDialog* dialog ){
975 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
976 UndoableCommand undo( "brushHollow::Pull" );
977 GlobalSceneGraph().traverse( BrushHollowSelectedWalker( offset, pull ) );
978 if( getRemoveInner() )
979 GlobalSceneGraph().traverse( BrushDeleteSelected() );
984 static gboolean CSGdlg_BrushShrink( GtkWidget *widget, CSGToolDialog* dialog ){
985 gtk_spin_button_update ( dialog->spin );
986 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
988 UndoableCommand undo( "Shrink brush" );
989 // GlobalSceneGraph().traverse( OffsetBrushFacesSelectedWalker( offset ) );
990 //Scene_ForEachSelectedBrush_ForEachFace( GlobalSceneGraph(), BrushFaceOffset( offset ) );
991 Scene_forEachSelectedBrush( BrushFaceOffset( offset ) );
996 static gboolean CSGdlg_BrushExpand( GtkWidget *widget, CSGToolDialog* dialog ){
997 gtk_spin_button_update ( dialog->spin );
998 float offset = static_cast<float>( gtk_spin_button_get_value( dialog->spin ) );
999 UndoableCommand undo( "Expand brush" );
1000 // GlobalSceneGraph().traverse( OffsetBrushFacesSelectedWalker( offset ) );
1001 //Scene_ForEachSelectedBrush_ForEachFace( GlobalSceneGraph(), BrushFaceOffset( offset ) );
1002 Scene_forEachSelectedBrush( BrushFaceOffset( offset ) );
1003 SceneChangeNotify();
1007 static gboolean CSGdlg_grid2spin( GtkWidget *widget, CSGToolDialog* dialog ){
1008 gtk_spin_button_set_value( dialog->spin, GetGridSize() );
1012 static gboolean CSGdlg_delete( GtkWidget *widget, GdkEventAny *event, CSGToolDialog* dialog ){
1013 gtk_widget_hide( GTK_WIDGET( dialog->window ) );
1018 // FIXME: there is probably improvements to do less raw GTK stuff, more GTK wrapper
1019 if ( !g_csgtool_dialog.allocated ) {
1020 g_csgtool_dialog.allocated = true;
1021 g_csgtool_dialog.window = MainFrame_getWindow().create_dialog_window( "CSG Tool", G_CALLBACK( CSGdlg_delete ), &g_csgtool_dialog );
1022 gtk_window_set_type_hint( g_csgtool_dialog.window, GDK_WINDOW_TYPE_HINT_UTILITY );
1024 //GtkAccelGroup* accel = gtk_accel_group_new();
1025 //gtk_window_add_accel_group( g_csgtool_dialog.window, accel );
1026 global_accel_connect_window( g_csgtool_dialog.window );
1029 auto hbox = create_dialog_hbox( 4, 4 );
1030 gtk_container_add( GTK_CONTAINER( g_csgtool_dialog.window ), GTK_WIDGET( hbox ) );
1032 auto table = create_dialog_table( 3, 8, 4, 4 );
1033 gtk_box_pack_start( GTK_BOX( hbox ), GTK_WIDGET( table ), TRUE, TRUE, 0 );
1035 //GtkWidget* label = gtk_label_new( "<->" );
1036 //gtk_widget_show( label );
1037 auto button = ui::Button( "Grid->" );
1038 table.attach( button, {0, 1, 0, 1}, {0, 0} );
1040 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_grid2spin ), &g_csgtool_dialog );
1043 GtkAdjustment* adj = GTK_ADJUSTMENT( gtk_adjustment_new( 16, 0, 9999, 1, 10, 0 ) );
1044 GtkSpinButton* spin = GTK_SPIN_BUTTON( gtk_spin_button_new( adj, 1, 3 ) );
1045 gtk_widget_show( GTK_WIDGET( spin ) );
1046 gtk_widget_set_tooltip_text( GTK_WIDGET( spin ), "Thickness" );
1047 gtk_table_attach( table, GTK_WIDGET( spin ), 1, 2, 0, 1,
1048 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1049 (GtkAttachOptions) ( 0 ), 0, 0 );
1050 gtk_widget_set_size_request( GTK_WIDGET( spin ), 64, -1 );
1051 gtk_spin_button_set_numeric( spin, TRUE );
1053 g_csgtool_dialog.spin = spin;
1056 //radio button group for choosing the exclude axis
1057 GtkWidget* radXYZ = gtk_radio_button_new_with_label( NULL, "XYZ" );
1058 GtkWidget* radX = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(radXYZ), "-X" );
1059 GtkWidget* radY = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(radXYZ), "-Y" );
1060 GtkWidget* radZ = gtk_radio_button_new_with_label_from_widget( GTK_RADIO_BUTTON(radXYZ), "-Z" );
1061 gtk_widget_show( radXYZ );
1062 gtk_widget_show( radX );
1063 gtk_widget_show( radY );
1064 gtk_widget_show( radZ );
1066 gtk_table_attach( table, radXYZ, 2, 3, 0, 1,
1067 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1068 (GtkAttachOptions) ( 0 ), 0, 0 );
1069 gtk_table_attach( table, radX, 3, 4, 0, 1,
1070 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1071 (GtkAttachOptions) ( 0 ), 0, 0 );
1072 gtk_table_attach( table, radY, 4, 5, 0, 1,
1073 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1074 (GtkAttachOptions) ( 0 ), 0, 0 );
1075 gtk_table_attach( table, radZ, 5, 6, 0, 1,
1076 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1077 (GtkAttachOptions) ( 0 ), 0, 0 );
1079 g_csgtool_dialog.radXYZ = GTK_TOGGLE_BUTTON( radXYZ );
1080 g_csgtool_dialog.radX = GTK_TOGGLE_BUTTON( radX );
1081 g_csgtool_dialog.radY = GTK_TOGGLE_BUTTON( radY );
1082 g_csgtool_dialog.radZ = GTK_TOGGLE_BUTTON( radZ );
1085 GtkWidget* button = gtk_toggle_button_new();
1086 auto ubutton = ui::Button::from( button );
1087 button_set_icon( ubutton, "f-caulk.png" );
1088 gtk_button_set_relief( GTK_BUTTON( button ), GTK_RELIEF_NONE );
1089 table.attach( ubutton, { 6, 7, 0, 1 }, { GTK_EXPAND, 0 } );
1090 gtk_widget_set_tooltip_text( button, "Caulk some faces" );
1091 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), TRUE );
1093 g_csgtool_dialog.caulk = GTK_TOGGLE_BUTTON( button );
1096 GtkWidget* button = gtk_toggle_button_new();
1097 auto ubutton = ui::Button::from( button );
1098 button_set_icon( ubutton, "csgtool_removeinner.png" );
1099 gtk_button_set_relief( GTK_BUTTON( button ), GTK_RELIEF_NONE );
1100 table.attach( ubutton, { 7, 8, 0, 1 }, { GTK_EXPAND, 0 } );
1101 gtk_widget_set_tooltip_text( button, "Remove inner brush" );
1102 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( button ), TRUE );
1104 g_csgtool_dialog.removeInner = GTK_TOGGLE_BUTTON( button );
1107 GtkWidget* sep = gtk_hseparator_new();
1108 gtk_widget_show( sep );
1109 gtk_table_attach( table, sep, 0, 8, 1, 2,
1110 (GtkAttachOptions) ( GTK_EXPAND | GTK_FILL ),
1111 (GtkAttachOptions) ( 0 ), 0, 0 );
1114 GtkWidget* button = gtk_button_new();
1115 auto ubutton = ui::Button::from( button );
1116 button_set_icon( ubutton, "csgtool_shrink.png" );
1117 table.attach( ubutton, { 0, 1, 2, 3 }, { GTK_EXPAND, 0 } );
1118 gtk_widget_set_tooltip_text( button, "Shrink brush" );
1120 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_BrushShrink ), &g_csgtool_dialog );
1123 GtkWidget* button = gtk_button_new();
1124 auto ubutton = ui::Button::from( button );
1125 button_set_icon( ubutton, "csgtool_expand.png" );
1126 table.attach( ubutton, { 1, 2, 2, 3 }, { GTK_EXPAND, 0 } );
1127 gtk_widget_set_tooltip_text( button, "Expand brush" );
1129 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_BrushExpand ), &g_csgtool_dialog );
1132 GtkWidget* button = gtk_button_new();
1133 auto ubutton = ui::Button::from( button );
1134 button_set_icon( ubutton, "csgtool_diagonal.png" );
1135 table.attach( ubutton, { 3, 4, 2, 3 }, { GTK_EXPAND, 0 } );
1136 gtk_widget_set_tooltip_text( button, "Hollow::diagonal joints" );
1138 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_HollowDiag ), &g_csgtool_dialog );
1141 GtkWidget* button = gtk_button_new();
1142 auto ubutton = ui::Button::from( button );
1143 button_set_icon( ubutton, "csgtool_wrap.png" );
1144 table.attach( ubutton, { 4, 5, 2, 3 }, { GTK_EXPAND, 0 } );
1145 gtk_widget_set_tooltip_text( button, "Hollow::warp" );
1147 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_HollowWrap ), &g_csgtool_dialog );
1150 GtkWidget* button = gtk_button_new();
1151 auto ubutton = ui::Button::from( button );
1152 button_set_icon( ubutton, "csgtool_extrude.png" );
1153 table.attach( ubutton, { 5, 6, 2, 3 }, { GTK_EXPAND, 0 } );
1154 gtk_widget_set_tooltip_text( button, "Hollow::extrude faces" );
1156 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_HollowExtrude ), &g_csgtool_dialog );
1159 GtkWidget* button = gtk_button_new();
1160 auto ubutton = ui::Button::from( button );
1161 button_set_icon( ubutton, "csgtool_pull.png" );
1162 table.attach( ubutton, { 6, 7, 2, 3 }, { GTK_EXPAND, 0 } );
1163 gtk_widget_set_tooltip_text( button, "Hollow::pull faces" );
1165 g_signal_connect( G_OBJECT( button ), "clicked", G_CALLBACK( CSGdlg_HollowPull ), &g_csgtool_dialog );
1172 gtk_widget_show( GTK_WIDGET( g_csgtool_dialog.window ) );
1173 gtk_window_present( g_csgtool_dialog.window );