#include <mil.h>
#include <map>
#include <math.h>
using namespace std;
void PrintHeader()
{
MosPrintf(MIL_TEXT("[EXAMPLE NAME]\n"));
MosPrintf(MIL_TEXT("MultiViewAnnotations\n\n"));
MosPrintf(MIL_TEXT("[SYNOPSIS]\n"));
MosPrintf(MIL_TEXT("This example shows the capabilities of advanced graphic annotations\n")
MIL_TEXT("when using an application that has multiple camera views. The resulting\n")
MIL_TEXT("subpixel annotations are drawn using the corresponding color of the most\n")
MIL_TEXT("suitable camera view with which to perform the processing operation.\n")
MIL_TEXT("Annotations can be panned, zoomed, and seen from any other camera point\n")
MIL_TEXT("of view by interactively selecting a different camera.\n\n"));
MosPrintf(MIL_TEXT("[MODULES USED]\n"));
MosPrintf(MIL_TEXT("Application, system, display, buffer, graphic, \n")
MIL_TEXT("image processing, calibration, model finder, \n")
MIL_TEXT("measurement, metrology, ocr.\n\n"));
}
#define EXAMPLE_IMAGE_PATH M_IMAGE_PATH MIL_TEXT("MultiViewAnnotations/")
static MIL_CONST_TEXT_PTR IMAGE1 = EXAMPLE_IMAGE_PATH MIL_TEXT("ImageView1.mim");
static MIL_CONST_TEXT_PTR IMAGE2 = EXAMPLE_IMAGE_PATH MIL_TEXT("ImageView2.mim");
static MIL_CONST_TEXT_PTR IMAGE3 = EXAMPLE_IMAGE_PATH MIL_TEXT("ImageView3.mim");
static MIL_CONST_TEXT_PTR IMAGE4 = EXAMPLE_IMAGE_PATH MIL_TEXT("ImageView4.mim");
static MIL_CONST_TEXT_PTR IMAGESKETCH = EXAMPLE_IMAGE_PATH MIL_TEXT("Sketch.mim");
static MIL_CONST_TEXT_PTR IMAGEGRID1 = EXAMPLE_IMAGE_PATH MIL_TEXT("CalGrid1.mim");
static MIL_CONST_TEXT_PTR IMAGEGRID2 = EXAMPLE_IMAGE_PATH MIL_TEXT("CalGrid2.mim");
static MIL_CONST_TEXT_PTR IMAGEGRID3 = EXAMPLE_IMAGE_PATH MIL_TEXT("CalGrid3.mim");
static MIL_CONST_TEXT_PTR IMAGEGRID4 = EXAMPLE_IMAGE_PATH MIL_TEXT("CalGrid4.mim");
static MIL_CONST_TEXT_PTR IMAGEGRID3D1 = EXAMPLE_IMAGE_PATH MIL_TEXT("CalGrid1AtAngle.mim");
static MIL_CONST_TEXT_PTR MODCONTEXT = EXAMPLE_IMAGE_PATH MIL_TEXT("ModelFinderContext.mmf");
static MIL_CONST_TEXT_PTR MEASCONTEXT = EXAMPLE_IMAGE_PATH MIL_TEXT("MeasMarker.edg");
static MIL_CONST_TEXT_PTR OCRCONTEXT = EXAMPLE_IMAGE_PATH MIL_TEXT("OCR_Context.mfo");
static MIL_CONST_TEXT_PTR METCONTEXT = EXAMPLE_IMAGE_PATH MIL_TEXT("MetrologyContext.met");
static const MIL_DOUBLE NB_PIXELS_PER_DEGREE = 10.0;
static const MIL_DOUBLE NB_PIXELS_PER_RADIAL_MM = 30.0;
static const MIL_DOUBLE TO_RAD = 3.14159265358989 / 180.0;
static const MIL_INT NB_FIXED_POINT = 4;
static const MIL_INT MAX_CHAR_LENGTH = 256;
static const MIL_INT NB_ANGLE_STEP = 5;
static const MIL_DOUBLE ANGLE_STEP = 60.0;
#define GRID_NB_ROWS 15L
#define GRID_NB_COLUMNS 16L
#define GRID_ROW_SPACING 5L
#define GRID_COLUMN_SPACING 5L
#define GRID_THICKNESS 2L
#define SRC_IMAGE_WIDTH 920L
#define SRC_IMAGE_HEIGHT 920L
#define DISPLAYED_IMAGE_WIDTH 512L
#define DISPLAYED_IMAGE_HEIGHT 512L
#define OFFSET_BETWEEN_IMAGES 6L
static const MIL_DOUBLE SRC_TO_DISP_ZOOM = (MIL_DOUBLE)DISPLAYED_IMAGE_WIDTH / (MIL_DOUBLE)SRC_IMAGE_WIDTH;
#define BUTTON1_POSX 234L
#define BUTTON1_POSY 150L
#define BUTTON2_POSX 389L
#define BUTTON2_POSY 328L
#define BUTTON3_POSX 345L
#define BUTTON3_POSY 213L
#define BUTTON4_POSX 68L
#define BUTTON4_POSY 310L
#define BUTTON_RADIUS 11L
#define CHILD_OFFSETX 146L
#define CHILD_OFFSETY 90L
#define CHILD_SIZEX 495L
#define CHILD_SIZEY 617L
#define STRING_LENGTH 16L
void ShowMultiViewAnnotations(MIL_ID MilSystem, MIL_ID MilDisplay1, MIL_ID MilDisplay2);
void CreateCalibration(MIL_ID MilSystem, MIL_ID& MilCalibration1, MIL_ID& MilCalibration2,
MIL_ID& MilCalibration3, MIL_ID& MilCalibration4);
void AddSelectableButton(MIL_ID MilGraphicContext, MIL_ID MilGraphicList, MIL_ID MilImage,
MIL_INT PosX, MIL_INT PosY,
map<MIL_INT, MIL_ID>& MapImageToDisplay,
map<MIL_INT, MIL_DOUBLE>& MapInitialZoom, MIL_INT* pRegionLabel);
void InitCameraViewDisplay(MIL_ID MilGraphicContextCameraView,
MIL_ID MilGraphicListCameraView, MIL_ID MilGraphicListSketch,
MIL_INT GlobalRegionLabel, bool ShowCalibrationAxis);
void ModelFinderProcess(MIL_ID MilSystem, MIL_ID MilGraphicContext, MIL_ID MilGraphicList,
MIL_ID MilImage, MIL_ID& MilModResult);
void MeasProcess(MIL_ID MilSystem, MIL_ID MilGraphicContext, MIL_ID MilGraphicList,
MIL_ID MilImage, MIL_DOUBLE& CircleCenterX,MIL_DOUBLE& CircleCenterY,
MIL_DOUBLE& CircleRadius);
void MetrologyProcess(MIL_ID MilSystem, MIL_ID MilGraphicContext, MIL_ID MilGraphicList,
const map<MIL_ID,MIL_DOUBLE>& ImageToProcessMapColor,
MIL_DOUBLE PosX, MIL_DOUBLE PosY);
bool MetrologyProcessStep(MIL_ID MilMetContext, MIL_ID MilMetResult,
MIL_INT ToleranceIndex, MIL_DOUBLE Angle, MIL_ID MilImage);
void CreatePolarImage(MIL_ID MilSystem, MIL_ID MilImageSource, MIL_ID& MilPolarImage,
MIL_ID& WrapCal,MIL_DOUBLE OffsetX, MIL_DOUBLE OffsetY,
MIL_DOUBLE StartAngle, MIL_DOUBLE EndAngle,
MIL_DOUBLE RMin, MIL_DOUBLE RMax);
void CreateLUTs(MIL_ID MilSystem, MIL_ID MilCalibration, MIL_ID* pMilLutX,
MIL_ID* pMilLutY, MIL_DOUBLE OffsetX, MIL_DOUBLE OffsetY,
MIL_DOUBLE StartAngle, MIL_DOUBLE EndAngle,
MIL_DOUBLE MinRadius, MIL_DOUBLE MaxRadius);
void OCRProcess(MIL_ID MilSystem, MIL_ID MilGraphicContext, MIL_ID MilGraphicList,
MIL_ID MilImage);
MIL_INT MFTYPE HookHandler(MIL_INT HookType, MIL_ID EventId, void* UserDataPtr);
struct SHookUserData
{
MIL_ID MilDisplay;
const map<MIL_INT, MIL_ID>* pMapImageToDisplay;
const map<MIL_INT, MIL_DOUBLE>* pMapInitialZoom;
};
int MosMain(void)
{
MIL_ID MilApplication,
MilSystem,
MilDisplay1,
MilDisplay2;
MappAlloc(M_NULL, M_DEFAULT, &MilApplication);
MsysAlloc(M_DEFAULT, M_SYSTEM_HOST, M_DEFAULT, M_DEFAULT, &MilSystem);
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay1);
MdispControl(MilDisplay1, M_TITLE, M_PTR_TO_DOUBLE(MIL_TEXT("Set-up")));
MdispAlloc(MilSystem, M_DEFAULT, MIL_TEXT("M_DEFAULT"), M_WINDOWED, &MilDisplay2);
MdispControl(MilDisplay2, M_TITLE, M_PTR_TO_DOUBLE(MIL_TEXT("Camera View")));
PrintHeader();
ShowMultiViewAnnotations(MilSystem, MilDisplay1, MilDisplay2);
MdispFree(MilDisplay1);
MdispFree(MilDisplay2);
MsysFree(MilSystem);
MappFree(MilApplication);
return 0;
}
void ShowMultiViewAnnotations(MIL_ID MilSystem, MIL_ID MilDisplay1, MIL_ID MilDisplay2)
{
MIL_ID MilImage1,
MilImage2,
MilImage3,
MilImage4,
MilImage4Child,
MilCorrectedImage4,
MilPolarImage,
MilCalibration1,
MilCalibration2,
MilCalibration3,
MilCalibration4,
MilWrapCal1,
MilImageSketch,
MilGraphicList1,
MilGraphicContext1,
MilGraphicList2,
MilGraphicContext2,
MilModResult;
MIL_DOUBLE CircleCenterX,
CircleCenterY,
CircleRadius;
MIL_INT RegionLabel,
GlobalRegionLabel;
map<MIL_ID, MIL_DOUBLE> ImageToProcessMapColor;
map<MIL_INT, MIL_ID> MapImageToDisplay;
map<MIL_INT, MIL_DOUBLE> MapInitialZoom;
SHookUserData DataStructure;
MosPrintf(MIL_TEXT("\nCalibration process, please wait"));
MbufRestore(IMAGE1, MilSystem, &MilImage1);
MbufRestore(IMAGE2, MilSystem, &MilImage2);
MbufRestore(IMAGE3, MilSystem, &MilImage3);
MbufRestore(IMAGE4, MilSystem, &MilImage4);
CreateCalibration(MilSystem, MilCalibration1, MilCalibration2, MilCalibration3,
MilCalibration4);
MosPrintf(MIL_TEXT("Done!\n\n"));
McalAssociate(MilCalibration1, MilImage1, M_DEFAULT);
McalAssociate(MilCalibration2, MilImage2, M_DEFAULT);
McalAssociate(MilCalibration3, MilImage3, M_DEFAULT);
McalAssociate(MilCalibration4, MilImage4, M_DEFAULT);
MbufRestore(IMAGESKETCH, MilSystem, &MilImageSketch);
MdispControl(MilDisplay1, M_WINDOW_RESIZE, M_DISABLE);
MdispControl(MilDisplay1, M_MOUSE_USE, M_DISABLE);
MdispControl(MilDisplay1, M_KEYBOARD_USE, M_DISABLE);
MdispZoom(MilDisplay2, SRC_TO_DISP_ZOOM, SRC_TO_DISP_ZOOM);
MdispControl(MilDisplay2, M_WINDOW_INITIAL_POSITION_X, DISPLAYED_IMAGE_WIDTH + OFFSET_BETWEEN_IMAGES);
MdispSelect(MilDisplay1, MilImageSketch);
MdispSelect(MilDisplay2, MilImage1);
MgraAllocList(MilSystem, M_DEFAULT, &MilGraphicList1);
MgraAlloc(MilSystem, &MilGraphicContext1);
MgraAllocList(MilSystem, M_DEFAULT, &MilGraphicList2);
MgraAlloc(MilSystem, &MilGraphicContext2);
MdispControl(MilDisplay1, M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicList1);
MdispControl(MilDisplay2, M_ASSOCIATED_GRAPHIC_LIST_ID, MilGraphicList2);
MdispControl(MilDisplay1, M_GRAPHIC_LIST_INTERACTIVE, M_ENABLE);
MgraControl(MilGraphicContext1, M_EDITABLE, M_DISABLE);
AddSelectableButton(MilGraphicContext1, MilGraphicList1, MilImage1, BUTTON1_POSX,
BUTTON1_POSY, MapImageToDisplay, MapInitialZoom, &RegionLabel);
GlobalRegionLabel = RegionLabel;
AddSelectableButton(MilGraphicContext1, MilGraphicList1, MilImage2, BUTTON2_POSX,
BUTTON2_POSY, MapImageToDisplay, MapInitialZoom, &RegionLabel);
AddSelectableButton(MilGraphicContext1, MilGraphicList1, MilImage3, BUTTON3_POSX,
BUTTON3_POSY, MapImageToDisplay, MapInitialZoom, &RegionLabel);
AddSelectableButton(MilGraphicContext1, MilGraphicList1, MilImage4, BUTTON4_POSX,
BUTTON4_POSY, MapImageToDisplay, MapInitialZoom, &RegionLabel);
MgraControlList(MilGraphicList1, M_LIST, M_DEFAULT, M_SELECTED_COLOR,
M_COLOR_DARK_GREEN);
MgraControlList(MilGraphicList1, M_LIST, M_DEFAULT, M_MULTIPLE_SELECTION, M_DISABLE);
ImageToProcessMapColor[MilImage1] = M_COLOR_CYAN;
ImageToProcessMapColor[MilImage2] = M_COLOR_RED;
ImageToProcessMapColor[MilImage3] = M_COLOR_YELLOW;
ImageToProcessMapColor[MilImage4] = M_COLOR_MAGENTA;
MgraControl(MilGraphicContext2, M_INPUT_UNITS, M_WORLD);
DataStructure.MilDisplay = MilDisplay2;
DataStructure.pMapImageToDisplay = &MapImageToDisplay;
DataStructure.pMapInitialZoom = &MapInitialZoom;
MgraHookFunction(MilGraphicList1, M_GRAPHIC_SELECTION_MODIFIED,
&HookHandler, &DataStructure);
if(MdispInquire(MilDisplay1, M_DISPLAY_TYPE, M_NULL) == M_AUXILIARY)
{
MosPrintf(MIL_TEXT("***NOTE: This example does not implement the interactivity\n"));
MosPrintf(MIL_TEXT(" with an auxiliary display.\n\n"));
}
MgraControlList(MilGraphicList1, M_GRAPHIC_LABEL(GlobalRegionLabel), M_DEFAULT,
M_GRAPHIC_SELECTED, M_TRUE);
MgraColor(MilGraphicContext2, M_COLOR_DARK_GREEN);
McalDraw(MilGraphicContext2, M_NULL, MilGraphicList2,M_DRAW_RELATIVE_COORDINATE_SYSTEM,
M_DEFAULT, M_DEFAULT);
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
InitCameraViewDisplay(MilGraphicContext2, MilGraphicList2, MilGraphicList1,
GlobalRegionLabel, true);
MgraColor(MilGraphicContext2, ImageToProcessMapColor[MilImage1]);
ModelFinderProcess(MilSystem, MilGraphicContext2, MilGraphicList2, MilImage1,
MilModResult);
MosPrintf(MIL_TEXT("The Model Finder tool locates the 2D model of an object in the\n"));
MosPrintf(MIL_TEXT("calibration plane of the image acquired by the blue camera.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
InitCameraViewDisplay(MilGraphicContext2, MilGraphicList2, MilGraphicList1,
GlobalRegionLabel, true);
MbufChild2d(MilImage4, CHILD_OFFSETX, CHILD_OFFSETY, CHILD_SIZEX, CHILD_SIZEY,
&MilImage4Child);
MilCorrectedImage4 = MbufClone(MilImage4, M_DEFAULT, M_DEFAULT, M_DEFAULT, M_DEFAULT,
M_DEFAULT, M_DEFAULT, &MilCorrectedImage4);
MbufClear(MilCorrectedImage4, 0);
McalTransformImage(MilImage4Child, MilCorrectedImage4, M_DEFAULT, M_DEFAULT,
M_FULL_CORRECTION, M_DEFAULT);
McalFixture(MilImage1, M_NULL, M_MOVE_RELATIVE, M_RESULT_MOD, MilModResult, 0,
M_DEFAULT, M_DEFAULT, M_DEFAULT);
McalFixture(MilImage2, M_NULL, M_MOVE_RELATIVE, M_RESULT_MOD, MilModResult, 0,
M_DEFAULT, M_DEFAULT, M_DEFAULT);
McalFixture(MilImage3, M_NULL, M_MOVE_RELATIVE, M_RESULT_MOD, MilModResult, 0,
M_DEFAULT, M_DEFAULT, M_DEFAULT);
McalFixture(MilImage4, M_NULL, M_MOVE_RELATIVE, M_RESULT_MOD, MilModResult, 0,
M_DEFAULT, M_DEFAULT, M_DEFAULT);
McalFixture(MilCorrectedImage4, M_NULL, M_MOVE_RELATIVE, M_RESULT_MOD, MilModResult, 0,
M_DEFAULT, M_DEFAULT, M_DEFAULT);
MgraColor(MilGraphicContext2, ImageToProcessMapColor[MilImage4]);
MeasProcess(MilSystem, MilGraphicContext2, MilGraphicList2, MilCorrectedImage4,
CircleCenterX, CircleCenterY, CircleRadius);
MosPrintf(MIL_TEXT("The Measurement tool is used to establish a circle in the\n"));
MosPrintf(MIL_TEXT("calibrated view acquired from the magenta camera.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
InitCameraViewDisplay(MilGraphicContext2, MilGraphicList2, MilGraphicList1,
GlobalRegionLabel, true);
MetrologyProcess(MilSystem, MilGraphicContext2, MilGraphicList2, ImageToProcessMapColor,
CircleCenterX, CircleCenterY);
McalAlloc(MilSystem, M_LINEAR_INTERPOLATION, M_DEFAULT, &MilWrapCal1);
CreatePolarImage(MilSystem, MilImage4, MilPolarImage, MilWrapCal1, CircleCenterX,
CircleCenterY, 20, 70, 0.5*CircleRadius, 1.1*CircleRadius);
MosPrintf(MIL_TEXT("The Metrology tool is used to validate geometric tolerances\n"));
MosPrintf(MIL_TEXT("in multiple calibrated views.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
InitCameraViewDisplay(MilGraphicContext2, MilGraphicList2, MilGraphicList1,
GlobalRegionLabel, false);
MgraColor(MilGraphicContext2, ImageToProcessMapColor[MilImage4]);
MapImageToDisplay[RegionLabel] = MilPolarImage;
MapInitialZoom[RegionLabel] = 1.0;
OCRProcess(MilSystem, MilGraphicContext2, MilGraphicList2, MilPolarImage);
MosPrintf(MIL_TEXT("The OCR tool is used to read a product number in the unwarped\n"));
MosPrintf(MIL_TEXT("calibrated view acquired from the magenta camera.\n\n"));
MosPrintf(MIL_TEXT("Press <Enter> to continue.\n\n"));
MosGetch();
MgraHookFunction(MilGraphicList1, M_GRAPHIC_SELECTION_MODIFIED+M_UNHOOK,
&HookHandler, &DataStructure);
MbufFree(MilPolarImage);
MbufFree(MilImage4Child);
MbufFree(MilImage1);
MbufFree(MilImage2);
MbufFree(MilImage3);
MbufFree(MilImage4);
MbufFree(MilCorrectedImage4);
MbufFree(MilImageSketch);
McalFree(MilCalibration1);
McalFree(MilCalibration2);
McalFree(MilCalibration3);
McalFree(MilCalibration4);
McalFree(MilWrapCal1);
MgraFree(MilGraphicContext1);
MgraFree(MilGraphicList1);
MgraFree(MilGraphicContext2);
MgraFree(MilGraphicList2);
MmodFree(MilModResult);
}
void CreateCalibration(MIL_ID MilSystem, MIL_ID& MilCalibration1, MIL_ID& MilCalibration2,
MIL_ID& MilCalibration3, MIL_ID& MilCalibration4)
{
MIL_ID MilImageGrid1,
MilImageGrid2,
MilImageGrid3,
MilImageGrid4,
MilImageGrid3D1;
McalAlloc(MilSystem, M_TSAI_BASED, M_DEFAULT, &MilCalibration1);
McalAlloc(MilSystem, M_TSAI_BASED, M_DEFAULT, &MilCalibration2);
McalAlloc(MilSystem, M_TSAI_BASED, M_DEFAULT, &MilCalibration3);
McalAlloc(MilSystem, M_TSAI_BASED, M_DEFAULT, &MilCalibration4);
MbufRestore(IMAGEGRID1 , MilSystem, &MilImageGrid1);
MbufRestore(IMAGEGRID3D1, MilSystem, &MilImageGrid3D1);
MbufRestore(IMAGEGRID2 , MilSystem, &MilImageGrid2);
MbufRestore(IMAGEGRID3 , MilSystem, &MilImageGrid3);
MbufRestore(IMAGEGRID4 , MilSystem, &MilImageGrid4);
McalGrid(MilCalibration1, MilImageGrid3D1, M_NULL, M_NULL, -GRID_THICKNESS, GRID_NB_ROWS,
GRID_NB_COLUMNS, GRID_ROW_SPACING, GRID_COLUMN_SPACING, M_DEFAULT,
M_CHESSBOARD_GRID);
McalGrid(MilCalibration1, MilImageGrid1, M_NULL, M_NULL, -GRID_THICKNESS, GRID_NB_ROWS,
GRID_NB_COLUMNS, GRID_ROW_SPACING, GRID_COLUMN_SPACING,
M_DISPLACE_CAMERA_COORD,M_CHESSBOARD_GRID);
MosPrintf(MIL_TEXT("."));
McalGrid(MilCalibration2, MilImageGrid2, M_NULL, M_NULL, -GRID_THICKNESS, GRID_NB_ROWS,
GRID_NB_COLUMNS, GRID_ROW_SPACING, GRID_COLUMN_SPACING, M_DEFAULT,
M_CHESSBOARD_GRID);
MosPrintf(MIL_TEXT("."));
McalGrid(MilCalibration3, MilImageGrid3, M_NULL, M_NULL, -GRID_THICKNESS, GRID_NB_ROWS,
GRID_NB_COLUMNS, GRID_ROW_SPACING, GRID_COLUMN_SPACING, M_DEFAULT,
M_CHESSBOARD_GRID);
MosPrintf(MIL_TEXT("."));
McalGrid(MilCalibration4, MilImageGrid4, M_NULL, M_NULL, -GRID_THICKNESS, GRID_NB_ROWS,
GRID_NB_COLUMNS, GRID_ROW_SPACING, GRID_COLUMN_SPACING, M_DEFAULT,
M_CHESSBOARD_GRID);
MosPrintf(MIL_TEXT("."));
MbufFree(MilImageGrid1);
MbufFree(MilImageGrid2);
MbufFree(MilImageGrid3);
MbufFree(MilImageGrid4);
MbufFree(MilImageGrid3D1);
}
void AddSelectableButton(MIL_ID MilGraphicContext, MIL_ID MilGraphicList, MIL_ID MilImage,
MIL_INT PosX, MIL_INT PosY,
map<MIL_INT, MIL_ID>& MapImageToDisplay,
map<MIL_INT, MIL_DOUBLE>& MapInitialZoom, MIL_INT* pRegionLabel)
{
MgraColor(MilGraphicContext, M_COLOR_BRIGHT_GRAY);
MgraArcFill(MilGraphicContext, MilGraphicList, PosX, PosY, BUTTON_RADIUS,
BUTTON_RADIUS, 0, 360 );
MgraInquireList(MilGraphicList, M_LIST, M_DEFAULT, M_LAST_LABEL, pRegionLabel);
MapImageToDisplay[*pRegionLabel] = MilImage;
MapInitialZoom [*pRegionLabel] = SRC_TO_DISP_ZOOM;
}
void InitCameraViewDisplay(MIL_ID MilGraphicContextCameraView,
MIL_ID MilGraphicListCameraView, MIL_ID MilGraphicListSketch,
MIL_INT GlobalRegionLabel, bool ShowCalibrationAxis)
{
MgraClear(MilGraphicContextCameraView, MilGraphicListCameraView);
MgraControlList(MilGraphicListSketch, M_ALL, M_DEFAULT, M_GRAPHIC_SELECTED, M_FALSE);
MgraControlList(MilGraphicListSketch, M_GRAPHIC_LABEL(GlobalRegionLabel), M_DEFAULT,
M_GRAPHIC_SELECTED, M_TRUE);
if(ShowCalibrationAxis)
{
MgraColor(MilGraphicContextCameraView, M_COLOR_DARK_GREEN);
McalDraw(MilGraphicContextCameraView, M_NULL, MilGraphicListCameraView,
M_DRAW_RELATIVE_COORDINATE_SYSTEM + M_DRAW_FRAME, M_DEFAULT, M_DEFAULT);
}
}
void ModelFinderProcess(MIL_ID MilSystem, MIL_ID MilGraphicContext, MIL_ID MilGraphicList,
MIL_ID MilImage, MIL_ID& MilModResult)
{
MIL_ID MilModContext;
MIL_DOUBLE PosX, PosY;
MmodRestore(MODCONTEXT, MilSystem, M_WITH_CALIBRATION, &MilModContext);
MmodAllocResult(MilSystem, M_DEFAULT, &MilModResult);
MmodPreprocess(MilModContext, M_DEFAULT);
MmodFind(MilModContext, MilImage, MilModResult);
MmodGetResult(MilModResult, 0, M_POSITION_X, &PosX);
MmodGetResult(MilModResult, 0, M_POSITION_Y, &PosY);
MmodDraw(MilGraphicContext, MilModResult,MilGraphicList,
M_DRAW_EDGES, M_ALL, M_DEFAULT);
MmodFree(MilModContext);
}
static const MIL_DOUBLE CLIP_COND_LOW = 64;
static const MIL_DOUBLE CLIP_CON_HIGH = 160;
static const MIL_DOUBLE CLIP_WRITE_LOW = 64;
static const MIL_DOUBLE CLIP_WRITE_HIGH = 160;
static const MIL_INT FILTER_SMOOTHNESS = 100;
void MeasProcess(MIL_ID MilSystem, MIL_ID MilGraphicContext, MIL_ID MilGraphicList,
MIL_ID MilImage, MIL_DOUBLE& CircleCenterX, MIL_DOUBLE& CircleCenterY,
MIL_DOUBLE& CircleRadius)
{
MIL_ID MilMeasMarker;
MimClip(MilImage, MilImage, M_OUT_RANGE, CLIP_COND_LOW, CLIP_CON_HIGH,
CLIP_WRITE_LOW, CLIP_WRITE_HIGH);
MimConvolve(MilImage, MilImage, M_DERICHE_FILTER(M_SHARPEN, FILTER_SMOOTHNESS));
MmeasRestoreMarker(MEASCONTEXT,MilSystem,M_DEFAULT,&MilMeasMarker);
MmeasSetMarker(MilMeasMarker, M_SEARCH_REGION_INPUT_UNITS, M_WORLD, M_NULL);
MmeasSetMarker(MilMeasMarker, M_RING_CENTER, 0, 0);
MmeasSetMarker(MilMeasMarker, M_RING_RADII, 24, 26);
MmeasFindMarker(M_DEFAULT,MilImage,MilMeasMarker,M_DEFAULT);
MmeasDraw(MilGraphicContext, MilMeasMarker, MilGraphicList, M_DRAW_EDGES, M_DEFAULT,
M_RESULT);
MmeasGetResult(MilMeasMarker, M_POSITION, &CircleCenterX, &CircleCenterY);
MmeasGetResult(MilMeasMarker, M_RADIUS , &CircleRadius , M_NULL );
MmeasFree(MilMeasMarker);
}
void MetrologyProcess(MIL_ID MilSystem, MIL_ID MilGraphicContext, MIL_ID MilGraphicList,
const map<MIL_ID,MIL_DOUBLE>& ImageToProcessMapColor,
MIL_DOUBLE PosX, MIL_DOUBLE PosY)
{
MIL_ID MilMetContext,
MilMetResult;
MIL_DOUBLE AngleToProcess,
NumberOfTolerance;
MIL_INT AngleIndex,
ToleranceIndex;
bool success;
map<MIL_ID,MIL_DOUBLE>::const_iterator
iterImage;
MmetAllocResult(MilSystem, M_DEFAULT, &MilMetResult);
MmetRestore(METCONTEXT, MilSystem, M_WITH_CALIBRATION, &MilMetContext);
MmetControl(MilMetContext, M_GLOBAL_FRAME, M_POSITION_X, PosX);
MmetControl(MilMetContext, M_GLOBAL_FRAME, M_POSITION_Y, PosY);
MmetInquire(MilMetContext, M_DEFAULT, M_NUMBER_OF_TOLERANCES, &NumberOfTolerance);
success = false;
AngleToProcess = 0.0;
for(AngleIndex = 0; AngleIndex < NB_ANGLE_STEP; AngleIndex++)
{
for(ToleranceIndex = 0; ToleranceIndex < NumberOfTolerance; ToleranceIndex++)
{
iterImage = ImageToProcessMapColor.begin();
while(iterImage != ImageToProcessMapColor.end())
{
success = MetrologyProcessStep(MilMetContext, MilMetResult, ToleranceIndex,
AngleToProcess, iterImage->first);
if(success)
{
MIL_DOUBLE AnnotationColor = iterImage->second;
MgraColor(MilGraphicContext, AnnotationColor);
MmetDraw(MilGraphicContext, MilMetResult, MilGraphicList,
M_DRAW_TOLERANCE_FEATURES, M_TOLERANCE_INDEX(ToleranceIndex),
M_DEFAULT);
MmetDraw(MilGraphicContext, MilMetResult, MilGraphicList, M_DRAW_TOLERANCE,
M_TOLERANCE_INDEX(ToleranceIndex), M_DEFAULT);
break;
}
iterImage++;
}
}
AngleToProcess += ANGLE_STEP;
}
MmetFree(MilMetContext);
MmetFree(MilMetResult);
}
bool MetrologyProcessStep(MIL_ID MilMetContext, MIL_ID MilMetResult,
MIL_INT ToleranceIndex, MIL_DOUBLE Angle, MIL_ID MilImage)
{
MIL_DOUBLE Status;
MmetControl( MilMetContext, M_GLOBAL_FRAME, M_ANGLE, Angle);
MmetCalculate(MilMetContext, MilImage, MilMetResult, M_DEFAULT);
MmetGetResult(MilMetResult, M_TOLERANCE_INDEX(ToleranceIndex), M_STATUS, &Status);
if(Status == M_PASS)
return true;
else
return false;
}
void CreatePolarImage(MIL_ID MilSystem, MIL_ID MilImageSource, MIL_ID& MilPolarImage,
MIL_ID& WrapCal, MIL_DOUBLE OffsetX, MIL_DOUBLE OffsetY,
MIL_DOUBLE StartAngle, MIL_DOUBLE EndAngle,
MIL_DOUBLE RMin, MIL_DOUBLE RMax)
{
MIL_ID MilLutX, MilLutY;
MIL_INT PolarSizeX, PolarSizeY;
CreateLUTs(MilSystem, MilImageSource, &MilLutX, &MilLutY, OffsetX, OffsetY, StartAngle,
EndAngle, RMin, RMax);
MbufInquire(MilLutX, M_SIZE_X, &PolarSizeX);
MbufInquire(MilLutX, M_SIZE_Y, &PolarSizeY);
MilPolarImage = MbufAlloc2d(MilSystem, PolarSizeX, PolarSizeY, 8+M_UNSIGNED,
M_IMAGE+M_PROC+M_DISP, M_NULL);
MbufClear(MilPolarImage, 0.0);
MimWarp(MilImageSource, MilPolarImage, MilLutX, MilLutY,
M_WARP_LUT + M_FIXED_POINT + NB_FIXED_POINT, M_BICUBIC);
McalWarp(MilImageSource, WrapCal, MilLutX, MilLutY, M_NULL, M_NULL, M_NULL, M_NULL,
50, 50, M_WARP_LUT+M_FIXED_POINT+NB_FIXED_POINT, M_DEFAULT);
McalAssociate(WrapCal, MilPolarImage, M_DEFAULT);
MbufFree(MilLutX);
MbufFree(MilLutY);
}
void CreateLUTs(MIL_ID MilSystem, MIL_ID MilCalibration, MIL_ID* pMilLutX,
MIL_ID* pMilLutY, MIL_DOUBLE OffsetX, MIL_DOUBLE OffsetY,
MIL_DOUBLE StartAngle, MIL_DOUBLE EndAngle,
MIL_DOUBLE MinRadius, MIL_DOUBLE MaxRadius)
{
MIL_INT SizeX, SizeY;
SizeX = static_cast<MIL_INT>(( (EndAngle - StartAngle) * NB_PIXELS_PER_DEGREE ));
SizeY = static_cast<MIL_INT>(ceil( (MaxRadius - MinRadius) * NB_PIXELS_PER_RADIAL_MM ));
if(SizeX > SRC_IMAGE_WIDTH)
{
SizeY = MIL_INT(ceil(MIL_DOUBLE(SizeY*SRC_IMAGE_WIDTH) / SizeX));
SizeX = SRC_IMAGE_WIDTH;
}
MbufAlloc2d(MilSystem, SizeX, SizeY, 32+M_SIGNED, M_LUT, pMilLutX);
MbufAlloc2d(MilSystem, SizeX, SizeY, 32+M_SIGNED, M_LUT, pMilLutY);
MIL_INT NbPixels = SizeX * SizeY;
MIL_DOUBLE* LutCoordsX = new MIL_DOUBLE[NbPixels];
MIL_DOUBLE* LutCoordsY = new MIL_DOUBLE[NbPixels];
MIL_INT32* LutIntCoords = new MIL_INT32 [NbPixels];
MIL_DOUBLE* pCurX = LutCoordsX;
MIL_DOUBLE* pCurY = LutCoordsY;
MIL_DOUBLE StepR = (MaxRadius - MinRadius) / SizeY;
MIL_DOUBLE StartTheta = -EndAngle * TO_RAD;
MIL_DOUBLE StepTheta =(EndAngle - StartAngle) * TO_RAD / SizeX;
MIL_DOUBLE CurR = MinRadius + 0.5*StepR;
for (MIL_INT y = 0; y < SizeY; ++y)
{
MIL_DOUBLE CurTheta = StartTheta - 0.5*StepTheta;
for (MIL_INT x = 0; x < SizeX; ++x)
{
*pCurX++ = CurR * cos(CurTheta) +OffsetX;
*pCurY++ = CurR * sin(CurTheta) +OffsetY;
CurTheta -= StepTheta;
}
CurR += StepR;
}
McalTransformCoordinateList(MilCalibration, M_WORLD_TO_PIXEL, NbPixels,
LutCoordsX, LutCoordsY, LutCoordsX, LutCoordsY);
MIL_DOUBLE Factor = static_cast<MIL_DOUBLE>(1 << NB_FIXED_POINT);
for (MIL_INT i = 0; i < NbPixels; ++i)
{
MIL_DOUBLE MultValue = Factor * LutCoordsX[i];
LutIntCoords[i] = static_cast<MIL_INT32>(MultValue > 0.0 ? MultValue + 0.5 : MultValue - 0.5);
}
MbufPut(*pMilLutX, LutIntCoords);
for (MIL_INT i = 0; i < NbPixels; ++i)
{
MIL_DOUBLE MultValue = Factor * LutCoordsY[i];
LutIntCoords[i] = static_cast<MIL_INT32>(MultValue > 0.0 ? MultValue + 0.5 : MultValue - 0.5);
}
MbufPut(*pMilLutY, LutIntCoords);
delete [] LutIntCoords;
delete [] LutCoordsY;
delete [] LutCoordsX;
}
void OCRProcess(MIL_ID MilSystem, MIL_ID MilGraphicContext, MIL_ID MilGraphicList,
MIL_ID MilImage)
{
MIL_ID MilOcrContext,
MilOcrResult;
MIL_TEXT_CHAR String[STRING_LENGTH];
MIL_DOUBLE PosX [STRING_LENGTH];
MIL_DOUBLE PosY [STRING_LENGTH];
MIL_DOUBLE SizeX [STRING_LENGTH];
MIL_DOUBLE SizeY [STRING_LENGTH];
MocrRestoreFont(OCRCONTEXT, M_DEFAULT, MilSystem, &MilOcrContext);
MocrAllocResult(MilSystem, M_DEFAULT, &MilOcrResult);
MocrReadString(MilImage, MilOcrContext, MilOcrResult);
MocrDraw(MilGraphicContext, MilOcrResult, MilGraphicList, M_DRAW_STRING_CHAR_BOX,
M_DEFAULT, M_NULL, M_DEFAULT);
MocrDraw(MilGraphicContext, MilOcrResult, MilGraphicList, M_DRAW_STRING_CHAR_POSITION,
M_DEFAULT, M_NULL, M_DEFAULT);
MocrGetResult(MilOcrResult, M_STRING, String);
MocrControl(MilOcrResult, M_RESULT_OUTPUT_UNITS, M_WORLD);
MocrGetResult(MilOcrResult, M_CHAR_POSITION_X, PosX);
MocrGetResult(MilOcrResult, M_CHAR_POSITION_Y, PosY);
MocrGetResult(MilOcrResult, M_CHAR_SIZE_X, SizeX);
MocrGetResult(MilOcrResult, M_CHAR_SIZE_Y, SizeY);
MgraFont(MilGraphicContext, M_FONT_DEFAULT_MEDIUM);
MgraText(MilGraphicContext, MilGraphicList,
PosX[0]-1.5*SizeX[0], PosY[0]-1.5*SizeY[0],
String);
MocrFree(MilOcrContext);
MocrFree(MilOcrResult);
}
MIL_INT MFTYPE HookHandler(MIL_INT HookType, MIL_ID EventId, void* UserDataPtr)
{
const SHookUserData& UserData = *((const SHookUserData*)(UserDataPtr));
MIL_INT ObjectLabelSelected;
MgraGetHookInfo(EventId, M_GRAPHIC_LABEL_VALUE, &ObjectLabelSelected);
if(ObjectLabelSelected == M_NO_LABEL)
{
MgraGetHookInfo(EventId, M_GRAPHIC_LABEL_VALUE_DESELECTED, &ObjectLabelSelected);
}
if(ObjectLabelSelected != M_NO_LABEL)
{
MIL_DOUBLE Zoom = UserData.pMapInitialZoom->find(ObjectLabelSelected)->second;
MdispZoom(UserData.MilDisplay, Zoom, Zoom);
MIL_ID ImageToDisplay = UserData.pMapImageToDisplay->find(ObjectLabelSelected)->second;
MdispSelect(UserData.MilDisplay, ImageToDisplay);
}
return 0;
}