Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 namespace PolyVox
00025 {
00026 template< template<typename> class VolumeType, typename VoxelType>
00027 RaycastWithCallback<VolumeType, VoxelType>::RaycastWithCallback(VolumeType<VoxelType>* volData, const Vector3DFloat& v3dStart, const Vector3DFloat& v3dDirection, polyvox_function<bool(const Vector3DInt32& position)> funcCallback)
00028 :m_volData(volData)
00029 ,m_sampVolume(volData)
00030 ,m_v3dStart(v3dStart)
00031 ,m_v3dDirection(v3dDirection)
00032 ,m_funcCallback(funcCallback)
00033 {
00034
00035
00036 assert(m_funcCallback);
00037 }
00038
00039 template< template<typename> class VolumeType, typename VoxelType>
00040 void RaycastWithCallback<VolumeType, VoxelType>::setStart(const Vector3DFloat& v3dStart)
00041 {
00042 m_v3dStart = v3dStart;
00043 }
00044
00045 template< template<typename> class VolumeType, typename VoxelType>
00046 void RaycastWithCallback<VolumeType, VoxelType>::setDirection(const Vector3DFloat& v3dDirection)
00047 {
00048 m_v3dDirection = v3dDirection;
00049 }
00050
00051 template< template<typename> class VolumeType, typename VoxelType>
00052 void RaycastWithCallback<VolumeType, VoxelType>::execute(void)
00053 {
00054
00055
00056
00057 Vector3DFloat v3dStart = m_v3dStart + Vector3DFloat(0.5f, 0.5f, 0.5f);
00058
00059
00060 Vector3DFloat v3dEnd = v3dStart + m_v3dDirection;
00061
00062
00063 doRaycast(v3dStart.getX(), v3dStart.getY(), v3dStart.getZ(), v3dEnd.getX(), v3dEnd.getY(), v3dEnd.getZ());
00064 }
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 template< template<typename> class VolumeType, typename VoxelType>
00096 void RaycastWithCallback<VolumeType, VoxelType>::doRaycast(float x1, float y1, float z1, float x2, float y2, float z2)
00097 {
00098 int i = (int)floorf(x1);
00099 int j = (int)floorf(y1);
00100 int k = (int)floorf(z1);
00101
00102 int iend = (int)floorf(x2);
00103 int jend = (int)floorf(y2);
00104 int kend = (int)floorf(z2);
00105
00106 int di = ((x1 < x2) ? 1 : ((x1 > x2) ? -1 : 0));
00107 int dj = ((y1 < y2) ? 1 : ((y1 > y2) ? -1 : 0));
00108 int dk = ((z1 < z2) ? 1 : ((z1 > z2) ? -1 : 0));
00109
00110 float minx = floorf(x1), maxx = minx + 1.0f;
00111 float tx = ((x1 > x2) ? (x1 - minx) : (maxx - x1)) / abs(x2 - x1);
00112 float miny = floorf(y1), maxy = miny + 1.0f;
00113 float ty = ((y1 > y2) ? (y1 - miny) : (maxy - y1)) / abs(y2 - y1);
00114 float minz = floorf(z1), maxz = minz + 1.0f;
00115 float tz = ((z1 > z2) ? (z1 - minz) : (maxz - z1)) / abs(z2 - z1);
00116
00117 float deltatx = 1.0f / abs(x2 - x1);
00118 float deltaty = 1.0f / abs(y2 - y1);
00119 float deltatz = 1.0f / abs(z2 - z1);
00120
00121 m_sampVolume.setPosition(i,j,k);
00122
00123 for(;;)
00124 {
00125
00126 if(!m_funcCallback(Vector3DInt32(i,j,k)))
00127 {
00128 break;
00129 }
00130
00131 if(tx <= ty && tx <= tz)
00132 {
00133 tx += deltatx;
00134 i += di;
00135
00136 if(di == 1) m_sampVolume.movePositiveX();
00137 if(di == -1) m_sampVolume.moveNegativeX();
00138 } else if (ty <= tz)
00139 {
00140 ty += deltaty;
00141 j += dj;
00142
00143 if(dj == 1) m_sampVolume.movePositiveY();
00144 if(dj == -1) m_sampVolume.moveNegativeY();
00145 } else
00146 {
00147 tz += deltatz;
00148 k += dk;
00149
00150 if(dk == 1) m_sampVolume.movePositiveZ();
00151 if(dk == -1) m_sampVolume.moveNegativeZ();
00152 }
00153 }
00154 }
00155 }