Base: implement Line3 Polygon3 class
This commit is contained in:
@@ -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
|
||||
|
||||
210
src/Base/Tools3D.cpp
Normal file
210
src/Base/Tools3D.cpp
Normal file
@@ -0,0 +1,210 @@
|
||||
/***************************************************************************
|
||||
* Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> *
|
||||
* *
|
||||
* 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 <cstdlib>
|
||||
# include <set>
|
||||
#endif
|
||||
|
||||
#include "Tools3D.h"
|
||||
#include "Vector3D.h"
|
||||
|
||||
using namespace Base;
|
||||
|
||||
|
||||
template <typename float_type>
|
||||
Line3<float_type>::Line3(const Line3<float_type>& line)
|
||||
: p1(line.p1)
|
||||
, p2(line.p2)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
Line3<float_type>::Line3(Line3<float_type>&& line)
|
||||
{
|
||||
*this = std::move(line);
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
Line3<float_type>::Line3(const Vector3<float_type>& p1, const Vector3<float_type>& p2)
|
||||
: p1(p1)
|
||||
, p2(p2)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
Line3<float_type>& Line3<float_type>::operator= (const Line3<float_type>& line)
|
||||
{
|
||||
p1 = line.p1;
|
||||
p2 = line.p2;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
Line3<float_type>& Line3<float_type>::operator= (Line3<float_type>&& line)
|
||||
{
|
||||
p1 = std::move(line.p1);
|
||||
p2 = std::move(line.p2);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
bool Line3<float_type>::operator== (const Line3<float_type>& line) const
|
||||
{
|
||||
return (p1 == line.p1 && p2 == line.p2);
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
double Line3<float_type>::Length() const
|
||||
{
|
||||
return Base::Distance(p1, p2);
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
double Line3<float_type>::SqrLength() const
|
||||
{
|
||||
return Base::DistanceP2(p1, p2);
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
BoundBox3<float_type> Line3<float_type>::CalcBoundBox() const
|
||||
{
|
||||
BoundBox3<float_type> box;
|
||||
box.Add(p1);
|
||||
box.Add(p2);
|
||||
return box;
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
Vector3<float_type> Line3<float_type>::GetBase() const
|
||||
{
|
||||
return p1;
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
Vector3<float_type> Line3<float_type>::GetDirection() const
|
||||
{
|
||||
return p2 - p1;
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
bool Line3<float_type>::Contains(const Vector3<float_type>& pt) const
|
||||
{
|
||||
return Contains(pt, Vector3<float_type>::epsilon());
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
bool Line3<float_type>::Contains(const Vector3<float_type>& pt, float_type eps) const
|
||||
{
|
||||
Vector3<float_type> v1 = p1 - pt;
|
||||
Vector3<float_type> v2 = p2 - pt;
|
||||
Vector3<float_type> v3 = p2 - p1;
|
||||
float_type dot = v1.Dot(v2);
|
||||
if (dot > eps)
|
||||
return false;
|
||||
return v3.Cross(v1).Length() < eps;
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
Vector3<float_type> Line3<float_type>::FromPos(float_type distance) const
|
||||
{
|
||||
Vector3<float_type> dir(p2 - p1);
|
||||
dir.Normalize();
|
||||
return p1 + dir * distance;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
template <typename float_type>
|
||||
size_t Polygon3<float_type>::GetSize() const
|
||||
{
|
||||
return points.size();
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
void Polygon3<float_type>::Add (const Vector3<float_type>& p)
|
||||
{
|
||||
points.push_back(p);
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
const Vector3<float_type>& Polygon3<float_type>::operator[] (size_t pos) const
|
||||
{
|
||||
return points[pos];
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
const Vector3<float_type>& Polygon3<float_type>::At (size_t pos) const
|
||||
{
|
||||
return points.at(pos);
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
Vector3<float_type>& Polygon3<float_type>::operator[] (size_t pos)
|
||||
{
|
||||
return points[pos];
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
Vector3<float_type>& Polygon3<float_type>::At (size_t pos)
|
||||
{
|
||||
return points.at(pos);
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
bool Polygon3<float_type>::Remove(size_t pos)
|
||||
{
|
||||
if (pos < points.size()) {
|
||||
auto it = points.begin();
|
||||
std::advance(it, pos);
|
||||
points.erase(it);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
void Polygon3<float_type>::Clear()
|
||||
{
|
||||
points.clear();
|
||||
}
|
||||
|
||||
template <typename float_type>
|
||||
BoundBox3<float_type> Polygon3<float_type>::CalcBoundBox() const
|
||||
{
|
||||
BoundBox3<float_type> box;
|
||||
for (const auto& it : points)
|
||||
box.Add(it);
|
||||
return box;
|
||||
}
|
||||
|
||||
// explicit template instantiation
|
||||
namespace Base {
|
||||
template class BaseExport Line3<float>;
|
||||
template class BaseExport Line3<double>;
|
||||
template class BaseExport Polygon3<float>;
|
||||
template class BaseExport Polygon3<double>;
|
||||
}
|
||||
135
src/Base/Tools3D.h
Normal file
135
src/Base/Tools3D.h
Normal file
@@ -0,0 +1,135 @@
|
||||
/***************************************************************************
|
||||
* Copyright (c) 2022 Werner Mayer <wmayer[at]users.sourceforge.net> *
|
||||
* *
|
||||
* 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 <algorithm>
|
||||
#include <cmath>
|
||||
#include <cfloat>
|
||||
#include <cstdio>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
|
||||
#include <Base/BoundBox.h>
|
||||
#include <Base/Vector3D.h>
|
||||
#ifndef FC_GLOBAL_H
|
||||
#include <FCGlobal.h>
|
||||
#endif
|
||||
|
||||
namespace Base {
|
||||
|
||||
class Vector2d;
|
||||
class BoundBox2d;
|
||||
class Line2d;
|
||||
class Polygon2d;
|
||||
|
||||
/** Line3 ********************************************/
|
||||
|
||||
/**
|
||||
* 3D line class.
|
||||
*/
|
||||
template <class float_type>
|
||||
class Line3
|
||||
{
|
||||
public:
|
||||
Vector3<float_type> p1, p2;
|
||||
|
||||
Line3() = default;
|
||||
~Line3() = default;
|
||||
Line3(const Line3<float_type>& line);
|
||||
Line3(Line3<float_type>&& line);
|
||||
Line3(const Vector3<float_type>& p1, const Vector3<float_type>& p2);
|
||||
|
||||
// operators
|
||||
Line3& operator= (const Line3<float_type>& line);
|
||||
Line3& operator= (Line3<float_type>&& line);
|
||||
bool operator== (const Line3<float_type>& line) const;
|
||||
|
||||
// methods
|
||||
double Length () const;
|
||||
double SqrLength () const;
|
||||
BoundBox3<float_type> CalcBoundBox() const;
|
||||
Vector3<float_type> GetBase() const;
|
||||
Vector3<float_type> GetDirection() const;
|
||||
|
||||
/*!
|
||||
* \brief Contains
|
||||
* Checks if the point \a p is part of the line segment.
|
||||
* \param p
|
||||
* \return
|
||||
*/
|
||||
bool Contains(const Vector3<float_type>& 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<float_type>& p, float_type eps) const;
|
||||
Vector3<float_type> FromPos (float_type distance) const;
|
||||
};
|
||||
|
||||
/** Polygon3 ********************************************/
|
||||
|
||||
/**
|
||||
* 3D polygon class.
|
||||
*/
|
||||
template <class float_type>
|
||||
class Polygon3
|
||||
{
|
||||
public:
|
||||
Polygon3() = default;
|
||||
~Polygon3() = default;
|
||||
Polygon3(const Polygon3<float_type>& poly) = default;
|
||||
|
||||
Polygon3& operator = (const Polygon3<float_type>& poly) = default;
|
||||
|
||||
size_t GetSize() const;
|
||||
void Add (const Vector3<float_type>& p);
|
||||
const Vector3<float_type>& operator[] (size_t pos) const;
|
||||
const Vector3<float_type>& At (size_t pos) const;
|
||||
Vector3<float_type>& operator[] (size_t pos);
|
||||
Vector3<float_type>& At (size_t pos);
|
||||
bool Remove(size_t pos);
|
||||
void Clear();
|
||||
|
||||
// misc
|
||||
BoundBox3<float_type> CalcBoundBox() const;
|
||||
|
||||
private:
|
||||
std::vector<Vector3<float_type>> points;
|
||||
};
|
||||
|
||||
using Line3f = Line3<float>;
|
||||
using Line3d = Line3<double>;
|
||||
|
||||
using Polygon3f = Polygon3<float>;
|
||||
using Polygon3d = Polygon3<double>;
|
||||
|
||||
} // namespace Base
|
||||
|
||||
#endif // BASE_TOOLS3D_H
|
||||
Reference in New Issue
Block a user