通過指定cellid獲取周圍cellid信息,改變指定cellid的顏色

參考url:https://lorensen.github.io/VTKExamples/site/Cxx/PolyData/CellEdgeNeighbors/

1.根據指定的cellid獲取周圍cellid信息

void getNeighborCellID(int cellid,vtkPolyData* polydata,QList<int>& neighbors){
    vtkSmartPointer<vtkIdList> cellPointIds =
      vtkSmartPointer<vtkIdList>::New();
    //根據cellID獲取頂點索引信息
    polydata->GetCellPoints(cellid, cellPointIds);
    for(vtkIdType i = 0; i < cellPointIds->GetNumberOfIds(); i++){
        vtkSmartPointer<vtkIdList> idList =
          vtkSmartPointer<vtkIdList>::New();
        idList->InsertNextId(cellPointIds->GetId(i));
        //根據是否查找共享頂點或者共享邊判斷是否註銷下面代碼
        //共享頂點註銷
        if(i+1 == cellPointIds->GetNumberOfIds()){
            idList->InsertNextId(cellPointIds->GetId(0));
        }else{
            idList->InsertNextId(cellPointIds->GetId(i+1));
        }//end
        vtkSmartPointer<vtkIdList> neighborCellIds =
          vtkSmartPointer<vtkIdList>::New();
        polydata->GetCellNeighbors(cellid, idList, neighborCellIds);

        for(vtkIdType j = 0; j < neighborCellIds->GetNumberOfIds(); j++)
        {
          neighbors.push_back(neighborCellIds->GetId(j));
        }
    }
    return;
}

2.根據指定cellid改變顏色

void createSelectCellActor(vtkPolyData* polydata,QList<int>& selectCellId,vtkActor* selectActor){
    if(polydata == NULL || selectActor == NULL){
        return;
    }
    vtkSmartPointer<vtkIdTypeArray> ids =
        vtkSmartPointer<vtkIdTypeArray>::New();
    ids->SetNumberOfComponents(1);
    for(int i=0;i<selectCellId.size();i++){
        ids->InsertNextValue(selectCellId.at(i));
    }

    vtkSmartPointer<vtkSelectionNode> selectionNode =
      vtkSmartPointer<vtkSelectionNode>::New();
    selectionNode->SetFieldType(vtkSelectionNode::CELL);
    selectionNode->SetContentType(vtkSelectionNode::INDICES);
    selectionNode->SetSelectionList(ids);
    // invert the selection
    //selectionNode->GetProperties()->Set(vtkSelectionNode::INVERSE(),1);

    vtkSmartPointer<vtkSelection> selection =
        vtkSmartPointer<vtkSelection>::New();
    selection->AddNode(selectionNode);

    vtkSmartPointer<vtkExtractSelection> extractSelection =
        vtkSmartPointer<vtkExtractSelection>::New();
    extractSelection->SetInputData(0, polydata);
    extractSelection->SetInputData(1, selection);
    extractSelection->Update();

    vtkSmartPointer<vtkDataSetMapper> mapper =
      vtkSmartPointer<vtkDataSetMapper>::New();
    mapper->SetInputConnection(extractSelection->GetOutputPort());

    selectActor->SetMapper(mapper);
    return;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章