#include "stdafx.h"
#ifndef SHARED_HANDLERS
#include "VtkSDI.h"
#endif
#include "VtkSDIDoc.h"
#include "VtkSDIView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CVtkSDIView, CView)
BEGIN_MESSAGE_MAP(CVtkSDIView, CView)
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CVtkSDIView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_CREATE()
END_MESSAGE_MAP()
CVtkSDIView::CVtkSDIView()
{
this
->ren = vtkRenderer::New();
this
->renWin = vtkWin32OpenGLRenderWindow::New();
this
->iren = vtkWin32RenderWindowInteractor::New();
this
->sphere = vtkSphereSource::New();
this
->sphereElevation = vtkElevationFilter::New();
this
->sphereMapper = vtkPolyDataMapper::New();
this
->sphereActor = vtkActor::New();
this
->cone = vtkConeSource::New();
this
->glyph = vtkGlyph3D::New();
this
->spikeMapper = vtkPolyDataMapper::New();
this
->spikeActor = vtkActor::New();
this
->sphereAxis = vtkCubeAxesActor2D::New();
}
CVtkSDIView::~CVtkSDIView()
{
this
->ren->Delete();
this
->iren->Delete();
this
->renWin->Delete();
this
->sphere->Delete();
this
->sphereElevation->Delete();
this
->sphereMapper->Delete();
this
->sphereActor->Delete();
this
->cone->Delete();
this
->glyph->Delete();
this
->spikeMapper->Delete();
this
->spikeActor->Delete();
this
->sphereAxis->Delete();
}
BOOL
CVtkSDIView::PreCreateWindow(CREATESTRUCT& cs)
{
return
CView::PreCreateWindow(cs);
}
void
CVtkSDIView::OnDraw(CDC* pDC)
{
CVtkSDIDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if
(!pDoc)
return
;
if
( !
this
->iren->GetInitialized() )
{
CRect rect;
this
->GetClientRect(&rect);
this
->iren->Initialize();
this
->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top);
this
->ren->ResetCamera();
}
Pipeline();
if
( pDC->IsPrinting() )
{
this
->BeginWaitCursor();
int
cxPage = pDC->GetDeviceCaps(HORZRES);
int
cyPage = pDC->GetDeviceCaps(VERTRES);
int
*size =
this
->renWin->GetSize();
int
cxWindow = size[0];
int
cyWindow = size[1];
float
fx =
float
(cxPage) /
float
(cxWindow);
float
fy =
float
(cyPage) /
float
(cyWindow);
float
scale = min(fx,fy);
int
x =
int
(scale *
float
(cxWindow));
int
y =
int
(scale *
float
(cyWindow));
this
->renWin->SetupMemoryRendering(cxWindow, cyWindow, pDC->GetSafeHdc());
this
->renWin->Render();
HDC
memDC =
this
->renWin->GetMemoryDC();
StretchBlt(pDC->GetSafeHdc(),0,0,x,y,memDC,0,0,cxWindow,cyWindow,SRCCOPY);
this
->renWin->ResumeScreenRendering();
this
->EndWaitCursor();
}
else
{
this
->renWin->Render();
}
}
void
CVtkSDIView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(
this
);
#endif
}
BOOL
CVtkSDIView::OnPreparePrinting(CPrintInfo* pInfo)
{
return
DoPreparePrinting(pInfo);
}
void
CVtkSDIView::OnBeginPrinting(CDC*
, CPrintInfo*
)
{
}
void
CVtkSDIView::OnEndPrinting(CDC*
, CPrintInfo*
)
{
}
void
CVtkSDIView::OnRButtonUp(
UINT
, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(
this
, point);
}
void
CVtkSDIView::OnContextMenu(CWnd*
, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y,
this
, TRUE);
#endif
}
#ifdef _DEBUG
void
CVtkSDIView::AssertValid()
const
{
CView::AssertValid();
}
void
CVtkSDIView::Dump(CDumpContext& dc)
const
{
CView::Dump(dc);
}
CVtkSDIDoc* CVtkSDIView::GetDocument()
const
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CVtkSDIDoc)));
return
(CVtkSDIDoc*)m_pDocument;
}
#endif //_DEBUG
void
CVtkSDIView::OnSize(
UINT
nType,
int
cx,
int
cy)
{
CView::OnSize(nType, cx, cy);
CRect rect;
this
->GetClientRect(&rect);
this
->renWin->SetSize(rect.right-rect.left,rect.bottom-rect.top);
}
BOOL
CVtkSDIView::OnEraseBkgnd(CDC* pDC)
{
return
TRUE;
}
int
CVtkSDIView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if
(CView::OnCreate(lpCreateStruct) == -1)
return
-1;
this
->renWin->AddRenderer(
this
->ren);
this
->renWin->SetParentId(
this
->m_hWnd);
this
->iren->SetRenderWindow(
this
->renWin);
return
0;
}
LRESULT
CVtkSDIView::WindowProc(
UINT
message,
WPARAM
wParam,
LPARAM
lParam)
{
switch
(message)
{
case
WM_LBUTTONDOWN:
case
WM_LBUTTONUP:
case
WM_MBUTTONDOWN:
case
WM_MBUTTONUP:
case
WM_RBUTTONDOWN:
case
WM_RBUTTONUP:
case
WM_MOUSEMOVE:
case
WM_CHAR:
case
WM_TIMER:
if
(
this
->iren->GetInitialized())
{
return
vtkHandleMessage2(
this
->m_hWnd, message, wParam, lParam,
this
->iren);
}
break
;
}
return
CView::WindowProc(message, wParam, lParam);
}
void
CVtkSDIView::Pipeline()
{
this
->sphere->SetRadius(1);
this
->sphere->SetThetaResolution(18);
this
->sphere->SetPhiResolution(18);
this
->sphere->LatLongTessellationOn();
this
->sphereElevation->SetInput(
this
->sphere->GetOutput());
this
->sphereElevation->SetLowPoint(0,0,-1);
this
->sphereElevation->SetHighPoint(0,0,1);
this
->sphereElevation->SetScalarRange(-1,1);
this
->sphereMapper->SetInput(
this
->sphereElevation->GetPolyDataOutput());
this
->sphereMapper->SetColorModeToMapScalars();
this
->sphereMapper->SetScalarRange(-1,1);
this
->sphereActor->SetMapper(
this
->sphereMapper);
this
->ren->AddActor(
this
->sphereActor);
this
->cone->SetResolution(8);
this
->glyph->SetInput(
this
->sphere->GetOutput());
this
->glyph->SetSource(
this
->cone->GetOutput());
this
->glyph->SetVectorModeToUseNormal();
this
->glyph->SetScaleModeToScaleByVector();
this
->glyph->SetScaleFactor(0.1);
this
->spikeMapper->SetInput(
this
->glyph->GetOutput());
this
->spikeActor->SetMapper(
this
->spikeMapper);
this
->ren->AddActor(
this
->spikeActor);
this
->sphereAxis->SetInput(
this
->sphereElevation->GetOutput());
this
->sphereAxis->SetCamera(
this
->ren->GetActiveCamera());
this
->ren->AddActor(
this
->sphereAxis);
}