Objc classes and math lib

Discussion in 'Mac Programming' started by MrRage, Apr 30, 2009.

  1. MrRage macrumors member

    Joined:
    Jun 14, 2008
    #1
    I'm working on a ray tracer and I wanted to use ObjC with garbage collection because of its ease of use and flexibility.

    I thought about using structures for this libraries primitives, but I think I'm better off using ObjC classes and if the function is time consuming - just use c functions to access the ivar directly instead of using a property. So really in some cases I might just be using Objc class as a simple container to go into an NSArray.

    For example:
    Code:
    @interface Vertex { 
    @public
        float x,y,z; 
    } 
    @property (assign) x;
    @property (assign) y;
    @property (assign) z;
    - (void)zero;
    @end
    
    @implementation 
    @synthesize x,y,z;
    - (void)zero {
        x = 0;
        y = 0;
        z = 0;
    }
    @end
    
    void zeroVertex(Vertex* v) {
        v->x = 0;
        v->y = 0;
        v->z = 0;
    }
    
    Both the method and function below will do the same thing, but the call time on the global function is shorter. When the vertex goes out of scope the garbage collector deals with it.

    Code:
    Vertex* vertex = [Vertex new];
    [vertex zero];
    zeroVertex(vertex);
    
    vertex = nil; 
    
    The other idea was just to use structs tagged as __strong when they were children of other structs.
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    IMO, if you're going to use Obj-C classes, use methods. For accessing ivars, either make them @public and access them directly, or keep them @private and use accessors. I wouldn't mix the two. But really I would not worry about speed at first until you have it working and want to improve performance, and know that these changes will be faster. Plus, GC will be slower anyways, so I'd start off without it.
     
  3. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #3
    Hi

    I have a suggestion - If your ray tracer will be constantly creating/deleting vertecies then why not dump Objective-C memory management in favour of using double linked listd for the verticies.

    Creating and deleting a vertex would be as simple as linking/unlinking a node from a list - much quicker than going through memory management functions. In C++ you would mark your vertex new and delete methods as inline which would make them as fast as they could be - I'm not sure you can do the same thing in Objective-C. If not you could write some macros to do the same sort of thing.

    b e n
     

Share This Page