// SPDX-License-Identifier: LGPL-2.1-or-later /*************************************************************************** * Copyright (c) 2022 Werner Mayer * * * * This file is part of the FreeCAD CAx development system. * * * * This library is free software; you can redistribute it and/or * * modify it under the terms of the GNU Library General Public * * License as published by the Free Software Foundation; either * * version 2 of the License, or (at your option) any later version. * * * * This library is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU Library General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this library; see the file COPYING.LIB. If not, * * write to the Free Software Foundation, Inc., 59 Temple Place, * * Suite 330, Boston, MA 02111-1307, USA * * * ***************************************************************************/ #include "BoundBox.h" #include "Placement.h" #include "Tools3D.h" using namespace Base; template Line3::Line3(const Vector3& p1, const Vector3& p2) : p1(p1) , p2(p2) {} template bool Line3::operator==(const Line3& line) const { return (p1 == line.p1 && p2 == line.p2); } template float_type Line3::Length() const { return Base::Distance(p1, p2); } template float_type Line3::SqrLength() const { return Base::DistanceP2(p1, p2); } template BoundBox3 Line3::CalcBoundBox() const { BoundBox3 box; box.Add(p1); box.Add(p2); return box; } template Vector3 Line3::GetBase() const { return p1; } template Vector3 Line3::GetDirection() const { return p2 - p1; } template void Line3::Transform(const Base::Matrix4D& mat) { mat.multVec(p1, p1); mat.multVec(p2, p2); } template void Line3::Transform(const Base::Placement& plm) { plm.multVec(p1, p1); plm.multVec(p2, p2); } template void Line3::Transform(const Base::Rotation& rot) { rot.multVec(p1, p1); rot.multVec(p2, p2); } template Line3 Line3::Transformed(const Base::Matrix4D& mat) const { Line3 line(*this); line.Transform(mat); return line; } template Line3 Line3::Transformed(const Base::Placement& plm) const { Line3 line(*this); line.Transform(plm); return line; } template Line3 Line3::Transformed(const Base::Rotation& rot) const { Line3 line(*this); line.Transform(rot); return line; } template bool Line3::Contains(const Vector3& pt) const { return Contains(pt, Vector3::epsilon()); } template bool Line3::Contains(const Vector3& pt, float_type eps) const { Vector3 v1 = p1 - pt; Vector3 v2 = p2 - pt; Vector3 v3 = p2 - p1; float_type dot = v1.Dot(v2); if (dot > eps) { return false; } return v3.Cross(v1).Length() < eps; } template Vector3 Line3::FromPos(float_type distance) const { Vector3 dir(p2 - p1); dir.Normalize(); return p1 + dir * distance; } // ---------------------------------------------------------------------------- template size_t Polygon3::GetSize() const { return points.size(); } template void Polygon3::Add(const Vector3& pnt) { points.push_back(pnt); } template const Vector3& Polygon3::operator[](size_t pos) const { return points[pos]; } template const Vector3& Polygon3::At(size_t pos) const { return points.at(pos); } template Vector3& Polygon3::operator[](size_t pos) { return points[pos]; } template Vector3& Polygon3::At(size_t pos) { return points.at(pos); } template bool Polygon3::Remove(size_t pos) { if (pos < points.size()) { auto it = points.begin(); std::advance(it, pos); points.erase(it); return true; } return false; } template void Polygon3::Clear() { points.clear(); } template BoundBox3 Polygon3::CalcBoundBox() const { BoundBox3 box; for (const auto& it : points) { box.Add(it); } return box; } template void Polygon3::Transform(const Base::Matrix4D& mat) { for (auto& it : points) { mat.multVec(it, it); } } template void Polygon3::Transform(const Base::Placement& plm) { for (auto& it : points) { plm.multVec(it, it); } } template void Polygon3::Transform(const Base::Rotation& rot) { for (auto& it : points) { rot.multVec(it, it); } } template Polygon3 Polygon3::Transformed(const Base::Matrix4D& mat) const { Polygon3 poly(*this); poly.Transform(mat); return poly; } template Polygon3 Polygon3::Transformed(const Base::Placement& plm) const { Polygon3 poly(*this); poly.Transform(plm); return poly; } template Polygon3 Polygon3::Transformed(const Base::Rotation& rot) const { Polygon3 poly(*this); poly.Transform(rot); return poly; } // explicit template instantiation namespace Base { template class BaseExport Line3; template class BaseExport Line3; template class BaseExport Polygon3; template class BaseExport Polygon3; } // namespace Base