Fix crash
[ryzomcore.git] / nel / tools / pacs / build_indoor_rbank / main.cpp
blob2e797d61dadd68a6173cca91121861e5e6adbf89
1 // NeL - MMORPG Framework <http://dev.ryzom.com/projects/nel/>
2 // Copyright (C) 2010 Winch Gate Property Limited
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as
6 // published by the Free Software Foundation, either version 3 of the
7 // License, or (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
14 // You should have received a copy of the GNU Affero General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <vector>
20 #include <stdlib.h>
22 #include "nel/misc/config_file.h"
23 #include "nel/misc/time_nl.h"
24 #include "nel/misc/file.h"
26 #include "nel/pacs/collision_mesh_build.h"
27 #include "nel/pacs/local_retriever.h"
28 #include "nel/pacs/retriever_instance.h"
29 #include "nel/pacs/global_retriever.h"
30 #include "nel/pacs/retriever_bank.h"
32 #include "nel/pacs/u_global_position.h"
34 #include "mouline.h"
36 using namespace std;
37 using namespace NLPACS;
38 using namespace NLMISC;
40 #ifndef NL_BIRB_CFG
41 #define NL_BIRB_CFG "."
42 #endif // NL_BIB_CFG
44 bool AddToRetriever = true;
45 bool Merge = true;
46 string MergePath = "c:/data_test/";
47 string MergeInputPrefix = "fyros";
48 string MergeOutputPrefix = "city_landscape_fyros";
49 string MeshPath = "c:/data_test/";
50 vector<string> Meshes;
51 string OutputPath = "c:/data_test/";
52 string OutputPrefix = "city_fyros";
55 CVector getZoneCenterById(uint16 id)
57 CAABBox bbox;
58 uint x, y;
59 const float zdim = 160.0f;
61 x = id%256;
62 y = id/256;
64 return CVector(zdim*((float)x+0.5f), -zdim*((float)y+0.5f), 0.0f);
67 uint32 getIdByCoord(uint x, uint y)
69 return y*256+x;
72 uint16 getZoneIdByName(string &name)
74 sint y = 0, x = 0;
75 const char *str = name.c_str();
77 while (*str != '_')
78 y = y*10 + *(str++)-'0';
80 ++str;
82 x = (str[0]-'A')*26+(str[1]-'A');
84 return (y-1)*256+x;
87 template<class A>
88 void serialAndSave(A &a, string fn)
90 COFile f;
91 f.open(fn);
92 f.serial(a);
93 f.close();
96 template<class A>
97 void openAndSerial(A &a, string fn)
99 CIFile f;
100 f.open(fn);
101 f.serial(a);
102 f.close();
107 // config management
108 int getInt(CConfigFile &cf, const string &varName)
110 CConfigFile::CVar &var = cf.getVar(varName);
111 return var.asInt();
114 string getString(CConfigFile &cf, const string &varName)
116 CConfigFile::CVar &var = cf.getVar(varName);
117 return var.asString();
120 void initConfig()
124 #ifdef NL_OS_UNIX
125 NLMISC::CPath::addSearchPath(NLMISC::CPath::getApplicationDirectory("NeL"));
126 #endif // NL_OS_UNIX
128 NLMISC::CPath::addSearchPath(NL_BIRB_CFG);
130 CConfigFile cf;
132 cf.load("build_indoor_rbank.cfg");
134 Merge = getInt(cf, "Merge") != 0;
135 AddToRetriever = getInt(cf, "AddToRetriever") != 0;
136 MergePath = getString(cf, "MergePath");
137 MergeInputPrefix = getString(cf, "MergeInputPrefix");
138 MergeOutputPrefix = getString(cf, "MergeOutputPrefix");
139 MeshPath = getString(cf, "MeshPath");
141 OutputPath = getString(cf, "OutputPath");
142 OutputPrefix = getString(cf, "OutputPrefix");
144 CConfigFile::CVar &meshes = cf.getVar("Meshes");
145 uint i;
146 for (i=0; i<(uint)meshes.size(); i++)
147 Meshes.push_back(meshes.asString(i));
149 catch (const EConfigFile &e)
151 printf ("Problem in config file : %s\n", e.what ());
158 void makeGlobalRetriever(vector<CVector> &translation)
160 CGlobalRetriever gr;
161 CRetrieverBank rb;
163 if (Merge)
165 openAndSerial(rb, MergePath+MergeInputPrefix+".rbank");
166 gr.setRetrieverBank(&rb);
167 openAndSerial(gr, MergePath+MergeInputPrefix+".gr");
169 else
171 gr.setRetrieverBank(&rb);
172 gr.init();
175 TTime start, end;
177 start = CTime::getLocalTime();
179 vector<uint> ninst;
181 uint i;
182 for (i=0; i<Meshes.size(); ++i)
184 CLocalRetriever lr;
187 openAndSerial(lr, OutputPath+Meshes[i]+".lr");
189 uint rid = rb.addRetriever(lr);
191 if (AddToRetriever)
193 uint iid = (gr.makeInstance(rid, 0, -translation[i])).getInstanceId();
195 ninst.push_back(iid);
198 catch (const Exception &e)
200 nlwarning("WARNING: can't merge lr '%s.lr': %s", Meshes[i].c_str(), e.what());
204 gr.initQuadGrid();
206 for (i=0; i<ninst.size(); ++i)
207 gr.makeLinks(ninst[i]);
210 end = CTime::getLocalTime();
211 nlinfo("instance making and linking work time: %.3f", (double)(end-start)/1000.0);
213 COFile output;
216 * This way, at the end of the build process,
217 * the rbank should be 5 bytes only and lr should be saved as well next to the rbank
220 if (Merge)
222 serialAndSave(gr, OutputPath+MergeOutputPrefix+".gr");
223 rb.saveShortBank(OutputPath, MergeOutputPrefix, true); // save 5 bytes rbank and lr as well
225 // serialAndSave(rb, OutputPath+MergeOutputPrefix+".rbank");
227 else
229 serialAndSave(gr, OutputPath+OutputPrefix+".gr");
230 rb.saveShortBank(OutputPath, OutputPrefix, true); // save 5 bytes rbank and lr as well
232 // serialAndSave(rb, OutputPath+OutputPrefix+".rbank");
235 gr.check();
238 UGlobalPosition gpos;
240 gpos = gr.retrievePosition(CVector(18630, -24604, 0));
241 const CLocalRetriever &lr = gr.getRetriever(gr.getInstance(gpos.InstanceId).getRetrieverId());
242 lr.dumpSurface(gpos.LocalPosition.Surface, gr.getInstance(gpos.InstanceId).getOrigin());
246 void createRetriever(vector<CVector> &translation)
248 uint i;
250 translation.resize(Meshes.size());
252 for (i=0; i<Meshes.size(); ++i)
254 CLocalRetriever lr;
255 CCollisionMeshBuild cmb;
257 string meshName = Meshes[i];
259 nlinfo("compute retriever %s", meshName.c_str());
263 openAndSerial(cmb, MeshPath+meshName+".cmb");
266 vector<bool> usedMaterials;
267 uint j;
268 uint maxMat = 0;
270 for (j=0; j<cmb.Faces.size(); ++j)
271 if (cmb.Faces[j].Material > (sint)maxMat)
272 maxMat = cmb.Faces[j].Material;
274 usedMaterials.resize(maxMat+1);
276 for (j=0; j<usedMaterials.size(); ++j)
277 usedMaterials[j] = false;
278 for (j=0; j<cmb.Faces.size(); ++j)
279 usedMaterials[cmb.Faces[j].Material] = true;
281 for (j=0; j<usedMaterials.size(); ++j)
282 if (usedMaterials[j])
283 nlinfo("Material %d used", j);
287 computeRetriever(cmb, lr, translation[i], true);
289 // TEMP
290 uint tt;
291 for (tt=0; tt<lr.getSurfaces().size(); ++tt)
292 lr.dumpSurface(tt);
296 // Compute an identifier name
297 string indentifier = meshName;
298 string::size_type sharpPos = indentifier.rfind ('#');
299 if (sharpPos != string::npos)
300 indentifier = indentifier.substr (0, sharpPos);
301 lr.setIdentifier(indentifier);
303 // Save the lr file
304 serialAndSave(lr, OutputPath+meshName+".lr");
306 catch (const Exception &e)
308 nlwarning("WARNING: can compute lr '%s.lr': %s", meshName.c_str(), e.what());
314 // pacs test
316 void serialGPos(UGlobalPosition &gp, NLMISC::IStream &f)
318 f.serial(gp.InstanceId, gp.LocalPosition.Surface, gp.LocalPosition.Estimation);
321 struct CGlobalPositionCompare
323 UGlobalPosition ClientPosition,
324 RetrievedPosition;
325 bool Compare;
327 void serial(NLMISC::IStream &f)
329 serialGPos(ClientPosition, f);
330 serialGPos(RetrievedPosition, f);
331 f.serial(Compare);
335 vector<CGlobalPositionCompare> StoredPosition;
338 void loadAndDumpPositions()
340 CIFile f;
342 f.open("backup.position");
343 f.serialCont(StoredPosition);
344 f.close();
346 uint i;
347 for (i=0; i<StoredPosition.size(); ++i)
349 CGlobalPositionCompare &c = StoredPosition[i];
350 nlinfo("cmp=%s cl(%4d,%4d,%.8f,%.8f,%.8f) gr(%4d,%4d,%.8f,%.8f,%.8f)",
351 c.Compare ? "true" : "false",
352 c.ClientPosition.InstanceId, c.ClientPosition.LocalPosition.Surface, c.ClientPosition.LocalPosition.Estimation.x, c.ClientPosition.LocalPosition.Estimation.y, c.ClientPosition.LocalPosition.Estimation.z,
353 c.RetrievedPosition.InstanceId, c.RetrievedPosition.LocalPosition.Surface, c.RetrievedPosition.LocalPosition.Estimation.x, c.RetrievedPosition.LocalPosition.Estimation.y, c.RetrievedPosition.LocalPosition.Estimation.z);
360 int main(int argc, char **argv)
362 CRetrieverBank bank;
363 CIFile f("R:/code/ryzom/data/3d/continents/matis/pacs/matis.rbank");
365 f.serial(bank);
367 bank.saveShortBank("R:/code/ryzom/data/3d/continents/matis/pacs", "matis");
372 int main(int argc, char **argv)
374 vector<CVector> translation;
375 TTime start, end;
377 createDebug ();
378 ErrorLog->removeDisplayer("DEFAULT_MBD");
380 initConfig();
382 start = CTime::getLocalTime();
383 createRetriever(translation);
385 // translation[0] = CVector(-4670.0f, 3579.0f, 0.0f);
387 makeGlobalRetriever(translation);
388 end = CTime::getLocalTime();
390 nlinfo("%.3f seconds work", (double)(end-start)/1000.0);
392 uint i;
393 for (i=0; i<translation.size(); ++i)
394 nlinfo("CVector\ttranslation_%d = CVector(%.1ff, %.1ff, %.1ff);", i, translation[i].x, translation[i].y, translation[i].z);
396 return 0;