Configuring and compiling VTK 6.1 on Mac OS X 10.9.2 + simple example of VTK usage

Hello. In this short post I will explain how to build static libraries of VTK.
Main reason why it needs: Because official “How to” has just 2 lines without any explanations. And by default will be builded dynamic libraries. With which I had a problem like link to vtkCocoaGLView was not found, but it actually was in a library.

Let’s start. You will need CMake to build and VTK sources.

- Launch CMake from your Application folder. In main menu go to “Tool” -> “Install for Command Line Use”, in case if you did not install yet.
- Unpack VTK to some directory, working place.
- Enter to VTK directory and create directory with name build

Now you can configure and generate makefile or Xcode project file from console or User interface of CMake application.
If you would like to build with makefile in terminal go to next steps, if not jump to Build with Xcode section.
Continue reading

Simple way to implement multi-thread

I want show you simple way to create multi-thread calculations in few strings of code.

Simple collection of operations. But remember about blocks specifications and use proper storage type.

1
2
3
4
5
6
7
NSMutableArray *operations = [NSMutableArray array];
    for (...) {
        NSBlockOperation *operation = [NSBlockOperation blockOperationWithBlock:^{
           // calculation
        }];
        [operations addObject:operation];
    }

If you don’t like blocks, you can easily make a subclass of NSOperation and overwrite main function.

Now time to create queue for operations, so just create instance of NSOperationQueue.
And in maxConcurrentOperationCount property you need setup proper value. For example 2, if you want to use just 2 concurrent threads for your calculations and continue in normal state with others operations in another threads.
In this case waitUntilFinished should be NO.

But if you want to use max threads of CPU, setup NSOperationQueueDefaultMaxConcurrentOperationCount to maxConcurrentOperationCount and YES to waitUntilFinished. Like in example below:

1
2
3
4
5
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    [queue setMaxConcurrentOperationCount:....];
    [queue addOperations:operations waitUntilFinished:YES];
    [queue waitUntilAllOperationsAreFinished];
    [operations removeAllObjects];

If this is set, it will choose an appropriate value based on the number of available processors and other relevant factors.

1
2
3
enum {
  NSOperationQueueDefaultMaxConcurrentOperationCount = -1
};

NSOperationQueueDefaultMaxConcurrentOperationCount: The default maximum number of operations is determined dynamically by the NSOperationQueue object based on current system conditions.

That it! It’s so simple today.

You need to know something …

Objective-c
Timing for different conditions in 2147483647 (max int) iterations cycle:

1
2
3
4
5
6
7
8
9
2013-05-30 15:09:26.719 self[10228:707] object is not nil 3.580853 sec
2013-05-30 15:09:38.837 self[10228:707] object check bool 12.118355 sec
2013-05-30 15:09:51.900 self[10228:707] object check BOOL 13.061721 sec
2013-05-30 15:10:03.228 self[10228:707] object has object 11.327167 sec
2013-05-30 15:10:13.916 self[10228:707] object has object which is not nil 10.687253 sec
2013-05-30 15:10:31.121 self[10228:707] object has 3 level of objects 17.203815 sec
2013-05-30 15:10:51.921 self[10228:707] object isKindOfClass 20.798705 sec
2013-05-30 15:10:56.361 self[10228:707] perform block 4.439318 sec
2013-05-30 15:11:02.305 self[10228:707] block is available and perform then 5.943721 sec

So, isKindOfClass will slow down your app much as possible! ;)

Continue reading

How to extend existing method

With blocks it’s more easy if you need extend your method. But if you will need extend some method of another class, not yours, and you will not be able to get the sources then this solution for you. (And if you will not be able or does not have any reason for creating a subclass)

1. You need create a category of class

2. import runtime in implementation file (.m)

1
#import

3. implement your method inside category, for example :

1
2
3
4
5
6
7
8
9
- (void) extend_method {

// your code

//  here will be performed the original method
    [self extend_method];
   
// your code
}

It looks like this method has recursive calls itself, but it’s not true. Look next step

4. add method for replace (you can use +initialize or +load)

1
2
3
4
5
+ (void) initialize {
    Method original = class_getInstanceMethod(self, @selector(method));
    Method replacement = class_getInstanceMethod(self, @selector(extend_method));
    method_exchangeImplementations(original, replacement);
}

Done!

NSInvocation alternative for performSelector:

This code doesn’t involve compiler flags or direct runtime calls:

1
2
3
4
5
6
SEL selector = @selector(zeroArgumentMethod);
NSMethodSignature *methodSig = [[self class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:methodSig];
[invocation setSelector:selector];
[invocation setTarget:self];
[invocation invoke];

NSInvocation allows multiple arguments to be set so unlike performSelector this will work on any method.

And tip:

1
2
3
4
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
    [self.ticketTarget performSelector: self.ticketAction withObject: self];
#pragma clang diagnostic pop

Mountain Lion SVN

Good or not, but SVN moved to Xcode.app
If you want access like before in previous OS X, you should make a link to /usr/bin/svn.

1
sudo ln -s /Applications/Xcode.app/Contents/Developer/usr/bin/svn /usr/bin/svn

Decompose matrix, functions for GLKit

Иногда появляется необходимость развернуть объект из одного положения в другое. Для этого нужно извлечь угол из текущей матрицы и добавить дельта угол, в результате чего получим угол на который будет повернут объект. Итак, сперва необходимо получить размер по x,y,z и исходя из этих величин и матрицы, получить матрицу вращения из которой мы уже получим угол на который повернут наш объект.

Decompose Scale factor, matrix rotation and translate vector from modelViewMatrix

Translate vector:
vt = (M41, M42, M43)T

Getting scaling factors:
sx = sqrt(M112 + M122 + M132);
sy = sqrt(M212 + M222 + M232);
sz = sqrt(M312 + M322 + M332);

 

 

Now you can work backwards for the rotation matrix:

Mrot = M11/sx   M12/sx   M13/sx   0
M21/sy M22/sy M23/sy 0
M31/sz M32/sz M33/sz 0
0 0 0 1

Additional functions for iOS GLKit
Continue reading

1 2 3 6 Next »