cleanup Camera

This commit is contained in:
Li-Ta Lo 2023-05-17 12:38:43 -06:00
parent fdf7aaece7
commit 9ed11540f1
5 changed files with 101 additions and 170 deletions

@ -96,6 +96,7 @@ public:
{
TwoD,
ThreeD,
Three3DPara,
};
VTKM_CONT

@ -98,13 +98,11 @@ void MapperVolume::RenderCells(const vtkm::cont::UnknownCellSet& cellset,
vtkm::rendering::raytracing::VolumeRendererStructured tracer;
vtkm::rendering::raytracing::Camera rayCamera;
vtkm::rendering::raytracing::Ray<vtkm::Float32> rays;
vtkm::Int32 width = (vtkm::Int32)this->Internals->Canvas->GetWidth();
vtkm::Int32 height = (vtkm::Int32)this->Internals->Canvas->GetHeight();
rayCamera.SetParameters(camera, width, height);
vtkm::rendering::raytracing::Ray<vtkm::Float32> rays;
rayCamera.CreateRays(rays, coords.GetBounds());
rays.Buffers.at(0).InitConst(0.f);
raytracing::RayOperations::MapCanvasToRays(rays, camera, *this->Internals->Canvas);

@ -20,13 +20,10 @@
#include <vtkm/rendering/raytracing/RayOperations.h>
#include <vtkm/rendering/raytracing/RayTracingTypeDefs.h>
#include <vtkm/rendering/raytracing/Sampler.h>
#include <vtkm/rendering/raytracing/Worklets.h>
#include <vtkm/worklet/DispatcherMapField.h>
#include <vtkm/worklet/WorkletMapField.h>
#include <limits>
namespace vtkm
{
namespace rendering
@ -229,7 +226,7 @@ public:
}; // class perspective ray gen jitter
class Camera::Ortho2DRayGen : public vtkm::worklet::WorkletMapField
class Ortho2DRayGen : public vtkm::worklet::WorkletMapField
{
public:
vtkm::Int32 w;
@ -320,7 +317,7 @@ public:
}; // class perspective ray gen
class Camera::PerspectiveRayGen : public vtkm::worklet::WorkletMapField
class PerspectiveRayGen : public vtkm::worklet::WorkletMapField
{
public:
vtkm::Int32 w;
@ -408,7 +405,6 @@ public:
bool Camera::operator==(const Camera& other) const
{
if (this->Height != other.Height)
return false;
if (this->Width != other.Width)
@ -427,68 +423,21 @@ bool Camera::operator==(const Camera& other) const
return false;
if (this->Zoom != other.Zoom)
return false;
if (this->Look[0] != other.Look[0])
if (this->Look != other.Look)
return false;
if (this->Look[1] != other.Look[1])
if (this->LookAt != other.LookAt)
return false;
if (this->Look[2] != other.Look[2])
if (this->Up != other.Up)
return false;
if (this->LookAt[0] != other.LookAt[0])
return false;
if (this->LookAt[1] != other.LookAt[1])
return false;
if (this->LookAt[2] != other.LookAt[2])
return false;
if (this->Up[0] != other.Up[0])
return false;
if (this->Up[1] != other.Up[1])
return false;
if (this->Up[2] != other.Up[2])
return false;
if (this->Position[0] != other.Position[0])
return false;
if (this->Position[1] != other.Position[1])
return false;
if (this->Position[2] != other.Position[2])
if (this->Position != other.Position)
return false;
return true;
}
VTKM_CONT
Camera::Camera()
{
this->Height = 500;
this->Width = 500;
this->SubsetWidth = 500;
this->SubsetHeight = 500;
this->SubsetMinX = 0;
this->SubsetMinY = 0;
this->FovY = 30.f;
this->FovX = 30.f;
this->Zoom = 1.f;
this->Look[0] = 0.f;
this->Look[1] = 0.f;
this->Look[2] = -1.f;
this->LookAt[0] = 0.f;
this->LookAt[1] = 0.f;
this->LookAt[2] = -1.f;
this->Up[0] = 0.f;
this->Up[1] = 1.f;
this->Up[2] = 0.f;
this->Position[0] = 0.f;
this->Position[1] = 0.f;
this->Position[2] = 0.f;
this->IsViewDirty = true;
}
VTKM_CONT
Camera::~Camera() {}
VTKM_CONT
void Camera::SetParameters(const vtkm::rendering::Camera& camera,
const vtkm::Int32 width,
const vtkm::Int32 height)
vtkm::Int32 width,
vtkm::Int32 height)
{
this->SetUp(camera.GetViewUp());
this->SetLookAt(camera.GetLookAt());
@ -500,7 +449,6 @@ void Camera::SetParameters(const vtkm::rendering::Camera& camera,
this->CameraView = camera;
}
VTKM_CONT
void Camera::SetHeight(const vtkm::Int32& height)
{
@ -726,19 +674,19 @@ void Camera::GetPixelData(const vtkm::cont::CoordinateSystem& coords,
}
VTKM_CONT
void Camera::CreateRays(Ray<vtkm::Float32>& rays, vtkm::Bounds bounds)
void Camera::CreateRays(Ray<vtkm::Float32>& rays, const vtkm::Bounds& bounds)
{
CreateRaysImpl(rays, bounds);
}
VTKM_CONT
void Camera::CreateRays(Ray<vtkm::Float64>& rays, vtkm::Bounds bounds)
void Camera::CreateRays(Ray<vtkm::Float64>& rays, const vtkm::Bounds& bounds)
{
CreateRaysImpl(rays, bounds);
}
template <typename Precision>
VTKM_CONT void Camera::CreateRaysImpl(Ray<Precision>& rays, const vtkm::Bounds boundingBox)
VTKM_CONT void Camera::CreateRaysImpl(Ray<Precision>& rays, const vtkm::Bounds& boundingBox)
{
Logger* logger = Logger::GetInstance();
vtkm::cont::Timer createTimer;
@ -748,9 +696,9 @@ VTKM_CONT void Camera::CreateRaysImpl(Ray<Precision>& rays, const vtkm::Bounds b
bool ortho = this->CameraView.GetMode() == vtkm::rendering::Camera::Mode::TwoD;
this->UpdateDimensions(rays, boundingBox, ortho);
this->WriteSettingsToLog();
vtkm::cont::Timer timer;
timer.Start();
//Set the origin of the ray back to the camera position
Precision infinity;
GetInfinity(infinity);
@ -772,29 +720,29 @@ VTKM_CONT void Camera::CreateRaysImpl(Ray<Precision>& rays, const vtkm::Bounds b
//Reset the camera look vector
this->Look = this->LookAt - this->Position;
vtkm::Normalize(this->Look);
vtkm::cont::Invoker invoke;
if (ortho)
{
vtkm::worklet::DispatcherMapField<Ortho2DRayGen> dispatcher(Ortho2DRayGen(this->Width,
invoke(Ortho2DRayGen{ this->Width,
this->Height,
this->Zoom,
this->SubsetWidth,
this->SubsetMinX,
this->SubsetMinY,
this->CameraView));
dispatcher.Invoke(rays.DirX,
this->CameraView },
rays.DirX,
rays.DirY,
rays.DirZ,
rays.OriginX,
rays.OriginY,
rays.OriginZ,
rays.PixelIdx); //X Y Z
rays.PixelIdx);
}
else
{
//Create the ray direction
vtkm::worklet::DispatcherMapField<PerspectiveRayGen> dispatcher(
PerspectiveRayGen(this->Width,
invoke(PerspectiveRayGen{ this->Width,
this->Height,
this->FovX,
this->FovY,
@ -803,9 +751,13 @@ VTKM_CONT void Camera::CreateRaysImpl(Ray<Precision>& rays, const vtkm::Bounds b
this->Zoom,
this->SubsetWidth,
this->SubsetMinX,
this->SubsetMinY));
dispatcher.Invoke(rays.DirX, rays.DirY, rays.DirZ, rays.PixelIdx); //X Y Z
this->SubsetMinY },
rays.DirX,
rays.DirY,
rays.DirZ,
rays.PixelIdx);
//Set the origin of the ray back to the camera position
vtkm::cont::ArrayHandleConstant<Precision> posX(this->Position[0], rays.NumRays);
vtkm::cont::Algorithm::Copy(posX, rays.OriginX);
@ -934,11 +886,10 @@ VTKM_CONT void Camera::UpdateDimensions(Ray<Precision>& rays,
if (imageSubsetModeOn && !ortho2D)
{
//Create a transform matrix using the rendering::camera class
vtkm::rendering::Camera camera = this->CameraView;
camera.SetFieldOfView(this->GetFieldOfView());
camera.SetLookAt(this->GetLookAt());
camera.SetPosition(this->GetPosition());
camera.SetViewUp(this->GetUp());
this->CameraView.SetFieldOfView(this->GetFieldOfView());
this->CameraView.SetLookAt(this->GetLookAt());
this->CameraView.SetPosition(this->GetPosition());
this->CameraView.SetViewUp(this->GetUp());
//
// Just create come clipping range, we ignore the zmax value in subsetting
//
@ -947,7 +898,8 @@ VTKM_CONT void Camera::UpdateDimensions(Ray<Precision>& rays,
vtkm::Max(boundingBox.Y.Max - boundingBox.Y.Min, boundingBox.Z.Max - boundingBox.Z.Min));
maxDim *= 100;
camera.SetClippingRange(.0001, maxDim);
this->CameraView.SetClippingRange(.0001, maxDim);
//Update our ViewProjection matrix
this->ViewProjectionMat =
vtkm::MatrixMultiply(this->CameraView.CreateProjectionMatrix(this->Width, this->Height),
@ -983,8 +935,7 @@ VTKM_CONT void Camera::UpdateDimensions(Ray<Precision>& rays,
// resize rays and buffers
if (rays.NumRays != SubsetWidth * SubsetHeight)
{
RayOperations::Resize(
rays, this->SubsetHeight * this->SubsetWidth, vtkm::cont::DeviceAdapterTagSerial());
RayOperations::Resize(rays, this->SubsetHeight * this->SubsetWidth);
}
}
@ -1001,7 +952,7 @@ void Camera::CreateDebugRay(vtkm::Vec2i_32 pixel, Ray<vtkm::Float32>& rays)
template <typename Precision>
void Camera::CreateDebugRayImp(vtkm::Vec2i_32 pixel, Ray<Precision>& rays)
{
RayOperations::Resize(rays, 1, vtkm::cont::DeviceAdapterTagSerial());
RayOperations::Resize(rays, 1);
vtkm::Int32 pixelIndex = this->Width * (this->Height - pixel[1]) + pixel[0];
rays.PixelIdx.WritePortal().Set(0, pixelIndex);
rays.OriginX.WritePortal().Set(0, this->Position[0]);

@ -12,7 +12,6 @@
#include <vtkm/cont/CoordinateSystem.h>
#include <vtkm/rendering/Camera.h>
#include <vtkm/rendering/CanvasRayTracer.h>
#include <vtkm/rendering/raytracing/Ray.h>
namespace vtkm
@ -24,55 +23,35 @@ namespace raytracing
class VTKM_RENDERING_EXPORT Camera
{
private:
struct PixelDataFunctor;
vtkm::Int32 Height;
vtkm::Int32 Width;
vtkm::Int32 SubsetWidth;
vtkm::Int32 SubsetHeight;
vtkm::Int32 SubsetMinX;
vtkm::Int32 SubsetMinY;
vtkm::Float32 FovX;
vtkm::Float32 FovY;
vtkm::Float32 Zoom;
bool IsViewDirty;
vtkm::Int32 Height = 500;
vtkm::Int32 Width = 500;
vtkm::Int32 SubsetWidth = 500;
vtkm::Int32 SubsetHeight = 500;
vtkm::Int32 SubsetMinX = 0;
vtkm::Int32 SubsetMinY = 0;
vtkm::Float32 FovX = 30.f;
vtkm::Float32 FovY = 30.f;
vtkm::Float32 Zoom = 1.f;
bool IsViewDirty = true;
vtkm::Vec3f_32 Look;
vtkm::Vec3f_32 Up;
vtkm::Vec3f_32 LookAt;
vtkm::Vec3f_32 Position;
vtkm::Vec3f_32 Look{ 0.f, 0.f, -1.f };
vtkm::Vec3f_32 Up{ 0.f, 1.f, 0.f };
vtkm::Vec3f_32 LookAt{ 0.f, 0.f, -1.f };
vtkm::Vec3f_32 Position{ 0.f, 0.f, 0.f };
vtkm::rendering::Camera CameraView;
vtkm::Matrix<vtkm::Float32, 4, 4> ViewProjectionMat;
public:
VTKM_CONT
Camera();
VTKM_CONT
~Camera();
// cuda does not compile if this is private
class PerspectiveRayGen;
class Ortho2DRayGen;
std::string ToString();
VTKM_CONT
void SetParameters(const vtkm::rendering::Camera& camera,
const vtkm::Int32 width,
const vtkm::Int32 height);
VTKM_CONT
void SetParameters(const vtkm::rendering::Camera& camera,
vtkm::rendering::CanvasRayTracer& canvas);
void SetParameters(const vtkm::rendering::Camera& camera, vtkm::Int32 width, vtkm::Int32 height);
VTKM_CONT
void SetHeight(const vtkm::Int32& height);
VTKM_CONT
void WriteSettingsToLog();
VTKM_CONT
vtkm::Int32 GetHeight() const;
@ -125,10 +104,10 @@ public:
bool GetIsViewDirty() const;
VTKM_CONT
void CreateRays(Ray<vtkm::Float32>& rays, vtkm::Bounds bounds);
void CreateRays(Ray<vtkm::Float32>& rays, const vtkm::Bounds& bounds);
VTKM_CONT
void CreateRays(Ray<vtkm::Float64>& rays, vtkm::Bounds bounds);
void CreateRays(Ray<vtkm::Float64>& rays, const vtkm::Bounds& bounds);
VTKM_CONT
void GetPixelData(const vtkm::cont::CoordinateSystem& coords,
@ -136,7 +115,7 @@ public:
vtkm::Float32& aveRayDistance);
template <typename Precision>
VTKM_CONT void CreateRaysImpl(Ray<Precision>& rays, const vtkm::Bounds boundingBox);
VTKM_CONT void CreateRaysImpl(Ray<Precision>& rays, const vtkm::Bounds& boundingBox);
void CreateDebugRay(vtkm::Vec2i_32 pixel, Ray<vtkm::Float32>& rays);
@ -146,10 +125,14 @@ public:
private:
template <typename Precision>
void CreateDebugRayImp(vtkm::Vec2i_32 pixel, Ray<Precision>& rays);
VTKM_CONT void CreateDebugRayImp(vtkm::Vec2i_32 pixel, Ray<Precision>& rays);
VTKM_CONT
void FindSubset(const vtkm::Bounds& bounds);
VTKM_CONT
void WriteSettingsToLog();
template <typename Precision>
VTKM_CONT void UpdateDimensions(Ray<Precision>& rays,
const vtkm::Bounds& boundingBox,

@ -285,48 +285,46 @@ public:
return masks;
}
template <typename Device, typename T>
static void Resize(Ray<T>& rays, const vtkm::Int32 newSize, Device)
template <typename T>
static void Resize(Ray<T>& rays, const vtkm::Int32 newSize)
{
if (newSize == rays.NumRays)
return; //nothing to do
rays.NumRays = newSize;
vtkm::cont::Token token;
if (rays.IntersectionDataEnabled)
{
rays.IntersectionX.PrepareForOutput(rays.NumRays, Device(), token);
rays.IntersectionY.PrepareForOutput(rays.NumRays, Device(), token);
rays.IntersectionZ.PrepareForOutput(rays.NumRays, Device(), token);
rays.U.PrepareForOutput(rays.NumRays, Device(), token);
rays.V.PrepareForOutput(rays.NumRays, Device(), token);
rays.Scalar.PrepareForOutput(rays.NumRays, Device(), token);
rays.IntersectionX.Allocate(rays.NumRays);
rays.IntersectionY.Allocate(rays.NumRays);
rays.IntersectionZ.Allocate(rays.NumRays);
rays.U.Allocate(rays.NumRays);
rays.V.Allocate(rays.NumRays);
rays.Scalar.Allocate(rays.NumRays);
rays.NormalX.PrepareForOutput(rays.NumRays, Device(), token);
rays.NormalY.PrepareForOutput(rays.NumRays, Device(), token);
rays.NormalZ.PrepareForOutput(rays.NumRays, Device(), token);
rays.NormalX.Allocate(rays.NumRays);
rays.NormalY.Allocate(rays.NumRays);
rays.NormalZ.Allocate(rays.NumRays);
}
rays.OriginX.PrepareForOutput(rays.NumRays, Device(), token);
rays.OriginY.PrepareForOutput(rays.NumRays, Device(), token);
rays.OriginZ.PrepareForOutput(rays.NumRays, Device(), token);
rays.OriginX.Allocate(rays.NumRays);
rays.OriginY.Allocate(rays.NumRays);
rays.OriginZ.Allocate(rays.NumRays);
rays.DirX.PrepareForOutput(rays.NumRays, Device(), token);
rays.DirY.PrepareForOutput(rays.NumRays, Device(), token);
rays.DirZ.PrepareForOutput(rays.NumRays, Device(), token);
rays.DirX.Allocate(rays.NumRays);
rays.DirY.Allocate(rays.NumRays);
rays.DirZ.Allocate(rays.NumRays);
rays.Distance.PrepareForOutput(rays.NumRays, Device(), token);
rays.MinDistance.PrepareForOutput(rays.NumRays, Device(), token);
rays.MaxDistance.PrepareForOutput(rays.NumRays, Device(), token);
rays.Status.PrepareForOutput(rays.NumRays, Device(), token);
rays.HitIdx.PrepareForOutput(rays.NumRays, Device(), token);
rays.PixelIdx.PrepareForOutput(rays.NumRays, Device(), token);
rays.Distance.Allocate(rays.NumRays);
rays.MinDistance.Allocate(rays.NumRays);
rays.MaxDistance.Allocate(rays.NumRays);
rays.Status.Allocate(rays.NumRays);
rays.HitIdx.Allocate(rays.NumRays);
rays.PixelIdx.Allocate(rays.NumRays);
const size_t bufferCount = static_cast<size_t>(rays.Buffers.size());
for (size_t i = 0; i < bufferCount; ++i)
for (auto& buffer : rays.Buffers)
{
rays.Buffers[i].Resize(rays.NumRays, Device());
buffer.Resize(rays.NumRays);
}
}