Merge branch 'fixes' into main/gingo-test
[ryzomcore.git] / nel / tools / pacs / build_rbank / prim_checker.h
blob80a0043d685d956a6ce8e1e84dc325202c68f0e1
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/>.
17 #ifndef NL_PRIM_CHECKER_H
18 #define NL_PRIM_CHECKER_H
20 #include "nel/misc/types_nl.h"
21 #include "nel/misc/stream.h"
23 #include <nel/misc/polygon.h>
25 namespace NLMISC
27 class CVectorD;
30 namespace NLLIGO
32 class IPrimitive;
33 class CPrimZone;
36 /**
37 * A class that reads .primitive files from ligo and that allows
38 * to define flags over pacs surfaces with CPrimZone
39 * \author Benjamin Legros
40 * \author Nevrax France
41 * \date 2003
43 class CPrimChecker
45 public:
47 /// Constructor
48 CPrimChecker();
51 /// Bits value for landscape
52 enum
54 Include = 1,
55 Exclude = 2,
56 ClusterHint = 4,
57 Water = 8,
58 Cliff = 16
62 /// Init CPrimChecker
63 bool build(const std::string &primitivesPath, const std::string &igLandPath, const std::string &igVillagePath, const std::string &outputDirectory = "./", bool forceRebuild = false);
65 /// load CPrimChecker state
66 bool load(const std::string &outputDirectory = "./");
68 /// Reads bits value at a given position
69 uint8 get(uint x, uint y) const { return _Grid.get(x, y); }
71 /// Reads bits value at a given position
72 uint16 index(uint x, uint y) const { return _Grid.index(x, y); }
74 /// Gete water height
75 float waterHeight(uint index, bool &exists) const
77 if (index >= _WaterHeight.size())
79 exists = false;
80 return 0.0;
82 exists = true;
83 return _WaterHeight[index];
86 /// Render a CPolygon of bit value
87 void renderBits(const NLMISC::CPolygon &poly, uint8 bits);
89 private:
91 /// \name Grid management
92 //@{
94 /**
95 * A class that allows to store 65536x65536 elements with minimum allocation and dynamic allocation of elements
97 class CGrid
99 public:
101 /// Constructor
102 CGrid() { clear(); }
104 /// Set bits in grid
105 void set(uint x, uint y, uint8 bits)
107 CGridCell *cell = _Grid[((x&0xff00)>>8) + (y&0xff00)];
108 if (cell == NULL)
110 cell = new CGridCell();
111 _Grid[((x&0xff00)>>8) + (y&0xff00)] = cell;
113 cell->set(x, y, bits);
116 /// Get bits in grid
117 uint8 get(uint x, uint y) const
119 CGridCell *cell = _Grid[((x&0xff00)>>8) + (y&0xff00)];
120 return cell != NULL ? cell->get(x, y) : (uint8)0;
123 /// Set bits in grid
124 void index(uint x, uint y, uint16 idx)
126 CGridCell *cell = _Grid[((x&0xff00)>>8) + (y&0xff00)];
127 if (cell == NULL)
129 cell = new CGridCell();
130 _Grid[((x&0xff00)>>8) + (y&0xff00)] = cell;
132 cell->index(x, y, idx);
135 /// Get bits in grid
136 uint16 index(uint x, uint y) const
138 CGridCell *cell = _Grid[((x&0xff00)>>8) + (y&0xff00)];
139 return cell != NULL ? cell->index(x, y) : (uint16)0;
143 /// Clear grid
144 void clear()
146 for (uint i=0; i<256*256; ++i)
147 if (_Grid != NULL)
149 delete _Grid[i];
150 _Grid[i] = NULL;
154 /// Serializes
155 void serial(NLMISC::IStream &f)
157 f.serialCheck(NELID("PCHK"));
158 f.serialVersion(0);
160 if (f.isReading())
161 clear();
163 for (uint i=0; i<256*256; ++i)
165 bool present = (_Grid[i] != NULL);
166 f.serial(present);
168 if (present)
170 if (_Grid[i] == NULL)
171 _Grid[i] = new CGridCell();
172 _Grid[i]->serial(f);
177 private:
180 * A class that allows to store 256x256 elements
182 class CGridCell
184 class CGridElm
186 uint16 _Value;
188 public:
190 CGridElm() : _Value(0) {}
192 uint8 flags() const { return (uint8)((_Value >> 11) & 0x1f); }
193 void flags(uint8 bits) { _Value |= (((uint16)bits) << 11); }
194 uint16 index() const { return _Value & 0x07ff; }
195 void index(uint16 idx) { _Value = ((idx & 0x07ff) | (_Value & 0xf800)); }
197 void serial(NLMISC::IStream &f) { f.serial(_Value); }
200 public:
202 /// Constructor
203 CGridCell() {}
205 /// Set bits in grid
206 void set(uint x, uint y, uint8 bits) { _Grid[(x&0xff) + ((y&0xff)<<8)].flags(bits); }
208 /// Get bits in grid
209 uint8 get(uint x, uint y) const { return _Grid[(x&0xff) + ((y&0xff)<<8)].flags(); }
213 uint16 index(uint x, uint y) { return _Grid[(x&0xff) + ((y&0xff)<<8)].index(); }
215 void index(uint x, uint y, uint idx) { _Grid[(x&0xff) + ((y&0xff)<<8)].index(idx); }
218 /// Serializes
219 void serial(NLMISC::IStream &f) { for (uint i=0; i<256*256; ++i) f.serial(_Grid[i]); }
221 private:
222 CGridElm _Grid[256*256];
225 CGridCell *_Grid[256*256];
228 CGrid _Grid;
230 std::vector<float> _WaterHeight;
232 //@}
236 /// Reads a primitive file and renders contained primitives into grid
237 void readFile(const std::string &filename);
239 /// Reads a primitive and its sub primitives
240 void readPrimitive(NLLIGO::IPrimitive *primitive);
242 /// Renders a primitive
243 void render(NLLIGO::CPrimZone *zone, uint8 bits);
245 /// Render a water shape, as a CPolygon
246 void render(const NLMISC::CPolygon &poly, uint16 value);
250 #endif // NL_PRIM_CHECKER_H
252 /* End of prim_checker.h */