📄 map.h
字号:
#ifndef MAP_H_
#define MAP_H_
//Date: Apr 6
//Name: map.h in "freeway" project
//Desc: State the Map class and define all the function
#include "freeway.h"
//normal func: calculate the distance between (x0,y0) and (x1,y1)
double getmy_distance(double a0,double b0,double a1,double b1)
{
double distx,disty;
distx = a1-a0;
disty = b1-b0;
return sqrt(distx*distx+disty*disty);
}
//normal func: calculate the angle between (x0,y0) and (x1,y1)
double getmy_angle(double a0,double b0,double a1,double b1)
{
double distx,disty;
double angle;
distx = a1-a0;
disty = b1-b0;
if(disty==0 && distx==0)
angle = 0 ;
else if(distx==0 && disty>0)
angle = PI/2;
else if(distx==0 && disty<0)
angle =-PI/2;
else if (disty>=0 && distx>0) //1
angle=atan((disty)/(distx));
else if (disty<=0 && distx>0) //4
angle=atan((disty)/(distx));
else if (disty>=0 && distx<0) //2
angle=atan((disty)/(distx))+PI;
else if (disty<=0 && distx<0) //3
angle=atan((disty)/(distx))-PI;
return angle;
}
//count the phase //dist1 for successor
float getmy_phase_dist1(int i,float x, int j,float y)
{
float a,b;
a=(float)i+x;
b=(float)j+y;
//printf("a=%f,b=%f\n",a,b);
if(a>=b)
return a-b;
else
return 10001.0;
}
//dist2 is for predecessor
float getmy_phase_dist2(int i,float x, int j,float y)
{
float a,b;
a=(float)i+x;
b=(float)j+y;
//printf("a=%f,b=%f\n",a,b);
if(a<=b)
return b-a;
else
return 10001.0;
}
//sign +/-
int getmy_sign(double x0,double x1,double y0,double y1)
{
int return_value;
if(x0-x1>=0 && y1-y0>=0)
return_value=1;
else if(x0-x1>=0 && y1-y0<0)
return_value=2;
else if(x0-x1<0 && y1-y0>=0)
return_value=3;
else if(x0-x1<0 && y1-y0<0)
return_value=4;
return return_value;
}
/*=* For freewaymap *=*/
struct phase_structure{
int phase_id;
double x0,y0;
double x1,y1;
double length,dir;
double vmin,vmax;
};
struct vehicle_link{
int node_id;
struct vehicle_link *next;
};
struct lane_structure{
int freeway_id;
int direction; //direction vs. inverse direction
//+: left-->right, top-->bottom
//-: right--->left, bottom--->top
int lane_id_of_freeway;
int lane_id_overall; //can be deduced from the freeway_id and lane_id_of_freeway
int num_of_phase;
struct phase_structure *phase_array;
struct vehicle_link *vehicle_head, *vehicle_end;
};
class Map{
friend class Node;
friend void sort_initial(class Map *my_map, class Node *my_node);
float speed_check(class Map *my_map, class Node *my_node);
private:
int map_status; //=0,no obstacle; =1,obstacle; =2,freeway; =3,manhattan
int num_of_freeway;
int num_of_lane; //total num of lanes in the map
struct lane_structure *lane_array; //lane[lane_num]
public:
int Map::get_freeway_num();
int Map::get_lane_num();
int Map::get_phase_num(int overall_lane_id);
int Map::get_lane_freeway_id(int overall_lane_id);
int Map::get_lane_direction(int overall_lane_id);
int Map::get_lane_laneid_of_freeway(int overall_lane_id);
int Map::get_lane_laneid_overall(int overall_lane_id);
struct phase_structure* Map::copy_phase(int overall_lane_id, int phase_id, struct phase_structure *temp);
void Map::map_read(char* filename);
void Map::map_show();
void Map::link_initiate(int overall_lane_id,int id); //id is the beginning node
void Map::link_insert(int overall_lane_id,int id1, int id2, int id); //insert 'id' into between 'id1' and 'id2'
void Map::link_delete(int overall_lane_id,int id); //delete id
int Map::predecessor(int id); //find the id's predecessor
int Map::successor(int id); //find id's successor
void Map::show_linklist(int overall_lane_id);
};
void Map::link_initiate(int overall_lane_id,int id)
{
struct vehicle_link *temp;
temp = new struct vehicle_link;
//at beginning, both head and end are null
lane_array[overall_lane_id].vehicle_head = NULL;
lane_array[overall_lane_id].vehicle_end = NULL;
//create the link, the head points to the 1st element
temp->node_id = id;
temp->next = NULL;
lane_array[overall_lane_id].vehicle_head = temp;
//the end points to the last element
lane_array[overall_lane_id].vehicle_end =temp;
}
void Map::link_insert(int overall_lane_id,int id1,int id2,int id)
{
struct vehicle_link *vehicle_p,*temp;
temp = new struct vehicle_link;
temp->next = NULL;
temp->node_id = id;
if(id1 == -1) //insert into the head
{
vehicle_p = lane_array[overall_lane_id].vehicle_head;
temp->next = vehicle_p;
lane_array[overall_lane_id].vehicle_head = temp;
}
else if(id2 == -1) //insert into the end
{
vehicle_p = lane_array[overall_lane_id].vehicle_end;
vehicle_p->next = temp;
lane_array[overall_lane_id].vehicle_end = temp;
}
else { //insert into the middle
vehicle_p = lane_array[overall_lane_id].vehicle_head;
while(vehicle_p!=NULL && vehicle_p->node_id!=id1)
{ vehicle_p = vehicle_p->next; }
if(vehicle_p==NULL)
printf("the id1=%d and id2=%d is not in the queue!\n",id1,id2);
else if(vehicle_p->next!=NULL){
if(vehicle_p->node_id == id1 && vehicle_p->next->node_id == id2){
temp->next = vehicle_p->next;
vehicle_p->next=temp;
}
else if(vehicle_p->node_id == id1 && vehicle_p->next->node_id != id2)
{ printf("the id1=%d and id2=%d is not correct order!\n",id1,id2); }
}
}//if-else
}
void Map::link_delete(int overall_lane_id,int id)
{
struct vehicle_link *vehicle_p;
vehicle_p = lane_array[overall_lane_id].vehicle_head;
if(vehicle_p!=NULL)
{
if(vehicle_p->node_id == id){//delete the head (all are such case in my program)
lane_array[overall_lane_id].vehicle_head = lane_array[overall_lane_id].vehicle_head->next;
}
else {
while(vehicle_p->next!= NULL && vehicle_p->next->node_id != id)
{ vehicle_p = vehicle_p->next; }
if(vehicle_p->next == NULL)
printf("Error for deleting,no such id=%d in the queue!\n",id);
else if(vehicle_p->next->node_id == id)
{
if(vehicle_p->next->next!=NULL)
vehicle_p->next = vehicle_p->next->next;
else
vehicle_p->next = NULL;
}
}
}
}
int Map::predecessor(int id)
{
int i;
struct vehicle_link *vehicle_p;
int return_value=-1;
for(i=0;i<get_lane_num();i++)
{
vehicle_p = lane_array[i].vehicle_head;
if(vehicle_p!=NULL){
while(vehicle_p->next!=NULL && vehicle_p->next->node_id!=id)
{ vehicle_p = vehicle_p->next ;}
if(vehicle_p->next!=NULL)
if(vehicle_p->next->node_id == id)
return_value = vehicle_p->node_id ;
}
}
return return_value;
}
int Map::successor(int id)
{
int i;
struct vehicle_link *vehicle_p;
int return_value=-1;
for(i=0;i<get_lane_num();i++)
{
vehicle_p = lane_array[i].vehicle_head;
if(vehicle_p!=NULL){
while(vehicle_p->next!=NULL && vehicle_p->node_id!=id)
{ vehicle_p = vehicle_p->next ;}
if(vehicle_p->next != NULL)
if(vehicle_p->node_id == id)
return_value = vehicle_p->next->node_id;
}
}
return return_value;
}
void Map::show_linklist(int overall_lane_id)
{
struct vehicle_link *vehicle_p;
vehicle_p = lane_array[overall_lane_id].vehicle_head;
while(vehicle_p!=NULL)
{
printf("ID=%d, ",vehicle_p->node_id);
vehicle_p = vehicle_p->next;
}
printf("\n");
}
// get the information about the map and the lanes
int Map::get_freeway_num()
{ return num_of_freeway; }
int Map::get_lane_num()
{ return num_of_lane; }
int Map::get_phase_num(int overall_lane_id)
{ return lane_array[overall_lane_id].num_of_phase; }
int Map::get_lane_freeway_id(int overall_lane_id)
{ return lane_array[overall_lane_id].freeway_id; }
int Map::get_lane_direction(int overall_lane_id)
{ return lane_array[overall_lane_id].direction; }
int Map::get_lane_laneid_of_freeway(int overall_lane_id)
{
if(lane_array[overall_lane_id].lane_id_overall!=overall_lane_id)
printf("something WRONG in overall_lane_id CHECK!!\n");
return lane_array[overall_lane_id].lane_id_overall;
}
int Map::get_lane_laneid_overall(int overall_lane_id)
{ return lane_array[overall_lane_id].lane_id_of_freeway; }
//copy the phase item into a new variable
struct phase_structure* Map::copy_phase(int overall_lane_id, int phase_id, struct phase_structure *temp)
{
temp->phase_id = lane_array[overall_lane_id].phase_array[phase_id].phase_id;
temp->x0 = lane_array[overall_lane_id].phase_array[phase_id].x0;
temp->y0 = lane_array[overall_lane_id].phase_array[phase_id].y0;
temp->x1 = lane_array[overall_lane_id].phase_array[phase_id].x1;
temp->y1 = lane_array[overall_lane_id].phase_array[phase_id].y1;
temp->length = lane_array[overall_lane_id].phase_array[phase_id].length;
temp->dir = lane_array[overall_lane_id].phase_array[phase_id].dir;
temp->vmin = lane_array[overall_lane_id].phase_array[phase_id].vmin;
temp->vmax = lane_array[overall_lane_id].phase_array[phase_id].vmax;
return temp;
}
//read the freeway.txt to get the map characteristics
void Map::map_read(char *filename)
{
FILE *map_fp;
char map_style[20];
char openfield_sign[20]="OPENFIELD";
char obstacle_sign[20]="OBSTACLE";
char freeway_sign[20]="FREEWAY";
char manhattan_sign[20]="MANHATTAN";
char headword[20];
char freeway_num_sign[20]="FREEWAY_NUM";
char lane_num_sign[20]="LANE_NUM";
char lane_begin_sign[20]="LANE_BEGIN";
char checkpoint_sign[20]="PHASE";
int temp0,temp1,temp2,temp3,temp4;
float temp5,temp6,temp7,temp8,temp9,temp10;
int i,j;
map_fp = fopen(filename,"r");
if(map_fp == NULL)
{ printf("CANNOT find map file!\n");
exit(0);
}
fscanf(map_fp,"%s",map_style);
if(strcmp(map_style,openfield_sign)==0)
map_status=OPENFIELD_MAP;
else if(strcmp(map_style,obstacle_sign)==0)
map_status=OBSTACLE_MAP;
else if(strcmp(map_style,freeway_sign)==0)
map_status=FREEWAY_MAP;
else if(strcmp(map_style,manhattan_sign)==0)
map_status=MANHATTAN_MAP;
if(map_status==FREEWAY_MAP)
{
fscanf(map_fp,"%s",headword);
if(strcmp(headword,freeway_num_sign)==0)
fscanf(map_fp,"%d",&num_of_freeway);
fscanf(map_fp,"%s",headword);
if(strcmp(headword,lane_num_sign)==0)
fscanf(map_fp,"%d",&num_of_lane);
lane_array = new struct lane_structure[num_of_lane];
for(i=0;i<num_of_lane;i++)
{
fscanf(map_fp,"%s",headword);
if(strcmp(headword,lane_begin_sign)==0){
fscanf(map_fp,"%d",&temp0);
fscanf(map_fp,"%d",&temp1);
fscanf(map_fp,"%d",&temp2);
fscanf(map_fp,"%d",&temp3);
fscanf(map_fp,"%d",&temp4);
lane_array[i].freeway_id=temp0;
lane_array[i].lane_id_of_freeway=temp1;
lane_array[i].lane_id_overall=temp2;
lane_array[i].direction=temp3;
lane_array[i].num_of_phase=temp4;
lane_array[i].vehicle_head = NULL;
lane_array[i].vehicle_end = NULL;
}
else{
printf("FORMAT ERROR:lack of lane_begin!\n");
}
lane_array[i].phase_array = new struct phase_structure[lane_array[i].num_of_phase];
for(j=0;j<lane_array[i].num_of_phase;j++)
{
fscanf(map_fp,"%s",headword);
if(strcmp(headword,checkpoint_sign)==0){
fscanf(map_fp,"%d",&temp0);
fgetc(map_fp);fgetc(map_fp);
fscanf(map_fp,"%f",&temp5);
fgetc(map_fp);
fscanf(map_fp,"%f",&temp6);
fgetc(map_fp);
fgetc(map_fp); fgetc(map_fp);
fscanf(map_fp,"%f",&temp7);
fgetc(map_fp);
fscanf(map_fp,"%f",&temp8);
fgetc(map_fp);
fscanf(map_fp,"%f",&temp9);
fscanf(map_fp,"%f",&temp10);
lane_array[i].phase_array[j].phase_id=temp0;
lane_array[i].phase_array[j].x0=temp5;
lane_array[i].phase_array[j].y0=temp6;
lane_array[i].phase_array[j].x1=temp7;
lane_array[i].phase_array[j].y1=temp8;
lane_array[i].phase_array[j].vmin=temp9;
lane_array[i].phase_array[j].vmax=temp10;
lane_array[i].phase_array[j].length=getmy_distance(temp5,temp6,temp7,temp8);
lane_array[i].phase_array[j].dir=getmy_angle(temp5,temp6,temp7,temp8);
//printf(" -- %d, %f %f\n",lane_array[i].freeway_checkpoint[j].position_num,lane_array[i].freeway_checkpoint[j].x,lane_array[i].freeway_checkpoint[j].y);
}
else
printf("FORMAT ERROR: lack of 'PHASE'!\n");
}
}
}
fclose(map_fp);
}
//show the map on the screen
void Map::map_show()
{
int i,j,k;
printf("MAP=%d\n",map_status);
if(map_status==FREEWAY_MAP)
{
printf("num_freeway=%d\n",num_of_freeway);
printf("num_lane=%d\n",num_of_lane);
for(i=0;i<num_of_lane;i++)
{
printf("Freeway_id=%d, ",lane_array[i].freeway_id);
printf("Lane_id_freeway=%d, ",lane_array[i].lane_id_of_freeway);
printf("Lane_id_overall=%d\n",lane_array[i].lane_id_overall);
printf("Lane_direction=%d, ",lane_array[i].direction);
printf("Num_of_position=%d\n",lane_array[i].num_of_phase);
for(j=0;j<lane_array[i].num_of_phase;j++)
printf(" -- %d, (%.1f, %.1f), (%.1f, %.1f), v=[%.1f %.1f], <%.1f, %.3f>\n",
lane_array[i].phase_array[j].phase_id,
lane_array[i].phase_array[j].x0,
lane_array[i].phase_array[j].y0,
lane_array[i].phase_array[j].x1,
lane_array[i].phase_array[j].y1,
lane_array[i].phase_array[j].vmin,
lane_array[i].phase_array[j].vmax,
lane_array[i].phase_array[j].length,
lane_array[i].phase_array[j].dir*180/PI);
}
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -