/* * ***************************************************************** * * * * * 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 最遠点対検出 FARTHEST.C */ /*[作成者]H.Yagi */ /* 目的: 関数: 履歴: Ver 1.0 97/08/15 Ver 1.1 99/03/16 計算幾何学ライブラリをコールするように変更 Ver 1.2 99/04/06 修正 注記: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_meas.h" #include "f_exmeas.h" #include "f_bndry.h" #include "f_cmpgeo.h" /* * Include CSC90X common local */ #include "m_menu.h" /* * プロトタイプ宣言 */ void main( void ); void main_menu_disp( void ); void disp_help( void ); void meas_set( void ); void pdm_binlevel( int, int, PARADIGM * ); void pdm_wind_set( int, int, PARADIGM * ); void exec( void ); void xdraw_cros( int, int, int, int ); void set_data( int, short *, WPNT2_T [] ); void disp_farthest( int, WPNT2_T [], int, int ); 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 MEAS ", " 2値設定 " }, { " EXEC ", " 実 行 " }, { " PROC3 ", " 処理3 " }, { " PROC4 ", " 処理4 " } }; #define INIT_CUR_POS_X 255 #define INIT_CUR_POS_Y 239 #define MAX_PNT_NUM 3000 /* 入力点集合の点の最大数 */ #define BUFF_SIZE (512*480/4) static char *select_color[2] = { "黒", "白" }; static int pad_level; int wBinLevel; /*2値レベル */ int wxs,wys,wxe,wye; /*計測範囲 */ int wColor; /*対象色 */ int wMeasEdge; /*接触塊オプション */ int wLlimit; /*面積下限値 */ int wUlimit; /*面積上限値 */ /* * メイン */ 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; wBinLevel = Lib_get_bin_level(); wxs = 0; wys = 0; wxe = 511; wye = 479; Lib_window( wxs, wys, wxe, wye ); wColor = 0; wMeasEdge = 1; wLlimit = 0; wUlimit = 245760; /* 入力ビデオ制御 */ 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 ); /* メニュー制御 */ 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 ); meas_set(); /*2値計測設定*/ 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; /* 終了 */ } } } } /* * メインメニュー表示 */ 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, "【機能\概要】 FARTHEST" ); Lib_chrdisp( 11, 10, "<最遠点対検出>" ); Lib_chrdisp( 11, 12, "○面積が最大の物体に対して処理します。" ); Lib_chrdisp( 11, 14, "○ターゲットの周囲座標を抽出し、最遠点対" ); Lib_chrdisp( 11, 16, " を求めその距離を計測します。" ); } /************************************************************************/ /* 設定 */ /************************************************************************/ void meas_set( ) { PVAL value[6]; int no; pad_level = Lib_view_open(); Lib_view_set_title( pad_level, "設定" ); Lib_view_set_uniq_numeral( pad_level, 5, 5, "2値レベル", wBinLevel, 0, 255, 0 ); Lib_view_set_null ( pad_level, 5, 35, "計測範囲" , 1 ); Lib_view_set_select ( pad_level, 5, 65, "対象色", wColor, 2, select_color, 2 ); Lib_view_set_uniq_alter ( pad_level, 5, 95, "境界", wMeasEdge, 3 ); Lib_view_set_uniq_numeral( pad_level, 5, 125, "面積下限値", wLlimit, 0, 245760, 4 ); Lib_view_set_uniq_numeral( pad_level, 5, 155, "面積上限値", wUlimit, 0, 245760, 5 ); Lib_set_paradigm( pad_level, 0, WHOLE_TIMING, pdm_binlevel ); Lib_set_paradigm( pad_level, 1, START_TIMING, pdm_wind_set ); Lib_view_set_size( pad_level, 50, 50, 5, 5 ); Lib_draw_menu( pad_level ); if ( ERROR_RETURN != ( no = Lib_process_menu( pad_level, value ) ) ) /* メニュー値の取得 */ { switch( no ) { case 101: /*「実行」が選択された */ wBinLevel = value[0].value_type; wColor = value[2].select_type; wMeasEdge = value[3].on_off_type; wLlimit = value[4].value_type; wUlimit = value[5].value_type; break; case 102: /*「取り消し」が選択された */ break; } } Lib_erase_menu( pad_level ); Lib_view_close( pad_level ); } /*********************************************************************/ /*  2値レベル設定                         */ /*********************************************************************/ void pdm_binlevel( Timing, Numb, val ) int Timing; int Numb; PARADIGM val[]; { if ( Timing == START_TIMING ) { Lib_erase_menu( pad_level ); Lib_input_video_control( BIN_PLANE ); Lib_display_control( BIN_PLANE | LINE_PLANE | CHAR_PLANE ); Lib_memory_clear( BIN_PLANE ); } else if ( Timing == END_TIMING ) { Lib_input_video_control( GRAY_PLANE ); Lib_display_control( GRAY_PLANE | LINE_PLANE | CHAR_PLANE ); Lib_memory_clear( LINE_PLANE | CHAR_PLANE ); Lib_draw_menu( pad_level ); } Lib_video_bin_level( (int)val[0].long_type ); Lib_freerun(); } /*********************************************************************/ /*  ウインド設定制御                        */ /*********************************************************************/ void pdm_wind_set( Timing, Numb, val ) int Timing; int Numb; PARADIGM val[]; { int pad_level_wind; PVAL value_wind[2]; int no; int x_size,y_size; int idc; idc = Lib_shelter_plane( 0, 0, (Lib_get_dx_size() - 1), (Lib_get_dy_size() - 1), CHAR_PLANE ); pad_level_wind = Lib_view_open(); Lib_view_set_title( pad_level_wind, "処理範囲" ); x_size = wxe - wxs + 1; y_size = wye - wys + 1; Lib_view_set_box( pad_level_wind, 5, 5, "始点" , wxs , wys , 3, 1, 0 ); Lib_view_set_box( pad_level_wind, 5, 35, "サイズ", x_size, y_size, 3, 0, 1 ); Lib_view_set_size( pad_level_wind, 100, 130, 5, 5 ); Lib_draw_menu( pad_level_wind ); if( ERROR_RETURN != ( no = Lib_process_menu( pad_level_wind, value_wind ) ) ) { switch( no ) { case 101: /*「実行」が選択された */ wxs = value_wind[0].box_type.x; wys = value_wind[0].box_type.y; wxe = value_wind[0].box_type.x + value_wind[1].box_type.x - 1; wye = value_wind[0].box_type.y + value_wind[1].box_type.y - 1; break; case 102: /*「取り消し」が選択された */ break; } } Lib_erase_menu( pad_level_wind ); Lib_view_close( pad_level_wind ); Lib_recover_plane( idc ); } /************************************************************************/ /* クロスマーク表示 */ /************************************************************************/ void xdraw_cros( x, y, length, mode ) int x, y; /* Input:Display Position */ int length; /* Input:Display Length */ int mode; /* Input:0=GRAPH_ERAS, 1=GRAPH_DRAW */ { if ( GRAPH_DRAW == mode ) { Lib_drawline( x-length, y, x+length, y ); Lib_drawline( x, y-length, x, y+length ); } else { Lib_erasline( x-length, y, x+length, y ); Lib_erasline( x, y-length, x, y+length ); } } /*********************************************************************/ /* 実行                 */ /*********************************************************************/ void exec( void ) { static struct MEASDATA *measp; static int *indextbl; static struct CALCDATA *calcultp; unsigned int max_blob; char *error_string; int error_code; int dsw; int i; int ct; int blobno; int totalarea; int maxarea; int maxarea_idx; static short buff[BUFF_SIZE]; int num; int vect; int no1, no2; int diameter2x; double diameter; void *cnvx_dscrp; /* 2次元凸包の識別子 */ WPNT2_T *vrtx; /* 2次元凸包の頂点座標列 */ int *vrtx_no; /* 2次元凸包の頂点の番号列 */ int vrtx_num; /* 2次元凸包の頂点の数 */ WPNT2_T *data; /* 元データ */ void *ord_data_dscrp; /* 順序データの識別子 */ WPNT2_T *ord_data; /* 入力点データ(=順序データ) */ int ord_pnt_num; /* 入力点データ(=順序データ)の点の数 */ int *orgnl_to_ord; /* 元データから順序データへの番号対応表 */ int *ord_to_orgnl; /* 順序データから元データへの番号対応表 */ char ss[70]; /* システムで設定されている「最大ブローブ数」の値を取得し、indextblに必要な容量の */ /* ワークメモリを確保する */ max_blob = Lib_get_meas_max_blob(); if ( ( int * )NULL != ( indextbl = ( int * )Lib_mlalloc( ( max_blob + 1 ) * 4 ) ) ) { Lib_input_video_control( BIN_PLANE ); Lib_display_control( BIN_PLANE | LINE_PLANE | CHAR_PLANE ); if ( ON == ( dsw = Lib_get_display_switch() ) ) Lib_set_display_switch( OFF ); /* 2値計測パラメータ設定 */ Lib_video_bin_level( wBinLevel ); Lib_window( wxs, wys, wxe, wye ); Lib_strtclk_count(); /*タイマーカウントモードのスタート*/ Lib_freeze( TRANSMIT ); /* システム構築対応1次特徴量計測処理オープン */ Lib_Open_meas_structure(); if ( NORMAL_RETURN == Lib_xmeasure( wColor, wMeasEdge, 0, &measp ) ) /* メジャー */ { if ( 0 < ( ct = Lib_xorderng( measp, 0, 0, wColor, wUlimit, wLlimit, indextbl ) ) ) { if ( ct == 1 ) { blobno = indextbl[0]; Lib_calcult( 3, blobno, &calcultp); maxarea = calcultp->totalarea; /* 全面積 */ maxarea_idx = 0; } else if ( ct > 1 ) /* BLOBは複数ある? */ { maxarea = 0; maxarea_idx = 0; for (i=0; itotalarea; /* 全面積 */ if ( maxarea < totalarea ) { maxarea = totalarea; maxarea_idx = i; } } } blobno = indextbl[maxarea_idx]; vect = OFF; /*ベクトル化しない*/ if ( 0 < ( num = Lib_periphry( 0, measp, blobno, vect, ON, BUFF_SIZE, buff ) ) ) /*周囲座標抽出*/ { /* 元データのバッファ確保 */ if( ( WPNT2_T * )NULL != ( data = ( WPNT2_T * )Lib_mlalloc( sizeof( WPNT2_T )* MAX_PNT_NUM ) ) ) { /*順序データのオープン*/ if( NULL != ( ord_data_dscrp = Lib_cg_ordering_data_open( MAX_PNT_NUM ) ) ) { /*2次元凸包のオープン*/ if( NULL != ( cnvx_dscrp = Lib_cg_2dCnvxHull_open( MAX_PNT_NUM ) ) ) { set_data( num, buff, data ); /*データセット*/ /*順序データの作成*/ if ( NORMAL_RETURN == ( Lib_cg_ordering_data_exec( ord_data_dscrp, data, num, &ord_data, &ord_pnt_num, &orgnl_to_ord, &ord_to_orgnl ))) { /*2次元凸包をつくる*/ if ( NORMAL_RETURN == ( Lib_cg_2dCnvxHull_make( cnvx_dscrp, ord_data, ord_pnt_num, &vrtx, &vrtx_no, &vrtx_num ))) { /*最も離れた2点とその距離を求める*/ if ( ERROR_RETURN != ( diameter2x = Lib_cg_cnvx_plygn_diameter( vrtx, vrtx_num, &no1, &no2 ))) { disp_farthest( vrtx_num, vrtx, no1, no2 ); /*最遠点対の表示*/ /*最大距離の表示*/ diameter = sqrt( (double)diameter2x ); Lib_sprintf( ss, "最大距離: %.1lf", diameter ); Lib_chrdisp( 1, 26, ss ); Lib_time_disp( 0, 452 ); } else Lib_display_keyinput( 80, 230, "ERR! ( Lib_cg_cnvx_plygn_diameter )" ); } else Lib_display_keyinput( 80, 230, "ERR! ( Lib_cg_cnvx_plygn_diameter )" ); } else Lib_display_keyinput( 5, 450, "ERR: Lib_cg_ordering_data_exec" ); /* 2次元凸包のクローズ */ Lib_cg_2dCnvxHull_close( cnvx_dscrp ); } else Lib_display_keyinput( 5, 450, "2次元凸包のオープンに失敗" ); /* 順序データのクローズ */ Lib_cg_ordering_data_close( ord_data_dscrp ); } else Lib_display_keyinput( 5, 450, " 順序データのオープンに失敗 " ); /* 元データのバッファを解放 */ Lib_lfree( ( char * )data ); } else Lib_display_keyinput( 5, 450, " 入力点データのバッファ確保に失敗 " ); } } else Lib_chrdisp( 1, 30, "エラー:ブローブがありません。" ); } else { error_code = Lib_get_meas_error_code(); error_string = Lib_get_meas_error_string(); Lib_sprintf( ss, "2値計測エラー [%d] : %s", error_code, error_string ); Lib_chrdisp( 1, 30, ss ); } } else Lib_chrdisp( 1, 30, "エラー:indextbl用のワークメモリを確保できません。" ); Lib_display_keyinput( 430, 0, " 確認 " ); Lib_cls( ( BIN_PLANE | LINE_PLANE | CHAR_PLANE ), BLACK_COLOR ); Lib_window( 0, 0, 511, 479 ); Lib_set_display_switch( dsw ); Lib_input_video_control( GRAY_PLANE ); Lib_display_control( GRAY_PLANE | LINE_PLANE | CHAR_PLANE ); Lib_freerun(); } /************************************************************************/ /* データセット */ /************************************************************************/ void set_data( num, buff, data ) int num; /*座標の数*/ short *buff; WPNT2_T data[]; { int i; short *sp; sp = buff; for ( i=0; i