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 314 315
|
/*****************************************************************************
* $CAMITK_LICENCE_BEGIN$
*
* CamiTK - Computer Assisted Medical Intervention ToolKit
* (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
*
* Visit http://camitk.imag.fr for more information
*
* This file is part of CamiTK.
*
* CamiTK is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3
* only, as published by the Free Software Foundation.
*
* CamiTK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 3 for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
*
* $CAMITK_LICENCE_END$
****************************************************************************/
#include "MeshComponent.h"
#include <algorithm>
// disable warning generated by clang about the surrounded headers
#include "CamiTKDisableWarnings"
#include <vtkExtractSelection.h>
#include <vtkConvertSelection.h>
#include "CamiTKReEnableWarnings"
#include <vtkSelection.h>
#include <vtkSelectionNode.h>
#include <vtkAbstractArray.h>
#include <vtkIdTypeArray.h>
namespace camitk {
// -------------------- constructor --------------------
MeshSelectionModel::MeshSelectionModel(MeshComponent* const meshComp) :
QAbstractTableModel(meshComp), meshComponent(meshComp) {
fieldName[vtkSelectionNode::POINT] = QString("Points");
fieldName[vtkSelectionNode::CELL] = QString("Cells");
contentName[vtkSelectionNode::INDICES] = "Indices";
}
// -------------------- rowCount --------------------
int MeshSelectionModel::rowCount(const QModelIndex& parent) const {
if (!meshComponent) {
return 0;
}
return meshComponent->getSelections().size();
}
// -------------------- columnCount --------------------
int MeshSelectionModel::columnCount(const QModelIndex& parent) const {
return 4;
}
// -------------------- data --------------------
QVariant MeshSelectionModel::data(const QModelIndex& index, int role) const {
if (!meshComponent) {
return QVariant();
}
int row = index.row();
int col = index.column();
vtkSmartPointer< vtkSelectionNode > selNode = meshComponent->getSelections().at(row);
switch (role) {
case Qt::DisplayRole : {
switch (col) {
case 0 :
if (selNode->GetSelectionList()) {
return QString(selNode->GetSelectionList()->GetName());
}
else {
return QString("Unknown");
}
break;
case 1 :
return QString(fieldName[selNode->GetFieldType()]);
break;
case 2 :
return QString(contentName[selNode->GetContentType()]);
break;
case 3 :
if (selNode->GetSelectionList()) {
return QString::number(selNode->GetSelectionList()->GetNumberOfTuples());
}
else {
return QString::number(0);
}
break;
default :
return QVariant();
break;
}
break;
}
case Qt::DecorationRole : {
if (col == 1) {
if (selNode->GetFieldType() == vtkSelectionNode::POINT) {
return QIcon(":/points");
}
else if (selNode->GetFieldType() == vtkSelectionNode::CELL) {
return QIcon(":/cell");
}
else {
return QVariant();
}
}
else {
return QVariant();
}
break;
}
default :
return QVariant();
break;
}
return QVariant();
}
// -------------------- headerData --------------------
QVariant MeshSelectionModel::headerData(int section, Qt::Orientation orientation, int role) const {
if (role == Qt::DisplayRole) {
if (orientation == Qt::Horizontal) {
switch (section) {
case 0:
return QString("Name");
break;
case 1:
return QString("Type");
break;
case 2:
return QString("Content");
break;
case 3 :
return QString("Count");
break;
default:
return QVariant();
break;
}
}
}
return QVariant();
}
// -------------------- insertSelection --------------------
int MeshSelectionModel::insertSelection(const QString& name, int fieldType, int contentType, vtkSmartPointer< vtkAbstractArray > array, InsertionPolicy policy) {
// TODO : check if a selection of the same name already exists ?
int index = meshComponent->getSelectionIndex(name);
// if no selection with the same name exists, create it and add it to the model
// except if the insertion policy is SUBTRACT
if ((index < 0) && (policy != SUBTRACT)) {
// insert the new row
beginInsertRows(QModelIndex(), meshComponent->getSelections().size(), meshComponent->getSelections().size());
vtkSmartPointer< vtkSelectionNode > selNode = vtkSmartPointer< vtkSelectionNode >::New();
array->SetName(name.toStdString().c_str());
selNode->SetFieldType(fieldType);
selNode->SetContentType(contentType);
selNode->SetSelectionList(array);
// TODO direct manipulation of selection list should not be allowed outside MeshComponent
// meshComponent->addSelection(name,fieldType,contentType,array);
meshComponent->getSelections().append(selNode);
endInsertRows();
index = meshComponent->getSelections().size() - 1;
}
// else update the selection data according to insertion policy
else {
vtkSmartPointer< vtkSelectionNode > selNode = meshComponent->getSelections().at(index);
// if the field types are different, the selection data cannot be merged or subtracted
if (fieldType != selNode->GetFieldType() && (policy == MERGE || policy == SUBTRACT)) {
index = -1;
}
else {
switch (policy) {
case REPLACE : {
// replace the selection data
array->SetName(name.toStdString().c_str());
selNode->SetFieldType(fieldType);
selNode->SetContentType(contentType);
selNode->SetSelectionList(array);
emit dataChanged(this->index(index, 1), this->index(index, 3));
}
break;
// TODO the selection content must be the same
case MERGE : {
// merge the selection list
// in this case, the content type of the new selection list are indices
vtkSmartPointer< vtkSelectionNode > newSelNode = vtkSmartPointer< vtkSelectionNode >::New();
newSelNode->SetFieldType(fieldType);
newSelNode->SetContentType(contentType);
newSelNode->SetSelectionList(array);
selNode->UnionSelectionList(newSelNode);
emit dataChanged(this->index(index, 1), this->index(index, 3));
}
break;
case SUBTRACT : {
// subtract the selection list
// in this case, the content type of the new selection list are indices
vtkSmartPointer< vtkIdTypeArray > oldIdsArray = vtkIdTypeArray::SafeDownCast(selNode->GetSelectionList());
vtkSmartPointer< vtkIdTypeArray > newIdsArray = vtkIdTypeArray::SafeDownCast(array);
if ((!newIdsArray) || (!oldIdsArray)) {
index = -1;
break;
}
const int nbOldIndices = oldIdsArray->GetNumberOfTuples();
const int nbNewIndices = newIdsArray->GetNumberOfTuples();
std::vector< vtkIdType > oldIndices(nbOldIndices);
std::vector< vtkIdType > newIndices(nbNewIndices);
// sort the indices
std::partial_sort_copy(oldIdsArray->GetPointer(0), oldIdsArray->GetPointer(0) + nbOldIndices, oldIndices.begin(), oldIndices.end());
std::partial_sort_copy(newIdsArray->GetPointer(0), newIdsArray->GetPointer(0) + nbNewIndices, newIndices.begin(), newIndices.end());
std::vector< vtkIdType > diff(nbOldIndices);
// compute the difference between old and new indices
std::vector< vtkIdType >::iterator it;
it = std::set_difference(oldIndices.begin(), oldIndices.end(), newIndices.begin(), newIndices.end(), diff.begin());
diff.resize(it - diff.begin());
vtkSmartPointer< vtkIdTypeArray > diffArray = vtkSmartPointer< vtkIdTypeArray >::New();
diffArray->SetNumberOfValues(diff.size());
// cannot use std::copy(diff.begin(), diff.end(), diffArray->GetPointer(0)) as MSVC complains and generates
// a warning C4996: 'std::copy::_Unchecked_iterators::_Deprecate'
for (const auto val : diff) {
diffArray->InsertNextValue(val);
}
selNode->SetSelectionList(diffArray);
emit dataChanged(this->index(index, 1), this->index(index, 3));
}
break;
case DISCARD :
default :
index = -1;
break;
}
}
}
return index;
}
// -------------------- removeSelection --------------------
int MeshSelectionModel::removeSelection(const QString& name) {
int index = meshComponent->getSelectionIndex(name);
if (index >= 0) {
beginRemoveRows(QModelIndex(), index, index);
meshComponent->getSelections().removeAt(index);
endRemoveRows();
}
return index;
}
// -------------------- setData --------------------
bool MeshSelectionModel::setData(const QModelIndex& index, const QVariant& value, int role) {
if (
index.isValid() &&
role == Qt::EditRole &&
index.column() == 0 /*&&
index.row() > 0*/
) {
// check if the name already exists
if (meshComponent->getSelectionIndex(value.toString()) >= 0) {
return false;
}
int row = index.row();
meshComponent->getSelections().at(row)->GetSelectionList()->SetName(value.toString().toStdString().c_str());
emit dataChanged(index, index);
return true;
}
return false;
}
// -------------------- flags --------------------
Qt::ItemFlags MeshSelectionModel::flags(const QModelIndex& index) const {
if (index.column() == 0) {
return Qt::ItemIsSelectable | Qt::ItemIsEditable | Qt::ItemIsEnabled ;
}
else {
return Qt::ItemIsSelectable | Qt::ItemIsEnabled ;
}
}
}
|