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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
#import "MyWindowController.h"
#import "BasicVTKView.h"
#import "CustomView.h"
#import "vtkCocoaRenderWindowInteractor.h"
#import "vtkConeSource.h"
#import "vtkCylinderSource.h"
#import "vtkInteractorStyleSwitch.h"
#import "vtkPolyDataMapper.h"
#import "vtkSmartPointer.h"
#import "vtkTextActor.h"
#import "vtkTextProperty.h"
// Private Interface
@interface MyWindowController ()
@property (readwrite, weak, nonatomic) IBOutlet BasicVTKView* leftView;
@property (readwrite, weak, nonatomic) IBOutlet BasicVTKView* middleView;
@property (readwrite, weak, nonatomic) IBOutlet CustomView* rightView;
@end
@implementation MyWindowController
// ----------------------------------------------------------------------------
// Private helper method to get the path to the system font appropriate for
// the given font and font size.
+ (nullable NSURL*)fontPathForString:(nullable NSString*)inString size:(CGFloat)inSize
{
NSURL* fontUrl = nil;
if (inString)
{
NSFont* startFont = [NSFont systemFontOfSize:inSize];
CTFontRef font = CTFontCreateForString((__bridge CTFontRef)startFont,
(__bridge CFStringRef)inString, CFRangeMake(0, [inString length]));
if (font)
{
NSFontDescriptor* fontDesc = [(__bridge NSFont*)font fontDescriptor];
fontUrl = [fontDesc objectForKey:(__bridge NSString*)kCTFontURLAttribute];
CFRelease(font);
}
}
return fontUrl;
}
// ----------------------------------------------------------------------------
- (void)setupLeftView
{
BasicVTKView* thisView = [self leftView];
// Explicitly enable HiDPI/Retina (this is the default anyway).
[thisView setWantsBestResolutionOpenGLSurface:YES];
[thisView initializeVTKSupport];
// 'smart pointers' are used because they are very similar to reference counting in Cocoa.
// Personal Taste Section. I like to use a trackball interactor
vtkSmartPointer<vtkInteractorStyleSwitch> intStyle =
vtkSmartPointer<vtkInteractorStyleSwitch>::New();
intStyle->SetCurrentStyleToTrackballCamera();
[thisView getInteractor] -> SetInteractorStyle(intStyle);
// Create a cone, see the "VTK User's Guide" for details
vtkSmartPointer<vtkConeSource> cone = vtkSmartPointer<vtkConeSource>::New();
cone->SetHeight(3.0);
cone->SetRadius(1.0);
cone->SetResolution(100);
vtkSmartPointer<vtkPolyDataMapper> coneMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
coneMapper->SetInputConnection(cone->GetOutputPort());
vtkSmartPointer<vtkActor> coneActor = vtkSmartPointer<vtkActor>::New();
coneActor->SetMapper(coneMapper);
[thisView getRenderer] -> AddActor(coneActor);
// Create a text actor.
NSString* string = @"日本語";
int fontSize = 30;
vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New();
textActor->SetPickable(false);
textActor->SetInput([string UTF8String]);
vtkTextProperty* properties = textActor->GetTextProperty();
NSURL* fontURL = [[self class] fontPathForString:string size:fontSize];
properties->SetFontFile([fontURL fileSystemRepresentation]);
properties->SetFontFamily(VTK_FONT_FILE);
properties->SetFontSize(fontSize);
vtkCoordinate* coord = textActor->GetPositionCoordinate();
coord->SetCoordinateSystemToWorld();
coord->SetValue(0.0, 0.5, 0.0);
[thisView getRenderer] -> AddViewProp(textActor);
// Tell the system that the view needs to be redrawn
[thisView setNeedsDisplay:YES];
}
// ----------------------------------------------------------------------------
- (void)setupMiddleView
{
BasicVTKView* thisView = [self middleView];
// Explicitly disable HiDPI/Retina as a demonstration of the difference.
// One might want to disable it to reduce memory usage / increase performance.
[thisView setWantsBestResolutionOpenGLSurface:NO];
[thisView initializeVTKSupport];
// 'smart pointers' are used because they are very similar to reference counting in Cocoa.
// Personal Taste Section. I like to use a trackball interactor
vtkSmartPointer<vtkInteractorStyleSwitch> intStyle =
vtkSmartPointer<vtkInteractorStyleSwitch>::New();
intStyle->SetCurrentStyleToTrackballCamera();
[thisView getInteractor] -> SetInteractorStyle(intStyle);
// Create a cylinder, see the "VTK User's Guide" for details
vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetResolution(100);
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
[thisView getRenderer] -> AddActor(cylinderActor);
// Create a text actor.
NSString* string = @"日本語";
int fontSize = 30;
vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New();
textActor->SetPickable(false);
textActor->SetInput([string UTF8String]);
vtkTextProperty* properties = textActor->GetTextProperty();
NSURL* fontURL = [[self class] fontPathForString:string size:fontSize];
properties->SetFontFile([fontURL fileSystemRepresentation]);
properties->SetFontFamily(VTK_FONT_FILE);
properties->SetFontSize(fontSize);
vtkCoordinate* coord = textActor->GetPositionCoordinate();
coord->SetCoordinateSystemToWorld();
coord->SetValue(0.3, 0.5, 0.0);
[thisView getRenderer] -> AddViewProp(textActor);
// Tell the system that the view needs to be redrawn
[thisView setNeedsDisplay:YES];
}
// ----------------------------------------------------------------------------
- (void)setupRightView
{
CustomView* thisView = [self rightView];
// Explicitly enable HiDPI/Retina (this is required when using CAOpenGLLayer, otherwise the view
// will be 1/4 size on Retina).
[thisView setWantsBestResolutionOpenGLSurface:YES];
[thisView initializeVTKSupport];
[thisView initializeLayerSupport];
// 'smart pointers' are used because they are very similar to reference counting in Cocoa.
// Personal Taste Section. I like to use a trackball interactor
vtkSmartPointer<vtkInteractorStyleSwitch> intStyle =
vtkSmartPointer<vtkInteractorStyleSwitch>::New();
intStyle->SetCurrentStyleToTrackballCamera();
[thisView renderWindowInteractor] -> SetInteractorStyle(intStyle);
// Create a cylinder, see the "VTK User's Guide" for details
vtkSmartPointer<vtkCylinderSource> cylinder = vtkSmartPointer<vtkCylinderSource>::New();
cylinder->SetResolution(100);
vtkSmartPointer<vtkPolyDataMapper> cylinderMapper = vtkSmartPointer<vtkPolyDataMapper>::New();
cylinderMapper->SetInputConnection(cylinder->GetOutputPort());
vtkSmartPointer<vtkActor> cylinderActor = vtkSmartPointer<vtkActor>::New();
cylinderActor->SetMapper(cylinderMapper);
[thisView renderer] -> AddActor(cylinderActor);
// Create a text actor.
NSString* string = @"日本語";
int fontSize = 30;
vtkSmartPointer<vtkTextActor> textActor = vtkSmartPointer<vtkTextActor>::New();
textActor->SetPickable(false);
textActor->SetInput([string UTF8String]);
vtkTextProperty* properties = textActor->GetTextProperty();
NSURL* fontURL = [[self class] fontPathForString:string size:fontSize];
properties->SetFontFile([fontURL fileSystemRepresentation]);
properties->SetFontFamily(VTK_FONT_FILE);
properties->SetFontSize(fontSize);
vtkCoordinate* coord = textActor->GetPositionCoordinate();
coord->SetCoordinateSystemToWorld();
coord->SetValue(0.3, 0.5, 0.0);
[thisView renderer] -> AddViewProp(textActor);
// Tell the system that the view needs to be redrawn
[thisView setNeedsDisplay:YES];
}
#pragma mark -
// ----------------------------------------------------------------------------
// Designated initializer
- (instancetype)init
{
// Call the superclass' designated initializer, giving it the name of the nib file.
self = [super initWithWindowNibName:@"MyWindow"];
return self;
}
// ----------------------------------------------------------------------------
// Called once when the window is loaded.
- (void)windowDidLoad
{
[self setupLeftView];
[self setupMiddleView];
[self setupRightView];
}
// ----------------------------------------------------------------------------
// Called once when the window is closed.
- (void)windowWillClose:(NSNotification*)inNotification
{
// Releases memory allocated in initializeVTKSupport.
[[self leftView] cleanUpVTKSupport];
[[self middleView] cleanUpVTKSupport];
[[self rightView] cleanUpVTKSupport];
}
#pragma mark -
// ----------------------------------------------------------------------------
- (IBAction)handleLeftButton:(id)sender
{
// Do anything you want with the view.
NSBeep();
// Here we just clean it up and remove it.
[[self leftView] cleanUpVTKSupport];
[[self leftView] removeFromSuperview];
}
// ----------------------------------------------------------------------------
- (IBAction)handleMiddleButton:(id)sender
{
// Do anything you want with the view.
NSBeep();
// Here we just clean it up and remove it.
[[self middleView] cleanUpVTKSupport];
[[self middleView] removeFromSuperview];
}
// ----------------------------------------------------------------------------
- (IBAction)handleRightButton:(id)sender
{
// Do anything you want with the view.
NSBeep();
// Here we just clean it up and remove it.
[[self rightView] cleanUpVTKSupport];
[[self rightView] removeFromSuperview];
}
@end
|