Advertisement
Guest User

Untitled

a guest
Aug 21st, 2013
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.01 KB | None | 0 0
  1. /*******************************************************************************
  2. Copyright (c) 2005-2009 David Williams
  3.  
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7.  
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11.  
  12.     1. The origin of this software must not be misrepresented; you must not
  13.     claim that you wrote the original software. If you use this software
  14.     in a product, an acknowledgment in the product documentation would be
  15.     appreciated but is not required.
  16.  
  17.     2. Altered source versions must be plainly marked as such, and must not be
  18.     misrepresented as being the original software.
  19.  
  20.     3. This notice may not be removed or altered from any source
  21.     distribution.  
  22. *******************************************************************************/
  23.  
  24. #include "OpenGLWidget.h"
  25.  
  26. #include "PolyVoxCore/CubicSurfaceExtractorWithNormals.h"
  27. #include "PolyVoxCore/LowPassFilter.h"
  28. #include "PolyVoxCore/MarchingCubesSurfaceExtractor.h"
  29. #include "PolyVoxCore/RawVolume.h"
  30. #include "PolyVoxCore/SurfaceMesh.h"
  31. #include "PolyVoxCore/SimpleVolume.h"
  32.  
  33. #include <QApplication>
  34.  
  35. //Use the PolyVox namespace
  36. using namespace PolyVox;
  37.  
  38. void createSphereInVolume(SimpleVolume<uint8_t>& volData, float fRadius)
  39. {
  40.     //This vector hold the position of the center of the volume
  41.     Vector3DFloat v3dVolCenter(volData.getWidth() / 2, volData.getHeight() / 2, volData.getDepth() / 2);
  42.  
  43.     //This three-level for loop iterates over every voxel in the volume
  44.     for (int z = 0; z < volData.getDepth(); z++)
  45.     {
  46.         for (int y = 0; y < volData.getHeight(); y++)
  47.         {
  48.             for (int x = 0; x < volData.getWidth(); x++)
  49.             {
  50.                 //Store our current position as a vector...
  51.                 Vector3DFloat v3dCurrentPos(x,y,z);
  52.                 //And compute how far the current position is from the center of the volume
  53.                 float fDistToCenter = (v3dCurrentPos - v3dVolCenter).length();
  54.  
  55.                 uint8_t uVoxelValue = 0;
  56.  
  57.                 //If the current voxel is less than 'radius' units from the center then we make it solid.
  58.                 if(fDistToCenter <= fRadius)
  59.                 {
  60.                     //Our new voxel value
  61.                     uVoxelValue = 255;
  62.                 }
  63.  
  64.                 //Wrte the voxel value into the volume 
  65.                 volData.setVoxelAt(x, y, z, uVoxelValue);
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71. int main(int argc, char *argv[])
  72. {
  73.     // Show logs
  74.     setTraceStream(&(std::cout));
  75.  
  76.     //Create and show the Qt OpenGL window
  77.     QApplication app(argc, argv);
  78.     OpenGLWidget openGLWidget(0);
  79.     openGLWidget.show();
  80.  
  81.     //Create an empty volume and then place a sphere in it
  82.     SimpleVolume<uint8_t> volData(PolyVox::Region(Vector3DInt32(0,0,0), Vector3DInt32(63, 63, 63)));
  83.     createSphereInVolume(volData, 30);
  84.  
  85.     // Smooth
  86.     RawVolume<uint8_t> tempVolume(volData.getEnclosingRegion());
  87.     LowPassFilter< SimpleVolume<uint8_t>, RawVolume<uint8_t>, int16_t > pass1(&volData, PolyVox::Region(volData.getEnclosingRegion()), &tempVolume, PolyVox::Region(volData.getEnclosingRegion()), 3);
  88.     pass1.executeSAT();
  89.     LowPassFilter< RawVolume<uint8_t>, SimpleVolume<uint8_t>, int16_t > pass2(&tempVolume, PolyVox::Region(volData.getEnclosingRegion()), &volData, PolyVox::Region(volData.getEnclosingRegion()), 3);
  90.     pass2.executeSAT();
  91.  
  92.     //A mesh object to hold the result of surface extraction
  93.     SurfaceMesh<PositionMaterialNormal> mesh;
  94.  
  95.     //Create a surface extractor. Comment out one of the following two lines to decide which type gets created.
  96.     //CubicSurfaceExtractorWithNormals< SimpleVolume<uint8_t> > surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh);
  97.     MarchingCubesSurfaceExtractor< SimpleVolume<uint8_t> > surfaceExtractor(&volData, volData.getEnclosingRegion(), &mesh);
  98.  
  99.     //Execute the surface extractor.
  100.     surfaceExtractor.execute();
  101.  
  102.     //Pass the surface to the OpenGL window
  103.     openGLWidget.setSurfaceMeshToRender(mesh);
  104.  
  105.     //Run the message pump.
  106.     return app.exec();
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement