forked from pixmeo/osirix
-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
vtkMieleView.mm
213 lines (173 loc) · 6.03 KB
/
vtkMieleView.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//
// ©Alex Bettarini -- all rights reserved
// License GPLv3.0 -- see License File
//
// At the end of 2014 the project was forked from OsiriX to become Miele-LXIV
// The original header follows:
/*=========================================================================
Program: OsiriX
Copyright (c) OsiriX Team
All rights reserved.
Distributed under GNU - LGPL
See http://www.osirix-viewer.com/copyright.html for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
=========================================================================*/
#include "options.h"
#import "mgl.h" // include first
#import "GLRenderer.h"
#define id Id
#include "vtkRenderer.h"
#include "vtkCocoaRenderWindow.h"
#include "vtkCocoaRenderWindowInteractor.h"
#include "vtkCommand.h"
#include "vtkCamera.h"
#include "vtkInteractorStyleTrackballCamera.h"
//#include "vtkOpenGLRenderer.h"
#undef id
#import "vtkMieleView.h"
#import "AppDefaults.h"
@implementation vtkMieleView
- (instancetype)initWithFrame:(NSRect)frame
{
if (self = [super initWithFrame:frame])
{
[self initializeVTKSupport];
}
return self;
}
-(void)dealloc
{
#ifndef NDEBUG
NSLog(@"vtkMieleView.mm:%d %@ dealloc %p", __LINE__, NSStringFromClass([self class]), self);
#endif
[self cleanUpVTKSupport];
[super dealloc];
}
// We are going to over ride the super class here to do some last minute
// setups. We need to do this because if we initialize in the constructor or
// even later, in say an NSDocument's windowControllerDidLoadNib, then
// we will get a warning about "Invalid Drawable" because the OpenGL Context
// is trying to be set and rendered into an NSView that most likely is not
// on screen yet. This is a way to defer that initialization until the NSWindow
// that contains our NSView subclass is actually on screen and ready to be drawn.
- (void)drawRect:(NSRect)theRect
{
// Check for a valid vtkWindowInteractor and then initialize it. Technically we
// do not need to do this, but what happens is that the window that contains
// this object will not immediately render it so you end up with a big empty
// space in your gui where this NSView subclass should be. This may or may
// not be what is wanted. If you allow this code then what you end up with is the
// typical empty black OpenGL view which seems more 'correct' or at least is
// more soothing to the eye.
vtkRenderWindowInteractor* theRenWinInt = [self getInteractor];
if (theRenWinInt && (theRenWinInt->GetInitialized() == NO))
{
theRenWinInt->Initialize();
}
// Let the vtkCocoaGLView do its regular drawing
[super drawRect:theRect];
}
- (void)initializeVTKSupport
{
#if 1
NSLog(@"%s %d, self:%p", __FUNCTION__, __LINE__, self);
checkOGLVersion();
#endif
// The usual VTK object creation
vtkRenderer *ren = vtkRenderer::New();
vtkRenderWindow *renWin = vtkRenderWindow::New();
#if 1
//renWin->InitializeFromCurrentContext();
NSLog(@"%s %d, self:%p", __FUNCTION__, __LINE__, self);
checkOGLVersion();
#endif
vtkRenderWindowInteractor* renWinInt = vtkRenderWindowInteractor::New();
vtkInteractorStyleTrackballCamera *interactorStyle = vtkInteractorStyleTrackballCamera::New();
renWinInt->SetInteractorStyle( interactorStyle );
interactorStyle->Delete();
// The cast should never fail, but we do it anyway, as
// it's more correct to do so.
_cocoaRenderWindow = vtkCocoaRenderWindow::SafeDownCast(renWin);
if (ren && _cocoaRenderWindow && renWinInt)
{
// This is special to our usage of vtk. To prevent vtk
// from creating an NSWindow and NSView automatically (its
// default behaviour) we tell vtk that they exist already.
// The APIs names are a bit misleading, due to the cross
// platform nature of vtk, but this usage is correct.
_cocoaRenderWindow->SetRootWindow([self window]);
_cocoaRenderWindow->SetWindowId(self);
// The usual VTK connections
_cocoaRenderWindow->AddRenderer(ren);
renWinInt->SetRenderWindow(_cocoaRenderWindow);
// This is special to our usage of VTK. vtkCocoaGLView
// keeps track of the renderWindow, and has a get
// accessor if you ever need it.
[self setVTKRenderWindow:_cocoaRenderWindow];
// Likewise, BasicVTKView keeps track of the renderer
[self setRenderer:ren];
}
}
- (void)cleanUpVTKSupport
{
vtkRenderer* ren = [self renderer];
vtkRenderWindow* renWin = [self getVTKRenderWindow];
vtkRenderWindowInteractor* renWinInt = [self getInteractor];
if (ren)
ren->Delete();
if (renWin)
renWin->Delete();
if (renWinInt)
renWinInt->Delete();
[self setRenderer:NULL];
[self setVTKRenderWindow:NULL];
// There is no setter accessor for the render window
// interactor, that's ok.
}
- (void) prepareForRelease // VTK memory leak
{
_cocoaRenderWindow->SetWindowId( nil);
_cocoaRenderWindow->SetRootWindow(nil);
}
- (BOOL)mouseDownCanMoveWindow
{
return NO;
}
// Return result in MB
+ (unsigned long) VRAMSizeForDisplayID: (CGDirectDisplayID) displayID
{
return [AppDefaults GPUModelVRAMInfo];
}
- (void) keyDown:(NSEvent *)event
{
if ([[event characters] length] == 0)
return;
unichar c = [[event characters] characterAtIndex:0];
if (c != 'q' && c!= 'e') // Don't forward to super if Q or E key pressed: DDP (051112,051128)
[super keyDown: event];
}
-(vtkRenderer *)renderer {
return _renderer;
}
- (void)setRenderer:(vtkRenderer*)theRenderer
{
_renderer = theRenderer;
}
-(vtkCocoaRenderWindow *) cocoaWindow
{
return _cocoaRenderWindow;
}
-(vtkRenderWindow *)renderWindow {
return [self getVTKRenderWindow];
}
-(void)removeAllActors
{
vtkRenderer *renderer = [self renderer];
if (!renderer)
return;
vtkActorCollection *coll = renderer->GetActors();
coll->RemoveAllItems();
}
@end