KiCad PCB EDA Suite
ccontainer2d.h
Go to the documentation of this file.
1 /*
2  * This program source code file is part of KiCad, a free EDA CAD application.
3  *
4  * Copyright (C) 2015-2020 Mario Luzeiro <mrluzeiro@ua.pt>
5  * Copyright (C) 1992-2020 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
30 #ifndef _CCONTAINER2D_H_
31 #define _CCONTAINER2D_H_
32 
33 #include "../shapes2D/cobject2d.h"
34 #include <list>
35 #include <mutex>
36 
37 typedef std::list<COBJECT2D *> LIST_OBJECT2D;
38 typedef std::list<const COBJECT2D *> CONST_LIST_OBJECT2D;
39 
40 
42 {
43 protected:
46 
47 public:
48  explicit CGENERICCONTAINER2D( OBJECT2D_TYPE aObjType );
49 
50  virtual ~CGENERICCONTAINER2D();
51 
52  void Add( COBJECT2D *aObject )
53  {
54  if( aObject ) // Only add if it is a valid pointer
55  {
56  std::lock_guard<std::mutex> lock( m_lock );
57  m_objects.push_back( aObject );
58  m_bbox.Union( aObject->GetBBox() );
59  }
60  }
61 
62  const CBBOX2D &GetBBox() const
63  {
64  return m_bbox;
65  }
66 
67  virtual void Clear();
68 
69  const LIST_OBJECT2D &GetList() const { return m_objects; }
70 
76  virtual void GetListObjectsIntersects( const CBBOX2D & aBBox,
77  CONST_LIST_OBJECT2D &aOutList ) const = 0;
78 
84  virtual bool IntersectAny( const RAYSEG2D &aSegRay ) const = 0;
85 
86 private:
87  std::mutex m_lock;
88 };
89 
90 
92 {
93 public:
94  CCONTAINER2D();
95 
96  // Imported from CGENERICCONTAINER2D
97  void GetListObjectsIntersects( const CBBOX2D & aBBox,
98  CONST_LIST_OBJECT2D &aOutList ) const override;
99 
100  bool IntersectAny( const RAYSEG2D &aSegRay ) const override;
101 };
102 
103 
105 {
108 
111 };
112 
113 
115 {
116 public:
117  CBVHCONTAINER2D();
119 
120  void BuildBVH();
121 
122  void Clear() override;
123 
124 private:
126  std::list<BVH_CONTAINER_NODE_2D *> m_elements_to_delete;
128 
129  void destroy();
132  const CBBOX2D & aBBox,
133  CONST_LIST_OBJECT2D &aOutList ) const;
134  bool recursiveIntersectAny( const BVH_CONTAINER_NODE_2D *aNode,
135  const RAYSEG2D &aSegRay ) const;
136 
137 public:
138 
139  // Imported from CGENERICCONTAINER2D
140  void GetListObjectsIntersects( const CBBOX2D & aBBox,
141  CONST_LIST_OBJECT2D &aOutList ) const override;
142 
143  bool IntersectAny( const RAYSEG2D &aSegRay ) const override;
144 };
145 
146 #endif // _CCONTAINER2D_H_
void GetListObjectsIntersects(const CBBOX2D &aBBox, CONST_LIST_OBJECT2D &aOutList) const override
GetListObjectsIntersects - Get a list of objects that intersects a bbox.
const CBBOX2D & GetBBox() const
Definition: ccontainer2d.h:62
CONST_LIST_OBJECT2D m_LeafList
Store the list of objects if that node is a Leaf.
Definition: ccontainer2d.h:110
void Union(const SFVEC2F &aPoint)
Function Union recalculate the bounding box adding a point.
Definition: cbbox2d.cpp:94
bool IntersectAny(const RAYSEG2D &aSegRay) const override
IntersectAny - Intersect and check if a segment ray hits a object or is inside it.
bool recursiveIntersectAny(const BVH_CONTAINER_NODE_2D *aNode, const RAYSEG2D &aSegRay) const
CBBOX manages a bounding box defined by two SFVEC2F min max points.
Definition: cbbox2d.h:40
std::list< const COBJECT2D * > CONST_LIST_OBJECT2D
Definition: ccontainer2d.h:38
std::list< BVH_CONTAINER_NODE_2D * > m_elements_to_delete
Definition: ccontainer2d.h:126
virtual void GetListObjectsIntersects(const CBBOX2D &aBBox, CONST_LIST_OBJECT2D &aOutList) const =0
GetListObjectsIntersects - Get a list of objects that intersects a bbox.
BVH_CONTAINER_NODE_2D * m_Children[2]
Definition: ccontainer2d.h:107
void recursiveGetListObjectsIntersects(const BVH_CONTAINER_NODE_2D *aNode, const CBBOX2D &aBBox, CONST_LIST_OBJECT2D &aOutList) const
void recursiveBuild_MIDDLE_SPLIT(BVH_CONTAINER_NODE_2D *aNodeParent)
bool IntersectAny(const RAYSEG2D &aSegRay) const override
IntersectAny - Intersect and check if a segment ray hits a object or is inside it.
virtual void Clear()
virtual ~CGENERICCONTAINER2D()
void Add(COBJECT2D *aObject)
Definition: ccontainer2d.h:52
virtual bool IntersectAny(const RAYSEG2D &aSegRay) const =0
IntersectAny - Intersect and check if a segment ray hits a object or is inside it.
CGENERICCONTAINER2D(OBJECT2D_TYPE aObjType)
std::list< COBJECT2D * > LIST_OBJECT2D
Definition: ccontainer2d.h:37
LIST_OBJECT2D m_objects
Definition: ccontainer2d.h:45
OBJECT2D_TYPE
Definition: cobject2d.h:46
const CBBOX2D & GetBBox() const
Definition: cobject2d.h:121
BVH_CONTAINER_NODE_2D * m_Tree
Definition: ccontainer2d.h:127
const LIST_OBJECT2D & GetList() const
Definition: ccontainer2d.h:69
void Clear() override
void GetListObjectsIntersects(const CBBOX2D &aBBox, CONST_LIST_OBJECT2D &aOutList) const override
GetListObjectsIntersects - Get a list of objects that intersects a bbox.
Definition: ray.h:110