Domanda

I have an graph which consists of edges and vertices. When the clicks an edge in the graph, the edge is supposed to change colour. I have included some code samples to demonstrate my problem.

To draw the initial graph;

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

For the mouse click function I have used the below code;

    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
    }

My problem is that I cannot change the colour of the edge dynamically. I would be very grateful for any help regarding the matter.

È stato utile?

Soluzione

The below code worked for me. First when I create the graph I set the colour of each and every 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);

Then in my mouse click function I get the vtkIdType of the clicked edge and set the colour of it.

    vtkIdType edge = edgeList->GetValue(0);
    edgeColors->InsertValue(edge.Id,1);//set colour of edge
    graphLayoutView->GetInteractor()->Render();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top