├── .azure └── build.sh ├── .clang-format ├── .gitattributes ├── CMakeLists.txt ├── README.md ├── azure-pipelines.yml ├── data └── kitchen.vtk ├── tut_2filters.cxx ├── tut_contour.cxx ├── tut_contour_2fields.cxx ├── tut_error_handling.cxx ├── tut_extract_edges.cxx ├── tut_io.cxx ├── tut_logging.cxx ├── tut_mag_grad.cxx ├── tut_mag_grad_generalized.cxx ├── tut_point_to_cell.cxx └── tut_rendering.cxx /.azure/build.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | path=$(pwd) 3 | 4 | cmake_cmd=cmake 5 | if [ "$1" != "" ]; then 6 | echo "Using the cmake provided at $1" 7 | cmake_cmd="$1" 8 | else 9 | echo "Defaulting to cmake found on path" 10 | fi 11 | 12 | source_dir="${path}/sources" 13 | build_dir="${path}/builds" 14 | install_dir="${path}/install" 15 | 16 | #Note this requires CMake 3.13+ as we are using `-S` and `-B` 17 | 18 | #download the required repos 19 | if [ ! -f "${source_dir}/vtkm/CMakeLists.txt" ]; then 20 | echo "cloning vtkm to ${source_dir}/vtkm" 21 | git clone --depth=1 https://gitlab.kitware.com/vtk/vtk-m.git "${source_dir}/vtkm" 22 | git checkout v1.5.0 23 | fi 24 | if [ ! -f "${source_dir}/tutorial/CMakeLists.txt" ]; then 25 | echo "cloning vtk-m tutorials to ${source_dir}/tutorial" 26 | git clone --depth=1 https://github.com/uo-cdux/vtk-m-tutorial.git "${source_dir}/tutorial" 27 | fi 28 | 29 | # 30 | # Configure && Build VTK-m 31 | # We cache the build directory so we run this each time 32 | 33 | echo "Configuring vtk-m with" 34 | echo "${cmake_cmd} -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=MinSizeRel VTKm_ENABLE_RENDERING=ON -DVTKm_ENABLE_TESTING=OFF -DVTKm_ENABLE_LOGGING=ON -DVTKm_USE_64BIT_IDS=OFF -DVTKm_USE_DOUBLE_PRECISION=ON" 35 | mkdir -p "${build_dir}/vtkm" 36 | cd "${build_dir}/vtkm" 37 | ${cmake_cmd} -DBUILD_SHARED_LIBS=OFF -DCMAKE_BUILD_TYPE=MinSizeRel -DVTKm_ENABLE_RENDERING=ON -DVTKm_ENABLE_TESTING=OFF -DVTKm_ENABLE_LOGGING=ON -DVTKm_USE_64BIT_IDS=OFF -DVTKm_USE_DOUBLE_PRECISION=ON "${source_dir}/vtkm" 38 | echo "Building vtk-m" 39 | ${cmake_cmd} --build "${build_dir}/vtkm" -j 3 40 | ${cmake_cmd} --install "${build_dir}/vtkm" --prefix "${install_dir}" 41 | 42 | 43 | cd ${path} 44 | # 45 | # Configure && Build tutorial 46 | echo "Configuring tutorial" 47 | mkdir -p "${build_dir}/tutorial" 48 | cd "${build_dir}/tutorial" 49 | ${cmake_cmd} -DCMAKE_BUILD_TYPE=Release -DCMAKE_PREFIX_PATH="${install_dir}" "${source_dir}/tutorial" 50 | cd ${path} 51 | echo "Building tutorial" 52 | ${cmake_cmd} --build "${build_dir}/tutorial" -j 3 53 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | # This configuration requires clang-format 3.8 or higher. 3 | BasedOnStyle: Mozilla 4 | AlignAfterOpenBracket: Align 5 | AlignOperands: false 6 | AlwaysBreakAfterReturnType: None 7 | AlwaysBreakAfterDefinitionReturnType: None 8 | BreakBeforeBraces: Allman 9 | BinPackArguments: false 10 | BinPackParameters: false 11 | ColumnLimit: 100 12 | MaxEmptyLinesToKeep: 4 13 | Standard: Cpp11 14 | # This requires clang-format 4.0 (at least). 15 | #FixNamespaceComments: true 16 | ReflowComments: false 17 | ... 18 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Attributes used for formatting. 2 | [attr]our-c-style whitespace=tab-in-indent format.clang-format 3 | 4 | *.cxx our-c-style 5 | *.h our-c-style 6 | *.hxx our-c-style 7 | *.cu our-c-style 8 | 9 | data/** filter=lfs diff=lfs merge=lfs -text 10 | /**/data/** filter=lfs diff=lfs merge=lfs -text 11 | 12 | *.cmake whitespace=tab-in-indent 13 | *.md whitespace=tab-in-indent conflict-marker-size=79 -whitespace 14 | *.rst whitespace=tab-in-indent conflict-marker-size=79 15 | *.txt whitespace=tab-in-indent 16 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.10) 2 | project(VTKm_tut) 3 | 4 | find_package(VTKm REQUIRED) 5 | 6 | add_executable(tut_io tut_io.cxx) 7 | target_link_libraries(tut_io PUBLIC vtkm_filter vtkm_io) 8 | 9 | add_executable(tut_contour tut_contour.cxx) 10 | target_link_libraries(tut_contour PUBLIC vtkm_filter vtkm_io) 11 | 12 | add_executable(tut_contour_2fields tut_contour_2fields.cxx) 13 | target_link_libraries(tut_contour_2fields PUBLIC vtkm_filter vtkm_io) 14 | 15 | add_executable(tut_2filters tut_2filters.cxx) 16 | target_link_libraries(tut_2filters PUBLIC vtkm_filter vtkm_io) 17 | 18 | add_executable(tut_mag_grad tut_mag_grad.cxx) 19 | target_link_libraries(tut_mag_grad PUBLIC vtkm_filter vtkm_io) 20 | 21 | add_executable(tut_rendering tut_rendering.cxx) 22 | target_link_libraries(tut_rendering PUBLIC vtkm_filter vtkm_io vtkm_rendering) 23 | 24 | add_executable(tut_error_handling tut_error_handling.cxx) 25 | target_link_libraries(tut_error_handling PUBLIC vtkm_filter vtkm_io) 26 | 27 | add_executable(tut_logging tut_logging.cxx) 28 | target_link_libraries(tut_logging PUBLIC vtkm_filter vtkm_io) 29 | 30 | add_executable(tut_point_to_cell tut_point_to_cell.cxx) 31 | target_link_libraries(tut_point_to_cell PUBLIC vtkm_cont vtkm_filter vtkm_io) 32 | 33 | add_executable(tut_extract_edges tut_extract_edges.cxx) 34 | target_link_libraries(tut_extract_edges PUBLIC vtkm_cont vtkm_filter vtkm_io) 35 | 36 | 37 | # Copy the data file to be adjacent to the binaries 38 | file(GENERATE OUTPUT "$/data/kitchen.vtk" INPUT "${CMAKE_CURRENT_SOURCE_DIR}/data/kitchen.vtk") 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # vtk-m-tutorial 2 | The repository containts code samples for the tutorial of the [VTK-m code library](http://m.vtk.org). 3 | Other tutorial material is avaialble on the [VTK-m wiki](http://m.vtk.org/index.php/Tutorial) 4 | 5 | # Organization 6 | The tutorial is divided into 3 sections based on what users want to accomplish with VTK-m: 7 | 1. Using VTK-m 8 | 2. Algorithm development with VTK-m 9 | 3. Advanced development with VTK-m 10 | -------------------------------------------------------------------------------- /azure-pipelines.yml: -------------------------------------------------------------------------------- 1 | # Verify VTK-m tutorial pipeline 2 | 3 | schedules: 4 | - cron: "0 0 * * *" 5 | displayName: Nighly build 6 | branches: 7 | include: 8 | - master 9 | always: true 10 | 11 | # Also run builds each time master changes 12 | trigger: 13 | - master 14 | 15 | pool: 16 | vmImage: 'ubuntu-16.04' 17 | jobs: 18 | - job: BuildAndVerify 19 | timeoutInMinutes: 100 20 | steps: 21 | - script: | 22 | sudo apt update -qq 23 | sudo apt install -y ninja-build 24 | sudo apt clean 25 | wget https://github.com/Kitware/CMake/releases/download/v3.15.1/cmake-3.15.1-Linux-x86_64.sh 26 | chmod +x cmake-3.15.1-Linux-x86_64.sh 27 | ./cmake-3.15.1-Linux-x86_64.sh --skip-license --prefix=${PWD} 28 | displayName: 'Setup Env with CMake 3.15, and Ninja' 29 | 30 | - script: | 31 | ./.azure/build.sh ${PWD}/bin/cmake 32 | displayName: 'Build VTK-m and tutorials' 33 | -------------------------------------------------------------------------------- /tut_2filters.cxx: -------------------------------------------------------------------------------- 1 | // Example 4: do a contour and a clip-with-field, and write it out. 2 | // 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | int main(int argc, char** argv) 10 | { 11 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 12 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 13 | 14 | vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); 15 | vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 16 | 17 | vtkm::filter::Contour contour; 18 | contour.SetActiveField("c1"); 19 | contour.SetFieldsToPass({ "c1", "ke" }); 20 | contour.SetNumberOfIsoValues(3); 21 | contour.SetIsoValue(0, 0.05); 22 | contour.SetIsoValue(1, 0.10); 23 | contour.SetIsoValue(2, 0.15); 24 | vtkm::cont::DataSet ds_from_mc = contour.Execute(ds_from_file); 25 | 26 | vtkm::filter::ClipWithField clip; 27 | clip.SetActiveField("ke"); 28 | clip.SetClipValue(1e-7); 29 | //clip.SetInvertClip(true); // <1e-7 instead of >1e-7 30 | 31 | vtkm::cont::DataSet ds_from_clip = clip.Execute(ds_from_mc); 32 | 33 | vtkm::io::VTKDataSetWriter writer("out_2filters.vtk"); 34 | writer.WriteDataSet(ds_from_clip); 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /tut_contour.cxx: -------------------------------------------------------------------------------- 1 | // Example 2: do a contour!, write it out. 2 | // 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char** argv) 9 | { 10 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 11 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 12 | 13 | vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); 14 | vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 15 | 16 | vtkm::filter::Contour contour; 17 | contour.SetActiveField("c1"); 18 | contour.SetNumberOfIsoValues(3); 19 | contour.SetIsoValue(0, 0.05); 20 | contour.SetIsoValue(1, 0.10); 21 | contour.SetIsoValue(2, 0.15); 22 | 23 | vtkm::cont::DataSet ds_from_mc = contour.Execute(ds_from_file); 24 | vtkm::io::VTKDataSetWriter writer("out_mc.vtk"); 25 | writer.WriteDataSet(ds_from_mc); 26 | 27 | return 0; 28 | } 29 | -------------------------------------------------------------------------------- /tut_contour_2fields.cxx: -------------------------------------------------------------------------------- 1 | // Example 3: do a contour (but only evaluate two fields), write it out. 2 | // 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char** argv) 9 | { 10 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 11 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 12 | 13 | vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); 14 | vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 15 | 16 | vtkm::filter::Contour contour; 17 | contour.SetActiveField("c1"); 18 | contour.SetFieldsToPass({ "c1", "ke" }); 19 | contour.SetNumberOfIsoValues(3); 20 | contour.SetIsoValue(0, 0.05); 21 | contour.SetIsoValue(1, 0.10); 22 | contour.SetIsoValue(2, 0.15); 23 | 24 | vtkm::cont::DataSet ds_from_mc = contour.Execute(ds_from_file); 25 | vtkm::io::VTKDataSetWriter writer("out_mc_2fields.vtk"); 26 | writer.WriteDataSet(ds_from_mc); 27 | 28 | return 0; 29 | } 30 | -------------------------------------------------------------------------------- /tut_error_handling.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | int main(int argc, char** argv) 7 | { 8 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 9 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 10 | 11 | try 12 | { 13 | vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); 14 | 15 | // PROBLEM! ... we aren't reading from a file, so we have an empty vtkm::cont::DataSet. 16 | //vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 17 | vtkm::cont::DataSet ds_from_file; 18 | 19 | vtkm::filter::Contour contour; 20 | contour.SetActiveField("c1"); 21 | contour.SetNumberOfIsoValues(3); 22 | contour.SetIsoValue(0, 0.05); 23 | contour.SetIsoValue(1, 0.10); 24 | contour.SetIsoValue(2, 0.15); 25 | 26 | vtkm::cont::DataSet ds_from_mc = contour.Execute(ds_from_file); 27 | vtkm::io::VTKDataSetWriter writer("out_mc.vtk"); 28 | writer.WriteDataSet(ds_from_mc); 29 | } 30 | catch (const vtkm::cont::Error& error) 31 | { 32 | std::cerr << "VTK-m error occurred!: " << error.GetMessage() << std::endl; 33 | return 1; 34 | } 35 | 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /tut_extract_edges.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | 7 | #include 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | 17 | namespace vtkm 18 | { 19 | namespace worklet 20 | { 21 | 22 | struct CountEdgesWorklet : vtkm::worklet::WorkletVisitCellsWithPoints 23 | { 24 | using ControlSignature = void(CellSetIn cellSet, FieldOut numEdges); 25 | using ExecutionSignature = _2(CellShape, PointCount); 26 | 27 | template 28 | VTKM_EXEC_CONT vtkm::IdComponent operator()( 29 | CellShapeTag cellShape, 30 | vtkm::IdComponent numPointsInCell) const 31 | { 32 | vtkm::IdComponent numEdges; 33 | vtkm::exec::CellEdgeNumberOfEdges(numPointsInCell, cellShape, numEdges); 34 | return numEdges; 35 | } 36 | }; 37 | 38 | struct EdgeIdsWorklet : vtkm::worklet::WorkletVisitCellsWithPoints 39 | { 40 | using ControlSignature = void(CellSetIn cellSet, FieldOut canonicalIds); 41 | using ExecutionSignature = void(CellShape cellShape, 42 | PointIndices globalPointIndices, 43 | VisitIndex localEdgeIndex, 44 | _2 canonicalIdOut); 45 | 46 | using ScatterType = vtkm::worklet::ScatterCounting; 47 | 48 | template 49 | VTKM_EXEC void operator()(CellShapeTag cellShape, 50 | const PointIndexVecType& globalPointIndicesForCell, 51 | vtkm::IdComponent localEdgeIndex, 52 | vtkm::Id2& canonicalIdOut) const 53 | { 54 | vtkm::IdComponent numPointsInCell = 55 | globalPointIndicesForCell.GetNumberOfComponents(); 56 | 57 | vtkm::exec::CellEdgeCanonicalId( 58 | numPointsInCell, localEdgeIndex, cellShape, globalPointIndicesForCell, canonicalIdOut); 59 | } 60 | }; 61 | 62 | struct EdgeIndicesWorklet : vtkm::worklet::WorkletReduceByKey 63 | { 64 | using ControlSignature = void(KeysIn keys, 65 | WholeCellSetIn<> inputCells, 66 | ValuesIn originCells, 67 | ValuesIn originEdges, 68 | ReducedValuesOut connectivityOut); 69 | using ExecutionSignature = void(_2 inputCells, 70 | _3 originCell, 71 | _4 originEdge, 72 | _5 connectivityOut); 73 | using InputDomain = _1; 74 | 75 | template 76 | VTKM_EXEC void operator()(const CellSetType& cellSet, 77 | const OriginCellsType& originCells, 78 | const OriginEdgesType& originEdges, 79 | vtkm::Id2& connectivityOut) const 80 | { 81 | // Regardless of how many cells are sharing the edge we are generating, we 82 | // know that each cell/edge given to us by the reduce-by-key refers to the 83 | // same edge, so we can just look at the first cell to get the edge. 84 | vtkm::IdComponent numPointsInCell = cellSet.GetNumberOfIndices(originCells[0]); 85 | vtkm::IdComponent edgeIndex = originEdges[0]; 86 | auto cellShape = cellSet.GetCellShape(originCells[0]); 87 | 88 | vtkm::IdComponent pointInCellIndex0; 89 | vtkm::exec::CellEdgeLocalIndex( 90 | numPointsInCell, 0, edgeIndex, cellShape, pointInCellIndex0); 91 | vtkm::IdComponent pointInCellIndex1; 92 | vtkm::exec::CellEdgeLocalIndex( 93 | numPointsInCell, 1, edgeIndex, cellShape, pointInCellIndex1); 94 | 95 | auto globalPointIndicesForCell = cellSet.GetIndices(originCells[0]); 96 | connectivityOut[0] = globalPointIndicesForCell[pointInCellIndex0]; 97 | connectivityOut[1] = globalPointIndicesForCell[pointInCellIndex1]; 98 | } 99 | }; 100 | 101 | } // namespace worklet 102 | } // namespace vtkm 103 | 104 | namespace vtkm 105 | { 106 | namespace filter 107 | { 108 | 109 | class ExtractEdges : public vtkm::filter::FilterDataSet 110 | { 111 | public: 112 | template 113 | VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& inData, 114 | vtkm::filter::PolicyBase policy); 115 | 116 | template 117 | VTKM_CONT bool DoMapField(vtkm::cont::DataSet& result, 118 | const vtkm::cont::ArrayHandle& input, 119 | const vtkm::filter::FieldMetadata& fieldMeta, 120 | const vtkm::filter::PolicyBase& policy); 121 | 122 | private: 123 | vtkm::worklet::ScatterCounting::OutputToInputMapType OutputToInputCellMap; 124 | vtkm::worklet::Keys CellToEdgeKeys; 125 | }; 126 | 127 | template 128 | inline VTKM_CONT vtkm::cont::DataSet ExtractEdges::DoExecute( 129 | const vtkm::cont::DataSet& inData, 130 | vtkm::filter::PolicyBase policy) 131 | { 132 | auto inCellSet = vtkm::filter::ApplyPolicyCellSet(inData.GetCellSet(), policy, *this); 133 | 134 | // First, count the edges in each cell. 135 | vtkm::cont::ArrayHandle edgeCounts; 136 | this->Invoke(vtkm::worklet::CountEdgesWorklet{}, inCellSet, edgeCounts); 137 | 138 | // Second, using these counts build a scatter that repeats a cell's visit 139 | // for each edge in the cell. 140 | vtkm::worklet::ScatterCounting scatter(edgeCounts); 141 | this->OutputToInputCellMap = 142 | scatter.GetOutputToInputMap(inCellSet.GetNumberOfCells()); 143 | vtkm::worklet::ScatterCounting::VisitArrayType outputToInputEdgeMap = 144 | scatter.GetVisitArray(inCellSet.GetNumberOfCells()); 145 | 146 | // Third, for each edge, extract a canonical id. 147 | vtkm::cont::ArrayHandle canonicalIds; 148 | this->Invoke(vtkm::worklet::EdgeIdsWorklet{}, scatter, inCellSet, canonicalIds); 149 | 150 | // Fourth, construct a Keys object to combine all like edge ids. 151 | this->CellToEdgeKeys = vtkm::worklet::Keys(canonicalIds); 152 | 153 | // Fifth, use a reduce-by-key to extract indices for each unique edge. 154 | vtkm::cont::ArrayHandle connectivityArray; 155 | this->Invoke(vtkm::worklet::EdgeIndicesWorklet{}, 156 | this->CellToEdgeKeys, 157 | inCellSet, 158 | this->OutputToInputCellMap, 159 | outputToInputEdgeMap, 160 | vtkm::cont::make_ArrayHandleGroupVec<2>(connectivityArray)); 161 | 162 | // Sixth, use the created connectivity array to build a cell set. 163 | vtkm::cont::CellSetSingleType<> outCellSet; 164 | outCellSet.Fill( 165 | inCellSet.GetNumberOfPoints(), vtkm::CELL_SHAPE_LINE, 2, connectivityArray); 166 | 167 | vtkm::cont::DataSet outData; 168 | 169 | outData.SetCellSet(outCellSet); 170 | 171 | for (vtkm::IdComponent coordSystemIndex = 0; 172 | coordSystemIndex < inData.GetNumberOfCoordinateSystems(); 173 | ++coordSystemIndex) 174 | { 175 | outData.AddCoordinateSystem(inData.GetCoordinateSystem(coordSystemIndex)); 176 | } 177 | 178 | return outData; 179 | } 180 | 181 | template 182 | inline VTKM_CONT bool ExtractEdges::DoMapField( 183 | vtkm::cont::DataSet& result, 184 | const vtkm::cont::ArrayHandle& inputArray, 185 | const vtkm::filter::FieldMetadata& fieldMeta, 186 | const vtkm::filter::PolicyBase&) 187 | { 188 | vtkm::cont::Field outputField; 189 | 190 | if (fieldMeta.IsPointField()) 191 | { 192 | outputField = fieldMeta.AsField(inputArray); // pass through 193 | } 194 | else if (fieldMeta.IsCellField()) 195 | { 196 | auto outputCellArray = 197 | vtkm::worklet::AverageByKey::Run(this->CellToEdgeKeys, 198 | vtkm::cont::make_ArrayHandlePermutation( 199 | this->OutputToInputCellMap, inputArray)); 200 | outputField = fieldMeta.AsField(outputCellArray); 201 | } 202 | else 203 | { 204 | return false; 205 | } 206 | 207 | result.AddField(outputField); 208 | 209 | return true; 210 | } 211 | 212 | } // namespace filter 213 | } // namespace vtkm 214 | 215 | int main(int argc, char** argv) 216 | { 217 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 218 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 219 | 220 | const char *input = "data/kitchen.vtk"; 221 | vtkm::io::VTKDataSetReader reader(input); 222 | vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 223 | 224 | vtkm::filter::Contour contour; 225 | contour.SetActiveField("c1"); 226 | contour.SetIsoValue(0.10); 227 | vtkm::cont::DataSet ds_from_contour = contour.Execute(ds_from_file); 228 | 229 | vtkm::filter::ExtractEdges extractEdges; 230 | vtkm::cont::DataSet wireframe = extractEdges.Execute(ds_from_contour); 231 | 232 | vtkm::io::VTKDataSetWriter writer("out_wireframe.vtk"); 233 | writer.WriteDataSet(wireframe); 234 | 235 | return 0; 236 | } 237 | -------------------------------------------------------------------------------- /tut_io.cxx: -------------------------------------------------------------------------------- 1 | // Example 1: very simple VTK-m program. 2 | // Read data set, write it out. 3 | // 4 | #include 5 | #include 6 | #include 7 | 8 | int main(int argc, char** argv) 9 | { 10 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 11 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 12 | 13 | const char *input = "data/kitchen.vtk"; 14 | vtkm::io::VTKDataSetReader reader(input); 15 | vtkm::cont::DataSet ds = reader.ReadDataSet(); 16 | vtkm::io::VTKDataSetWriter writer("out_io.vtk"); 17 | writer.WriteDataSet(ds); 18 | 19 | return 0; 20 | } 21 | -------------------------------------------------------------------------------- /tut_logging.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | 6 | int main(int argc, char** argv) 7 | { 8 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 9 | 10 | // SetLogLevelName must be called before Initialize 11 | vtkm::cont::SetLogLevelName(vtkm::cont::LogLevel::UserFirst, "tut_log"); 12 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 13 | 14 | const std::string input = "data/kitchen.vtk"; 15 | vtkm::io::VTKDataSetReader reader(input); 16 | VTKM_LOG_F(vtkm::cont::LogLevel::Info, "Reading from file %s", input.c_str()); 17 | vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 18 | VTKM_LOG_F(vtkm::cont::LogLevel::Info, "Done reading from file %s", input.c_str()); 19 | 20 | const std::string output = "out_logging.vtk"; 21 | VTKM_LOG_S(vtkm::cont::LogLevel::Info, "Writing to file" << output); 22 | vtkm::io::VTKDataSetWriter writer(output); 23 | writer.WriteDataSet(ds_from_file); 24 | VTKM_LOG_S(vtkm::cont::LogLevel::Info, "Done writing to file" << output); 25 | 26 | return 0; 27 | } 28 | -------------------------------------------------------------------------------- /tut_mag_grad.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | #include 8 | #include 9 | 10 | #include 11 | 12 | #include 13 | 14 | struct ComputeMagnitude : vtkm::worklet::WorkletMapField 15 | { 16 | using ControlSignature = void(FieldIn inputVectors, FieldOut outputMagnitudes); 17 | 18 | VTKM_EXEC void operator()(const vtkm::Vec3f& inVector, vtkm::FloatDefault& outMagnitude) const 19 | { 20 | outMagnitude = vtkm::Magnitude(inVector); 21 | } 22 | }; 23 | 24 | #include 25 | 26 | class FieldMagnitude : public vtkm::filter::FilterField 27 | { 28 | public: 29 | using SupportedTypes = vtkm::ListTagBase; 30 | 31 | template 32 | VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& inDataSet, 33 | const ArrayHandleType& inField, 34 | const vtkm::filter::FieldMetadata& fieldMetadata, 35 | vtkm::filter::PolicyBase) 36 | { 37 | vtkm::cont::ArrayHandle outField; 38 | this->Invoke(ComputeMagnitude{}, inField, outField); 39 | 40 | std::string outFieldName = this->GetOutputFieldName(); 41 | if (outFieldName == "") 42 | { 43 | outFieldName = fieldMetadata.GetName() + "_magnitude"; 44 | } 45 | 46 | return vtkm::filter::CreateResult(inDataSet, outField, outFieldName, fieldMetadata); 47 | } 48 | }; 49 | 50 | int main(int argc, char** argv) 51 | { 52 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 53 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 54 | 55 | vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); 56 | vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 57 | 58 | vtkm::filter::Gradient grad; 59 | grad.SetActiveField("c1"); 60 | vtkm::cont::DataSet ds_from_grad = grad.Execute(ds_from_file); 61 | 62 | FieldMagnitude mag; 63 | mag.SetActiveField("Gradients"); 64 | vtkm::cont::DataSet mag_grad = mag.Execute(ds_from_grad); 65 | 66 | vtkm::io::VTKDataSetWriter writer("out_mag_grad.vtk"); 67 | writer.WriteDataSet(mag_grad); 68 | 69 | return 0; 70 | } 71 | -------------------------------------------------------------------------------- /tut_mag_grad_generalized.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #include 10 | #include 11 | 12 | #include 13 | 14 | #include 15 | 16 | namespace vtkm 17 | { 18 | namespace worklet 19 | { 20 | 21 | struct Magnitude : public vtkm::worklet::WorkletMapField 22 | { 23 | using ControlSignature = void(FieldIn inputVectors, FieldOut outputMagnitudes); 24 | 25 | template 26 | VTKM_EXEC void operator()(const vtkm::Vec& inVector, T& outMagnitude) const 27 | { 28 | outMagnitude = vtkm::Magnitude(inVector); 29 | } 30 | }; 31 | 32 | } // namespace worklet 33 | } // namespace vtkm 34 | 35 | #include 36 | 37 | namespace vtkm 38 | { 39 | namespace filter 40 | { 41 | 42 | class FieldMagnitude : public vtkm::filter::FilterField 43 | { 44 | public: 45 | using SupportedTypes = vtkm::TypeListTagVecCommon; 46 | 47 | template 48 | VTKM_CONT cont::DataSet DoExecute(const vtkm::cont::DataSet& inDataSet, 49 | const ArrayHandleType& inField, 50 | const vtkm::filter::FieldMetadata& fieldMetadata, 51 | vtkm::filter::PolicyBase) 52 | { 53 | VTKM_IS_ARRAY_HANDLE(ArrayHandleType); 54 | 55 | using ValueType = typename ArrayHandleType::ValueType; 56 | using ComponentType = decltype(ValueType{}[0]); //component type of vector 57 | 58 | vtkm::cont::ArrayHandle outField; 59 | this->Invoke(vtkm::worklet::Magnitude{}, inField, outField); 60 | 61 | std::string outFieldName = this->GetOutputFieldName(); 62 | if (outFieldName == "") 63 | { 64 | outFieldName = fieldMetadata.GetName() + "_magnitude"; 65 | } 66 | 67 | return vtkm::filter::CreateResult(inDataSet, outField, outFieldName, fieldMetadata); 68 | } 69 | }; 70 | 71 | } // namespace filter 72 | } // namespace vtkm 73 | 74 | int main(int argc, char** argv) 75 | { 76 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 77 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 78 | 79 | vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); 80 | vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 81 | 82 | vtkm::filter::Gradient grad; 83 | grad.SetActiveField("c1"); 84 | vtkm::cont::DataSet ds_from_grad = grad.Execute(ds_from_file); 85 | 86 | vtkm::filter::FieldMagnitude mag; 87 | mag.SetActiveField("Gradients"); 88 | vtkm::cont::DataSet mag_grad = mag.Execute(ds_from_grad); 89 | 90 | vtkm::io::VTKDataSetWriter writer("out_mag_grad.vtk"); 91 | writer.WriteDataSet(mag_grad); 92 | 93 | return 0; 94 | } 95 | -------------------------------------------------------------------------------- /tut_point_to_cell.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | #include 7 | 8 | namespace vtkm 9 | { 10 | namespace worklet 11 | { 12 | 13 | struct ConvertPointFieldToCells : vtkm::worklet::WorkletVisitCellsWithPoints 14 | { 15 | using ControlSignature = void(CellSetIn topology, 16 | FieldInPoint inPointField, 17 | FieldOutCell outCellField); 18 | using ExecutionSignature = void(_2 inPointField, _3 outCellField); 19 | using InputDomain = _1; 20 | 21 | template 22 | void operator()(const InPointFieldVecType& inPointFieldVec, 23 | OutCellFieldType& outCellField) const 24 | { 25 | vtkm::IdComponent numPoints = inPointFieldVec.GetNumberOfComponents(); 26 | 27 | outCellField = OutCellFieldType(0); 28 | for (vtkm::IdComponent pointIndex = 0; pointIndex < numPoints; ++pointIndex) 29 | { 30 | outCellField = outCellField + inPointFieldVec[pointIndex]; 31 | } 32 | outCellField = outCellField / OutCellFieldType(numPoints); 33 | } 34 | }; 35 | 36 | } // namespace worklet 37 | } // namespace vtkm 38 | 39 | #include 40 | 41 | namespace vtkm 42 | { 43 | namespace filter 44 | { 45 | 46 | struct ConvertPointFieldToCells : vtkm::filter::FilterField 47 | { 48 | template 49 | VTKM_CONT vtkm::cont::DataSet DoExecute( 50 | const vtkm::cont::DataSet& inDataSet, 51 | const ArrayHandleType& inField, 52 | const vtkm::filter::FieldMetadata& fieldMetadata, 53 | vtkm::filter::PolicyBase); 54 | }; 55 | 56 | template 57 | VTKM_CONT cont::DataSet ConvertPointFieldToCells::DoExecute( 58 | const vtkm::cont::DataSet& inDataSet, 59 | const ArrayHandleType& inField, 60 | const vtkm::filter::FieldMetadata& fieldMetadata, 61 | vtkm::filter::PolicyBase policy) 62 | { 63 | VTKM_IS_ARRAY_HANDLE(ArrayHandleType); 64 | 65 | using ValueType = typename ArrayHandleType::ValueType; 66 | 67 | vtkm::cont::ArrayHandle outField; 68 | this->Invoke(vtkm::worklet::ConvertPointFieldToCells{}, 69 | vtkm::filter::ApplyPolicyCellSet(inDataSet.GetCellSet(), policy, *this), 70 | inField, 71 | outField); 72 | 73 | std::string outFieldName = this->GetOutputFieldName(); 74 | if (outFieldName == "") 75 | { 76 | outFieldName = fieldMetadata.GetName(); 77 | } 78 | 79 | return vtkm::filter::CreateResultFieldCell(inDataSet, outField, outFieldName); 80 | } 81 | 82 | } // namespace filter 83 | } // namespace vtkm 84 | 85 | 86 | int main(int argc, char** argv) 87 | { 88 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 89 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 90 | 91 | const char *input = "data/kitchen.vtk"; 92 | vtkm::io::VTKDataSetReader reader(input); 93 | vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 94 | 95 | vtkm::filter::ConvertPointFieldToCells pointToCell; 96 | pointToCell.SetActiveField("c1"); 97 | vtkm::cont::DataSet ds_from_convert = pointToCell.Execute(ds_from_file); 98 | 99 | vtkm::io::VTKDataSetWriter writer("out_point_to_cell.vtk"); 100 | writer.WriteDataSet(ds_from_convert); 101 | 102 | return 0; 103 | } 104 | -------------------------------------------------------------------------------- /tut_rendering.cxx: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | int main(int argc, char** argv) 14 | { 15 | auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; 16 | vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); 17 | 18 | //Loading .vtk File 19 | vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); 20 | vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); 21 | 22 | //Creating Actor 23 | vtkm::cont::ColorTable colorTable("viridis"); 24 | vtkm::rendering::Actor actor(ds_from_file.GetCellSet(), 25 | ds_from_file.GetCoordinateSystem(), 26 | ds_from_file.GetField("c1"), 27 | colorTable); 28 | 29 | //Creating Scene and adding Actor 30 | vtkm::rendering::Scene scene; 31 | scene.AddActor(actor); 32 | 33 | //Creating and initializing the View using the Canvas, Ray Tracer Mappers, and Scene 34 | vtkm::rendering::MapperRayTracer mapper; 35 | vtkm::rendering::CanvasRayTracer canvas(1920, 1080); 36 | vtkm::rendering::View3D view(scene, mapper, canvas); 37 | 38 | //Setting the background and foreground colors; optional. 39 | view.SetBackgroundColor(vtkm::rendering::Color(1.0f, 1.0f, 1.0f)); 40 | view.SetForegroundColor(vtkm::rendering::Color(0.0f, 0.0f, 0.0f)); 41 | 42 | //Painting View 43 | view.Paint(); 44 | 45 | //Saving View 46 | view.SaveAs("BasicRendering.ppm"); 47 | 48 | return 0; 49 | } 50 | --------------------------------------------------------------------------------