From d2d2bd57fe88f8301167969a12d5a7d73f656c4d Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 4 Aug 2022 02:17:22 +0200 Subject: [PATCH] Base: implement Line3 Polygon3 class --- src/Base/CMakeLists.txt | 2 + src/Base/Tools3D.cpp | 210 ++++++++++++++++++++++++++++++++++++++++ src/Base/Tools3D.h | 135 ++++++++++++++++++++++++++ 3 files changed, 347 insertions(+) create mode 100644 src/Base/Tools3D.cpp create mode 100644 src/Base/Tools3D.h diff --git a/src/Base/CMakeLists.txt b/src/Base/CMakeLists.txt index f39a2fcd55..c51994e433 100644 --- a/src/Base/CMakeLists.txt +++ b/src/Base/CMakeLists.txt @@ -267,6 +267,7 @@ SET(FreeCADBase_CPP_SRCS TimeInfo.cpp Tools.cpp Tools2D.cpp + Tools3D.cpp Translate.cpp Type.cpp TypePyImp.cpp @@ -330,6 +331,7 @@ SET(FreeCADBase_HPP_SRCS TimeInfo.h Tools.h Tools2D.h + Tools3D.h Translate.h Type.h Uuid.h diff --git a/src/Base/Tools3D.cpp b/src/Base/Tools3D.cpp new file mode 100644 index 0000000000..8f2c4a4ed9 --- /dev/null +++ b/src/Base/Tools3D.cpp @@ -0,0 +1,210 @@ +/*************************************************************************** + * 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 "PreCompiled.h" + +#ifndef _PreComp_ +# include +# include +#endif + +#include "Tools3D.h" +#include "Vector3D.h" + +using namespace Base; + + +template +Line3::Line3(const Line3& line) + : p1(line.p1) + , p2(line.p2) +{ +} + +template +Line3::Line3(Line3&& line) +{ + *this = std::move(line); +} + +template +Line3::Line3(const Vector3& p1, const Vector3& p2) + : p1(p1) + , p2(p2) +{ +} + +template +Line3& Line3::operator= (const Line3& line) +{ + p1 = line.p1; + p2 = line.p2; + return *this; +} + +template +Line3& Line3::operator= (Line3&& line) +{ + p1 = std::move(line.p1); + p2 = std::move(line.p2); + return *this; +} + +template +bool Line3::operator== (const Line3& line) const +{ + return (p1 == line.p1 && p2 == line.p2); +} + +template +double Line3::Length() const +{ + return Base::Distance(p1, p2); +} + +template +double 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 +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& p) +{ + points.push_back(p); +} + +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; +} + +// explicit template instantiation +namespace Base { +template class BaseExport Line3; +template class BaseExport Line3; +template class BaseExport Polygon3; +template class BaseExport Polygon3; +} diff --git a/src/Base/Tools3D.h b/src/Base/Tools3D.h new file mode 100644 index 0000000000..b6511d6e15 --- /dev/null +++ b/src/Base/Tools3D.h @@ -0,0 +1,135 @@ +/*************************************************************************** + * 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 * + * * + ***************************************************************************/ + + +#ifndef BASE_TOOLS3D_H +#define BASE_TOOLS3D_H + + +#include +#include +#include +#include +#include +#include + +#include +#include +#ifndef FC_GLOBAL_H +#include +#endif + +namespace Base { + +class Vector2d; +class BoundBox2d; +class Line2d; +class Polygon2d; + +/** Line3 ********************************************/ + +/** + * 3D line class. + */ +template +class Line3 +{ +public: + Vector3 p1, p2; + + Line3() = default; + ~Line3() = default; + Line3(const Line3& line); + Line3(Line3&& line); + Line3(const Vector3& p1, const Vector3& p2); + + // operators + Line3& operator= (const Line3& line); + Line3& operator= (Line3&& line); + bool operator== (const Line3& line) const; + + // methods + double Length () const; + double SqrLength () const; + BoundBox3 CalcBoundBox() const; + Vector3 GetBase() const; + Vector3 GetDirection() const; + + /*! + * \brief Contains + * Checks if the point \a p is part of the line segment. + * \param p + * \return + */ + bool Contains(const Vector3& p) const; + /*! + * \brief Contains + * Checks if the distance of point \a p to the line segment is + * less than \a eps + * \param p + * \param eps + * \return + */ + bool Contains(const Vector3& p, float_type eps) const; + Vector3 FromPos (float_type distance) const; +}; + +/** Polygon3 ********************************************/ + +/** + * 3D polygon class. + */ +template +class Polygon3 +{ +public: + Polygon3() = default; + ~Polygon3() = default; + Polygon3(const Polygon3& poly) = default; + + Polygon3& operator = (const Polygon3& poly) = default; + + size_t GetSize() const; + void Add (const Vector3& p); + const Vector3& operator[] (size_t pos) const; + const Vector3& At (size_t pos) const; + Vector3& operator[] (size_t pos); + Vector3& At (size_t pos); + bool Remove(size_t pos); + void Clear(); + + // misc + BoundBox3 CalcBoundBox() const; + +private: + std::vector> points; +}; + +using Line3f = Line3; +using Line3d = Line3; + +using Polygon3f = Polygon3; +using Polygon3d = Polygon3; + +} // namespace Base + +#endif // BASE_TOOLS3D_H