Visual Computing Library
Loading...
Searching...
No Matches
plane.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_SPACE_CORE_PLANE_H
24#define VCL_SPACE_CORE_PLANE_H
25
26#include "point.h"
27
28#include <vclib/concepts/space/plane.h>
29#include <vclib/exceptions/misc.h>
30
31namespace vcl {
32
48// TODO: Make Plane Serializable and add tests.
49template<typename Scalar, bool NORM = true>
50class Plane
51{
52 Point3<Scalar> mDir;
53 Scalar mOffset;
54
55public:
56 using ScalarType = Scalar;
58
62 Plane() {}
63
70 mDir(direction), mOffset(offset)
71 {
72 if constexpr (NORM) {
73 Scalar n = mDir.norm();
74 mDir /= n;
75 mOffset /= n;
76 }
77 }
78
85 Plane(const Point3<Scalar>& p0, const Point3<Scalar>& normal)
86 {
87 mDir = normal;
88 if constexpr (NORM) {
89 mDir.normalize();
90 }
91 mOffset = p0.dot(mDir);
92 }
93
101 const Point3<Scalar>& p0,
102 const Point3<Scalar>& p1,
103 const Point3<Scalar>& p2) :
104 Plane<Scalar, NORM>(p0, (p2 - p0).cross(p1 - p0))
105 {
106 }
107
108 template<typename S>
109 auto cast() const
110 {
111 if constexpr (std::is_same<Scalar, S>::value) {
112 return *this;
113 }
114 else {
115 return Plane<S, NORM>(mDir.template cast<S>(), mOffset);
116 }
117 }
118
123 const Point3<Scalar>& direction() const { return mDir; }
124
129 Scalar offset() const { return mOffset; }
130
137 {
138 Scalar k = p.dot(mDir) - mOffset;
139 return p - mDir * k;
140 }
141
148 {
150 mirr += mirr - p;
151 return mirr;
152 }
153
154 bool operator==(const Plane& p) const
155 {
156 return mOffset == p.mOffset && mDir == p.mDir;
157 }
158
159 bool operator!=(const Plane& p) const { return !(*this == p); }
160};
161
162/* Specialization Aliases */
163
164using Planef = Plane<float>;
165using Planed = Plane<double>;
166
167} // namespace vcl
168
169#endif // VCL_SPACE_CORE_PLANE_H
The Plane class represent a 2D plane in 3D space.
Definition plane.h:51
Point3< Scalar > projectPoint(const Point3< Scalar > &p) const
Given a point, returns the point projected to this plane.
Definition plane.h:136
Plane(const Point3< Scalar > &p0, const Point3< Scalar > &normal)
Constructor of a plane given a point lying to the plane and the normal of the plane.
Definition plane.h:85
Plane(const Point3< Scalar > &p0, const Point3< Scalar > &p1, const Point3< Scalar > &p2)
Constructor of a plane given three points.
Definition plane.h:100
Point3< Scalar > mirrorPoint(const Point3< Scalar > &p) const
Given a point, returns the point mirrored w.r.t. this plane.
Definition plane.h:147
Plane(const Point3< Scalar > &direction, Scalar offset)
Constructor of a plane given a direction and an offset.
Definition plane.h:69
Plane()
Empty constructor. The plane is uninitialized.
Definition plane.h:62
const Point3< Scalar > & direction() const
Returns the direction component of the plane.
Definition plane.h:123
Scalar offset() const
Returns the offset component of the plane.
Definition plane.h:129
A class representing a line segment in n-dimensional space. The class is parameterized by a PointConc...
Definition segment.h:43