/* * ***************************************************************** * * * * * Copyright (c) Fast Corporation, 1997 * * * * * * All Rights Reserved. Unpublished rights reserved under * * * the copyright laws of the Japan. * * * * * * The software contained on this media is proprietary to * * * and embodies the confidential technology of Fast * * * Corporation. Possession, use, duplication or * * * dissemination of the software and media is authorized only * * * pursuant to a valid written license from Fast Corporation. * * * * * ***************************************************************** */ /* CSC90X グレイサーチ後1点エッジ検出(XYθ角度補正) GSEDGE.C */ /*[作成者]H.Yagi */ /* 目的: 関数: 履歴: Ver 1.0 97/08/26 Ver 1.1 99/03/26 座標変換処理の不具合を修正 Ver 2.0 00/05/16 注記:m_menu.hをインクルードして下さい。 m_menu.c及びm_note.cをリンクして下さい。 */ /* * Include compiler runtime library */ #include #include /* * Include CSC90X library */ #include "f_stdlib.h" #include "f_time.h" #include "f_gui.h" #include "f_stdio.h" #include "f_graph.h" #include "f_image.h" #include "f_pinf.h" #include "f_video.h" #include "f_system.h" #include "f_search.h" #include "f_math.h" #include "f_gray.h" /* * Include CSC90X common local */ #include "m_menu.h" /* * プロトタイプ宣言 */ void main( void ); void main_menu_disp( void ); void disp_help( void ); void para_set( void ); void gs_joken( void ); void ptnreg( void ); void line( void ); void kijun_ctrl( void ); int kijun( void ); void exec( void ); int search( int *, int *, int *, int *, int *, int * ); int d4i5_d( double ); void line_set( int *, int *, int *, int * ); int area_chk( int, int, int, int ); void draw_cros( int, int ); void calc_WxWy( double, double, double, double, double, double, double *, double * ); void calc_CVxCVy( double, double, double, double, double, double, double *, double * ); extern int message_note( void ); /* * メニュー項目 */ #define LANG_N 2 #define MAIN_MENU_N 4 static const char *str_main_menu[MAIN_MENU_N][LANG_N] = { { " SET ", " 設 定 " }, { " EXEC ", " 実 行 " }, { " PROC3 ", " 処理3 " }, { " PROC4 ", " 処理4 " } }; #define INIT_CUR_POS_X 255 #define INIT_CUR_POS_Y 239 #define OK 100 static char *select[3] = { "通常" , "高 " , "超高" }; int sts; int name1, name2; /* グレイパタン名称 */ int rtn_code; int edge_mode; int edge_minmaxdiff; int xa, ya, va, xb, yb, vb; /*基準位置サーチ*/ int wwxa, wwya, wwva, wwxb, wwyb, wwvb; /*ワークサーチ*/ int exs, eys, exe, eye; int wwexs, wweys, wwexe, wweye; static double Wx[2], Wy[2]; static double CVx[2], CVy[2]; int seido; /* 精度 */ int complex; /* 複雑度 */ int mid_lower; /* 途中下限値 */ int last_lower; /* 最終下限値 */ /* * メイン */ void main( void ) { int sts; int xpos; int ypos; int s_xpos; int s_ypos; /* 初期メッセージ表示 */ if( NORMAL_RETURN != message_note() ) return; /* パラメータ初期化 */ xpos = INIT_CUR_POS_X; ypos = INIT_CUR_POS_Y; s_xpos = INIT_CUR_POS_X; s_ypos = INIT_CUR_POS_Y; name1 = 0x314e5450; /* PTN1 */ name2 = 0x324e5450; /* PTN2 */ edge_mode = 1; edge_minmaxdiff = 0; /* サーチ・パラメータ初期化 */ seido = 1; complex = 1; mid_lower = 5000; last_lower = 7000; /* 入力ビデオ制御 */ Lib_input_video_control( GRAY_PLANE ); /* ビデオ出力表示項目制御 */ Lib_display_control( GRAY_PLANE | LINE_PLANE | CHAR_PLANE ); /* ビデオ表示項目クリア */ Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); /* カーソル初期化 */ Lib_init_cursor(); /* メインメニューの表示 */ main_menu_disp(); /* マウスカーソルの表示 */ Lib_draw_cursor( INIT_CUR_POS_X, INIT_CUR_POS_Y ); if ( NORMAL_RETURN == Lib_gs_open_data_file( CONTINUE_PTN_AREA ) ) /*GRAY_PTN.SYSを使用する*/ { /* メニュー制御 */ for (;;) { sts = 0; /* マウス位置読みとり */ sts = Lib_see_current_position( &xpos, &ypos ); if ( s_xpos != xpos || s_ypos != ypos ) { /* マウス表示位置移動 */ Lib_move_cursor( xpos, ypos ); s_xpos = xpos; s_ypos = ypos; } /* 処理振り分け */ if( CURSOR_EXECUTE == sts ) { if ( xpos > MENU_1_XS && xpos < MENU_1_XE && ypos > MENU_1_YS && ypos < MENU_1_YE ) { Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); para_set(); /* 設定 */ main_menu_disp(); } else if ( xpos > MENU_2_XS && xpos < MENU_2_XE && ypos > MENU_2_YS && ypos < MENU_2_YE ) { Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); exec(); /* 実行 */ main_menu_disp(); } else if ( xpos > MENU_H_XS && xpos < MENU_H_XE && ypos > MENU_H_YS && ypos < MENU_H_YE ) { Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); disp_help(); /* ヘルプ */ main_menu_disp(); } else if ( xpos > MENU_E_XS && xpos < MENU_E_XE && ypos > MENU_E_YS && ypos < MENU_E_YE ) { break; /* 終了 */ } } } Lib_gs_close_data_file(); } else Lib_display_message( 100, 200, "エラー", "パタンエリアは使用できません" ); } /* * メインメニュー表示 */ static void main_menu_disp( void ) { int iLanguage; /* 日本語/英語表示文字列切替情報取得 */ iLanguage = Lib_get_disp_language(); /* 整列キー表示 -> ( m_menu.c ) */ SUB_menu_disp4he( (char *)str_main_menu[0][iLanguage], (char *)str_main_menu[1][iLanguage], (char *)NULL, (char *)NULL ); } /* * ヘルプ表示 */ static void disp_help( void ) { Lib_chrdisp( 10, 8, "【機能\概要】 GSEDGE" ); Lib_chrdisp( 11, 10, "<1点エッジ検出ラインアライメント>" ); Lib_chrdisp( 11, 12, "2箇所のグレーサーチ後、エッジ検出ラインを" ); Lib_chrdisp( 11, 14, "xyθズレに合わせてアライメントしエッジ検" ); Lib_chrdisp( 11, 16, "出を行う。" ); } /***********************************************************************/ /* パラメータ設定    */ /***********************************************************************/ void para_set( void ) { int pad_level; int status_flag; int rtn; pad_level = Lib_view_open(); Lib_view_set_title( pad_level, "パラメータ" ); Lib_view_set_null( pad_level, 5, 5, "サーチ条件", 0 ); Lib_view_set_null( pad_level, 5, 35, "パタン登録", 1 ); Lib_view_set_null( pad_level, 5, 65, "ライン設定", 2 ); Lib_view_set_null( pad_level, 5, 95, "基準値計測", 3 ); Lib_view_set_size( pad_level, 30, 30, 5, 5 ); Lib_draw_command( pad_level ); for ( status_flag = ON; OFF != status_flag; ) { if ( ERROR_RETURN != ( rtn = Lib_process_command( pad_level ) ) ) { switch( rtn ) { case 0:/* サーチ条件 */ gs_joken(); break; case 1:/* パタン登録 */ ptnreg(); break; case 2:/* ライン設定 */ line(); break; case 3:/* 基準値計測 */ kijun_ctrl(); break; case 102:/* 終了 */ status_flag = OFF; break; } } } Lib_erase_command( pad_level ); Lib_view_close( pad_level ); } /************************************************************************/ /* サーチ条件設定 */ /************************************************************************/ void gs_joken( void ) { int pad_level_gs; PVAL value_gs[4]; int no; int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); pad_level_gs = Lib_view_open(); Lib_view_set_title( pad_level_gs, "サーチ条件" ); Lib_view_set_select ( pad_level_gs, 5, 5, "精度" , seido - 1, 3 , select, 0 ); Lib_view_set_uniq_numeral( pad_level_gs, 5, 35, "複雑度" , complex, 1, 9, 1 ); Lib_view_set_uniq_numeral( pad_level_gs, 5, 65, "途中下限値", mid_lower , 1000, 9999 , 2 ); Lib_view_set_uniq_numeral( pad_level_gs, 5, 95, "最終下限値", last_lower, 1000, 9999 , 3 ); Lib_view_set_size( pad_level_gs, 100, 130, 35, 5 ); Lib_draw_menu( pad_level_gs ); if( ERROR_RETURN != ( no = Lib_process_menu( pad_level_gs, value_gs ) ) ) { switch( no ) { case 101: /*「実行」が選択された */ seido = value_gs[0].select_type + 1; complex = value_gs[1].value_type; mid_lower = value_gs[2].value_type; last_lower = value_gs[3].value_type; break; case 102: /*「取り消し」が選択された */ break; } } Lib_erase_menu( pad_level_gs ); Lib_view_close( pad_level_gs ); Lib_recover_plane( idc ); } /***********************************************************************/ /* パタン登録      */ /***********************************************************************/ void ptnreg( void ) { int rtn; int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); Lib_freeze( TRANSMIT ); /* サーチパタン1登録 */ if ( NORMAL_RETURN == ( rtn = Lib_gs_defpat( name1 ) ) ) { /* サーチパタン2登録 */ if ( -1 == ( rtn = Lib_gs_defpat( name2 ) ) ) Lib_display_message( 150, 200, "エラー", "既に200個登録済みです" ); else if ( -2 == rtn ) Lib_display_message( 150, 200, "エラー", "パタン格納領域不足です" ); } else if ( -1 == rtn ) Lib_display_message( 150, 200, "エラー", "既に200個登録済みです" ); else if ( -2 == rtn ) Lib_display_message( 150, 200, "エラー", "パタン格納領域不足です" ); Lib_freerun(); Lib_recover_plane( idc ); } /***********************************************************************/ /* ライン設定      */ /***********************************************************************/ void line( void ) { int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); line_set( &exs, &eys, &exe, &eye ); /*ライン設定*/ Lib_drawline( exs, eys, exe, eye ); /*ライン表示*/ Lib_recover_plane( idc ); } /***********************************************************************/ /* 基準値計測制御    */ /***********************************************************************/ void kijun_ctrl( void ) { int status; int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); /* 基準値計測 */ if ( OK == ( status = kijun() ) ) { Lib_drawline( exs, eys, exe, eye ); /*エッジ検出ライン表示*/ Lib_display_keyinput( 460, 0, "確認" ); } else if ( 0 == status ) Lib_display_message( 100, 200, "エラー", "対象物がありません" ); else if ( -1 == status ) Lib_display_message( 100, 200, "エラー", "パタンが登録されていません" ); Lib_cls( ( GRAY_PLANE | CHAR_PLANE | LINE_PLANE ), BLACK_COLOR ); Lib_recover_plane( idc ); } /***********************************************************************/ /* 基準値計測      */ /***********************************************************************/ int kijun( void ) { int status; Lib_freeze( TRANSMIT ); Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); /* アライメント(グレイサーチ)の実行 */ if ( OK == ( status = search( &xa, &ya, &va, &xb, &yb, &vb ) ) ) { xa = d4i5_d( (double)xa / 10.0 ); ya = d4i5_d( (double)ya / 10.0 ); xb = d4i5_d( (double)xb / 10.0 ); yb = d4i5_d( (double)yb / 10.0 ); calc_WxWy( (double)xa, (double)ya, (double)xb, (double)yb, (double)exs, (double)eys, &Wx[0], &Wy[0] ); calc_WxWy( (double)xa, (double)ya, (double)xb, (double)yb, (double)exe, (double)eye, &Wx[1], &Wy[1] ); } Lib_freerun(); return( status ); } /************************************************************************/ /* グレイサーチ実行    */ /************************************************************************/ int search( xa, ya, va, xb, yb, vb ) int *xa; int *ya; int *va; int *xb; int *yb; int *vb; { int Rslt_Buf[3]; int rtn; /* サーチ実行1 */ if ( 0 < ( rtn = Lib_gs_search( ON_DISPLAY, name1, 1, seido, complex, mid_lower, last_lower, Rslt_Buf ) ) ) { *xa = Rslt_Buf[0]; *ya = Rslt_Buf[1]; *va = Rslt_Buf[2]; /* サーチ実行2 */ if ( 0 < ( rtn = Lib_gs_xsearch( SAME_IMAGE, ON_DISPLAY, name2, 1, seido, complex, mid_lower, last_lower, Rslt_Buf ) ) ) { *xb = Rslt_Buf[0]; *yb = Rslt_Buf[1]; *vb = Rslt_Buf[2]; rtn = OK; } } return( rtn ); } /***********************************************************************/ /* 入力数値(double)の4捨5入した数値(int)を返す */ /***********************************************************************/ int d4i5_d( double in ) { int out; if ( in >= 0.0 ) out = (int)( in + 0.5 ); else out = (int)( in - 0.5 ); return( out ); } /************************************************************************/ /* クロスマーク表示 */ /************************************************************************/ void draw_cros( x, y ) int x, y; { Lib_drawline( x-5, y, x+5, y ); Lib_drawline( x, y-5, x, y+5 ); } /***********************************************************************/ /* 計測ラインの設定          */ /***********************************************************************/ void line_set( xs, ys, xe, ye ) int *xs; /* 出力:始点x */ int *ys; /* 出力:始点y */ int *xe; /* 出力:終点x */ int *ye; /* 出力:終点y */ { /*注釈: 始点から水平or垂直方向にライン設定します*/ int sts, area; int xpos, ypos; int s_xpos, s_ypos; *xs = *ys = *xe = *ye = 0; xpos = s_xpos = 255; ypos = s_ypos = 240; Lib_draw_cursor( xpos, ypos ); Lib_chrdisp( 1, 30, "始点の指定" ); for (;;) { sts = 0; sts = Lib_see_current_position( &xpos, &ypos ); /* マウス位置読みとり */ if ( CURSOR_EXECUTE == sts ) { *xs = xpos; *ys = ypos; break; } if ( xpos != s_xpos || ypos != s_ypos ) { s_xpos = xpos; s_ypos = ypos; Lib_move_cursor( xpos, ypos ); } } xpos = s_xpos = *xs; ypos = s_ypos = *ys; Lib_draw_cursor( xpos, ypos ); Lib_chrdisp( 1, 30, "終点の指定" ); for (;;) { sts = 0; sts = Lib_see_current_position( &xpos, &ypos ); /* マウス位置読みとり */ if ( xpos != s_xpos || ypos != s_ypos ) { area = area_chk( *xs, *ys, xpos, ypos ); if ( area == 2 || area == 4 ) { Lib_erasline( *xs, *ys, *xe, *ye ); Lib_drawline( *xs, *ys, xpos, *ys ); Lib_move_cursor( xpos, *ys ); *xe = xpos; *ye = *ys; s_xpos = xpos; s_ypos = ypos; } else if ( area == 1 || area == 3 ) { Lib_erasline( *xs, *ys, *xe, *ye ); Lib_drawline( *xs, *ys, *xs, ypos ); Lib_move_cursor( *xs, ypos ); *xe = *xs; *ye = ypos; s_xpos = xpos; s_ypos = ypos; } } if ( CURSOR_EXECUTE == sts ) { Lib_chrdisp( 1, 30, " " ); break; } } } /***********************************************************************/ /* 4方向エリア判別          */ /***********************************************************************/ /* 基点 :kx, ky */ /* ワーク点:wx, wy */ /*(戻り値) 0 : 基点とワーク点は同位置 */ /* 1 : 基点から見て上方向にワーク点がある */ /* 2 : 基点から見て左方向にワーク点がある */ /* 3 : 基点から見て下方向にワーク点がある */ /* 4 : 基点から見て右方向にワーク点がある */ int area_chk( kx, ky, wx, wy ) int kx, ky; /* 入力:基点座標 */ int wx, wy; /* 入力:ワーク座標 */ { if ( wx == kx && wy == ky ) return( 0 ); if ( wx > kx ) /* 右半分側にある */ { if ( wy < ky ) /* 右上側にある */ { if ( ( wx - kx ) > ( ky - wy ) ) return( 4 ); else return( 1 ); } else /* 右下側にある */ { if ( ( wx - kx ) > ( wy - ky ) ) return( 4 ); else return( 3 ); } } else /* 左半分側にある */ { if ( wy < ky ) /* 左上側にある */ { if ( ( kx - wx ) > ( ky - wy ) ) return( 2 ); else return( 1 ); } else /* 左下側にある */ { if ( ( kx - wx ) > ( wy - ky ) ) return( 2 ); else return( 3 ); } } } /***********************************************************************/ /* 実行         */ /***********************************************************************/ void exec( void ) { char ss[50]; int status; int xx, yy; double dx, dy; Lib_strtclk_count(); /*タイマーカウントモードのスタート*/ Lib_freeze( TRANSMIT ); /* アライメント(グレイサーチ)の実行 */ if ( OK == ( status = search( &wwxa, &wwya, &wwva, &wwxb, &wwyb, &wwvb ) ) ) { wwxa = d4i5_d( (double)wwxa / 10.0 ); wwya = d4i5_d( (double)wwya / 10.0 ); wwxb = d4i5_d( (double)wwxb / 10.0 ); wwyb = d4i5_d( (double)wwyb / 10.0 ); /* エッジ検出ライン始点/終点座標の座標変換 */ calc_CVxCVy( (double)wwxa, (double)wwya, (double)wwxb, (double)wwyb, Wx[0], Wy[0], &CVx[0], &CVy[0] ); calc_CVxCVy( (double)wwxa, (double)wwya, (double)wwxb, (double)wwyb, Wx[1], Wy[1], &CVx[1], &CVy[1] ); wwexs = d4i5_d( CVx[0] ); wweys = d4i5_d( CVy[0] ); wwexe = d4i5_d( CVx[1] ); wweye = d4i5_d( CVy[1] ); /*サーチ位置に円マーク表示*/ Lib_drawcircle( wwxa, wwya, 10 ); /*ワーク*/ Lib_drawcircle( wwxb, wwyb, 10 ); /*変換後のエッジ検出ライン表示*/ Lib_line( wwexs, wweys, wwexe, wweye, SOLID_LINE ); Lib_drawcircle( wwexs, wweys, 2 ); Lib_drawcircle( wwexe, wweye, 2 ); /*エッジ検出*/ if( NORMAL_RETURN == ( Lib_edge_pos_xy( wwexs, wweys, wwexe, wweye, edge_mode, edge_minmaxdiff, CURRENT_MEMORY, &xx, &yy ))) { dx = (double)xx / 16.0; dy = (double)yy / 16.0; draw_cros( d4i5_d( dx ), d4i5_d( dy ) ); Lib_sprintf( ss, "エッジ座標:X=%.1lf, Y=%.1lf", dx, dy ); Lib_chrdisp( 1, 30, ss ); Lib_time_disp( 0, 0 ); Lib_display_keyinput( 460, 0, "確認" ); } else Lib_display_message( 0, 450, "エラー", "エッジ検出エラー!"); } else if ( 0 == status ) Lib_display_message( 100, 200, "サーチエラー", "対象物がありません" ); else if ( -1 == status ) Lib_display_message( 100, 200, "サーチエラー", "パタンが登録されていません" ); Lib_freerun(); Lib_cls( ( GRAY_PLANE | CHAR_PLANE | LINE_PLANE ), BLACK_COLOR ); } /************************************************************************/ /* Wx,Wy算出       */ /************************************************************************/ void calc_WxWy( ox1, oy1, ox2, oy2, x, y, Wx, Wy ) double ox1, oy1, ox2, oy2; /*基準サーチ位置(2箇所)*/ double x, y; /*変換元座標 */ double *Wx, *Wy; /*Wx, Wy */ { double xc, yc, qc; xc = (double)(ox1 + ox2) / 2.0; yc = (double)(oy1 + oy2) / 2.0; qc = atan2( oy2 - oy1, ox2 - ox1 ); *Wx = x * cos( qc ) + y * sin( qc ) - xc * cos( qc ) - yc * sin( qc ); *Wy = -x * sin( qc ) + y * cos( qc ) + xc * sin( qc ) - yc * cos( qc ); } /************************************************************************/ /* 変換後座標( CVx, CVy )算出   */ /************************************************************************/ void calc_CVxCVy( wx1, wy1, wx2, wy2, Wx, Wy, CVx, CVy ) double wx1, wy1, wx2, wy2; /*ワークのサーチ位置(2箇所)*/ double Wx, Wy; /*変換元座標係数 */ double *CVx, *CVy; /*変換後座標 */ { double xc, yc, qc; xc = (double)(wx1 + wx2) / 2.0; yc = (double)(wy1 + wy2) / 2.0; qc = atan2( wy2 - wy1, wx2 - wx1 ); *CVx = Wx * cos( qc ) - Wy * sin( qc ) + xc; *CVy = Wx * sin( qc ) + Wy * cos( qc ) + yc; }