๋ฐ˜์‘ํ˜•

VTK SDK์—๋Š” ๋ณดํ†ต 3D ์‹œ๊ฐํ™” ๋„๊ตฌ๋ผ๊ณ ๋งŒ ์•Œ๊ณ  ๊ณ„์‹ค ๊ฒƒ ๊ฐ™์€๋ฐ์š”.

์‚ฌ์‹ค VTK SDK๋Š” 2D, 3D์— ๊ด€๋ จ๋œ ๋งŽ์€ ๊ธฐ๋Šฅ๋“ค์„ ์ง€์›ํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค.


์•„๋ž˜ ๋งํฌ๋Š” VTK์—์„œ ์ œ๊ณตํ•˜๋Š” ๊ณต์‹ ์˜ˆ์ œ ์‚ฌ์ดํŠธ์ž…๋‹ˆ๋‹ค.

๋งํฌ: https://lorensen.github.io/VTKExamples/site/Cxx


์˜ค๋Š˜ ๋ณด์—ฌ๋“œ๋ฆด ์˜ˆ์ œ๋Š” ํ‰์†Œ์— ์‚ฌ์šฉ๋  ๋งŒํ•œ ๋„๊ตฌ ์ค‘ ํ•˜๋‚˜์ธ '์ฐจํŠธ'์ž…๋‹ˆ๋‹ค.

<MFC BarChart ์˜ˆ์ œ>


๋จผ์ € ์˜ˆ์ œ๋Š” ์•„๋ž˜ ๋งํฌ์—์„œ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

๋งํฌ: https://lorensen.github.io/VTKExamples/site/Cxx/Plotting/BarChart/


์‹คํ–‰ ๊ฒฐ๊ณผ๋Š” ์œ„ ๊ทธ๋ฆผ๊ณผ ๊ฐ™์€๋ฐ์š”.

๋งˆ์šฐ์Šค ์ด๋ฒคํŠธ๊ฐ€ ๋“ฑ๋ก๋˜์–ด ์žˆ์–ด ์ด๋™, ํ™•๋Œ€/์ถ•์†Œ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋ฉฐ,
์‚ฌ์šฉ์ž ์ž…๋ง›์— ๋งž๊ฒŒ ๊ธ€์ž ํฌ๊ธฐ, ์ƒ‰์ƒ, Tick ๊ฐ„๊ฒฉ ๋“ฑ ๋ชจ๋“  ๊ฒƒ์„ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

MFC UI์—๋Š” Chart๋ผ๋Š” ๊ฒƒ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์‚ฌ์šฉ์ž๊ฐ€ ์ง์ ‘ ๋งŒ๋“ค์–ด์•ผ ํ•˜๋Š”๋ฐ์š”.

์ด๋Ÿฐ ๊ธฐ๋Šฅ์„ ๊ฐ€์ ธ๋‹ค ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด, ๊ตณ์ด ๋งŒ๋“ค์ง€ ์•Š์•„๋„ ๋˜๊ฒ ์ฃ ?


์ž, ๋ณธ๊ฒฉ์ ์œผ๋กœ ์˜ˆ์ œ ๋งŒ๋“ค๊ธฐ๋ฅผ ์‹ค์Šตํ•ด ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.
(ํ•ต์‹ฌ ๋ถ€๋ถ„๋งŒ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ์ž์„ธํ•œ ๋‚ด์šฉ์€ ๋ณธ๋ฌธ ํ•˜๋‹จ์˜ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.)

๋จผ์ € Dialog ๊ธฐ๋ฐ˜์œผ๋กœ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.


๊ทธ๋ฆฌ๊ณ  MFC์—์„œ VTK ์‚ฌ์šฉ์„ ์œ„ํ•ด ์•„๋ž˜ ๋งํฌ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.


1. VTK ์ดˆ๊ธฐํ™”

Main Dialog header ํŒŒ์ผ์— ์•„๋ž˜์˜ ์ดˆ๊ธฐํ™” ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

#include <vtkAutoInit.h>

#define vtkRenderingCore_AUTOINIT 3(vtkRenderingOpenGL2,vtkInteractionStyle, vtkRenderingFreeType)
#define vtkRenderingContext2D_AUTOINIT 1(vtkRenderingContextOpenGL2)

#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkSmartPointer.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkInteractorStyleTrackballCamera.h>
#include <vtkVersion.h>
#include <vtkChartXY.h>
#include <vtkPlot.h>
#include <vtkAxis.h>
#include <vtkTable.h>
#include <vtkTextProperty.h>
#include <vtkIntArray.h>
#include <vtkContextView.h>
#include <vtkContextScene.h>
#include <vtkColorSeries.h>
#include <vtkNamedColors.h>
#include <vtkColor.h>


2. Picture Control์— ๊ทธ๋ ค์งˆ vtkRenderWindow๋ฅผ ์ดˆ๊ธฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

<Header File>

<Source File>

void CExampleBarChartDlg::InitializeVTKWindow(void* hWnd)
{
	vtkNew<vtkRenderWindowInteractor> interactor;
	
	interactor->SetInteractorStyle(vtkSmartPointer<vtkInteractorStyleTrackballCamera>::New());

	vtkNew<vtkRenderer> renderer;
	renderer->SetBackground(0.1, 0.2, 0.3);

	m_vtkRenderWindow->SetParentId(hWnd);
	m_vtkRenderWindow->SetInteractor(interactor);
	m_vtkRenderWindow->AddRenderer(renderer);
	m_vtkRenderWindow->Render();
}

void CExampleBarChartDlg::ResizeVTKWindow()
{
	CRect rc;
	GetDlgItem(IDC_PC_CHART)->GetClientRect(rc);
	m_vtkRenderWindow->SetSize(rc.Width(), rc.Height());
}


3. Button Event์— Chart data๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ฝ”๋“œ๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

void CExampleBarChartDlg::OnBnClickedBtnInitchart()
{
	// Colors
	vtkNew<vtkColorSeries> colorSeries;
	colorSeries->SetColorScheme(vtkColorSeries::BREWER_SEQUENTIAL_YELLOW_ORANGE_BROWN_3);
	colorSeries->SetColorScheme(vtkColorSeries::BREWER_DIVERGING_SPECTRAL_3);
	colorSeries->SetColorScheme(vtkColorSeries::BREWER_DIVERGING_BROWN_BLUE_GREEN_3);
	colorSeries->SetColorScheme(vtkColorSeries::BREWER_SEQUENTIAL_BLUE_GREEN_3);
	colorSeries->SetColorScheme(vtkColorSeries::BREWER_SEQUENTIAL_BLUE_PURPLE_3);
	colorSeries->SetColorScheme(vtkColorSeries::BREWER_DIVERGING_PURPLE_ORANGE_3);

	vtkNew<vtkNamedColors> colors;
	vtkColor3d backgroundColor = colors->GetColor3d("Sienna");
	vtkColor3d axisColor = colors->GetColor3d("Wheat");
	vtkColor3d titleColor = colors->GetColor3d("Wheat");

	// Set up a 2D scene, add an XY chart to it
	vtkNew<vtkContextView>  view;
	view->GetRenderer()->SetBackground(backgroundColor.GetData());
	view->GetRenderWindow()->SetSize(640, 480);
	
	// ์ถ”๊ฐ€
	view->SetRenderWindow(m_vtkRenderWindow);

	vtkNew<vtkChartXY>  chart;
	view->GetScene()->AddItem(chart);

	// Set various properties
	vtkAxis *xAxis = chart->GetAxis(vtkAxis::BOTTOM);
	xAxis->SetTitle("Monthly");
	xAxis->GetTitleProperties()->SetColor(axisColor.GetData());
	xAxis->GetTitleProperties()->SetFontSize(16);
	xAxis->GetTitleProperties()->ItalicOn();
	xAxis->GetLabelProperties()->SetColor(axisColor.GetData());

	vtkAxis *yAxis = chart->GetAxis(vtkAxis::LEFT);
	yAxis->SetTitle("Circulation");
	yAxis->GetTitleProperties()->SetColor(axisColor.GetData());
	yAxis->GetTitleProperties()->SetFontSize(16);
	yAxis->GetTitleProperties()->ItalicOn();
	yAxis->GetLabelProperties()->SetColor(axisColor.GetData());

	chart->SetTitle("Circulation 2008, 2009, 2010");
	chart->GetTitleProperties()->SetFontSize(24);
	chart->GetTitleProperties()->SetColor(titleColor.GetData());
	chart->GetTitleProperties()->BoldOn();

	// Create a table with some points in it...
	vtkNew<vtkTable>  table;

	vtkNew<vtkIntArray>  arrMonth;
	arrMonth->SetName("Month");
	table->AddColumn(arrMonth);

	vtkNew<vtkIntArray>  arr2008;
	arr2008->SetName("2008");
	table->AddColumn(arr2008);

	vtkNew<vtkIntArray>  arr2009;
	arr2009->SetName("2009");
	table->AddColumn(arr2009);

	vtkNew<vtkIntArray>  arr2010;
	arr2010->SetName("2010");
	table->AddColumn(arr2010);

	table->SetNumberOfRows(12);
	for (int i = 0; i < 12; i++)
	{
		table->SetValue(i, 0, i + 1);
		table->SetValue(i, 1, data_2008[i]);
		table->SetValue(i, 2, data_2009[i]);
		table->SetValue(i, 3, data_2010[i]);
	}

	// Add multiple line plots, setting the colors etc
	vtkPlot *line = 0;
	line = chart->AddPlot(vtkChart::BAR);
	line->SetColor(colorSeries->GetColor(0).GetRed() / 255.0,
		colorSeries->GetColor(0).GetGreen() / 255.0,
		colorSeries->GetColor(0).GetBlue() / 255.0);
	line->SetInputData(table, 0, 1);

	line = chart->AddPlot(vtkChart::BAR);
	line->SetColor(colorSeries->GetColor(1).GetRed() / 255.0,
		colorSeries->GetColor(1).GetGreen() / 255.0,
		colorSeries->GetColor(1).GetBlue() / 255.0);
	line->SetInputData(table, 0, 2);

	line = chart->AddPlot(vtkChart::BAR);
	line->SetColor(colorSeries->GetColor(2).GetRed() / 255.0,
		colorSeries->GetColor(2).GetGreen() / 255.0,
		colorSeries->GetColor(2).GetBlue() / 255.0);
	line->SetInputData(table, 0, 3);

	//Finally render the scene and compare the image to a reference image
	view->GetRenderWindow()->SetMultiSamples(0);
	view->GetRenderWindow()->Render();
	view->GetInteractor()->Initialize();
	//view->GetInteractor()->Start();
}


๊ธฐ๋ณธ C++ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ MFC Dialog ํ”„๋กœ์ ํŠธ๋กœ ๋ณ€๊ฒฝํ•œ ๊ฒƒ ๋ฐ–์— ์—†์œผ๋‹ˆ ๊ฐ„๋‹จํ•˜์ฃ ?

์ž์„ธํ•œ ๋‚ด์šฉ์€ ์ฒจ๋ถ€๋œ ์ฝ”๋“œ๋ฅผ ์ฐธ๊ณ ํ•ด์ฃผ์„ธ์š”.

ExampleBarChart.zip

๋ฐ˜์‘ํ˜•