2016-01-19 14:59:31 +00:00
|
|
|
//============================================================================
|
|
|
|
// 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.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
2016-01-19 14:59:31 +00:00
|
|
|
// Copyright 2014 UT-Battelle, LLC.
|
|
|
|
// Copyright 2014 Los Alamos National Security.
|
|
|
|
//
|
2017-09-20 21:33:44 +00:00
|
|
|
// Under the terms of Contract DE-NA0003525 with NTESS,
|
2016-01-19 14:59:31 +00:00
|
|
|
// 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/cont/ArrayHandleIndex.h>
|
|
|
|
#include <vtkm/cont/CellSetSingleType.h>
|
2017-06-19 17:44:36 +00:00
|
|
|
#include <vtkm/cont/CellSetStructured.h>
|
2017-06-06 17:01:45 +00:00
|
|
|
#include <vtkm/cont/DynamicArrayHandle.h>
|
2017-05-18 14:51:24 +00:00
|
|
|
#include <vtkm/cont/DynamicCellSet.h>
|
2018-04-02 21:58:21 +00:00
|
|
|
#include <vtkm/cont/ErrorFilterExecution.h>
|
2016-01-19 14:59:31 +00:00
|
|
|
|
|
|
|
#include <vtkm/worklet/DispatcherMapTopology.h>
|
|
|
|
#include <vtkm/worklet/ScatterCounting.h>
|
2017-06-19 17:44:36 +00:00
|
|
|
#include <vtkm/worklet/SurfaceNormals.h>
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
namespace vtkm
|
|
|
|
{
|
|
|
|
namespace filter
|
|
|
|
{
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2017-06-19 17:44:36 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
template <typename CellSetList>
|
|
|
|
bool IsCellSetStructured(const vtkm::cont::DynamicCellSetBase<CellSetList>& cellset)
|
|
|
|
{
|
|
|
|
if (cellset.template IsType<vtkm::cont::CellSetStructured<1>>() ||
|
|
|
|
cellset.template IsType<vtkm::cont::CellSetStructured<2>>() ||
|
|
|
|
cellset.template IsType<vtkm::cont::CellSetStructured<3>>())
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-01-19 14:59:31 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-05-18 14:29:41 +00:00
|
|
|
inline VTKM_CONT MarchingCubes::MarchingCubes()
|
|
|
|
: vtkm::filter::FilterDataSetWithField<MarchingCubes>()
|
|
|
|
, IsoValues()
|
|
|
|
, GenerateNormals(false)
|
2017-06-19 17:44:36 +00:00
|
|
|
, ComputeFastNormalsForStructured(false)
|
|
|
|
, ComputeFastNormalsForUnstructured(true)
|
2017-05-18 14:29:41 +00:00
|
|
|
, NormalArrayName("normals")
|
|
|
|
, Worklet()
|
2016-01-19 14:59:31 +00:00
|
|
|
{
|
2016-07-21 20:54:33 +00:00
|
|
|
// todo: keep an instance of marching cubes worklet as a member variable
|
2016-01-19 14:59:31 +00:00
|
|
|
}
|
|
|
|
|
2017-03-14 12:56:44 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-05-18 14:29:41 +00:00
|
|
|
inline void MarchingCubes::SetNumberOfIsoValues(vtkm::Id num)
|
2017-03-14 12:56:44 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
if (num >= 0)
|
2017-03-14 12:56:44 +00:00
|
|
|
{
|
|
|
|
this->IsoValues.resize(static_cast<std::size_t>(num));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-05-18 14:29:41 +00:00
|
|
|
inline vtkm::Id MarchingCubes::GetNumberOfIsoValues() const
|
2017-03-14 12:56:44 +00:00
|
|
|
{
|
|
|
|
return static_cast<vtkm::Id>(this->IsoValues.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-05-18 14:29:41 +00:00
|
|
|
inline void MarchingCubes::SetIsoValue(vtkm::Id index, vtkm::Float64 v)
|
2017-03-14 12:56:44 +00:00
|
|
|
{
|
|
|
|
std::size_t i = static_cast<std::size_t>(index);
|
2017-05-18 14:29:41 +00:00
|
|
|
if (i >= this->IsoValues.size())
|
2017-03-14 12:56:44 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
this->IsoValues.resize(i + 1);
|
2017-03-14 12:56:44 +00:00
|
|
|
}
|
|
|
|
this->IsoValues[i] = v;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-05-18 14:29:41 +00:00
|
|
|
inline void MarchingCubes::SetIsoValues(const std::vector<vtkm::Float64>& values)
|
2017-03-14 12:56:44 +00:00
|
|
|
{
|
|
|
|
this->IsoValues = values;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-05-18 14:29:41 +00:00
|
|
|
inline vtkm::Float64 MarchingCubes::GetIsoValue(vtkm::Id index) const
|
2017-03-14 12:56:44 +00:00
|
|
|
{
|
|
|
|
return this->IsoValues[static_cast<std::size_t>(index)];
|
|
|
|
}
|
|
|
|
|
2016-01-19 14:59:31 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
|
2018-04-02 21:58:21 +00:00
|
|
|
inline VTKM_CONT vtkm::cont::DataSet MarchingCubes::DoExecute(
|
2017-05-26 17:53:28 +00:00
|
|
|
const vtkm::cont::DataSet& input,
|
|
|
|
const vtkm::cont::ArrayHandle<T, StorageType>& field,
|
2017-05-18 14:29:41 +00:00
|
|
|
const vtkm::filter::FieldMetadata& fieldMeta,
|
2017-05-26 17:53:28 +00:00
|
|
|
const vtkm::filter::PolicyBase<DerivedPolicy>& policy,
|
|
|
|
const DeviceAdapter& device)
|
2016-01-19 14:59:31 +00:00
|
|
|
{
|
2017-05-18 14:29:41 +00:00
|
|
|
if (fieldMeta.IsPointField() == false)
|
2016-01-19 14:59:31 +00:00
|
|
|
{
|
2018-04-02 21:58:21 +00:00
|
|
|
throw vtkm::cont::ErrorFilterExecution("Point field expected.");
|
2016-01-19 14:59:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
if (this->IsoValues.size() == 0)
|
2017-03-14 12:56:44 +00:00
|
|
|
{
|
2018-04-02 21:58:21 +00:00
|
|
|
throw vtkm::cont::ErrorFilterExecution("No iso-values provided.");
|
2017-03-14 12:56:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 21:02:55 +00:00
|
|
|
// Check the fields of the dataset to see what kinds of fields are present so
|
|
|
|
// we can free the mapping arrays that won't be needed. A point field must
|
|
|
|
// exist for this algorithm, so just check cells.
|
|
|
|
const vtkm::Id numFields = input.GetNumberOfFields();
|
|
|
|
bool hasCellFields = false;
|
|
|
|
for (vtkm::Id fieldIdx = 0; fieldIdx < numFields && !hasCellFields; ++fieldIdx)
|
|
|
|
{
|
|
|
|
auto f = input.GetField(fieldIdx);
|
|
|
|
if (f.GetAssociation() == vtkm::cont::Field::ASSOC_CELL_SET)
|
|
|
|
{
|
|
|
|
hasCellFields = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-19 14:59:31 +00:00
|
|
|
//get the cells and coordinates of the dataset
|
2017-05-18 14:29:41 +00:00
|
|
|
const vtkm::cont::DynamicCellSet& cells = input.GetCellSet(this->GetActiveCellSetIndex());
|
2016-01-19 14:59:31 +00:00
|
|
|
|
|
|
|
const vtkm::cont::CoordinateSystem& coords =
|
2017-05-18 14:29:41 +00:00
|
|
|
input.GetCoordinateSystem(this->GetActiveCoordinateSystemIndex());
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2018-02-22 13:29:13 +00:00
|
|
|
using Vec3HandleType = vtkm::cont::ArrayHandle<vtkm::Vec<vtkm::FloatDefault, 3>>;
|
2016-07-21 20:54:33 +00:00
|
|
|
Vec3HandleType vertices;
|
2016-01-19 14:59:31 +00:00
|
|
|
Vec3HandleType normals;
|
|
|
|
|
|
|
|
vtkm::cont::DataSet output;
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::cont::CellSetSingleType<> outputCells;
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2017-03-14 12:56:44 +00:00
|
|
|
std::vector<T> ivalues(this->IsoValues.size());
|
2017-05-18 14:29:41 +00:00
|
|
|
for (std::size_t i = 0; i < ivalues.size(); ++i)
|
2017-03-14 12:56:44 +00:00
|
|
|
{
|
|
|
|
ivalues[i] = static_cast<T>(this->IsoValues[i]);
|
|
|
|
}
|
|
|
|
|
2016-07-21 20:54:33 +00:00
|
|
|
//not sold on this as we have to generate more signatures for the
|
|
|
|
//worklet with the design
|
|
|
|
//But I think we should get this to compile before we tinker with
|
|
|
|
//a more efficient api
|
2017-06-19 17:44:36 +00:00
|
|
|
|
|
|
|
bool generateHighQualityNormals = IsCellSetStructured(cells)
|
|
|
|
? !this->ComputeFastNormalsForStructured
|
|
|
|
: !this->ComputeFastNormalsForUnstructured;
|
|
|
|
if (this->GenerateNormals && generateHighQualityNormals)
|
2016-01-19 14:59:31 +00:00
|
|
|
{
|
2017-05-26 17:53:28 +00:00
|
|
|
outputCells = this->Worklet.Run(&ivalues[0],
|
|
|
|
static_cast<vtkm::Id>(ivalues.size()),
|
|
|
|
vtkm::filter::ApplyPolicy(cells, policy),
|
2017-12-12 19:26:23 +00:00
|
|
|
coords.GetData(),
|
2017-05-26 17:53:28 +00:00
|
|
|
field,
|
|
|
|
vertices,
|
|
|
|
normals,
|
|
|
|
device);
|
2016-01-19 14:59:31 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2017-05-26 17:53:28 +00:00
|
|
|
outputCells = this->Worklet.Run(&ivalues[0],
|
|
|
|
static_cast<vtkm::Id>(ivalues.size()),
|
|
|
|
vtkm::filter::ApplyPolicy(cells, policy),
|
2017-12-12 19:26:23 +00:00
|
|
|
coords.GetData(),
|
2017-05-26 17:53:28 +00:00
|
|
|
field,
|
|
|
|
vertices,
|
|
|
|
device);
|
2016-01-19 14:59:31 +00:00
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
if (this->GenerateNormals)
|
2016-01-19 14:59:31 +00:00
|
|
|
{
|
2017-06-19 17:44:36 +00:00
|
|
|
if (!generateHighQualityNormals)
|
|
|
|
{
|
|
|
|
Vec3HandleType faceNormals;
|
|
|
|
vtkm::worklet::FacetedSurfaceNormals faceted;
|
|
|
|
faceted.Run(outputCells, vertices, faceNormals, device);
|
|
|
|
|
|
|
|
vtkm::worklet::SmoothSurfaceNormals smooth;
|
|
|
|
smooth.Run(outputCells, faceNormals, normals, device);
|
|
|
|
}
|
|
|
|
|
2017-05-18 14:29:41 +00:00
|
|
|
vtkm::cont::Field normalField(this->NormalArrayName, vtkm::cont::Field::ASSOC_POINTS, normals);
|
|
|
|
output.AddField(normalField);
|
2016-01-19 14:59:31 +00:00
|
|
|
}
|
|
|
|
|
2016-03-23 21:06:30 +00:00
|
|
|
//assign the connectivity to the cell set
|
2017-05-18 14:29:41 +00:00
|
|
|
output.AddCellSet(outputCells);
|
2016-03-23 21:06:30 +00:00
|
|
|
|
2016-01-19 14:59:31 +00:00
|
|
|
//add the coordinates to the output dataset
|
2016-03-23 14:38:14 +00:00
|
|
|
vtkm::cont::CoordinateSystem outputCoords("coordinates", vertices);
|
2017-05-18 14:29:41 +00:00
|
|
|
output.AddCoordinateSystem(outputCoords);
|
2016-01-19 14:59:31 +00:00
|
|
|
|
2017-06-08 21:02:55 +00:00
|
|
|
if (!hasCellFields)
|
|
|
|
{
|
|
|
|
this->Worklet.ReleaseCellMapArrays();
|
|
|
|
}
|
|
|
|
|
2018-04-02 21:58:21 +00:00
|
|
|
return output;
|
2016-01-19 14:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2017-05-18 14:29:41 +00:00
|
|
|
template <typename T, typename StorageType, typename DerivedPolicy, typename DeviceAdapter>
|
|
|
|
inline VTKM_CONT bool MarchingCubes::DoMapField(
|
2018-04-02 21:58:21 +00:00
|
|
|
vtkm::cont::DataSet& result,
|
2017-05-26 17:53:28 +00:00
|
|
|
const vtkm::cont::ArrayHandle<T, StorageType>& input,
|
|
|
|
const vtkm::filter::FieldMetadata& fieldMeta,
|
|
|
|
const vtkm::filter::PolicyBase<DerivedPolicy>&,
|
2017-05-18 14:29:41 +00:00
|
|
|
const DeviceAdapter& device)
|
2016-01-19 14:59:31 +00:00
|
|
|
{
|
2017-06-06 17:01:45 +00:00
|
|
|
vtkm::cont::ArrayHandle<T> fieldArray;
|
|
|
|
|
|
|
|
if (fieldMeta.IsPointField())
|
|
|
|
{
|
|
|
|
fieldArray = this->Worklet.ProcessPointField(input, device);
|
|
|
|
}
|
|
|
|
else if (fieldMeta.IsCellField())
|
|
|
|
{
|
|
|
|
fieldArray = this->Worklet.ProcessCellField(input, device);
|
|
|
|
}
|
|
|
|
else
|
2016-01-19 14:59:31 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
//use the same meta data as the input so we get the same field name, etc.
|
2018-04-02 21:58:21 +00:00
|
|
|
result.AddField(fieldMeta.AsField(fieldArray));
|
2016-01-19 14:59:31 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} // namespace vtkm::filter
|