1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkCellGridCopyQuery.h"
#include "vtkAbstractArray.h"
#include "vtkCellAttribute.h"
#include "vtkCellGrid.h"
#include "vtkDataSetAttributes.h"
#include "vtkIdList.h"
#include "vtkObjectFactory.h"
#include <algorithm>
VTK_ABI_NAMESPACE_BEGIN
vtkStandardNewMacro(vtkCellGridCopyQuery);
vtkCxxSetObjectMacro(vtkCellGridCopyQuery, Source, vtkCellGrid);
vtkCxxSetObjectMacro(vtkCellGridCopyQuery, Target, vtkCellGrid);
vtkCellGridCopyQuery::~vtkCellGridCopyQuery()
{
this->SetSource(nullptr);
this->SetTarget(nullptr);
this->ResetCellAttributeIds();
}
void vtkCellGridCopyQuery::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
os << indent << "Source: " << this->Source << "\n";
os << indent << "Target: " << this->Target << "\n";
os << indent << "CopyCells: " << (this->CopyCells ? "Y" : "N") << "\n";
os << indent << "CopyOnlyShape: " << (this->CopyOnlyShape ? "Y" : "N") << "\n";
os << indent << "CopyArrays: " << (this->CopyArrays ? "Y" : "N") << "\n";
os << indent << "CopyArrayValues: " << (this->CopyArrayValues ? "Y" : "N") << "\n";
os << indent << "DeepCopyArrays: " << (this->DeepCopyArrays ? "Y" : "N") << "\n";
os << indent << "CopySchema: " << (this->CopySchema ? "Y" : "N") << "\n";
os << indent << "CellAttributeIds: ";
for (const auto& id : this->CellAttributeIds)
{
os << " " << id;
}
if (this->CellAttributeIds.empty())
{
os << " (empty)";
}
os << "\n";
vtkIndent i2 = indent.GetNextIndent();
os << indent << "ArrayMap: " << this->ArrayMap.size() << " entries\n";
for (const auto& entry : this->ArrayMap)
{
if (!entry.first || !entry.second)
{
os << i2 << entry.first << ": " << entry.second << "\n";
}
else
{
os << i2 << entry.first << " (" << entry.first->GetName() << "): " << entry.second << " ("
<< entry.second->GetName() << ")\n";
}
}
os << indent << "AttributeMap: " << this->AttributeMap.size() << " entries\n";
for (const auto& entry : this->AttributeMap)
{
if (!entry.first || !entry.second)
{
os << i2 << entry.first << ": " << entry.second << "\n";
}
else
{
os << i2 << entry.first << " (" << entry.first->GetName().Data() << "): " << entry.second
<< " (" << entry.second->GetName().Data() << ")\n";
}
}
}
bool vtkCellGridCopyQuery::Initialize()
{
bool ok = this->Superclass::Initialize();
this->ArrayMap.clear();
this->AttributeMap.clear();
return ok;
}
bool vtkCellGridCopyQuery::Finalize()
{
// Copy cell-attribute range caches, but only when performing shallow
// copies as deep copies indicate that values will likely be changed
// downstream.
if (this->CopyArrays && this->CopyArrayValues && !this->DeepCopyArrays)
{
const auto& sourceRangeCache(this->Source->GetRangeCache());
auto& targetRangeCache(this->Target->GetRangeCache());
for (const auto& entry : this->AttributeMap)
{
auto it = sourceRangeCache.find(entry.first);
if (it != sourceRangeCache.end())
{
targetRangeCache[entry.second] = it->second;
}
}
}
this->ArrayMap.clear();
this->AttributeMap.clear();
if (this->CopySchema)
{
this->Target->SetSchema(this->Source->GetSchemaName(), this->Source->GetSchemaVersion());
}
if (this->Source->GetContentVersion() > 0)
{
this->Target->SetContentVersion(this->Source->GetContentVersion() + 1);
}
// Do not copy the integer attribute counter:
// this->Target->NextAttribute = this->Source->NextAttribute;
return true;
}
bool vtkCellGridCopyQuery::AddSourceCellAttributeId(int attributeId)
{
return this->CellAttributeIds.insert(attributeId).second;
}
bool vtkCellGridCopyQuery::RemoveSourceCellAttributeId(int attributeId)
{
return this->CellAttributeIds.erase(attributeId) > 0;
}
bool vtkCellGridCopyQuery::AddAllSourceCellAttributeIds()
{
if (this->Source)
{
std::size_t nn = this->CellAttributeIds.size();
auto allIds = this->Source->GetCellAttributeIds();
this->CellAttributeIds.insert(allIds.begin(), allIds.end());
return nn != this->CellAttributeIds.size();
}
return false;
}
void vtkCellGridCopyQuery::GetCellAttributeIds(vtkIdList* ids) const
{
if (!ids)
{
vtkErrorMacro("Null ids passed to " << __func__ << ".");
return;
}
ids->Initialize();
ids->SetNumberOfIds(this->CellAttributeIds.size());
int ii = 0;
for (const auto& id : this->CellAttributeIds)
{
ids->SetId(ii, id);
++ii;
}
}
void vtkCellGridCopyQuery::ResetCellAttributeIds()
{
this->CellAttributeIds.clear();
}
void vtkCellGridCopyQuery::CopyAttributeArrays(vtkCellAttribute* srcAtt, vtkStringToken cellType)
{
if (!this->CopyArrays)
{
return;
}
if (!srcAtt)
{
vtkErrorMacro("Null source attribute.");
return;
}
auto srcCellTypeInfo = srcAtt->GetCellTypeInfo(cellType);
auto& arraysByRole = srcCellTypeInfo.ArraysByRole;
for (const auto& roleEntry : arraysByRole)
{
auto srcArr = roleEntry.second;
int arrType = this->Source->GetAttributeTypeForArray(srcArr);
auto* tgtGroup = this->Target->GetAttributes(arrType);
if (this->CopyArrayValues && !this->DeepCopyArrays)
{
// We should copy by referencing the original array;
// just add the source array to the destination.
tgtGroup->AddArray(srcArr);
}
else
{
// Deep-copy the source array as needed.
// I. See if we've already copied the array:
auto mapIt = this->ArrayMap.find(srcArr);
if (mapIt != this->ArrayMap.end())
{
// No need to re-create the array; we've already copied it.
// Just add it to the array grouping.
tgtGroup->AddArray(mapIt->second);
continue;
}
// II. We need to create an array.
auto* tgtArr = vtkAbstractArray::CreateArray(srcArr->GetDataType());
if (this->CopyArrayValues)
{
// Create a deep copy including the values:
tgtArr->DeepCopy(srcArr);
}
else
{
// Copy array "metadata" only.
if (srcArr->HasInformation())
{
tgtArr->CopyInformation(srcArr->GetInformation(), /*deep*/ 1);
}
tgtArr->SetName(srcArr->GetName());
tgtArr->SetNumberOfComponents(srcArr->GetNumberOfComponents());
tgtArr->CopyComponentNames(srcArr);
}
tgtGroup->AddArray(tgtArr);
this->ArrayMap[srcArr] = tgtArr;
tgtArr->Delete();
}
}
}
vtkCellAttribute* vtkCellGridCopyQuery::CopyOrUpdateAttributeRecord(
vtkCellAttribute* srcAtt, vtkStringToken cellType)
{
// If we already have the attribute, use it. Otherwise, create it.
vtkCellAttribute* targetAttribute = nullptr;
auto amit = this->AttributeMap.find(srcAtt);
if (amit == this->AttributeMap.end())
{
// We need to create the attribute.
vtkNew<vtkCellAttribute> tgtAtt;
tgtAtt->ShallowCopy(srcAtt, /* do not copy arrays for cell types*/ false);
targetAttribute = tgtAtt;
// Copy cached range data if we are copying cells.
if (this->GetCopyCells() && this->GetCopyArrayValues())
{
const auto& srcRange = this->Source->GetRangeCache();
auto& tgtRange = this->Target->GetRangeCache();
auto cacheIt = srcRange.find(srcAtt);
if (cacheIt != srcRange.end())
{
tgtRange[tgtAtt].resize(tgtAtt->GetNumberOfComponents() + 2);
std::size_t ii = 0;
for (const auto& componentRange : cacheIt->second)
{
if (componentRange.FiniteRangeTime > srcAtt->GetMTime())
{
tgtRange[tgtAtt][ii].FiniteRange = cacheIt->second[ii].FiniteRange;
tgtRange[tgtAtt][ii].FiniteRangeTime.Modified();
}
if (componentRange.EntireRangeTime > srcAtt->GetMTime())
{
tgtRange[tgtAtt][ii].EntireRange = cacheIt->second[ii].EntireRange;
tgtRange[tgtAtt][ii].EntireRangeTime.Modified();
}
++ii;
}
}
}
this->Target->AddCellAttribute(tgtAtt);
this->AttributeMap[srcAtt] = tgtAtt;
if (this->Source->GetShapeAttribute() == srcAtt)
{
this->Target->SetShapeAttribute(tgtAtt);
}
}
else
{
targetAttribute = amit->second;
}
if (!this->CopyCellTypes)
{
return targetAttribute;
}
// Regardless of whether the attribute pre-existed or not,
// add arrays for each cell type.
auto oldCellTypeInfo = srcAtt->GetCellTypeInfo(cellType);
auto& oldArraysForCellType = oldCellTypeInfo.ArraysByRole;
vtkCellAttribute::CellTypeInfo newCellTypeInfo;
auto& arraysForCellType = newCellTypeInfo.ArraysByRole;
newCellTypeInfo.DOFSharing = oldCellTypeInfo.DOFSharing;
newCellTypeInfo.FunctionSpace = oldCellTypeInfo.FunctionSpace;
newCellTypeInfo.Basis = oldCellTypeInfo.Basis;
newCellTypeInfo.Order = oldCellTypeInfo.Order;
for (const auto& entry : oldArraysForCellType)
{
auto it = this->ArrayMap.find(entry.second);
if (it != this->ArrayMap.end())
{
arraysForCellType[entry.first] = it->second;
}
else if (this->CopyArrays && this->CopyArrayValues && !this->DeepCopyArrays)
{
arraysForCellType[entry.first] = entry.second;
}
}
targetAttribute->SetCellTypeInfo(cellType, newCellTypeInfo);
return targetAttribute;
}
VTK_ABI_NAMESPACE_END
|