VTK
dox/Accelerators/Dax/vtkDaxMarchingCubesImpl.h
Go to the documentation of this file.
00001 //=============================================================================
00002 //
00003 //  Copyright (c) Kitware, Inc.
00004 //  All rights reserved.
00005 //  See LICENSE.txt for details.
00006 //
00007 //  This software is distributed WITHOUT ANY WARRANTY; without even
00008 //  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00009 //  PURPOSE.  See the above copyright notice for more information.
00010 //
00011 //  Copyright 2012 Sandia Corporation.
00012 //  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00013 //  the U.S. Government retains certain rights in this software.
00014 //
00015 //=============================================================================
00016 
00017 #ifndef __vtkDaxMarchingCubesImpl_h
00018 #define __vtkDaxMarchingCubesImpl_h
00019 
00020 // Common code
00021 #include "vtkDaxConfig.h"
00022 #include "vtkDaxDetailCommon.h"
00023 
00024 #include "vtkDispatcher.h"
00025 #include "vtkDoubleDispatcher.h"
00026 #include "vtkNew.h"
00027 
00028 //fields we support
00029 #include "vtkFloatArray.h"
00030 
00031 //cell types we support
00032 #include "vtkCellTypes.h"
00033 #include "vtkGenericCell.h"
00034 #include "vtkTriangle.h"
00035 #include "vtkVoxel.h"
00036 
00037 //datasets we support
00038 #include "vtkDataObjectTypes.h"
00039 #include "vtkImageData.h"
00040 #include "vtkUniformGrid.h"
00041 #include "vtkPolyData.h"
00042 
00043 //helpers that convert vtk to dax
00044 #include "vtkToDax/Portals.h"
00045 #include "vtkToDax/Containers.h"
00046 #include "vtkToDax/CellTypeToType.h"
00047 #include "vtkToDax/DataSetTypeToType.h"
00048 #include "vtkToDax/FieldTypeToType.h"
00049 #include "vtkToDax/MarchingCubes.h"
00050 
00051 namespace vtkDax {
00052 namespace detail {
00053   struct ValidMarchingCubesInput
00054   {
00055     typedef int ReturnType;
00056     vtkDataSet* Input;
00057     vtkCell* Cell;
00058     double IsoValue;
00059 
00060     vtkPolyData* Result;
00061 
00062     ValidMarchingCubesInput(vtkDataSet* in, vtkPolyData* out,
00063                         vtkCell* cell, double isoValue):
00064       Input(in),Cell(cell),IsoValue(isoValue),Result(out){}
00065 
00066     template<typename LHS>
00067     int operator()(LHS &arrayField) const
00068       {
00069       //we can derive the type of the field at compile time, but not the
00070       //length
00071       if (arrayField.GetNumberOfComponents() == 1)
00072         {
00073         //first we extract the field type of the array
00074         //second we extract the number of components
00075         typedef typename vtkToDax::FieldTypeToType<LHS,1>::FieldType FT1;
00076         return dispatchOnFieldType<LHS,FT1>(arrayField);
00077         }
00078       return 0;
00079       }
00080 
00081     template<typename VTKArrayType, typename DaxFieldType>
00082     int dispatchOnFieldType(VTKArrayType& vtkField) const
00083       {
00084       typedef DaxFieldType FieldType;
00085       typedef vtkToDax::vtkArrayContainerTag<VTKArrayType> FieldTag;
00086       typedef dax::cont::ArrayHandle<FieldType,FieldTag> FieldHandle;
00087 
00088       typedef typename dax::cont::ArrayHandle
00089         <FieldType, FieldTag>::PortalConstControl PortalType;
00090 
00091       FieldHandle field = FieldHandle( PortalType(&vtkField,
00092                                             vtkField.GetNumberOfTuples() ) );
00093       vtkToDax::MarchingCubes<FieldHandle> marching(field,
00094                                                  FieldType(IsoValue));
00095       marching.setFieldName(vtkField.GetName());
00096       marching.setOutputGrid(this->Result);
00097 
00098       // see if we have a valid data set type if so will perform the
00099       // marchingcubes if possible
00100       vtkDoubleDispatcher<vtkDataSet,vtkCell,int> dataDispatcher;
00101       dataDispatcher.Add<vtkImageData,vtkVoxel>(marching);
00102       dataDispatcher.Add<vtkUniformGrid,vtkVoxel>(marching);
00103 
00104       int validMC = dataDispatcher.Go(this->Input,this->Cell);
00105       return validMC;
00106       }
00107   private:
00108     void operator=(const ValidMarchingCubesInput&);
00109   };
00110 } //namespace detail
00111 
00112 
00113 //------------------------------------------------------------------------------
00114 int MarchingCubes(vtkDataSet* input, vtkPolyData *output,
00115               vtkDataArray* field, float isoValue)
00116 {
00117   //we are doing a point threshold now verify we have suitable cells
00118   //Dax currently supports: hexs,lines,quads,tets,triangles,vertex,voxel,wedge
00119   //if something a cell that doesn't match that list we punt to the
00120   //VTK implementation.
00121   vtkDax::detail::CellTypeInDataSet cType = vtkDax::detail::cellType(input);
00122 
00123   //construct the object that holds all the state needed to do the MC
00124   vtkDax::detail::ValidMarchingCubesInput validInput(input,output,cType.Cell,
00125                                                      isoValue);
00126 
00127 
00128   //setup the dispatch to only allow float and int array to go to the next step
00129   vtkDispatcher<vtkAbstractArray,int> fieldDispatcher;
00130   fieldDispatcher.Add<vtkFloatArray>(validInput);
00131   return fieldDispatcher.Go(field);
00132 }
00133 
00134 } //end vtkDax namespace
00135 // VTK-HeaderTest-Exclude: vtkDaxMarchingCubesImpl.h
00136 #endif