Commit d09cb785 authored by Jeff Baumes's avatar Jeff Baumes

BUG: Adding VTK_USE_CHARTS to VTKConfig.cmake.in. ENH: Adding a 2D graph...

BUG: Adding VTK_USE_CHARTS to VTKConfig.cmake.in. ENH: Adding a 2D graph layout example for context items.
parent 2f19d7fd
......@@ -145,6 +145,9 @@ IF(VTK_BINARY_DIR)
IF(VTK_USE_MFC)
ADD_DEPENDENCIES(VTKExamplesTarget vtkMFC)
ENDIF(VTK_USE_MFC)
IF(VTK_USE_CHARTS)
ADD_DEPENDENCIES(VTKExamplesTarget vtkCharts)
ENDIF(VTK_USE_CHARTS)
ELSE(VTK_BINARY_DIR)
# We are building outside the VTK tree. Build the examples directly.
......@@ -193,6 +196,10 @@ ELSE(VTK_BINARY_DIR)
SUBDIRS(GUI/Qt)
ENDIF(VTK_USE_QVTK)
IF(VTK_USE_CHARTS)
SUBDIRS(Charts/Cxx)
ENDIF(VTK_USE_CHARTS)
IF(WIN32)
SUBDIRS(GUI/Win32/SimpleCxx)
......
PROJECT (Charts)
FIND_PACKAGE(VTK REQUIRED)
IF(NOT VTK_USE_CHARTS)
MESSAGE(FATAL_ERROR "Example ${PROJECT_NAME} requires VTK_USE_CHARTS.")
ENDIF(NOT VTK_USE_CHARTS)
INCLUDE(${VTK_USE_FILE})
ADD_EXECUTABLE(GraphItem GraphItem.cxx vtkGraphItem.cxx)
TARGET_LINK_LIBRARIES(GraphItem vtkCharts)
/*=========================================================================
Program: Visualization Toolkit
Module: GraphItem.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkCommand.h"
#include "vtkRandomGraphSource.h"
#include "vtkGraph.h"
#include "vtkGraphItem.h"
#include "vtkVariant.h"
#include "vtkRenderer.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkContextView.h"
#include "vtkContextScene.h"
#include "vtkObjectFactory.h"
#include "vtkRegressionTestImage.h"
class GraphAnimate : public vtkCommand
{
public:
static GraphAnimate *New() { return new GraphAnimate(); }
vtkTypeRevisionMacro(GraphAnimate, vtkCommand);
virtual void Execute(vtkObject *caller, unsigned long eventId, void *callData)
{
this->GraphItem->UpdatePositions();
this->View->Render();
this->View->GetRenderWindow()->GetInteractor()->CreateOneShotTimer(10);
}
vtkGraphItem* GraphItem;
vtkContextView* View;
};
vtkCxxRevisionMacro(GraphAnimate, "1.1");
//----------------------------------------------------------------------------
int main( int argc, char * argv [] )
{
// Set up a 2D context view, context test object and add it to the scene
vtkSmartPointer<vtkContextView> view = vtkSmartPointer<vtkContextView>::New();
view->GetRenderer()->SetBackground(1.0, 1.0, 1.0);
view->GetRenderWindow()->SetSize(800, 600);
vtkSmartPointer<vtkRandomGraphSource> source = vtkSmartPointer<vtkRandomGraphSource>::New();
source->SetNumberOfVertices(100);
source->SetNumberOfEdges(0);
source->StartWithTreeOn();
source->Update();
vtkSmartPointer<vtkGraphItem> item = vtkSmartPointer<vtkGraphItem>::New();
item->SetGraph(source->GetOutput());
view->GetScene()->AddItem(item);
vtkSmartPointer<GraphAnimate> anim = vtkSmartPointer<GraphAnimate>::New();
anim->View = view;
anim->GraphItem = item;
view->GetRenderWindow()->GetInteractor()->Initialize();
view->GetRenderWindow()->GetInteractor()->CreateOneShotTimer(10);
view->GetRenderWindow()->GetInteractor()->AddObserver(vtkCommand::TimerEvent, anim);
view->GetRenderWindow()->GetInteractor()->Start();
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGraphItem.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkGraphItem.h"
#include "vtkEdgeListIterator.h"
#include "vtkOutEdgeIterator.h"
#include "vtkGraph.h"
#include "vtkMinimalStandardRandomSequence.h"
#include "vtkVariant.h"
#include "vtkContext2D.h"
#include "vtkContextScene.h"
#include "vtkPen.h"
#include "vtkBrush.h"
#include "vtkTextProperty.h"
#include "vtkTransform2D.h"
#include "vtkObjectFactory.h"
#include "vtkSmartPointer.h"
#include <vtkstd/utility>
#include <vtkstd/vector>
//-----------------------------------------------------------------------------
vtkCxxSetObjectMacro(vtkGraphItem, Graph, vtkGraph);
vtkCxxRevisionMacro(vtkGraphItem, "1.1");
vtkStandardNewMacro(vtkGraphItem);
class vtkGraphItem::Implementation
{
public:
Implementation()
{
Random = vtkSmartPointer<vtkMinimalStandardRandomSequence>::New();
}
void CheckPositionSize(vtkIdType i)
{
while (i >= this->Position.size())
{
int size[2] = {100, 100};
if (this->Item->GetScene())
{
this->Item->GetScene()->GetGeometry(size);
}
this->Random->Next();
float x = static_cast<int>(this->Random->GetValue()*size[0]);
this->Random->Next();
float y = static_cast<int>(this->Random->GetValue()*size[1]);
this->Position.push_back(vtkstd::make_pair(x, y));
}
}
void GetPosition(vtkIdType i, float x[2])
{
this->CheckPositionSize(i);
x[0] = this->Position[i].first;
x[1] = this->Position[i].second;
}
void SetPosition(vtkIdType i, float x[2])
{
this->CheckPositionSize(i);
this->Position[i] = vtkstd::make_pair(x[0], x[1]);
}
void CheckVelocitySize(vtkIdType i)
{
while (i >= this->Velocity.size())
{
this->Velocity.push_back(vtkstd::make_pair(0.0f, 0.0f));
}
}
void GetVelocity(vtkIdType i, float x[2])
{
this->CheckVelocitySize(i);
x[0] = this->Velocity[i].first;
x[1] = this->Velocity[i].second;
}
void SetVelocity(vtkIdType i, float x[2])
{
this->CheckVelocitySize(i);
this->Velocity[i] = vtkstd::make_pair(x[0], x[1]);
}
vtkSmartPointer<vtkMinimalStandardRandomSequence> Random;
vtkGraphItem* Item;
vtkstd::vector<vtkstd::pair<float, float> > Position;
vtkstd::vector<vtkstd::pair<float, float> > Velocity;
};
//-----------------------------------------------------------------------------
vtkGraphItem::vtkGraphItem()
{
this->Impl = new Implementation();
this->Impl->Item = this;
this->Graph = NULL;
this->MouseOver = false;
this->MouseButtonPressed = -1;
this->HitVertex = 0;
}
//-----------------------------------------------------------------------------
vtkGraphItem::~vtkGraphItem()
{
delete this->Impl;
this->SetGraph(0);
}
//-----------------------------------------------------------------------------
bool vtkGraphItem::Paint(vtkContext2D *painter)
{
painter->GetTextProp()->SetVerticalJustificationToCentered();
painter->GetTextProp()->SetJustificationToCentered();
painter->GetTextProp()->SetColor(0.0, 0.0, 0.0);
painter->GetTextProp()->SetFontSize(12);
painter->GetPen()->SetColorF(0.0f, 0.0f, 0.0f);
painter->GetBrush()->SetColorF(0.8f, 0.8f, 1.0f, 0.5f);
vtkSmartPointer<vtkEdgeListIterator> it = vtkSmartPointer<vtkEdgeListIterator>::New();
this->Graph->GetEdges(it);
float line[4] = {0.0f, 0.0f, 0.0f, 0.0f};
while (it->HasNext())
{
vtkEdgeType e = it->Next();
this->Impl->GetPosition(e.Source, line);
this->Impl->GetPosition(e.Target, line+2);
for (int i = 0; i < 4; ++i)
{
line[i] += 10.0f;
}
painter->DrawLine(line);
}
float dims[4] = {0.0f, 0.0f, 20.0f, 20.0f};
for (vtkIdType i = 0; i < this->Graph->GetNumberOfVertices(); ++i)
{
this->Impl->GetPosition(i, dims);
painter->DrawRect(dims[0], dims[1], dims[2], dims[3]);
float x = dims[0] + 0.5 * dims[2];
float y = dims[1] + 0.5 * dims[3];
painter->DrawString(x, y, vtkVariant(i).ToString());
}
return true;
}
//-----------------------------------------------------------------------------
bool vtkGraphItem::Hit(const vtkContextMouseEvent &mouse)
{
float pos[2] = {0.0f, 0.0f};
for (vtkIdType i = this->Graph->GetNumberOfVertices()-1; i >= 0; --i)
{
this->Impl->GetPosition(i, pos);
if (mouse.Pos[0] > pos[0] &&
mouse.Pos[0] < pos[0]+20.0f &&
mouse.Pos[1] > pos[1] &&
mouse.Pos[1] < pos[1]+20.0f)
{
this->HitVertex = i;
return true;
}
}
return false;
}
//-----------------------------------------------------------------------------
bool vtkGraphItem::MouseEnterEvent(const vtkContextMouseEvent &)
{
this->MouseOver = true;
return true;
}
//-----------------------------------------------------------------------------
bool vtkGraphItem::MouseMoveEvent(const vtkContextMouseEvent &mouse)
{
int deltaX = static_cast<int>(mouse.Pos[0] - this->LastPosition[0]);
int deltaY = static_cast<int>(mouse.Pos[1] - this->LastPosition[1]);
this->LastPosition[0] = mouse.Pos[0];
this->LastPosition[1] = mouse.Pos[1];
if (this->MouseButtonPressed == 0)
{
// Move the vertex by this amount
float pos[2];
this->Impl->GetPosition(this->HitVertex, pos);
pos[0] += deltaX;
pos[1] += deltaY;
this->Impl->SetPosition(this->HitVertex, pos);
return true;
}
#if 0
if (this->MouseButtonPressed == 1)
{
if (deltaX > 0.0)
{
if (!this->GetTransform())
{
vtkSmartPointer<vtkTransform2D> t = vtkSmartPointer<vtkTransform2D>::New();
t->Identity();
this->SetTransform(t);
}
this->GetTransform()->Scale(deltaX/50.0f, deltaX/50.0f);
}
return true;
}
#endif
return false;
}
//-----------------------------------------------------------------------------
bool vtkGraphItem::MouseLeaveEvent(const vtkContextMouseEvent &)
{
this->MouseOver = false;
return true;
}
//-----------------------------------------------------------------------------
bool vtkGraphItem::MouseButtonPressEvent(const vtkContextMouseEvent &mouse)
{
this->MouseButtonPressed = mouse.Button;
this->LastPosition[0] = mouse.Pos[0];
this->LastPosition[1] = mouse.Pos[1];
return true;
}
//-----------------------------------------------------------------------------
bool vtkGraphItem::MouseButtonReleaseEvent(const vtkContextMouseEvent &)
{
this->MouseButtonPressed = -1;
return true;
}
//-----------------------------------------------------------------------------
void vtkGraphItem::UpdatePositions()
{
vtkIdType numVerts = this->Graph->GetNumberOfVertices();
float restDistance = 40.0f;
float dampenLast = 0.5f;
float springConstant = 0.3f;
float repulseConstant = 1.0f;
//float restDistance = 40.0f;
//float dampenLast = 0.5f;
//float springConstant = 0.1f;
//float repulseConstant = 2.0f;
float epsilon = 0.0000001f;
float border = 20.0f;
vtkSmartPointer<vtkOutEdgeIterator> it = vtkSmartPointer<vtkOutEdgeIterator>::New();
float uPos[2];
float vPos[2];
float uVel[2];
int geom[2] = {100, 100};
if (this->GetScene())
{
this->GetScene()->GetGeometry(geom);
}
for (vtkIdType u = 0; u < numVerts; ++u)
{
if (this->MouseButtonPressed == 0 && u == this->HitVertex)
{
continue;
}
this->Impl->GetPosition(u, uPos);
float fx = 0.0;
float fy = 0.0;
for (vtkIdType v = 0; v < numVerts; ++v)
{
this->Impl->GetPosition(v, vPos);
float deltaX = uPos[0] - vPos[0];
float deltaY = uPos[1] - vPos[1];
float distSquared = deltaX*deltaX + deltaY*deltaY;
// Avoid divide by zero
distSquared += epsilon;
fx += repulseConstant * deltaX / distSquared;
fy += repulseConstant * deltaY / distSquared;
}
this->Graph->GetOutEdges(u, it);
while (it->HasNext())
{
vtkOutEdgeType e = it->Next();
vtkIdType v = e.Target;
if (u == v)
{
continue;
}
this->Impl->GetPosition(v, vPos);
float deltaX = uPos[0] - vPos[0];
float deltaY = uPos[1] - vPos[1];
float dist = sqrt(deltaX*deltaX + deltaY*deltaY);
float force = springConstant*(dist - restDistance);
fx -= force * deltaX / dist;
fy -= force * deltaY / dist;
}
float center[2] = {uPos[0] + 10.0f, uPos[1] + 10.0f};
// Change the force if it is near the edge
if (center[0] < border)
{
fx += -(center[0] - border);
}
else if (center[0] > geom[0] - border)
{
fx += -(center[0] - (geom[0] - border));
}
if (center[1] < border)
{
fy += -(center[1] - border);
}
else if (center[1] > geom[1] - border)
{
fy += -(center[1] - (geom[1] - border));
}
// Update velocity and position
this->Impl->GetVelocity(u, uVel);
uVel[0] = dampenLast*uVel[0] + fx;
uVel[1] = dampenLast*uVel[1] + fy;
uPos[0] += uVel[0];
uPos[1] += uVel[1];
this->Impl->SetPosition(u, uPos);
this->Impl->SetVelocity(u, uVel);
}
}
//-----------------------------------------------------------------------------
void vtkGraphItem::PrintSelf(ostream &os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
}
/*=========================================================================
Program: Visualization Toolkit
Module: vtkGraphItem.h
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkGraphItem - a vtkContextItem that draws a block (optional label).
//
// .SECTION Description
// This is a vtkContextItem that can be placed into a vtkContextScene. It draws
// a block of the given dimensions, and reacts to mouse events.
#ifndef __vtkGraphItem_h
#define __vtkGraphItem_h
#include "vtkContextItem.h"
class vtkContext2D;
class vtkGraph;
class VTK_CHARTS_EXPORT vtkGraphItem : public vtkContextItem
{
public:
vtkTypeRevisionMacro(vtkGraphItem, vtkContextItem);
virtual void PrintSelf(ostream &os, vtkIndent indent);
static vtkGraphItem *New();
vtkGetObjectMacro(Graph, vtkGraph);
virtual void SetGraph(vtkGraph* g);
// Description:
// Paint event for the item.
virtual bool Paint(vtkContext2D *painter);
//BTX
// Description:
// Returns true if the supplied x, y coordinate is inside the item.
virtual bool Hit(const vtkContextMouseEvent &mouse);
// Description:
// Mouse enter event.
virtual bool MouseEnterEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse move event.
virtual bool MouseMoveEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse leave event.
virtual bool MouseLeaveEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse button down event.
virtual bool MouseButtonPressEvent(const vtkContextMouseEvent &mouse);
// Description:
// Mouse button release event.
virtual bool MouseButtonReleaseEvent(const vtkContextMouseEvent &mouse);
//ETX
void UpdatePositions();
//BTX
protected:
vtkGraphItem();
~vtkGraphItem();
float LastPosition[2];
bool MouseOver;
int MouseButtonPressed;
vtkGraph* Graph;
vtkIdType HitVertex;
class Implementation;
Implementation* Impl;
private:
vtkGraphItem(const vtkGraphItem &); // Not implemented.
void operator=(const vtkGraphItem &); // Not implemented.
//ETX
};
#endif //__vtkGraphItem_h
......@@ -86,6 +86,7 @@ SET(VTK_USE_BOOST "@VTK_USE_BOOST@")
SET(VTK_USE_QT "@VTK_USE_QT@")
SET(VTK_USE_CARBON "@VTK_USE_CARBON@")
SET(VTK_USE_CG_SHADERS "@VTK_USE_CG_SHADERS@")
SET(VTK_USE_CHARTS "@VTK_USE_CHARTS@")
SET(VTK_USE_COCOA "@VTK_USE_COCOA@")
SET(VTK_USE_GEOVIS "@VTK_USE_GEOVIS@")
SET(VTK_USE_GL2PS "@VTK_USE_GL2PS@")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment