Commit 08685886 authored by T.J. Corona's avatar T.J. Corona

VTK session: fix Read/Import ops to enable persistent model properties

parent 8db6edcd
......@@ -221,14 +221,15 @@ smtk::model::EntityRef Session::toEntityRef(const EntityHandle& ent)
// ++ 6 ++
/// Add the dataset and its blocks to the session.
smtk::model::Model Session::addModel(vtkSmartPointer<vtkMultiBlockDataSet>& model)
smtk::model::Model Session::addModel(
vtkSmartPointer<vtkMultiBlockDataSet>& model, SessionInfoBits requestedInfo)
{
EntityHandle handle(
static_cast<int>(this->m_models.size()), model.GetPointer(), shared_from_this());
this->m_models.push_back(model);
smtk::model::Model result = this->toEntityRef(handle);
this->m_revIdMap[result] = handle;
this->transcribe(result, smtk::model::SESSION_EVERYTHING, false);
this->transcribe(result, requestedInfo, false);
result.setSession(smtk::model::SessionRef(this->resource(), this->sessionId()));
return result;
}
......@@ -372,8 +373,10 @@ SessionInfoBits Session::transcribeInternal(
// If the entity is valid, is there any reason to refresh it?
// Perhaps we want additional information transcribed?
if (this->danglingEntities().find(mutableEntityRef) == this->danglingEntities().end())
{
return smtk::model::
SESSION_EVERYTHING; // Not listed as dangling => everything transcribed already.
}
}
// -- 10 --
......@@ -594,14 +597,20 @@ static void AddBoxToTessellation(vtkImageData* img, smtk::model::Tessellation& t
bool Session::addTessellation(const smtk::model::EntityRef& entityref, const EntityHandle& handle)
{
if (entityref.hasTessellation())
{
return true; // no need to recompute.
}
vtkDataObject* data = handle.object<vtkDataObject>();
if (!data)
{
return false; // Can't squeeze triangles from a NULL
}
if (vtkMultiBlockDataSet::SafeDownCast(data))
{
return false; // Don't try to tessellate parent groups of leaf nodes.
}
// Don't tessellate image data that is serving as a label map.
EntityType etype = static_cast<EntityType>(data->GetInformation()->Get(SMTK_GROUP_TYPE()));
......
......@@ -154,12 +154,19 @@ public:
static vtkInformationObjectBaseVectorKey* SMTK_CHILDREN();
static vtkInformationDoubleKey* SMTK_LABEL_VALUE();
smtk::model::Model addModel(vtkSmartPointer<vtkMultiBlockDataSet>& model);
smtk::model::Model addModel(vtkSmartPointer<vtkMultiBlockDataSet>& model,
SessionInfoBits requestedInfo = smtk::model::SESSION_EVERYTHING);
std::string defaultFileExtension(const smtk::model::Model& model) const override;
bool ensureChildParentMapEntry(vtkDataObject* child, vtkDataObject* parent, int idxInParent);
bool addTessellation(const smtk::model::EntityRef&, const EntityHandle&);
ReverseIdMap_t& reverseIdMap() { return m_revIdMap; }
size_t numberOfModels() const;
protected:
friend struct EntityHandle;
......@@ -175,9 +182,6 @@ protected:
// std::map<EntityHandle,smtk::model::EntityRef> m_fwdIdMap; // not needed; store UUID in vtkInformation.
// -- 1 --
bool addTessellation(const smtk::model::EntityRef&, const EntityHandle&);
size_t numberOfModels() const;
vtkDataObject* modelOfHandle(const EntityHandle& h) const;
vtkDataObject* parent(vtkDataObject* obj) const;
int parentIndex(vtkDataObject* obj) const;
......@@ -210,7 +214,9 @@ T* EntityHandle::object() const
// Never return the pointer if the other data is invalid:
if (!this->m_session || !this->m_object || this->m_modelNumber < 0 ||
this->m_modelNumber > static_cast<int>(this->m_session->numberOfModels()))
{
return NULL;
}
return dynamic_cast<T*>(this->m_object.GetPointer());
}
......
......@@ -137,21 +137,28 @@ Import::Result Import::operateInternal()
return this->importExodus(resource);
}
static void AddPreservedUUID(
vtkDataObject* data, int& curId, const std::vector<smtk::common::UUID>& uuids)
static void AddPreservedUUID(vtkDataObject* data, int& curId, vtkDataObject* parentData,
int idxInParent, int modelNumber, const std::vector<smtk::common::UUID>& uuids,
const SessionPtr& session, const ResourcePtr& resource)
{
if (!data || curId < 0 || static_cast<std::size_t>(curId) >= uuids.size())
return;
// Assign the preserved UUID to the data's information.
vtkInformation* info = data->GetInformation();
info->Set(Session::SMTK_UUID_KEY(), uuids.at(curId).toString().c_str());
EntityHandle handle(modelNumber, data, parentData, idxInParent, session);
EntityRef eref(resource, uuids.at(curId));
session->reverseIdMap()[eref] = handle;
session->addTessellation(eref, handle);
++curId;
}
static void AddPreservedUUIDsRecursive(
vtkDataObject* data, int& curId, const std::vector<smtk::common::UUID>& uuids)
static void AddPreservedUUIDsRecursive(vtkDataObject* data, int& curId, vtkDataObject* parentData,
int idxInParent, int modelNumber, const std::vector<smtk::common::UUID>& uuids,
const SessionPtr& session, const ResourcePtr& resource)
{
AddPreservedUUID(data, curId, uuids);
AddPreservedUUID(data, curId, parentData, idxInParent, modelNumber, uuids, session, resource);
vtkMultiBlockDataSet* mbds = vtkMultiBlockDataSet::SafeDownCast(data);
if (mbds)
......@@ -159,11 +166,64 @@ static void AddPreservedUUIDsRecursive(
int nb = mbds->GetNumberOfBlocks();
for (int i = 0; i < nb; ++i)
{
AddPreservedUUIDsRecursive(mbds->GetBlock(i), curId, uuids);
AddPreservedUUIDsRecursive(
mbds->GetBlock(i), curId, data, i, modelNumber, uuids, session, resource);
}
}
}
static smtk::model::Model addModel(vtkSmartPointer<vtkMultiBlockDataSet>& modelOut,
const std::vector<smtk::common::UUID>& uuids, const SessionPtr& session,
const ResourcePtr& resource)
{
// If we are reading a vtk session resource (as opposed to a new import), we
// should access the existing model instead of creating a new one here. If
// this is the case, then the first preserved id will be related to a model
// entity that is already in the resource (as it was put there by the Read
// operation calling this one).
// A default-constructed model is invalid.
smtk::model::Model smtkModelOut;
if (uuids.empty() == false)
{
// The first id in the list of preserved ids is for the containing model.
smtkModelOut = smtk::model::Model(resource, uuids.at(0));
// The resulting model may be invalid if the model was not successfully
// constructed in the parent Read operation (e.g. for LegacyRead). In this
// situation, we can construct the model here.
if (smtkModelOut.isValid() == false)
{
// First set the UUID on the multiblock dataset representing the model.
// This way, the resulting smtk model will have the right UUID.
vtkInformation* info = modelOut->GetInformation();
info->Set(Session::SMTK_UUID_KEY(), uuids.at(0).toString().c_str());
smtkModelOut = session->addModel(modelOut, smtk::model::SESSION_EVERYTHING);
}
// We are about to add a model. Its number will be the next index in the
// session's vector of models.
int modelNumber = session->numberOfModels();
// Add the backend information associated to the model, but don't bother
// transcribing it.
smtkModelOut = session->addModel(modelOut, smtk::model::SESSION_NOTHING);
// Assign preserved UUIDs, link the vtk data object to the smtk component
// and construct tessellations for the imported components.
int curId = 0;
AddPreservedUUIDsRecursive(modelOut, curId, nullptr, -1, modelNumber, uuids, session, resource);
}
else
{
// This is a proper import (there are no preexisitng UUIDs), so we can let
// the Session create entries in the model resource for us.
smtkModelOut = session->addModel(modelOut, smtk::model::SESSION_EVERYTHING);
}
return smtkModelOut;
}
static void AddBlockChildrenAsModelChildren(vtkMultiBlockDataSet* data)
{
if (!data)
......@@ -315,16 +375,12 @@ Import::Result Import::importExodus(const smtk::session::vtk::Resource::Ptr& res
MarkMeshInfo(modelOut, dim, path(filename).stem().string<std::string>().c_str(), EXO_MODEL, -1);
AddBlockChildrenAsModelChildren(modelOut);
// If we have preserved UUIDs, assign them now before anything else does:
int curId = 0;
AddPreservedUUIDsRecursive(modelOut, curId, this->m_preservedUUIDs);
// Now that the datasets we wish to present are marked,
// have the Session create entries in the model resource for us:
smtk::model::Model smtkModelOut = session->addModel(modelOut);
smtkModelOut.setStringProperty("url", filename);
smtkModelOut.setStringProperty("type", "exodus");
smtk::model::Model smtkModelOut = addModel(modelOut, this->m_preservedUUIDs, session, resource);
if (this->m_preservedUUIDs.empty())
{
smtkModelOut.setStringProperty("url", filename);
smtkModelOut.setStringProperty("type", "exodus");
}
// Now set model for session and transcribe everything.
Import::Result result = this->createResult(Import::Outcome::SUCCEEDED);
......@@ -376,10 +432,6 @@ Import::Result Import::importSLAC(const smtk::session::vtk::Resource::Ptr& resou
FillAndMarkBlocksFromSrc(modelOut, ii, blocks[0], 2, "surface", EXO_SIDE_SET);
FillAndMarkBlocksFromSrc(modelOut, ii, blocks[1], 3, "volume", EXO_BLOCK);
// If we have preserved UUIDs, assign them now before anything else does:
int curId = 0;
AddPreservedUUIDsRecursive(modelOut, curId, this->m_preservedUUIDs);
MarkMeshInfo(
modelOut.GetPointer(), 3, path(filename).stem().string<std::string>().c_str(), EXO_MODEL, -1);
AddBlockChildrenAsModelChildren(modelOut);
......@@ -396,9 +448,12 @@ Import::Result Import::importSLAC(const smtk::session::vtk::Resource::Ptr& resou
}
}
smtk::model::Model smtkModelOut = session->addModel(modelOut);
smtkModelOut.setStringProperty("url", filename);
smtkModelOut.setStringProperty("type", "slac");
smtk::model::Model smtkModelOut = addModel(modelOut, this->m_preservedUUIDs, session, resource);
if (this->m_preservedUUIDs.empty())
{
smtkModelOut.setStringProperty("url", filename);
smtkModelOut.setStringProperty("type", "slac");
}
// Now set model for session and transcribe everything.
Import::Result result = this->createResult(Import::Outcome::SUCCEEDED);
......@@ -546,10 +601,6 @@ Import::Result Import::importLabelMap(const smtk::session::vtk::Resource::Ptr& r
modelOut->SetNumberOfBlocks(1);
modelOut->SetBlock(0, img.GetPointer());
// If we have preserved UUIDs, assign them now before anything else does:
int curId = 0;
AddPreservedUUIDsRecursive(modelOut, curId, this->m_preservedUUIDs);
MarkMeshInfo(modelOut.GetPointer(), imgDim, path(filename).stem().string<std::string>().c_str(),
EXO_MODEL, -1);
MarkMeshInfo(img.GetPointer(), imgDim, labelname.c_str(), EXO_LABEL_MAP, -1);
......@@ -559,10 +610,13 @@ Import::Result Import::importLabelMap(const smtk::session::vtk::Resource::Ptr& r
vtkDataObject::SafeDownCast(Session::SMTK_CHILDREN()->Get(info, j)), img.GetPointer(), j);
}
smtk::model::Model smtkModelOut = session->addModel(modelOut);
smtkModelOut.setStringProperty("url", filename);
smtkModelOut.setStringProperty("type", "label map");
smtkModelOut.setStringProperty("label array", labelname);
smtk::model::Model smtkModelOut = addModel(modelOut, this->m_preservedUUIDs, session, resource);
if (this->m_preservedUUIDs.empty())
{
smtkModelOut.setStringProperty("url", filename);
smtkModelOut.setStringProperty("type", "label map");
smtkModelOut.setStringProperty("label array", labelname);
}
// Now set model for session and transcribe everything.
Import::Result result = this->createResult(Import::Outcome::SUCCEEDED);
......
......@@ -66,6 +66,10 @@ Read::Result Read::operateInternal()
resource->setLocation(filename);
resource->setSession(session);
// Transcribe model data onto the resource
auto modelResource = std::static_pointer_cast<smtk::model::Resource>(resource);
smtk::model::from_json(j, modelResource);
// Access all of the model files contained by the resource file.
std::vector<std::string> modelFiles = j.at("modelFiles");
......
......@@ -84,7 +84,7 @@ PySharedPtrClass< smtk::session::vtk::Session, smtk::model::Session > pybind11_i
.def_static("SMTK_PEDIGREE", &smtk::session::vtk::Session::SMTK_PEDIGREE)
.def_static("SMTK_UUID_KEY", &smtk::session::vtk::Session::SMTK_UUID_KEY)
.def_static("SMTK_VISIBILITY", &smtk::session::vtk::Session::SMTK_VISIBILITY)
.def("addModel", &smtk::session::vtk::Session::addModel, py::arg("model"))
.def("addModel", &smtk::session::vtk::Session::addModel, py::arg("model"), py::arg("requestedInfo"))
.def("allSupportedInformation", &smtk::session::vtk::Session::allSupportedInformation)
.def("className", &smtk::session::vtk::Session::className)
.def_static("create", (std::shared_ptr<smtk::session::vtk::Session> (*)()) &smtk::session::vtk::Session::create)
......
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