Debian Codegraph @

src/Base/Tools3D.h

raw
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2
3 /***************************************************************************
4 * Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> *
5 * *
6 * This file is part of the FreeCAD CAx development system. *
7 * *
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Library General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Library General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Library General Public *
19 * License along with this library; see the file COPYING.LIB. If not, *
20 * write to the Free Software Foundation, Inc., 59 Temple Place, *
21 * Suite 330, Boston, MA 02111-1307, USA *
22 * *
23 ***************************************************************************/
24
25
26 #ifndef BASE_TOOLS3D_H
27 #define BASE_TOOLS3D_H
28
29 #ifndef FC_GLOBAL_H
30 # include <FCGlobal.h>
31 #endif
32
33 #include <cmath>
34 #include <vector>
35
36 namespace Base
37 {
38 template<typename T>
39 class BoundBox3;
40 class BoundBox2d;
41 class Line2d;
42 class Matrix4D;
43 class Polygon2d;
44 class Placement;
45 class Rotation;
46 template<typename T>
47 class Vector3;
48 using Vector3d = Vector3<double>;
49 class Vector2d;
50
51 /** Line3 ********************************************/
52
53 /**
54 * 3D line class.
55 */
56 template<class float_type>
57 class Line3
58 {
59 public:
60 Vector3<float_type> p1, p2;
61
62 Line3() = default;
63 ~Line3() = default;
64 Line3(const Line3& line) = default;
65 Line3(Line3&& line) noexcept = default;
66 Line3(const Vector3<float_type>& p1, const Vector3<float_type>& p2);
67
68 // operators
69 Line3& operator=(const Line3& line) = default;
70 Line3& operator=(Line3&& line) noexcept = default;
71 bool operator==(const Line3& line) const;
72
73 // methods
74 float_type Length() const;
75 float_type SqrLength() const;
76 BoundBox3<float_type> CalcBoundBox() const;
77 Vector3<float_type> GetBase() const;
78 Vector3<float_type> GetDirection() const;
79 void Transform(const Base::Matrix4D&);
80 void Transform(const Base::Placement&);
81 void Transform(const Base::Rotation&);
82 Line3 Transformed(const Base::Matrix4D&) const;
83 Line3 Transformed(const Base::Placement&) const;
84 Line3 Transformed(const Base::Rotation&) const;
85
86 /*!
87 * \brief Contains
88 * Checks if the point \a p is part of the line segment.
89 * \param pt
90 * \return
91 */
92 bool Contains(const Vector3<float_type>& pt) const;
93 /*!
94 * \brief Contains
95 * Checks if the distance of point \a p to the line segment is
96 * less than \a eps
97 * \param pt
98 * \param eps
99 * \return
100 */
101 bool Contains(const Vector3<float_type>& pt, float_type eps) const;
102 Vector3<float_type> FromPos(float_type distance) const;
103 };
104
105 /** Polygon3 ********************************************/
106
107 /**
108 * 3D polygon class.
109 */
110 template<class float_type>
111 class Polygon3
112 {
113 public:
114 Polygon3() = default;
115 ~Polygon3() = default;
116 Polygon3(const Polygon3<float_type>& poly) = default;
117 Polygon3(Polygon3<float_type>&& poly) noexcept = default;
118
119 Polygon3& operator=(const Polygon3<float_type>& poly) = default;
120 Polygon3& operator=(Polygon3<float_type>&& poly) noexcept = default;
121
122 size_t GetSize() const;
123 void Add(const Vector3<float_type>& pnt);
124 const Vector3<float_type>& operator[](size_t pos) const;
125 const Vector3<float_type>& At(size_t pos) const;
126 Vector3<float_type>& operator[](size_t pos);
127 Vector3<float_type>& At(size_t pos);
128 bool Remove(size_t pos);
129 void Clear();
130
131 // misc
132 BoundBox3<float_type> CalcBoundBox() const;
133 void Transform(const Base::Matrix4D&);
134 void Transform(const Base::Placement&);
135 void Transform(const Base::Rotation&);
136 Polygon3 Transformed(const Base::Matrix4D&) const;
137 Polygon3 Transformed(const Base::Placement&) const;
138 Polygon3 Transformed(const Base::Rotation&) const;
139
140 private:
141 std::vector<Vector3<float_type>> points;
142 };
143
144 using Line3f = Line3<float>;
145 using Line3d = Line3<double>;
146
147 using Polygon3f = Polygon3<float>;
148 using Polygon3d = Polygon3<double>;
149
150 } // namespace Base
151
152 #endif // BASE_TOOLS3D_H