#include <mil.h>
void DxfWithCalibrationContextExample(MIL_ID MilSystem, MIL_ID MilDisplay);
void DxfWithPixelScaleExample(MIL_ID MilSystem, MIL_ID MilDisplay);
#define DXF_FILE_MODEL_PATH M_IMAGE_PATH MIL_TEXT("DxfModelFinding\\Model.dxf")
#define TARGET_IMAGE_PATH M_IMAGE_PATH MIL_TEXT("SingleTarget.mim")
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n"));
MosPrintf(MIL_TEXT("DxfModelFinding\n\n"));
MosPrintf(MIL_TEXT("[SYNOPSIS]\n"));
MosPrintf(MIL_TEXT("This example shows how to match a CAD model to a target image\n"));
MosPrintf(MIL_TEXT("using Model Finder.\n\n"));
MosPrintf(MIL_TEXT("[MODULES USED]\n"));
MosPrintf(MIL_TEXT("Modules used: Display, Graphics, Model Finder, Calibration.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
}
int MosMain(void)
{
PrintHeader();
MIL_ID MilApplication,
MilSystem,
MilDisplay;
MappAllocDefault(M_DEFAULT, &MilApplication, &MilSystem, &MilDisplay, M_NULL, M_NULL);
DxfWithCalibrationContextExample(MilSystem, MilDisplay);
DxfWithPixelScaleExample(MilSystem, MilDisplay);
MappFreeDefault(MilApplication, MilSystem, MilDisplay, M_NULL, M_NULL);
return 0;
}
void DxfWithCalibrationContextExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilTargetImage,
ModelFinderContext,
ModelFinderResult,
CalibrationContext,
GraphicList;
MIL_DOUBLE ModelDrawColor = M_COLOR_RED,
CalibrationDrawColor = M_COLOR_CYAN,
PositionDrawColor = M_COLOR_BLUE;
MbufImport(TARGET_IMAGE_PATH, M_DEFAULT, M_RESTORE + M_NO_GRAB + M_NO_COMPRESS, MilSystem,
&MilTargetImage);
MdispSelect(MilDisplay, MilTargetImage);
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &ModelFinderContext);
MmodDefineFromFile(ModelFinderContext, M_DXF_FILE, DXF_FILE_MODEL_PATH, M_DEFAULT);
MmodAllocResult(MilSystem, M_DEFAULT, &ModelFinderResult);
McalAlloc(MilSystem, M_UNIFORM_TRANSFORMATION, M_DEFAULT, &CalibrationContext);
McalUniform(CalibrationContext, 0.0, 0.0, 0.75, 0.75, 0.0, M_DEFAULT);
McalAssociate(CalibrationContext, MilTargetImage, M_DEFAULT);
MmodControl(ModelFinderContext, 0, M_ASSOCIATED_CALIBRATION, CalibrationContext);
MmodPreprocess(ModelFinderContext, M_DEFAULT);
MmodFind(ModelFinderContext, MilTargetImage, ModelFinderResult);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MgraColor(M_DEFAULT, ModelDrawColor);
MmodDraw(M_DEFAULT, ModelFinderResult, GraphicList,
M_DRAW_EDGES + M_DRAW_BOX, 0, M_DEFAULT);
MgraColor(M_DEFAULT, PositionDrawColor);
MmodDraw(M_DEFAULT, ModelFinderResult, GraphicList,
M_DRAW_POSITION, M_DEFAULT, M_DEFAULT);
MgraColor(M_DEFAULT, CalibrationDrawColor);
McalDraw(M_DEFAULT, CalibrationContext, GraphicList,
M_DRAW_ABSOLUTE_COORDINATE_SYSTEM, M_DEFAULT, M_DEFAULT);
MosPrintf(MIL_TEXT("Solution 1:\n"));
MosPrintf(MIL_TEXT("----------\n"));
MosPrintf(MIL_TEXT("A calibration context is used to map the target\n"));
MosPrintf(MIL_TEXT("image to the world system of the CAD model.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MbufFree(MilTargetImage);
MmodFree(ModelFinderContext);
MmodFree(ModelFinderResult);
McalFree(CalibrationContext);
MgraFree(GraphicList);
}
void DxfWithPixelScaleExample(MIL_ID MilSystem, MIL_ID MilDisplay)
{
MIL_ID MilTargetImage,
ModelFinderContext,
ModelFinderResult,
GraphicList;
MIL_DOUBLE ModelDrawColor = M_COLOR_RED,
PositionDrawColor = M_COLOR_BLUE;
MbufImport(TARGET_IMAGE_PATH, M_DEFAULT, M_RESTORE + M_NO_GRAB + M_NO_COMPRESS, MilSystem,
&MilTargetImage);
MdispSelect(MilDisplay, MilTargetImage);
MmodAlloc(MilSystem, M_GEOMETRIC, M_DEFAULT, &ModelFinderContext);
MmodDefineFromFile(ModelFinderContext, M_DXF_FILE, DXF_FILE_MODEL_PATH, M_DEFAULT);
MmodAllocResult(MilSystem, M_DEFAULT, &ModelFinderResult);
MmodControl(ModelFinderContext, 0, M_PIXEL_SCALE, 1.33);
MmodPreprocess(ModelFinderContext, M_DEFAULT);
MmodFind(ModelFinderContext, MilTargetImage, ModelFinderResult);
MgraAllocList(MilSystem, M_DEFAULT, &GraphicList);
MdispControl(MilDisplay, M_ASSOCIATED_GRAPHIC_LIST_ID, GraphicList);
MgraColor(M_DEFAULT, ModelDrawColor);
MmodDraw(M_DEFAULT, ModelFinderResult, GraphicList,
M_DRAW_EDGES + M_DRAW_BOX, 0, M_DEFAULT);
MgraColor(M_DEFAULT, PositionDrawColor);
MmodDraw(M_DEFAULT, ModelFinderResult, GraphicList,
M_DRAW_POSITION, M_DEFAULT, M_DEFAULT);
MosPrintf(MIL_TEXT("Solution 2:\n"));
MosPrintf(MIL_TEXT("----------\n"));
MosPrintf(MIL_TEXT("If the mapping between the CAD model and the target image is uniform,\n"));
MosPrintf(MIL_TEXT("the control M_PIXEL_SCALE can be used to specify the pixel to world ratio\n"));
MosPrintf(MIL_TEXT("without the need of a calibration context.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to end.\n\n"));
MosGetch();
MbufFree(MilTargetImage);
MmodFree(ModelFinderContext);
MmodFree(ModelFinderResult);
MgraFree(GraphicList);
}