?? liver_tree_template.cpp
字號:
/* -------------------------------------------------------------------------
* Callback Name: LIVER_TREE_constructor
* This is a callback function associated with an action taken from a
* UIStyler object.
*
* Input: dialog_id - The dialog id indicate which dialog this callback
* is associated with. The dialog id is a dynamic,
* unique id and should not be stored. It is
* strictly for the use in the NX Open API:
* UF_STYLER_ask_value(s)
* UF_STYLER_set_value
* client_data - Client data is user defined data associated
* with your dialog. Client data may be bound
* to your dialog with UF_MB_add_styler_actions
* or UF_STYLER_create_dialog.
* callback_data - This structure pointer contains information
* specific to the UIStyler Object type that
* invoked this callback and the callback type.
* -----------------------------------------------------------------------*/
int LIVER_TREE_constructor ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
UF_terminate ();
/* Callback acknowledged, do not terminate dialog */
return (UF_UI_CB_CONTINUE_DIALOG);
/* A return value of UF_UI_CB_EXIT_DIALOG will not be accepted */
/* for this callback type. You must continue dialog construction.*/
}
/* -------------------------------------------------------------------------
* Callback Name: LIVER_TREE_destructor
* This is a callback function associated with an action taken from a
* UIStyler object.
*
* Input: dialog_id - The dialog id indicate which dialog this callback
* is associated with. The dialog id is a dynamic,
* unique id and should not be stored. It is
* strictly for the use in the NX Open API:
* UF_STYLER_ask_value(s)
* UF_STYLER_set_value
* client_data - Client data is user defined data associated
* with your dialog. Client data may be bound
* to your dialog with UF_MB_add_styler_actions
* or UF_STYLER_create_dialog.
* callback_data - This structure pointer contains information
* specific to the UIStyler Object type that
* invoked this callback and the callback type.
* -----------------------------------------------------------------------*/
int LIVER_TREE_destructor ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
UF_terminate ();
/* Callback acknowledged, do not terminate dialog. */
/* A return value of UF_UI_CB_EXIT_DIALOG will not be accepted */
/* for this callback type. You must continue dialog destruction*/
return (UF_UI_CB_CONTINUE_DIALOG);
}
/* -------------------------------------------------------------------------
* Callback Name: LIVER_TREE_ok
* This is a callback function associated with an action taken from a
* UIStyler object.
*
* Input: dialog_id - The dialog id indicate which dialog this callback
* is associated with. The dialog id is a dynamic,
* unique id and should not be stored. It is
* strictly for the use in the NX Open API:
* UF_STYLER_ask_value(s)
* UF_STYLER_set_value
* client_data - Client data is user defined data associated
* with your dialog. Client data may be bound
* to your dialog with UF_MB_add_styler_actions
* or UF_STYLER_create_dialog.
* callback_data - This structure pointer contains information
* specific to the UIStyler Object type that
* invoked this callback and the callback type.
* -----------------------------------------------------------------------*/
int LIVER_TREE_ok ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
UF_terminate ();
/* Callback acknowledged, terminate dialog */
/* It is STRONGLY recommended that you exit your */
/* callback with UF_UI_CB_EXIT_DIALOG in a ok callback.*/
/* return ( UF_UI_CB_EXIT_DIALOG ); */
return (UF_UI_CB_EXIT_DIALOG);
}
/* -------------------------------------------------------------------------
* Callback Name: LIVER_TREE_cancel
* This is a callback function associated with an action taken from a
* UIStyler object.
*
* Input: dialog_id - The dialog id indicate which dialog this callback
* is associated with. The dialog id is a dynamic,
* unique id and should not be stored. It is
* strictly for the use in the NX Open API:
* UF_STYLER_ask_value(s)
* UF_STYLER_set_value
* client_data - Client data is user defined data associated
* with your dialog. Client data may be bound
* to your dialog with UF_MB_add_styler_actions
* or UF_STYLER_create_dialog.
* callback_data - This structure pointer contains information
* specific to the UIStyler Object type that
* invoked this callback and the callback type.
* -----------------------------------------------------------------------*/
int LIVER_TREE_cancel ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
UF_terminate ();
/* Callback acknowledged, terminate dialog */
/* It is STRONGLY recommended that you exit your */
/* callback with UF_UI_CB_EXIT_DIALOG in a cancel call */
/* back rather than UF_UI_CB_CONTINUE_DIALOG. */
return ( UF_UI_CB_EXIT_DIALOG );
}
/* -------------------------------------------------------------------------
* Callback Name: LIVER_TREE_FORM_cb
* This is a callback function associated with an action taken from a
* UIStyler object.
*
* Input: dialog_id - The dialog id indicate which dialog this callback
* is associated with. The dialog id is a dynamic,
* unique id and should not be stored. It is
* strictly for the use in the NX Open API:
* UF_STYLER_ask_value(s)
* UF_STYLER_set_value
* client_data - Client data is user defined data associated
* with your dialog. Client data may be bound
* to your dialog with UF_MB_add_styler_actions
* or UF_STYLER_create_dialog.
* callback_data - This structure pointer contains information
* specific to the UIStyler Object type that
* invoked this callback and the callback type.
* -----------------------------------------------------------------------*/
int LIVER_TREE_FORM_cb ( int dialog_id,
void * client_data,
UF_STYLER_item_value_type_p_t callback_data)
{
/* Make sure User Function is available. */
if ( UF_initialize() != 0)
return ( UF_UI_CB_CONTINUE_DIALOG );
/* ---- Enter your callback code here ----- */
float Qperf=0.125,pperf=100,pterm=60;
float x0=0,y0=0;
float dthresh=2.5;
float deltap=pperf-pterm;
float obfun=0; //目標函數計算
struct node
{
float x,y;
node *parent,*left,*right;
float Resist;
float r,l;
int subterm;
};
struct node seg[N_MAX],seg_copy[N_MAX];
int live_branch=0;
struct node p,q,p_copy,q_copy;//定義兩個結構體,來存儲新生成的兩個枝的數據,因為需對每個已存在連接比較
for(int m=0;m<N_MAX;m++)//initialization
{
seg[m].x=0;
seg[m].y=0;
seg[m].parent=NULL;
seg[m].left=NULL;
seg[m].right=NULL;
seg[m].r=0;
seg[m].l=0;
seg[m].Resist=0;
seg[m].subterm=0;
}
float x,y,l,Resist,r;
do
{
x=rand()%500/100.;
y=rand()%500/100.;
l=sqrt(x*x+y*y);
Resist=deltap/Qperf;
r=sqrt(sqrt((l/Resist)*(8*eta/pi)));
}while(l<=2*r||l<=dthresh);
seg[0].x=x;
seg[0].y=y;
seg[0].parent=NULL;
seg[0].left=NULL;
seg[0].right=NULL;
seg[0].r=r;
seg[0].l=l;
seg[0].Resist=Resist;
seg[0].subterm=1;
for(int i=2;i<M;i++)//生成末端
{
for(int k=0;k<N_MAX;k++)//duplicate original data
seg_copy[k]=seg[k];
x=rand()%500/100.;//新生成的點坐標
y=rand()%500/100.;//
dthresh=dthresh*0.9;//新的門限值
float T=1000;//假設目標函數值,及連接枝的序號
for(int j=0;j<2*i-3;j++)//對每個已經存在的節進行連接比較
{
if(seg[j].parent==NULL) //如果要加到第一條邊上;
{
q.x=seg[j].x/2;
q.y=seg[j].y/2;
}
else
{
q.x=(seg[j].x+seg[j].parent->x)/2;
q.y=(seg[j].y+seg[j].parent->y)/2;
}
l=sqrt((x-q.x)*(x-q.x)+(y-q.y)*(y-q.y));
p.x=x;
p.y=y;
p.l=l;
seg[j].l=seg[j].l/2;
seg[j].Resist=seg[j].Resist-8*eta/pi*seg[j].l/seg[j].r/seg[j].r/seg[j].r/seg[j].r;
/////////////////////////////////////////////////////////////////////////
p.subterm = 1;
p.Resist = seg[j].Resist*seg[j].subterm;
p.r = pow(8*eta*p.l/(pi*p.Resist),0.25);
if(p.l<2*p.r||p.l<dthresh)
{
seg[j] = seg_copy[j]; //如果此邊不符合邊界條件,需要將剛才所做的修改恢復
continue;
}
/////////////此次產生的隨機點符合邊界條件,可以被添加到現有樹中
live_branch = 1;
q.l = seg[j].l;
q.subterm = seg[j].subterm + 1;
q.r=sqrt(p.r*p.r + seg[j].r*seg[j].r);
q.Resist=8*eta*q.l/pi/q.r/q.r/q.r/q.r+
1/(1/p.Resist + 1/seg[j].Resist);
q.parent=seg[j].parent;
q.left=&seg[j];
q.right=&p;
p.parent=&q;
p.left=NULL;
p.right=NULL;
//////將q的父結點的兒子指針指向q
if(q.parent != NULL)
{
if(q.parent->left == &seg[j])
q.parent->left = &q;
if(q.parent->right == &seg[j])
q.parent->right = &q;
}
node * temp;//對該節點以上的父枝的半徑改變
temp=&q;
while(temp->parent != NULL)
{
temp= temp->parent;
temp->r=sqrt(pow(temp->left->r,2)+pow(temp->right->r,2));
}
for(int t=0;t<2*i-3;t++)
{
obfun=obfun+seg[t].l*seg[t].r*seg[t].r;
}
obfun=obfun+p.l*p.r*p.r;
if(T>obfun)
{
T=obfun;
p_copy = p;
q_copy = q;
}
seg[j]=seg_copy[j];
}
if(live_branch)
{
live_branch = 0;
//////////////////////////真正添加新邊
//不只是添加數值,而且要將指針指向正確的位置,此處要注意q的地址單元不在seg[]數組中
seg[2*i-3]=q_copy;
seg[2*i-3].left = &seg[j];
seg[2*i-3].right = &seg[2*i-2];
struct node * temp = seg[2*i-3].parent;
if(temp != NULL)
{
if(temp->parent->left == &seg[j])
temp->parent->left = &seg[2*i-3];
if(temp->parent->right == &seg[j])
temp->parent->right = &seg[2*i-3];
}
seg[2*i-2]=p_copy;
seg[2*i-2].parent=&seg[2*i-3];
}
else
i--; //為了保證 i 和所添加的末端數一致,即此循環結束后,末端數目為M,否則,可能比M少而且會出現空指針引用
}
for(i=0;i<N_MAX;i++)
{
double origin[3],direction[3];
char height[256],diam[256];
height[0]='\0';
diam[0]='\0';
tag_t cyl_tag;
origin[0]=seg[i].x;
origin[1]=seg[i].y;
origin[2]=0;
direction[0]=seg[i].x-seg[i].parent->x;
direction[1]=seg[i].y-seg[i].parent->y;
direction[2]=0;
height[0]='\0';
diam[0]='\0';
sprintf(height,"%f",seg[i].l);
sprintf(diam,"%f",seg[i].r*2);
UF_MODL_create_cyl1(UF_NULLSIGN,origin,height,diam,direction,&cyl_tag);
}
UF_terminate ();
/* Callback acknowledged, do not terminate dialog */
return (UF_UI_CB_CONTINUE_DIALOG);
/* or Callback acknowledged, terminate dialog. */
/* return ( UF_UI_CB_EXIT_DIALOG ); */
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -