kicad/common/hash_eda.cpp

305 lines
9.7 KiB
C++
Raw Permalink Normal View History

2017-10-09 09:44:02 +00:00
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 CERN
* Copyright (C) 2020-2023 KiCad Developers, see AUTHORS.txt for contributors.
2017-10-09 09:44:02 +00:00
* @author Maciej Suminski <maciej.suminski@cern.ch>
*
* 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
*/
#include <hash_eda.h>
#include <hash.h>
#include <footprint.h>
#include <pcb_text.h>
#include <pcb_textbox.h>
#include <pcb_shape.h>
#include <pad.h>
#include <pcb_track.h>
2017-10-09 09:44:02 +00:00
#include <macros.h>
2017-10-09 09:44:02 +00:00
#include <functional>
using namespace std;
// Common calculation part for all BOARD_ITEMs
static inline size_t hash_board_item( const BOARD_ITEM* aItem, int aFlags )
{
size_t ret = 0;
if( aFlags & HASH_LAYER )
ret = hash<unsigned long long>{}( aItem->GetLayerSet().to_ullong() );
2017-10-09 09:44:02 +00:00
return ret;
}
2020-11-14 01:16:02 +00:00
size_t hash_fp_item( const EDA_ITEM* aItem, int aFlags )
2017-10-09 09:44:02 +00:00
{
size_t ret = 0;
2017-10-09 09:44:02 +00:00
switch( aItem->Type() )
{
2020-11-13 12:21:02 +00:00
case PCB_FOOTPRINT_T:
{
2020-11-13 15:15:52 +00:00
const FOOTPRINT* footprint = static_cast<const FOOTPRINT*>( aItem );
2017-10-09 09:44:02 +00:00
2020-11-13 12:21:02 +00:00
ret = hash_board_item( footprint, aFlags );
2017-10-09 09:44:02 +00:00
2020-11-13 12:21:02 +00:00
if( aFlags & HASH_POS )
hash_combine( ret, footprint->GetPosition().x, footprint->GetPosition().y );
2017-10-09 09:44:02 +00:00
2020-11-13 12:21:02 +00:00
if( aFlags & HASH_ROT )
2022-01-17 20:49:48 +00:00
hash_combine( ret, footprint->GetOrientation().AsDegrees() );
2017-10-09 09:44:02 +00:00
2020-11-13 12:21:02 +00:00
for( BOARD_ITEM* item : footprint->GraphicalItems() )
2020-11-14 01:16:02 +00:00
hash_combine( ret, hash_fp_item( item, aFlags ) );
2017-10-09 09:44:02 +00:00
2020-11-13 12:21:02 +00:00
for( PAD* pad : footprint->Pads() )
2020-11-14 01:16:02 +00:00
hash_combine( ret, hash_fp_item( static_cast<EDA_ITEM*>( pad ), aFlags ) );
2020-11-13 12:21:02 +00:00
}
2017-10-09 09:44:02 +00:00
break;
case PCB_VIA_T:
{
const PCB_VIA* via = static_cast<const PCB_VIA*>( aItem );
ret = hash_val( via->GetWidth() );
hash_combine( ret, via->GetDrillValue() );
hash_combine( ret, via->TopLayer() );
hash_combine( ret, via->BottomLayer() );
for( PCB_LAYER_ID layer : via->GetLayerSet().Seq() )
hash_combine( ret, via->FlashLayer( layer ) );
break;
}
2017-10-09 09:44:02 +00:00
case PCB_PAD_T:
2020-11-13 12:21:02 +00:00
{
const PAD* pad = static_cast<const PAD*>( aItem );
ret = hash<int>{}( static_cast<int>( pad->GetShape() ) );
hash_combine( ret, pad->GetAttribute() );
if( pad->GetAttribute() == PAD_ATTRIB::PTH || pad->GetAttribute() == PAD_ATTRIB::NPTH )
{
hash_combine( ret, pad->GetDrillSizeX(), pad->GetDrillSizeY() );
hash_combine( ret, pad->GetDrillShape() );
for( PCB_LAYER_ID layer : pad->GetLayerSet().Seq() )
hash_combine( ret, pad->FlashLayer( layer ) );
}
hash_combine( ret, pad->GetSize().x, pad->GetSize().y );
hash_combine( ret, pad->GetOffset().x, pad->GetOffset().y );
switch( pad->GetShape() )
{
case PAD_SHAPE::CHAMFERED_RECT:
hash_combine( ret, pad->GetChamferPositions() );
hash_combine( ret, pad->GetChamferRectRatio() );
break;
case PAD_SHAPE::ROUNDRECT:
hash_combine( ret, pad->GetRoundRectCornerRadius() );
break;
case PAD_SHAPE::TRAPEZOID:
hash_combine( ret, pad->GetDelta().x, pad->GetDelta().y );
break;
case PAD_SHAPE::CUSTOM:
{
auto poly = pad->GetEffectivePolygon( ERROR_INSIDE );
for( int ii = 0; ii < poly->VertexCount(); ++ii )
{
VECTOR2I point = poly->CVertex( ii ) - pad->GetPosition();
hash_combine( ret, point.x, point.y );
}
break;
}
default:
break;
}
2020-11-13 12:21:02 +00:00
hash_combine( ret, hash_board_item( pad, aFlags ) );
2020-11-13 12:21:02 +00:00
if( aFlags & HASH_POS )
{
if( aFlags & REL_COORD )
hash_combine( ret, pad->GetFPRelativePosition().x, pad->GetFPRelativePosition().y );
2020-11-13 12:21:02 +00:00
else
hash_combine( ret, pad->GetPosition().x, pad->GetPosition().y );
}
2017-10-09 09:44:02 +00:00
2020-11-13 12:21:02 +00:00
if( aFlags & HASH_ROT )
2022-01-17 20:49:48 +00:00
hash_combine( ret, pad->GetOrientation().AsDegrees() );
2017-10-09 09:44:02 +00:00
2020-11-13 12:21:02 +00:00
if( aFlags & HASH_NET )
hash_combine( ret, pad->GetNetCode() );
}
break;
2017-10-09 09:44:02 +00:00
2023-06-06 15:09:34 +00:00
case PCB_FIELD_T:
if( !( aFlags & HASH_REF ) && static_cast<const PCB_FIELD*>( aItem )->IsReference() )
2020-11-13 12:21:02 +00:00
break;
2017-10-09 09:44:02 +00:00
if( !( aFlags & HASH_VALUE ) && static_cast<const PCB_FIELD*>( aItem )->IsValue() )
2020-11-13 12:21:02 +00:00
break;
KI_FALLTHROUGH;
case PCB_TEXT_T:
{
const PCB_TEXT* text = static_cast<const PCB_TEXT*>( aItem );
2020-11-13 12:21:02 +00:00
ret = hash_board_item( text, aFlags );
hash_combine( ret, text->GetText().ToStdString() );
2022-01-30 10:52:52 +00:00
hash_combine( ret, text->IsItalic() );
2020-11-13 12:21:02 +00:00
hash_combine( ret, text->IsBold() );
hash_combine( ret, text->IsMirrored() );
hash_combine( ret, text->GetTextWidth() );
hash_combine( ret, text->GetTextHeight() );
hash_combine( ret, text->GetHorizJustify() );
hash_combine( ret, text->GetVertJustify() );
2017-10-09 09:44:02 +00:00
2020-11-13 12:21:02 +00:00
if( aFlags & HASH_POS )
{
VECTOR2I pos = ( aFlags & REL_COORD ) ? text->GetFPRelativePosition()
: text->GetPosition();
hash_combine( ret, pos.x, pos.y );
2017-10-09 09:44:02 +00:00
}
2020-11-13 12:21:02 +00:00
if( aFlags & HASH_ROT )
2022-01-17 20:49:48 +00:00
hash_combine( ret, text->GetTextAngle().AsDegrees() );
2020-11-13 12:21:02 +00:00
}
2017-10-09 09:44:02 +00:00
break;
case PCB_SHAPE_T:
2020-11-13 12:21:02 +00:00
{
const PCB_SHAPE* shape = static_cast<const PCB_SHAPE*>( aItem );
ret = hash_board_item( shape, aFlags );
hash_combine( ret, shape->GetShape() );
hash_combine( ret, shape->GetWidth() );
hash_combine( ret, shape->IsFilled() );
hash_combine( ret, shape->GetLineStyle() );
if( shape->GetShape() == SHAPE_T::ARC || shape->GetShape() == SHAPE_T::CIRCLE )
hash_combine( ret, shape->GetRadius() );
2020-11-13 12:21:02 +00:00
if( aFlags & HASH_POS )
2017-10-09 09:44:02 +00:00
{
std::vector<VECTOR2I> points;
points.push_back( shape->GetStart() );
points.push_back( shape->GetEnd() );
if( shape->GetShape() == SHAPE_T::CIRCLE )
points.push_back( shape->GetCenter() );
if( shape->GetShape() == SHAPE_T::ARC )
points.push_back( shape->GetArcMid() );
FOOTPRINT* parentFP = shape->GetParentFootprint();
if( shape->GetShape() == SHAPE_T::POLY )
2017-10-09 09:44:02 +00:00
{
const SHAPE_POLY_SET& poly = shape->GetPolyShape();
for( auto it = poly.CIterateWithHoles(); it; it++ )
points.push_back( *it );
2020-11-13 12:21:02 +00:00
}
if( shape->GetShape() == SHAPE_T::BEZIER )
{
points.push_back( shape->GetBezierC1() );
points.push_back( shape->GetBezierC2() );
}
if( parentFP && ( aFlags & REL_COORD ) )
2020-11-13 12:21:02 +00:00
{
for( VECTOR2I& point : points )
{
point -= parentFP->GetPosition();
RotatePoint( point, -parentFP->GetOrientation() );
}
2017-10-09 09:44:02 +00:00
}
if( aFlags & REL_POS )
{
for( VECTOR2I& point : points )
point -= shape->GetPosition();
}
for( VECTOR2I& point : points )
hash_combine( ret, point.x, point.y );
2017-10-09 09:44:02 +00:00
}
2020-11-13 12:21:02 +00:00
}
2017-10-09 09:44:02 +00:00
break;
case PCB_TEXTBOX_T:
2022-01-30 10:52:52 +00:00
{
const PCB_TEXTBOX* textbox = static_cast<const PCB_TEXTBOX*>( aItem );
2022-01-30 10:52:52 +00:00
ret = hash_board_item( textbox, aFlags );
hash_combine( ret, textbox->GetText().ToStdString() );
hash_combine( ret, textbox->IsItalic() );
hash_combine( ret, textbox->IsBold() );
hash_combine( ret, textbox->IsMirrored() );
hash_combine( ret, textbox->GetTextWidth() );
hash_combine( ret, textbox->GetTextHeight() );
hash_combine( ret, textbox->GetHorizJustify() );
hash_combine( ret, textbox->GetVertJustify() );
if( aFlags & HASH_ROT )
hash_combine( ret, textbox->GetTextAngle().AsDegrees() );
hash_combine( ret, textbox->GetShape() );
hash_combine( ret, textbox->GetWidth() );
hash_combine( ret, textbox->GetLineStyle() );
2022-01-30 10:52:52 +00:00
if( aFlags & HASH_POS )
{
VECTOR2I start = textbox->GetStart();
VECTOR2I end = textbox->GetEnd();
FOOTPRINT* parentFP = textbox->GetParentFootprint();
if( parentFP && ( aFlags & REL_COORD ) )
2022-01-30 10:52:52 +00:00
{
start -= parentFP->GetPosition();
end -= parentFP->GetPosition();
RotatePoint( start, -parentFP->GetOrientation() );
RotatePoint( end, -parentFP->GetOrientation() );
2022-01-30 10:52:52 +00:00
}
hash_combine( ret, start.x );
hash_combine( ret, start.y );
hash_combine( ret, end.x );
hash_combine( ret, end.y );
2022-01-30 10:52:52 +00:00
}
}
break;
2017-10-09 09:44:02 +00:00
default:
2020-11-14 01:16:02 +00:00
wxASSERT_MSG( false, "Unhandled type in function hash_fp_item() (exporter_gencad.cpp)" );
2017-10-09 09:44:02 +00:00
}
return ret;
}