1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
* Copyright (C) 1992-2016 KiCad Developers, see AUTHORS.txt for contributors.
*
* 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
*/
/**
* @file ccontainer2d.h
* @brief
*/
#ifndef _CCONTAINER2D_H_
#define _CCONTAINER2D_H_
#include "../shapes2D/cobject2d.h"
#include <list>
#include <mutex>
typedef std::list<COBJECT2D *> LIST_OBJECT2D;
typedef std::list<const COBJECT2D *> CONST_LIST_OBJECT2D;
class CGENERICCONTAINER2D
{
protected:
CBBOX2D m_bbox;
LIST_OBJECT2D m_objects;
public:
explicit CGENERICCONTAINER2D( OBJECT2D_TYPE aObjType );
virtual ~CGENERICCONTAINER2D();
void Add( COBJECT2D *aObject )
{
if( aObject ) // Only add if it is a valid pointer
{
std::lock_guard<std::mutex> lock( m_lock );
m_objects.push_back( aObject );
m_bbox.Union( aObject->GetBBox() );
}
}
void Clear();
const LIST_OBJECT2D &GetList() const { return m_objects; }
/**
* @brief GetListObjectsIntersects - Get a list of objects that intersects a bbox
* @param aBBox - a bbox to make the query
* @param aOutList - A list of objects that intersects the bbox
*/
virtual void GetListObjectsIntersects( const CBBOX2D & aBBox,
CONST_LIST_OBJECT2D &aOutList ) const = 0;
private:
std::mutex m_lock;
};
class CCONTAINER2D : public CGENERICCONTAINER2D
{
public:
CCONTAINER2D();
// Imported from CGENERICCONTAINER2D
void GetListObjectsIntersects( const CBBOX2D & aBBox,
CONST_LIST_OBJECT2D &aOutList ) const override;
};
struct BVH_CONTAINER_NODE_2D
{
CBBOX2D m_BBox;
BVH_CONTAINER_NODE_2D *m_Children[2];
/// Store the list of objects if that node is a Leaf
CONST_LIST_OBJECT2D m_LeafList;
};
class CBVHCONTAINER2D : public CGENERICCONTAINER2D
{
public:
CBVHCONTAINER2D();
~CBVHCONTAINER2D();
void BuildBVH();
private:
bool m_isInitialized;
std::list<BVH_CONTAINER_NODE_2D *> m_elements_to_delete;
BVH_CONTAINER_NODE_2D *m_Tree;
void destroy();
void recursiveBuild_MIDDLE_SPLIT( BVH_CONTAINER_NODE_2D *aNodeParent );
void recursiveGetListObjectsIntersects( const BVH_CONTAINER_NODE_2D *aNode,
const CBBOX2D & aBBox,
CONST_LIST_OBJECT2D &aOutList ) const;
public:
// Imported from CGENERICCONTAINER2D
void GetListObjectsIntersects( const CBBOX2D & aBBox,
CONST_LIST_OBJECT2D &aOutList ) const override;
};
#endif // _CCONTAINER2D_H_
|