Mechanics of Defect Evolution Library
Loading...
Searching...
No Matches
testEllipsoidalInclusionPolyData.cpp
Go to the documentation of this file.
2#include <Eigen/Geometry>
3#include <vtkDataArray.h>
4#include <vtkPointData.h>
5#include <iostream>
6#include <limits>
7#include <stdexcept>
8
9void check(bool condition, const char *message)
10{
11 if(!condition)
12 {
13 throw std::runtime_error(message);
14 }
15}
16
17void verify(const Eigen::Vector3d &center, const Eigen::Matrix3d &axes)
18{
19 const auto mesh = model::ellipsoidalInclusionPolyData(center, axes);
20 check(mesh->GetNumberOfPoints() > 0 && mesh->GetNumberOfCells() > 0, "Empty ellipsoid geometry");
21 const Eigen::Matrix3d inverse = axes.inverse();
22 auto *normals = mesh->GetPointData()->GetNormals();
23 check(normals != nullptr, "Missing surface normals");
24 Eigen::Matrix<double, 3, 2> tipDistance =
25 Eigen::Matrix<double, 3, 2>::Constant(std::numeric_limits<double>::infinity());
26 for(vtkIdType k = 0; k < mesh->GetNumberOfPoints(); ++k)
27 {
28 double p[3], n[3];
29 mesh->GetPoint(k, p);
30 normals->GetTuple(k, n);
31 const Eigen::Vector3d position(p[0], p[1], p[2]);
32 const Eigen::Vector3d unit = inverse * (position - center);
33 check(std::abs(unit.squaredNorm() - 1.0) < 1.e-11, "Incorrect surface scale or orientation");
34 const Eigen::Vector3d normal(n[0], n[1], n[2]);
35 const Eigen::Vector3d expectedNormal = (inverse.transpose() * unit).normalized();
36 check(normal.normalized().dot(expectedNormal) > 1.0 - 1.e-6, "Incorrect transformed surface normal");
37 for(int j = 0; j < 3; ++j)
38 {
39 tipDistance(j, 0) = std::min(tipDistance(j, 0), (position - center - axes.col(j)).norm());
40 tipDistance(j, 1) = std::min(tipDistance(j, 1), (position - center + axes.col(j)).norm());
41 }
42 }
43 check(tipDistance.maxCoeff() < 1.e-10, "Semiaxis endpoints missing or misoriented");
44}
45
46int main()
47{
48 try
49 {
50 const Eigen::Vector3d center(4, -3, 2);
51 verify(center, Eigen::Vector3d(3, 2, 1).asDiagonal());
52 const Eigen::Matrix3d rotation =
53 Eigen::AngleAxisd(.73, Eigen::Vector3d(1, 2, 3).normalized()).toRotationMatrix();
54 Eigen::Matrix3d axes = rotation * Eigen::Vector3d(7, 2, .5).asDiagonal();
55 verify(center, axes);
56 axes.col(1) *= -1;
57 verify(center, axes);
58 axes.col(0).swap(axes.col(2));
59 verify(center, axes);
60 verify(center, 2.0 * Eigen::Matrix3d::Identity());
61 std::cout
62 << "PASS rendered ellipsoid surfaces, semiaxis endpoints, rotation, reflection, permutation and normals\n";
63 return 0;
64 }
65 catch(const std::exception &error)
66 {
67 std::cerr << error.what() << '\n';
68 return 1;
69 }
70}
vtkSmartPointer< vtkPolyData > ellipsoidalInclusionPolyData(const Eigen::Vector3d &center, const Eigen::Matrix3d &axes)
void verify(const Eigen::Vector3d &center, const Eigen::Matrix3d &axes)
void check(bool condition, const char *message)
int j
vertex p[3]
int k