Visual Computing Library  devel
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
28namespace vcl {
29
45template<typename Scalar, bool NORM = true>
46class Plane
47{
48 Point3<Scalar> mDir;
49 Scalar mOffset;
50
51public:
52 using ScalarType = Scalar;
54
55 static constexpr bool NORMED = NORM;
56
60 Plane() {}
61
68 mDir(direction), mOffset(offset)
69 {
70 if constexpr (NORM) {
71 Scalar n = mDir.norm();
72 mDir /= n;
73 mOffset /= n;
74 }
75 }
76
83 Plane(const Point3<Scalar>& p0, const Point3<Scalar>& normal)
84 {
85 mDir = normal;
86 if constexpr (NORM) {
87 mDir.normalize();
88 }
89 mOffset = p0.dot(mDir);
90 }
91
99 const Point3<Scalar>& p0,
100 const Point3<Scalar>& p1,
101 const Point3<Scalar>& p2) :
102 Plane<Scalar, NORM>(p0, (p2 - p0).cross(p1 - p0))
103 {
104 }
105
106 template<typename S>
107 auto cast() const
108 {
109 if constexpr (std::is_same<Scalar, S>::value) {
110 return *this;
111 }
112 else {
113 return Plane<S, NORM>(mDir.template cast<S>(), mOffset);
114 }
115 }
116
121 const Point3<Scalar>& direction() const { return mDir; }
122
127 Scalar offset() const { return mOffset; }
128
135 {
136 Scalar k = p.dot(mDir) - mOffset;
137 return p - mDir * k;
138 }
139
146 {
148 mirr += mirr - p;
149 return mirr;
150 }
151
156 void serialize(std::ostream& os) const
157 {
158 mDir.serialize(os);
159 vcl::serialize(os, mOffset);
160 }
161
166 void deserialize(std::istream& is)
167 {
168 mDir.deserialize(is);
169 vcl::deserialize(is, mOffset);
170 }
171
172 bool operator==(const Plane& p) const
173 {
174 return mOffset == p.mOffset && mDir == p.mDir;
175 }
176
177 bool operator!=(const Plane& p) const { return !(*this == p); }
178};
179
180/* Specialization Aliases */
181
182using Planef = Plane<float>;
183using Planed = Plane<double>;
184
185/* Concepts */
186
197template<typename T>
198concept PlaneConcept = std::derived_from< // same type or derived type
199 std::remove_cvref_t<T>,
200 Plane<typename RemoveRef<T>::ScalarType, RemoveRef<T>::NORMED>>;
201
202} // namespace vcl
203
204#endif // VCL_SPACE_CORE_PLANE_H
A class representing a box in N-dimensional space.
Definition box.h:46
void deserialize(std::istream &is)
Deserializes the box from the given input stream.
Definition box.h:476
void serialize(std::ostream &os) const
Serializes the box to the given output stream.
Definition box.h:466
The Plane class represent a 2D plane in 3D space.
Definition plane.h:47
void deserialize(std::istream &is)
Deserializes the plane from the given input stream.
Definition plane.h:166
Point3< Scalar > projectPoint(const Point3< Scalar > &p) const
Given a point, returns the point projected to this plane.
Definition plane.h:134
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:83
Plane(const Point3< Scalar > &p0, const Point3< Scalar > &p1, const Point3< Scalar > &p2)
Constructor of a plane given three points.
Definition plane.h:98
Point3< Scalar > mirrorPoint(const Point3< Scalar > &p) const
Given a point, returns the point mirrored w.r.t. this plane.
Definition plane.h:145
Plane(const Point3< Scalar > &direction, Scalar offset)
Constructor of a plane given a direction and an offset.
Definition plane.h:67
Plane()
Empty constructor. The plane is uninitialized.
Definition plane.h:60
const Point3< Scalar > & direction() const
Returns the direction component of the plane.
Definition plane.h:121
void serialize(std::ostream &os) const
Serializes the plane to the given output stream.
Definition plane.h:156
Scalar offset() const
Returns the offset component of the plane.
Definition plane.h:127
A concept representing a Plane.
Definition plane.h:198