• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

PolyVoxCore/include/PolyVoxCore/RaycastWithCallback.inl

Go to the documentation of this file.
00001 /*******************************************************************************
00002 Copyright (c) 2005-2009 David Williams
00003 
00004 This software is provided 'as-is', without any express or implied
00005 warranty. In no event will the authors be held liable for any damages
00006 arising from the use of this software.
00007 
00008 Permission is granted to anyone to use this software for any purpose,
00009 including commercial applications, and to alter it and redistribute it
00010 freely, subject to the following restrictions:
00011 
00012     1. The origin of this software must not be misrepresented; you must not
00013     claim that you wrote the original software. If you use this software
00014     in a product, an acknowledgment in the product documentation would be
00015     appreciated but is not required.
00016 
00017     2. Altered source versions must be plainly marked as such, and must not be
00018     misrepresented as being the original software.
00019 
00020     3. This notice may not be removed or altered from any source
00021     distribution.   
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         //Check the user provided a callback, because it
00035         //is used to determine when to finish the raycast.
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         //The doRaycast function is assuming that it is iterating over the areas defined between
00055         //voxels. We actually want to define the areas as being centered on voxels (as this is
00056         //what the CubicSurfaceExtractor generates). We add (0.5,0.5,0.5) here to adjust for this.
00057         Vector3DFloat v3dStart = m_v3dStart + Vector3DFloat(0.5f, 0.5f, 0.5f);
00058 
00059         //Compute the end point
00060         Vector3DFloat v3dEnd = v3dStart + m_v3dDirection;
00061 
00062         //Do the raycast
00063         doRaycast(v3dStart.getX(), v3dStart.getY(), v3dStart.getZ(), v3dEnd.getX(), v3dEnd.getY(), v3dEnd.getZ());
00064     }
00065 
00066     // This function is based on Christer Ericson's code and description of the 'Uniform Grid Intersection Test' in
00067     // 'Real Time Collision Detection'. The following information from the errata on the book website is also relevent:
00068     //
00069     //  pages 326-327. In the function VisitCellsOverlapped() the two lines calculating tx and ty are incorrect.
00070     //  The less-than sign in each line should be a greater-than sign. That is, the two lines should read:
00071     //
00072     //  float tx = ((x1 > x2) ? (x1 - minx) : (maxx - x1)) / Abs(x2 - x1);
00073     //  float ty = ((y1 > y2) ? (y1 - miny) : (maxy - y1)) / Abs(y2 - y1);
00074     //
00075     //  Thanks to Jetro Lauha of Fathammer in Helsinki, Finland for reporting this error.
00076     //
00077     //  Jetro also points out that the computations of i, j, iend, and jend are incorrectly rounded if the line
00078     //  coordinates are allowed to go negative. While that was not really the intent of the code — that is, I
00079     //  assumed grids to be numbered from (0, 0) to (m, n) — I'm at fault for not making my assumption clear.
00080     //  Where it is important to handle negative line coordinates the computation of these variables should be
00081     //  changed to something like this:
00082     //
00083     //  // Determine start grid cell coordinates (i, j)
00084     //  int i = (int)floorf(x1 / CELL_SIDE);
00085     //  int j = (int)floorf(y1 / CELL_SIDE);
00086     //
00087     //  // Determine end grid cell coordinates (iend, jend)
00088     //  int iend = (int)floorf(x2 / CELL_SIDE);
00089     //  int jend = (int)floorf(y2 / CELL_SIDE);
00090     //
00091     //  page 328. The if-statement that reads "if (ty <= tx && ty <= tz)" has a superfluous condition.
00092     //  It should simply read "if (ty <= tz)".
00093     //
00094     //  This error was reported by Joey Hammer (PixelActive). 
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             //Call the callback. If it returns false then finish the loop.
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 }

Generated on Sat Nov 19 2011 00:27:30 for PolyVox by  doxygen 1.7.1