📄 node.h
字号:
//Date: June 22, 2002
//Name: node.h in "manhattan" project
//define the mobile node class and all its function
#ifndef NODE_H_
#define NODE_H_
#include "manhattan.h"
//mobile node class
class Node{
friend class Map;
private:
int id;
float src_x,src_y,src_offset;
int src_lane,src_crspnt;
float current_x,current_y; //current position of the node
int current_lane,current_crspnt;
float next_x,next_y; //next position of the node
int next_lane,next_crspnt;
float current_speed,current_angle;
void set_speed(class Map *my_map,int clock);
void set_angle(class Map *my_map,int clock);
void set_next_xy(class Map *my_map,int clock);
public:
float speed_array[SIMULATION_TIME];
float angle_array[SIMULATION_TIME];
float x_array[SIMULATION_TIME];
float y_array[SIMULATION_TIME];
float nextx_array[SIMULATION_TIME];
float nexty_array[SIMULATION_TIME];
//Node();
//~Node();
void initialize(int my_id, class Map *my_map);
void update(class Map *my_map,int clock);
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)
{
id=my_id;
new_random1();
new_random1();
//calculate the initial position of node
src_lane = new_random1() * (float)my_map->num_of_lane;
src_offset = new_random1();
//printf("id=%d, lane=%d, offset=%f, road angle=%f, length=%f\n", my_id,src_lane,src_offset,my_map->lane_array[src_lane].length, my_map->lane_array[src_lane].angle);
src_x = my_map->lane_array[src_lane].x0 +
src_offset * (float) my_map->lane_array[src_lane].length
* (float) cos(my_map->lane_array[src_lane].angle);
//printf("x0=%f,src_x=%f,",my_map->lane_array[src_lane].x0,src_x);
src_y = my_map->lane_array[src_lane].y0 +
src_offset * (float) my_map->lane_array[src_lane].length
* (float) sin(my_map->lane_array[src_lane].angle);
//printf("y0=%f,src_y=%f\n",my_map->lane_array[src_lane].y0,src_y);
src_crspnt = my_map->find_crspnt(src_lane,src_x,src_y);
//print out the initial position
//calculate the initial speed/angle
current_angle = my_map->lane_array[src_lane].angle ;
current_speed = my_map->lane_array[src_lane].vmin + new_random1()
* (my_map->lane_array[src_lane].vmax - my_map->lane_array[src_lane].vmin);
current_x = src_x;
current_y = src_y;
current_lane = src_lane;
current_crspnt = src_crspnt;
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].angle;
//store the information
angle_array[clock]=current_angle;
}
void Node::set_speed(class Map *my_map, int clock)
{
float temp;
temp = current_speed;
current_speed = current_speed + new_random2() * ACCELERATION; //3 is the acceleration speed
if(current_speed<0)
current_speed = temp;
if(current_speed < VMIN)
current_speed = VMIN;
if(current_speed > VMAX)
current_speed = VMAX;
//store information
speed_array[clock]=current_speed;
}
void Node::set_next_xy(class Map *my_map, int clock)
{
float remain1,remain2; //remain1=to the 1st lane, remain2=to the 2nd lane
float remain;
int crspnt;
float choice;
crspnt = my_map->find_crspnt(current_lane,current_x,current_y);
//printf("crspnt_num=%d\n",crspnt);
if( crspnt == my_map->lane_array[current_lane].crosspoint_num-1 )
{
remain1 = getmy_distance(current_x,current_y,
my_map->lane_array[current_lane].x1,
my_map->lane_array[current_lane].y1);
//printf("CASE1: remain1=%f\n",remain1);
if( current_speed * SIMULATION_STEP < remain1 )
{
next_lane = current_lane;
next_crspnt = current_crspnt;
next_x = current_x + current_speed * cos(current_angle)
* SIMULATION_STEP;
next_y = current_y + current_speed * sin(current_angle)
* SIMULATION_STEP;
}
else
{
src_lane = new_random1() * my_map->num_of_lane;
src_offset = 0.2 * new_random1();
src_x = my_map->lane_array[src_lane].x0 +
src_offset * my_map->lane_array[src_lane].length * cos(my_map->lane_array[src_lane].angle);
src_y = my_map->lane_array[src_lane].y0 +
src_offset * my_map->lane_array[src_lane].length * sin(my_map->lane_array[src_lane].angle);
src_crspnt = my_map->find_crspnt(src_lane,src_x,src_y);
//printf("id=%d,offset=%f, x0=%f, src_x=%f, y0=%f, src_y=%f, crspnt=%d\n",
// id, src_offset,my_map->lane_array[src_lane].x0, src_x,
// my_map->lane_array[src_lane].y0,src_y,src_crspnt);
next_lane = src_lane; next_crspnt = src_crspnt;
next_x = src_x; next_y = src_y;
//modify the speed
speed_array[clock] = sqrt( (current_x-next_x)*(current_x-next_x)+(current_y-next_y)*(current_y-next_y) );
}
}
else if( oddeven(crspnt) == 1 && crspnt != my_map->lane_array[current_lane].crosspoint_num-1 )
{
remain1 = getmy_distance(current_x,current_y,
my_map->lane_array[current_lane].crosspoint[crspnt+1].x,
my_map->lane_array[current_lane].crosspoint[crspnt+1].y);
remain2 = getmy_distance(current_x,current_y,
my_map->lane_array[current_lane].crosspoint[crspnt+2].x,
my_map->lane_array[current_lane].crosspoint[crspnt+2].y);
//printf("CASE2: remain1=%f,remain2=%f\n",remain1,remain2);
if(current_speed*SIMULATION_STEP < remain1)
{
next_lane = current_lane;
next_crspnt = current_crspnt;
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_speed*SIMULATION_STEP >= remain1 &&
current_speed*SIMULATION_STEP < remain2)
{
choice = new_random1();
//printf("choice1 = %f\n",choice);
if(choice <= 0.25) //turn
{
next_lane = my_map->lane_array[current_lane].crosspoint[crspnt+1].street_id * LANE_PER_STREET
+ my_map->lane_array[current_lane].crosspoint[crspnt+1].lane_id;
next_x = my_map->lane_array[current_lane].crosspoint[crspnt+1].x
+ (current_speed*SIMULATION_STEP - remain1) * cos(my_map->lane_array[next_lane].angle);
next_y = my_map->lane_array[current_lane].crosspoint[crspnt+1].y
+ (current_speed*SIMULATION_STEP - remain1) * sin(my_map->lane_array[next_lane].angle);
next_crspnt = my_map->find_crspnt(next_lane,next_x,next_y);
}
else //stay on the track
{
next_lane = current_lane;
next_crspnt = current_crspnt + 1;
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_speed*SIMULATION_STEP > remain2)
{
choice = new_random1();
//printf("choice2=%f\n",choice);
if(choice <= 0.25) //turn on first
{
next_lane = my_map->lane_array[current_lane].crosspoint[crspnt+1].street_id * LANE_PER_STREET
+ my_map->lane_array[current_lane].crosspoint[crspnt+1].lane_id;
next_x = my_map->lane_array[current_lane].crosspoint[crspnt+1].x
+ (current_speed*SIMULATION_STEP - remain1) * cos(my_map->lane_array[next_lane].angle);
next_y = my_map->lane_array[current_lane].crosspoint[crspnt+1].y
+ (current_speed*SIMULATION_STEP - remain1) * sin(my_map->lane_array[next_lane].angle);
next_crspnt = my_map->find_crspnt(next_lane,next_x,next_y);
}
else if(choice <= 0.50 && choice > 0.25) //turn on first
{
next_lane = my_map->lane_array[current_lane].crosspoint[crspnt+2].street_id * LANE_PER_STREET
+ my_map->lane_array[current_lane].crosspoint[crspnt+2].lane_id;
next_x = my_map->lane_array[current_lane].crosspoint[crspnt+2].x
+ (current_speed*SIMULATION_STEP - remain1) * cos(my_map->lane_array[next_lane].angle);
next_y = my_map->lane_array[current_lane].crosspoint[crspnt+2].y
+ (current_speed*SIMULATION_STEP - remain1) * sin(my_map->lane_array[next_lane].angle);
next_crspnt = my_map->find_crspnt(next_lane,next_x,next_y);
}
else //stay on the track
{
next_lane = current_lane;
next_crspnt = current_crspnt +2;
next_x = current_x + current_speed * cos(current_angle)
* SIMULATION_STEP;
next_y = current_y + current_speed * sin(current_angle)
* SIMULATION_STEP;
}
}
}
else if( oddeven(crspnt) == 0 )
{
remain1 = getmy_distance(current_x,current_y,
my_map->lane_array[current_lane].crosspoint[crspnt+1].x,
my_map->lane_array[current_lane].crosspoint[crspnt+1].y);
//printf("CASE3: remain1=%f\n",remain1);
if(current_speed*SIMULATION_STEP < remain1)
{
next_lane = current_lane ;
next_crspnt = current_crspnt;
next_x = current_x + current_speed * cos(current_angle)
* SIMULATION_STEP;
next_y = current_y + current_speed * sin(current_angle)
* SIMULATION_STEP;
}
else
{
choice = new_random1();
//printf("choice3=%f\n",choice);
if(choice <= 0.33)
{
next_lane = my_map->lane_array[current_lane].crosspoint[crspnt+1].street_id * LANE_PER_STREET
+ my_map->lane_array[current_lane].crosspoint[crspnt+1].lane_id;
next_x = my_map->lane_array[current_lane].crosspoint[crspnt+1].x
+ (current_speed*SIMULATION_STEP - remain1) * cos(my_map->lane_array[next_lane].angle);
next_y = my_map->lane_array[current_lane].crosspoint[crspnt+1].y
+ (current_speed*SIMULATION_STEP - remain1) * sin(my_map->lane_array[next_lane].angle);
next_crspnt = my_map->find_crspnt(next_lane,next_x,next_y);
}
else //stay on the track
{
next_lane = current_lane;
next_crspnt = current_crspnt +1;
next_x = current_x + current_speed * cos(current_angle)
* SIMULATION_STEP;
next_y = current_y + current_speed * sin(current_angle)
* SIMULATION_STEP;
}
}
}
//store the information
nextx_array[clock]=next_x;
nexty_array[clock]=next_y;
}
void Node::update(class Map *my_map,int clock)
{
current_x = next_x;
current_y = next_y;
current_lane = next_lane;
current_crspnt = next_crspnt;
//store the information
x_array[clock]=current_x;
y_array[clock]=current_y;
set_angle(my_map,clock);
set_speed(my_map,clock);
set_next_xy(my_map,clock);
}
void Node::show_trace (int clock)
{
printf("ID=%d,t=%d: \n",id,clock);
printf("src_lane=%d, src_crspnt=%d, src_offset=%f, src_x=%f, src_y=%f\n",
src_lane,src_crspnt,src_offset,src_x,src_y);
printf("cur_lane=%d, cur_crspnt=%d, cur_x=%f, cur_y=%f\n",
current_lane,current_crspnt,current_x,current_y);
printf("next_lane=%d, next_crspnt=%d, next_x=%f, next_y=%f\n",
next_lane, next_crspnt, 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 + -