14 #define VECTOR_OVERFLOW
19 #ifdef VECTOR_OVERFLOW
22 const double VECTOR_MAX_VALUE = 2.0;
24 const double VECTOR_MIN_VALUE = 0.0;
26 const double VECTOR_MAX_VALUE = 1.0;
28 const double VECTOR_MAX_CODE = 255.0 / (VECTOR_MAX_VALUE -
VECTOR_MIN_VALUE);
29 const double VECTOR_DEFAULT_VALUE = ((int)(floor((VECTOR_MAX_CODE*(0.5-VECTOR_MIN_VALUE)) + 0.5))) / VECTOR_MAX_CODE +
VECTOR_MIN_VALUE;
44 typedef unsigned char VoxelType;
45 typedef unsigned short MaterialId;
47 const VoxelType VOXEL_HAS_MATERIAL = 0x01;
48 const VoxelType VOXEL_HAS_VECTOR = 0x02;
49 const VoxelType VOXEL_HAS_ROTATION = 0x04;
70 template <
int xDim,
int yDim,
int zDim,
typename IdxType> IdxType
getCellBlockIndex(
int x,
int y,
int z)
72 return IdxType((xDim * yDim * z) + (yDim * x) + y);
75 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
79 enum { cSize = xDim * yDim * zDim };
81 enum { cDimX = xDim };
82 enum { cDimY = yDim };
83 enum { cDimZ = zDim };
87 Index(
int idx) : index(static_cast<IdxType>(idx)) {}
88 Index(
int x,
int y,
int z) : index(IdxType(getCellBlockIndex<xDim, yDim, zDim, IdxType>(x, y, z))) {}
89 operator IdxType()
const
96 void setVoxel(
const Index& index,
const Voxel& voxel);
97 void getVoxel(
const Index& index,
Voxel& voxel)
const;
109 const Index& index)
const;
112 VoxelType type)
const;
119 MaterialId material);
163 const Index& index)
const;
193 unsigned char& dz)
const;
223 unsigned char& dz)
const;
234 MaterialId* getMaterials()
238 const MaterialId* getMaterials()
const
242 static int getMaterialsSize()
244 return sizeof(MaterialId) * cSize;
246 unsigned char* getVectors()
248 return (
unsigned char*)vectors;
250 const unsigned char* getVectors()
const
252 return (
unsigned char*)vectors;
254 static int getVectorsSize()
256 return sizeof(
unsigned char) * cAxes * cSize;
258 unsigned char* getRotations()
260 return (
unsigned char*)vectors;
262 const unsigned char* getRotations()
const
264 return (
unsigned char*)rotations;
266 static int getRotationsSize()
268 return sizeof(
unsigned char) * cAxes * cSize;
271 VoxelType types[cSize];
272 MaterialId materials[cSize];
273 unsigned char vectors[cSize][cAxes];
274 unsigned char rotations[cSize][cAxes];
276 enum { cSerialSize = cSize*(
sizeof(VoxelType) +
sizeof(MaterialId) + cAxes + cAxes) };
277 void serialize(
unsigned char* buffer)
const;
278 void unserialize(
unsigned char* buffer);
281 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
282 void VoxelData<xDim, yDim, zDim, IdxType>::setVoxel(
const Index& index,
const Voxel& voxel)
284 types[index] = voxel.type;
285 materials[index] = voxel.material;
286 vectors[index][0] = voxel.vx;
287 vectors[index][1] = voxel.vy;
288 vectors[index][2] = voxel.vz;
289 rotations[index][0] = voxel.rx;
290 rotations[index][1] = voxel.ry;
291 rotations[index][2] = voxel.rz;
294 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
295 void VoxelData<xDim, yDim, zDim, IdxType>::getVoxel(
const Index& index, Voxel& voxel)
const
297 voxel.type = types[index];
298 voxel.material = materials[index];
299 voxel.vx = vectors[index][0];
300 voxel.vy = vectors[index][1];
301 voxel.vz = vectors[index][2];
302 voxel.rx = rotations[index][0];
303 voxel.ry = rotations[index][1];
304 voxel.rz =rotations[index][2];
307 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
308 void VoxelData<xDim, yDim, zDim, IdxType>::setType(
const Index& index, VoxelType type)
313 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
314 void VoxelData<xDim, yDim, zDim, IdxType>::addType(
const Index& index, VoxelType type)
316 types[index] |= type;
319 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
320 void VoxelData<xDim, yDim, zDim, IdxType>::removeType(
const Index& index, VoxelType type)
322 types[index] &= ~type;
325 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
326 VoxelType VoxelData<xDim, yDim, zDim, IdxType>::getType(
const Index& index)
const
331 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
332 bool VoxelData<xDim, yDim, zDim, IdxType>::hasType(
const Index& index, VoxelType type)
const
334 return (types[index] & type) != 0;
337 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
340 materials[index] = material;
341 types[index] |= VOXEL_HAS_MATERIAL;
350 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
358 types[index] |= VOXEL_HAS_VECTOR;
361 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
364 vectors[index][0] = dx;
365 vectors[index][1] = dy;
366 vectors[index][2] = dz;
368 types[index] |= VOXEL_HAS_VECTOR;
371 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
375 rotations[index][0] = (
unsigned char)(255.0*dx);
376 rotations[index][1] = (
unsigned char)(255.0*dy);
377 rotations[index][2] = (
unsigned char)(255.0*dz);
378 types[index] |= VOXEL_HAS_VECTOR;
381 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
384 rotations[index][0] = dx;
385 rotations[index][1] = dy;
386 rotations[index][2] = dz;
387 types[index] |= VOXEL_HAS_VECTOR;
390 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
393 return materials[index];
396 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
404 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
412 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
415 dx = vectors[index][0];
416 dy = vectors[index][1];
417 dz = vectors[index][2];
420 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
423 dx = rotations[index][0] / 255.0;
424 dy = rotations[index][1] / 255.0;
425 dz = rotations[index][2] / 255.0;
428 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
431 dx = rotations[index][0] / 255.0f;
432 dy = rotations[index][1] / 255.0f;
433 dz = rotations[index][2] / 255.0f;
436 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
439 dx = rotations[index][0];
440 dy = rotations[index][1];
441 dz = rotations[index][2];
444 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
447 memset(types, 0, cSize*
sizeof(VoxelType));
453 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
456 memcpy(types, source.types, cSize*
sizeof(VoxelType));
457 memcpy(materials, source.materials, cSize*
sizeof(MaterialId));
458 memcpy(vectors, source.vectors, 3*cSize);
459 memcpy(rotations, source.rotations, 3*cSize);
477 STAT_TRIANGLES_MEDIUM_0,
478 STAT_TRIANGLES_MEDIUM_1,
479 STAT_TRIANGLES_MEDIUM_2,
480 STAT_TRIANGLES_MEDIUM_3,
481 STAT_TRIANGLES_BILLBOARDS,
488 STAT_CUSTOM_JOB_PLAN,
495 STAT_CONTOUR_LOOP_IDLE,
497 STAT_SCENE_DELTA_REQUEST,
503 STAT_SCENE_ADD_1_FAN,
504 STAT_SCENE_ADD_2_FAN,
505 STAT_SCENE_ADD_3_FAN,
506 STAT_SCENE_ADD_4_FAN,
511 STAT_SCENE_REPAIR_1_FAN,
512 STAT_SCENE_REPAIR_2_FAN,
513 STAT_SCENE_REPAIR_3_FAN,
514 STAT_SCENE_REPAIR_4_FAN,
519 STAT_SCENE_REMOVE_1_FAN,
520 STAT_SCENE_REMOVE_2_FAN,
521 STAT_SCENE_REMOVE_3_FAN,
522 STAT_SCENE_REMOVE_4_FAN,
524 STAT_MAT_INSTANCE_TRIANGLES,
525 STAT_SCENE_CLEAR_USER_1,
539 virtual void getContourData(
543 ContourVoxelData* data,
546 void* threadContext) = 0;
566 return STAT_CUSTOM_JOB_PLAN;
571 return STAT_CUSTOM_JOB_RUN;
585 virtual double getMaskValue(
597 const int VOXEL_BITS_MATERIAL = 16;
603 const int VOXEL_EMPTY = 0;
605 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
609 memcpy(buffer, types, cSize*
sizeof(VoxelType));
610 pos += cSize*
sizeof(VoxelType);
611 for (
int i = 0; i < cSize; i++)
613 MaterialId* dest = (MaterialId*)&buffer[pos];
614 if ((types[i] & VOXEL_HAS_MATERIAL) != 0)
616 *dest = materials[i];
622 pos +=
sizeof(MaterialId);
624 for (
int channel = 0; channel < 3; channel++)
625 for (
int i = 0; i < cSize; i++)
626 if ((types[i] & VOXEL_HAS_VECTOR) != 0)
628 buffer[pos++] = vectors[i][channel];
634 for (
int channel = 0; channel < 3; channel++)
635 for (
int i = 0; i < cSize; i++)
636 if ((types[i] & VOXEL_HAS_ROTATION) != 0)
638 buffer[pos++] = rotations[i][channel];
646 template <
int xDim,
int yDim,
int zDim,
typename IdxType>
647 void VoxelData<xDim, yDim, zDim, IdxType>::unserialize(
unsigned char* buffer)
650 memcpy(types, buffer, cSize*
sizeof(VoxelType));
651 pos += cSize*
sizeof(VoxelType);
652 memcpy(materials, &buffer[pos], cSize*
sizeof(MaterialId));
653 pos += cSize*
sizeof(MaterialId);
654 for (
int channel = 0; channel < 3; channel++)
655 for (
int i = 0; i < cSize; i++)
657 vectors[i][channel] = buffer[pos++];
659 for (
int channel = 0; channel < 3; channel++)
660 for (
int i = 0; i < cSize; i++)
662 rotations[i][channel] = buffer[pos++];
678 extern TVector<DebugPoint> debugPoints;
679 extern TVector<DebugLine> debugLines;
const int BLOCK_DIMENSION
Number of voxels along one Cell Dim.
unsigned char encodeVectorCoord(double coord)
It encodes a double coord in an unsigned char one.
const int BLOCK_MARGIN
Number of voxels a Cell will overlap to its neighbors.
virtual void planJobs(Scene *scene)
This is called when a new scene is discovered. It can be used to create objects that will be later re...
Contains all classes and functions for the VoxelFarm engine.
virtual void disposeThreadContext(void *threadContext)
Destroys the specified thread context.
An interface for a 3D mask. A mask can be used to control the application of other features...
const int BLOCK_SIZE
Actual number of voxels in one Cell Dim once the margin is considered.
time_t time
Used to track elapsed time.
IdxType getCellBlockIndex(int x, int y, int z)
Return the index into the cell blocks for a given x, y, z offset.
MaterialId getMaterial(const Index &index) const
Gets the value of a voxel material.
const int MAX_BLOCK_LOD
Maximum level to retain block data.
virtual void runJobs()
Execute any additional generation task.
VoxelType * getTypes()
Raw data interfaces.
void setRotation(const Index &index, double dx, double dy, double dz)
Sets the rotation of the voxel using doubles.
void adjustCellCoordinates(CellId &cell, int &x, int &y, int &z)
Checks the provided coordinates to make sure the fall within the cell's range. If they are outside ra...
virtual bool isCacheable()
Returns true if the layer's output can be cached. Return true if the output for a cell will always be...
unsigned __int64 CellId
A 64bit integer that identifies a single world octree Cell.
void clear()
Initializes the blocks.
unsigned int count
Used to count occurrences.
Cells Scene
A Scene is a set of cells.
An interface for a voxel layer. By implementing this interface, very different modules can contribute...
void getVector(const Index &index, double &dx, double &dy, double &dz) const
Gets the interior point of the voxel as doubles.
const int BLOCK_CUBE_SIZE
Actual number of voxels in a Cell once margins are considered.
const int BLOCK_DATA_SIZE
Number of voxels in a Cell.
void getRotation(const Index &index, double &dx, double &dy, double &dz) const
Gets the rotation of the voxel as doubles.
StatTracker LODStats[LEVELS][STAT_NULL]
Used to track stats across all LOD.
StatId
Enummerates different stat IDs supported by the system.
double decodeVectorCoord(unsigned char coord)
It decodes an unsigned char coord in a double one.
const int VOXEL_ONLY_COORDS
A special material ID that specified material information is undefined, only voxel coordinates should...
void setMaterial(const Index &index, MaterialId material)
Sets the material of the voxel.
virtual int getStatsContourId()
Returns the stats ID to be used for measuring performance of the getContourData() method...
virtual void * createThreadContext()
The voxel layer can use this method to return a structure that will be unique for each calling thread...
const double VECTOR_MIN_VALUE
How much the vector can overfloat the voxel.
virtual int getStatsPlanJobsId()
Returns the stats ID to be used for measuring performance of the planJobs() method.
An object to track stats. Contains two different counters.
void setVector(const Index &index, double dx, double dy, double dz)
Sets the internal point in the voxel using doubles.
virtual int getStatsRunJobsId()
Returns the stats ID to be used for measuring performance of the runJobs() method.
const int VOXEL_NIL
A special material ID that specified no information is defined for the voxel.