-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgrid2d.cpp
More file actions
174 lines (139 loc) · 3.31 KB
/
Copy pathgrid2d.cpp
File metadata and controls
174 lines (139 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* grid2d.cpp
* topicNet
*
* Created by basak alper on 1/12/11.
* Copyright 2011 ucsb. All rights reserved.
*
*/
#include "grid2d.h"
Grid2D :: Grid2D()
:gridsize(10), dim(1.0)
{
grids = new GridNode**[gridsize];
//assume we first traverse in x
for (int x=0; x<gridsize; x++) {
grids[x] = new GridNode *[gridsize];
//then traverse in y
for (int y=0; y<gridsize; y++) {
grids[x][y] = new GridNode [gridsize];
}
}
}
Grid2D :: Grid2D(int grsize, double dimen)
{
dim = dimen;
gridsize = grsize;
grids = new GridNode**[gridsize];
//assume we first traverse in x
for (int x=0; x<gridsize; x++) {
grids[x] = new GridNode * [gridsize];
//then traverse in y
for (int y=0; y<gridsize; y++) {
grids[x][y] = new GridNode [gridsize];
}
}
}
Grid2D :: ~Grid2D()
{
for (int x=0; x<gridsize; x++)
{
for (int y=0; y<gridsize; y++)
{
delete grids[x][y];
}
delete grids[x];
}
delete grids;
}
void Grid2D :: cleargrid()
{
for (int x=0; x<gridsize; x++)
{
for (int y=0; y<gridsize; y++)
{
GridNode * g = grids[x][y];
g->graph_nodes.erase(g->graph_nodes.begin(), g->graph_nodes.end());
}
}
}
bool Grid2D :: findgridnode(space::Vec3d pos, GridNode * gn){
double div = dim / gridsize;
if (pos.x == dim) pos.x -= 0.0001;
if (pos.y == dim) pos.y -= 0.0001;
int indx = (int) (pos.x / div);
int indy = (int) (pos.y / div);
if ((indx< 0 || indy<0 ) || (indx>= gridsize || indy>= gridsize)) {
printf("find grid node failed %f, %f \n", pos.x, pos.y);
return false;
}
else{
gn = grids[indx][indy];
return true;
}
}
bool Grid2D :: getgridindex(space::Vec3d pos, int &i, int &j){
double div = dim / gridsize;
if (pos.x == dim) pos.x -= 0.0001;
if (pos.y == dim) pos.y -= 0.0001;
if (pos.z == dim) pos.z -= 0.0001;
i = (int) (pos.x / div);
j = (int) (pos.y / div);
if ((i< 0 || j<0 ) || (i>= gridsize || j>= gridsize)) {
printf("getgridindex failed %f, %f \n", pos.x, pos.y);
return false;
}
else{
return true;
}
}
GridNode * Grid2D :: getgridnode(int i, int j){
return grids[i][j];
}
void Grid2D :: addGraphNode(GraphNode * nd){
GridNode * grnd = new GridNode();
bool found = findgridnode(nd->getPos(), grnd);
if(found)
grnd->graph_nodes.push_back(nd);
else {
printf("add graph node failed \n");
}
}
void Grid2D :: addGraph(Graph * thegraph){
for(int n=0; n< thegraph->getNumNodes(); n++){
GraphNode * gnd = thegraph->getGraphNode(n);
addGraphNode(gnd);
}
}
void Grid2D :: drawGridNode(int ix, int iy){
float xs = (float)(ix * dim) / gridsize;
//xs = xs - (dim * 0.5);
float xf = (float)((ix+1) * dim) / gridsize;
//xf = xf - (dim * 0.5);
float ys = (float)(iy * dim) / gridsize;
//ys = ys - (dim * 0.5);
float yf = (float)((iy+1) * dim) / gridsize;
//yf = yf - (dim * 0.5);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_COLOR, GL_ONE_MINUS_SRC_COLOR);
glDisable(GL_DEPTH_TEST);
glBegin(GL_QUADS);
glColor4f(1.0, 0.2, 0.2, 0.3);
glVertex3f(xs, ys, 0.0);
glColor4f(1.0, 0.8, 0.2, 0.3);
glVertex3f(xf, ys, 0.0);
glColor4f(0.3, 0.9, 0.2, 0.3);
glVertex3f(xf, yf, 0.0);
glColor4f(0.1, 0.3, 0.9, 0.3);
glVertex3f(xs, yf, 0.0);
glEnd();
glEnable(GL_DEPTH_TEST);
glDisable(GL_BLEND);
}
void Grid2D :: drawGrid(){
for (int xx=0; xx<gridsize; xx++) {
for (int yy=0; yy<gridsize; yy++) {
drawGridNode(xx, yy);
}
}
}