[vtkusers] here is a sample for vtkXYPlotActor but...
Yasin Alakese
y_alakese at hotmail.com
Mon Mar 31 17:02:16 EST 2003
Hi,
Here is a sample class for vtkXYPlotActor,
But there is a problem with it.... Whenever I want to change the values of y
axis and x axis in the runtime, the numbers glide to right or to up... Can
someone give me an information about how to avoid this behaviour?
Thank you.
Yasin Alakese
.H
//////////////////////////////////////////////////////////////////////
// VtkStripChart.h: interface for the CVtkStripChart class.
//
//////////////////////////////////////////////////////////////////////
#if
!defined(AFX_VTKSTRIPCHART_H__45272B35_A613_4AF2_8FB0_2C32D5890BFE__INCLUDED_)
#define AFX_VTKSTRIPCHART_H__45272B35_A613_4AF2_8FB0_2C32D5890BFE__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#include <vtkRenderer.h>
#include <vtkWin32OpenGLRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkXYPlotActor.h>
#include <vtkDataArray.h>
#include <vtkFieldData.h>
#include <vtkAxisActor2D.h>
#include <vtkProperty2D.h>
#include <math.h>
#include <map>
#define TUPLENUM 100
struct CVtkData;
struct CVtkRGBColor;
using namespace std;
class CVtkStripChart
{
public:
map <int, CVtkData*> m_stlCurveData;
map <int, CVtkRGBColor*> m_stlRGBColor;
public:
CVtkStripChart(CWnd* TartgetWnd);
virtual ~CVtkStripChart();
void SetTitleName(const CString &strTitleName);
void SetXTitleName(const CString &strTitleName);
void SetYTitleName(const CString &strTitleName);
void SetXRange(float dbRange);
void SetYRange(float yMin, float yMax);
void DisplayDescription(bool bDisplayX, bool bDisplayY);
void DisplayValues(bool bDisplayX, bool bDisplayY);
int AddCurve(COLORREF dwRGBColor);//COLORREF dwRGBColor, int iThickness);
//gibt Curve-ID zurück
void AddValues(int CurveID, double x, double y, DWORD dwCount);
void CreatePlotWindow();
double *ConvertRGBtoDouble(COLORREF dwRGBColor);
void SetBackgroundColor(COLORREF dwBackground);
void SetAxisColor(COLORREF dwAxisColor);
void ControlYValue(double y);
protected:
CWnd *m_pWindow;
float m_xRange, m_xRangeFlag;
float m_yRangeMin, m_yRangeMax;
BOOL m_displayXDesc, m_displayYDesc;
BOOL m_displayXnum, m_displayYnum;
CString m_titleName, m_xtitleName, m_ytitleName;
public:
int m_curveNum;
CStatic* m_cStatic;
vtkXYPlotActor *plot;
vtkRenderer *renderer;
vtkWin32OpenGLRenderWindow *renderWindow;
vtkRenderWindowInteractor *iren;
vtkAxisActor2D *xAxis, *yAxis;
};
struct CVtkData {
vtkDataArray *dataArray1;
vtkDataArray *dataArray2;
vtkFieldData *fieldData;
vtkDataObject *dataObject;
map <double, double> stlXYValues;
};
struct CVtkRGBColor {
float red;
float green;
float blue;
};
#endif //
!defined(AFX_VTKSTRIPCHART_H__45272B35_A613_4AF2_8FB0_2C32D5890BFE__INCLUDED_)
/*****/
.CPP
//////////////////////////////////////////////////////////////////////
// VtkStripChart.cpp: implementation of the CVtkStripChart class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "VtkStripChart.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CVtkStripChart::CVtkStripChart(CWnd* pTargetWnd)
{
plot = vtkXYPlotActor::New();
renderer = vtkRenderer::New();
renderWindow = vtkWin32OpenGLRenderWindow::New();
iren = vtkRenderWindowInteractor::New();
xAxis = vtkAxisActor2D::New();
yAxis = vtkAxisActor2D::New();
m_xRange = 10; //default x range
m_curveNum = -1;
m_pWindow = pTargetWnd;
renderWindow->SetParentId(m_pWindow->GetSafeHwnd());
renderWindow->WindowInitialize();
}
CVtkStripChart::~CVtkStripChart()
{
}
//Add plot properties
void CVtkStripChart::CreatePlotWindow()
{
if (!m_titleName.IsEmpty())
plot->SetTitle(m_titleName);
if (m_displayXDesc)
plot->SetXTitle(m_xtitleName);
else
plot->SetXTitle("");
if (m_displayYDesc)
plot->SetYTitle(m_ytitleName);
else
plot->SetYTitle("");
//to modify x,y texts
plot->ItalicOff();
xAxis = plot->GetXAxisActor2D();
yAxis = plot->GetYAxisActor2D();
xAxis->SetFontFactor(10);
yAxis->SetFontFactor(10);
CRect rect;
m_pWindow->GetClientRect(rect);
renderWindow->SetSize(rect.Width(), rect.Height());
plot->SetYRange(m_yRangeMin, m_yRangeMax);
plot->SetXRange(0.0, m_xRange);
m_xRangeFlag = m_xRange;
if (!m_displayXnum)
plot->SetNumberOfXLabels(2);
if (!m_displayYnum)
plot->SetNumberOfYLabels(2);
plot->SetXValuesToIndex();
plot->SetWidth(0.9);
plot->SetHeight(0.9);
plot->SetPosition(0.08, 0.08);
plot->SetDataObjectXComponent(0, 0);
plot->SetDataObjectYComponent(0, 1);
renderer->AddActor2D(plot);
renderWindow->AddRenderer(renderer);
}
//Add Curve
int CVtkStripChart::AddCurve(COLORREF dwRGBColor) //COLORREF dwRGBColor, int
iThickness)
{
double *tempColors = new double;
tempColors = ConvertRGBtoDouble(dwRGBColor);
CVtkData *piDummy = new CVtkData;
CVtkRGBColor *pcDummy = new CVtkRGBColor;
++m_curveNum;
piDummy->dataArray1 = vtkDataArray::CreateDataArray(VTK_FLOAT);
piDummy->dataArray2 = vtkDataArray::CreateDataArray(VTK_FLOAT);
piDummy->fieldData = vtkFieldData::New();
piDummy->dataObject = vtkDataObject::New();
piDummy->dataArray1->SetNumberOfTuples(TUPLENUM);
piDummy->dataArray2->SetNumberOfTuples(TUPLENUM);
piDummy->fieldData->AllocateArrays(2);
pcDummy->red = tempColors[0];
pcDummy->green = tempColors[1];
pcDummy->blue = tempColors[2];
m_stlCurveData[m_curveNum] = piDummy;
m_stlRGBColor[m_curveNum] = pcDummy;
return m_curveNum;
}
//Add values for any curve
void CVtkStripChart::AddValues(int CurveID, double x, double y, DWORD
dwCount)
{
if (y < m_yRangeMin || y > m_yRangeMax)
ControlYValue(y);
map <double, double> stlTempXYValues;
//saving (x, y) values
m_stlCurveData[CurveID]->stlXYValues.insert(pair<double, double> (x, y));
if (dwCount <= m_xRange) {
m_stlCurveData[CurveID]->dataArray1->InsertTuple(dwCount, &x);
m_stlCurveData[CurveID]->dataArray2->InsertTuple(dwCount, &y);
m_stlCurveData[CurveID]->fieldData->AddArray(m_stlCurveData[CurveID]->dataArray1);
m_stlCurveData[CurveID]->fieldData->AddArray(m_stlCurveData[CurveID]->dataArray2);
m_stlCurveData[CurveID]->dataObject->SetFieldData(m_stlCurveData[CurveID]->fieldData);
plot->AddDataObjectInput(m_stlCurveData[CurveID]->dataObject);
plot->SetPlotColor(CurveID, m_stlRGBColor[CurveID]->red,
m_stlRGBColor[CurveID]->green, m_stlRGBColor[CurveID]->blue);
plot->SetDataObjectXComponent(0, 0);
plot->SetDataObjectYComponent(CurveID, 1);
}
if (dwCount > m_xRange) {
//to let xAxis glide
plot->SetXRange(dwCount - m_xRangeFlag, dwCount);
++m_xRange;
for (float i = 0; i < m_xRange; ++i) {
//to animate plot we need to update current points within a xrange value
//the second (x,y) will be stlTempXYValues's first point, the 21th will
be the 20th
stlTempXYValues[i] = m_stlCurveData[CurveID]->stlXYValues[dwCount -
m_xRange + i];
}
for (i = 0; i < m_xRange; ++i) {
x = i;
m_stlCurveData[CurveID]->dataArray1->InsertTuple(i, &x);
m_stlCurveData[CurveID]->dataArray2->InsertTuple(i, &stlTempXYValues[i]);
}
m_stlCurveData[CurveID]->fieldData->AddArray(m_stlCurveData[CurveID]->dataArray1);
m_stlCurveData[CurveID]->fieldData->AddArray(m_stlCurveData[CurveID]->dataArray2);
m_stlCurveData[CurveID]->dataObject->SetFieldData(m_stlCurveData[CurveID]->fieldData);
plot->AddDataObjectInput(m_stlCurveData[CurveID]->dataObject);
}
}
void CVtkStripChart::SetTitleName(const CString &strTitleName)
{
m_titleName = strTitleName;
}
void CVtkStripChart::SetXTitleName(const CString &strTitleName)
{
m_xtitleName = strTitleName;
}
void CVtkStripChart::SetYTitleName(const CString &strTitleName)
{
m_ytitleName = strTitleName;
}
void CVtkStripChart::SetXRange(float dbRange)
{
m_xRange = dbRange;
}
void CVtkStripChart::SetYRange(float yMin, float yMax)
{
m_yRangeMin = yMin;
m_yRangeMax = yMax;
}
void CVtkStripChart::DisplayDescription(bool bDisplayX, bool bDisplayY)
{
m_displayXDesc = bDisplayX;
m_displayYDesc = bDisplayY;
}
void CVtkStripChart::DisplayValues(bool bDisplayX, bool bDisplayY)
{
m_displayXnum = bDisplayX;
m_displayYnum = bDisplayY;
}
double *CVtkStripChart::ConvertRGBtoDouble(COLORREF dwRGBColor)
{
double *tempColors = new double;
DWORD red, green, blue;
//red
red = dwRGBColor;
red = red << 24;
red = red >> 24;
//green
green = dwRGBColor;
green = green << 16;
green = green >> 24;
//blue
blue = dwRGBColor;
blue = blue << 8;
blue = blue >> 24;
//the values must be used between 0.0 - 1.0 in vtk
tempColors[0] = red/255.0;
tempColors[1] = green/255.0;
tempColors[2] = blue/255.0;
return tempColors;
}
void CVtkStripChart::SetBackgroundColor(COLORREF dwBackground)
{
double *tempColors = new double;
tempColors = ConvertRGBtoDouble(dwBackground);
renderer->SetBackground(tempColors[0], tempColors[1], tempColors[2]);
}
void CVtkStripChart::SetAxisColor(COLORREF dwAxisColor)
{
double *tempColors = new double;
tempColors = ConvertRGBtoDouble(dwAxisColor);
xAxis = plot->GetXAxisActor2D();
vtkProperty2D *temp = xAxis->GetProperty();
temp->SetColor(tempColors[0], tempColors[1], tempColors[2]);
}
void CVtkStripChart::ControlYValue(double y)
{
int temp, result;
y = abs(y);
temp = (int)y;
if (temp < 100) {
result = temp/10 + 1;
result *= 10;
}
if (temp >= 100 && temp < 1000) {
result = temp/100 + 1;
result *= 100;
}
if (temp >= 1000 && temp < 10000) {
result = temp/1000 + 1;
result *= 1000;
}
if (temp >= 10000) {
result = temp/10000 + 1;
result *= 10000;
}
m_yRangeMin = result*(-1);
m_yRangeMax = result;
plot->SetYRange(m_yRangeMin, m_yRangeMax);
}
/*****/
/*****/
here is how to use it....
---->
void xxx::OnOK() //if you press OK button
{
int curveID, curveID1, curveID2;
float x, y, y1, y2;
CStatic *pStatic = new CStatic;
//give a handle of a static control on a dialog window
pStatic = (CStatic *)GetDlgItem(IDC_STATIC);
CVtkStripChart plot(pStatic);
//the functions of the new class not vtkXYPlotActor
plot.SetXRange(20);
plot.SetYRange(-1.0, 1.0);
plot.SetXTitleName("t");
plot.SetYTitleName("y");
plot.DisplayDescription(TRUE, TRUE);
plot.DisplayValues(TRUE, TRUE);
curveID = plot.AddCurve(RGB(255, 0, 0));
plot.SetBackgroundColor(RGB(47, 77, 77));
plot.SetAxisColor(RGB(224, 213, 198));
plot.CreatePlotWindow();
srand(time(NULL));
for (int k = 0; k < 41; ++k) {
//give the values for x and y
x = k;
y = 2*k*sin(k);
plot.AddValues(curveID, x, y, k);
plot.renderWindow->Render();
::Sleep(100);
}
}
_________________________________________________________________
The new MSN 8: advanced junk mail protection and 2 months FREE*
http://join.msn.com/?page=features/junkmail
More information about the vtkusers
mailing list