Data Type woes in plain C

Post Reply
cheshirekow
Posts: 6
Joined: Wed Mar 26, 2008 8:19 pm

Data Type woes in plain C

Post by cheshirekow »

I've been trying to work out some sample code in plain C using the baseline SDK and everything seems to be working fine as far as the COM stuff goes but in trying to retrieve objects the data seems to be a little screwy. get_X always returns 0, get_Y and get_Area are returning really large numbers, get_Height always returns 1. I've attached a sample code. It's not very long so hopefully someone can help my spot what I'm doing wrong.

Thanks!

Code: Select all


#include <windows.h>
#include "optitrack.h"

#include <stdio.h>

int main(int argc, char **argv)
{
    INPCameraCollection     *cameraCollection   = NULL;
    INPCamera               *camera             = NULL;
    INPCameraFrame          *frame              = NULL;
    HRESULT                 result              = E_FAIL;
    long                    cameraCount         = 0;
    int                     frameCounter        = 0;
    int                     i                   = 0;
    int                     j                   = 0;
    
    long                    serial,
                            width,
                            height,
                            model,
                            revision,
                            rate;
                            
    long                    objectCount,
                            frameId, 
                            switchState;
    double                  timeStamp;
    INPObject               *obj;
                            
    double                  x,
                            y,
                            area;
    long                    objWidth,
                            objHeight,
                            rank,
                            score;
    
    VARIANT                 variant;
    
    if( !SUCCEEDED( result = CoInitialize( NULL ) ) )
    {
        fprintf(stderr, "Couldn't initialize Component Object Model\n");
        return 1;
    }
    
    result = CoCreateInstance( &CLSID_NPCameraCollection, NULL, CLSCTX_ALL, 
                               &IID_INPCameraCollection, (void **)&cameraCollection );
                            
    if( !SUCCEEDED( result ) )
    {
        fprintf(stderr, "Couldn't retrieve camera collection COM object\n");
        return 1;
    }
    
    cameraCollection->lpVtbl->Enum( cameraCollection );
    cameraCollection->lpVtbl->get_Count( cameraCollection, &cameraCount );
    
    printf(" %i Camera(s) Detected: \n", cameraCount );
    
    for(i=0; i < cameraCount; i++)
    {
        cameraCollection->lpVtbl->Item( cameraCollection, i, &camera );
        
        camera->lpVtbl->get_SerialNumber(    camera, &serial     );
        camera->lpVtbl->get_Width(          camera, &width      );
        camera->lpVtbl->get_Height(         camera, &height     );
        camera->lpVtbl->get_Model(          camera, &model      );
        camera->lpVtbl->get_Revision(       camera, &revision   );
        camera->lpVtbl->get_FrameRate(      camera, &rate       );
        
        printf( "  Camera %d\n", serial );
		printf( "  =========================\n" );
		printf( "  Resolution: %dx%d\n", width,height );
		printf( "  Revision  : 0x%8x\n", revision );
		printf( "  Model     : 0x%8x\n", model );
		printf( "  Frame rate: %d\n\n" , rate );
        printf( "=================================================\n");
        
        variant.vt      = VT_I4;
        
        variant.lVal = 0;
        camera->lpVtbl->SetOption( camera, NP_OPTION_VIDEO_TYPE, variant );
        
        variant.lVal = 1;
        camera->lpVtbl->SetOption( camera, NP_OPTION_FRAME_DECIMATION, variant );
        
        variant.lVal = 03;
        camera->lpVtbl->SetOption( camera, NP_OPTION_NUMERIC_DISPLAY_ON, variant );
        
        camera->lpVtbl->Release( camera );
    }
    
    if( cameraCount > 0 )
    {
        cameraCollection->lpVtbl->Item( cameraCollection, 0, &camera );
        
        camera->lpVtbl->Open( camera );
        camera->lpVtbl->Start( camera );

        for( i = 0; i < 1000; i++ )
        {
            result = camera->lpVtbl->GetFrame( camera, 0, &frame );
            
            if( SUCCEEDED( result ) && frame)
            {
                frame->lpVtbl->get_Count( frame, &objectCount );
                frame->lpVtbl->get_Id( frame, &frameId );
                frame->lpVtbl->get_SwitchState( frame, &switchState );
                frame->lpVtbl->get_TimeStamp( frame, &variant );
                    timeStamp = variant.dblVal;
                
                printf("\n\nFrame:      %i\n", frameId);
                printf("----------------------\n");
                printf("Items:      %i\n", objectCount);
                printf("Switches:   %x\n", switchState);
                printf("TimeStamp:  %d\n", timeStamp);
                printf("----------------------\n");
                
                for( j=0; j < objectCount; j++)
                {
                    frame->lpVtbl->Item( frame, j, &obj );
                    obj->lpVtbl->get_Area( obj, &variant );
                        area = variant.dblVal;
                    obj->lpVtbl->get_X(obj, &variant);
                        x = variant.dblVal; 
                    obj->lpVtbl->get_Y(obj, &variant);
                        y = variant.dblVal;
                    obj->lpVtbl->get_Score( obj, &variant );
                        score = variant.dblVal;
                    obj->lpVtbl->get_Rank( obj, &rank );
                    obj->lpVtbl->get_Width( obj, &objWidth );
                    obj->lpVtbl->get_Height( obj, &objHeight );
                    
                    printf("%i  x: %x, y: %x, a: %x, score: %x, rank: %x, width: %x, height: %x\n", 
                        j, x, y, area, score, rank, objWidth, objHeight);
                    //printf("%i  x: %4d, y: %4d, a: %3i, score: %4d, rank: %3i, width: %3i, height: %3i\n", 
                    //        j, x, y, area, score, rank, objWidth, objHeight);
                    
                }
                
                frame->lpVtbl->Free( frame );
                frame = NULL;
                printf("---\n\n");
            }
            
            Sleep(10);
        }
        
        camera->lpVtbl->Stop( camera );
        camera->lpVtbl->Close( camera );
        
        camera->lpVtbl->Release( camera );
    }
    
    CoUninitialize();
}
In addition, I also tried getting the object data with the INPCameraFrame::GetObjectData function but that didn't quite work either (same problems). On a side note, the SDK doesn't really go over this method very well. There is some disembodied code that sort of indicates what needs to be done but nothing explicit. My interpretation is that GetObjectData( (byte *)Buffer, (long) BufferSize, (long *) ReturnedObjectCount) fills up "Buffer" with the data up to "BufferSize" and stores the actual number of objects that were played in "BufferSize" in "ReturnedObjectCount". Also, it is my understanding that it fills "Buffer" in the following order: X, Y, Width, Height, Area, Rank, Score, each taking up 4 bytes. This is what is indicated to me by the sample code using struct sCameraObject... in 4.1.8.2.2 in the API docs.
Post Reply