]> git.xonotic.org Git - xonotic/netradiant.git/blobdiff - radiant/csg.cpp
Merge commit '93a3d833ff57ef8304cb547dd1e3664b6b276ec4' into master-merge
[xonotic/netradiant.git] / radiant / csg.cpp
index 6ca051973c49ebdd731afefcdeb07842428cd5f3..5d20bf1c742b9753953e804201eee50a27109bc6 100644 (file)
@@ -34,7 +34,7 @@
 void Face_makeBrush( Face& face, const Brush& brush, brush_vector_t& out, float offset ){
        if ( face.contributes() ) {
                out.push_back( new Brush( brush ) );
-               Face* newFace = out.back()->addFace( face );
+               std::shared_ptr<Face> newFace = out.back()->addFace( face );
                face.getPlane().offset( -offset );
                face.planeChanged();
                if ( newFace != 0 ) {
@@ -50,7 +50,7 @@ void Face_extrude( Face& face, const Brush& brush, brush_vector_t& out, float of
                face.getPlane().offset( offset );
                out.push_back( new Brush( brush ) );
                face.getPlane().offset( -offset );
-               Face* newFace = out.back()->addFace( face );
+               std::shared_ptr<Face> newFace = out.back()->addFace( face );
                if ( newFace != 0 ) {
                        newFace->flipWinding();
                        newFace->planeChanged();
@@ -166,7 +166,7 @@ void operator()( Face& face ) const {
                                if( caulk ){
                                        Brush_forEachFace( *out.back(), CaulkFace( ExclusionAxis, mindot, maxdot, exclude_vec ) );
                                }
-                               Face* newFace = out.back()->addFace( face );
+                               std::shared_ptr<Face> newFace = out.back()->addFace( face );
                                if ( newFace != 0 ) {
                                        newFace->flipWinding();
                                }
@@ -179,7 +179,7 @@ void operator()( Face& face ) const {
                                out.push_back( new Brush( brush ) );
                                if( !RemoveInner && caulk )
                                        face.SetShader( getCaulkShader() );
-                               Face* newFace = out.back()->addFace( face );
+                               std::shared_ptr<Face> newFace = out.back()->addFace( face );
                                face.getPlane().offset( -offset );
                                face.planeChanged();
                                if( caulk )
@@ -197,7 +197,7 @@ void operator()( Face& face ) const {
                                out.push_back( new Brush( brush ) );
                                out.back()->clear();
 
-                               Face* newFace = out.back()->addFace( face );
+                               std::shared_ptr<Face> newFace = out.back()->addFace( face );
                                if ( newFace != 0 ) {
                                        newFace->getPlane().offset( offset );
                                        newFace->planeChanged();
@@ -225,7 +225,7 @@ void operator()( Face& face ) const {
                                out.push_back( new Brush( brush ) );
                                out.back()->clear();
 
-                               Face* newFace = out.back()->addFace( face );
+                               std::shared_ptr<Face> newFace = out.back()->addFace( face );
                                if ( newFace != 0 ) {
 
                                        newFace->planeChanged();
@@ -392,10 +392,10 @@ bool pre( const scene::Path& path, scene::Instance& instance ) const {
                        {
                                ( *i )->removeEmptyFaces();
                                if( ( *i )->hasContributingFaces() ){
-                                       NodeSmartReference node( ( new BrushNode() )->node() );
-                                       Node_getBrush( node )->copy( *( *i ) );
-                                       delete ( *i );
-                                       Node_getTraversable( path.parent() )->insert( node );
+                               NodeSmartReference node( ( new BrushNode() )->node() );
+                               Node_getBrush( node )->copy( *( *i ) );
+                               delete ( *i );
+                               Node_getTraversable( path.parent() )->insert( node );
                                        //path.push( makeReference( node.get() ) );
                                        //selectPath( path, true );
                                        //Instance_getSelectable( *GlobalSceneGraph().find( path ) )->setSelected( true );
@@ -533,15 +533,6 @@ inline Dereference<Functor> makeDereference( const Functor& functor ){
        return Dereference<Functor>( functor );
 }
 
-typedef Face* FacePointer;
-const FacePointer c_nullFacePointer = 0;
-
-template<typename Predicate>
-Face* Brush_findIf( const Brush& brush, const Predicate& predicate ){
-       Brush::const_iterator i = std::find_if( brush.begin(), brush.end(), makeDereference( predicate ) );
-       return i == brush.end() ? c_nullFacePointer : *i; // uses c_nullFacePointer instead of 0 because otherwise gcc 4.1 attempts conversion to int
-}
-
 template<typename Caller>
 class BindArguments1
 {
@@ -591,7 +582,6 @@ typedef Function<bool ( const Face &, const Plane3 &, bool ), Face_testPlane> Fa
 /// \li flipped && brush is FRONT or ON
 bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
        brush.evaluateBRep();
-#if 1
        for ( Brush::const_iterator i( brush.begin() ); i != brush.end(); ++i )
        {
                if ( Face_testPlane( *( *i ), plane, flipped ) ) {
@@ -599,9 +589,6 @@ bool Brush_testPlane( const Brush& brush, const Plane3& plane, bool flipped ){
                }
        }
        return true;
-#else
-       return Brush_findIf( brush, bindArguments( FaceTestPlane(), makeReference( plane ), flipped ) ) == 0;
-#endif
 }
 
 brushsplit_t Brush_classifyPlane( const Brush& brush, const Plane3& plane ){
@@ -622,24 +609,24 @@ bool Brush_subtract( const Brush& brush, const Brush& other, brush_vector_t& ret
                fragments.reserve( other.size() );
                Brush back( brush );
 
-               for ( Brush::const_iterator i( other.begin() ); i != other.end(); ++i )
+               for ( const std::shared_ptr<Face>& b : other )
                {
-                       if ( ( *i )->contributes() ) {
-                               brushsplit_t split = Brush_classifyPlane( back, ( *i )->plane3() );
+                       if ( b->contributes() ) {
+                               brushsplit_t split = Brush_classifyPlane( back, b->plane3() );
                                if ( split.counts[ePlaneFront] != 0
                                         && split.counts[ePlaneBack] != 0 ) {
                                        fragments.push_back( new Brush( back ) );
-                                       Face* newFace = fragments.back()->addFace( *( *i ) );
-                                       if ( newFace != 0 ) {
+                                       std::shared_ptr<Face> newFace = fragments.back()->addFace( *b );
+                                       if ( newFace != nullptr ) {
                                                newFace->flipWinding();
                                        }
-                                       back.addFace( *( *i ) );
+                                       back.addFace( *b );
                                }
                                else if ( split.counts[ePlaneBack] == 0 ) {
-                                       for ( brush_vector_t::iterator i = fragments.begin(); i != fragments.end(); ++i )
-                                       {
-                                               delete( *i );
+                                       for ( Brush *i : fragments ) {
+                                               delete( i );
                                        }
+                                       fragments.clear();
                                        return false;
                                }
                        }
@@ -704,19 +691,15 @@ void post( const scene::Path& path, scene::Instance& instance ) const {
                        else
                        {
                                ++m_before;
-                               for ( brush_vector_t::const_iterator i = out.begin(); i != out.end(); ++i )
-                               {
+                               for ( Brush *b : out ) {
                                        ++m_after;
-                                       ( *i )->removeEmptyFaces();
-                                       if ( !( *i )->empty() ) {
+                                       b->removeEmptyFaces();
+                                       if ( !b->empty() ) {
                                                NodeSmartReference node( ( new BrushNode() )->node() );
-                                               Node_getBrush( node )->copy( *( *i ) );
-                                               delete ( *i );
+                                               Node_getBrush( node )->copy( *b );
                                                Node_getTraversable( path.parent() )->insert( node );
                                        }
-                                       else{
-                                               delete ( *i );
-                                       }
+                                       delete b;
                                }
                                scene::Node& parent = path.parent();
                                Path_deleteTop( path );
@@ -743,9 +726,7 @@ void CSG_Subtract(){
 
        if ( selected_brushes.empty() ) {
                globalOutputStream() << "CSG Subtract: No brushes selected.\n";
-       }
-       else
-       {
+       } else {
                globalOutputStream() << "CSG Subtract: Subtracting " << Unsigned( selected_brushes.size() ) << " brushes.\n";
 
                UndoableCommand undo( "brushSubtract" );
@@ -780,12 +761,20 @@ bool pre( const scene::Path& path, scene::Instance& instance ) const {
 }
 
 void post( const scene::Path& path, scene::Instance& instance ) const {
-       if ( path.top().get().visible() ) {
+       if ( !path.top().get().visible() ) {
+               return;
+       }
+
                Brush* brush = Node_getBrush( path.top() );
-               if ( brush != 0
-                        && Instance_getSelectable( instance )->isSelected() ) {
+       if ( brush == nullptr || !Instance_getSelectable( instance )->isSelected() ) {
+               return;
+       }
+
                        Plane3 plane( plane3_for_points( m_p0, m_p1, m_p2 ) );
-                       if ( plane3_valid( plane ) ) {
+       if ( !plane3_valid( plane ) ) {
+               return;
+       }
+
                                brushsplit_t split = Brush_classifyPlane( *brush, m_split == eFront ? plane3_flipped( plane ) : plane );
                                if ( split.counts[ePlaneBack] && split.counts[ePlaneFront] ) {
                                        // the plane intersects this brush
@@ -793,7 +782,8 @@ void post( const scene::Path& path, scene::Instance& instance ) const {
                                                NodeSmartReference node( ( new BrushNode() )->node() );
                                                Brush* fragment = Node_getBrush( node );
                                                fragment->copy( *brush );
-                                               Face* newFace = fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
+                       std::shared_ptr<Face> newFace =
+                               fragment->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
                                                if ( newFace != 0 && m_split != eFront ) {
                                                        newFace->flipWinding();
                                                }
@@ -808,7 +798,7 @@ void post( const scene::Path& path, scene::Instance& instance ) const {
                                                }
                                        }
 
-                                       Face* newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
+               std::shared_ptr<Face> newFace = brush->addPlane( m_p0, m_p1, m_p2, m_shader, m_projection );
                                        if ( newFace != 0 && m_split == eFront ) {
                                                newFace->flipWinding();
                                        }
@@ -820,9 +810,6 @@ void post( const scene::Path& path, scene::Instance& instance ) const {
                                if ( m_split != eFrontAndBack && split.counts[ePlaneBack] != 0 ) {
                                        // the brush is "behind" the plane
                                        Path_deleteTop( path );
-                               }
-                       }
-               }
        }
 }
 };