Skip to content

Commit

Permalink
Unify flip direction handling
Browse files Browse the repository at this point in the history
There was a gentle mish-mash of booleans, some with
true being left/right and some up/down, and some functions that
can flip in both axes (which is never actually done, and doesn't
really make geometric sense).

Replace all this with the FLIP_DIRECTION enum class, which makes
the intention completely unambiguous.

This also then allows a small scattering of simplifications,
because everything takes the same type and you don't have to
fiddle booleans to fit.
  • Loading branch information
johnbeard committed Sep 27, 2024
1 parent 3066ad2 commit 215533f
Show file tree
Hide file tree
Showing 75 changed files with 293 additions and 448 deletions.
2 changes: 1 addition & 1 deletion 3d-viewer/dialogs/panel_preview_3d_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ PANEL_PREVIEW_3D_MODEL::PANEL_PREVIEW_3D_MODEL( wxWindow* aParent, PCB_BASE_FRAM
// Ensure the footprint is shown like in Fp editor: rot 0, not flipped
// to avoid mistakes when setting the3D shape position/rotation
if( m_dummyFootprint->IsFlipped() )
m_dummyFootprint->Flip( m_dummyFootprint->GetPosition(), false );
m_dummyFootprint->Flip( m_dummyFootprint->GetPosition(), FLIP_DIRECTION::TOP_BOTTOM );

m_dummyFootprint->SetOrientation( ANGLE_0 );

Expand Down
11 changes: 6 additions & 5 deletions common/bitmap_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 jean-pierre.charras
* Copyright (C) 2011-2023 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2011-2024 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
Expand All @@ -22,7 +22,8 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#include <bitmap_base.h>
#include "bitmap_base.h"

#include <gr_basic.h>
#include <math/util.h> // for KiROUND
#include <memory> // for make_unique, unique_ptr
Expand Down Expand Up @@ -408,7 +409,7 @@ VECTOR2I BITMAP_BASE::GetSize() const
}


void BITMAP_BASE::Mirror( bool aVertically )
void BITMAP_BASE::Mirror( FLIP_DIRECTION aFlipDirection )
{
if( m_image )
{
Expand All @@ -420,13 +421,13 @@ void BITMAP_BASE::Mirror( bool aVertically )
int resY = m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONY );
int unit = m_image->GetOptionInt( wxIMAGE_OPTION_RESOLUTIONUNIT );

*m_image = m_image->Mirror( not aVertically );
*m_image = m_image->Mirror( aFlipDirection == FLIP_DIRECTION::LEFT_RIGHT );

m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONUNIT , unit);
m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONX, resX);
m_image->SetOption( wxIMAGE_OPTION_RESOLUTIONY, resY);

if( aVertically )
if( aFlipDirection == FLIP_DIRECTION::TOP_BOTTOM )
m_isMirroredY = !m_isMirroredY;
else
m_isMirroredX = !m_isMirroredX;
Expand Down
61 changes: 13 additions & 48 deletions common/eda_shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -428,73 +428,38 @@ void EDA_SHAPE::rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle )
}


void EDA_SHAPE::flip( const VECTOR2I& aCentre, bool aFlipLeftRight )
void EDA_SHAPE::flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection )
{
switch ( m_shape )
{
case SHAPE_T::SEGMENT:
case SHAPE_T::RECTANGLE:
if( aFlipLeftRight )
{
m_start.x = aCentre.x - ( m_start.x - aCentre.x );
m_end.x = aCentre.x - ( m_end.x - aCentre.x );
}
else
{
m_start.y = aCentre.y - ( m_start.y - aCentre.y );
m_end.y = aCentre.y - ( m_end.y - aCentre.y );
}
MIRROR( m_start, aCentre, aFlipDirection );
MIRROR( m_end, aCentre, aFlipDirection );
break;

case SHAPE_T::CIRCLE:
if( aFlipLeftRight )
{
m_start.x = aCentre.x - ( m_start.x - aCentre.x );
m_end.x = aCentre.x - ( m_end.x - aCentre.x );
}
else
{
m_start.y = aCentre.y - ( m_start.y - aCentre.y );
m_end.y = aCentre.y - ( m_end.y - aCentre.y );
}
MIRROR( m_start, aCentre, aFlipDirection );
MIRROR( m_end, aCentre, aFlipDirection );
break;

case SHAPE_T::ARC:
if( aFlipLeftRight )
{
m_start.x = aCentre.x - ( m_start.x - aCentre.x );
m_end.x = aCentre.x - ( m_end.x - aCentre.x );
m_arcCenter.x = aCentre.x - ( m_arcCenter.x - aCentre.x );
}
else
{
m_start.y = aCentre.y - ( m_start.y - aCentre.y );
m_end.y = aCentre.y - ( m_end.y - aCentre.y );
m_arcCenter.y = aCentre.y - ( m_arcCenter.y - aCentre.y );
}
MIRROR( m_start, aCentre, aFlipDirection );
MIRROR( m_end, aCentre, aFlipDirection );
MIRROR( m_arcCenter, aCentre, aFlipDirection );

std::swap( m_start, m_end );
break;

case SHAPE_T::POLY:
m_poly.Mirror( aFlipLeftRight, !aFlipLeftRight, aCentre );
m_poly.Mirror( aCentre, aFlipDirection );
break;

case SHAPE_T::BEZIER:
if( aFlipLeftRight )
{
m_start.x = aCentre.x - ( m_start.x - aCentre.x );
m_end.x = aCentre.x - ( m_end.x - aCentre.x );
m_bezierC1.x = aCentre.x - ( m_bezierC1.x - aCentre.x );
m_bezierC2.x = aCentre.x - ( m_bezierC2.x - aCentre.x );
}
else
{
m_start.y = aCentre.y - ( m_start.y - aCentre.y );
m_end.y = aCentre.y - ( m_end.y - aCentre.y );
m_bezierC1.y = aCentre.y - ( m_bezierC1.y - aCentre.y );
m_bezierC2.y = aCentre.y - ( m_bezierC2.y - aCentre.y );
}
MIRROR( m_start, aCentre, aFlipDirection );
MIRROR( m_end, aCentre, aFlipDirection );
MIRROR( m_bezierC1, aCentre, aFlipDirection );
MIRROR( m_bezierC2, aCentre, aFlipDirection );

RebuildBezierToSegmentsPointsList( m_stroke.GetWidth() / 2 );
break;
Expand Down
4 changes: 2 additions & 2 deletions common/widgets/footprint_diff_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ void FOOTPRINT_DIFF_WIDGET::DisplayDiff( FOOTPRINT* aBoardFootprint,
m_boardItemCopy->Move( -m_boardItemCopy->GetPosition() );

if( m_boardItemCopy->IsFlipped() )
m_boardItemCopy->Flip( {0,0}, false );
m_boardItemCopy->Flip( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );

if( m_boardItemCopy->GetOrientation() != ANGLE_0 )
m_boardItemCopy->Rotate( {0,0}, -m_boardItemCopy->GetOrientation() );
m_boardItemCopy->Rotate( { 0, 0 }, -m_boardItemCopy->GetOrientation() );

m_libraryItem = aLibFootprint;

Expand Down
4 changes: 2 additions & 2 deletions eeschema/sch_bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,14 @@ VECTOR2I SCH_BITMAP::GetSize() const
void SCH_BITMAP::MirrorVertically( int aCenter )
{
MIRROR( m_pos.y, aCenter );
m_bitmapBase->Mirror( true );
m_bitmapBase->Mirror( FLIP_DIRECTION::TOP_BOTTOM );
}


void SCH_BITMAP::MirrorHorizontally( int aCenter )
{
MIRROR( m_pos.x, aCenter );
m_bitmapBase->Mirror( false );
m_bitmapBase->Mirror( FLIP_DIRECTION::LEFT_RIGHT );
}


Expand Down
4 changes: 2 additions & 2 deletions gerbview/export_to_pcbnew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void GBR_TO_PCB_EXPORTER::export_non_copper_item( const GERBER_DRAW_ITEM* aGbrIt
SHAPE_POLY_SET polyshape = d_codeDescr->m_Polygon;

// Compensate the Y axis orientation ( writePcbPolygon invert the Y coordinate )
polyshape.Outline( 0 ).Mirror( false, true );
polyshape.Outline( 0 ).Mirror( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );
writePcbPolygon( polyshape, aLayer, aGbrItem->GetABPosition( seg_start ) );
}
break;
Expand Down Expand Up @@ -470,7 +470,7 @@ void GBR_TO_PCB_EXPORTER::export_flashed_copper_item( const GERBER_DRAW_ITEM* aG
macroShape = *macro->GetApertureMacroShape( aGbrItem, VECTOR2I( 0, 0 ) );

// Compensate the Y axis orientation ( writePcbPolygon invert the Y coordinate )
macroShape.Outline( 0 ).Mirror( false, true );
macroShape.Outline( 0 ).Mirror( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );

writePcbPolygon( macroShape, aLayer, offset );
}
Expand Down
2 changes: 1 addition & 1 deletion gerbview/gerber_draw_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ void GERBER_DRAW_ITEM::ConvertSegmentToPolygon( SHAPE_POLY_SET* aPolygon ) const

// Create final polygon:
if( change )
aPolygon->Mirror( false, true );
aPolygon->Mirror( { 0, 0 }, FLIP_DIRECTION::TOP_BOTTOM );

aPolygon->Move( VECTOR2I( start ) );
}
Expand Down
14 changes: 6 additions & 8 deletions include/bitmap_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 jean-pierre.charras jp.charras at wanadoo.fr
* Copyright (C) 2013-2023 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2013-2024 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
Expand All @@ -22,11 +22,12 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#ifndef BITMAP_BASE_H
#define BITMAP_BASE_H
#pragma once

#include <wx/bitmap.h>
#include <wx/image.h>

#include <core/mirror.h>
#include <kiid.h>
#include <math/box2.h>
#include <gal/color4d.h>
Expand Down Expand Up @@ -194,9 +195,9 @@ class BITMAP_BASE
/**
* Mirror image vertically (i.e. relative to its horizontal X axis ) or horizontally (i.e
* relative to its vertical Y axis).
* @param aVertically false to mirror horizontally or true to mirror vertically.
* @param aFlipDirection the direction to flip the image.
*/
void Mirror( bool aVertically );
void Mirror( FLIP_DIRECTION aFlipDirection );

/**
* Rotate image CW or CCW.
Expand Down Expand Up @@ -274,6 +275,3 @@ class BITMAP_BASE
bool m_isMirroredY; // Used for OpenGL rendering only
EDA_ANGLE m_rotation; // Used for OpenGL rendering only
};


#endif // BITMAP_BASE_H
15 changes: 6 additions & 9 deletions include/board_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2018 Jean-Pierre Charras, jp.charras at wandadoo.fr
* Copyright (C) 1992-2023 KiCad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 1992-2024 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
Expand All @@ -22,15 +22,15 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#ifndef BOARD_ITEM_STRUCT_H
#define BOARD_ITEM_STRUCT_H
#pragma once


#include <core/mirror.h>
#include <eda_item.h>
#include <geometry/approximation.h>
#include <layer_ids.h>
#include <lseq.h>
#include <lset.h>
#include <geometry/approximation.h>
#include <stroke_params.h>
#include <geometry/eda_angle.h>

Expand Down Expand Up @@ -358,9 +358,9 @@ class BOARD_ITEM : public EDA_ITEM
* Flip this object, i.e. change the board side for this object.
*
* @param aCentre the rotation point.
* @param aFlipLeftRight mirror across Y axis instead of X (the default).
* @param aFlipDirection the flip direction
*/
virtual void Flip( const VECTOR2I& aCentre, bool aFlipLeftRight );
virtual void Flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection );

/**
* Perform any normalization required after a user rotate and/or flip.
Expand Down Expand Up @@ -489,6 +489,3 @@ class DELETED_BOARD_ITEM : public BOARD_ITEM
void Show( int , std::ostream& ) const override {}
#endif
};


#endif /* BOARD_ITEM_STRUCT_H */
29 changes: 23 additions & 6 deletions include/core/mirror.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2020 KiCad Developers, see AUTHORS.TXT for contributors.
* Copyright (C) 2024 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
Expand All @@ -21,14 +21,19 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#ifndef MIRROR_H
#define MIRROR_H
#pragma once

enum class FLIP_DIRECTION
{
LEFT_RIGHT, ///< Flip left to right (around the Y axis)
TOP_BOTTOM ///< Flip top to bottom (around the X axis)
};

/**
* Returns the mirror of aPoint relative to the aMirrorRef.
*/
template <typename T>
T MIRRORVAL( T aPoint, T aMirrorRef )
constexpr T MIRRORVAL( T aPoint, T aMirrorRef )
{
return -( aPoint - aMirrorRef ) + aMirrorRef;
}
Expand All @@ -37,9 +42,21 @@ T MIRRORVAL( T aPoint, T aMirrorRef )
* Updates aPoint with the mirror of aPoint relative to the aMirrorRef.
*/
template <typename T>
void MIRROR( T& aPoint, const T& aMirrorRef )
constexpr void MIRROR( T& aPoint, const T& aMirrorRef )
{
aPoint = MIRRORVAL( aPoint, aMirrorRef );
}

#endif /* MIRROR_H */

/**
* Updates aPoint with the mirror of aPoint relative to the aMirrorRef,
* in the specified direction.
*/
template <typename VT>
constexpr void MIRROR( VT& aPoint, const VT& aMirrorRef, FLIP_DIRECTION aFlipDirection )
{
if( aFlipDirection == FLIP_DIRECTION::LEFT_RIGHT )
MIRROR( aPoint.x, aMirrorRef.x );
else
MIRROR( aPoint.y, aMirrorRef.y );
}
10 changes: 4 additions & 6 deletions include/eda_shape.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/

#ifndef EDA_SHAPE_H
#define EDA_SHAPE_H
#pragma once

#include <trigo.h>
#include <core/mirror.h>
#include <geometry/shape_poly_set.h>
#include <geometry/approximation.h>
#include <properties/property.h>
#include <stroke_params.h>
#include <trigo.h>

class LINE_READER;
class EDA_DRAW_FRAME;
Expand Down Expand Up @@ -375,7 +375,7 @@ class EDA_SHAPE

void move( const VECTOR2I& aMoveVector );
void rotate( const VECTOR2I& aRotCentre, const EDA_ANGLE& aAngle );
void flip( const VECTOR2I& aCentre, bool aFlipLeftRight );
void flip( const VECTOR2I& aCentre, FLIP_DIRECTION aFlipDirection );
void scale( double aScale );

const BOX2I getBoundingBox() const;
Expand Down Expand Up @@ -443,5 +443,3 @@ class EDA_SHAPE
DECLARE_ENUM_TO_WXANY( SHAPE_T );
DECLARE_ENUM_TO_WXANY( LINE_STYLE );
#endif

#endif // EDA_SHAPE_H
9 changes: 0 additions & 9 deletions include/settings/app_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ enum class ARC_EDIT_MODE
KEEP_ENDPOINTS_OR_START_DIRECTION
};

/**
* Settings for board items flip. Used by pcbnew
*/
enum class FLIP_DIRECTION
{
LEFT_RIGHT,
TOP_BOTTOM
};

/**
* Stores the window positioning/state
*/
Expand Down
Loading

0 comments on commit 215533f

Please sign in to comment.