Commit 437ba964 authored by Robert Maynard's avatar Robert Maynard Committed by Kitware Robot
Browse files

Merge topic 'cross_dot_product'

18ece6dc Better type signatures.
2537a1cf

 Worklets for cross and dot product.
Acked-by: Kitware Robot's avatarKitware Robot <kwrobot@kitware.com>
Merge-request: !1040
parents 45ba5f1a 18ece6dc
......@@ -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
......
//============================================================================
// 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>
VTKM_EXEC void operator()(const vtkm::Vec<T, 3>& vec1,
const vtkm::Vec<T, 3>& vec2,
vtkm::Vec<T, 3>& outVec) const
{
outVec = vtkm::Cross(vec1, vec2);
}
};
}
} // namespace vtkm::worklet
#endif // vtk_m_worklet_CrossProduct_h
//============================================================================
// 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, vtkm::IdComponent Size>
VTKM_EXEC void operator()(const vtkm::Vec<T, Size>& v1,
const vtkm::Vec<T, Size>& v2,
T& 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
......
//============================================================================
// 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);
}
//============================================================================
// 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);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment