Remove unit-less angles from geometry manager APIs.

This commit is contained in:
Jeff Young 2022-01-20 16:39:28 +00:00
parent fbab335128
commit 854987f663
6 changed files with 78 additions and 72 deletions

View File

@ -1,7 +1,7 @@
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2017 Kicad Developers, see AUTHORS.txt for contributors.
* Copyright (C) 2017-2022 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
@ -57,27 +57,6 @@ const BOX2I ARC_ASSISTANT::ViewBBox() const
}
/**
* Get deci-degrees from radians, normalised to +/- 360.
*
* The normalisation is such that a negative angle will stay
* negative.
*/
double getNormDeciDegFromRad( double aRadians )
{
double degs = RAD2DECIDEG( aRadians );
// normalise to +/- 360
while( degs < -3600.0 )
degs += 3600.0;
while( degs > 3600.0 )
degs -= 3600.0;
return degs;
}
void ARC_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
{
KIGFX::GAL& gal = *aView->GetGAL();
@ -104,31 +83,29 @@ void ARC_ASSISTANT::ViewDraw( int aLayer, KIGFX::VIEW* aView ) const
{
// haven't started the angle selection phase yet
double initAngle = m_constructMan.GetStartAngle();
EDA_ANGLE initAngle = m_constructMan.GetStartAngle();
// draw the radius guide circle
preview_ctx.DrawCircle( origin, m_constructMan.GetRadius(), true );
EDA_ANGLE angle( initAngle, RADIANS_T );
angle.Normalize();
initAngle.Normalize720();
cursorStrings.push_back( DimensionLabel( "r", m_constructMan.GetRadius(), m_units ) );
cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), angle.AsDegrees(),
cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), initAngle.AsDegrees(),
EDA_UNITS::DEGREES ) );
}
else
{
preview_ctx.DrawLineWithAngleHighlight( origin, m_constructMan.GetEndRadiusEnd(), false );
double start = m_constructMan.GetStartAngle();
double subtended = m_constructMan.GetSubtended();
EDA_ANGLE incAngle( subtended, RADIANS_T );
EDA_ANGLE endAngle( start + subtended, RADIANS_T );
EDA_ANGLE start = m_constructMan.GetStartAngle();
EDA_ANGLE subtended = m_constructMan.GetSubtended();
EDA_ANGLE endAngle = start + subtended;
// draw dimmed extender line to cursor
preview_ctx.DrawLineWithAngleHighlight( origin, m_constructMan.GetLastPoint(), true );
cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "Δθ" ), incAngle.AsDegrees(),
cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "Δθ" ), subtended.AsDegrees(),
EDA_UNITS::DEGREES ) );
cursorStrings.push_back( DimensionLabel( wxString::FromUTF8( "θ" ), endAngle.AsDegrees(),
EDA_UNITS::DEGREES ) );

View File

@ -24,14 +24,16 @@
#include <preview_items/arc_geom_manager.h>
#include <math/util.h> // for KiROUND
#include <geometry/eda_angle.h>
#include <trigo.h>
using namespace KIGFX::PREVIEW;
///< Snap an angle to the nearest 45 degrees
static double snapAngle( double aAngle )
static EDA_ANGLE snapAngle( const EDA_ANGLE& aAngle )
{
return KiROUND( aAngle / M_PI_4 ) * M_PI_4;
return ANGLE_45 * KiROUND( aAngle / ANGLE_45 );
}
@ -73,13 +75,17 @@ VECTOR2I ARC_GEOM_MANAGER::GetOrigin() const
VECTOR2I ARC_GEOM_MANAGER::GetStartRadiusEnd() const
{
return m_origin + VECTOR2I( m_radius, 0 ).Rotate( m_startAngle );
VECTOR2I vec( m_radius, 0 );
RotatePoint( vec, -m_startAngle );
return m_origin +vec;
}
VECTOR2I ARC_GEOM_MANAGER::GetEndRadiusEnd() const
{
return m_origin + VECTOR2I( m_radius, 0 ).Rotate( m_endAngle );
VECTOR2I vec( m_radius, 0 );
RotatePoint( vec, -m_endAngle );
return m_origin + vec;
}
@ -89,26 +95,26 @@ double ARC_GEOM_MANAGER::GetRadius() const
}
double ARC_GEOM_MANAGER::GetStartAngle() const
EDA_ANGLE ARC_GEOM_MANAGER::GetStartAngle() const
{
double angle = m_startAngle;
EDA_ANGLE angle = m_startAngle;
if( m_clockwise )
angle -= 2 * M_PI;
angle -= ANGLE_360;
return -angle;
}
double ARC_GEOM_MANAGER::GetSubtended() const
EDA_ANGLE ARC_GEOM_MANAGER::GetSubtended() const
{
double angle = m_endAngle - m_startAngle;
EDA_ANGLE angle = m_endAngle - m_startAngle;
if( m_endAngle <= m_startAngle )
angle += 2 * M_PI;
angle += ANGLE_360;
if( m_clockwise )
angle -= 2 * M_PI;
angle -= ANGLE_360;
return -angle;
}
@ -117,8 +123,8 @@ double ARC_GEOM_MANAGER::GetSubtended() const
bool ARC_GEOM_MANAGER::setOrigin( const VECTOR2I& aOrigin )
{
m_origin = aOrigin;
m_startAngle = 0.0;
m_endAngle = 0.0;
m_startAngle = ANGLE_0;
m_endAngle = ANGLE_0;
return true;
}
@ -129,14 +135,14 @@ bool ARC_GEOM_MANAGER::setStart( const VECTOR2I& aEnd )
const VECTOR2I radVec = aEnd - m_origin;
m_radius = radVec.EuclideanNorm();
m_startAngle = radVec.Angle();
m_startAngle = EDA_ANGLE( radVec );
if( m_angleSnap )
m_startAngle = snapAngle( m_startAngle );
// normalise into 0-2Pi
while( m_startAngle < 0 )
m_startAngle += M_PI * 2;
// normalise to 0..360
while( m_startAngle < ANGLE_0 )
m_startAngle += ANGLE_360;
m_endAngle = m_startAngle;
@ -148,30 +154,30 @@ bool ARC_GEOM_MANAGER::setEnd( const VECTOR2I& aCursor )
{
const VECTOR2I radVec = aCursor - m_origin;
m_endAngle = radVec.Angle();
m_endAngle = EDA_ANGLE( radVec );
if( m_angleSnap )
m_endAngle = snapAngle( m_endAngle );
// normalise into 0-2Pi
while( m_endAngle < 0 )
m_endAngle += M_PI * 2;
// normalise to 0..360
while( m_endAngle < ANGLE_0 )
m_endAngle += ANGLE_360;
if( !m_directionLocked )
{
double ccwAngle = m_endAngle - m_startAngle;
EDA_ANGLE ccwAngle = m_endAngle - m_startAngle;
if( m_endAngle <= m_startAngle )
ccwAngle += 2 * M_PI;
ccwAngle += ANGLE_360;
double cwAngle = std::abs( ccwAngle - 2 * M_PI );
EDA_ANGLE cwAngle = std::abs( ccwAngle - ANGLE_360 );
if( std::min( ccwAngle, cwAngle ) >= M_PI_2 )
if( std::min( ccwAngle, cwAngle ) >= ANGLE_90 )
m_directionLocked = true;
else
m_clockwise = cwAngle < ccwAngle;
}
else if( std::abs( GetSubtended() ) < M_PI_2 )
else if( std::abs( GetSubtended() ) < ANGLE_90 )
{
m_directionLocked = false;
}

View File

@ -25,6 +25,7 @@
#include <preview_items/two_point_geom_manager.h>
#include <gal/graphics_abstraction_layer.h>
#include <view/view.h>
#include <trigo.h>
using namespace KIGFX::PREVIEW;
@ -54,7 +55,9 @@ static SHAPE_POLY_SET getRectangleAlongCentreLine( const VECTOR2D& aClStart,
// the "side" of the rectangle is the centre line rotated by 90 deg
// and scaled by the aspect ratio
VECTOR2D side = cl.Rotate( M_PI / 2.0 ) * aAspect;
VECTOR2D side = cl;
RotatePoint( side, -ANGLE_90 );
side = side * aAspect;
VECTOR2D pt = aClStart + ( side / 2.0 );
poly.Append( pt );

View File

@ -27,9 +27,7 @@
#include "tool/edit_points.h"
#include <geometry/seg.h>
#include <math/util.h> // for KiROUND
#include <common.h>
#include <trigo.h>
#include <utility>
#include <geometry/geometry_utils.h>
@ -83,10 +81,10 @@ void EC_CIRCLE::Apply( EDIT_POINT& aHandle )
VECTOR2I centerToPoint = aHandle.GetPosition() - m_center.GetPosition();
int radius = centerToEnd.EuclideanNorm();
double angle = centerToPoint.Angle();
EDA_ANGLE angle( centerToPoint );
VECTOR2I newLine( radius, 0 );
newLine = newLine.Rotate( angle );
RotatePoint( newLine, -angle );
aHandle.SetPosition( m_center.GetPosition() + newLine );
}

View File

@ -25,6 +25,7 @@
#define PREVIEW_ITEMS_ARC_GEOMETRY_MANAGER_H
#include <preview_items/multistep_geom_manager.h>
#include <geometry/eda_angle.h>
namespace KIGFX {
namespace PREVIEW {
@ -97,10 +98,10 @@ public:
double GetRadius() const;
///< Get the angle of the vector leading to the start point (valid if step >= SET_START)
double GetStartAngle() const;
EDA_ANGLE GetStartAngle() const;
///< Get the angle of the vector leading to the end point (valid if step >= SET_ANGLE)
double GetSubtended() const;
EDA_ANGLE GetSubtended() const;
private:
@ -123,8 +124,8 @@ private:
bool m_clockwise = true;
VECTOR2I m_origin;
double m_radius = 0.0;
double m_startAngle = 0.0;
double m_endAngle = 0.0;
EDA_ANGLE m_startAngle;
EDA_ANGLE m_endAngle;
/*
* construction parameters

View File

@ -347,6 +347,12 @@ inline EDA_ANGLE operator/( const EDA_ANGLE& aAngleA, double aOperator )
}
inline double operator/( const EDA_ANGLE& aAngleA, EDA_ANGLE& aOperator )
{
return aAngleA.AsDegrees() / aOperator.AsDegrees();
}
inline bool operator==( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
{
return aAngleA.AsDegrees() == aAngleB.AsDegrees();
@ -383,6 +389,21 @@ inline bool operator>=( const EDA_ANGLE& aAngleA, const EDA_ANGLE& aAngleB )
}
inline std::ostream& operator<<( std::ostream& aStream, const EDA_ANGLE& aAngle )
{
return aStream << aAngle.AsDegrees();
}
namespace std
{
inline EDA_ANGLE abs( const EDA_ANGLE& aAngle )
{
return EDA_ANGLE( std::abs( aAngle.AsDegrees() ), DEGREES_T );
}
}
static constexpr EDA_ANGLE& ANGLE_HORIZONTAL = EDA_ANGLE::m_Angle0;
static constexpr EDA_ANGLE& ANGLE_VERTICAL = EDA_ANGLE::m_Angle90;
static constexpr EDA_ANGLE& FULL_CIRCLE = EDA_ANGLE::m_Angle360;