diff --git a/vtkm-tutorial/tutorialExamples/CMakeLists.txt b/vtkm-tutorial/tutorialExamples/CMakeLists.txt new file mode 100644 index 000000000..e0e8a168e --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 3.10) +project(VTKm_tut) + +find_package(VTKm REQUIRED) + +add_executable(tut_io tut_io.cxx) +target_link_libraries(tut_io PUBLIC vtkm_filter vtkm_io) + +add_executable(tut_contour tut_contour.cxx) +target_link_libraries(tut_contour PUBLIC vtkm_filter vtkm_io) + +add_executable(tut_contour_2fields tut_contour_2fields.cxx) +target_link_libraries(tut_contour_2fields PUBLIC vtkm_filter vtkm_io) + +add_executable(tut_2filters tut_2filters.cxx) +target_link_libraries(tut_2filters PUBLIC vtkm_filter vtkm_io) + +add_executable(tut_mag_grad tut_mag_grad.cxx) +target_link_libraries(tut_mag_grad PUBLIC vtkm_filter vtkm_io) + +add_executable(tut_rendering tut_rendering.cxx) +target_link_libraries(tut_rendering PUBLIC vtkm_filter vtkm_io vtkm_rendering) + +add_executable(tut_error_handling tut_error_handling.cxx) +target_link_libraries(tut_error_handling PUBLIC vtkm_filter vtkm_io) + +add_executable(tut_logging tut_logging.cxx) +target_link_libraries(tut_logging PUBLIC vtkm_filter vtkm_io) + +add_executable(tut_point_to_cell tut_point_to_cell.cxx) +target_link_libraries(tut_point_to_cell PUBLIC vtkm_cont vtkm_filter vtkm_io) + +add_executable(tut_extract_edges tut_extract_edges.cxx) +target_link_libraries(tut_extract_edges PUBLIC vtkm_cont vtkm_filter vtkm_io) + + +# Copy the data file to be adjacent to the binaries +file(GENERATE OUTPUT "$/data/kitchen.vtk" INPUT "${CMAKE_CURRENT_SOURCE_DIR}/data/kitchen.vtk") diff --git a/vtkm-tutorial/tutorialExamples/README.md b/vtkm-tutorial/tutorialExamples/README.md new file mode 100644 index 000000000..1aff329d9 --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/README.md @@ -0,0 +1,9 @@ +# vtk-m-tutorial +The repository containts code samples for the tutorial of the [VTK-m code library](http://m.vtk.org). +Other tutorial material is avaialble on the [VTK-m wiki](http://m.vtk.org/index.php/Tutorial) + +# Organization +The tutorial is divided into 3 sections based on what users want to accomplish with VTK-m: +1. Using VTK-m +2. Algorithm development with VTK-m +3. Advanced development with VTK-m diff --git a/vtkm-tutorial/tutorialExamples/azure-pipelines.yml b/vtkm-tutorial/tutorialExamples/azure-pipelines.yml new file mode 100644 index 000000000..c87cdd596 --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/azure-pipelines.yml @@ -0,0 +1,32 @@ +# Verify VTK-m tutorial pipeline + +schedules: +- cron: "0 0 * * *" + displayName: Nighly build + branches: + include: + - master + always: true + +# Also run builds each time master changes +trigger: +- master + +pool: + vmImage: 'ubuntu-16.04' +jobs: +- job: BuildAndVerify + timeoutInMinutes: 100 + steps: + - script: | + sudo apt update -qq + sudo apt install -y ninja-build + sudo apt clean + wget https://github.com/Kitware/CMake/releases/download/v3.15.1/cmake-3.15.1-Linux-x86_64.sh + chmod +x cmake-3.15.1-Linux-x86_64.sh + ./cmake-3.15.1-Linux-x86_64.sh --skip-license --prefix=${PWD} + displayName: 'Setup Env with CMake 3.15, and Ninja' + + - script: | + ./.azure/build.sh ${PWD}/bin/cmake + displayName: 'Build VTK-m and tutorials' diff --git a/vtkm-tutorial/tutorialExamples/tut_2filters.cxx b/vtkm-tutorial/tutorialExamples/tut_2filters.cxx new file mode 100644 index 000000000..af0160d10 --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_2filters.cxx @@ -0,0 +1,37 @@ +// Example 4: do a contour and a clip-with-field, and write it out. +// +#include +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); + vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + + vtkm::filter::Contour contour; + contour.SetActiveField("c1"); + contour.SetFieldsToPass({ "c1", "ke" }); + contour.SetNumberOfIsoValues(3); + contour.SetIsoValue(0, 0.05); + contour.SetIsoValue(1, 0.10); + contour.SetIsoValue(2, 0.15); + vtkm::cont::DataSet ds_from_mc = contour.Execute(ds_from_file); + + vtkm::filter::ClipWithField clip; + clip.SetActiveField("ke"); + clip.SetClipValue(1e-7); + //clip.SetInvertClip(true); // <1e-7 instead of >1e-7 + + vtkm::cont::DataSet ds_from_clip = clip.Execute(ds_from_mc); + + vtkm::io::VTKDataSetWriter writer("out_2filters.vtk"); + writer.WriteDataSet(ds_from_clip); + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_contour.cxx b/vtkm-tutorial/tutorialExamples/tut_contour.cxx new file mode 100644 index 000000000..34a801875 --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_contour.cxx @@ -0,0 +1,28 @@ +// Example 2: do a contour!, write it out. +// +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); + vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + + vtkm::filter::Contour contour; + contour.SetActiveField("c1"); + contour.SetNumberOfIsoValues(3); + contour.SetIsoValue(0, 0.05); + contour.SetIsoValue(1, 0.10); + contour.SetIsoValue(2, 0.15); + + vtkm::cont::DataSet ds_from_mc = contour.Execute(ds_from_file); + vtkm::io::VTKDataSetWriter writer("out_mc.vtk"); + writer.WriteDataSet(ds_from_mc); + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_contour_2fields.cxx b/vtkm-tutorial/tutorialExamples/tut_contour_2fields.cxx new file mode 100644 index 000000000..8f25eacdb --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_contour_2fields.cxx @@ -0,0 +1,29 @@ +// Example 3: do a contour (but only evaluate two fields), write it out. +// +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); + vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + + vtkm::filter::Contour contour; + contour.SetActiveField("c1"); + contour.SetFieldsToPass({ "c1", "ke" }); + contour.SetNumberOfIsoValues(3); + contour.SetIsoValue(0, 0.05); + contour.SetIsoValue(1, 0.10); + contour.SetIsoValue(2, 0.15); + + vtkm::cont::DataSet ds_from_mc = contour.Execute(ds_from_file); + vtkm::io::VTKDataSetWriter writer("out_mc_2fields.vtk"); + writer.WriteDataSet(ds_from_mc); + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_error_handling.cxx b/vtkm-tutorial/tutorialExamples/tut_error_handling.cxx new file mode 100644 index 000000000..5d09ab873 --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_error_handling.cxx @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + try + { + vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); + + // PROBLEM! ... we aren't reading from a file, so we have an empty vtkm::cont::DataSet. + //vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + vtkm::cont::DataSet ds_from_file; + + vtkm::filter::Contour contour; + contour.SetActiveField("c1"); + contour.SetNumberOfIsoValues(3); + contour.SetIsoValue(0, 0.05); + contour.SetIsoValue(1, 0.10); + contour.SetIsoValue(2, 0.15); + + vtkm::cont::DataSet ds_from_mc = contour.Execute(ds_from_file); + vtkm::io::VTKDataSetWriter writer("out_mc.vtk"); + writer.WriteDataSet(ds_from_mc); + } + catch (const vtkm::cont::Error& error) + { + std::cerr << "VTK-m error occurred!: " << error.GetMessage() << std::endl; + return 1; + } + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_extract_edges.cxx b/vtkm-tutorial/tutorialExamples/tut_extract_edges.cxx new file mode 100644 index 000000000..7ff7a003f --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_extract_edges.cxx @@ -0,0 +1,226 @@ +#include +#include +#include + +#include + +#include +#include +#include + +#include +#include + +#include +#include + +namespace vtkm +{ +namespace worklet +{ + +struct CountEdgesWorklet : vtkm::worklet::WorkletVisitCellsWithPoints +{ + using ControlSignature = void(CellSetIn cellSet, FieldOut numEdges); + using ExecutionSignature = _2(CellShape, PointCount); + + template + VTKM_EXEC_CONT vtkm::IdComponent operator()(CellShapeTag cellShape, + vtkm::IdComponent numPointsInCell) const + { + vtkm::IdComponent numEdges; + vtkm::exec::CellEdgeNumberOfEdges(numPointsInCell, cellShape, numEdges); + return numEdges; + } +}; + +struct EdgeIdsWorklet : vtkm::worklet::WorkletVisitCellsWithPoints +{ + using ControlSignature = void(CellSetIn cellSet, FieldOut canonicalIds); + using ExecutionSignature = void(CellShape cellShape, + PointIndices globalPointIndices, + VisitIndex localEdgeIndex, + _2 canonicalIdOut); + + using ScatterType = vtkm::worklet::ScatterCounting; + + template + VTKM_EXEC void operator()(CellShapeTag cellShape, + const PointIndexVecType& globalPointIndicesForCell, + vtkm::IdComponent localEdgeIndex, + vtkm::Id2& canonicalIdOut) const + { + vtkm::IdComponent numPointsInCell = globalPointIndicesForCell.GetNumberOfComponents(); + + vtkm::exec::CellEdgeCanonicalId( + numPointsInCell, localEdgeIndex, cellShape, globalPointIndicesForCell, canonicalIdOut); + } +}; + +struct EdgeIndicesWorklet : vtkm::worklet::WorkletReduceByKey +{ + using ControlSignature = void(KeysIn keys, + WholeCellSetIn<> inputCells, + ValuesIn originCells, + ValuesIn originEdges, + ReducedValuesOut connectivityOut); + using ExecutionSignature = void(_2 inputCells, _3 originCell, _4 originEdge, _5 connectivityOut); + using InputDomain = _1; + + template + VTKM_EXEC void operator()(const CellSetType& cellSet, + const OriginCellsType& originCells, + const OriginEdgesType& originEdges, + vtkm::Id2& connectivityOut) const + { + // Regardless of how many cells are sharing the edge we are generating, we + // know that each cell/edge given to us by the reduce-by-key refers to the + // same edge, so we can just look at the first cell to get the edge. + vtkm::IdComponent numPointsInCell = cellSet.GetNumberOfIndices(originCells[0]); + vtkm::IdComponent edgeIndex = originEdges[0]; + auto cellShape = cellSet.GetCellShape(originCells[0]); + + vtkm::IdComponent pointInCellIndex0; + vtkm::exec::CellEdgeLocalIndex(numPointsInCell, 0, edgeIndex, cellShape, pointInCellIndex0); + vtkm::IdComponent pointInCellIndex1; + vtkm::exec::CellEdgeLocalIndex(numPointsInCell, 1, edgeIndex, cellShape, pointInCellIndex1); + + auto globalPointIndicesForCell = cellSet.GetIndices(originCells[0]); + connectivityOut[0] = globalPointIndicesForCell[pointInCellIndex0]; + connectivityOut[1] = globalPointIndicesForCell[pointInCellIndex1]; + } +}; + +} // namespace worklet +} // namespace vtkm + +namespace vtkm +{ +namespace filter +{ + +class ExtractEdges : public vtkm::filter::FilterDataSet +{ +public: + template + VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& inData, + vtkm::filter::PolicyBase policy); + + template + VTKM_CONT bool DoMapField(vtkm::cont::DataSet& result, + const vtkm::cont::ArrayHandle& input, + const vtkm::filter::FieldMetadata& fieldMeta, + const vtkm::filter::PolicyBase& policy); + +private: + vtkm::worklet::ScatterCounting::OutputToInputMapType OutputToInputCellMap; + vtkm::worklet::Keys CellToEdgeKeys; +}; + +template +inline VTKM_CONT vtkm::cont::DataSet ExtractEdges::DoExecute( + const vtkm::cont::DataSet& inData, + vtkm::filter::PolicyBase policy) +{ + auto inCellSet = vtkm::filter::ApplyPolicyCellSet(inData.GetCellSet(), policy, *this); + + // First, count the edges in each cell. + vtkm::cont::ArrayHandle edgeCounts; + this->Invoke(vtkm::worklet::CountEdgesWorklet{}, inCellSet, edgeCounts); + + // Second, using these counts build a scatter that repeats a cell's visit + // for each edge in the cell. + vtkm::worklet::ScatterCounting scatter(edgeCounts); + this->OutputToInputCellMap = scatter.GetOutputToInputMap(inCellSet.GetNumberOfCells()); + vtkm::worklet::ScatterCounting::VisitArrayType outputToInputEdgeMap = + scatter.GetVisitArray(inCellSet.GetNumberOfCells()); + + // Third, for each edge, extract a canonical id. + vtkm::cont::ArrayHandle canonicalIds; + this->Invoke(vtkm::worklet::EdgeIdsWorklet{}, scatter, inCellSet, canonicalIds); + + // Fourth, construct a Keys object to combine all like edge ids. + this->CellToEdgeKeys = vtkm::worklet::Keys(canonicalIds); + + // Fifth, use a reduce-by-key to extract indices for each unique edge. + vtkm::cont::ArrayHandle connectivityArray; + this->Invoke(vtkm::worklet::EdgeIndicesWorklet{}, + this->CellToEdgeKeys, + inCellSet, + this->OutputToInputCellMap, + outputToInputEdgeMap, + vtkm::cont::make_ArrayHandleGroupVec<2>(connectivityArray)); + + // Sixth, use the created connectivity array to build a cell set. + vtkm::cont::CellSetSingleType<> outCellSet; + outCellSet.Fill(inCellSet.GetNumberOfPoints(), vtkm::CELL_SHAPE_LINE, 2, connectivityArray); + + vtkm::cont::DataSet outData; + + outData.SetCellSet(outCellSet); + + for (vtkm::IdComponent coordSystemIndex = 0; + coordSystemIndex < inData.GetNumberOfCoordinateSystems(); + ++coordSystemIndex) + { + outData.AddCoordinateSystem(inData.GetCoordinateSystem(coordSystemIndex)); + } + + return outData; +} + +template +inline VTKM_CONT bool ExtractEdges::DoMapField( + vtkm::cont::DataSet& result, + const vtkm::cont::ArrayHandle& inputArray, + const vtkm::filter::FieldMetadata& fieldMeta, + const vtkm::filter::PolicyBase&) +{ + vtkm::cont::Field outputField; + + if (fieldMeta.IsPointField()) + { + outputField = fieldMeta.AsField(inputArray); // pass through + } + else if (fieldMeta.IsCellField()) + { + auto outputCellArray = vtkm::worklet::AverageByKey::Run( + this->CellToEdgeKeys, + vtkm::cont::make_ArrayHandlePermutation(this->OutputToInputCellMap, inputArray)); + outputField = fieldMeta.AsField(outputCellArray); + } + else + { + return false; + } + + result.AddField(outputField); + + return true; +} + +} // namespace filter +} // namespace vtkm + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + const char* input = "data/kitchen.vtk"; + vtkm::io::VTKDataSetReader reader(input); + vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + + vtkm::filter::Contour contour; + contour.SetActiveField("c1"); + contour.SetIsoValue(0.10); + vtkm::cont::DataSet ds_from_contour = contour.Execute(ds_from_file); + + vtkm::filter::ExtractEdges extractEdges; + vtkm::cont::DataSet wireframe = extractEdges.Execute(ds_from_contour); + + vtkm::io::VTKDataSetWriter writer("out_wireframe.vtk"); + writer.WriteDataSet(wireframe); + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_io.cxx b/vtkm-tutorial/tutorialExamples/tut_io.cxx new file mode 100644 index 000000000..daa5c3492 --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_io.cxx @@ -0,0 +1,20 @@ +// Example 1: very simple VTK-m program. +// Read data set, write it out. +// +#include +#include +#include + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + const char* input = "data/kitchen.vtk"; + vtkm::io::VTKDataSetReader reader(input); + vtkm::cont::DataSet ds = reader.ReadDataSet(); + vtkm::io::VTKDataSetWriter writer("out_io.vtk"); + writer.WriteDataSet(ds); + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_logging.cxx b/vtkm-tutorial/tutorialExamples/tut_logging.cxx new file mode 100644 index 000000000..2c54dbc2a --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_logging.cxx @@ -0,0 +1,27 @@ +#include +#include +#include + + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + + // SetLogLevelName must be called before Initialize + vtkm::cont::SetLogLevelName(vtkm::cont::LogLevel::UserFirst, "tut_log"); + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + const std::string input = "data/kitchen.vtk"; + vtkm::io::VTKDataSetReader reader(input); + VTKM_LOG_F(vtkm::cont::LogLevel::Info, "Reading from file %s", input.c_str()); + vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + VTKM_LOG_F(vtkm::cont::LogLevel::Info, "Done reading from file %s", input.c_str()); + + const std::string output = "out_logging.vtk"; + VTKM_LOG_S(vtkm::cont::LogLevel::Info, "Writing to file" << output); + vtkm::io::VTKDataSetWriter writer(output); + writer.WriteDataSet(ds_from_file); + VTKM_LOG_S(vtkm::cont::LogLevel::Info, "Done writing to file" << output); + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_mag_grad.cxx b/vtkm-tutorial/tutorialExamples/tut_mag_grad.cxx new file mode 100644 index 000000000..dd8bfd188 --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_mag_grad.cxx @@ -0,0 +1,70 @@ +#include + +#include +#include + +#include +#include +#include + +#include + +#include + +struct ComputeMagnitude : vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn inputVectors, FieldOut outputMagnitudes); + + VTKM_EXEC void operator()(const vtkm::Vec3f& inVector, vtkm::FloatDefault& outMagnitude) const + { + outMagnitude = vtkm::Magnitude(inVector); + } +}; + +#include + +class FieldMagnitude : public vtkm::filter::FilterField +{ +public: + using SupportedTypes = vtkm::ListTagBase; + + template + VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& inDataSet, + const ArrayHandleType& inField, + const vtkm::filter::FieldMetadata& fieldMetadata, + vtkm::filter::PolicyBase) + { + vtkm::cont::ArrayHandle outField; + this->Invoke(ComputeMagnitude{}, inField, outField); + + std::string outFieldName = this->GetOutputFieldName(); + if (outFieldName == "") + { + outFieldName = fieldMetadata.GetName() + "_magnitude"; + } + + return vtkm::filter::CreateResult(inDataSet, outField, outFieldName, fieldMetadata); + } +}; + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); + vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + + vtkm::filter::Gradient grad; + grad.SetActiveField("c1"); + vtkm::cont::DataSet ds_from_grad = grad.Execute(ds_from_file); + + FieldMagnitude mag; + mag.SetActiveField("Gradients"); + vtkm::cont::DataSet mag_grad = mag.Execute(ds_from_grad); + + vtkm::io::VTKDataSetWriter writer("out_mag_grad.vtk"); + writer.WriteDataSet(mag_grad); + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_mag_grad_generalized.cxx b/vtkm-tutorial/tutorialExamples/tut_mag_grad_generalized.cxx new file mode 100644 index 000000000..9034770e7 --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_mag_grad_generalized.cxx @@ -0,0 +1,94 @@ +#include +#include + +#include +#include +#include +#include + +#include +#include + +#include + +#include + +namespace vtkm +{ +namespace worklet +{ + +struct Magnitude : public vtkm::worklet::WorkletMapField +{ + using ControlSignature = void(FieldIn inputVectors, FieldOut outputMagnitudes); + + template + VTKM_EXEC void operator()(const vtkm::Vec& inVector, T& outMagnitude) const + { + outMagnitude = vtkm::Magnitude(inVector); + } +}; + +} // namespace worklet +} // namespace vtkm + +#include + +namespace vtkm +{ +namespace filter +{ + +class FieldMagnitude : public vtkm::filter::FilterField +{ +public: + using SupportedTypes = vtkm::TypeListTagVecCommon; + + template + VTKM_CONT cont::DataSet DoExecute(const vtkm::cont::DataSet& inDataSet, + const ArrayHandleType& inField, + const vtkm::filter::FieldMetadata& fieldMetadata, + vtkm::filter::PolicyBase) + { + VTKM_IS_ARRAY_HANDLE(ArrayHandleType); + + using ValueType = typename ArrayHandleType::ValueType; + using ComponentType = decltype(ValueType{}[0]); //component type of vector + + vtkm::cont::ArrayHandle outField; + this->Invoke(vtkm::worklet::Magnitude{}, inField, outField); + + std::string outFieldName = this->GetOutputFieldName(); + if (outFieldName == "") + { + outFieldName = fieldMetadata.GetName() + "_magnitude"; + } + + return vtkm::filter::CreateResult(inDataSet, outField, outFieldName, fieldMetadata); + } +}; + +} // namespace filter +} // namespace vtkm + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); + vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + + vtkm::filter::Gradient grad; + grad.SetActiveField("c1"); + vtkm::cont::DataSet ds_from_grad = grad.Execute(ds_from_file); + + vtkm::filter::FieldMagnitude mag; + mag.SetActiveField("Gradients"); + vtkm::cont::DataSet mag_grad = mag.Execute(ds_from_grad); + + vtkm::io::VTKDataSetWriter writer("out_mag_grad.vtk"); + writer.WriteDataSet(mag_grad); + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_point_to_cell.cxx b/vtkm-tutorial/tutorialExamples/tut_point_to_cell.cxx new file mode 100644 index 000000000..be8958a36 --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_point_to_cell.cxx @@ -0,0 +1,101 @@ +#include + +#include +#include + +#include + +namespace vtkm +{ +namespace worklet +{ + +struct ConvertPointFieldToCells : vtkm::worklet::WorkletVisitCellsWithPoints +{ + using ControlSignature = void(CellSetIn topology, + FieldInPoint inPointField, + FieldOutCell outCellField); + using ExecutionSignature = void(_2 inPointField, _3 outCellField); + using InputDomain = _1; + + template + void operator()(const InPointFieldVecType& inPointFieldVec, OutCellFieldType& outCellField) const + { + vtkm::IdComponent numPoints = inPointFieldVec.GetNumberOfComponents(); + + outCellField = OutCellFieldType(0); + for (vtkm::IdComponent pointIndex = 0; pointIndex < numPoints; ++pointIndex) + { + outCellField = outCellField + inPointFieldVec[pointIndex]; + } + outCellField = outCellField / OutCellFieldType(numPoints); + } +}; + +} // namespace worklet +} // namespace vtkm + +#include + +namespace vtkm +{ +namespace filter +{ + +struct ConvertPointFieldToCells : vtkm::filter::FilterField +{ + template + VTKM_CONT vtkm::cont::DataSet DoExecute(const vtkm::cont::DataSet& inDataSet, + const ArrayHandleType& inField, + const vtkm::filter::FieldMetadata& fieldMetadata, + vtkm::filter::PolicyBase); +}; + +template +VTKM_CONT cont::DataSet ConvertPointFieldToCells::DoExecute( + const vtkm::cont::DataSet& inDataSet, + const ArrayHandleType& inField, + const vtkm::filter::FieldMetadata& fieldMetadata, + vtkm::filter::PolicyBase policy) +{ + VTKM_IS_ARRAY_HANDLE(ArrayHandleType); + + using ValueType = typename ArrayHandleType::ValueType; + + vtkm::cont::ArrayHandle outField; + this->Invoke(vtkm::worklet::ConvertPointFieldToCells{}, + vtkm::filter::ApplyPolicyCellSet(inDataSet.GetCellSet(), policy, *this), + inField, + outField); + + std::string outFieldName = this->GetOutputFieldName(); + if (outFieldName == "") + { + outFieldName = fieldMetadata.GetName(); + } + + return vtkm::filter::CreateResultFieldCell(inDataSet, outField, outFieldName); +} + +} // namespace filter +} // namespace vtkm + + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + const char* input = "data/kitchen.vtk"; + vtkm::io::VTKDataSetReader reader(input); + vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + + vtkm::filter::ConvertPointFieldToCells pointToCell; + pointToCell.SetActiveField("c1"); + vtkm::cont::DataSet ds_from_convert = pointToCell.Execute(ds_from_file); + + vtkm::io::VTKDataSetWriter writer("out_point_to_cell.vtk"); + writer.WriteDataSet(ds_from_convert); + + return 0; +} diff --git a/vtkm-tutorial/tutorialExamples/tut_rendering.cxx b/vtkm-tutorial/tutorialExamples/tut_rendering.cxx new file mode 100644 index 000000000..11217013e --- /dev/null +++ b/vtkm-tutorial/tutorialExamples/tut_rendering.cxx @@ -0,0 +1,49 @@ +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char** argv) +{ + auto opts = vtkm::cont::InitializeOptions::DefaultAnyDevice; + vtkm::cont::InitializeResult config = vtkm::cont::Initialize(argc, argv, opts); + + //Loading .vtk File + vtkm::io::VTKDataSetReader reader("data/kitchen.vtk"); + vtkm::cont::DataSet ds_from_file = reader.ReadDataSet(); + + //Creating Actor + vtkm::cont::ColorTable colorTable("viridis"); + vtkm::rendering::Actor actor(ds_from_file.GetCellSet(), + ds_from_file.GetCoordinateSystem(), + ds_from_file.GetField("c1"), + colorTable); + + //Creating Scene and adding Actor + vtkm::rendering::Scene scene; + scene.AddActor(actor); + + //Creating and initializing the View using the Canvas, Ray Tracer Mappers, and Scene + vtkm::rendering::MapperRayTracer mapper; + vtkm::rendering::CanvasRayTracer canvas(1920, 1080); + vtkm::rendering::View3D view(scene, mapper, canvas); + + //Setting the background and foreground colors; optional. + view.SetBackgroundColor(vtkm::rendering::Color(1.0f, 1.0f, 1.0f)); + view.SetForegroundColor(vtkm::rendering::Color(0.0f, 0.0f, 0.0f)); + + //Painting View + view.Paint(); + + //Saving View + view.SaveAs("BasicRendering.ppm"); + + return 0; +}