CGR Localization
 All Classes Namespaces Files Functions Variables Macros Pages
pre_render.cpp
Go to the documentation of this file.
1 //========================================================================
2 // This software is free: you can redistribute it and/or modify
3 // it under the terms of the GNU Lesser General Public License Version 3,
4 // as published by the Free Software Foundation.
5 //
6 // This software is distributed in the hope that it will be useful,
7 // but WITHOUT ANY WARRANTY; without even the implied warranty of
8 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 // GNU Lesser General Public License for more details.
10 //
11 // You should have received a copy of the GNU Lesser General Public License
12 // Version 3 in the file COPYING that came with this distribution.
13 // If not, see <http://www.gnu.org/licenses/>.
14 //========================================================================
20 //========================================================================
21 
22 #include <stdlib.h>
23 #include <stdio.h>
24 #include <limits.h>
25 #include <string.h>
26 #include <vector>
27 #include <pthread.h>
28 
29 #include "vector_map.h"
30 #include "popt_pp.h"
31 #include "terminal_utils.h"
32 #include "timer.h"
33 #include "proghelp.h"
34 
35 using namespace std;
36 
37 
38 bool run = true;
39 int numSteps = 0;
40 int debugLevel = 0;
41 float minRange = 0.000001;
42 float maxRange = 8.0;
43 double resolution = 0.2;
44 
45 VectorMap *vectorMap;
46 
47 typedef struct{
48  int taskIndex;
49  int taskStart;
50  int taskEnd;
51 } task_t;
52 
53 vector<vector<int> > preRenderList;
54 //Synchronization for threads
55 int numThreads = 1;
56 pthread_t* threads;
57 vector<vector<int> >** sceneLines;
58 vector<vector2i>** sceneLocations;
59 int* threadProgress;
60 
61 vector<int> getUniqueValues(vector<int> &v)
62 {
63  vector<int> ret;
64  ret.clear();
65  for(int i=0; i<(int)v.size(); i++){
66  if(v[i]<0)
67  continue;
68  bool found = false;
69  for(int j=0; !found && j<(int)ret.size(); j++){
70  found = (v[i]==ret[j]);
71  }
72  if(!found)
73  ret.push_back(v[i]);
74  }
75  return ret;
76 }
77 
78 void* PreRenderThread(void* arg)
79 {
80  static const bool BlankTest = false;
81 
82  task_t subTask = *((task_t*) arg);
83 
84  if(debugLevel>2) printf("Thread %d sub-task: %d - %d\n",subTask.taskIndex, subTask.taskStart,subTask.taskEnd);
85 
86  int w = ceil((vectorMap->maxX-vectorMap->minX)/resolution);
87  vector2f loc;
88  vector<vector<int> > * mySceneLines = new vector<vector<int> >();
89  mySceneLines->clear();
90  sceneLines[subTask.taskIndex] = mySceneLines;
91  vector<vector2i> * mylocations = new vector<vector2i>();
92  mylocations->clear();
93  sceneLocations[subTask.taskIndex] = mylocations;
94 
95  threadProgress[subTask.taskIndex] = subTask.taskStart;
96  for(int i=subTask.taskStart; i<=subTask.taskEnd; i++){
97  double y = double(i)*resolution + vectorMap->minY;
98  for(int j=0; j<w; j++){
99  double x = double(j)*resolution + vectorMap->minX;
100  loc.set(x,y);
101  mylocations->push_back(vector2i(j,i));
102  if(BlankTest){
103  vector<int> list;
104  list.push_back(1);
105  list.push_back(0);
106  mySceneLines->push_back(list);
107  }else{
108  //mySceneLines->push_back(vectorMap->getSceneLines(loc,0.0,RAD(360),RAD(0.5),minRange,maxRange));
109  mySceneLines->push_back(vectorMap->getSceneLines(loc,maxRange));
110  }
111 
112  threadProgress[subTask.taskIndex] = i+1;
113  }
114  }
115  return NULL;
116 }
117 
118 void PreRenderMultiThread()
119 {
120  threads = (pthread_t*) malloc(numThreads*sizeof(pthread_t));
121 
122  int w = ceil((vectorMap->maxX-vectorMap->minX)/resolution);
123  int h = ceil((vectorMap->maxY-vectorMap->minY)/resolution);
124  vector2d loc;
125 
126  preRenderList.clear();
127 
128  printf("\nPre-Render Using %d threads.\n",numThreads);
129  printf("Size: %d x %d\nResolution:%.3f\n",w, h, resolution);
130  task_t subTasks[numThreads];
131  int start = 0;
132  int taskIncrement = h/numThreads;
133  for(int i=0; i<numThreads; i++){
134  subTasks[i].taskIndex = i;
135  subTasks[i].taskStart = start;
136  start += taskIncrement;
137  subTasks[i].taskEnd = start-1;
138  }
139  subTasks[numThreads-1].taskEnd = h-1;
140 
141  //Allocate
142  sceneLines = (vector<vector<int> > **) malloc(numThreads*sizeof(vector<vector<int> > *));
143  sceneLocations = (vector<vector2i> **) malloc(numThreads*sizeof(vector<vector2i> *));
144  threadProgress = (int*) malloc(numThreads*sizeof(int));
145 
146  //Fork
147  double tStart = GetTimeSec();
148  for(int i=0; i<numThreads; i++){
149  pthread_create( &threads[i], NULL, PreRenderThread, (void*) &subTasks[i]);
150  }
151 
152  //Monitor Progress
153  bool done=false;
154  while(!done){
155  Sleep(0.1);
156  done = true;
157  double totalProgress = 0.0;
158  printf("\rThread Progress: ");
159  for(int i=0;i<numThreads; i++){
160  printf("%6.2f%% ",double(threadProgress[i]-subTasks[i].taskStart)/double(subTasks[i].taskEnd-subTasks[i].taskStart+1)*100.0);
161  //printf("%6.2f%% ",double(threadProgress[i]));
162  done = done && (threadProgress[i]>subTasks[i].taskEnd);
163  totalProgress += double(threadProgress[i]-subTasks[i].taskStart)/double(subTasks[i].taskEnd-subTasks[i].taskStart+1)*100.0;
164  }
165  printf("Total: %6.2f%% ",totalProgress/double(numThreads));
166  fflush(stdout);
167  }
168 
169  //Join
170  for(int i=0; i<numThreads; i++){
171  pthread_join(threads[i],NULL);
172  }
173 
174  double tDuration = GetTimeSec()-tStart;
175  printf("\nDone Rendering in %.3fs, Saving to file...\n", tDuration);
176  FILE* fidstats = fopen("pre_render_stats.txt","a");
177  fprintf(fidstats, "%d, %d, %f\n",h*w,numThreads,tDuration);
178  fclose(fidstats);
179 
180  //Combine all the data
181  static const bool debugLines = false;
182  char renderFile[4096];
183  snprintf(renderFile, 4095,"./maps/%s/%s_render.dat",vectorMap->mapName.c_str(),vectorMap->mapName.c_str());
184  FILE* fid = fopen(renderFile,"w+");
185  printf("Saving to %s\n",renderFile);
186 
187  fwrite(&w,sizeof(int),1,fid);
188  fwrite(&h,sizeof(int),1,fid);
189  fwrite(&resolution,sizeof(double),1,fid);
190  for(int i=0; i<numThreads;i++){
191  const vector<vector<int> > &partScene = *sceneLines[i];
192  int numLocs = partScene.size();
193  for(int j=0; j<numLocs; j++){
194  int x = sceneLocations[i]->at(j).x;
195  int y = sceneLocations[i]->at(j).y;
196  if(debugLines){
197  printf("loc:%5d,%5d, lines:",x,y);
198  for(unsigned int k=0; k<partScene[j].size(); k++){
199  printf(" %3d", partScene[j][k]);
200  }
201  printf("\n");
202  }
203  fwrite(&x,sizeof(int),1,fid);
204  fwrite(&y,sizeof(int),1,fid);
205  int size = partScene[j].size();
206  fwrite(&size,sizeof(int),1,fid);
207  fwrite(partScene[j].data(),sizeof(int),size,fid);
208  }
209  }
210 }
211 
212 int main(int argc, char** argv)
213 {
214  //========================== Set up Command line parameters =======================
215  char *map_name = (char*) malloc(4096);
216  snprintf(map_name, 4095, "GHC7");
217 
218  static struct poptOption options[] = {
219  { "map-name", 'm', POPT_ARG_STRING , &map_name, 0, "Map name", "STRING"},
220  { "debug", 'd', POPT_ARG_INT, &debugLevel, 0, "Debug Level", "NUM"},
221  { "num-threads", 'n', POPT_ARG_INT, &numThreads, 0, "Num Threads", "NUM"},
222  { "render-resolution",'r', POPT_ARG_DOUBLE, &resolution, 0, "Render Resolution", "NUM"},
223 
224  POPT_AUTOHELP
225  { NULL, 0, 0, NULL, 0, NULL, NULL }
226  };
227  // parse options
228  POpt popt(NULL,argc,(const char**)argv,options,0);
229  int c;
230  while((c = popt.getNextOpt()) >= 0){
231  }
232 
233  //========================= Welcome screen, Load map & log ========================
234  ColourTerminal(TerminalUtils::TERMINAL_COL_GREEN,TerminalUtils::TERMINAL_COL_BLACK,TerminalUtils::TERMINAL_ATTR_BRIGHT);
235  printf("\nVector Localization Pre-Render Optimization\n\n");
236  ResetTerminal();
237 
238  printf("Map: %s\n",map_name);
239  printf("Num Threads: %d\n",numThreads);
240  //Read Map
241  vectorMap = new VectorMap(map_name,"./maps",false);
242 
243  InitHandleStop(&run);
244  PreRenderMultiThread();
245 
246  printf("closing.\n");
247  return 0;
248 }
Subroutines to spice up stdout.
void set(num nx, num ny)
set the components of the vector
Definition: gvector.h:768
C++ Interfaces: VectorMap, LineSection.
Definition: popt_pp.h:6
float minX
Map Extents.
Definition: vector_map.h:58
vector< int > getSceneLines(vector2f loc, float maxRange)
Get a set of lines which are visible from loc.
Definition: vector_map.cpp:392