Debian Codegraph @

src/Base/Tools3D.cpp

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 #include "BoundBox.h"
26 #include "Placement.h"
27 #include "Tools3D.h"
28
29 using namespace Base;
30
31
32 template<typename float_type>
33 Line3<float_type>::Line3(const Vector3<float_type>& p1, const Vector3<float_type>& p2)
34 : p1(p1)
35 , p2(p2)
36 {}
37
38 template<typename float_type>
39 bool Line3<float_type>::operator==(const Line3<float_type>& line) const
40 {
41 return (p1 == line.p1 && p2 == line.p2);
42 }
43
44 template<typename float_type>
45 float_type Line3<float_type>::Length() const
46 {
47 return Base::Distance(p1, p2);
48 }
49
50 template<typename float_type>
51 float_type Line3<float_type>::SqrLength() const
52 {
53 return Base::DistanceP2(p1, p2);
54 }
55
56 template<typename float_type>
57 BoundBox3<float_type> Line3<float_type>::CalcBoundBox() const
58 {
59 BoundBox3<float_type> box;
60 box.Add(p1);
61 box.Add(p2);
62 return box;
63 }
64
65 template<typename float_type>
66 Vector3<float_type> Line3<float_type>::GetBase() const
67 {
68 return p1;
69 }
70
71 template<typename float_type>
72 Vector3<float_type> Line3<float_type>::GetDirection() const
73 {
74 return p2 - p1;
75 }
76
77 template<typename float_type>
78 void Line3<float_type>::Transform(const Base::Matrix4D& mat)
79 {
80 mat.multVec(p1, p1);
81 mat.multVec(p2, p2);
82 }
83
84 template<typename float_type>
85 void Line3<float_type>::Transform(const Base::Placement& plm)
86 {
87 plm.multVec(p1, p1);
88 plm.multVec(p2, p2);
89 }
90
91 template<typename float_type>
92 void Line3<float_type>::Transform(const Base::Rotation& rot)
93 {
94 rot.multVec(p1, p1);
95 rot.multVec(p2, p2);
96 }
97
98 template<typename float_type>
99 Line3<float_type> Line3<float_type>::Transformed(const Base::Matrix4D& mat) const
100 {
101 Line3<float_type> line(*this);
102 line.Transform(mat);
103 return line;
104 }
105
106 template<typename float_type>
107 Line3<float_type> Line3<float_type>::Transformed(const Base::Placement& plm) const
108 {
109 Line3<float_type> line(*this);
110 line.Transform(plm);
111 return line;
112 }
113
114 template<typename float_type>
115 Line3<float_type> Line3<float_type>::Transformed(const Base::Rotation& rot) const
116 {
117 Line3<float_type> line(*this);
118 line.Transform(rot);
119 return line;
120 }
121
122 template<typename float_type>
123 bool Line3<float_type>::Contains(const Vector3<float_type>& pt) const
124 {
125 return Contains(pt, Vector3<float_type>::epsilon());
126 }
127
128 template<typename float_type>
129 bool Line3<float_type>::Contains(const Vector3<float_type>& pt, float_type eps) const
130 {
131 Vector3<float_type> v1 = p1 - pt;
132 Vector3<float_type> v2 = p2 - pt;
133 Vector3<float_type> v3 = p2 - p1;
134 float_type dot = v1.Dot(v2);
135 if (dot > eps) {
136 return false;
137 }
138 return v3.Cross(v1).Length() < eps;
139 }
140
141 template<typename float_type>
142 Vector3<float_type> Line3<float_type>::FromPos(float_type distance) const
143 {
144 Vector3<float_type> dir(p2 - p1);
145 dir.Normalize();
146 return p1 + dir * distance;
147 }
148
149 // ----------------------------------------------------------------------------
150
151 template<typename float_type>
152 size_t Polygon3<float_type>::GetSize() const
153 {
154 return points.size();
155 }
156
157 template<typename float_type>
158 void Polygon3<float_type>::Add(const Vector3<float_type>& pnt)
159 {
160 points.push_back(pnt);
161 }
162
163 template<typename float_type>
164 const Vector3<float_type>& Polygon3<float_type>::operator[](size_t pos) const
165 {
166 return points[pos];
167 }
168
169 template<typename float_type>
170 const Vector3<float_type>& Polygon3<float_type>::At(size_t pos) const
171 {
172 return points.at(pos);
173 }
174
175 template<typename float_type>
176 Vector3<float_type>& Polygon3<float_type>::operator[](size_t pos)
177 {
178 return points[pos];
179 }
180
181 template<typename float_type>
182 Vector3<float_type>& Polygon3<float_type>::At(size_t pos)
183 {
184 return points.at(pos);
185 }
186
187 template<typename float_type>
188 bool Polygon3<float_type>::Remove(size_t pos)
189 {
190 if (pos < points.size()) {
191 auto it = points.begin();
192 std::advance(it, pos);
193 points.erase(it);
194 return true;
195 }
196
197 return false;
198 }
199
200 template<typename float_type>
201 void Polygon3<float_type>::Clear()
202 {
203 points.clear();
204 }
205
206 template<typename float_type>
207 BoundBox3<float_type> Polygon3<float_type>::CalcBoundBox() const
208 {
209 BoundBox3<float_type> box;
210 for (const auto& it : points) {
211 box.Add(it);
212 }
213 return box;
214 }
215
216 template<typename float_type>
217 void Polygon3<float_type>::Transform(const Base::Matrix4D& mat)
218 {
219 for (auto& it : points) {
220 mat.multVec(it, it);
221 }
222 }
223
224 template<typename float_type>
225 void Polygon3<float_type>::Transform(const Base::Placement& plm)
226 {
227 for (auto& it : points) {
228 plm.multVec(it, it);
229 }
230 }
231
232 template<typename float_type>
233 void Polygon3<float_type>::Transform(const Base::Rotation& rot)
234 {
235 for (auto& it : points) {
236 rot.multVec(it, it);
237 }
238 }
239
240 template<typename float_type>
241 Polygon3<float_type> Polygon3<float_type>::Transformed(const Base::Matrix4D& mat) const
242 {
243 Polygon3<float_type> poly(*this);
244 poly.Transform(mat);
245 return poly;
246 }
247
248 template<typename float_type>
249 Polygon3<float_type> Polygon3<float_type>::Transformed(const Base::Placement& plm) const
250 {
251 Polygon3<float_type> poly(*this);
252 poly.Transform(plm);
253 return poly;
254 }
255
256 template<typename float_type>
257 Polygon3<float_type> Polygon3<float_type>::Transformed(const Base::Rotation& rot) const
258 {
259 Polygon3<float_type> poly(*this);
260 poly.Transform(rot);
261 return poly;
262 }
263
264 // explicit template instantiation
265 namespace Base
266 {
267 template class BaseExport Line3<float>;
268 template class BaseExport Line3<double>;
269 template class BaseExport Polygon3<float>;
270 template class BaseExport Polygon3<double>;
271 } // namespace Base