1
我有一個由邊和頂點組成的圖。當點擊圖中的一條邊時,邊應該改變顏色。我已經包含了一些代碼示例來演示我的問題。更改邊緣的顏色
繪製初始圖形;
#include "StdAfx.h"
#include <vtkSmartPointer.h>
#include <vtkCallbackCommand.h>
#include <vtkAnnotationLink.h>
#include <vtkRenderedGraphRepresentation.h>
#include <vtkRenderer.h>
#include <vtkDoubleArray.h>
#include <vtkSelectionNode.h>
#include <vtkIdTypeArray.h>
#include <vtkSelection.h>
#include <vtkRenderWindow.h>
#include <vtkUnsignedCharArray.h>
#include <vtkObjectFactory.h>
#include <vtkGraphLayoutStrategy.h>
#include <vtkGraphLayoutView.h>
#include <vtkGraphWriter.h>
#include <vtkMutableUndirectedGraph.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkIntArray.h>
#include <vtkLookupTable.h>
#include <vtkDataSetAttributes.h>
#include <vtkViewTheme.h>
void SelectionCallbackFunction(vtkObject* caller, long unsigned int eventId, void* clientData, void* callData);
vtkSmartPointer<vtkMutableUndirectedGraph> g;
int main(int, char *[])
{
g =
vtkSmartPointer<vtkMutableUndirectedGraph>::New();
vtkIdType v1 = g->AddVertex();
vtkIdType v2 = g->AddVertex();
g->AddEdge(v1, v2);
g->AddEdge(v1, v2);
vtkSmartPointer<vtkCallbackCommand> selectionCallback =
vtkSmartPointer<vtkCallbackCommand>::New();
selectionCallback->SetCallback (SelectionCallbackFunction);
// Create the color array
vtkSmartPointer<vtkIntArray> edgeColors =
vtkSmartPointer<vtkIntArray>::New();
edgeColors->SetNumberOfComponents(1);
edgeColors->SetName("Color");
vtkSmartPointer<vtkLookupTable> lookupTable =
vtkSmartPointer<vtkLookupTable>::New();
lookupTable->SetNumberOfTableValues(1);
lookupTable->SetTableValue(0, 1.0, 0.0, 0.0); // red
lookupTable->Build();
edgeColors->InsertNextValue(0);
// Add the color array to the graph
g->GetEdgeData()->AddArray(edgeColors);
vtkSmartPointer<vtkGraphLayoutView> view =
vtkSmartPointer<vtkGraphLayoutView>::New();
view->SetEdgeColorArrayName("Color");
view->ColorEdgesOn();
vtkSmartPointer<vtkViewTheme> theme =
vtkSmartPointer<vtkViewTheme>::New();
theme->SetCellLookupTable(lookupTable);
view->ApplyViewTheme(theme);
view->AddRepresentationFromInput(g);
view->SetLayoutStrategy("Simple 2D");
view->GetRepresentation()->GetAnnotationLink()->AddObserver("AnnotationChangedEvent", selectionCallback);
view->ResetCamera();
view->Render();
view->GetInteractor()->Start();
return EXIT_SUCCESS;
}
對於鼠標點擊功能,我使用了下面的代碼;
vtkAnnotationLink* annotationLink =
static_cast<vtkAnnotationLink*>(caller);
vtkSelection* selection = annotationLink->GetCurrentSelection();
vtkSelectionNode* edges;
if(selection->GetNode(0)->GetFieldType() == vtkSelectionNode::EDGE)
{
edges = selection->GetNode(0);
}
if(selection->GetNode(1)->GetFieldType() == vtkSelectionNode::EDGE)
{
edges = selection->GetNode(1);
}
vtkIdTypeArray* edgeList = vtkIdTypeArray::SafeDownCast(edges->GetSelectionList());
for(vtkIdType i = 0; i < edgeList->GetNumberOfTuples(); i++)
{
//Change colour of the edge
}
我的問題是,我不能動態地改變邊緣的顏色。我將非常感謝有關此事的任何幫助。
「dinamically」是什麼意思?它應該在沒有鼠標點擊的情況下完成?或者你在點擊和渲染結果之間有滯後?或者別的,也許? –
是的,鼠標點擊 – Madz
好吧,對不起,我剛剛注意到上次循環中缺少的代碼,請求已清除。不幸的是,我沒有VTK圖形對象的經驗......但是你可以從代表圖形的對象中調用類似GetProperty() - > SetEdgeColor()的東西嗎?這應該是你想要的... –