Skip to content

Commit

Permalink
- Add intersection and exclusive functions on selection
Browse files Browse the repository at this point in the history
- Move complex functions on ranges in algorithm/range (merge, susbtract, intersect and exclusive)
  • Loading branch information
Nicolas Dacquay committed Nov 9, 2023
1 parent ac8284e commit 6243aea
Show file tree
Hide file tree
Showing 13 changed files with 23,104 additions and 136 deletions.
57 changes: 48 additions & 9 deletions lib/app/src/app/application/selection/molecule_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "app/core/ecs/registry.hpp"
#include "app/vtx_app.hpp"
#include <sstream>
#include <util/algorithm/range.hpp>

namespace VTX::App::Application::Selection
{
Expand Down Expand Up @@ -39,26 +40,64 @@ namespace VTX::App::Application::Selection
{
const MoleculeData & castedOther = dynamic_cast<const MoleculeData &>( p_other );

_chainIds.merge( castedOther._chainIds );
_residueIds.merge( castedOther._residueIds );
_atomIds.merge( castedOther._atomIds );
if ( _molecule == castedOther._molecule )
{
Util::Algorithm::Range::mergeInSitu( _chainIds, castedOther._chainIds );
Util::Algorithm::Range::mergeInSitu( _residueIds, castedOther._residueIds );
Util::Algorithm::Range::mergeInSitu( _atomIds, castedOther._atomIds );
}

return *this;
}
SelectionData & MoleculeData::remove( const SelectionData & p_other )
{
const MoleculeData & castedOther = dynamic_cast<const MoleculeData &>( p_other );

_chainIds.substract( castedOther._chainIds );
_residueIds.substract( castedOther._residueIds );
_atomIds.substract( castedOther._atomIds );
if ( _molecule == castedOther._molecule )
{
Util::Algorithm::Range::substractInSitu( _chainIds, castedOther._chainIds );
Util::Algorithm::Range::substractInSitu( _residueIds, castedOther._residueIds );
Util::Algorithm::Range::substractInSitu( _atomIds, castedOther._atomIds );

_valid = _chainIds.size() > 0 || _residueIds.size() > 0 || _atomIds.size() > 0;
}

return *this;
}
SelectionData & MoleculeData::intersect( const SelectionData & p_other )
{
const MoleculeData & castedOther = dynamic_cast<const MoleculeData &>( p_other );

if ( _molecule == castedOther._molecule )
{
Util::Algorithm::Range::intersectInSitu( _chainIds, castedOther._chainIds );
Util::Algorithm::Range::intersectInSitu( _residueIds, castedOther._residueIds );
Util::Algorithm::Range::intersectInSitu( _atomIds, castedOther._atomIds );

_valid = _chainIds.size() > 0 || _residueIds.size() > 0 || _atomIds.size() > 0;
}
else
{
_valid = false;
}

return *this;
}
SelectionData & MoleculeData::exclude( const SelectionData & p_other )
{
const MoleculeData & castedOther = dynamic_cast<const MoleculeData &>( p_other );

if ( _molecule == castedOther._molecule )
{
_chainIds = Util::Algorithm::Range::exclusive( _chainIds, castedOther._chainIds );
_residueIds = Util::Algorithm::Range::exclusive( _residueIds, castedOther._residueIds );
_atomIds = Util::Algorithm::Range::exclusive( _atomIds, castedOther._atomIds );

_valid = _chainIds.size() > 0 || _residueIds.size() > 0 || _atomIds.size() > 0;
_valid = _chainIds.size() > 0 || _residueIds.size() > 0 || _atomIds.size() > 0;
}

return *this;
}
SelectionData & MoleculeData::intersect( const SelectionData & p_other ) { return *this; }
SelectionData & MoleculeData::exclude( const SelectionData & p_other ) { return *this; }

void MoleculeData::selectAll()
{
Expand Down
47 changes: 45 additions & 2 deletions lib/app/src/app/application/selection/selection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,52 @@ namespace VTX::App::Application::Selection
return res;
}

Selection Selection::intersection( const Selection & p_lhs, const Selection & p_rhs )
{
Selection res = Selection( p_lhs );

for ( const std::unique_ptr<SelectionData> & item : p_rhs._items )
{
const Component::Scene::Selectable & rhsSelectableComponent = item->getSelectionComponent();

if ( res.isSelected( rhsSelectableComponent ) )
{
const SelectionData & rhsSelectionData
= res.getSelectionData( rhsSelectableComponent ).intersect( *item );

if ( !rhsSelectionData.isValid() )
res.unselect( rhsSelectableComponent );
}
}

return res;
}
Selection Selection::exclusive( const Selection & p_lhs, const Selection & p_rhs )
{
Selection res = Selection( p_lhs );

for ( const std::unique_ptr<SelectionData> & item : p_rhs._items )
{
const Component::Scene::Selectable & rhsSelectableComponent = item->getSelectionComponent();

if ( res.isSelected( rhsSelectableComponent ) )
{
const SelectionData & rhsSelectionData
= res.getSelectionData( rhsSelectableComponent ).exclude( *item );

if ( !rhsSelectionData.isValid() )
res.unselect( rhsSelectableComponent );
}
else
{
res.getSelectionData( rhsSelectableComponent ).add( *item );
}
}

return res;
}

// TODO
Selection Selection::intersection( const Selection & p_lhs, const Selection & p_rhs ) { return Selection(); }
Selection Selection::exclusive( const Selection & p_lhs, const Selection & p_rhs ) { return Selection(); }
Selection Selection::inverse( const Selection & p_selection ) { return Selection(); }

SelectionData & Selection::select( const Component::Scene::Selectable & p_selectableComponent )
Expand Down
12 changes: 10 additions & 2 deletions lib/app/src/app/application/selection/selection_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ namespace VTX::App::Application::Selection
_valid = _selectionComponent != p_other._selectionComponent;
return *this;
}
SelectionData & SelectionData::intersect( const SelectionData & p_other ) { return *this; }
SelectionData & SelectionData::exclude( const SelectionData & p_other ) { return *this; }
SelectionData & SelectionData::intersect( const SelectionData & p_other )
{
_valid = _selectionComponent == p_other._selectionComponent;
return *this;
}
SelectionData & SelectionData::exclude( const SelectionData & p_other )
{
_valid = _selectionComponent != p_other._selectionComponent;
return *this;
}

std::string SelectionData::toString() const
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ namespace VTX::PythonBinding::API::Selection
SelectionWrapper();
SelectionWrapper( const Selection & p_selection );

static SelectionWrapper intersect( const SelectionWrapper & p_lhs, const SelectionWrapper & p_rhs );
static SelectionWrapper exclusive( const SelectionWrapper & p_lhs, const SelectionWrapper & p_rhs );

SelectionWrapper & add( const SelectionWrapper & p_other );
SelectionWrapper & remove( const SelectionWrapper & p_other );
SelectionWrapper & intersect( const SelectionWrapper & p_other );
SelectionWrapper & exclusive( const SelectionWrapper & p_other );
// SelectionWrapper & intersect( const SelectionWrapper & p_other );
// SelectionWrapper & exclusive( const SelectionWrapper & p_other );
SelectionWrapper & inverse();

SelectionWrapper & save( const std::string & p_name );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,25 @@ namespace VTX::PythonBinding::API::Selection
{
}

SelectionWrapper & SelectionWrapper::add( const SelectionWrapper & p_other )
SelectionWrapper SelectionWrapper::intersect( const SelectionWrapper & p_lhs, const SelectionWrapper & p_rhs )
{
Selection res = Selection::add( *_selection, *( p_other._selection ) );
_selection = std::make_unique<Selection>( res );

return *this;
return SelectionWrapper( Selection::intersection( *( p_lhs._selection ), *( p_rhs._selection ) ) );
}
SelectionWrapper & SelectionWrapper::remove( const SelectionWrapper & p_other )
SelectionWrapper SelectionWrapper::exclusive( const SelectionWrapper & p_lhs, const SelectionWrapper & p_rhs )
{
Selection res = Selection::remove( *_selection, *( p_other._selection ) );
_selection = std::make_unique<Selection>( res );

return *this;
return SelectionWrapper( Selection::exclusive( *( p_lhs._selection ), *( p_rhs._selection ) ) );
}
SelectionWrapper & SelectionWrapper::intersect( const SelectionWrapper & p_other )

SelectionWrapper & SelectionWrapper::add( const SelectionWrapper & p_other )
{
Selection res = Selection::intersection( *_selection, *( p_other._selection ) );
Selection res = Selection::add( *_selection, *( p_other._selection ) );
_selection = std::make_unique<Selection>( res );

return *this;
}
SelectionWrapper & SelectionWrapper::exclusive( const SelectionWrapper & p_other )
SelectionWrapper & SelectionWrapper::remove( const SelectionWrapper & p_other )
{
Selection res = Selection::exclusive( *_selection, *( p_other._selection ) );
Selection res = Selection::remove( *_selection, *( p_other._selection ) );
_selection = std::make_unique<Selection>( res );

return *this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ namespace VTX::PythonBinding::Binding::Binders
"select", &API::Selection::SelectionInterpretor::select, pybind11::return_value_policy::reference
);

p_apiModule.def( "intersect", &API::Selection::SelectionWrapper::intersect );
p_apiModule.def( "exclusive", &API::Selection::SelectionWrapper::exclusive );

pybind11::class_<App::Application::Selection::Selection>(
p_apiModule, "_VTXSelection", pybind11::module_local()
);
Expand Down
2 changes: 2 additions & 0 deletions lib/python_binding/src/python_binding/interpretor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ namespace VTX::PythonBinding
{
pybind11::exec( "from PyTX.Command import *" );
pybind11::exec( "from PyTX.API import select" );
pybind11::exec( "from PyTX.API import intersect" );
pybind11::exec( "from PyTX.API import exclusive" );
}

private:
Expand Down
Loading

0 comments on commit 6243aea

Please sign in to comment.