summaryrefslogtreecommitdiff
path: root/src/Voxel/Voxel_FloatDS.cxx
blob: 894818fb5a47c2dfbb5b12af85e2113a0bc2ce2c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// File:	Voxel_FloatDS.cxx
// Created:	Mon May 15 13:38:43 2008
// Author:	Vladislav ROMASHKO
//		<vladislav.romashko@opencascade.com>

#include <Voxel_FloatDS.ixx>

#include <stdlib.h>

// Empty constructor
Voxel_FloatDS::Voxel_FloatDS():Voxel_DS()
{

}

// Constructor with intialization.
Voxel_FloatDS::Voxel_FloatDS(const Standard_Real      x, const Standard_Real      y, const Standard_Real      z, 
			     const Standard_Real   xlen, const Standard_Real   ylen, const Standard_Real   zlen,
			     const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
:Voxel_DS()
{
  Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);
}

// Initialization.
void Voxel_FloatDS::Init(const Standard_Real      x, const Standard_Real      y, const Standard_Real      z, 
			 const Standard_Real   xlen, const Standard_Real   ylen, const Standard_Real   zlen,
			 const Standard_Integer nbx, const Standard_Integer nby, const Standard_Integer nbz)
{
  Destroy();

  Voxel_DS::Init(x, y, z, xlen, ylen, zlen, nbx, nby, nbz);

  if (!myNbX || !myNbY || !myNbZ)
    return;

  Standard_Integer nb_floats = myNbXY * myNbZ;
  Standard_Integer nb_slices = RealToInt(ceil(nb_floats / 32.0)); // 32 values in 1 slice
  myData = (Standard_Address) calloc(nb_slices, sizeof(Standard_ShortReal*));
}

// Destructor
void Voxel_FloatDS::Destroy()
{
  if (myData)
  {
    SetZero();
    free((Standard_ShortReal**)myData);
    myData = 0;
  }
}

void Voxel_FloatDS::SetZero()
{
  if (myData)
  {
    Standard_Integer nb_bytes = myNbXY * myNbZ;
    Standard_Integer ix = 0, nb_slices = RealToInt(ceil(nb_bytes / 32.0));
    for (; ix < nb_slices; ix++)
    {
      if (((Standard_ShortReal**)myData)[ix])
      {
	free(((Standard_ShortReal**)myData)[ix]);
	((Standard_ShortReal**)myData)[ix] = 0;
      }
    }
  }
}

// Access to the floating-point information attached to a particular voxel:
// Info: (ix >= 0 && ix < theNb_x), etc.
void Voxel_FloatDS::Set(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz, 
			const Standard_ShortReal data)
{
  Standard_Integer ifloat = ix + myNbX * iy + myNbXY * iz;
  Standard_Integer islice = ifloat >> 5;

  // Allocate the slice if it is not done yet.
  if (!((Standard_ShortReal**)myData)[islice])
  {
    ((Standard_ShortReal**)myData)[islice] = 
      (Standard_ShortReal*) calloc(32/*number of floating values in slice*/, sizeof(Standard_ShortReal));
  }

  // Index of start-byte of the value within the slice.
  Standard_Integer ivalue = ifloat - (islice << 5);

  // Value (float)
  ((Standard_ShortReal*)((Standard_ShortReal**)myData)[islice])[ivalue] = data;
}

Standard_ShortReal Voxel_FloatDS::Get(const Standard_Integer ix, const Standard_Integer iy, const Standard_Integer iz) const
{
  Standard_Integer ifloat = ix + myNbX * iy + myNbXY * iz;
  Standard_Integer islice = ifloat >> 5;

  // If the slice of data is not allocated, it means that its values are 0.
  if (!((Standard_ShortReal**)myData)[islice])
    return 0.0f;

  // Index of start-byte of the value within the slice.
  Standard_Integer ivalue = ifloat - (islice << 5);

  // Value (floating-point value)
  return ((Standard_ShortReal*)((Standard_ShortReal**)myData)[islice])[ivalue];
}