Advertisement
Guest User

Untitled

a guest
Oct 6th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. //============================================================================
  2. // Copyright (c) Kitware, Inc.
  3. // All rights reserved.
  4. // See LICENSE.txt for details.
  5. // This software is distributed WITHOUT ANY WARRANTY; without even
  6. // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  7. // PURPOSE. See the above copyright notice for more information.
  8. //
  9. // Copyright 2014 Sandia Corporation.
  10. // Copyright 2014 UT-Battelle, LLC.
  11. // Copyright 2014 Los Alamos National Security.
  12. //
  13. // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
  14. // the U.S. Government retains certain rights in this software.
  15. //
  16. // Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
  17. // Laboratory (LANL), the U.S. Government retains certain rights in
  18. // this software.
  19. //============================================================================
  20.  
  21. //We first check if VTKM_DEVICE_ADAPTER is defined, so that when TBB and CUDA
  22. //includes this file we use the device adapter that they have set.
  23. #ifndef VTKM_DEVICE_ADAPTER
  24. #define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_SERIAL
  25. #endif
  26.  
  27. #include <vtkm/worklet/IsosurfaceUniformGrid.h>
  28. #include <vtkm/worklet/DispatcherMapField.h>
  29.  
  30. #include <vtkm/Math.h>
  31. #include <vtkm/cont/ArrayHandleCounting.h>
  32. #include <vtkm/cont/CellSetExplicit.h>
  33. #include <vtkm/cont/DataSet.h>
  34.  
  35. //Suppress warnings about glut being deprecated on OSX
  36. #if (defined(VTKM_GCC) || defined(VTKM_CLANG)) && !defined(VTKM_PGI)
  37. # pragma GCC diagnostic push
  38. # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  39. #endif
  40.  
  41. #if defined (__APPLE__)
  42. # include <GLUT/glut.h>
  43. #else
  44. # include <GL/glut.h>
  45. #endif
  46.  
  47. #include "quaternion.h"
  48.  
  49. #include <vector>
  50.  
  51. typedef VTKM_DEFAULT_DEVICE_ADAPTER_TAG DeviceAdapter;
  52.  
  53. vtkm::Id3 dims(16,16,16);
  54. vtkm::worklet::IsosurfaceFilterUniformGrid<vtkm::Float32, DeviceAdapter> *isosurfaceFilter;
  55. vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::Float32,3> > verticesArray, normalsArray;
  56. vtkm::cont::ArrayHandle<vtkm::Float32> scalarsArray;
  57. Quaternion qrot;
  58. int lastx, lasty;
  59. int mouse_state = 1;
  60.  
  61.  
  62. // Construct an input data set using the tangle field worklet
  63. vtkm::cont::DataSet MakeIsosurfaceTestDataSet(vtkm::Id3 dims)
  64. {
  65. vtkm::cont::DataSet dataSet;
  66.  
  67. const int nVerts = 18;
  68. vtkm::cont::ArrayHandleUniformPointCoordinates
  69. coordinates(vtkm::Id3(3, 2, 3));
  70. vtkm::Float32 vars[nVerts] = {10.1f, 20.1f, 30.1f, 40.1f, 50.2f, 60.2f, 70.2f, 80.2f, 90.3f,
  71. 100.3f, 110.3f, 120.3f, 130.4f, 140.4f, 150.4f, 160.4f, 170.5f,
  72. 180.5f};
  73.  
  74. dataSet.AddCoordinateSystem(
  75. vtkm::cont::CoordinateSystem("coordinates", 1, coordinates));
  76.  
  77. //Set point scalar
  78. dataSet.AddField(Field("pointvar", 1, vtkm::cont::Field::ASSOC_POINTS, vars, nVerts));
  79.  
  80. //Set cell scalar
  81. vtkm::Float32 cellvar[4] = {100.1f, 100.2f, 100.3f, 100.4f};
  82. dataSet.AddField(Field("cellvar", 1, vtkm::cont::Field::ASSOC_CELL_SET, "cells", cellvar, 4));
  83.  
  84. static const vtkm::IdComponent dim = 3;
  85. vtkm::cont::CellSetStructured<dim> cellSet("cells");
  86. cellSet.SetPointDimensions( vtkm::make_Vec(3,2,3) );
  87. dataSet.AddCellSet(cellSet);
  88.  
  89. return dataSet;
  90. }
  91.  
  92. }
  93.  
  94.  
  95. // Initialize the OpenGL state
  96. void initializeGL()
  97. {
  98. glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  99. glEnable(GL_DEPTH_TEST);
  100. glShadeModel(GL_SMOOTH);
  101.  
  102. float white[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  103. float black[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  104. float lightPos[] = { 10.0f, 10.0f, 10.5f, 1.0f };
  105.  
  106. glLightfv(GL_LIGHT0, GL_AMBIENT, white);
  107. glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
  108. glLightfv(GL_LIGHT0, GL_SPECULAR, black);
  109. glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
  110.  
  111. glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
  112.  
  113. glEnable(GL_LIGHTING);
  114. glEnable(GL_LIGHT0);
  115. glEnable(GL_NORMALIZE);
  116. glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
  117. glEnable(GL_COLOR_MATERIAL);
  118. }
  119.  
  120.  
  121. // Render the output using simple OpenGL
  122. void displayCall()
  123. {
  124. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  125. glEnable(GL_DEPTH_TEST);
  126.  
  127. glMatrixMode(GL_PROJECTION);
  128. glLoadIdentity();
  129. gluPerspective( 45.0f, 1.0f, 1.0f, 20.0f);
  130.  
  131. glMatrixMode(GL_MODELVIEW);
  132. glLoadIdentity();
  133. gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
  134.  
  135. glPushMatrix();
  136. float rotationMatrix[16];
  137. qrot.getRotMat(rotationMatrix);
  138. glMultMatrixf(rotationMatrix);
  139. glTranslatef(-0.5f, -0.5f, -0.5f);
  140.  
  141. glColor3f(0.1f, 0.1f, 0.6f);
  142.  
  143. glBegin(GL_TRIANGLES);
  144. for (vtkm::IdComponent i=0; i<verticesArray.GetNumberOfValues(); i++)
  145. {
  146. vtkm::Vec<vtkm::Float32, 3> curNormal = normalsArray.GetPortalConstControl().Get(i);
  147. vtkm::Vec<vtkm::Float32, 3> curVertex = verticesArray.GetPortalConstControl().Get(i);
  148. glNormal3f(curNormal[0], curNormal[1], curNormal[2]);
  149. glVertex3f(curVertex[0], curVertex[1], curVertex[2]);
  150. }
  151. glEnd();
  152.  
  153. glPopMatrix();
  154. glutSwapBuffers();
  155. }
  156.  
  157.  
  158. // Allow rotations of the view
  159. void mouseMove(int x, int y)
  160. {
  161. int dx = x - lastx;
  162. int dy = y - lasty;
  163.  
  164. if (mouse_state == 0)
  165. {
  166. vtkm::Float32 pideg = static_cast<float>(vtkm::Pi()/180.0);
  167. Quaternion newRotX;
  168. newRotX.setEulerAngles(-0.2f*dx*pideg/180.0f, 0.0f, 0.0f);
  169. qrot.mul(newRotX);
  170.  
  171. Quaternion newRotY;
  172. newRotY.setEulerAngles(0.0f, 0.0f, -0.2f*dy*pideg/180.0f);
  173. qrot.mul(newRotY);
  174. }
  175. lastx = x;
  176. lasty = y;
  177.  
  178. glutPostRedisplay();
  179. }
  180.  
  181.  
  182. // Respond to mouse button
  183. void mouseCall(int button, int state, int x, int y)
  184. {
  185. if (button == 0) mouse_state = state;
  186. if ((button == 0) && (state == 0)) { lastx = x; lasty = y; }
  187. }
  188.  
  189.  
  190. // Compute and render an isosurface for a uniform grid example
  191. int main(int argc, char* argv[])
  192. {
  193. typedef vtkm::cont::internal::DeviceAdapterTraits<DeviceAdapter>
  194. DeviceAdapterTraits;
  195. std::cout << "Running IsosurfaceUniformGrid example on device adapter: "
  196. << DeviceAdapterTraits::GetId() << std::endl;
  197.  
  198. vtkm::cont::DataSet dataSet = MakeIsosurfaceTestDataSet(dims);
  199.  
  200. isosurfaceFilter = new vtkm::worklet::IsosurfaceFilterUniformGrid<vtkm::Float32, DeviceAdapter>(dims, dataSet);
  201.  
  202. isosurfaceFilter->Run(0.5,
  203. dataSet.GetField("nodevar").GetData(),
  204. verticesArray,
  205. normalsArray,
  206. scalarsArray);
  207.  
  208. std::cout << "Number of output vertices: " << verticesArray.GetNumberOfValues() << std::endl;
  209.  
  210. lastx = lasty = 0;
  211. glutInit(&argc, argv);
  212. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
  213. glutInitWindowSize(1000, 1000);
  214. glutCreateWindow("VTK-m Isosurface");
  215. initializeGL();
  216. glutDisplayFunc(displayCall);
  217. glutMotionFunc(mouseMove);
  218. glutMouseFunc(mouseCall);
  219. glutMainLoop();
  220.  
  221. return 0;
  222. }
  223.  
  224. #if (defined(VTKM_GCC) || defined(VTKM_CLANG)) && !defined(VTKM_PGI)
  225. # pragma GCC diagnostic pop
  226. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement