Adding examples directory with isosurface rendering example

This commit is contained in:
Christopher Meyer Sewell - 188584 2015-09-01 17:09:36 -06:00
parent 74dcd1c5cc
commit 30100e2ae8
7 changed files with 398 additions and 3 deletions

@ -70,6 +70,7 @@ option(VTKm_ENABLE_TESTING "Enable VTKm Testing" ON)
option(VTKm_ENABLE_BENCHMARKS "Enable VTKm Benchmarking" OFF)
option(VTKm_BUILD_DOCUMENTATION "Build Doxygen documentation" OFF)
option(VTKm_BUILD_EXAMPLES "Build examples" OFF)
option(VTKm_USE_DOUBLE_PRECISION
"Use double precision for floating point calculations"
@ -194,6 +195,12 @@ if (VTKm_BUILD_DOCUMENTATION)
vtkm_build_documentation()
endif()
#-----------------------------------------------------------------------------
# Build examples
if(VTKm_BUILD_EXAMPLES)
add_subdirectory(examples)
endif(VTKm_BUILD_EXAMPLES)
#-----------------------------------------------------------------------------
# Configuration for build directory.
set(VTKm_INCLUDE_DIRS_CONFIG "${VTKm_SOURCE_DIR};${VTKm_BINARY_DIR}")

44
examples/CMakeLists.txt Normal file

@ -0,0 +1,44 @@
##=============================================================================
##
## 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 2015 Sandia Corporation.
## Copyright 2015 UT-Battelle, LLC.
## Copyright 2015 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.
##
##=============================================================================
cmake_minimum_required(VERSION 2.8.11)
find_package(OpenGL)
find_package(GLUT)
find_package(TBB)
if(OPENGL_FOUND AND GLUT_FOUND)
add_executable(IsosurfaceUniformGrid_SERIAL IsosurfaceUniformGrid.cxx)
target_include_directories(IsosurfaceUniformGrid_SERIAL PRIVATE ${GLUT_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR})
target_link_libraries(IsosurfaceUniformGrid_SERIAL ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES})
if(VTKm_Cuda_FOUND)
cuda_add_executable(IsosurfaceUniformGrid_CUDA IsosurfaceUniformGrid.cxx)
target_link_libraries(IsosurfaceUniformGrid_CUDA ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES})
endif()
if(VTKm_ENABLE_TBB)
add_executable(IsosurfaceUniformGrid_TBB IsosurfaceUniformGrid.cxx)
target_include_directories(IsosurfaceUniformGrid_TBB PRIVATE ${GLUT_INCLUDE_DIR} ${OPENGL_INCLUDE_DIR} ${TBB_INCLUDE_DIRS})
target_link_libraries(IsosurfaceUniformGrid_TBB ${OPENGL_LIBRARIES} ${GLUT_LIBRARIES} ${TBB_LIBRARIES})
endif()
endif()

@ -0,0 +1,249 @@
//============================================================================
// 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.
//============================================================================
#include <vtkm/worklet/IsosurfaceUniformGrid.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/cont/CellSetExplicit.h>
#include <vtkm/cont/DataSet.h>
#include <vtkm/cont/testing/Testing.h>
#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::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;
namespace {
class TangleField : public vtkm::worklet::WorkletMapField
{
public:
typedef void ControlSignature(FieldIn<IdType> vertexId, FieldOut<Scalar> v);
typedef void ExecutionSignature(_1, _2);
typedef _1 InputDomain;
const vtkm::Id xdim, ydim, zdim;
const float xmin, ymin, zmin, xmax, ymax, zmax;
const vtkm::Id cellsPerLayer;
VTKM_CONT_EXPORT
TangleField(const vtkm::Id3 dims, const float mins[3], const float maxs[3]) : xdim(dims[0]), ydim(dims[1]), zdim(dims[2]),
xmin(mins[0]), ymin(mins[1]), zmin(mins[2]), xmax(maxs[0]), ymax(maxs[1]), zmax(maxs[2]), cellsPerLayer((xdim) * (ydim)) { };
VTKM_EXEC_EXPORT
void operator()(const vtkm::Id &vertexId, vtkm::Float32 &v) const
{
const vtkm::Id x = vertexId % (xdim);
const vtkm::Id y = (vertexId / (xdim)) % (ydim);
const vtkm::Id z = vertexId / cellsPerLayer;
const float fx = static_cast<float>(x) / static_cast<float>(xdim-1);
const float fy = static_cast<float>(y) / static_cast<float>(xdim-1);
const float fz = static_cast<float>(z) / static_cast<float>(xdim-1);
const vtkm::Float32 xx = 3.0f*(xmin+(xmax-xmin)*(fx));
const vtkm::Float32 yy = 3.0f*(ymin+(ymax-ymin)*(fy));
const vtkm::Float32 zz = 3.0f*(zmin+(zmax-zmin)*(fz));
v = (xx*xx*xx*xx - 5.0f*xx*xx + yy*yy*yy*yy - 5.0f*yy*yy + zz*zz*zz*zz - 5.0f*zz*zz + 11.8f) * 0.2f + 0.5f;
}
};
vtkm::cont::DataSet MakeIsosurfaceTestDataSet(vtkm::Id3 dims)
{
vtkm::cont::DataSet dataSet;
const vtkm::Id3 vdims(dims[0] + 1, dims[1] + 1, dims[2] + 1);
const vtkm::Id dim3 = dims[0]*dims[1]*dims[2];
float mins[3] = {-1.0f, -1.0f, -1.0f};
float maxs[3] = {1.0f, 1.0f, 1.0f};
vtkm::cont::ArrayHandle<vtkm::Float32> fieldArray;
vtkm::cont::ArrayHandleCounting<vtkm::Id> vertexCountImplicitArray(0, vdims[0]*vdims[1]*vdims[2]);
vtkm::worklet::DispatcherMapField<TangleField> tangleFieldDispatcher(TangleField(vdims, mins, maxs));
tangleFieldDispatcher.Invoke(vertexCountImplicitArray, fieldArray);
vtkm::cont::ArrayHandleUniformPointCoordinates coordinates(vdims);
dataSet.AddCoordinateSystem(
vtkm::cont::CoordinateSystem("coordinates", 1, coordinates));
dataSet.AddField(vtkm::cont::Field("nodevar", 1, vtkm::cont::Field::ASSOC_POINTS, fieldArray));
std::vector<vtkm::Float32> cellvar( static_cast<std::size_t>(dim3) );
for(std::size_t i=0; i < cellvar.size(); i++)
{
cellvar[i] = vtkm::Float32(i);
}
vtkm::cont::Field cellField("cellvar", 1,
vtkm::cont::Field::ASSOC_CELL_SET,
"cells",
cellvar);
dataSet.AddField(cellField);
static const vtkm::IdComponent ndim = 3;
vtkm::cont::CellSetStructured<ndim> cellSet("cells");
cellSet.SetPointDimensions(vdims);
dataSet.AddCellSet(cellSet);
return dataSet;
}
}
void initializeGL()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
float white[] = { 0.8, 0.8, 0.8, 1.0 };
float black[] = { 0.0, 0.0, 0.0, 1.0 };
float lightPos[] = { 10.0, 10.0, 10.5, 1.0 };
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);
}
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);
glColor3f(0.1f, 0.1f, 0.6f);
glBegin(GL_TRIANGLES);
for (unsigned int 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();
}
void mouseMove(int x, int y)
{
int dx = x - lastx;
int dy = y - lasty;
if (mouse_state == 0)
{
Quaternion newRotX;
newRotX.setEulerAngles(-0.2*dx*M_PI/180.0, 0.0, 0.0);
qrot.mul(newRotX);
Quaternion newRotY;
newRotY.setEulerAngles(0.0, 0.0, -0.2*dy*M_PI/180.0);
qrot.mul(newRotY);
}
lastx = x;
lasty = y;
glutPostRedisplay();
}
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; }
}
int main(int argc, char* argv[])
{
std::cout << "IsosurfaceUniformGrid Example" << std::endl;
vtkm::Id3 dims(16,16,16);
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 << verticesArray.GetNumberOfValues() << std::endl;
lastx = lasty = 0;
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowSize(1000, 1000);
glutInitWindowPosition(300, 200);
glutCreateWindow("VTK-m Isosurface");
initializeGL();
glutDisplayFunc(displayCall);
glutMotionFunc(mouseMove);
glutMouseFunc(mouseCall);
glutMainLoop();
return 0;
}

79
examples/quaternion.h Executable file

@ -0,0 +1,79 @@
//=============================================================================
//
// 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 2015 Sandia Corporation.
// Copyright 2015 UT-Battelle, LLC.
// Copyright 2015 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.
//
//=============================================================================
/*
* quaternion.h
*
* Created on: Oct 10, 2014
* Author: csewell
*/
#ifndef QUATERNION_H_
#define QUATERNION_H_
#include <math.h>
#include <stdlib.h>
class Quaternion
{
public:
Quaternion() { x = y = z = 0.0; w = 1.0; }
Quaternion(double x, double y, double z, double w) : x(x), y(y), z(z), w(w) {};
void set(double ax, double ay, double az, double aw) { x = ax; y = ay; z = az; w = aw; }
void normalize()
{
float norm = sqrt(x*x + y*y + z*z + w*w);
if (norm > 0.00001) { x /= norm; y /= norm; z /= norm; w /= norm; }
}
void mul(Quaternion q)
{
float tx, ty, tz, tw;
tx = w*q.x + x*q.w + y*q.z - z*q.y;
ty = w*q.y + y*q.w + z*q.x - x*q.z;
tz = w*q.z + z*q.w + x*q.y - y*q.x;
tw = w*q.w - x*q.x - y*q.y - z*q.z;
x = tx; y = ty; z = tz; w = tw;
}
void setEulerAngles(float pitch, float yaw, float roll)
{
w = cos(pitch/2.0)*cos(yaw/2.0)*cos(roll/2.0) - sin(pitch/2.0)*sin(yaw/2.0)*sin(roll/2.0);
x = sin(pitch/2.0)*sin(yaw/2.0)*cos(roll/2.0) + cos(pitch/2.0)*cos(yaw/2.0)*sin(roll/2.0);
y = sin(pitch/2.0)*cos(yaw/2.0)*cos(roll/2.0) + cos(pitch/2.0)*sin(yaw/2.0)*sin(roll/2.0);
z = cos(pitch/2.0)*sin(yaw/2.0)*cos(roll/2.0) - sin(pitch/2.0)*cos(yaw/2.0)*sin(roll/2.0);
normalize();
}
void getRotMat(float* m) const
{
for (int i=0; i<16; i++) m[i] = 0.0; m[15] = 1.0;
m[0] = 1 - 2*y*y - 2*z*z; m[1] = 2*x*y - 2*z*w; m[2] = 2*x*z + 2*y*w;
m[4] = 2*x*y + 2*z*w; m[5] = 1 - 2*x*x - 2*z*z; m[6] = 2*y*z - 2*x*w;
m[8] = 2*x*z - 2*y*w; m[9] = 2*y*z + 2*x*w; m[10] = 1 - 2*x*x - 2*y*y;
}
double x,y,z,w;
};
#endif /* QUATERNION_H_ */

@ -139,6 +139,7 @@ RMagnitudeTemplate(const T &x, vtkm::TypeTraitsVectorTag)
/// as fast as MagnitudeSquared.
///
template<typename T>
VTKM_EXEC_CONT_EXPORT
typename vtkm::VecTraits<T>::ComponentType
RMagnitude(const T &x)
{

@ -105,6 +105,7 @@ public:
typedef typename vtkm::cont::ArrayHandle<vtkm::Vec<FieldType, 3> >::template ExecutionTypes<DeviceAdapter>::Portal VectorPortalType;
VectorPortalType Vertices;
VectorPortalType Normals;
typedef typename vtkm::cont::ArrayHandle<vtkm::Id> IdArrayHandle;
typedef typename IdArrayHandle::ExecutionTypes<DeviceAdapter>::PortalConst IdPortalType;
@ -115,14 +116,15 @@ public:
template<typename U, typename W, typename X>
VTKM_CONT_EXPORT
IsoSurfaceGenerate(const float ivalue, const vtkm::Id3 cdims, IdPortalType triTablePortal,
const U & field, const U & source, const W & vertices, const X & scalars) :
const U & field, const U & source, const W & vertices, const W & normals, const X & scalars) :
Isovalue(ivalue),
xdim(cdims[0]), ydim(cdims[1]), zdim(cdims[2]),
xmin(-1), ymin(-1), zmin(-1), xmax(1), ymax(1), zmax(1),
Field( field.PrepareForInput( DeviceAdapter() ) ),
Source( source.PrepareForInput( DeviceAdapter() ) ),
Scalars(scalars),
Vertices(vertices),
Normals(normals),
Scalars(scalars),
TriTable(triTablePortal),
cellsPerLayer(xdim * ydim),
pointsPerLayer ((xdim+1)*(ydim+1))
@ -224,6 +226,16 @@ public:
this->Vertices.Set(outputVertId + v, vtkm::Lerp(p[v0], p[v1], t));
this->Scalars.Set(outputVertId + v, vtkm::Lerp(s[v0], s[v1], t));
}
vtkm::Vec<FieldType, 3> vertex0 = this->Vertices.Get(outputVertId + 0);
vtkm::Vec<FieldType, 3> vertex1 = this->Vertices.Get(outputVertId + 1);
vtkm::Vec<FieldType, 3> vertex2 = this->Vertices.Get(outputVertId + 2);
vtkm::Vec<FieldType, 3> curNorm = vtkm::Cross(vertex1-vertex0, vertex2-vertex0);
vtkm::Normalize(curNorm);
this->Normals.Set(outputVertId + 0, curNorm);
this->Normals.Set(outputVertId + 1, curNorm);
this->Normals.Set(outputVertId + 2, curNorm);
}
};
@ -242,6 +254,7 @@ public:
void Run(const float &isovalue,
IsoField isoField,
vtkm::cont::ArrayHandle< vtkm::Vec<CoordinateType,3> > &verticesArray,
vtkm::cont::ArrayHandle< vtkm::Vec<CoordinateType,3> > &normalsArray,
vtkm::cont::ArrayHandle<FieldType> &scalarsArray)
{
typedef typename vtkm::cont::DeviceAdapterAlgorithm<DeviceAdapter> DeviceAlgorithms;
@ -303,6 +316,7 @@ public:
field,
field,
verticesArray.PrepareForOutput(numTotalVertices, DeviceAdapter()),
normalsArray.PrepareForOutput(numTotalVertices, DeviceAdapter()),
scalarsArray.PrepareForOutput(numTotalVertices, DeviceAdapter())
);

@ -121,11 +121,12 @@ void TestIsosurfaceUniformGrid()
vtkm::worklet::IsosurfaceFilterUniformGrid<vtkm::Float32,
DeviceAdapter> isosurfaceFilter(dims, dataSet);
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::Float32,3> > verticesArray;
vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::Float32,3> > verticesArray, normalsArray;
vtkm::cont::ArrayHandle<vtkm::Float32> scalarsArray;
isosurfaceFilter.Run(0.5,
dataSet.GetField("nodevar").GetData(),
verticesArray,
normalsArray,
scalarsArray);
VTKM_TEST_ASSERT(test_equal(verticesArray.GetNumberOfValues(), 480),