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, TwoD,
ThreeD, ThreeD,
Three3DPara,
}; };
VTKM_CONT VTKM_CONT

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

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

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

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