mirror of
https://gitlab.kitware.com/vtk/vtk-m
synced 2024-09-16 17:22:55 +00:00
Worklets for cross and dot product.
This commit is contained in:
parent
de7162ab8f
commit
2537a1cf56
@ -25,11 +25,13 @@ set(headers
|
||||
Clip.h
|
||||
ContourTreeUniform.h
|
||||
CosmoTools.h
|
||||
CrossProduct.h
|
||||
DispatcherMapField.h
|
||||
DispatcherMapTopology.h
|
||||
DispatcherPointNeighborhood.h
|
||||
DispatcherReduceByKey.h
|
||||
DispatcherStreamingMapField.h
|
||||
DotProduct.h
|
||||
ExternalFaces.h
|
||||
ExtractGeometry.h
|
||||
ExtractPoints.h
|
||||
|
47
vtkm/worklet/CrossProduct.h
Normal file
47
vtkm/worklet/CrossProduct.h
Normal file
@ -0,0 +1,47 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//
|
||||
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
||||
// Copyright 2014 UT-Battelle, LLC.
|
||||
// Copyright 2014 Los Alamos National Security.
|
||||
//
|
||||
// Under the terms of Contract DE-NA0003525 with NTESS,
|
||||
// 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.
|
||||
//============================================================================
|
||||
#ifndef vtk_m_worklet_CrossProduct_h
|
||||
#define vtk_m_worklet_CrossProduct_h
|
||||
|
||||
#include <vtkm/worklet/WorkletMapField.h>
|
||||
|
||||
#include <vtkm/VectorAnalysis.h>
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
namespace worklet
|
||||
{
|
||||
|
||||
class CrossProduct : public vtkm::worklet::WorkletMapField
|
||||
{
|
||||
public:
|
||||
typedef void ControlSignature(FieldIn<VecAll>, FieldIn<VecAll>, FieldOut<VecAll>);
|
||||
typedef void ExecutionSignature(_1, _2, _3);
|
||||
|
||||
template <typename T, typename T2>
|
||||
VTKM_EXEC void operator()(const T& vec1, const T& vec2, T2& outVec) const
|
||||
{
|
||||
outVec = vtkm::Cross(vec1, vec2);
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace vtkm::worklet
|
||||
|
||||
#endif // vtk_m_worklet_CrossProduct_h
|
47
vtkm/worklet/DotProduct.h
Normal file
47
vtkm/worklet/DotProduct.h
Normal file
@ -0,0 +1,47 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//
|
||||
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
||||
// Copyright 2014 UT-Battelle, LLC.
|
||||
// Copyright 2014 Los Alamos National Security.
|
||||
//
|
||||
// Under the terms of Contract DE-NA0003525 with NTESS,
|
||||
// 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.
|
||||
//============================================================================
|
||||
#ifndef vtk_m_worklet_DotProduct_h
|
||||
#define vtk_m_worklet_DotProduct_h
|
||||
|
||||
#include <vtkm/worklet/WorkletMapField.h>
|
||||
|
||||
#include <vtkm/VectorAnalysis.h>
|
||||
|
||||
namespace vtkm
|
||||
{
|
||||
namespace worklet
|
||||
{
|
||||
|
||||
class DotProduct : public vtkm::worklet::WorkletMapField
|
||||
{
|
||||
public:
|
||||
typedef void ControlSignature(FieldIn<VecAll>, FieldIn<VecAll>, FieldOut<Scalar>);
|
||||
typedef void ExecutionSignature(_1, _2, _3);
|
||||
|
||||
template <typename T, typename T2>
|
||||
VTKM_EXEC void operator()(const T& v1, const T& v2, T2& outValue) const
|
||||
{
|
||||
outValue = vtkm::dot(v1, v2);
|
||||
}
|
||||
};
|
||||
}
|
||||
} // namespace vtkm::worklet
|
||||
|
||||
#endif // vtk_m_worklet_Normalize_h
|
@ -26,6 +26,8 @@ set(unit_tests
|
||||
UnitTestClipping.cxx
|
||||
UnitTestContourTreeUniform.cxx
|
||||
UnitTestCosmoTools.cxx
|
||||
UnitTestCrossProduct.cxx
|
||||
UnitTestDotProduct.cxx
|
||||
UnitTestExternalFaces.cxx
|
||||
UnitTestExtractGeometry.cxx
|
||||
UnitTestExtractPoints.cxx
|
||||
|
130
vtkm/worklet/testing/UnitTestCrossProduct.cxx
Normal file
130
vtkm/worklet/testing/UnitTestCrossProduct.cxx
Normal file
@ -0,0 +1,130 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//
|
||||
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
||||
// Copyright 2014 UT-Battelle, LLC.
|
||||
// Copyright 2014 Los Alamos National Security.
|
||||
//
|
||||
// Under the terms of Contract DE-NA0003525 with NTESS,
|
||||
// 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/worklet/CrossProduct.h>
|
||||
#include <vtkm/worklet/DispatcherMapField.h>
|
||||
|
||||
#include <random>
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
std::mt19937 randGenerator;
|
||||
|
||||
template <typename T>
|
||||
void createVectors(std::vector<vtkm::Vec<T, 3>>& vecs1, std::vector<vtkm::Vec<T, 3>>& vecs2)
|
||||
{
|
||||
// First, test the standard directions.
|
||||
// X x Y
|
||||
vecs1.push_back(vtkm::make_Vec(1, 0, 0));
|
||||
vecs2.push_back(vtkm::make_Vec(0, 1, 0));
|
||||
|
||||
// Y x Z
|
||||
vecs1.push_back(vtkm::make_Vec(0, 1, 0));
|
||||
vecs2.push_back(vtkm::make_Vec(0, 0, 1));
|
||||
|
||||
// Z x X
|
||||
vecs1.push_back(vtkm::make_Vec(0, 0, 1));
|
||||
vecs2.push_back(vtkm::make_Vec(1, 0, 0));
|
||||
|
||||
// Y x X
|
||||
vecs1.push_back(vtkm::make_Vec(0, 1, 0));
|
||||
vecs2.push_back(vtkm::make_Vec(1, 0, 0));
|
||||
|
||||
// Z x Y
|
||||
vecs1.push_back(vtkm::make_Vec(0, 0, 1));
|
||||
vecs2.push_back(vtkm::make_Vec(0, 1, 0));
|
||||
|
||||
// X x Z
|
||||
vecs1.push_back(vtkm::make_Vec(1, 0, 0));
|
||||
vecs2.push_back(vtkm::make_Vec(0, 0, 1));
|
||||
|
||||
//Test some other vector combinations
|
||||
std::uniform_real_distribution<vtkm::Float64> randomDist(-10.0, 10.0);
|
||||
randomDist(randGenerator);
|
||||
|
||||
for (int i = 0; i < 100; i++)
|
||||
{
|
||||
vecs1.push_back(vtkm::make_Vec(
|
||||
randomDist(randGenerator), randomDist(randGenerator), randomDist(randGenerator)));
|
||||
vecs2.push_back(vtkm::make_Vec(
|
||||
randomDist(randGenerator), randomDist(randGenerator), randomDist(randGenerator)));
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void TestCrossProduct()
|
||||
{
|
||||
std::vector<vtkm::Vec<T, 3>> inputVecs1, inputVecs2;
|
||||
createVectors(inputVecs1, inputVecs2);
|
||||
|
||||
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>> inputArray1, inputArray2;
|
||||
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>> outputArray;
|
||||
inputArray1 = vtkm::cont::make_ArrayHandle(inputVecs1);
|
||||
inputArray2 = vtkm::cont::make_ArrayHandle(inputVecs2);
|
||||
|
||||
vtkm::worklet::CrossProduct crossProductWorklet;
|
||||
vtkm::worklet::DispatcherMapField<vtkm::worklet::CrossProduct> dispatcherCrossProduct(
|
||||
crossProductWorklet);
|
||||
dispatcherCrossProduct.Invoke(inputArray1, inputArray2, outputArray);
|
||||
|
||||
VTKM_TEST_ASSERT(outputArray.GetNumberOfValues() == inputArray1.GetNumberOfValues(),
|
||||
"Wrong number of results for CrossProduct worklet");
|
||||
|
||||
//Test the cannonical cases.
|
||||
VTKM_TEST_ASSERT(
|
||||
test_equal(outputArray.GetPortalConstControl().Get(0), vtkm::make_Vec(0, 0, 1)) &&
|
||||
test_equal(outputArray.GetPortalConstControl().Get(1), vtkm::make_Vec(1, 0, 0)) &&
|
||||
test_equal(outputArray.GetPortalConstControl().Get(2), vtkm::make_Vec(0, 1, 0)) &&
|
||||
test_equal(outputArray.GetPortalConstControl().Get(3), vtkm::make_Vec(0, 0, -1)) &&
|
||||
test_equal(outputArray.GetPortalConstControl().Get(4), vtkm::make_Vec(-1, 0, 0)) &&
|
||||
test_equal(outputArray.GetPortalConstControl().Get(5), vtkm::make_Vec(0, -1, 0)),
|
||||
"Wrong result for CrossProduct worklet");
|
||||
|
||||
for (vtkm::Id i = 0; i < inputArray1.GetNumberOfValues(); i++)
|
||||
{
|
||||
vtkm::Vec<T, 3> v1 = inputArray1.GetPortalConstControl().Get(i);
|
||||
vtkm::Vec<T, 3> v2 = inputArray2.GetPortalConstControl().Get(i);
|
||||
vtkm::Vec<T, 3> res = outputArray.GetPortalConstControl().Get(i);
|
||||
|
||||
//Make sure result is orthogonal each input vector. Need to normalize to compare with zero.
|
||||
vtkm::Vec<T, 3> v1N(vtkm::Normal(v1)), v2N(vtkm::Normal(v1)), resN(vtkm::Normal(res));
|
||||
VTKM_TEST_ASSERT(test_equal(vtkm::dot(resN, v1N), T(0.0)), "Wrong result for cross product");
|
||||
VTKM_TEST_ASSERT(test_equal(vtkm::dot(resN, v2N), T(0.0)), "Wrong result for cross product");
|
||||
|
||||
T sinAngle = vtkm::Magnitude(res) * vtkm::RMagnitude(v1) * vtkm::RMagnitude(v2);
|
||||
T cosAngle = vtkm::dot(v1, v2) * vtkm::RMagnitude(v1) * vtkm::RMagnitude(v2);
|
||||
VTKM_TEST_ASSERT(test_equal(sinAngle * sinAngle + cosAngle * cosAngle, T(1.0)),
|
||||
"Bad cross product length.");
|
||||
}
|
||||
}
|
||||
|
||||
void TestCrossProductWorklets()
|
||||
{
|
||||
std::cout << "Testing CrossProduct Worklet" << std::endl;
|
||||
TestCrossProduct<vtkm::Float32>();
|
||||
TestCrossProduct<vtkm::Float64>();
|
||||
}
|
||||
}
|
||||
|
||||
int UnitTestCrossProduct(int, char* [])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(TestCrossProductWorklets);
|
||||
}
|
115
vtkm/worklet/testing/UnitTestDotProduct.cxx
Normal file
115
vtkm/worklet/testing/UnitTestDotProduct.cxx
Normal file
@ -0,0 +1,115 @@
|
||||
//============================================================================
|
||||
// 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.
|
||||
//
|
||||
// Copyright 2014 National Technology & Engineering Solutions of Sandia, LLC (NTESS).
|
||||
// Copyright 2014 UT-Battelle, LLC.
|
||||
// Copyright 2014 Los Alamos National Security.
|
||||
//
|
||||
// Under the terms of Contract DE-NA0003525 with NTESS,
|
||||
// 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/worklet/DispatcherMapField.h>
|
||||
#include <vtkm/worklet/DotProduct.h>
|
||||
|
||||
#include <vtkm/cont/testing/Testing.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
template <typename T>
|
||||
T normalizedVector(T v)
|
||||
{
|
||||
T vN = vtkm::Normal(v);
|
||||
return vN;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void createVectors(std::vector<vtkm::Vec<T, 3>>& vecs1,
|
||||
std::vector<vtkm::Vec<T, 3>>& vecs2,
|
||||
std::vector<T>& result)
|
||||
{
|
||||
vecs1.push_back(normalizedVector(vtkm::make_Vec(T(1), T(0), T(0))));
|
||||
vecs2.push_back(normalizedVector(vtkm::make_Vec(T(1), T(0), T(0))));
|
||||
result.push_back(1);
|
||||
|
||||
vecs1.push_back(normalizedVector(vtkm::make_Vec(T(1), T(0), T(0))));
|
||||
vecs2.push_back(normalizedVector(vtkm::make_Vec(T(-1), T(0), T(0))));
|
||||
result.push_back(-1);
|
||||
|
||||
vecs1.push_back(normalizedVector(vtkm::make_Vec(T(1), T(0), T(0))));
|
||||
vecs2.push_back(normalizedVector(vtkm::make_Vec(T(0), T(1), T(0))));
|
||||
result.push_back(0);
|
||||
|
||||
vecs1.push_back(normalizedVector(vtkm::make_Vec(T(1), T(0), T(0))));
|
||||
vecs2.push_back(normalizedVector(vtkm::make_Vec(T(0), T(-1), T(0))));
|
||||
result.push_back(0);
|
||||
|
||||
vecs1.push_back(normalizedVector(vtkm::make_Vec(T(1), T(0), T(0))));
|
||||
vecs2.push_back(normalizedVector(vtkm::make_Vec(T(1), T(1), T(0))));
|
||||
result.push_back(T(1.0 / vtkm::Sqrt(2.0)));
|
||||
|
||||
vecs1.push_back(normalizedVector(vtkm::make_Vec(T(1), T(1), T(0))));
|
||||
vecs2.push_back(normalizedVector(vtkm::make_Vec(T(1), T(0), T(0))));
|
||||
result.push_back(T(1.0 / vtkm::Sqrt(2.0)));
|
||||
|
||||
vecs1.push_back(normalizedVector(vtkm::make_Vec(T(-1), T(0), T(0))));
|
||||
vecs2.push_back(normalizedVector(vtkm::make_Vec(T(1), T(1), T(0))));
|
||||
result.push_back(-T(1.0 / vtkm::Sqrt(2.0)));
|
||||
|
||||
vecs1.push_back(normalizedVector(vtkm::make_Vec(T(0), T(1), T(0))));
|
||||
vecs2.push_back(normalizedVector(vtkm::make_Vec(T(1), T(1), T(0))));
|
||||
result.push_back(T(1.0 / vtkm::Sqrt(2.0)));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void TestDotProduct()
|
||||
{
|
||||
std::vector<vtkm::Vec<T, 3>> inputVecs1, inputVecs2;
|
||||
std::vector<T> answer;
|
||||
createVectors(inputVecs1, inputVecs2, answer);
|
||||
|
||||
vtkm::cont::ArrayHandle<vtkm::Vec<T, 3>> inputArray1, inputArray2;
|
||||
vtkm::cont::ArrayHandle<T> outputArray;
|
||||
inputArray1 = vtkm::cont::make_ArrayHandle(inputVecs1);
|
||||
inputArray2 = vtkm::cont::make_ArrayHandle(inputVecs2);
|
||||
|
||||
vtkm::worklet::DotProduct dotProductWorklet;
|
||||
vtkm::worklet::DispatcherMapField<vtkm::worklet::DotProduct> dispatcherDotProduct(
|
||||
dotProductWorklet);
|
||||
dispatcherDotProduct.Invoke(inputArray1, inputArray2, outputArray);
|
||||
|
||||
VTKM_TEST_ASSERT(outputArray.GetNumberOfValues() == inputArray1.GetNumberOfValues(),
|
||||
"Wrong number of results for DotProduct worklet");
|
||||
|
||||
for (vtkm::Id i = 0; i < inputArray1.GetNumberOfValues(); i++)
|
||||
{
|
||||
vtkm::Vec<T, 3> v1 = inputArray1.GetPortalConstControl().Get(i);
|
||||
vtkm::Vec<T, 3> v2 = inputArray2.GetPortalConstControl().Get(i);
|
||||
T ans = answer[static_cast<std::size_t>(i)];
|
||||
|
||||
VTKM_TEST_ASSERT(test_equal(ans, vtkm::dot(v1, v2)), "Wrong result for dot product");
|
||||
}
|
||||
}
|
||||
|
||||
void TestDotProductWorklets()
|
||||
{
|
||||
std::cout << "Testing DotProduct Worklet" << std::endl;
|
||||
TestDotProduct<vtkm::Float32>();
|
||||
// TestDotProduct<vtkm::Float64>();
|
||||
}
|
||||
}
|
||||
|
||||
int UnitTestDotProduct(int, char* [])
|
||||
{
|
||||
return vtkm::cont::testing::Testing::Run(TestDotProductWorklets);
|
||||
}
|
Loading…
Reference in New Issue
Block a user