7 #include "fwMath/PlaneFunctions.hpp" 10 #include <glm/ext.hpp> 13 #define EPSILON 0.00000001 20 fwPlane getPlane(
const fwVec3d & point1,
const fwVec3d & point2,
const fwVec3d & point3)
30 void setValues(fwPlane& plane,
const fwVec3d & point1,
const fwVec3d & point2,
const fwVec3d & point3)
32 ::glm::dvec3 p1(point1[0], point1[1], point1[2]);
33 ::glm::dvec3 p2(point2[0], point2[1], point2[2]);
34 ::glm::dvec3 p3(point3[0], point3[1], point3[2]);
36 ::glm::dvec3 normal = ::glm::cross(p2 - p1, p3 - p1);
37 if(::glm::length(normal) <= 0.0)
43 normal = ::glm::normalize(normal);
44 double distance = ::glm::dot(normal, p1);
56 return {{plane[0], plane[1], plane[2]}};
61 void setNormal(fwPlane& plane,
const fwVec3d& normal)
63 ::glm::dvec3 vecNormal(normal[0], normal[1], normal[2]);
64 vecNormal = ::glm::normalize(vecNormal);
66 plane[0] = vecNormal[0];
67 plane[1] = vecNormal[1];
68 plane[2] = vecNormal[2];
89 ::glm::dvec3 normal(plane[0], plane[1], plane[2]);
90 normal = ::glm::normalize(normal);
91 ::glm::dvec3 lineDirection(line.second[0] - line.first[0],
92 line.second[1] - line.first[1],
93 line.second[2] - line.first[2]);
94 lineDirection = ::glm::normalize(lineDirection);
95 ::glm::dvec3 lineOrigin(line.first[0], line.first[1], line.first[2]);
97 double intersectionDistance = 0.;
98 double d = ::glm::dot(lineDirection, normal);
100 if(std::abs(d) < EPSILON)
105 intersectionDistance = (plane[3] - ::glm::dot(normal, lineOrigin)) / d;
107 lineOrigin += lineDirection * intersectionDistance;
108 point[0] = lineOrigin[0];
109 point[1] = lineOrigin[1];
110 point[2] = lineOrigin[2];
119 ::glm::dvec3 pointGlm(point[0], point[1], point[2]);
120 ::glm::dvec3 normal(plane[0], plane[1], plane[2]);
121 ::glm::normalize(normal);
122 ::glm::dvec3 pos = normal * plane[3];
123 return (::glm::dot(normal, pointGlm-pos) >= 0.0);
128 void transform(fwPlane& plane,
const fwMatrix4x4& matrix)
130 ::glm::dvec3 normal(plane[0], plane[1], plane[2]);
131 ::glm::dvec3 beg(normal * plane[3]);
132 ::glm::dvec3 end(beg + normal);
133 ::glm::dvec4 beg4(beg, 1.0);
134 ::glm::dvec4 end4(end, 1.0);
135 ::glm::dmat4x4 mat(matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0],
136 matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1],
137 matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2],
138 matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]);
152 normal = ::glm::normalize(normal);
154 plane[0] = normal[0];
155 plane[1] = normal[1];
156 plane[2] = normal[2];
157 plane[3] = ::glm::dot(normal, beg);
171 fwPlane getPlane(
const fwVec3d& normal,
const fwVec3d&
point)
173 ::glm::dvec3 pointGlm(point[0], point[1], point[2]);
174 ::glm::dvec3 normalGlm(normal[0], normal[1], normal[2]);
175 normalGlm = ::glm::normalize(normalGlm);
177 plane[0] = normalGlm[0];
178 plane[1] = normalGlm[1];
179 plane[2] = normalGlm[2];
180 plane[3] = ::glm::dot(normalGlm, pointGlm);
188 bool operator==(fwPlane& plane1, fwPlane& plane2)
190 ::glm::dvec4 pl1(plane1[0], plane1[1], plane1[2], plane1[3]);
191 ::glm::dvec4 pl2(plane2[0], plane2[1], plane2[2], plane2[3]);
193 double dx = pl1[0] - pl2[0];
194 double dy = pl1[1] - pl2[1];
195 double dz = pl1[2] - pl2[2];
196 double dd = pl1[3] - pl2[3];
198 return ( std::abs(dx) < EPSILON &&
199 std::abs(dy) < EPSILON &&
200 std::abs(dz) < EPSILON &&
201 std::abs(dd) < EPSILON );
FWMATH_API bool isInHalfSpace(const fwPlane &_plane, const fwVec3d &_point)
Compute if a point is in a half plane.
FWMATH_API double getDistance(const fwPlane &_plane)
Get the distance from origin for the given plan (_plane).
FWMATH_API void offset(fwPlane &_plane, double _offset)
Add an offset at the distance of origin which define the plane (_plane).
FWMATH_API void setValues(fwPlane &_plane, const fwVec3d &_point1, const fwVec3d &_point2, const fwVec3d &_point3)
Initialize a plane _plane with three points (_point1, _point2, _point3). It computes the plane's norm...
FWMATH_API void setDistance(fwPlane &_plane, const double _distance)
Set the distance from origin (_distance) for the given plan (_plane).
FWMATH_API void setNormal(fwPlane &_plane, const fwVec3d &_normal)
Set the normal of the given plane _plane.
The namespace fwMath contains classes which provide the implementation of several mathematic function...
FWMATH_API fwVec3d getNormal(const fwPlane &_plane)
Return the normal of the given plane _plane.
FWMATH_API void transform(fwPlane &_plane, const fwMatrix4x4 &_matrix)
Apply a transformation to a plane. The transformation is defined by a matrix 4x4. ...
FWMATH_API bool intersect(const fwLine &_ray, double _radius, const fwVec3d &_point)
Compute the projection of a point in a given direction and test if this intersection is inside a give...