2016-07-19 17:35:25 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015-2016 Mario Luzeiro <mrluzeiro@ua.pt>
|
2020-12-12 17:29:11 +00:00
|
|
|
* Copyright (C) 2015-2020 KiCad Developers, see AUTHORS.txt for contributors.
|
2016-07-19 17:35:25 +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.cpp
|
2016-07-19 17:35:25 +00:00
|
|
|
*/
|
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "container_2d.h"
|
2021-01-05 22:19:00 +00:00
|
|
|
#include "../ray.h"
|
2016-07-19 17:35:25 +00:00
|
|
|
#include <boost/range/algorithm/partition.hpp>
|
|
|
|
#include <boost/range/algorithm/nth_element.hpp>
|
|
|
|
#include <wx/debug.h>
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
CONTAINER_2D_BASE::CONTAINER_2D_BASE( OBJECT_2D_TYPE aObjType )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
m_bbox.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void CONTAINER_2D_BASE::Clear()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2018-09-21 04:23:15 +00:00
|
|
|
std::lock_guard<std::mutex> lock( m_lock );
|
2016-07-19 17:35:25 +00:00
|
|
|
m_bbox.Reset();
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
for( LIST_OBJECT2D::iterator ii = m_objects.begin(); ii != m_objects.end(); ++ii )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
delete *ii;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_objects.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
CONTAINER_2D_BASE::~CONTAINER_2D_BASE()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
CONTAINER_2D::CONTAINER_2D() : CONTAINER_2D_BASE( OBJECT_2D_TYPE::CONTAINER )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-19 10:39:29 +00:00
|
|
|
void CONTAINER_2D::GetIntersectingObjects( const BBOX_2D& aBBox,
|
|
|
|
CONST_LIST_OBJECT2D& aOutList ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
/// @todo Determine what to do with this code.
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool CONTAINER_2D::IntersectAny( const RAYSEG2D& aSegRay ) const
|
2020-11-04 09:36:41 +00:00
|
|
|
{
|
2020-12-12 17:29:11 +00:00
|
|
|
/// @todo Determine what what needs done because someone wrote TODO here.
|
2020-11-04 09:36:41 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVH_CONTAINER_2D::BVH_CONTAINER_2D() : CONTAINER_2D_BASE( OBJECT_2D_TYPE::BVHCONTAINER )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
m_isInitialized = false;
|
|
|
|
m_bbox.Reset();
|
2021-01-07 19:33:43 +00:00
|
|
|
m_elementsToDelete.clear();
|
|
|
|
m_tree = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BVH_CONTAINER_2D::Clear()
|
2020-11-11 09:42:14 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
CONTAINER_2D_BASE::Clear();
|
2020-11-11 09:42:14 +00:00
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BVH_CONTAINER_2D::destroy()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
for( std::list<BVH_CONTAINER_NODE_2D*>::iterator ii = m_elementsToDelete.begin();
|
|
|
|
ii != m_elementsToDelete.end();
|
2016-07-19 17:35:25 +00:00
|
|
|
++ii )
|
|
|
|
{
|
|
|
|
delete *ii;
|
|
|
|
}
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_elementsToDelete.clear();
|
|
|
|
m_tree = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
m_isInitialized = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BVH_CONTAINER_2D::~BVH_CONTAINER_2D()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#define BVH_CONTAINER2D_MAX_OBJ_PER_LEAF 4
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BVH_CONTAINER_2D::BuildBVH()
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
if( m_isInitialized )
|
|
|
|
destroy();
|
|
|
|
|
2020-11-09 13:19:12 +00:00
|
|
|
m_isInitialized = true;
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
if( m_objects.empty() )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_tree = new BVH_CONTAINER_NODE_2D;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
m_elementsToDelete.push_back( m_tree );
|
|
|
|
m_tree->m_BBox = m_bbox;
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
for( LIST_OBJECT2D::const_iterator ii = m_objects.begin(); ii != m_objects.end(); ++ii )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
m_tree->m_LeafList.push_back( static_cast<const OBJECT_2D*>( *ii ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
recursiveBuild_MIDDLE_SPLIT( m_tree );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Based on a blog post by VADIM KRAVCENKO
|
|
|
|
// http://www.vadimkravcenko.com/bvh-tree-building
|
|
|
|
// Implements:
|
|
|
|
|
|
|
|
// "Split in the middle of the longest Axis"
|
|
|
|
// "Creates a binary tree with Top-Down approach.
|
|
|
|
// Fastest BVH building, but least [speed] accuracy."
|
2021-01-07 19:33:43 +00:00
|
|
|
static bool sortByCentroidX( const OBJECT_2D* a, const OBJECT_2D* b )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
return a->GetCentroid()[0] < b->GetCentroid()[0];
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
static bool sortByCentroidY( const OBJECT_2D* a, const OBJECT_2D* b )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
return a->GetCentroid()[0] < b->GetCentroid()[0];
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
static bool sortByCentroidZ( const OBJECT_2D* a, const OBJECT_2D* b )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
return a->GetCentroid()[0] < b->GetCentroid()[0];
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BVH_CONTAINER_2D::recursiveBuild_MIDDLE_SPLIT( BVH_CONTAINER_NODE_2D* aNodeParent )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( aNodeParent != nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
wxASSERT( aNodeParent->m_BBox.IsInitialized() == true );
|
|
|
|
wxASSERT( aNodeParent->m_LeafList.size() > 0 );
|
|
|
|
|
|
|
|
if( aNodeParent->m_LeafList.size() > BVH_CONTAINER2D_MAX_OBJ_PER_LEAF )
|
|
|
|
{
|
|
|
|
// Create Leaf Nodes
|
2021-01-02 21:05:29 +00:00
|
|
|
BVH_CONTAINER_NODE_2D* leftNode = new BVH_CONTAINER_NODE_2D;
|
|
|
|
BVH_CONTAINER_NODE_2D* rightNode = new BVH_CONTAINER_NODE_2D;
|
2021-01-07 19:33:43 +00:00
|
|
|
m_elementsToDelete.push_back( leftNode );
|
|
|
|
m_elementsToDelete.push_back( rightNode );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
leftNode->m_BBox.Reset();
|
|
|
|
rightNode->m_BBox.Reset();
|
|
|
|
leftNode->m_LeafList.clear();
|
|
|
|
rightNode->m_LeafList.clear();
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
// Decide which axis to split
|
2016-07-19 17:35:25 +00:00
|
|
|
const unsigned int axis_to_split = aNodeParent->m_BBox.MaxDimension();
|
|
|
|
|
|
|
|
// Divide the objects
|
|
|
|
switch( axis_to_split )
|
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
case 0: aNodeParent->m_LeafList.sort( sortByCentroidX ); break;
|
|
|
|
case 1: aNodeParent->m_LeafList.sort( sortByCentroidY ); break;
|
|
|
|
case 2: aNodeParent->m_LeafList.sort( sortByCentroidZ ); break;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsigned int i = 0;
|
|
|
|
|
|
|
|
for( CONST_LIST_OBJECT2D::const_iterator ii = aNodeParent->m_LeafList.begin();
|
|
|
|
ii != aNodeParent->m_LeafList.end();
|
|
|
|
++ii )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const OBJECT_2D* object = static_cast<const OBJECT_2D*>( *ii );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( i < (aNodeParent->m_LeafList.size() / 2 ) )
|
|
|
|
{
|
|
|
|
leftNode->m_BBox.Union( object->GetBBox() );
|
|
|
|
leftNode->m_LeafList.push_back( object );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
rightNode->m_BBox.Union( object->GetBBox() );
|
|
|
|
rightNode->m_LeafList.push_back( object );
|
|
|
|
}
|
|
|
|
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxASSERT( leftNode->m_LeafList.size() > 0 );
|
|
|
|
wxASSERT( rightNode->m_LeafList.size() > 0 );
|
|
|
|
wxASSERT( ( leftNode->m_LeafList.size() + rightNode->m_LeafList.size() ) ==
|
|
|
|
aNodeParent->m_LeafList.size() );
|
|
|
|
|
|
|
|
aNodeParent->m_Children[0] = leftNode;
|
|
|
|
aNodeParent->m_Children[1] = rightNode;
|
|
|
|
aNodeParent->m_LeafList.clear();
|
|
|
|
|
|
|
|
recursiveBuild_MIDDLE_SPLIT( leftNode );
|
|
|
|
recursiveBuild_MIDDLE_SPLIT( rightNode );
|
2020-11-11 09:42:14 +00:00
|
|
|
|
|
|
|
wxASSERT( aNodeParent->m_LeafList.size() == 0 );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// It is a Leaf
|
2021-01-02 21:05:29 +00:00
|
|
|
aNodeParent->m_Children[0] = nullptr;
|
|
|
|
aNodeParent->m_Children[1] = nullptr;
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
2020-11-11 09:42:14 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( aNodeParent != nullptr );
|
2020-11-11 09:42:14 +00:00
|
|
|
wxASSERT( aNodeParent->m_BBox.IsInitialized() == true );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BVH_CONTAINER_2D::IntersectAny( const RAYSEG2D& aSegRay ) const
|
2020-11-04 09:36:41 +00:00
|
|
|
{
|
|
|
|
wxASSERT( m_isInitialized == true );
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( m_tree )
|
|
|
|
return recursiveIntersectAny( m_tree, aSegRay );
|
2020-11-04 09:36:41 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BVH_CONTAINER_2D::recursiveIntersectAny( const BVH_CONTAINER_NODE_2D* aNode,
|
|
|
|
const RAYSEG2D& aSegRay ) const
|
2020-11-04 09:36:41 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( aNode != nullptr );
|
2020-11-04 09:36:41 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
if( aNode->m_BBox.Inside( aSegRay.m_Start ) || aNode->m_BBox.Inside( aSegRay.m_End ) ||
|
2020-11-04 09:36:41 +00:00
|
|
|
aNode->m_BBox.Intersect( aSegRay ) )
|
|
|
|
{
|
|
|
|
if( !aNode->m_LeafList.empty() )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( aNode->m_Children[0] == nullptr );
|
|
|
|
wxASSERT( aNode->m_Children[1] == nullptr );
|
2020-11-04 09:36:41 +00:00
|
|
|
|
|
|
|
// Leaf
|
2021-01-02 21:05:29 +00:00
|
|
|
for( const OBJECT_2D* obj : aNode->m_LeafList )
|
2020-11-04 09:36:41 +00:00
|
|
|
{
|
|
|
|
if( obj->IsPointInside( aSegRay.m_Start ) ||
|
|
|
|
obj->IsPointInside( aSegRay.m_End ) ||
|
|
|
|
obj->Intersect( aSegRay, nullptr, nullptr ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( aNode->m_Children[0] != nullptr );
|
|
|
|
wxASSERT( aNode->m_Children[1] != nullptr );
|
2020-11-04 09:36:41 +00:00
|
|
|
|
|
|
|
// Node
|
|
|
|
if( recursiveIntersectAny( aNode->m_Children[0], aSegRay ) )
|
|
|
|
return true;
|
2021-01-07 19:33:43 +00:00
|
|
|
|
2020-11-04 09:36:41 +00:00
|
|
|
if( recursiveIntersectAny( aNode->m_Children[1], aSegRay ) )
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:29:11 +00:00
|
|
|
|
2021-08-19 10:39:29 +00:00
|
|
|
void BVH_CONTAINER_2D::GetIntersectingObjects( const BBOX_2D& aBBox,
|
|
|
|
CONST_LIST_OBJECT2D& aOutList ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
wxASSERT( aBBox.IsInitialized() == true );
|
|
|
|
wxASSERT( m_isInitialized == true );
|
|
|
|
|
|
|
|
aOutList.clear();
|
|
|
|
|
2021-01-07 19:33:43 +00:00
|
|
|
if( m_tree )
|
|
|
|
recursiveGetListObjectsIntersects( m_tree, aBBox, aOutList );
|
2016-07-19 17:35:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BVH_CONTAINER_2D::recursiveGetListObjectsIntersects( const BVH_CONTAINER_NODE_2D* aNode,
|
|
|
|
const BBOX_2D& aBBox,
|
|
|
|
CONST_LIST_OBJECT2D& aOutList ) const
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( aNode != nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
wxASSERT( aBBox.IsInitialized() == true );
|
|
|
|
|
|
|
|
if( aNode->m_BBox.Intersects( aBBox ) )
|
|
|
|
{
|
|
|
|
if( !aNode->m_LeafList.empty() )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( aNode->m_Children[0] == nullptr );
|
|
|
|
wxASSERT( aNode->m_Children[1] == nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Leaf
|
|
|
|
for( CONST_LIST_OBJECT2D::const_iterator ii = aNode->m_LeafList.begin();
|
|
|
|
ii != aNode->m_LeafList.end();
|
|
|
|
++ii )
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
const OBJECT_2D* obj = static_cast<const OBJECT_2D*>( *ii );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
if( obj->Intersects( aBBox ) )
|
|
|
|
aOutList.push_back( obj );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
wxASSERT( aNode->m_Children[0] != nullptr );
|
|
|
|
wxASSERT( aNode->m_Children[1] != nullptr );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
|
|
|
// Node
|
|
|
|
recursiveGetListObjectsIntersects( aNode->m_Children[0], aBBox, aOutList );
|
|
|
|
recursiveGetListObjectsIntersects( aNode->m_Children[1], aBBox, aOutList );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|