⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 liver_tree_template.cpp

📁 vc++为集成开发环境
💻 CPP
📖 第 1 页 / 共 2 页
字号:

/* -------------------------------------------------------------------------
 * 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 + -