|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0.txt |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + *=========================================================================*/ |
| 18 | + |
| 19 | +// First include the header file to be tested: |
| 20 | +#include "itkPointSet.h" |
| 21 | +#include "../../QuadEdgeMesh/include/itkQuadEdgeMeshTraits.h" |
| 22 | +#include <gtest/gtest.h> |
| 23 | +#include <algorithm> // For equal. |
| 24 | +#include <numeric> // For iota. |
| 25 | + |
| 26 | +namespace |
| 27 | +{ |
| 28 | +template <typename TPointSet> |
| 29 | +void |
| 30 | +TestSetPointsByCoordinates(TPointSet & pointSet) |
| 31 | +{ |
| 32 | + using CoordRepType = typename TPointSet::CoordRepType; |
| 33 | + |
| 34 | + static constexpr auto PointDimension = TPointSet::PointDimension; |
| 35 | + |
| 36 | + for (unsigned int numberOfCoordinates{ 1 }; numberOfCoordinates < PointDimension; ++numberOfCoordinates) |
| 37 | + { |
| 38 | + // SetPointsByCoordinates is expected to throw an exception when the specified number of coordinates is not a |
| 39 | + // multiple of PointDimension. |
| 40 | + EXPECT_THROW(pointSet.SetPointsByCoordinates(std::vector<CoordRepType>(numberOfCoordinates)), itk::ExceptionObject); |
| 41 | + } |
| 42 | + |
| 43 | + for (const unsigned int numberOfPoints : { 2, 1, 0 }) |
| 44 | + { |
| 45 | + std::vector<CoordRepType> coordinates(numberOfPoints * PointDimension); |
| 46 | + |
| 47 | + // Just make sure that all coordinates have different values, for the purpose of the test. |
| 48 | + std::iota(coordinates.begin(), coordinates.end(), CoordRepType()); |
| 49 | + { |
| 50 | + const auto modifiedTime = pointSet.GetMTime(); |
| 51 | + pointSet.SetPointsByCoordinates(coordinates); |
| 52 | + EXPECT_GT(pointSet.GetMTime(), modifiedTime); |
| 53 | + } |
| 54 | + |
| 55 | + using PointsContainerType = typename TPointSet::PointsContainer; |
| 56 | + using PointIdentifier = typename TPointSet::PointIdentifier; |
| 57 | + using PointType = typename TPointSet::PointType; |
| 58 | + |
| 59 | + const typename PointsContainerType::ConstPointer points = pointSet.GetPoints(); |
| 60 | + |
| 61 | + ASSERT_NE(points, nullptr); |
| 62 | + ASSERT_EQ(points->size(), numberOfPoints); |
| 63 | + |
| 64 | + const typename PointsContainerType::STLContainerType & stlContainer = points->CastToSTLConstContainer(); |
| 65 | + auto coordinateIterator = coordinates.cbegin(); |
| 66 | + |
| 67 | + for (PointIdentifier pointIdentifier{}; pointIdentifier < numberOfPoints; ++pointIdentifier) |
| 68 | + { |
| 69 | + const PointType & point = stlContainer.at(pointIdentifier); |
| 70 | + EXPECT_TRUE(std::equal(point.cbegin(), point.cend(), coordinateIterator)); |
| 71 | + coordinateIterator += PointDimension; |
| 72 | + } |
| 73 | + } |
| 74 | +} |
| 75 | +} // namespace |
| 76 | + |
| 77 | + |
| 78 | +TEST(PointSet, SetPointsByCoordinates) |
| 79 | +{ |
| 80 | + TestSetPointsByCoordinates(*itk::PointSet<int>::New()); |
| 81 | + TestSetPointsByCoordinates(*itk::PointSet<double, 2, itk::QuadEdgeMeshTraits<double, 2, bool, bool>>::New()); |
| 82 | +} |
0 commit comments