opensurgsim
MockTriangle.h
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_MATH_UNITTESTS_MOCKTRIANGLE_H
17 #define SURGSIM_MATH_UNITTESTS_MOCKTRIANGLE_H
18 
19 #include "SurgSim/Math/Vector.h"
21 
22 namespace SurgSim
23 {
24 namespace Math
25 {
26 
29 {
30 public:
31  // Default constructor.
32  MockTriangle() {}
33 
34  // Constructor.
35  MockTriangle(const Vector3d& vertex0, const Vector3d& vertex1, const Vector3d& vertex2) :
36  v0(vertex0), v1(vertex1), v2(vertex2), v0v1(vertex1 - vertex0), v0v2(vertex2 - vertex0),
37  v1v2(vertex2 - vertex1)
38  {
39  n = v0v1.cross(v0v2);
40  n.normalize();
41  }
42  // Constructor with normal.
43  MockTriangle(const Vector3d& vertex0, const Vector3d& vertex1, const Vector3d& vertex2, const Vector3d& normal) :
44  v0(vertex0), v1(vertex1), v2(vertex2), v0v1(vertex1 - vertex0), v0v2(vertex2 - vertex0),
45  v1v2(vertex2 - vertex1), n(normal)
46  {
47  }
48 
49  // Find a point inside the triangle, given a pair of scaling factor for the edges (v0v1 and v0v2).
50  Vector3d pointInTriangle(double a, double b) const
51  {
52  return v0 + a * v0v1 + b * v0v2;
53  }
54 
55  // Move this triangle by the given vector.
56  void translate(const Vector3d& v)
57  {
58  v0 += v;
59  v1 += v;
60  v2 += v;
61  }
62 
63  // Rotate this triangle about the x-axis by the given angle.
64  void rotateAboutXBy(double angle)
65  {
66  RigidTransform3d r(Eigen::AngleAxis<double>(angle * (M_PI / 180.0), Vector3d(1, 0, 0)));
67  v0 = r * v0;
68  v1 = r * v1;
69  v2 = r * v2;
70  n = r * n;
71  }
72 
73  // Rotate this triangle about the y-axis by the given angle.
74  void rotateAboutYBy(double angle)
75  {
76  SurgSim::Math::RigidTransform3d r(Eigen::AngleAxis<double>(angle * (M_PI / 180.0), Vector3d(0, 1, 0)));
77  v0 = r * v0;
78  v1 = r * v1;
79  v2 = r * v2;
80  n = r * n;
81  }
82 
83  // Rotate this triangle about the z-axis by the given angle.
84  void rotateAboutZBy(double angle)
85  {
86  SurgSim::Math::RigidTransform3d r(Eigen::AngleAxis<double>(angle * (M_PI / 180.0), Vector3d(0, 0, 1)));
87  v0 = r * v0;
88  v1 = r * v1;
89  v2 = r * v2;
90  n = r * n;
91  }
92 
93  // Transform this triangle by the given matrix.
94  void transform(SurgSim::Math::RigidTransform3d transform)
95  {
96  v0 = transform * v0;
97  v1 = transform * v1;
98  v2 = transform * v2;
99  n = transform.linear() * n;
100  }
101 
102  // Vertices of this triangle.
103  Vector3d v0;
104  Vector3d v1;
105  Vector3d v2;
106 
107  // Edges of this triangle.
108  Vector3d v0v1;
109  Vector3d v0v2;
110  Vector3d v1v2;
111 
112  // Normal of the triangle.
113  Vector3d n;
114 };
115 
116 
117 } // namespace Math
118 
119 } // namespace SurgSim
120 
121 #endif // SURGSIM_MATH_UNITTESTS_MOCKTRIANGLE_H
Wraps glewInit() to separate the glew opengl definitions from the osg opengl definitions only imgui n...
Definition: AddRandomSphereBehavior.cpp:36
Eigen::Transform< double, 3, Eigen::Isometry > RigidTransform3d
A 3D rigid (isometric) transform, represented as doubles.
Definition: RigidTransform.h:46
Eigen::Matrix< double, 3, 1 > Vector3d
A 3D vector of doubles.
Definition: Vector.h:57
MockTriangle class used in the unit tests.
Definition: MockTriangle.h:28
Definitions of 2x2 and 3x3 rigid (isometric) transforms.
Definitions of small fixed-size vector types.