tags:

views:

45

answers:

3

Hello guys: I have to project many points before drawing them on a frame. my codes are blow:

-(Coordination*)xyWorldToDev:(Coordination*)pt isIphoneYAxis:(BOOL)isIphoneYAxis{
    CGPoint tmpPoint=CGPointApplyAffineTransform(CGPointMake(pt.x,pt.y),worldToDevMatrix);
    Coordination *resultPoint=[[[Coordination alloc]initWithXY:tmpPoint.x withY:(isIphoneYAxis)?(sscy-tmpPoint.y):tmpPoint.y]autorelease];
    return resultPoint; }

-(Coordination*)xyDevTo3D:(Coordination*)cPt{
    double x=0.0,y=0.0;
    double divide=1+m_cView3DPara.v*cPt.y;
    x=(m_cView3DPara.a*cPt.x+m_cView3DPara.b*cPt.y+m_cView3DPara.e)/divide;
    y=(m_cView3DPara.d*cPt.y+m_cView3DPara.f)/divide;
    return [[[Coordination alloc]initWithXY:x withY:sscy-y]autorelease];
}
-(Coordination*)transformWorldTo3D:(Coordination*)pt{
    return [self xyDevTo3D:[self xyWorldToDev:pt isIphoneYAxis:NO]];
} 

Therefore,the method "-(Coordination*)transformWorldTo3D:(Coordination*)pt " is called hundreds times because projecting.

But i found it is very very SLOW while calling transformWorldTo3D! Is there another way to accelerate it? Or using another framework which could caculate the projecting value faster?

+2  A: 

Object allocations are expensive (relative to arithmetic operations); and it appears that you're doing 2 alloc-init-autorelease sequences for every point.

My first suggestion would be to try to do some of this work with CGPoints and avoid the allocations.

(Actually, that's my second suggestion: my first is to profile the code to see where the time is being spent.)

David Gelhar
i had used CGPoint and stored it into NSMutableArray via NSValue, but it has no significant difference. After testing the executing time, the bottleneck is CGPointApplyAffineTransform
catskytw
A: 

For Manny: these the code which calls transformWorldto3D

-(NSMutableArray*)getAllFeatureAfterTransform:(NSInteger)featureType{
    NSMutableArray *allFeatures=[NSMutableArray arrayWithObjects:nil];

    CGPoint worldLeftUp=worldRect.origin;
    CGPoint worldRightBottom=CGPointMake(worldRect.origin.x+worldRect.size.width,worldRect.origin.y+worldRect.size.height);
    NSMutableDictionary *dic=[idxCtrl getAllFeatureIdWithPoint1:worldLeftUp withPoint2:worldRightBottom];
    NSMutableArray *allValues=[NSMutableArray arrayWithArray:[dic allValues]];
    for(FeatureIdxObject *thisFeatureIdx in allValues){
        MapFeature *thisFeature=[self searchFeatureInCache:thisFeatureIdx.featureId withFeatureType:featureType];
        if(thisFeature==nil)
         thisFeature=[dataCtrl getFeatureObject:thisFeatureIdx.featureId withFeatureType:featureType];

        if(thisFeature!=nil && thisFeature.style==featureType){
            NSMutableDictionary *cacheDic;
            switch (featureType) {
                case FeatureTypePoint:
                    cacheDic=[FeatureCache currentPointFeatureCache];
                    break;
                case FeatureTypeLine:
                    cacheDic=[FeatureCache currentLineFeatureCache];
                    break;
                case FeatureTypeRegion:
                    cacheDic=[FeatureCache currentRegionFeatureCache];
                    break;
            }
            [cacheDic setObject:thisFeature forKey:[NSString stringWithFormat:@"%i", thisFeature.featureId]];
            [self transFormFeature:thisFeature];
            [allFeatures addObject:thisFeature];
        }
    }
    return allFeatures;
}

-(void)transFormFeature:(MapFeature*)inputFeature{
    switch (inputFeature.style) {
        case FeatureTypePoint:
            inputFeature.drawingArray=(flag3D)?
            [t transformPointsTo3D:inputFeature.points]:
            [t transformPointsToDev:inputFeature.points];
            break;
        case FeatureTypeLine:
            inputFeature.drawingArray=(flag3D)?
            [t transformPointsTo3D:[t getClippedPointsForLines:inputFeature.points withRect:worldRect]]:
            [t transformPointsToDev:[t getClippedPointsForLines:inputFeature.points withRect:worldRect]];
            break;
        case FeatureTypeRegion:
            inputFeature.drawingArray=(flag3D)?
            [t transformPointsTo3D:[t getClippedPointsForPolygon:inputFeature.points withRect:worldRect]]:
            [t transformPointsToDev:[t getClippedPointsForPolygon:inputFeature.points withRect:worldRect]];
            break;
    }
}

I call "getAllFeatureAfterTransform" by some featureType. As you see, i pick up many feature from data and transform each point in one feature(maybe hundreds points in a feature).

catskytw
A: 

follow-on development.

After estimating the cost for transformWorldTo3D, it needs about 0.12 sec for transforming 500 points(CGPoint) before drawing a frame. That means, even if drawing method spend NO time to draw, my FPS is only 8~10.

Any way to accelerate the transforming method?

ps. my testing platform is iphone3G.

catskytw