vtk-m2/vtkm/rendering/Mapper.cxx

146 lines
4.8 KiB
C++
Raw Normal View History

//============================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
2019-04-15 23:24:21 +00:00
//
// 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.
//============================================================================
#include <vtkm/cont/BoundsCompute.h>
#include <vtkm/rendering/Mapper.h>
2017-05-18 14:29:41 +00:00
namespace vtkm
{
namespace rendering
{
Mapper::~Mapper() {}
void Mapper::RenderCells(const vtkm::cont::UnknownCellSet& cellset,
const vtkm::cont::CoordinateSystem& coords,
const vtkm::cont::Field& scalarField,
const vtkm::cont::ColorTable& colorTable,
const vtkm::rendering::Camera& camera,
const vtkm::Range& scalarRange)
{
RenderCellsImpl(
cellset,
coords,
scalarField,
colorTable,
camera,
scalarRange,
make_FieldCell(vtkm::cont::GetGlobalGhostCellFieldName(),
vtkm::cont::ArrayHandleConstant<vtkm::UInt8>(0, cellset.GetNumberOfCells())));
2023-11-08 00:20:05 +00:00
};
void Mapper::RenderCells(const vtkm::cont::UnknownCellSet& cellset,
const vtkm::cont::CoordinateSystem& coords,
const vtkm::cont::Field& scalarField,
const vtkm::cont::ColorTable& colorTable,
const vtkm::rendering::Camera& camera,
const vtkm::Range& scalarRange,
const vtkm::cont::Field& ghostField)
{
RenderCellsImpl(cellset, coords, scalarField, colorTable, camera, scalarRange, ghostField);
};
struct CompareIndices
{
vtkm::Vec3f CameraDirection;
2023-11-08 00:20:05 +00:00
std::vector<vtkm::Vec3f>& Centers;
2023-11-08 15:44:52 +00:00
bool SortBackToFront;
CompareIndices(std::vector<vtkm::Vec3f>& centers,
vtkm::Vec3f cameraDirection,
bool sortBackToFront)
: CameraDirection(cameraDirection)
, Centers(centers)
2023-11-08 15:44:52 +00:00
, SortBackToFront(sortBackToFront)
{
}
2024-01-02 21:21:30 +00:00
bool operator()(vtkm::Id i, vtkm::Id j) const
{
2023-11-08 15:44:52 +00:00
if (SortBackToFront)
{
return (vtkm::Dot(Centers[i], CameraDirection) > vtkm::Dot(Centers[j], CameraDirection));
}
else
{
return (vtkm::Dot(Centers[i], CameraDirection) < vtkm::Dot(Centers[j], CameraDirection));
}
}
};
void Mapper::RenderCellsPartitioned(const vtkm::cont::PartitionedDataSet partitionedData,
const std::string fieldName,
const vtkm::cont::ColorTable& colorTable,
const vtkm::rendering::Camera& camera,
const vtkm::Range& scalarRange)
{
// sort partitions back to front for best rendering with the volume renderer
2023-11-08 00:20:05 +00:00
std::vector<vtkm::Vec3f> centers(partitionedData.GetNumberOfPartitions()); // vector for centers
2024-01-02 21:21:30 +00:00
std::vector<vtkm::Id> indices(partitionedData.GetNumberOfPartitions());
2023-11-08 00:20:05 +00:00
for (vtkm::Id p = 0; p < partitionedData.GetNumberOfPartitions(); p++)
{
2023-11-08 00:20:05 +00:00
indices[static_cast<size_t>(p)] = p;
centers[static_cast<size_t>(p)] =
vtkm::cont::BoundsCompute(partitionedData.GetPartition(p)).Center();
}
2023-11-08 15:44:52 +00:00
CompareIndices comparator(
centers, camera.GetLookAt() - camera.GetPosition(), this->SortBackToFront);
std::sort(indices.begin(), indices.end(), comparator);
2023-11-08 00:20:05 +00:00
for (vtkm::Id p = 0; p < partitionedData.GetNumberOfPartitions(); p++)
{
2023-11-08 00:20:05 +00:00
auto partition = partitionedData.GetPartition(indices[static_cast<size_t>(p)]);
this->RenderCells(partition.GetCellSet(),
partition.GetCoordinateSystem(),
2023-11-08 00:20:05 +00:00
partition.GetField(fieldName),
colorTable,
camera,
scalarRange,
partition.GetGhostCellField());
}
}
void Mapper::SetActiveColorTable(const vtkm::cont::ColorTable& colorTable)
{
constexpr vtkm::Float32 conversionToFloatSpace = (1.0f / 255.0f);
vtkm::cont::ArrayHandle<vtkm::Vec4ui_8> temp;
{
vtkm::cont::ScopedRuntimeDeviceTracker tracker(vtkm::cont::DeviceAdapterTagSerial{});
colorTable.Sample(1024, temp);
}
this->ColorMap.Allocate(1024);
auto portal = this->ColorMap.WritePortal();
auto colorPortal = temp.ReadPortal();
for (vtkm::Id i = 0; i < 1024; ++i)
{
auto color = colorPortal.Get(i);
vtkm::Vec4f_32 t(color[0] * conversionToFloatSpace,
color[1] * conversionToFloatSpace,
color[2] * conversionToFloatSpace,
color[3] * conversionToFloatSpace);
portal.Set(i, t);
}
}
void Mapper::SetLogarithmX(bool l)
{
this->LogarithmX = l;
}
void Mapper::SetLogarithmY(bool l)
{
this->LogarithmY = l;
}
}
}