Commit 954a267f authored by T.J. Corona's avatar T.J. Corona

Add selection observer for mesh selection

parent b44a9ac9
Pipeline #143290 failed with stage
......@@ -80,11 +80,14 @@ vtkSMTKWrapper::vtkSMTKWrapper()
this->ProjectManager =
smtk::project::Manager::create(this->ResourceManager, this->OperationManager);
smtk::extension::paraview::PluginManager::instance()->registerPluginsTo(this->ProjectManager);
// NB: this->Selection may never be overwritten with a different instance
// of smtk::view::Selection once a wrapper is created; consumers of the
// wrapper assume it is constant, will add observers, etc..
this->Selection = smtk::view::Selection::create();
smtk::extension::paraview::PluginManager::instance()->registerPluginsTo(this->Selection);
this->Selection->setDefaultAction(smtk::view::SelectionAction::FILTERED_REPLACE);
this->SelectedValue = this->Selection->findOrCreateLabeledValue("selected");
this->HoveredValue = this->Selection->findOrCreateLabeledValue("hovered");
......
......@@ -56,6 +56,7 @@ set(meshSrcs
operators/WriteResource.cxx
resource/Registrar.cxx
resource/SelectionObserver.cxx
utility/ApplyToMesh.cxx
utility/Create.cxx
......@@ -122,6 +123,7 @@ set(meshHeaders
resource/PropertyData.h
resource/Registrar.h
resource/SelectionObserver.h
utility/ApplyToMesh.h
utility/Create.h
......@@ -156,7 +158,7 @@ if (SMTK_ENABLE_PARAVIEW_SUPPORT)
ENABLED_BY_DEFAULT
REGISTRAR smtk::mesh::Registrar
REGISTRAR_HEADER smtk/mesh/resource/Registrar.h
MANAGERS smtk::operation::Manager smtk::resource::Manager
MANAGERS smtk::operation::Manager smtk::resource::Manager smtk::view::Selection
LIBRARIES smtkCore)
smtk_install_library(smtkMeshPlugin)
endif()
......
......@@ -103,6 +103,12 @@ Resource::~Resource()
}
}
bool Resource::erase(const smtk::mesh::Selection::Ptr& selection)
{
std::size_t nComponents = m_componentMap.size();
return m_componentMap.erase(selection->id()) != nComponents;
}
smtk::resource::ComponentPtr Resource::find(const smtk::common::UUID& compId) const
{
return std::static_pointer_cast<smtk::resource::Component>(
......
......@@ -27,6 +27,7 @@
#include "smtk/mesh/core/MeshSet.h"
#include "smtk/mesh/core/PointConnectivity.h"
#include "smtk/mesh/core/QueryTypes.h"
#include "smtk/mesh/core/Selection.h"
#include "smtk/mesh/core/TypeSet.h"
#include "smtk/model/EntityRef.h"
......@@ -106,6 +107,8 @@ public:
// visit all components in a resource.
void visit(resource::Component::Visitor& v) const override;
bool erase(const smtk::mesh::Selection::Ptr&);
//determine if the given Resource is valid.
bool isValid() const;
......@@ -367,6 +370,9 @@ private:
friend std::shared_ptr<Component> Component::create(
const ResourcePtr&, const smtk::common::UUID&);
friend std::shared_ptr<Component> Component::create(const MeshSet&);
friend std::shared_ptr<Selection> Selection::create(
const ResourcePtr&, const smtk::common::UUID&);
friend std::shared_ptr<Selection> Selection::create(const MeshSet&);
std::map<smtk::common::UUID, Component::Ptr> m_componentMap;
//holds a reference to the specific backend interface
......
......@@ -52,6 +52,16 @@ std::shared_ptr<Selection> Selection::create(
return std::shared_ptr<Selection>();
}
// Attempt to find a preexisting component associated with the incident id
auto idAndComponent = resource->m_componentMap.find(id);
if (idAndComponent != resource->m_componentMap.end())
{
// If one is found, return the stored component
return std::static_pointer_cast<Selection>(idAndComponent->second);
}
// If no preexisting component exists, we need to create one.
// First, confirm there is a meshset associated with the incident id
smtk::mesh::MeshSet meshset;
{
......@@ -69,14 +79,16 @@ std::shared_ptr<Selection> Selection::create(
// Create a new component to represent the existing meshset
std::shared_ptr<smtk::resource::Component> shared(new Selection(resource, id));
auto ptr = std::static_pointer_cast<smtk::mesh::Selection>(shared->shared_from_this());
// Store the component in the resource's component map
auto ptr = std::static_pointer_cast<Selection>(shared->shared_from_this());
resource->m_componentMap[id] = ptr;
return ptr;
}
std::shared_ptr<Selection> Selection::create(const smtk::mesh::MeshSet& meshset)
{
// If the meshset is invalid, return an invalid component
// If the meshset is invalid, return an invalid selection
if (meshset.isValid() == false)
{
return std::shared_ptr<Selection>();
......@@ -90,11 +102,24 @@ std::shared_ptr<Selection> Selection::create(const smtk::mesh::MeshSet& meshset)
const_cast<smtk::mesh::MeshSet&>(meshset).setId(
smtk::common::UUIDGenerator::instance().random());
}
else
{
// Attempt to find a preexisting component associated with the incident
// meshset
auto& resource = meshset.resource();
auto idAndComponent = resource->m_componentMap.find(meshset.id());
if (idAndComponent != resource->m_componentMap.end())
{
return std::static_pointer_cast<Selection>(idAndComponent->second);
}
}
// Create a new component to represent the existing meshset
std::shared_ptr<smtk::resource::Component> shared(new Selection(meshset));
// Store the component in the resource's component map
auto ptr = std::static_pointer_cast<smtk::mesh::Selection>(shared->shared_from_this());
meshset.resource()->m_componentMap[meshset.id()] = ptr;
return ptr;
}
......
......@@ -31,6 +31,8 @@
#include "smtk/mesh/operators/Write.h"
#include "smtk/mesh/operators/WriteResource.h"
#include "smtk/mesh/resource/SelectionObserver.h"
#include "smtk/operation/groups/ExporterGroup.h"
#include "smtk/operation/groups/ImporterGroup.h"
#include "smtk/operation/groups/ReaderGroup.h"
......@@ -86,5 +88,15 @@ void Registrar::unregisterFrom(const smtk::resource::Manager::Ptr& resourceManag
{
resourceManager->unregisterResource<smtk::mesh::Resource>();
}
void Registrar::registerTo(const smtk::view::Selection::Ptr& selectionManager)
{
SelectionObserver::addTo(selectionManager);
}
void Registrar::unregisterFrom(const smtk::view::Selection::Ptr& selectionManager)
{
SelectionObserver::removeFrom(selectionManager);
}
}
}
......@@ -14,6 +14,7 @@
#include "smtk/operation/Manager.h"
#include "smtk/resource/Manager.h"
#include "smtk/view/Selection.h"
namespace smtk
{
......@@ -27,6 +28,9 @@ public:
static void registerTo(const smtk::resource::Manager::Ptr&);
static void unregisterFrom(const smtk::resource::Manager::Ptr&);
static void registerTo(const smtk::view::Selection::Ptr&);
static void unregisterFrom(const smtk::view::Selection::Ptr&);
};
}
}
......
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
#include "smtk/mesh/resource/SelectionObserver.h"
#include "smtk/mesh/core/Resource.h"
#include <algorithm>
namespace smtk
{
namespace mesh
{
std::map<smtk::view::Selection*, smtk::view::SelectionObservers::Key>
SelectionObserver::g_observerMap;
void SelectionObserver::operator()(const std::string&, smtk::view::SelectionPtr selection)
{
std::set<smtk::mesh::Selection::Ptr> newSelections, oldSelections;
for (auto& selected : selection->currentSelection())
{
if (auto meshSelection = std::dynamic_pointer_cast<smtk::mesh::Selection>(selected.first))
{
newSelections.insert(meshSelection);
}
}
std::set_difference(m_selections.begin(), m_selections.end(), newSelections.begin(),
newSelections.end(), std::inserter(oldSelections, oldSelections.begin()));
for (auto& oldSelection : oldSelections)
{
if (auto meshResource =
std::dynamic_pointer_cast<smtk::mesh::Resource>(oldSelection->resource()))
{
meshResource->erase(oldSelection);
}
m_selections.erase(oldSelection);
}
m_selections = std::move(newSelections);
}
void SelectionObserver::addTo(const smtk::view::Selection::Ptr& selectionManager)
{
g_observerMap[selectionManager.get()] =
selectionManager->observers().insert(SelectionObserver());
}
void SelectionObserver::removeFrom(const smtk::view::Selection::Ptr& selectionManager)
{
selectionManager->observers().erase(g_observerMap[selectionManager.get()]);
g_observerMap.erase(selectionManager.get());
}
}
}
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
#ifndef __smtk_mesh_resource_SelectionObserver_h
#define __smtk_mesh_resource_SelectionObserver_h
#include "smtk/CoreExports.h"
#include "smtk/mesh/core/Selection.h"
#include "smtk/view/Selection.h"
#include "smtk/view/SelectionObserver.h"
namespace smtk
{
namespace mesh
{
class SMTKCORE_EXPORT SelectionObserver
{
public:
void operator()(const std::string&, smtk::view::SelectionPtr);
static void addTo(const smtk::view::Selection::Ptr&);
static void removeFrom(const smtk::view::Selection::Ptr&);
private:
std::set<smtk::mesh::Selection::Ptr> m_selections;
static std::map<smtk::view::Selection*, smtk::view::SelectionObservers::Key> g_observerMap;
};
}
}
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment