2015-12-08 07:31:57 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
2017-01-18 22:39:02 +00:00
|
|
|
* Copyright (C) 2015-2017 Mario Luzeiro <mrluzeiro@ua.pt>
|
2021-05-03 20:07:16 +00:00
|
|
|
* Copyright (C) 1992-2021 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 bbox_3d.cpp
|
|
|
|
* @brief 3D bounding box implementation.
|
2015-12-08 07:31:57 +00:00
|
|
|
*/
|
2016-07-19 17:35:25 +00:00
|
|
|
#include "3d_fastmath.h"
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-03 22:23:00 +00:00
|
|
|
#include "bbox_3d.h"
|
2021-01-05 22:19:00 +00:00
|
|
|
#include "../ray.h"
|
2020-10-01 08:52:03 +00:00
|
|
|
#include <wx/log.h>
|
2016-07-19 17:35:25 +00:00
|
|
|
#include <wx/debug.h> // For the wxASSERT
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D::BBOX_3D()
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D::BBOX_3D( const SFVEC3F& aPbInit )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_min = aPbInit;
|
|
|
|
m_max = aPbInit;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D::BBOX_3D( const SFVEC3F& aPbMin, const SFVEC3F& aPbMax )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
Set( aPbMin, aPbMax );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
BBOX_3D::~BBOX_3D()
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::Set( const SFVEC3F& aPoint )
|
2016-07-19 17:35:25 +00:00
|
|
|
{
|
|
|
|
m_min = aPoint;
|
|
|
|
m_max = aPoint;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::Set( const SFVEC3F& aPbMin, const SFVEC3F& aPbMax )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_min.x = fminf( aPbMin.x, aPbMax.x );
|
|
|
|
m_min.y = fminf( aPbMin.y, aPbMax.y );
|
|
|
|
m_min.z = fminf( aPbMin.z, aPbMax.z );
|
|
|
|
|
|
|
|
m_max.x = fmaxf( aPbMin.x, aPbMax.x );
|
|
|
|
m_max.y = fmaxf( aPbMin.y, aPbMax.y );
|
|
|
|
m_max.z = fmaxf( aPbMin.z, aPbMax.z );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::Set( const BBOX_3D& aBBox )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
wxASSERT( aBBox.IsInitialized() );
|
|
|
|
|
|
|
|
Set( aBBox.Min(), aBBox.Max() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BBOX_3D::IsInitialized() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2021-01-07 19:33:43 +00:00
|
|
|
return !( ( FLT_MAX == m_min.x ) || ( FLT_MAX == m_min.y ) || ( FLT_MAX == m_min.z )
|
|
|
|
|| ( -FLT_MAX == m_max.x ) || ( -FLT_MAX == m_max.y ) || ( -FLT_MAX == m_max.z ) );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::Reset()
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_min = SFVEC3F( FLT_MAX, FLT_MAX, FLT_MAX );
|
2021-01-07 19:33:43 +00:00
|
|
|
m_max = SFVEC3F( -FLT_MAX, -FLT_MAX, -FLT_MAX );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::Union( const SFVEC3F& aPoint )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2020-12-11 13:51:42 +00:00
|
|
|
// get the minimum value between the added point and the existent bounding box
|
2015-12-08 07:31:57 +00:00
|
|
|
m_min.x = fminf( m_min.x, aPoint.x );
|
|
|
|
m_min.y = fminf( m_min.y, aPoint.y );
|
|
|
|
m_min.z = fminf( m_min.z, aPoint.z );
|
|
|
|
|
2020-12-11 13:51:42 +00:00
|
|
|
// get the maximum value between the added point and the existent bounding box
|
2015-12-08 07:31:57 +00:00
|
|
|
m_max.x = fmaxf( m_max.x, aPoint.x );
|
|
|
|
m_max.y = fmaxf( m_max.y, aPoint.y );
|
|
|
|
m_max.z = fmaxf( m_max.z, aPoint.z );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::Union( const BBOX_3D& aBBox )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2016-07-19 17:35:25 +00:00
|
|
|
wxASSERT( aBBox.IsInitialized() );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2020-12-11 13:51:42 +00:00
|
|
|
// get the minimum value between the added bounding box and the existent bounding box
|
2015-12-08 07:31:57 +00:00
|
|
|
m_min.x = fmin( m_min.x, aBBox.m_min.x );
|
|
|
|
m_min.y = fmin( m_min.y, aBBox.m_min.y );
|
|
|
|
m_min.z = fmin( m_min.z, aBBox.m_min.z );
|
|
|
|
|
2020-12-11 13:51:42 +00:00
|
|
|
// get the maximum value between the added bounding box and the existent bounding box
|
2015-12-08 07:31:57 +00:00
|
|
|
m_max.x = fmax( m_max.x, aBBox.m_max.x );
|
|
|
|
m_max.y = fmax( m_max.y, aBBox.m_max.y );
|
|
|
|
m_max.z = fmax( m_max.z, aBBox.m_max.z );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
SFVEC3F BBOX_3D::GetCenter() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
2021-01-02 21:05:29 +00:00
|
|
|
return ( m_max + m_min ) * 0.5f;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
float BBOX_3D::GetCenter( unsigned int aAxis ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
wxASSERT( aAxis < 3 );
|
2021-01-07 19:33:43 +00:00
|
|
|
return ( m_max[aAxis] + m_min[aAxis] ) * 0.5f;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F BBOX_3D::GetExtent() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
return m_max - m_min;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
unsigned int BBOX_3D::MaxDimension() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
unsigned int result = 0;
|
|
|
|
|
|
|
|
SFVEC3F extent = GetExtent();
|
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
if( extent.y > extent.x )
|
|
|
|
result = 1;
|
2021-01-02 21:05:29 +00:00
|
|
|
|
2016-07-19 17:35:25 +00:00
|
|
|
if( extent.z > extent.y )
|
|
|
|
result = 2;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
float BBOX_3D::GetMaxDimension() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
SFVEC3F extent = GetExtent();
|
2021-10-24 17:00:46 +00:00
|
|
|
return std::max( std::max( extent.x, extent.y ), extent.z );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
float BBOX_3D::SurfaceArea() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
SFVEC3F extent = GetExtent();
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
return 2.0f * ( extent.x * extent.z + extent.x * extent.y + extent.y * extent.z );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::Scale( float aScale )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
wxASSERT( IsInitialized() );
|
|
|
|
|
|
|
|
SFVEC3F scaleV = SFVEC3F( aScale, aScale, aScale );
|
|
|
|
SFVEC3F centerV = GetCenter();
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
m_min = ( m_min - centerV ) * scaleV + centerV;
|
|
|
|
m_max = ( m_max - centerV ) * scaleV + centerV;
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::ScaleNextUp()
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_min.x = NextFloatDown( m_min.x );
|
|
|
|
m_min.y = NextFloatDown( m_min.y );
|
|
|
|
m_min.z = NextFloatDown( m_min.z );
|
|
|
|
|
|
|
|
m_max.x = NextFloatUp( m_max.x );
|
|
|
|
m_max.y = NextFloatUp( m_max.y );
|
|
|
|
m_max.z = NextFloatUp( m_max.z );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::ScaleNextDown()
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
m_min.x = NextFloatUp( m_min.x );
|
|
|
|
m_min.y = NextFloatUp( m_min.y );
|
|
|
|
m_min.z = NextFloatUp( m_min.z );
|
|
|
|
|
|
|
|
m_max.x = NextFloatDown( m_max.x );
|
|
|
|
m_max.y = NextFloatDown( m_max.y );
|
|
|
|
m_max.z = NextFloatDown( m_max.z );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BBOX_3D::Intersects( const BBOX_3D& aBBox ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
wxASSERT( IsInitialized() );
|
|
|
|
wxASSERT( aBBox.IsInitialized() );
|
|
|
|
|
|
|
|
bool x = ( m_max.x >= aBBox.m_min.x ) && ( m_min.x <= aBBox.m_max.x );
|
|
|
|
bool y = ( m_max.y >= aBBox.m_min.y ) && ( m_min.y <= aBBox.m_max.y );
|
|
|
|
bool z = ( m_max.z >= aBBox.m_min.z ) && ( m_min.z <= aBBox.m_max.z );
|
|
|
|
|
|
|
|
return ( x && y && z );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BBOX_3D::Inside( const SFVEC3F& aPoint ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
wxASSERT( IsInitialized() );
|
|
|
|
|
2020-11-24 22:16:41 +00:00
|
|
|
return ( aPoint.x >= m_min.x ) && ( aPoint.x <= m_max.x ) &&
|
|
|
|
( aPoint.y >= m_min.y ) && ( aPoint.y <= m_max.y ) &&
|
|
|
|
( aPoint.z >= m_min.z ) && ( aPoint.z <= m_max.z );
|
2015-12-08 07:31:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-03-02 22:29:08 +00:00
|
|
|
bool BBOX_3D::Inside( const BBOX_3D& aBBox ) const
|
|
|
|
{
|
|
|
|
wxASSERT( IsInitialized() );
|
|
|
|
wxASSERT( aBBox.IsInitialized() );
|
|
|
|
|
|
|
|
return Inside( aBBox.Min() ) &&
|
|
|
|
Inside( aBBox.Max() );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
float BBOX_3D::Volume() const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
wxASSERT( IsInitialized() );
|
|
|
|
|
|
|
|
SFVEC3F extent = GetExtent();
|
|
|
|
|
|
|
|
return extent.x * extent.y * extent.z;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
SFVEC3F BBOX_3D::Offset( const SFVEC3F& p ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
return (p - m_min) / (m_max - m_min);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// https://github.com/mmp/pbrt-v2/blob/master/src/accelerators/bvh.cpp#L126
|
2021-01-02 21:05:29 +00:00
|
|
|
bool BBOX_3D::Intersect( const RAY& aRay, float* aOutHitt0, float* aOutHitt1 ) const
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
wxASSERT( aOutHitt0 );
|
|
|
|
wxASSERT( aOutHitt1 );
|
|
|
|
|
|
|
|
const SFVEC3F bounds[2] = {m_min, m_max};
|
|
|
|
|
|
|
|
// Check for ray intersection against x and y slabs
|
2021-01-07 19:33:43 +00:00
|
|
|
float tmin = ( bounds[aRay.m_dirIsNeg[0]].x - aRay.m_Origin.x ) * aRay.m_InvDir.x;
|
|
|
|
float tmax = ( bounds[1 - aRay.m_dirIsNeg[0]].x - aRay.m_Origin.x ) * aRay.m_InvDir.x;
|
|
|
|
const float tymin = ( bounds[aRay.m_dirIsNeg[1]].y - aRay.m_Origin.y ) * aRay.m_InvDir.y;
|
|
|
|
const float tymax = ( bounds[1 - aRay.m_dirIsNeg[1]].y - aRay.m_Origin.y ) * aRay.m_InvDir.y;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
if( ( tmin > tymax ) || ( tymin > tmax ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
tmin = ( tymin > tmin ) ? tymin : tmin;
|
|
|
|
tmax = ( tymax < tmax ) ? tymax : tmax;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
// Check for ray intersection against z slab
|
2021-01-07 19:33:43 +00:00
|
|
|
const float tzmin = ( bounds[aRay.m_dirIsNeg[2]].z - aRay.m_Origin.z ) * aRay.m_InvDir.z;
|
|
|
|
const float tzmax = ( bounds[1 - aRay.m_dirIsNeg[2]].z - aRay.m_Origin.z ) * aRay.m_InvDir.z;
|
2015-12-08 07:31:57 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
if( ( tmin > tzmax ) || ( tzmin > tmax ) )
|
2015-12-08 07:31:57 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
tmin = (tzmin > tmin)? tzmin : tmin;
|
|
|
|
tmin = ( tmin < 0.0f)? 0.0f : tmin;
|
|
|
|
|
|
|
|
tmax = (tzmax < tmax)? tzmax : tmax;
|
|
|
|
|
|
|
|
*aOutHitt0 = tmin;
|
|
|
|
*aOutHitt1 = tmax;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
void BBOX_3D::ApplyTransformation( glm::mat4 aTransformMatrix )
|
2015-12-08 07:31:57 +00:00
|
|
|
{
|
|
|
|
wxASSERT( IsInitialized() );
|
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F v1 = SFVEC3F( aTransformMatrix * glm::vec4( m_min.x, m_min.y, m_min.z, 1.0f ) );
|
2016-07-19 17:35:25 +00:00
|
|
|
|
2021-01-02 21:05:29 +00:00
|
|
|
const SFVEC3F v2 = SFVEC3F( aTransformMatrix * glm::vec4( m_max.x, m_max.y, m_max.z, 1.0f ) );
|
2015-12-08 07:31:57 +00:00
|
|
|
|
|
|
|
Reset();
|
|
|
|
Union( v1 );
|
|
|
|
Union( v2 );
|
|
|
|
}
|
|
|
|
|
|
|
|
|