2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2020-11-11 11:02:23 +00:00
|
|
|
* Copyright (C) 2015-2020 Mario Luzeiro <mrluzeiro@ua.pt>
|
2020-12-12 17:29:11 +00:00
|
|
|
* Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2015-12-08 07:31:57 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, you may find one here:
|
|
|
|
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
|
|
|
* or you may search the http://www.gnu.org website for the version 2 license,
|
|
|
|
* or you may write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2021-01-03 22:23:00 +00:00
|
|
|
* @file container_2d.h
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
#ifndef _CONTAINER_2D_H_
|
|
|
|
#define _CONTAINER_2D_H_
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "../shapes2D/object_2d.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
#include <list>
|
2018-09-21 04:23:15 +00:00
|
|
|
#include <mutex>
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-06 08:29:03 +00:00
|
|
|
struct RAYSEG2D;
|
2021-01-05 22:19:00 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
typedef std::list<OBJECT_2D*> LIST_OBJECT2D;
|
|
|
|
typedef std::list<const OBJECT_2D*> CONST_LIST_OBJECT2D;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
class CONTAINER_2D_BASE
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-01-02 21:05:29 +00:00
|
|
|
explicit CONTAINER_2D_BASE( OBJECT_2D_TYPE aObjType );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
virtual ~CONTAINER_2D_BASE();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void Add( OBJECT_2D* aObject )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
if( aObject )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock( m_lock );
|
2015-12-08 07:31:57 +00:00
|
|
|
m_objects.push_back( aObject );
|
|
|
|
m_bbox.Union( aObject->GetBBox() );
|
|
|
|
}
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const BBOX_2D& GetBBox() const
|
2020-10-29 12:48:45 +00:00
|
|
|
{
|
|
|
|
return m_bbox;
|
|
|
|
}
|
|
|
|
|
2020-11-11 09:42:14 +00:00
|
|
|
virtual void Clear();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const LIST_OBJECT2D& GetList() const { return m_objects; }
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
/**
|
2020-12-12 17:29:11 +00:00
|
|
|
* Get a list of objects that intersects a bounding box.
|
|
|
|
*
|
|
|
|
* @param aBBox The bounding box to test.
|
|
|
|
* @param aOutList The list of objects that intersects the bounding box.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2021-08-19 10:39:29 +00:00
|
|
|
virtual void GetIntersectingObjects( const BBOX_2D& aBBox,
|
|
|
|
CONST_LIST_OBJECT2D& aOutList ) const = 0;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-11-04 09:36:41 +00:00
|
|
|
/**
|
2020-12-12 17:29:11 +00:00
|
|
|
* Intersect and check if a segment ray hits a object or is inside it.
|
|
|
|
*
|
|
|
|
* @param aSegRay The segment to intersect with objects.
|
|
|
|
* @return true if it hits any of the objects or is inside any object.
|
2020-11-04 09:36:41 +00:00
|
|
|
*/
|
2021-01-02 21:05:29 +00:00
|
|
|
virtual bool IntersectAny( const RAYSEG2D& aSegRay ) const = 0;
|
2020-11-04 09:36:41 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
protected:
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_2D m_bbox;
|
2020-12-12 17:29:11 +00:00
|
|
|
LIST_OBJECT2D m_objects;
|
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
private:
|
2018-09-21 04:23:15 +00:00
|
|
|
std::mutex m_lock;
|
2015-12-08 07:31:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
class CONTAINER_2D : public CONTAINER_2D_BASE
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-01-02 21:05:29 +00:00
|
|
|
CONTAINER_2D();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-08-19 10:39:29 +00:00
|
|
|
void GetIntersectingObjects( const BBOX_2D& aBBox,
|
|
|
|
CONST_LIST_OBJECT2D& aOutList ) const override;
|
2020-11-04 09:36:41 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool IntersectAny( const RAYSEG2D& aSegRay ) const override;
|
2015-12-08 07:31:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct BVH_CONTAINER_NODE_2D
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_2D m_BBox;
|
|
|
|
BVH_CONTAINER_NODE_2D* m_Children[2];
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
/// Store the list of objects if that node is a Leaf
|
|
|
|
CONST_LIST_OBJECT2D m_LeafList;
|
2015-12-08 07:31:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
class BVH_CONTAINER_2D : public CONTAINER_2D_BASE
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
public:
|
2021-01-02 21:05:29 +00:00
|
|
|
BVH_CONTAINER_2D();
|
|
|
|
~BVH_CONTAINER_2D();
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
void BuildBVH();
|
|
|
|
|
2020-11-11 11:02:23 +00:00
|
|
|
void Clear() override;
|
|
|
|
|
2021-08-19 10:39:29 +00:00
|
|
|
void GetIntersectingObjects( const BBOX_2D& aBBox,
|
|
|
|
CONST_LIST_OBJECT2D& aOutList ) const override;
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool IntersectAny( const RAYSEG2D& aSegRay ) const override;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
private:
|
2015-12-08 07:31:57 +00:00
|
|
|
void destroy();
|
2021-01-02 21:05:29 +00:00
|
|
|
void recursiveBuild_MIDDLE_SPLIT( BVH_CONTAINER_NODE_2D* aNodeParent );
|
|
|
|
void recursiveGetListObjectsIntersects( const BVH_CONTAINER_NODE_2D* aNode,
|
|
|
|
const BBOX_2D& aBBox,
|
|
|
|
CONST_LIST_OBJECT2D& aOutList ) const;
|
|
|
|
bool recursiveIntersectAny( const BVH_CONTAINER_NODE_2D* aNode,
|
|
|
|
const RAYSEG2D& aSegRay ) const;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
bool m_isInitialized;
|
2021-01-07 19:33:43 +00:00
|
|
|
std::list<BVH_CONTAINER_NODE_2D*> m_elementsToDelete;
|
|
|
|
BVH_CONTAINER_NODE_2D* m_tree;
|
2020-11-04 09:36:41 +00:00
|
|
|
|
2015-12-08 07:31:57 +00:00
|
|
|
};
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
#endif // _CONTAINER_2D_H_
|