27#ifndef EASY3D_CORE_PLANE_H
28#define EASY3D_CORE_PLANE_H
30#include <easy3d/core/vec.h>
31#include <easy3d/core/line.h>
32#include <easy3d/util/logging.h>
90 FT
a()
const {
return coeff_[0]; }
96 FT
b()
const {
return coeff_[1]; }
102 FT
c()
const {
return coeff_[2]; }
108 FT
d()
const {
return coeff_[3]; }
190 return (coeff_[0] * p.x + coeff_[1] * p.y + coeff_[2] * p.z + coeff_[3]);
257 const FT *
data()
const {
return coeff_; }
270 operator const FT *()
const {
return coeff_; }
277 operator FT *() {
return coeff_; }
284 template<
typename FT>
285 std::ostream &
operator<<(std::ostream &os,
const GenericPlane<FT> &plane);
287 template<
typename FT>
288 std::istream &
operator>>(std::istream &is, GenericPlane<FT> &plane);
291 template<
typename FT>
299 coeff_[3] = -(coeff_[0] * p1.x + coeff_[1] * p1.y + coeff_[2] * p1.z);
301 DLOG_IF(
length(n) < 1e-15, ERROR) <<
"degenerate plane constructed from 3 points:"
302 <<
"\t(" << p1 <<
")"
303 <<
"\t(" << p2 <<
")"
304 <<
"\t(" << p3 <<
")";
307 template<
typename FT>
313 coeff_[3] = -(coeff_[0] * p.x + coeff_[1] * p.y + coeff_[2] * p.z);
315 DLOG_IF(
length(nn) < 1e-15, ERROR) <<
"degenerate plane constructed from point ("
316 << p <<
") and normal (" << n <<
")";
320 template<
typename FT>
323 DLOG_IF(
length(n) < 1e-15, ERROR) <<
"degenerate plane with normal: (" << n <<
")";
328 template<
typename FT>
331 return Vector3(FT(1), FT(0), FT(0));
332 else if (coeff_[1] == 0)
333 return Vector3(FT(0), FT(1), FT(0));
334 else if (coeff_[2] == 0)
335 return Vector3(FT(0), FT(0), FT(1));
337 Vector3 base(-coeff_[1], coeff_[0], FT(0));
343 template<
typename FT>
349 template<
typename FT>
358 template<
typename FT>
364 template<
typename FT>
366 Point3 p(FT(0), FT(0), FT(0));
367 if (std::abs(coeff_[0]) >= std::abs(coeff_[1]) && std::abs(coeff_[0]) >= std::abs(coeff_[2]))
368 p.x = -coeff_[3] / coeff_[0];
369 else if (std::abs(coeff_[1]) >= std::abs(coeff_[0]) && std::abs(coeff_[1]) >= std::abs(coeff_[2]))
370 p.y = -coeff_[3] / coeff_[1];
372 p.z = -coeff_[3] / coeff_[2];
391 template<
typename FT>
394 if (std::abs(v) < 1e-15)
397 return (v > 0.0 ? 1 : -1);
401 template<
typename FT>
408 FT num = coeff_[0] * p.x + coeff_[1] * p.y + coeff_[2] * p.z + coeff_[3];
409 FT den = coeff_[0] * coeff_[0] + coeff_[1] * coeff_[1] + coeff_[2] * coeff_[2];
410 FT lambda = num / den;
412 FT x = p.x - lambda * coeff_[0];
413 FT y = p.y - lambda * coeff_[1];
414 FT z = p.z - lambda * coeff_[2];
419 template<
typename FT>
422 return (v * v) / (coeff_[0] * coeff_[0] + coeff_[1] * coeff_[1] + coeff_[2] * coeff_[2]);
426 template<
typename FT>
430 if (std::abs(
c) < 1e-15)
437 template<
typename FT>
441 if (std::abs(
c) < 1e-15)
447 FT t = -(coeff_[0] * p0.x + coeff_[1] * p0.y + coeff_[2] * p0.z + coeff_[3]) /
448 (coeff_[0] * dir.x + coeff_[1] * dir.y + coeff_[2] * dir.z);
454 template<
typename FT>
458 if ((ss == 1 && st == -1) || (ss == -1 && st == 1))
460 else if (ss == 0 || st == 0)
467 template<
typename FT>
471 if ((ss == 1 && st == -1) || (ss == -1 && st == 1)) {
475 LOG(ERROR) <<
"fatal error. Should have intersection";
482 }
else if (st == 0) {
493 template<
typename FT>
495 const FT &
a = coeff_[0];
496 const FT &
b = coeff_[1];
497 const FT &
c = coeff_[2];
498 const FT &
d = coeff_[3];
499 const FT &p = another.coeff_[0];
500 const FT &q = another.coeff_[1];
501 const FT &r = another.coeff_[2];
502 const FT &s = another.coeff_[3];
504 FT
det =
a * q - p *
b;
515 if (
a != 0 || p != 0)
516 return (
a * s == p *
d);
517 if (
b != 0 || q != 0)
518 return (
b * s == q *
d);
519 if (
c != 0 || r != 0)
520 return (
c * s == r *
d);
526 template<
typename FT>
528 const FT &
a = coeff_[0];
529 const FT &
b = coeff_[1];
530 const FT &
c = coeff_[2];
531 const FT &
d = coeff_[3];
532 const FT &p = another.coeff_[0];
533 const FT &q = another.coeff_[1];
534 const FT &r = another.coeff_[2];
535 const FT &s = another.coeff_[3];
537 FT
det =
a * q - p *
b;
565 template<
typename FT>
567 return os << plane[0] <<
' ' << plane[1] <<
' ' << plane[2] <<
' ' << plane[3];
571 template<
typename FT>
573 return is >> plane[0] >> plane[1] >> plane[2] >> plane[3];
586 template<
typename FT>
A generic line representation, which supports both 2D and 3D lines.
Definition line.h:45
static GenericLine from_two_points(const Point &p, const Point &q)
Definition line.h:65
const Vector & direction() const
Returns the direction of the line.
Definition line.h:86
const Point & point() const
Returns an arbitrary point on the line.
Definition line.h:92
static GenericLine from_point_and_direction(const Point &p, const Vector &dir)
Definition line.h:58
A 3D Plane of equation a*x + b*y + c*z + d = 0.
Definition plane.h:43
bool intersect(const Line3 &line, Point3 &p) const
Tests if a line intersects with this plane (both line and the plane are unlimited).
Definition plane.h:438
GenericPlane(const Point3 &p1, const Point3 &p2, const Point3 &p3)
Constructs a plane from three points p1, p2, and p3.
Definition plane.h:292
FT * data()
Returns the memory address of the coefficients.
Definition plane.h:263
FT squared_distance(const Point3 &p) const
Computes the squared distance of a point p to this plane.
Definition plane.h:420
FT value(const Point3 &p) const
Evaluates the plane (i.e., computes the value of a * x + b * y + c * z + d) for the given point p.
Definition plane.h:189
float c() const
Definition plane.h:102
Vec< 3, FT > Vector3
The type of the 3D vector.
Definition plane.h:47
Vector3 base2() const
Returns the second ortho-base defined on this plane.
Definition plane.h:344
bool intersect(const thisclass &another, Line3 &line) const
Tests if this plane intersects with another plane.
Definition plane.h:527
Point2 to_2d(const Point3 &p) const
Converts a 3D point into a 2D point relative to the local coordinate system defined by the three orth...
Definition plane.h:350
float b() const
Definition plane.h:96
bool intersect(const thisclass &another) const
Tests if this plane intersects with another plane.
Definition plane.h:494
int orient(const Point3 &p) const
Determines the relative orientation of a point with respect to this plane.
Definition plane.h:392
bool intersect(const Point3 &s, const Point3 &t) const
Tests if a line segment (given by its two end points s and t) intersects with this plane.
Definition plane.h:455
bool intersect(const Line3 &line) const
Tests if a line intersects with this plane (both line and the plane are unlimited).
Definition plane.h:427
float a() const
Definition plane.h:90
const FT * data() const
Returns the constant memory address of the coefficients.
Definition plane.h:257
Vec< 3, FT > Point3
The type of the 3D point.
Definition plane.h:46
const FT & operator[](size_t idx) const
Returns the idx_th const parameter of the plane equation (const version).
Definition plane.h:125
Vector3 base1() const
Returns the first ortho-base defined on this plane.
Definition plane.h:329
GenericPlane< FT > thisclass
The type of this class.
Definition plane.h:49
GenericPlane()=default
Default constructor.
FT & operator[](size_t idx)
Returns the idx_th parameter of the plane equation.
Definition plane.h:115
bool intersect(const Point3 &s, const Point3 &t, Point3 &p) const
Tests if a line segment (given by its two end points s and t) intersects with this plane.
Definition plane.h:468
Point3 projection(const Point3 &p) const
The projection of a point p on this plane.
Definition plane.h:402
GenericPlane(FT a, FT b, FT c, FT d)
Constructs a plane from its equation parameters a, b, c, and d.
Definition plane.h:74
Vector3 normal() const
Returns the normal of the plane.
Definition plane.h:321
GenericPlane(const Point3 &p, const Vector3 &n)
Constructs a plane from a point p and the plane normal n.
Definition plane.h:308
Point3 to_3d(const Point2 &p) const
Converts a 2D point in the local coordinate system defined by the three orthogonal vectors base1(),...
Definition plane.h:359
Point3 point() const
Returns a point lying on this plane.
Definition plane.h:365
GenericLine< 3, FT > Line3
The type of the 3D line.
Definition plane.h:48
Vec< 2, FT > Point2
The type of the 2D point.
Definition plane.h:45
float d() const
Definition plane.h:108
Base class for vector types. It provides generic functionality for N dimensional vectors.
Definition vec.h:30
bool intersect(const GenericPlane< FT > &plane1, const GenericPlane< FT > &plane2, const GenericPlane< FT > &plane3, typename GenericPlane< FT >::Point3 &point)
Definition plane.h:587
Definition collider.cpp:182
Vec< N, T > normalize(const Vec< N, T > &v)
Computes and returns the normalized vector (Note: the input vector is not modified).
Definition vec.h:299
T length(const Vec< N, T > &v)
Computes the length/magnitude of a vector.
Definition vec.h:289
Vec< 3, T > cross(const Vec< 3, T > &v1, const Vec< 3, T > &v2)
Compute the cross product of two 3D vectors.
Definition vec.h:685
std::ostream & operator<<(std::ostream &os, Graph::Vertex v)
Output stream support for Graph::Vertex.
Definition graph.h:1300
T det(const Vec< 2, T > &v1, const Vec< 2, T > &v2)
Compute the determinant of the 2x2 matrix formed by the two 2D vectors as the two rows.
Definition vec.h:481
std::istream & operator>>(std::istream &is, GenericLine< DIM, FT > &line)
Input stream support for GenericLine.
Definition line.h:183
FT dot(const std::vector< FT > &, const std::vector< FT > &)
Inner product for vectors.
Definition matrix.h:1834