c++ - 改变边的颜色

标签 c++ vtk

我有一个由边和顶点组成的图。当单击图中的一条边时,该边应该会改变颜色。我包含了一些代码示例来演示我的问题。

绘制初始图形;

#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
    }

我的问题是我不能动态改变边缘的颜色。如有任何帮助,我将不胜感激。

最佳答案

下面的代码对我有用。首先,当我创建图形时,我设置了每条边的颜色,

    edgeColors = vtkSmartPointer<vtkIntArray>::New();
    edgeColors->SetNumberOfComponents(1);
    edgeColors->SetName("Color");

    vtkSmartPointer<vtkLookupTable> lookupTable = 
        vtkSmartPointer<vtkLookupTable>::New();
    lookupTable->SetNumberOfTableValues(2);
    lookupTable->SetTableValue(0, 0.5, 1.0, 0.5); // green
    lookupTable->SetTableValue(1, 0.0, 1.0, 0.0); // white

    lookupTable->Build();

    //For each edge id insert colour
    for(int i = 0;i<=graph->GetNumberOfEdges();i++)
        edgeColors->InsertValue(i,0);


    // Add the color array to the graph
    graph->GetEdgeData()->AddArray(edgeColors);

然后在我的鼠标点击函数中,我得到被点击边的 vtkIdType 并设置它的颜色。

    vtkIdType edge = edgeList->GetValue(0);
    edgeColors->InsertValue(edge.Id,1);//set colour of edge
    graphLayoutView->GetInteractor()->Render();

关于c++ - 改变边的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19627720/

相关文章:

computational-geometry - vtk中顶点和点的区别

python - 数据从vtk到python

python - vtk boxwidget2的实现

javascript - 用作 XTK 输入的 VTK 文件的文件格式

模板参数中的 C++ 子类化

c++ - 如何连接两个表? (C++)

c++ - 为什么 std::filesystem 提供这么多非成员函数?

c++ - 变量卡在 If 中

c++ - Value Printed 根据其后的指令而变化

c++ - Windows 10下为VTK生成Makefile时Qt mkspecs路径错误