int fillPosWithInterpolatedFrame(struct VirtualStream * stream,ObjectIDHandler ObjID,float * pos, unsigned int PrevFrame,unsigned int NextFrame , unsigned int time ) { if (stream->object[ObjID].frame==0) { #if PRINT_WARNING_INFO fprintf(stderr,"Cannot Access interpolated frames for object %u \n",ObjID); #endif return 0; } if (PrevFrame==NextFrame) { return fillPosWithFrame(stream,ObjID,PrevFrame,pos); } #if PRINT_DEBUGGING_INFO fprintf(stderr,"Interpolating frames @ %u , between %u and %u \n",time,PrevFrame,NextFrame); #endif float interPos[7]; unsigned int MAX_stepTime= stream->object[ObjID].frame[NextFrame].time - stream->object[ObjID].frame[PrevFrame].time; if (MAX_stepTime == 0 ) { MAX_stepTime=1; } unsigned int our_stepTime= time - stream->object[ObjID].frame[PrevFrame].time; interPos[0]=(float) ( stream->object[ObjID].frame[NextFrame].x-stream->object[ObjID].frame[PrevFrame].x ) * our_stepTime / MAX_stepTime; interPos[0]+=stream->object[ObjID].frame[PrevFrame].x; interPos[1]=(float) ( stream->object[ObjID].frame[NextFrame].y-stream->object[ObjID].frame[PrevFrame].y ) * our_stepTime / MAX_stepTime; interPos[1]+=stream->object[ObjID].frame[PrevFrame].y; interPos[2]=(float) ( stream->object[ObjID].frame[NextFrame].z-stream->object[ObjID].frame[PrevFrame].z ) * our_stepTime / MAX_stepTime; interPos[2]+=stream->object[ObjID].frame[PrevFrame].z; interPos[3]=(float) ( stream->object[ObjID].frame[NextFrame].rot1-stream->object[ObjID].frame[PrevFrame].rot1 ) * our_stepTime / MAX_stepTime; interPos[3]+=stream->object[ObjID].frame[PrevFrame].rot1; interPos[4]=(float) ( stream->object[ObjID].frame[NextFrame].rot2-stream->object[ObjID].frame[PrevFrame].rot2 ) * our_stepTime / MAX_stepTime; interPos[4]+=stream->object[ObjID].frame[PrevFrame].rot2; interPos[5]=(float) ( stream->object[ObjID].frame[NextFrame].rot3-stream->object[ObjID].frame[PrevFrame].rot3 ) * our_stepTime / MAX_stepTime; interPos[5]+=stream->object[ObjID].frame[PrevFrame].rot3; interPos[6]=(float) ( stream->object[ObjID].frame[NextFrame].rot4-stream->object[ObjID].frame[PrevFrame].rot4 ) * our_stepTime / MAX_stepTime; interPos[6]+=stream->object[ObjID].frame[PrevFrame].rot4; pos[0]=interPos[0]; pos[1]=interPos[1]; pos[2]=interPos[2]; pos[3]=interPos[3]; pos[4]=interPos[4]; pos[5]=interPos[5]; pos[6]=interPos[6]; #if PRINT_DEBUGGING_INFO fprintf(stderr,"ok \n"); #endif return 1; }
int calculateVirtualStreamPos(struct VirtualStream * stream,ObjectIDHandler ObjID,unsigned int timeAbsMilliseconds,float * pos) { if (stream==0) { fprintf(stderr,"calculateVirtualStreamPos called with null stream\n"); return 0; } if (stream->object==0) { fprintf(stderr,"calculateVirtualStreamPos called with null object array\n"); return 0; } if (stream->numberOfObjects<=ObjID) { fprintf(stderr,"calculateVirtualStreamPos ObjID %u is out of bounds (%u)\n",ObjID,stream->numberOfObjects); return 0; } if (stream->object[ObjID].frame == 0 ) { fprintf(stderr,"calculateVirtualStreamPos ObjID %u does not have a frame array allocated\n",ObjID); return 0; } if (stream->object[ObjID].numberOfFrames == 0 ) { fprintf(stderr,"calculateVirtualStreamPos ObjID %u has 0 frames\n",ObjID); return 0; } if (stream->autoRefresh != 0 ) { //Check for refreshed version ? if (stream->autoRefresh < timeAbsMilliseconds-stream->lastRefresh ) { unsigned long current_size = getFileSize(stream->filename); if (current_size != stream->fileSize) { refreshVirtualStream(stream); stream->lastRefresh = timeAbsMilliseconds; } } } unsigned int FrameIDToReturn = 0; unsigned int FrameIDLast = 0; unsigned int FrameIDNext = 0; /*!OK , Two major cases here..! The one is a simple Next frame getter , the second is a more complicated interpolated frame getter..! */ if ( (stream->object[ObjID].MAX_numberOfFrames == 0 ) ) { fillPosWithNull(stream,ObjID,pos); return 1; } else if ( (stream->ignoreTime) || (stream->object[ObjID].MAX_numberOfFrames == 1 ) ) { //We might want to ignore time and just return frame after frame on each call! //Also if we only got one frame for the object there is no point in trying to interpolate time etc.. so just handle things here.. if ( stream->object[ObjID].lastFrame +1 >= stream->object[ObjID].MAX_numberOfFrames ) { stream->object[ObjID].lastFrame = 0; } FrameIDToReturn = stream->object[ObjID].lastFrame; ++stream->object[ObjID].lastFrame; fillPosWithFrame(stream,ObjID,FrameIDToReturn,pos); FrameIDLast = FrameIDToReturn; FrameIDNext = FrameIDToReturn+1; if ( FrameIDNext >= stream->object[ObjID].numberOfFrames ) { FrameIDNext = 0; } return 1; } /*!END OF SIMPLE FRAME GETTER*/ else { /*!START OF INTERPOLATED FRAME GETTER*/ //This is the case when we respect time , we will pick two frames and interpolate between them if ( timeAbsMilliseconds > stream->object[ObjID].MAX_timeOfFrames ) { //This means we have passed the last frame.. so lets find out where we really are.. timeAbsMilliseconds = timeAbsMilliseconds % stream->object[ObjID].MAX_timeOfFrames; //timeAbsMilliseconds should contain a valid value now somewhere from 0->MAX_timeOfFrames } #if PRINT_DEBUGGING_INFO fprintf(stderr,"Object %u has %u frames , lets search where we are \n",ObjID,stream->object[ObjID].numberOfFrames); #endif //We scan all the frames to find out the "last one" and the "next one" unsigned int i =0; for ( i=0; i <stream->object[ObjID].MAX_numberOfFrames-1; i++ ) { if (( stream->object[ObjID].frame[i].time <= timeAbsMilliseconds ) && ( timeAbsMilliseconds <= stream->object[ObjID].frame[i+1].time ) ) { //This is the "next" frame! FrameIDLast = i; FrameIDNext = i+1; //This should be handled by raw response to zero elemetn :P break; } } //We now have our Last and Next frame , all that remains is extracting the //interpolated time between them..! return fillPosWithInterpolatedFrame(stream,ObjID,pos,FrameIDLast,FrameIDNext,timeAbsMilliseconds); } /*!END OF INTERPOLATED FRAME GETTER*/ return 0; }
int calculateVirtualStreamPos( struct VirtualStream * stream, ObjectIDHandler ObjID, unsigned int timeAbsMilliseconds, float * pos, float * joints, float * scaleX, float * scaleY, float * scaleZ ) { if (stream==0) { fprintf(stderr,"calculateVirtualStreamPos called with null stream\n"); return 0; } if (stream->object==0) { fprintf(stderr,"calculateVirtualStreamPos called with null object array\n"); return 0; } if (stream->numberOfObjects<=ObjID) { fprintf(stderr,"calculateVirtualStreamPos ObjID %u is out of bounds (%u)\n",ObjID,stream->numberOfObjects); return 0; } if ( (ObjID==0) && (stream->object[ObjID].frame == 0 ) ) { /*Special case with non declared cameras , it is ok , dont spam for every frame..! */ return 0; } else if (stream->object[ObjID].frame == 0 ) { fprintf(stderr,"calculateVirtualStreamPos ObjID %u does not have a frame array allocated\n",ObjID); return 0; } if ( (ObjID==0) && (stream->object[ObjID].numberOfFrames == 0 ) ) { /*Special case with non declared cameras , it is ok , dont spam for every frame..! */ return 0; } else if (stream->object[ObjID].numberOfFrames == 0 ) { fprintf(stderr,"calculateVirtualStreamPos ObjID %u has 0 frames\n",ObjID); return 0; } if ( (stream->autoRefresh != 0 ) || (stream->autoRefreshForce) ) { //Check for refreshed version ? if ( (stream->autoRefreshForce) || (stream->autoRefresh < timeAbsMilliseconds-stream->lastRefresh ) ) { unsigned long current_size = getFileSize(stream->filename); if ( (current_size != stream->fileSize) || (stream->autoRefreshForce) ) { refreshVirtualStream(stream,stream->associatedModelList); stream->lastRefresh = timeAbsMilliseconds; stream->autoRefreshForce=0; } } } unsigned int FrameIDToReturn = 0; unsigned int FrameIDLast = 0; unsigned int FrameIDNext = 0; /*!OK , Two major cases here..! The one is a simple Next frame getter , the second is a more complicated interpolated frame getter..! */ if ( (stream->object[ObjID].MAX_numberOfFrames == 0 ) ) { fprintf(stderr,"Returning Null position for ObjID %u\n",ObjID); fillPosWithNull(/*stream,ObjID,*/pos,scaleX,scaleY,scaleZ); return 1; } else if ( (stream->ignoreTime) || (stream->object[ObjID].MAX_numberOfFrames == 1 ) || ((stream->alwaysShowLastFrame)) ) { //We might want to ignore time and just return frame after frame on each call! //Also if we only got one frame for the object there is no point in trying to interpolate time etc.. so just handle things here.. if ( stream->object[ObjID].lastFrame +1 >= stream->object[ObjID].MAX_numberOfFrames ) { stream->object[ObjID].lastFrame = 0; } FrameIDToReturn = stream->object[ObjID].lastFrame; ++stream->object[ObjID].lastFrame; if (stream->alwaysShowLastFrame) { if (stream->object[ObjID].numberOfFrames>0) { FrameIDToReturn = stream->object[ObjID].numberOfFrames-1; //fprintf(stderr,"Always showing last frame ( %u ) , F7 to change\n",FrameIDToReturn); } } //fprintf(stderr,"Simple Getter ObjID %u Frame %u\n",ObjID,FrameIDToReturn); fillPosWithFrame( stream, ObjID, FrameIDToReturn, pos, joints, scaleX, scaleY, scaleZ ); //fprintf(stderr,"fillPosWithFrame %u => ( %0.2f %0.2f %0.2f , %0.2f %0.2f %0.2f)\n",FrameIDToReturn,pos[0],pos[1],pos[2],pos[3],pos[4],pos[5]); FrameIDLast = FrameIDToReturn; FrameIDNext = FrameIDToReturn+1; if ( FrameIDNext >= stream->object[ObjID].numberOfFrames ) { //We 've reached the end of the stream so the last frame should truncate 0 stream->object[ObjID].lastFrame=0; } return 1; } /*!END OF SIMPLE FRAME GETTER*/ else { /*!START OF INTERPOLATED FRAME GETTER*/ //fprintf(stderr,"Interpolated Getter ObjID %u Frame %u\n",ObjID,FrameIDToReturn); //fprintf(stderr,"interpolated position for ObjID %u\n",ObjID); //This is the case when we respect time , we will pick two frames and interpolate between them if ( timeAbsMilliseconds > stream->object[ObjID].MAX_timeOfFrames ) { //This means we have passed the last frame.. so lets find out where we really are.. if (stream->object[ObjID].MAX_timeOfFrames == 0 ) { //If max time of frames is 0 then our time is also zero ( since it never goes over max ) //fprintf(stderr,"timeAbsMilliseconds can not be something more than zero"); timeAbsMilliseconds=0; } else { timeAbsMilliseconds = timeAbsMilliseconds % stream->object[ObjID].MAX_timeOfFrames; } //timeAbsMilliseconds should contain a valid value now somewhere from 0->MAX_timeOfFrames } #if PRINT_DEBUGGING_INFO fprintf(stderr,"Object %u has %u frames , lets search where we are \n",ObjID,stream->object[ObjID].numberOfFrames); #endif //We scan all the frames to find out the "last one" and the "next one" unsigned int i =0; for ( i=0; i <stream->object[ObjID].MAX_numberOfFrames-1; i++ ) { if (( stream->object[ObjID].frame[i].time <= timeAbsMilliseconds ) && ( timeAbsMilliseconds <= stream->object[ObjID].frame[i+1].time ) ) { //This is the "next" frame! FrameIDLast = i; FrameIDNext = i+1; //This should be handled by raw response to zero elemetn :P break; } } //We now have our Last and Next frame , all that remains is extracting the //interpolated time between them..! return fillPosWithInterpolatedFrame( stream, ObjID, pos, joints, scaleX, scaleY, scaleZ, FrameIDLast, FrameIDNext, timeAbsMilliseconds ); } /*!END OF INTERPOLATED FRAME GETTER*/ return 0; }
int fillPosWithInterpolatedFrame( struct VirtualStream * stream, ObjectIDHandler ObjID, float * pos, float * joints, float * scaleX, float * scaleY, float * scaleZ, unsigned int PrevFrame, unsigned int NextFrame , unsigned int time ) { if (stream->object[ObjID].frame==0) { #if PRINT_WARNING_INFO fprintf(stderr,"Cannot Access interpolated frames for object %u \n",ObjID); #endif return 0; } if (PrevFrame==NextFrame) { return fillPosWithFrame( stream, ObjID, PrevFrame, pos, joints, scaleX, scaleY, scaleZ ); } #if PRINT_DEBUGGING_INFO fprintf(stderr,"Interpolating frames @ %u , between %u and %u \n",time,PrevFrame,NextFrame); #endif float interPos[7]={0}; float interScale; unsigned int MAX_stepTime= stream->object[ObjID].frame[NextFrame].time - stream->object[ObjID].frame[PrevFrame].time; if (MAX_stepTime == 0 ) { MAX_stepTime=1; } unsigned int our_stepTime= time - stream->object[ObjID].frame[PrevFrame].time; interPos[0]=(float) ( stream->object[ObjID].frame[NextFrame].x-stream->object[ObjID].frame[PrevFrame].x ) * our_stepTime / MAX_stepTime; interPos[0]+=stream->object[ObjID].frame[PrevFrame].x; interPos[1]=(float) ( stream->object[ObjID].frame[NextFrame].y-stream->object[ObjID].frame[PrevFrame].y ) * our_stepTime / MAX_stepTime; interPos[1]+=stream->object[ObjID].frame[PrevFrame].y; interPos[2]=(float) ( stream->object[ObjID].frame[NextFrame].z-stream->object[ObjID].frame[PrevFrame].z ) * our_stepTime / MAX_stepTime; interPos[2]+=stream->object[ObjID].frame[PrevFrame].z; interPos[3]=(float) ( stream->object[ObjID].frame[NextFrame].rot1-stream->object[ObjID].frame[PrevFrame].rot1 ) * our_stepTime / MAX_stepTime; interPos[3]+=stream->object[ObjID].frame[PrevFrame].rot1; interPos[4]=(float) ( stream->object[ObjID].frame[NextFrame].rot2-stream->object[ObjID].frame[PrevFrame].rot2 ) * our_stepTime / MAX_stepTime; interPos[4]+=stream->object[ObjID].frame[PrevFrame].rot2; interPos[5]=(float) ( stream->object[ObjID].frame[NextFrame].rot3-stream->object[ObjID].frame[PrevFrame].rot3 ) * our_stepTime / MAX_stepTime; interPos[5]+=stream->object[ObjID].frame[PrevFrame].rot3; interPos[6]=(float) ( stream->object[ObjID].frame[NextFrame].rot4-stream->object[ObjID].frame[PrevFrame].rot4 ) * our_stepTime / MAX_stepTime; interPos[6]+=stream->object[ObjID].frame[PrevFrame].rot4; interScale = (float) ( stream->object[ObjID].frame[NextFrame].scaleX -stream->object[ObjID].frame[PrevFrame].scaleX ) * our_stepTime / MAX_stepTime; interScale += stream->object[ObjID].frame[PrevFrame].scaleX; *scaleX=interScale; interScale = (float) ( stream->object[ObjID].frame[NextFrame].scaleY -stream->object[ObjID].frame[PrevFrame].scaleY ) * our_stepTime / MAX_stepTime; interScale += stream->object[ObjID].frame[PrevFrame].scaleY; *scaleY=interScale; interScale = (float) ( stream->object[ObjID].frame[NextFrame].scaleZ -stream->object[ObjID].frame[PrevFrame].scaleZ ) * our_stepTime / MAX_stepTime; interScale += stream->object[ObjID].frame[PrevFrame].scaleZ; *scaleZ=interScale; pos[0]=interPos[0]; pos[1]=interPos[1]; pos[2]=interPos[2]; pos[3]=interPos[3]; pos[4]=interPos[4]; pos[5]=interPos[5]; pos[6]=interPos[6]; if ( (joints!=0) && ( (stream->object[ObjID].frame[PrevFrame].jointList!=0) || (stream->object[ObjID].frame[NextFrame].jointList!=0) ) ) { //fprintf(stderr,"interpolating joints for frame %u ( %u joints / obj %u ) \n",PrevFrame , stream->object[ObjID].frame[PrevFrame].jointList->numberOfJoints , ObjID); fillJointsWithInterpolatedFrame( stream, ObjID, joints, PrevFrame, NextFrame , our_stepTime, MAX_stepTime ); } #if PRINT_DEBUGGING_INFO fprintf(stderr,"ok \n"); #endif return 1; }