#include <mil.h>
#define GEOMETRIC 0
#define GRAYSCALE 1
#define PATTERN_MATCHING_METHOD GRAYSCALE
#define TARGET_PLATFORM M_NULL
#define DISPLAY_UPDATE M_YES
#define MODEL_MIN_MATCH_SCORE 50.0
#define NB_TARGET_IMAGES 4
#if M_MIL_USE_CE
#define SLAVE_SYSTEM_DESCRIPTOR MIL_TEXT("dmiltcp:
#else
#define SLAVE_SYSTEM_DESCRIPTOR M_SYSTEM_DEFAULT
#endif
#define SLAVE_DLL_NAME MIL_TEXT("dmilobjecttrackingslave")
#if M_MIL_USE_WINDOWS && !M_MIL_USE_CE
#define SLAVE_DLL_TARGET_NAME INSTALL_DIR MIL_TEXT("mil\\dll\\") SLAVE_DLL_NAME MIL_TEXT(".dll")
#elif M_MIL_USE_CE
#define SLAVE_DLL_TARGET_NAME INSTALL_DIR SLAVE_DLL_NAME MIL_TEXT(".dll")
#elif M_MIL_USE_LINUX
#define SLAVE_DLL_TARGET_NAME INSTALL_DIR MIL_TEXT("mil/lib/lib") SLAVE_DLL_NAME MIL_TEXT(".so")
#endif
#if M_MIL_USE_CE
#define DISPLAY_FORMAT MIL_TEXT("M_REMOTEVIEW_RGB")
#else
#define DISPLAY_FORMAT MIL_TEXT("M_DEFAULT")
#endif
MIL_UINT32 MFTYPE PatternMatchingLoop(void *MilDataExchangeBuffer);
typedef struct
{
MIL_ID MilSystem;
MIL_ID MilDisplay;
MIL_ID MilDigitizer;
MIL_ID MilImageDisp;
MIL_ID MilImage[NB_TARGET_IMAGES];
MIL_ID MilThread;
MIL_ID MilDataExchangeBuffer;
MIL_ID MilDataExchangeBufferReadyEvent;
MIL_ID MilModelPositionOkEvent;
MIL_ID MilStopProcessingEvent;
MIL_ID MilModel;
MIL_ID MilResult;
MIL_INT32 PatternMatchingMethod;
MIL_INT32 DisplayUpdateFlag;
MIL_INT32 Found;
MIL_DOUBLE PosX;
MIL_DOUBLE PosY;
MIL_DOUBLE Score;
MIL_DOUBLE Time;
MIL_INT32 NbFindDone;
MIL_INT32 Error;
} DataExchangeStruct;
MIL_INT MFTYPE DataExchangeBufferModified(MIL_INT, MIL_ID, void*);
int MosMain(void)
{
#if (!M_MIL_LITE)
MIL_ID MilApplication,
MilSystemOwnerApplication;
DataExchangeStruct DataEx;
long i=0;
long Error = 0;
long NbGrab = 0;
MIL_DOUBLE Time = 0.0;
bool CheckExistence = false;
MappAlloc(M_NULL, M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, SLAVE_SYSTEM_DESCRIPTOR, M_DEFAULT, M_DEFAULT, &DataEx.MilSystem);
if(MsysInquire(DataEx.MilSystem, M_LOCATION, M_NULL) != M_REMOTE)
{
MosPrintf(MIL_TEXT("This example requires the default system to be a remote system.\n"));
MosPrintf(MIL_TEXT("Please select a remote system as the default.\n"));
MosPrintf(MIL_TEXT("If no remote systems are registered "));
MosPrintf(MIL_TEXT("please go to the DistributedMIL->Connections page, "));
MosPrintf(MIL_TEXT("register a remote system, "));
MosPrintf(MIL_TEXT("and then select it as the default system.\n"));
MsysFree(DataEx.MilSystem);
MappFree(MilApplication);
MosGetch();
return -1;
}
MsysInquire(DataEx.MilSystem, M_OWNER_APPLICATION, &MilSystemOwnerApplication);
if((MappInquire(M_DEFAULT, M_PLATFORM_BITNESS, M_NULL) !=
MappInquire(MilSystemOwnerApplication, M_PLATFORM_BITNESS, M_NULL)))
{
MosPrintf(MIL_TEXT("This example does not work with a mix of 32 and 64 bit\n"));
MosPrintf(MIL_TEXT("client and server.\n"));
MsysFree(DataEx.MilSystem);
MappFree(MilApplication);
MosPrintf(MIL_TEXT("Press a key to terminate.\n\n"));
MosGetch();
return -1;
}
if((MappInquire(M_DEFAULT, M_PLATFORM_OS_TYPE, M_NULL) ==
MappInquire(MilSystemOwnerApplication, M_PLATFORM_OS_TYPE, M_NULL)))
{
MappControl(M_DEFAULT, M_ERROR, M_PRINT_DISABLE);
MappFileOperation(M_DEFAULT, SLAVE_DLL_TARGET_NAME, MilSystemOwnerApplication, M_NULL,
M_FILE_COPY_MIL_DLL, M_DEFAULT, M_NULL);
if(0 != MappGetError(M_DEFAULT, M_CURRENT, M_NULL))
{
MosPrintf(MIL_TEXT("There was an error while copying the slave library.\n"));
MosPrintf(MIL_TEXT("Checking if one is present on the remote system.\n"));
CheckExistence = true;
}
MappControl(M_DEFAULT, M_ERROR, M_PRINT_ENABLE);
}
else
CheckExistence = true;
if(CheckExistence)
{
MIL_INT DllExists = M_NO;
MappFileOperation(MilSystemOwnerApplication, SLAVE_DLL_NAME, M_NULL, M_NULL,
M_FILE_EXISTS_MIL_DLL, M_DEFAULT, &DllExists);
if(DllExists != M_YES)
{
MosPrintf(MIL_TEXT("The slave library was NOT copied to the remote system.\n"));
MosPrintf(MIL_TEXT("Make sure it is present for the example to work properly.\n"));
MosPrintf(MIL_TEXT("See DistributedMILExamples.txt in the DistributedMIL examples "));
MosPrintf(MIL_TEXT("folder\nfor more information.\n"));
MsysFree(DataEx.MilSystem);
MappFree(MilApplication);
MosPrintf(MIL_TEXT("Press a key to terminate.\n\n"));
MosGetch();
return -1;
}
}
MdispAlloc(DataEx.MilSystem, M_DEFAULT, DISPLAY_FORMAT, M_DEFAULT, &DataEx.MilDisplay);
MdigAlloc(DataEx.MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_DEFAULT,
&DataEx.MilDigitizer);
MbufAlloc2d(DataEx.MilSystem,
MdigInquire(DataEx.MilDigitizer, M_SIZE_X, M_NULL),
MdigInquire(DataEx.MilDigitizer, M_SIZE_Y, M_NULL),
8L+M_UNSIGNED,
M_IMAGE+M_PROC+M_DISP+M_GRAB, &DataEx.MilImageDisp);
MthrAlloc(DataEx.MilSystem, M_EVENT, M_NOT_SIGNALED+M_AUTO_RESET,
M_NULL, M_NULL, &DataEx.MilModelPositionOkEvent);
MthrAlloc(DataEx.MilSystem, M_EVENT, M_NOT_SIGNALED+M_AUTO_RESET,
M_NULL, M_NULL, &DataEx.MilStopProcessingEvent);
MthrAlloc(DataEx.MilSystem, M_EVENT, M_SIGNALED +M_AUTO_RESET,
M_NULL, M_NULL, &DataEx.MilDataExchangeBufferReadyEvent);
MbufClear(DataEx.MilImageDisp, 0x0);
MdispSelect(DataEx.MilDisplay, DataEx.MilImageDisp);
MbufAlloc1d(DataEx.MilSystem, sizeof(DataExchangeStruct),
8L+M_UNSIGNED, M_ARRAY, &DataEx.MilDataExchangeBuffer);
DataEx.DisplayUpdateFlag = DISPLAY_UPDATE;
DataEx.PatternMatchingMethod = PATTERN_MATCHING_METHOD;
MbufPut(DataEx.MilDataExchangeBuffer, &DataEx);
MbufHookFunction(DataEx.MilDataExchangeBuffer, M_MODIFIED_BUFFER,
DataExchangeBufferModified, &DataEx.MilDataExchangeBuffer);
MthrAlloc(DataEx.MilSystem, M_THREAD, M_DEFAULT, PatternMatchingLoop,
&DataEx.MilDataExchangeBuffer, &DataEx.MilThread);
MosPrintf(MIL_TEXT("\nMIL DTK:\n"));
MosPrintf(MIL_TEXT("--------\n\n"));
MosPrintf(MIL_TEXT("Distributed object tracking:\n\n"));
MosPrintf(MIL_TEXT("Place the rectangle on the model to be found.\n"));
MosPrintf(MIL_TEXT("Press a key to continue.\n\n"));
MosGetch();
MthrControl(DataEx.MilModelPositionOkEvent, M_EVENT_SET, M_SIGNALED);
MosPrintf(MIL_TEXT("Finding the model using an autonomous user-defined function.\n"));
MosPrintf(MIL_TEXT("Press a key to stop processing.\n\n"));
MosGetch();
MthrControl(DataEx.MilStopProcessingEvent, M_EVENT_SET, M_SIGNALED);
MthrWait(DataEx.MilThread, M_THREAD_END_WAIT, M_NULL);
MbufHookFunction(DataEx.MilDataExchangeBuffer, M_MODIFIED_BUFFER+M_UNHOOK,
DataExchangeBufferModified, &DataEx.MilDataExchangeBuffer);
MthrFree(DataEx.MilDataExchangeBufferReadyEvent);
MthrFree(DataEx.MilStopProcessingEvent);
MthrFree(DataEx.MilModelPositionOkEvent);
MthrFree(DataEx.MilThread);
MbufFree(DataEx.MilDataExchangeBuffer);
MbufFree(DataEx.MilImageDisp);
MdispFree(DataEx.MilDisplay);
MdigFree(DataEx.MilDigitizer);
MsysFree(DataEx.MilSystem);
MappFree(MilApplication);
#else
MosPrintf(MIL_TEXT("MIL-Lite is not sufficient to run this example.\n");
MosPrintf(MIL_TEXT("This example requires the complete version of MIL.\n"));
MosPrintf(MIL_TEXT("Press a key to end\n\n"));
MosGetch();
#endif
return 0;
}
MIL_INT MFTYPE DataExchangeBufferModified(MIL_INT HookType, MIL_ID EventId,
void* DataExchangeIdPtr)
{
DataExchangeStruct DataEx;
MbufGet(*(MIL_ID *)DataExchangeIdPtr, &DataEx);
MthrControl(DataEx.MilDataExchangeBufferReadyEvent, M_EVENT_SET, M_SIGNALED);
if (DataEx.Found)
{
MosPrintf(MIL_TEXT("Search #%ld: X=%-6.2f, Y=%-6.2f, Score=%5.1f %%, Frame rate=%.1f fps. \r"),
DataEx.NbFindDone, DataEx.PosX, DataEx.PosY, DataEx.Score, DataEx.NbFindDone/DataEx.Time);
}
else
{
MosPrintf(MIL_TEXT("Search #%ld: Model not found: Score<%5.1f %%, Frame rate=%.1f fps. \r"),
DataEx.NbFindDone, MODEL_MIN_MATCH_SCORE, DataEx.NbFindDone/DataEx.Time);
}
return(M_NULL);
}
#define PATTERN_MATCHING_LOOP (M_USER_FUNCTION+7)
#define NB_PARAMETERS 1
#define SLAVE_FUNC_NAME MIL_TEXT("SlavePatternMatchingLoop")
MIL_UINT32 MFTYPE PatternMatchingLoop(void *MilDataExchangeBuffer)
{
MIL_ID Func;
MfuncAlloc(MIL_TEXT("PatternMatchingLoop"),
NB_PARAMETERS,
M_NULL, SLAVE_DLL_NAME, SLAVE_FUNC_NAME,
PATTERN_MATCHING_LOOP,
M_SYNCHRONOUS_FUNCTION+TARGET_PLATFORM,
&Func);
MfuncParamMilId(Func, 1, *(MIL_ID *)MilDataExchangeBuffer, M_ARRAY, M_IN+M_OUT);
MfuncCall(Func);
MfuncFree(Func);
return(M_TRUE);
}