Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //============================================================================
- // Copyright (c) Kitware, Inc.
- // All rights reserved.
- // See LICENSE.txt for details.
- // This software is distributed WITHOUT ANY WARRANTY; without even
- // the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
- // PURPOSE. See the above copyright notice for more information.
- //
- // Copyright 2014 Sandia Corporation.
- // Copyright 2014 UT-Battelle, LLC.
- // Copyright 2014 Los Alamos National Security.
- //
- // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
- // the U.S. Government retains certain rights in this software.
- //
- // Under the terms of Contract DE-AC52-06NA25396 with Los Alamos National
- // Laboratory (LANL), the U.S. Government retains certain rights in
- // this software.
- //============================================================================
- //We first check if VTKM_DEVICE_ADAPTER is defined, so that when TBB and CUDA
- //includes this file we use the device adapter that they have set.
- #ifndef VTKM_DEVICE_ADAPTER
- #define VTKM_DEVICE_ADAPTER VTKM_DEVICE_ADAPTER_SERIAL
- #endif
- #include <vtkm/worklet/IsosurfaceUniformGrid.h>
- #include <vtkm/worklet/DispatcherMapField.h>
- #include <vtkm/Math.h>
- #include <vtkm/cont/ArrayHandleCounting.h>
- #include <vtkm/cont/CellSetExplicit.h>
- #include <vtkm/cont/DataSet.h>
- //Suppress warnings about glut being deprecated on OSX
- #if (defined(VTKM_GCC) || defined(VTKM_CLANG)) && !defined(VTKM_PGI)
- # pragma GCC diagnostic push
- # pragma GCC diagnostic ignored "-Wdeprecated-declarations"
- #endif
- #if defined (__APPLE__)
- # include <GLUT/glut.h>
- #else
- # include <GL/glut.h>
- #endif
- #include "quaternion.h"
- #include <vector>
- typedef VTKM_DEFAULT_DEVICE_ADAPTER_TAG DeviceAdapter;
- vtkm::Id3 dims(16,16,16);
- vtkm::worklet::IsosurfaceFilterUniformGrid<vtkm::Float32, DeviceAdapter> *isosurfaceFilter;
- vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::Float32,3> > verticesArray, normalsArray;
- vtkm::cont::ArrayHandle<vtkm::Float32> scalarsArray;
- Quaternion qrot;
- int lastx, lasty;
- int mouse_state = 1;
- // Construct an input data set using the tangle field worklet
- vtkm::cont::DataSet MakeIsosurfaceTestDataSet(vtkm::Id3 dims)
- {
- vtkm::cont::DataSet dataSet;
- const int nVerts = 18;
- vtkm::cont::ArrayHandleUniformPointCoordinates
- coordinates(vtkm::Id3(3, 2, 3));
- vtkm::Float32 vars[nVerts] = {10.1f, 20.1f, 30.1f, 40.1f, 50.2f, 60.2f, 70.2f, 80.2f, 90.3f,
- 100.3f, 110.3f, 120.3f, 130.4f, 140.4f, 150.4f, 160.4f, 170.5f,
- 180.5f};
- dataSet.AddCoordinateSystem(
- vtkm::cont::CoordinateSystem("coordinates", 1, coordinates));
- //Set point scalar
- dataSet.AddField(Field("pointvar", 1, vtkm::cont::Field::ASSOC_POINTS, vars, nVerts));
- //Set cell scalar
- vtkm::Float32 cellvar[4] = {100.1f, 100.2f, 100.3f, 100.4f};
- dataSet.AddField(Field("cellvar", 1, vtkm::cont::Field::ASSOC_CELL_SET, "cells", cellvar, 4));
- static const vtkm::IdComponent dim = 3;
- vtkm::cont::CellSetStructured<dim> cellSet("cells");
- cellSet.SetPointDimensions( vtkm::make_Vec(3,2,3) );
- dataSet.AddCellSet(cellSet);
- return dataSet;
- }
- }
- // Initialize the OpenGL state
- void initializeGL()
- {
- glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
- glEnable(GL_DEPTH_TEST);
- glShadeModel(GL_SMOOTH);
- float white[] = { 0.8f, 0.8f, 0.8f, 1.0f };
- float black[] = { 0.0f, 0.0f, 0.0f, 1.0f };
- float lightPos[] = { 10.0f, 10.0f, 10.5f, 1.0f };
- glLightfv(GL_LIGHT0, GL_AMBIENT, white);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, white);
- glLightfv(GL_LIGHT0, GL_SPECULAR, black);
- glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
- glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, 1);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
- glEnable(GL_COLOR_MATERIAL);
- }
- // Render the output using simple OpenGL
- void displayCall()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glEnable(GL_DEPTH_TEST);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective( 45.0f, 1.0f, 1.0f, 20.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- gluLookAt(0.0f, 0.0f, 3.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
- glPushMatrix();
- float rotationMatrix[16];
- qrot.getRotMat(rotationMatrix);
- glMultMatrixf(rotationMatrix);
- glTranslatef(-0.5f, -0.5f, -0.5f);
- glColor3f(0.1f, 0.1f, 0.6f);
- glBegin(GL_TRIANGLES);
- for (vtkm::IdComponent i=0; i<verticesArray.GetNumberOfValues(); i++)
- {
- vtkm::Vec<vtkm::Float32, 3> curNormal = normalsArray.GetPortalConstControl().Get(i);
- vtkm::Vec<vtkm::Float32, 3> curVertex = verticesArray.GetPortalConstControl().Get(i);
- glNormal3f(curNormal[0], curNormal[1], curNormal[2]);
- glVertex3f(curVertex[0], curVertex[1], curVertex[2]);
- }
- glEnd();
- glPopMatrix();
- glutSwapBuffers();
- }
- // Allow rotations of the view
- void mouseMove(int x, int y)
- {
- int dx = x - lastx;
- int dy = y - lasty;
- if (mouse_state == 0)
- {
- vtkm::Float32 pideg = static_cast<float>(vtkm::Pi()/180.0);
- Quaternion newRotX;
- newRotX.setEulerAngles(-0.2f*dx*pideg/180.0f, 0.0f, 0.0f);
- qrot.mul(newRotX);
- Quaternion newRotY;
- newRotY.setEulerAngles(0.0f, 0.0f, -0.2f*dy*pideg/180.0f);
- qrot.mul(newRotY);
- }
- lastx = x;
- lasty = y;
- glutPostRedisplay();
- }
- // Respond to mouse button
- void mouseCall(int button, int state, int x, int y)
- {
- if (button == 0) mouse_state = state;
- if ((button == 0) && (state == 0)) { lastx = x; lasty = y; }
- }
- // Compute and render an isosurface for a uniform grid example
- int main(int argc, char* argv[])
- {
- typedef vtkm::cont::internal::DeviceAdapterTraits<DeviceAdapter>
- DeviceAdapterTraits;
- std::cout << "Running IsosurfaceUniformGrid example on device adapter: "
- << DeviceAdapterTraits::GetId() << std::endl;
- vtkm::cont::DataSet dataSet = MakeIsosurfaceTestDataSet(dims);
- isosurfaceFilter = new vtkm::worklet::IsosurfaceFilterUniformGrid<vtkm::Float32, DeviceAdapter>(dims, dataSet);
- isosurfaceFilter->Run(0.5,
- dataSet.GetField("nodevar").GetData(),
- verticesArray,
- normalsArray,
- scalarsArray);
- std::cout << "Number of output vertices: " << verticesArray.GetNumberOfValues() << std::endl;
- lastx = lasty = 0;
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitWindowSize(1000, 1000);
- glutCreateWindow("VTK-m Isosurface");
- initializeGL();
- glutDisplayFunc(displayCall);
- glutMotionFunc(mouseMove);
- glutMouseFunc(mouseCall);
- glutMainLoop();
- return 0;
- }
- #if (defined(VTKM_GCC) || defined(VTKM_CLANG)) && !defined(VTKM_PGI)
- # pragma GCC diagnostic pop
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement