ArchitectureManager.h
1 /************************************************************
2 * (C) Voxel Farm Inc. 2015
3 */
4 
5 #pragma once
6 
7 // 260 was taken from windef.h
8 #ifndef MAX_PATH
9  #define MAX_PATH 260
10 #endif
11 
12 #include "mapindex.h"
13 #include "Vector.h"
14 #include "MatrixAlg.h"
15 #include "FastQuadrics.h"
16 #include "Grammar.h"
17 #include "Scene.h"
18 #include "VoxelLayer.h"
19 #include "RTree.h"
20 #include "ExternalMutex.h"
21 #include <map>
22 #include <set>
23 #include <vector>
24 #include "StampMesh.h"
25 
26 namespace VoxelFarm
27 {
29  namespace Architecture
30  {
33  {
42  };
43 
47  const int ARCH_MAX_LEVEL = 9;
48 
49  struct Entity;
50 
53  {
57  int material;
67  double minBounds[3];
69  double maxBounds[3];
70  };
71 
73  typedef TVector<InstancedMesh*> InstanceArray;
74 
77 
79  struct Entity
80  {
82  int classId;
84  double position[3];
86  double orientation[3];
88  double size[3];
90  TMap<int, InstanceIndex*> lod;
92  TMap<String, int>* materials;
94  double groundLevel;
96  double decay;
97  };
98 
100  typedef TVector<Entity*> EntityArray;
101 
104 
107  {
108  Entity* entity;
109  int lod;
110  };
111 
114  {
115  int x, y, z;
116  int head;
117  int tail;
118  int count;
119  };
120 
123  {
124  Algebra::Vector position;
125  Algebra::Vector normal;
126  int material;
127  int next;
128  };
129 
131  struct RayTest
132  {
133  double pos;
134  bool facing;
135  double normal[3];
136  int next;
137  };
138 
141 
143  const unsigned int MAX_TEST_INTERSECTIONS = 0x7FFFF;
144 
147  {
158 
165 
168  };
169 
171  struct CPrefabDesc
172  {
173  //prefab file name
174  char name[MAX_PATH];
176  int id;
178  double xsize;
180  double ysize;
182  double zsize;
184  double minxsize;
186  double minysize;
188  double minzsize;
189  };
190 
193  {
194  public:
197 
198  void init(int prefabCount);
199  CPrefabDesc& getPrefab(int index)
200  {
201  return prefabIndex[index];
202  }
203 
204  public:
209  };
210 
211 
214  {
215  //table of materials
216  TMap<String, int>* materials;
217  };
218 
221  {
222  public:
225 
226  void init(int paletteCount);
227  CPaletteDesc& getPalette(int index)
228  {
229  return paletteIndex[index];
230  }
231 
232  public:
237  };
238 
241  {
242  public:
243  CArchitectureMesh(InstanceArray* instances, Algebra::Quaternion boxRotations);
244  virtual int getSolidCount();
245  virtual MaterialId getSolidMaterial(int solid);
246  virtual int getFaceCount(int solid);
247  virtual void getFace(int solid, int index, Algebra::Vector& v0, Algebra::Vector& v1, Algebra::Vector& v2);
248  private:
250  InstanceArray* meshInstances;
252  Algebra::Matrix rotate_transform;
253  };
254  class CArchitectureManager;
255 
258  {
259  public:
261  virtual void generateEntities(
263  CellId cell,
265  CArchitectureManager& architectureManager) = 0;
266  };
267 
270  {
271  public:
272  CArchitectureManager(void);
273  virtual ~CArchitectureManager(void);
274  public:
276  virtual void planJobs(Scene* scene) override;
278  virtual void runJobs() override;
280  void loadModule(int id, char* filename);
282  void loadPrefab(int id, char* filename);
284  void loadMesh(String id, char* filename);
286  void addEntity(
288  double position[3],
290  double size[3],
292  double orientation[3],
294  int classId,
296  TMap<String, int>* materials,
298  double groundLevel,
300  double decay);
301  virtual void getContourData(
303  CellId cell,
307  bool& empty,
308  void* threadContext) override;
310  virtual void* createThreadContext() override;
312  virtual void disposeThreadContext(void* threadContext) override;
313  virtual int getStatsContourId() override
314  {
315  return STAT_ARCH;
316  }
317  virtual int getStatsRunJobsId() override
318  {
319  return STAT_LSYS_RUN;
320  }
322  bool volumeIsClear(double minVol[3], double maxVol[3]);
324  void generateEntities(CellId cell);
326  void registerGenerator(IEntityGenerator* generator);
328  int prefabCount();
329  public:
331  ExternalMutex::Mutex instanceLock;
332  EntityIndex entityIndex;
333  private:
334  TVector<Entity*> entityIndexObjects;
335  private:
336  // class info
337  TMap<String, CFastQuadrics*> meshes;
338  TMap<int, std::pair<CGrammar*, CModule*>> modules;
339  TMap<int, std::pair<CGrammar*, CModule*>> prefabs;
340  private:
341  TMap<CellId, InstanceArray> instanceCache;
342  TMap<CellId, EntityArray> cellToEntityMap;
343  int inprogress;
344  TSet<std::pair<int, Entity*>> pending;
345  TVector<GenerationRequest*> pendingOrder;
346  TSet<GenerationRequest*> processing;
347  TSet<CellId> generatedCells;
348  TMap<String, int>* defaultMaterials;
349  TVector<IEntityGenerator*> generators;
350  public:
351  struct EntityAABB
352  {
353  int type;
354  Entity* entity;
355  double minBounds[3];
356  double maxBounds[3];
357  };
358  TVector<EntityAABB> allEntities;
359  public:
360  InstanceArray previewModel(int id, double boxSize[3], Algebra::Quaternion boxRotations);
361  void stampModel(
362  int id,
363  CBlockData* blockData,
364  IMeshStamMaterialSource* materials,
365  const double boxSize[3],
366  Algebra::Quaternion boxRotations,
367  const double boxWorldPos[3],
368  TSet<CellId>* changedCells,
369  PaletteType type,
370  CPaletteDesc* palette);
371 
372  };
373 
375  CFastQuadrics* loadMesh(char* filename);
376 
377  }
378 }
RTree< Entity *, double, 3, double, 32 > EntityIndex
RTree index to speed up spacial queries for entities.
A thread context for the archicteture system. A single instance of the architecture system can be sha...
int prefabCount()
Returns the count of loaded prefabs.
double decay
An entropy value to simulate decay and breackage for the entity.
double xsize
Indentifies the ideal X dimention.
Tracks a single instance of a mesh. An architecture entity will be composed of many mesh instances...
const int MASTER_ARCH_GENERATION_LEVEL
Octree level at which architecture is generated.
Algebra::Vector size
Scale for the instance.
Entity * entity
Reference to the entity to which the instance belongs.
CPaletteDesc * paletteIndex
An index to all palettes.
double ysize
Indentifies the ideal Y dimention.
int intersectionListCount
Total number of intersection lists.
TMap< String, int > * materials
A map that resolves alphanumeric material IDs as defined in the grammar source code to numeric materi...
double groundLevel
Height of the ground level for the instance.
const unsigned int MAX_INTERSECTIONS
Limit for the intersections to be tracked during voxelization.
Contains all classes and functions for the VoxelFarm engine.
const int BLOCK_SIZE
Actual number of voxels in one Cell Dim once the margin is considered.
Definition: VoxelLayer.h:38
int id
Identifies the prefab id in the prefabs collection in architecture.
Tracks a generation request for an entity.
int rayTestCount
Number of ray tests records in use.
A 3D Vector.
Definition: Vector.h:34
virtual int getStatsRunJobsId() override
Returns the stats ID to be used for measuring performance of the runJobs() method.
set material to 0 for deleting with the instance
A default implementation of IBlockData that also acts as a VoxelLayer.
Definition: BlockData.h:182
double maxBounds[3]
Maximum bounds for the instance.
An Entity is an unique architecture element. The entity struct tracks the location and type of the en...
const int ARCH_MAX_LEVEL
Max Octree level at which architecture is voxelized and returned for contouring.
Architecture system. It keeps a list of entities, their classes and the mesh instances they have prod...
double minxsize
Indentifies the minimum X dimention.
do not use a palette...take the material from the entity material list
It allows to access to the faces and materials of a list of solids.
Definition: StampMesh.h:31
This interface allows the architecture engine to delegate the creation of entities to the application...
ScopeType scopeType
Type of scope for the instance (Box or Prism)
TVector< InstancedMesh * > InstanceArray
A list of instances.
void registerGenerator(IEntityGenerator *generator)
Registers an IEntityGenerator.
bool volumeIsClear(double minVol[3], double maxVol[3])
Returns wheter there are entities in the specified volume.
double orientation[3]
Orientation for the entity in degrees over each main coordinate axis.
unsigned __int64 CellId
A 64bit integer that identifies a single world octree Cell.
Definition: mapindex.h:23
CFastQuadrics * loadMesh(char *filename)
Loads a mesh from a file (*.dat)
virtual int getFaceCount(int solid)
Returns the number of faces in a solid.
Cells Scene
A Scene is a set of cells.
Definition: scene.h:12
int rayTestHead
Index for the first item in the test list.
An interface for a voxel layer. By implementing this interface, very different modules can contribute...
Definition: VoxelLayer.h:535
void loadMesh(String id, char *filename)
Loads and registers a mesh file (*.dat) The provided ID is the one used in the "instance" commands in...
TMap< int, InstanceIndex * > lod
Index of all the instances that were produced for the entity, grouped by architecture LOD...
int * intersectionIndex
A 3D index that links a voxels to the list of intersections found for it.
double * cellHeightCache
A buffer to cache height data.
int classId
Class identifier for the entity. Grammar rules to generate the entity are defined at the class level...
virtual int getSolidCount()
Returns the number of solids.
virtual MaterialId getSolidMaterial(int solid)
Returns the material of a solid.
virtual void getFace(int solid, int index, Algebra::Vector &v0, Algebra::Vector &v1, Algebra::Vector &v2)
Get the vertices of a face of a solid.
Algebra::Matrix transform
Position and rotation of the instance.
void loadPrefab(int id, char *filename)
Loads and registers a grammar prefab with the specified class ID. The filename must point to a compil...
A Matrix for 3D operations.
Definition: MatrixAlg.h:38
int material
Material for the instance.
RayIntersection * intersections
A buffer to store intersection nodes.
double minzsize
Indentifies the minimum Z dimention.
double minysize
Indentifies the minimum Y dimention.
virtual void runJobs() override
Processes pending generation requests. This is called by the contouring loop. Pending entities will b...
virtual void disposeThreadContext(void *threadContext) override
Disposes an architecture thread context.
take as material the user material in use
A prefab definition for the VoxelFarm.com engine.
void loadModule(int id, char *filename)
Loads and registers a grammar module with the specified class ID. The filename must point to a compil...
PaletteType
Type of palette of materials when collecting instances.
virtual void * createThreadContext() override
Creates a new instance of an architecture thread context.
double minBounds[3]
Minimum bounds for the instance.
Contains all palette descriptions available in the system.
virtual void getContourData(CellId cell, VoxelFarm::ContourVoxelData *data, bool &empty, void *threadContext) override
Returns voxel data for the specified cell.
A record that tracks intersection points along one ray.
RayTest * rayTests
A buffer to store ray test records.
CFastQuadrics * mesh
A reference to a polygonal mesh for the instance.
double size[3]
Scale for the entity. Determines the size of the initial scope used by the generation system...
void generateEntities(CellId cell)
Uses the registered IEntityGenerator to generate entities for the cell.
RayIntersectionList * intersectionLists
A buffer to store intersection lists.
Contains all prefab descriptions available in the system.
RTree< InstancedMesh *, double, 3, double, 32 > InstanceIndex
An RTree index to accelerate spatial instance queries.
ScopeType
Enumerates possible scope types.
Definition: Grammar.h:67
int intersectionCount
Total number of intersections found.
CPrefabDesc * prefabIndex
An index to all prefab.
virtual void generateEntities(CellId cell, CArchitectureManager &architectureManager)=0
Generates entities for the specified cell.
double position[3]
Position for the entity in world coordinates.
This class allows to access meshes in a list of instances.
const unsigned int MAX_TEST_INTERSECTIONS
Limit for the intersections to be tracked along one ray.
An efficient mesh representation that features Quadratic Error function simplification using a Multip...
Definition: FastQuadrics.h:44
choose the material from a palette of materials
An intersection list produced during the voxelization process.
virtual int getStatsContourId() override
Returns the stats ID to be used for measuring performance of the getContourData() method...
A palette of materials definition for the VoxelFarm.com engine.
virtual void planJobs(Scene *scene) override
Processes a scene to get new entities from it. This is called by the scene creation loop...
void addEntity(double position[3], double size[3], double orientation[3], int classId, TMap< String, int > *materials, double groundLevel, double decay)
Adds one entity.
double zsize
Indentifies the ideal Z dimention.
A Quaternion object.
Definition: Quaternion.h:36
TVector< Entity * > EntityArray
A list of entities.
A record used to create intersection lists during the voxelization process.
It translates a given material depending of the position in the world.
Definition: StampMesh.h:49