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

📄 node.h

📁 These mobility generator tools are used to generate a rich set of mobility scenarios used to evalua
💻 H
字号:
#ifndef NODE_H_
#define NODE_H_


//Date: Apr 6, 2002
//Name: node.h in "freeway" project
//define the mobile node class and all its function

#include "freeway.h"

//return a random double number at interval [0,1]
double new_random1()
{	return (double) (rand()*1.0/(double)RAND_MAX);	}

//return a random double number at the interval [-1,1]
double new_random2()
{	return (double) ((rand()*1.0/(double)RAND_MAX-0.5)*2);	}


//return the min number
float max(float a, float b)
{
	if(a>=b)
		return a;
	else 
		return b;
}

//return the max number
float min(float a, float b)
{
	if(a<=b)
		return a;
	else 
		return b;
}



//mobile node class
class Node{

	friend class Map;
	friend void sort_initial(class Map *my_map, class Node *my_node);
	friend float speed_check(class Map *my_map, class Node *my_node);


	private:
		int id;   
		//int map_status; //=0,no obstacle; =1,obstacle; =2,freeway; =3,manhattan
		//int placement_status;	//=4,uniform; =5,clustering
		//int mobility_status;	//=6,random; =7,group; =8,followup,=9,bypass
		
		int predecessor;
		int successor;
	
		float src_x,src_y,src_offset;
		int src_lane,src_phase;
		
		float current_x,current_y;	//current position of the node
		int current_lane,current_phase;
		
		float next_x,next_y;	//next position of the node
		int next_lane,next_phase;

		float current_speed,current_angle, saved_current_speed;

		
		void set_speed(class Map *my_map,int clock,float speed_limit);
		void set_angle(class Map *my_map,int clock);
		void set_next_xy(class Map *my_map,int clock);
	
	public:
	
		float speed_array[SIMULATION_DURATION];
		float angle_array[SIMULATION_DURATION];
		float x_array[SIMULATION_DURATION];
		float y_array[SIMULATION_DURATION];
		float nextx_array[SIMULATION_DURATION];
		float nexty_array[SIMULATION_DURATION];

		//Node();
		//~Node();
		void initialize(int my_id, class Map *my_map);
		void update(class Map *my_map,int clock,float speed_limit);

		void show_trace(int clock);
		void save_file(FILE *output,int mode,int clock);	//mode=0, initial
															//    =1, ongoing

};

//initialize: set the initial position of each node
void Node::initialize(int my_id, class Map *my_map)
{
	//printf("lane_num=%d,rand=%f\n",my_map->get_lane_num(),new_random1());
	id=my_id;	
	//printf("my_id=%d,id=%d\n",my_id,id);
	//calculate the initial position of node
	src_lane = int( new_random1() * (float)my_map->get_lane_num() );
	src_phase = int( new_random1() * (float)my_map->get_phase_num(src_lane) );
	src_offset = new_random1();

	src_x = my_map->lane_array[src_lane].phase_array[src_phase].x0 + 
		src_offset * my_map->lane_array[src_lane].phase_array[src_phase].length 
				   * (float) cos(my_map->lane_array[src_lane].phase_array[src_phase].dir); 	

	src_y = my_map->lane_array[src_lane].phase_array[src_phase].y0 + 
		src_offset * my_map->lane_array[src_lane].phase_array[src_phase].length 
				   * (float) sin(my_map->lane_array[src_lane].phase_array[src_phase].dir); 
	//print out the initial position
	//printf("ID=%d: lane=%d,phase=%d,offset=%f <%f,%f>\n",id,src_lane,src_phase,src_offset,src_x,src_y);
	
	//calculate the initial speed/angle
	current_angle = my_map->lane_array[src_lane].phase_array[src_phase].dir;
	current_speed = my_map->lane_array[src_lane].phase_array[src_phase].vmin + new_random1() 
		* (my_map->lane_array[src_lane].phase_array[src_phase].vmax - my_map->lane_array[src_lane].phase_array[src_phase].vmin);    

	current_x = src_x;
	current_y = src_y;
	current_lane = src_lane;
	current_phase = src_phase;
	
	set_next_xy(my_map,0);
	
	//store the information
	speed_array[0]=current_speed; angle_array[0]=current_angle;
	x_array[0]=current_x; y_array[0]=current_y;
}

void Node::set_angle(class Map *my_map, int clock)
{
	current_angle = my_map->lane_array[current_lane].phase_array[current_phase].dir;
	//store the information
	angle_array[clock]=current_angle;
}


//speed_limit is from high-level func 
//	if getmy_distance(xi[clock],yi[clock],xj[clock],yj[clock])
//		i's speed_limit = v[j][[clock+1]
void Node::set_speed(class Map *my_map, int clock, float speed_limit)
{
	//printf("current_speed=%f\n",current_speed);
	if(speed_limit != -1.0)
		current_speed = speed_limit;
	else
		current_speed = current_speed + new_random2() * ACCELERATION;	//3 is the acceleration speed
	//printf("current_speed=%f\n",current_speed);

	if( current_speed > my_map->lane_array[current_lane].phase_array[current_phase].vmax )   
		current_speed = my_map->lane_array[current_lane].phase_array[current_phase].vmax;
	
	if( current_speed < my_map->lane_array[current_lane].phase_array[current_phase].vmin )   
		current_speed = my_map->lane_array[current_lane].phase_array[current_phase].vmin;
	//printf("current_speed=%f\n",current_speed);

	//store information
	speed_array[clock]=current_speed;
}


void Node::set_next_xy(class Map *my_map, int clock)
{
	float remain1,remain2;
	
	remain1 = getmy_distance(current_x,current_y,my_map->lane_array[current_lane].phase_array[current_phase].x1,my_map->lane_array[current_lane].phase_array[current_phase].y1);

	if( remain1 >= current_speed * SIMULATION_STEP)
	{
		next_lane = current_lane;
		next_phase = current_phase;
		next_x = current_x + current_speed * cos(current_angle) * SIMULATION_STEP;
		next_y = current_y + current_speed * sin(current_angle) * SIMULATION_STEP;	}
	else if( current_phase + 1 > my_map->get_phase_num(current_lane)-1 ) 
	{
		//delete it from lane's list;
		my_map->link_delete(current_lane,id);
		
		src_lane = int( new_random1() * (double)my_map->get_lane_num()); 
		src_phase = 0;
		src_offset =0.1* new_random1();
		src_x = my_map->lane_array[src_lane].phase_array[src_phase].x0
			+ src_offset * my_map->lane_array[src_lane].phase_array[src_phase].length
			  * cos(my_map->lane_array[src_lane].phase_array[src_phase].dir);
		src_y = my_map->lane_array[src_lane].phase_array[src_phase].y0
			+ src_offset * my_map->lane_array[src_lane].phase_array[src_phase].length
			  * sin(my_map->lane_array[src_lane].phase_array[src_phase].dir);
			
		next_lane = src_lane; next_phase = src_phase;
		next_x = src_x; next_y = src_y;	
	
		if(my_map->lane_array[next_lane].vehicle_head!=NULL)  
			my_map->link_insert(next_lane,10000,-1,id);
		else 
			my_map->link_initiate(next_lane,id);

		//change the angle and speed
		speed_array[clock] = sqrt( (next_x-current_x)*(next_x-current_x) 
			+ (next_y-current_y)*(next_y-current_y)  ); 

	}
	else
	{
		next_lane = current_lane;
		next_phase = current_phase + 1;
		remain2 = current_speed - remain1;
		next_x = my_map->lane_array[next_lane].phase_array[next_phase].x0 + remain2 * cos(my_map->lane_array[next_lane].phase_array[next_phase].dir);
		next_y = my_map->lane_array[next_lane].phase_array[next_phase].y0 + remain2 * sin(my_map->lane_array[next_lane].phase_array[next_phase].dir);	}

	//store the information
	nextx_array[clock]=next_x;
	nexty_array[clock]=next_y;
}


void Node::update(class Map *my_map,int clock, float speed_limit)
{
	current_x = next_x;
	current_y = next_y;
	current_lane = next_lane;
	current_phase = next_phase;

	//store the information
	x_array[clock]=current_x;
	y_array[clock]=current_y;

	set_angle(my_map,clock);
	set_speed(my_map,clock,speed_limit);
	
	set_next_xy(my_map,clock);

	//update the clock
	//clock++;
}

void Node::show_trace (int clock)
{
	printf("ID=%d,t=%d: \n",id,clock);
	printf("src_lane=%d, src_phase=%d, src_offset=%f, src_x=%f, src_y=%f\n",
		src_lane,src_phase,src_offset,src_x,src_y);
	printf("cur_lane=%d, cur_phase=%d, cur_x=%f, cur_y=%f\n",
		current_lane,current_phase,current_x,current_y);
	printf("next_lane=%d, next_phase=%d, next_x=%f, next_y=%f\n",
		next_lane, next_phase, next_x, next_y);
	printf("cur_speed=%f, cur_angle=%f\n\n",
		current_speed,current_angle);
}


void Node::save_file(FILE *output,int mode,int clock)
{
	float z=0.0;
	char temp='"';

	if(mode==0){
		fprintf(output,"$node_(%d) set X_ %f\n",id,src_x);
		fprintf(output,"$node_(%d) set Y_ %f\n",id,src_y);
		fprintf(output,"$node_(%d) set Z_ %f\n",id,z);}
	else if(mode==1){
		fprintf(output,"$ns_ at %f %c$node_(%d) setdest %f %f %f%c\n",(double)clock,temp,id,nextx_array[clock],nexty_array[clock],speed_array[clock],temp); 
	}
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -