From 67febc27ade0da0d6d806fc7695c280954586a00 Mon Sep 17 00:00:00 2001 From: Sujin Philip Date: Mon, 15 May 2017 16:08:51 -0400 Subject: [PATCH] Add ImplicitFunction for Cylinder and Frustum --- vtkm/cont/ImplicitFunction.h | 265 ++++++------- vtkm/cont/ImplicitFunction.hxx | 414 +++++++++++++++++++- vtkm/cont/testing/TestingImplicitFunction.h | 117 ++++-- 3 files changed, 617 insertions(+), 179 deletions(-) diff --git a/vtkm/cont/ImplicitFunction.h b/vtkm/cont/ImplicitFunction.h index ec910e34e..2d6be4030 100644 --- a/vtkm/cont/ImplicitFunction.h +++ b/vtkm/cont/ImplicitFunction.h @@ -125,63 +125,28 @@ protected: class VTKM_ALWAYS_EXPORT Box : public ImplicitFunctionImpl { public: - Box() : MinPoint(vtkm::Vec(FloatDefault(0), FloatDefault(0), FloatDefault(0))), - MaxPoint(vtkm::Vec(FloatDefault(1), FloatDefault(1), FloatDefault(1))) - { } - - Box(vtkm::Vec minPoint, vtkm::Vec maxPoint) - : MinPoint(minPoint), MaxPoint(maxPoint) - { } - + Box(); + Box(vtkm::Vec minPoint, vtkm::Vec maxPoint); Box(FloatDefault xmin, FloatDefault xmax, FloatDefault ymin, FloatDefault ymax, - FloatDefault zmin, FloatDefault zmax) - { - MinPoint[0] = xmin; MaxPoint[0] = xmax; - MinPoint[1] = ymin; MaxPoint[1] = ymax; - MinPoint[2] = zmin; MaxPoint[2] = zmax; - } + FloatDefault zmin, FloatDefault zmax); - void SetMinPoint(const vtkm::Vec &point) - { - this->MinPoint = point; - this->Modified(); - } + void SetMinPoint(const vtkm::Vec &point); + void SetMaxPoint(const vtkm::Vec &point); - void SetMaxPoint(const vtkm::Vec &point) - { - this->MaxPoint = point; - this->Modified(); - } - - const vtkm::Vec& GetMinPoint() const - { - return this->MinPoint; - } - - const vtkm::Vec& GetMaxPoint() const - { - return this->MaxPoint; - } + const vtkm::Vec& GetMinPoint() const; + const vtkm::Vec& GetMaxPoint() const; VTKM_EXEC_CONT FloatDefault Value(const vtkm::Vec &x) const; - VTKM_EXEC_CONT - FloatDefault Value(FloatDefault x, FloatDefault y, FloatDefault z) const - { - return this->Value(vtkm::Vec(x, y, z)); - } + FloatDefault Value(FloatDefault x, FloatDefault y, FloatDefault z) const; VTKM_EXEC_CONT vtkm::Vec Gradient(const vtkm::Vec &x) const; - VTKM_EXEC_CONT - vtkm::Vec Gradient(FloatDefault x, FloatDefault y, FloatDefault z) - const - { - return this->Gradient(vtkm::Vec(x, y, z)); - } + vtkm::Vec + Gradient(FloatDefault x, FloatDefault y, FloatDefault z) const; private: vtkm::Vec MinPoint; @@ -189,73 +154,109 @@ private: }; +//============================================================================ +/// \brief Implicit function for a cylinder +class VTKM_ALWAYS_EXPORT Cylinder : public ImplicitFunctionImpl +{ +public: + Cylinder(); + Cylinder(const vtkm::Vec &axis, FloatDefault radius); + Cylinder(const vtkm::Vec ¢er, + const vtkm::Vec &axis, + FloatDefault radius); + + void SetCenter(const vtkm::Vec ¢er); + void SetAxis(const vtkm::Vec &axis); + void SetRadius(FloatDefault radius); + + const vtkm::Vec& GetCenter() const; + const vtkm::Vec& GetAxis() const; + FloatDefault GetRadius() const; + + VTKM_EXEC_CONT + FloatDefault Value(const vtkm::Vec &x) const; + VTKM_EXEC_CONT + FloatDefault Value(FloatDefault x, FloatDefault y, FloatDefault z) const; + + VTKM_EXEC_CONT + vtkm::Vec Gradient(const vtkm::Vec &x) const; + VTKM_EXEC_CONT + vtkm::Vec + Gradient(FloatDefault x, FloatDefault y, FloatDefault z) const; + +private: + vtkm::Vec Center; + vtkm::Vec Axis; + FloatDefault Radius; +}; + + +//============================================================================ +/// \brief Implicit function for a frustum +class VTKM_ALWAYS_EXPORT Frustum : public ImplicitFunctionImpl +{ +public: + Frustum(); + Frustum(const vtkm::Vec points[6], + const vtkm::Vec normals[6]); + explicit Frustum(const vtkm::Vec points[8]); + + void SetPlanes(const vtkm::Vec points[6], + const vtkm::Vec normals[6]); + void SetPlane(int idx, vtkm::Vec &point, + vtkm::Vec &normal); + + void GetPlanes(vtkm::Vec points[6], + vtkm::Vec normals[6]) const; + const vtkm::Vec* GetPoints() const; + const vtkm::Vec* GetNormals() const; + + // The points should be specified in the order of hex-cell vertices + void CreateFromPoints(const vtkm::Vec points[8]); + + VTKM_EXEC_CONT + FloatDefault Value(FloatDefault x, FloatDefault y, FloatDefault z) const; + VTKM_EXEC_CONT + FloatDefault Value(const vtkm::Vec &x) const; + + VTKM_EXEC_CONT + vtkm::Vec + Gradient(FloatDefault, FloatDefault, FloatDefault) const; + VTKM_EXEC_CONT + vtkm::Vec Gradient(const vtkm::Vec&) const; + +private: + vtkm::Vec Points[6]; + vtkm::Vec Normals[6]; +}; + + //============================================================================ /// \brief Implicit function for a plane class VTKM_ALWAYS_EXPORT Plane : public ImplicitFunctionImpl { public: - Plane() - : Origin(FloatDefault(0)), - Normal(FloatDefault(0), FloatDefault(0), FloatDefault(1)) - { } - - explicit Plane(const vtkm::Vec &normal) - : Origin(FloatDefault(0)), - Normal(normal) - { } - + Plane(); + explicit Plane(const vtkm::Vec &normal); Plane(const vtkm::Vec &origin, - const vtkm::Vec &normal) - : Origin(origin), Normal(normal) - { } + const vtkm::Vec &normal); - void SetOrigin(const vtkm::Vec &origin) - { - this->Origin = origin; - this->Modified(); - } + void SetOrigin(const vtkm::Vec &origin); + void SetNormal(const vtkm::Vec &normal); - void SetNormal(const vtkm::Vec &normal) - { - this->Normal = normal; - this->Modified(); - } - - const vtkm::Vec& GetOrigin() const - { - return this->Origin; - } - - const vtkm::Vec& GetNormal() const - { - return this->Normal; - } + const vtkm::Vec& GetOrigin() const; + const vtkm::Vec& GetNormal() const; VTKM_EXEC_CONT - FloatDefault Value(FloatDefault x, FloatDefault y, FloatDefault z) const - { - return ((x - this->Origin[0]) * this->Normal[0]) + - ((y - this->Origin[1]) * this->Normal[1]) + - ((z - this->Origin[2]) * this->Normal[2]); - } + FloatDefault Value(FloatDefault x, FloatDefault y, FloatDefault z) const; + VTKM_EXEC_CONT + FloatDefault Value(const vtkm::Vec &x) const; VTKM_EXEC_CONT - FloatDefault Value(const vtkm::Vec &x) const - { - return this->Value(x[0], x[1], x[2]); - } - + vtkm::Vec + Gradient(FloatDefault, FloatDefault, FloatDefault) const; VTKM_EXEC_CONT - vtkm::Vec Gradient(FloatDefault, FloatDefault, FloatDefault) const - { - return this->Normal; - } - - VTKM_EXEC_CONT - vtkm::Vec Gradient(const vtkm::Vec&) const - { - return this->Normal; - } + vtkm::Vec Gradient(const vtkm::Vec&) const; private: vtkm::Vec Origin; @@ -268,65 +269,27 @@ private: class VTKM_ALWAYS_EXPORT Sphere : public ImplicitFunctionImpl { public: - Sphere() : Radius(FloatDefault(0.2)), Center(FloatDefault(0)) - { } + Sphere(); + explicit Sphere(FloatDefault radius); + Sphere(vtkm::Vec center, FloatDefault radius); - explicit Sphere(FloatDefault radius) : Radius(radius), Center(FloatDefault(0)) - { } + void SetRadius(FloatDefault radius); + void SetCenter(const vtkm::Vec ¢er); - Sphere(vtkm::Vec center, FloatDefault radius) - : Radius(radius), Center(center) - { } - - void SetRadius(FloatDefault radius) - { - this->Radius = radius; - this->Modified(); - } - - void SetCenter(const vtkm::Vec ¢er) - { - this->Center = center; - this->Modified(); - } - - FloatDefault GetRadius() const - { - return this->Radius; - } - - const vtkm::Vec& GetCenter() const - { - return this->Center; - } + FloatDefault GetRadius() const; + const vtkm::Vec& GetCenter() const; VTKM_EXEC_CONT - FloatDefault Value(FloatDefault x, FloatDefault y, FloatDefault z) const - { - return ((x - this->Center[0]) * (x - this->Center[0]) + - (y - this->Center[1]) * (y - this->Center[1]) + - (z - this->Center[2]) * (z - this->Center[2])) - - (this->Radius * this->Radius); - } + FloatDefault Value(FloatDefault x, FloatDefault y, FloatDefault z) const; + VTKM_EXEC_CONT + FloatDefault Value(const vtkm::Vec &x) const; VTKM_EXEC_CONT - FloatDefault Value(const vtkm::Vec &x) const - { - return this->Value(x[0], x[1], x[2]); - } - + vtkm::Vec + Gradient(FloatDefault x, FloatDefault y, FloatDefault z) const; VTKM_EXEC_CONT - vtkm::Vec Gradient(FloatDefault x, FloatDefault y, FloatDefault z) - const - { - return this->Gradient(vtkm::Vec(x, y, z)); - } - - VTKM_EXEC_CONT - vtkm::Vec Gradient(const vtkm::Vec &x) const - { - return FloatDefault(2) * (x - this->Center); - } + vtkm::Vec + Gradient(const vtkm::Vec &x) const; private: FloatDefault Radius; diff --git a/vtkm/cont/ImplicitFunction.hxx b/vtkm/cont/ImplicitFunction.hxx index ffe053393..8a8014543 100644 --- a/vtkm/cont/ImplicitFunction.hxx +++ b/vtkm/cont/ImplicitFunction.hxx @@ -17,12 +17,72 @@ // Laboratory (LANL), the U.S. Government retains certain rights in // this software. //============================================================================ +#include #include +#include +#include + + namespace vtkm { namespace cont { //============================================================================ +inline Box::Box() + : MinPoint(vtkm::Vec(FloatDefault(0))), + MaxPoint(vtkm::Vec(FloatDefault(1))) +{ } + +inline Box::Box(vtkm::Vec minPoint, + vtkm::Vec maxPoint) + : MinPoint(minPoint), MaxPoint(maxPoint) +{ } + +inline Box::Box(FloatDefault xmin, FloatDefault xmax, + FloatDefault ymin, FloatDefault ymax, + FloatDefault zmin, FloatDefault zmax) +{ + MinPoint[0] = xmin; MaxPoint[0] = xmax; + MinPoint[1] = ymin; MaxPoint[1] = ymax; + MinPoint[2] = zmin; MaxPoint[2] = zmax; +} + +inline void Box::SetMinPoint(const vtkm::Vec &point) +{ + this->MinPoint = point; + this->Modified(); +} + +inline void Box::SetMaxPoint(const vtkm::Vec &point) +{ + this->MaxPoint = point; + this->Modified(); +} + +inline const vtkm::Vec& Box::GetMinPoint() const +{ + return this->MinPoint; +} + +inline const vtkm::Vec& Box::GetMaxPoint() const +{ + return this->MaxPoint; +} + +VTKM_EXEC_CONT +inline FloatDefault +Box::Value(FloatDefault x, FloatDefault y, FloatDefault z) const +{ + return this->Value(vtkm::Vec(x, y, z)); +} + +VTKM_EXEC_CONT +inline vtkm::Vec +Box::Gradient(FloatDefault x, FloatDefault y, FloatDefault z) const +{ + return this->Gradient(vtkm::Vec(x, y, z)); +} + VTKM_EXEC_CONT inline FloatDefault Box::Value(const vtkm::Vec &x) const @@ -93,7 +153,6 @@ Box::Value(const vtkm::Vec &x) const } } -//============================================================================ VTKM_EXEC_CONT inline vtkm::Vec Box::Gradient(const vtkm::Vec &x) const @@ -103,11 +162,9 @@ Box::Gradient(const vtkm::Vec &x) const FloatDefault minDist = vtkm::Infinity32(); vtkm::Vec location; vtkm::Vec normal; - vtkm::Vec inside(FloatDefault(0), FloatDefault(0), FloatDefault(0)); - vtkm::Vec outside(FloatDefault(0), FloatDefault(0), FloatDefault(0)); - vtkm::Vec center((this->MaxPoint[0] + this->MinPoint[0]) * FloatDefault(0.5), - (this->MaxPoint[1] + this->MinPoint[1]) * FloatDefault(0.5), - (this->MaxPoint[2] + this->MinPoint[2]) * FloatDefault(0.5)); + vtkm::Vec inside(FloatDefault(0)); + vtkm::Vec outside(FloatDefault(0)); + vtkm::Vec center((this->MaxPoint + this->MinPoint) * FloatDefault(0.5)); // Compute the location of the point with respect to the box // Point will lie in one of 27 separate regions around or within the box @@ -200,5 +257,350 @@ Box::Gradient(const vtkm::Vec &x) const } +//============================================================================ +inline Cylinder::Cylinder() + : Center(FloatDefault(0)), + Axis(vtkm::make_Vec(FloatDefault(1), FloatDefault(0), FloatDefault(0))), + Radius(FloatDefault(0.2)) +{ } + +inline Cylinder::Cylinder(const vtkm::Vec &axis, + FloatDefault radius) + : Center(FloatDefault(0)), Axis(vtkm::Normal(axis)), Radius(radius) +{ } + +inline Cylinder::Cylinder(const vtkm::Vec ¢er, + const vtkm::Vec &axis, + FloatDefault radius) + : Center(center), Axis(vtkm::Normal(axis)), Radius(radius) +{ } + +inline void Cylinder::SetCenter(const vtkm::Vec ¢er) +{ + this->Center = center; + this->Modified(); +} + +inline void Cylinder::SetAxis(const vtkm::Vec &axis) +{ + this->Axis = vtkm::Normal(axis); + this->Modified(); +} + +inline void Cylinder::SetRadius(FloatDefault radius) +{ + this->Radius = radius; + this->Modified(); +} + +inline const vtkm::Vec& Cylinder::GetCenter() const +{ + return this->Center; +} + +inline const vtkm::Vec& Cylinder::GetAxis() const +{ + return this->Axis; +} + +inline FloatDefault Cylinder::GetRadius() const +{ + return this->Radius; +} + +VTKM_EXEC_CONT +inline FloatDefault Cylinder::Value(const vtkm::Vec &x) const +{ + vtkm::Vec x2c = x - this->Center; + FloatDefault proj = vtkm::dot(this->Axis, x2c); + return vtkm::dot(x2c, x2c) - (proj * proj) - (this->Radius * this->Radius); +} + +VTKM_EXEC_CONT +inline FloatDefault +Cylinder::Value(FloatDefault x, FloatDefault y, FloatDefault z) const +{ + return this->Value(vtkm::Vec(x, y, z)); +} + +VTKM_EXEC_CONT +inline vtkm::Vec +Cylinder::Gradient(const vtkm::Vec &x) const +{ + vtkm::Vec x2c = x - this->Center; + FloatDefault t = this->Axis[0] * x2c[0] + + this->Axis[1] * x2c[1] + + this->Axis[2] * x2c[2]; + vtkm::Vec closestPoint = this->Center + (this->Axis * t); + return (x - closestPoint) * FloatDefault(2); +} + +VTKM_EXEC_CONT +inline vtkm::Vec +Cylinder::Gradient(FloatDefault x, FloatDefault y, FloatDefault z) const +{ + return this->Gradient(vtkm::Vec(x, y, z)); +} + + +//============================================================================ +inline Frustum::Frustum() +{ + std::fill(this->Points, this->Points + 6, vtkm::Vec{}); + std::fill(this->Normals, this->Normals + 6, vtkm::Vec{}); +} + +inline Frustum::Frustum(const vtkm::Vec points[6], + const vtkm::Vec normals[6]) +{ + std::copy(points, points + 6, this->Points); + std::copy(normals, normals + 6, this->Normals); +} + +inline Frustum::Frustum(const vtkm::Vec points[8]) +{ + this->CreateFromPoints(points); +} + +inline void Frustum::SetPlanes(const vtkm::Vec points[6], + const vtkm::Vec normals[6]) +{ + std::copy(points, points + 6, this->Points); + std::copy(normals, normals + 6, this->Normals); + this->Modified(); +} + +inline void Frustum::GetPlanes(vtkm::Vec points[6], + vtkm::Vec normals[6]) const +{ + std::copy(this->Points, this->Points + 6, points); + std::copy(this->Normals, this->Normals + 6, normals); +} + +inline const vtkm::Vec* Frustum::GetPoints() const +{ + return this->Points; +} + +inline const vtkm::Vec* Frustum::GetNormals() const +{ + return this->Normals; +} + +inline void Frustum::SetPlane(int idx, vtkm::Vec &point, + vtkm::Vec &normal) +{ + if (idx < 0 || idx >= 6) + { + std::string msg = "Plane idx "; + msg += std::to_string(idx) + " is out of range [0, 5]"; + throw vtkm::cont::ErrorBadValue(msg); + } + + this->Points[idx] = point; + this->Normals[idx] = normal; + this->Modified(); +} + +inline void Frustum::CreateFromPoints(const vtkm::Vec points[8]) +{ + int planes[6][3] = {{3, 2, 0}, {4, 5, 7}, {0, 1, 4}, + {1, 2, 5}, {2, 3, 6}, {3, 0, 7}}; + + for (int i = 0; i < 6; ++i) + { + auto &v0 = points[planes[i][0]]; + auto &v1 = points[planes[i][1]]; + auto &v2 = points[planes[i][2]]; + + this->Points[i] = v0; + this->Normals[i] = vtkm::Normal(vtkm::Cross(v2 - v0, v1 - v0)); + this->Modified(); + } +} + +VTKM_EXEC_CONT +inline FloatDefault +Frustum::Value(FloatDefault x, FloatDefault y, FloatDefault z) const +{ + FloatDefault maxVal = -std::numeric_limits::max(); + for (int i = 0; i < 6; ++i) + { + auto &p = this->Points[i]; + auto &n = this->Normals[i]; + FloatDefault val = ((x - p[0]) * n[0]) + ((y - p[1]) * n[1]) + ((z - p[2]) * n[2]); + maxVal = vtkm::Max(maxVal, val); + } + return maxVal; +} + +VTKM_EXEC_CONT +inline FloatDefault Frustum::Value(const vtkm::Vec &x) const +{ + return this->Value(x[0], x[1], x[2]); +} + +VTKM_EXEC_CONT +inline vtkm::Vec +Frustum::Gradient(FloatDefault x, FloatDefault y, FloatDefault z) const +{ + FloatDefault maxVal = -std::numeric_limits::max(); + int maxValIdx = 0; + for (int i = 0; i < 6; ++i) + { + auto &p = this->Points[i]; + auto &n = this->Normals[i]; + FloatDefault val = ((x - p[0]) * n[0]) + ((y - p[1]) * n[1]) + ((z - p[2]) * n[2]); + if (val > maxVal) + { + maxVal = val; + maxValIdx = i; + } + } + return this->Normals[maxValIdx]; +} + +VTKM_EXEC_CONT +inline vtkm::Vec +Frustum::Gradient(const vtkm::Vec &x) const +{ + return this->Gradient(x[0], x[1], x[2]); +} + + +//============================================================================ +inline Plane::Plane() + : Origin(FloatDefault(0)), + Normal(FloatDefault(0), FloatDefault(0), FloatDefault(1)) +{ } + +inline Plane::Plane(const vtkm::Vec &normal) + : Origin(FloatDefault(0)), + Normal(normal) +{ } + +inline Plane::Plane(const vtkm::Vec &origin, + const vtkm::Vec &normal) + : Origin(origin), Normal(normal) +{ } + +inline void Plane::SetOrigin(const vtkm::Vec &origin) +{ + this->Origin = origin; + this->Modified(); +} + +inline void Plane::SetNormal(const vtkm::Vec &normal) +{ + this->Normal = normal; + this->Modified(); +} + +inline const vtkm::Vec& Plane::GetOrigin() const +{ + return this->Origin; +} + +inline const vtkm::Vec& Plane::GetNormal() const +{ + return this->Normal; +} + +VTKM_EXEC_CONT +inline FloatDefault +Plane::Value(FloatDefault x, FloatDefault y, FloatDefault z) const +{ + return ((x - this->Origin[0]) * this->Normal[0]) + + ((y - this->Origin[1]) * this->Normal[1]) + + ((z - this->Origin[2]) * this->Normal[2]); +} + +VTKM_EXEC_CONT +inline FloatDefault Plane::Value(const vtkm::Vec &x) const +{ + return this->Value(x[0], x[1], x[2]); +} + +VTKM_EXEC_CONT +inline vtkm::Vec +Plane::Gradient(FloatDefault, FloatDefault, FloatDefault) const +{ + return this->Normal; +} + +VTKM_EXEC_CONT +inline vtkm::Vec +Plane::Gradient(const vtkm::Vec&) const +{ + return this->Normal; +} + + +//============================================================================ +inline Sphere::Sphere() + : Radius(FloatDefault(0.2)), Center(FloatDefault(0)) +{ } + +inline Sphere::Sphere(FloatDefault radius) + : Radius(radius), Center(FloatDefault(0)) +{ } + +inline Sphere::Sphere(vtkm::Vec center, FloatDefault radius) + : Radius(radius), Center(center) +{ } + +inline void Sphere::SetRadius(FloatDefault radius) +{ + this->Radius = radius; + this->Modified(); +} + +inline void Sphere::SetCenter(const vtkm::Vec ¢er) +{ + this->Center = center; + this->Modified(); +} + +inline FloatDefault Sphere::GetRadius() const +{ + return this->Radius; +} + +inline const vtkm::Vec& Sphere::GetCenter() const +{ + return this->Center; +} + +VTKM_EXEC_CONT +inline FloatDefault +Sphere::Value(FloatDefault x, FloatDefault y, FloatDefault z) const +{ + return ((x - this->Center[0]) * (x - this->Center[0]) + + (y - this->Center[1]) * (y - this->Center[1]) + + (z - this->Center[2]) * (z - this->Center[2])) - + (this->Radius * this->Radius); +} + +VTKM_EXEC_CONT +inline FloatDefault Sphere::Value(const vtkm::Vec &x) const +{ + return this->Value(x[0], x[1], x[2]); +} + +VTKM_EXEC_CONT +inline vtkm::Vec +Sphere::Gradient(FloatDefault x, FloatDefault y, FloatDefault z) + const +{ + return this->Gradient(vtkm::Vec(x, y, z)); +} + +VTKM_EXEC_CONT +inline vtkm::Vec +Sphere::Gradient(const vtkm::Vec &x) const +{ + return FloatDefault(2) * (x - this->Center); +} + } } // vtkm::cont diff --git a/vtkm/cont/testing/TestingImplicitFunction.h b/vtkm/cont/testing/TestingImplicitFunction.h index d366e4b8a..c7e504199 100644 --- a/vtkm/cont/testing/TestingImplicitFunction.h +++ b/vtkm/cont/testing/TestingImplicitFunction.h @@ -79,21 +79,40 @@ template bool TestArrayEqual(const vtkm::cont::ArrayHandle &result, const std::array &expected) { - if (result.GetNumberOfValues() != N) - { - return false; - } + bool success = false; + auto portal = result.GetPortalConstControl(); + vtkm::Id count = portal.GetNumberOfValues(); - vtkm::cont::ArrayHandle::PortalConstControl portal = - result.GetPortalConstControl(); - for (std::size_t i = 0; i < N; ++i) + if (static_cast(count) == N) { - if (!test_equal(portal.Get(static_cast(i)), expected[i])) + success = true; + for (vtkm::Id i = 0; i < count; ++i) { - return false; + if (!test_equal(portal.Get(i), expected[static_cast(i)])) + { + success = false; + break; + } } } - return true; + if (!success) + { + if (count == 0) + { + std::cout << "result: \n"; + } + else + { + std::cout << "result: " << portal.Get(0); + for (vtkm::Id i = 1; i < count; ++i) + { + std::cout << ", " << portal.Get(i); + } + std::cout << "\n"; + } + } + + return success; } } // anonymous namespace @@ -109,26 +128,80 @@ public: template void Run(DeviceAdapter device) { - this->TestSphere(device); - this->TestPlane(device); this->TestBox(device); + this->TestCylinder(device); + this->TestFrustum(device); + this->TestPlane(device); + this->TestSphere(device); } private: template - void TestSphere(DeviceAdapter device) + void TestBox(DeviceAdapter device) { - std::cout << "Testing vtkm::cont::Sphere on " + std::cout << "Testing vtkm::cont::Box on " << vtkm::cont::DeviceAdapterTraits::GetName() << "\n"; - vtkm::cont::Sphere sphere({0.0f, 0.0f, 0.0f}, 1.0f); + vtkm::cont::Box box({0.0f, -0.5f, -0.5f}, {1.5f, 1.5f, 0.5f}); vtkm::cont::ArrayHandle values; implicit_function_detail::EvaluateOnCoordinates( - this->Input.GetCoordinateSystem(0), sphere, values, device); + this->Input.GetCoordinateSystem(0), box, values, device); std::array expected = - { {-1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 2.0f, 1.0f} }; + { {0.0f, -0.5f, 0.5f, 0.5f, 0.0f, -0.5f, 0.5f, 0.5f} }; + VTKM_TEST_ASSERT(implicit_function_detail::TestArrayEqual(values, expected), + "Result does not match expected values"); + } + + template + void TestCylinder(DeviceAdapter device) + { + std::cout << "Testing vtkm::cont::Cylinder on " + << vtkm::cont::DeviceAdapterTraits::GetName() + << "\n"; + + vtkm::cont::Cylinder cylinder; + cylinder.SetCenter({0.0f, 0.0f, 1.0f}); + cylinder.SetAxis({0.0f, 1.0f, 0.0f}); + cylinder.SetRadius(1.0f); + + vtkm::cont::ArrayHandle values; + implicit_function_detail::EvaluateOnCoordinates( + this->Input.GetCoordinateSystem(0), cylinder, values, device); + + std::array expected = + { {0.0f, 1.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f, -1.0f} }; + VTKM_TEST_ASSERT(implicit_function_detail::TestArrayEqual(values, expected), + "Result does not match expected values"); + } + + template + void TestFrustum(DeviceAdapter device) + { + std::cout << "Testing vtkm::cont::Frustum on " + << vtkm::cont::DeviceAdapterTraits::GetName() + << "\n"; + + vtkm::Vec points[8] = { + {0.0f, 0.0f, 0.0f}, // 0 + {1.0f, 0.0f, 0.0f}, // 1 + {1.0f, 0.0f, 1.0f}, // 2 + {0.0f, 0.0f, 1.0f}, // 3 + {0.5f, 1.5f, 0.5f}, // 4 + {1.5f, 1.5f, 0.5f}, // 5 + {1.5f, 1.5f, 1.5f}, // 6 + {0.5f, 1.5f, 1.5f} // 7 + }; + vtkm::cont::Frustum frustum; + frustum.CreateFromPoints(points); + + vtkm::cont::ArrayHandle values; + implicit_function_detail::EvaluateOnCoordinates( + this->Input.GetCoordinateSystem(0), frustum, values, device); + + std::array expected = + { {0.0f, 0.0f, 0.0f, 0.0f, 0.316228f, 0.316228f, -0.316228f, 0.316228f} }; VTKM_TEST_ASSERT(implicit_function_detail::TestArrayEqual(values, expected), "Result does not match expected values"); } @@ -160,19 +233,19 @@ private: } template - void TestBox(DeviceAdapter device) + void TestSphere(DeviceAdapter device) { - std::cout << "Testing vtkm::cont::Box on " + std::cout << "Testing vtkm::cont::Sphere on " << vtkm::cont::DeviceAdapterTraits::GetName() << "\n"; - vtkm::cont::Box box({0.0f, -0.5f, -0.5f}, {1.5f, 1.5f, 0.5f}); + vtkm::cont::Sphere sphere({0.0f, 0.0f, 0.0f}, 1.0f); vtkm::cont::ArrayHandle values; implicit_function_detail::EvaluateOnCoordinates( - this->Input.GetCoordinateSystem(0), box, values, device); + this->Input.GetCoordinateSystem(0), sphere, values, device); std::array expected = - { {0.0f, -0.5f, 0.5f, 0.5f, 0.0f, -0.5f, 0.5f, 0.5f} }; + { {-1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 2.0f, 1.0f} }; VTKM_TEST_ASSERT(implicit_function_detail::TestArrayEqual(values, expected), "Result does not match expected values"); }