Commit 82fe1a49 authored by Maciej Suminski's avatar Maciej Suminski
Browse files

Merged Tom's branch

parents bfdad278 ac871702
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -163,6 +163,10 @@ set(COMMON_SRCS
    tool/tool_event.cpp
    tool/tool_interactive.cpp
    tool/context_menu.cpp

	geometry/seg.cpp
	geometry/shape_line_chain.cpp
	geometry/shape_collisions.cpp
)

add_library(common STATIC ${COMMON_SRCS})
+49 −2
Original line number Diff line number Diff line
@@ -87,8 +87,15 @@ EDA_DRAW_PANEL_GAL::EDA_DRAW_PANEL_GAL( wxWindow* aParentWindow, wxWindowID aWin
	Connect( wxEVT_MIDDLE_UP,   wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
	Connect( wxEVT_MIDDLE_DOWN, wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
	Connect( wxEVT_MOUSEWHEEL,  wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
//	Connect( wxEVT_CHAR_HOOK,   wxEventHandler( EDA_DRAW_PANEL_GAL::skipEvent ) );
	Connect( wxEVT_KEY_UP,      wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
	Connect( wxEVT_KEY_DOWN,    wxEventHandler( EDA_DRAW_PANEL_GAL::onEvent ), NULL, this );
//    Connect( wxEVT_ENTER_WINDOW, wxEventHandler( EDA_DRAW_PANEL_GAL::onEnter ), NULL, this );
    
    m_refreshTimer.SetOwner( this );
    Connect( wxEVT_TIMER, wxTimerEventHandler( EDA_DRAW_PANEL_GAL::onRefreshTimer ), NULL, this );

    this->SetFocus();
}


@@ -110,6 +117,9 @@ EDA_DRAW_PANEL_GAL::~EDA_DRAW_PANEL_GAL()

void EDA_DRAW_PANEL_GAL::onPaint( wxPaintEvent& WXUNUSED( aEvent ) )
{
    m_pendingRefresh = false;
    m_lastRefresh = wxGetLocalTimeMillis();

#ifdef __WXDEBUG__
    prof_counter time;
    prof_start( &time, false );
@@ -144,10 +154,33 @@ void EDA_DRAW_PANEL_GAL::onSize( wxSizeEvent& aEvent )
}


void EDA_DRAW_PANEL_GAL::onRefreshTimer( wxTimerEvent& aEvent )
{
    wxPaintEvent redrawEvent;
    wxPostEvent( this, redrawEvent );
}


void EDA_DRAW_PANEL_GAL::Refresh( bool eraseBackground, const wxRect* rect )
{
    if( m_pendingRefresh )
        return;

    wxLongLong t = wxGetLocalTimeMillis();
    wxLongLong delta = t - m_lastRefresh;

    if( delta >= MinRefreshPeriod )
    {
        wxPaintEvent redrawEvent;
        wxPostEvent( this, redrawEvent );
        m_pendingRefresh = true;
    }
    else
    {
        // One shot timer
        m_refreshTimer.Start( ( MinRefreshPeriod - delta ).ToLong(), true );
        m_pendingRefresh = true;
    }
} 


@@ -205,5 +238,19 @@ void EDA_DRAW_PANEL_GAL::onEvent( wxEvent& aEvent )
        m_eventDispatcher->DispatchWxEvent( aEvent );
    }

//	if( m_view->IsDirty() )
        Refresh();
}


void EDA_DRAW_PANEL_GAL::onEnter ( wxEvent& aEvent )
{
    SetFocus();
}


void EDA_DRAW_PANEL_GAL::skipEvent( wxEvent& aEvent )
{
    // This is necessary for CHAR_HOOK event to generate KEY_UP and KEY_DOWN events
    aEvent.Skip();
}
+114 −0
Original line number Diff line number Diff line
@@ -58,3 +58,117 @@ const bool COLOR4D::operator!=( const COLOR4D& aColor )
{
    return a != aColor.a || r != aColor.r || g != aColor.g || b != aColor.b;
}


void COLOR4D::ToHSV( double& aOutH, double& aOutS, double& aOutV ) const
{
    double min, max, delta;

    min = r < g ? r : g;
    min = min < b ? min : b;

    max = r > g ? r : g;
    max = max > b ? max : b;

    aOutV = max;                                // v
    delta = max - min;

    if( max > 0.0 )
    {
        aOutS = ( delta / max );                  // s
    }
    else
    {
        // r = g = b = 0                        // s = 0, v is undefined
        aOutS = 0.0;
        aOutH = NAN;                            // its now undefined
        return;
    }

    if( r >= max )                           // > is bogus, just keeps compiler happy
        aOutH = ( g - b ) / delta;           // between yellow & magenta
    else if( g >= max )
        aOutH = 2.0 + ( b - r ) / delta;     // between cyan & yellow
    else
        aOutH = 4.0 + ( r - g ) / delta;     // between magenta & cyan

    aOutH *= 60.0;                              // degrees

    if( aOutH < 0.0 )
        aOutH += 360.0;
}


void COLOR4D::FromHSV( double aInH, double aInS, double aInV )
{
    double hh, p, q, t, ff;
    long i;

    if( aInS <= 0.0 )   // < is bogus, just shuts up warnings
    {
        r = aInV;
        g = aInV;
        b = aInV;
        return;
    }

    hh = aInH;
    if( hh >= 360.0 )
        hh = 0.0;
    hh /= 60.0;

    i = (long) hh;
    ff = hh - i;

    p = aInV * ( 1.0 - aInS );
    q = aInV * ( 1.0 - ( aInS * ff ) );
    t = aInV * ( 1.0 - ( aInS * ( 1.0 - ff ) ) );

    switch (i)
    {
    case 0:
        r = aInV;
        g = t;
        b = p;
        break;
    case 1:
        r = q;
        g = aInV;
        b = p;
        break;
    case 2:
        r = p;
        g = aInV;
        b = t;
        break;

    case 3:
        r = p;
        g = q;
        b = aInV;
        break;
    case 4:
        r = t;
        g = p;
        b = aInV;
        break;
    case 5:
    default:
        r = aInV;
        g = p;
        b = q;
        break;
    }
    
}


COLOR4D& COLOR4D::Saturate( double aFactor )
{
    double h, s, v;
    ToHSV( h, s, v );
    FromHSV( h, aFactor, 1.0 );
    
    return *this;
}
        
+150 −0
Original line number Diff line number Diff line
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Tomasz Wlostowski <tomasz.wlostowski@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 <geometry/seg.h>

template <typename T> int sgn(T val) {
    return (T(0) < val) - (val < T(0));
}

bool SEG::PointCloserThan (const VECTOR2I& aP, int dist) const
{
	VECTOR2I   	  d = b - a;
	ecoord dist_sq = (ecoord) dist * dist;

 	SEG::ecoord l_squared = d.Dot(d);
    SEG::ecoord t = d.Dot(aP - a);


    if( t <= 0 || !l_squared )
    	return (aP - a).SquaredEuclideanNorm() < dist_sq;
    else if( t >= l_squared ) 
    	return (aP - b).SquaredEuclideanNorm() < dist_sq;


	int dxdy =  abs(d.x) - abs(d.y);	

    if( (dxdy >= -1 && dxdy <= 1) || abs(d.x) <= 1 || abs(d.y) <= 1)
    {
    	int ca = -sgn(d.y);
    	int cb = sgn(d.x);
    	int cc = -ca * a.x - cb * a.y;
    	
		ecoord num = ca * aP.x + cb * aP.y + cc;
		num *= num;

		if(ca && cb)
			num >>= 1;

		if(num > (dist_sq + 100))
			return false;
		else if(num < (dist_sq - 100))
			return true;
    }

	VECTOR2I nearest;    
    nearest.x = a.x + rescale(t, (ecoord)d.x, l_squared);
    nearest.y = a.y + rescale(t, (ecoord)d.y, l_squared);

    return (nearest - aP).SquaredEuclideanNorm() <= dist_sq;
}

SEG::ecoord SEG::SquaredDistance( const SEG& aSeg ) const 
{
		// fixme: rather inefficient.... 
		if(Intersect(aSeg))
			return 0;

		const VECTOR2I pts[4] =
		{
			aSeg.NearestPoint(a) - a,
			aSeg.NearestPoint(b) - b,
			NearestPoint(aSeg.a) - aSeg.a,
			NearestPoint(aSeg.b) - aSeg.b
			};

		ecoord m = VECTOR2I::ECOORD_MAX;
		for (int i = 0; i<4 ; i++)
			m = std::min(m, pts[i].SquaredEuclideanNorm());
		return m;
}
	
OPT_VECTOR2I SEG::Intersect( const SEG& aSeg, bool aIgnoreEndpoints, bool aLines ) const
{
	const VECTOR2I e (b - a);
	const VECTOR2I f (aSeg.b - aSeg.a);
	const VECTOR2I ac (aSeg.a - a);
	
	ecoord d = f.Cross(e);
	ecoord p = f.Cross(ac);
	ecoord q = e.Cross(ac);
	
	if(d == 0)
		return OPT_VECTOR2I();
	if (!aLines && d > 0 && (q < 0 || q > d || p < 0 || p > d))
		return OPT_VECTOR2I();
	if (!aLines && d < 0 && (q < d || p < d || p > 0 || q > 0))
		return OPT_VECTOR2I();
	if (!aLines && aIgnoreEndpoints && (q == 0 || q == d) && (p == 0 || p == d))
		return OPT_VECTOR2I();
		
	
	 VECTOR2I  ip ( aSeg.a.x + rescale(q, (ecoord)f.x, d),
					  aSeg.a.y + rescale(q, (ecoord)f.y, d) );

	 return ip;
}


bool SEG::ccw ( const VECTOR2I& a, const VECTOR2I& b, const VECTOR2I &c ) const
{
	return (ecoord)(c.y - a.y) * (b.x - a.x) > (ecoord)(b.y - a.y) * (c.x - a.x);
}

bool SEG::Collide( const SEG& aSeg, int aClearance ) const 
{
	// check for intersection 
	// fixme: move to a method
	if( ccw(a,aSeg.a,aSeg.b) != ccw(b,aSeg.a,aSeg.b) && ccw(a,b,aSeg.a) != ccw(a,b,aSeg.b) )
		return true;

#define CHK(_seg, _pt) \
	if( (_seg).PointCloserThan (_pt, aClearance ) ) return true;
	
	CHK(*this, aSeg.a);
	CHK(*this, aSeg.b);
	CHK(aSeg, a);
	CHK(aSeg, b);

#undef CHK

	return false;
}
		
bool SEG::Contains(const VECTOR2I& aP) const
{
	return PointCloserThan(aP, 1);
} 
+210 −0
Original line number Diff line number Diff line
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2013 CERN
 * @author Tomasz Wlostowski <tomasz.wlostowski@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 <math/vector2d.h>

#include <geometry/shape.h>
#include <geometry/shape_line_chain.h>
#include <geometry/shape_circle.h>
#include <geometry/shape_rect.h>

typedef typename VECTOR2I::extended_type ecoord;

static inline bool Collide( const SHAPE_CIRCLE& a, const SHAPE_CIRCLE& b, int clearance, bool needMTV, VECTOR2I& aMTV )
{
	ecoord min_dist = clearance + a.GetRadius() + b.GetRadius();
	ecoord min_dist_sq = min_dist * min_dist;
		
	const VECTOR2I delta = b.GetCenter() - a.GetCenter();

	ecoord dist_sq = delta.SquaredEuclideanNorm();

	if ( dist_sq >= min_dist_sq )
		return false;
	
	if ( needMTV )
		aMTV = delta.Resize( sqrt (abs(min_dist_sq - dist_sq)) + 1);

	return true;
}

static inline  bool Collide( const SHAPE_RECT& a, const SHAPE_CIRCLE& b, int clearance, bool needMTV, VECTOR2I& aMTV )
{
	const VECTOR2I c = b.GetCenter();
	const VECTOR2I p0 = a.GetPosition();
	const VECTOR2I size = a.GetSize();
	const ecoord r = b.GetRadius();
	const ecoord min_dist = clearance + r;
	const ecoord min_dist_sq = min_dist * min_dist;

	if (a.BBox(0).Contains(c))
		return true;
			
	const VECTOR2I vts[] = { 
						VECTOR2I(p0.x, 			p0.y),
						VECTOR2I(p0.x,			p0.y + size.y),
						VECTOR2I(p0.x + size.x, p0.y + size.y),
						VECTOR2I(p0.x + size.x, p0.y),
						VECTOR2I(p0.x, 			p0.y) };

	ecoord nearest_seg_dist_sq = VECTOR2I::ECOORD_MAX;
	VECTOR2I nearest;

	bool inside =  c.x >= p0.x && c.x <= (p0.x + size.x) 
				&& c.y >= p0.y && c.y <= (p0.y + size.y);

	if(!inside)
	{

		for (int i = 0; i < 4; i++)
		{
			const SEG seg (vts[i], vts[i+1]);
			ecoord dist_sq = seg.SquaredDistance ( c );
		

			if(dist_sq < min_dist_sq)
			{
				if(!needMTV)
					return true;
				else
				{
					nearest = seg.NearestPoint ( c );
					nearest_seg_dist_sq = dist_sq;
				}
			}
		}
	}

	if(nearest_seg_dist_sq >= min_dist_sq && !inside)
		return false;

	VECTOR2I delta = c - nearest;

	if(!needMTV)
		return true;

	if(inside)
		aMTV = -delta.Resize(sqrt(abs(r * r + nearest_seg_dist_sq) + 1));
	else
		aMTV = delta.Resize(sqrt(abs(r * r - nearest_seg_dist_sq) + 1));

	return true;
}

static inline  bool Collide( const SHAPE_CIRCLE& a, const SHAPE_LINE_CHAIN& b, int clearance, bool needMTV, VECTOR2I& aMTV )
{
	for (int s = 0; s < b.SegmentCount(); s++)
	{
		if ( a.Collide (b.CSegment(s), clearance))
			return true;
	}
	
	return false;	
}

static inline bool Collide( const SHAPE_LINE_CHAIN& a, const SHAPE_LINE_CHAIN& b, int clearance, bool needMTV, VECTOR2I& aMTV )
{
	for( int i = 0; i < b.SegmentCount() ;i++)
		if(a.Collide(b.CSegment(i), clearance))
			return true;
	return false;
}


static inline bool Collide( const SHAPE_RECT& a, const SHAPE_LINE_CHAIN& b, int clearance, bool needMTV, VECTOR2I& aMTV )
{
	for (int s = 0; s < b.SegmentCount(); s++)
	{
		SEG seg = b.CSegment(s);
			if ( a.Collide (seg, clearance))
				return true;
	}

	return false;
}



bool CollideShapes ( const SHAPE *a, const SHAPE *b, int clearance, bool needMTV, VECTOR2I& aMTV )
{
	switch(a->Type())
	{
		case SH_RECT:
			switch(b->Type())
			{
				case SH_CIRCLE:
					return Collide( *static_cast<const SHAPE_RECT *> (a), *static_cast<const SHAPE_CIRCLE *> (b), clearance, needMTV, aMTV );
				case SH_LINE_CHAIN:
					return Collide( *static_cast<const SHAPE_RECT *> (a), *static_cast<const SHAPE_LINE_CHAIN *> (b), clearance, needMTV, aMTV );
				default:
					break;
			}

		case SH_CIRCLE:
			switch(b->Type())
			{
				case SH_RECT:
					return Collide( *static_cast<const SHAPE_RECT *> (b), *static_cast<const SHAPE_CIRCLE *> (a), clearance, needMTV, aMTV );
				case SH_CIRCLE:
					return Collide( *static_cast<const SHAPE_CIRCLE *> (a), *static_cast<const SHAPE_CIRCLE *> (b), clearance, needMTV, aMTV );
				case SH_LINE_CHAIN:
					return Collide( *static_cast<const SHAPE_CIRCLE *> (a), *static_cast<const SHAPE_LINE_CHAIN *> (b), clearance, needMTV, aMTV );
				default:
					break;
			}

		case SH_LINE_CHAIN:
			switch(b->Type())
			{
				case SH_RECT:
					return Collide( *static_cast<const SHAPE_RECT *> (b), *static_cast<const SHAPE_LINE_CHAIN *> (a), clearance, needMTV, aMTV );
				case SH_CIRCLE:
					return Collide( *static_cast<const SHAPE_CIRCLE *> (b), *static_cast<const SHAPE_LINE_CHAIN *> (a), clearance, needMTV, aMTV );
				case SH_LINE_CHAIN:
					return Collide( *static_cast<const SHAPE_LINE_CHAIN *> (a), *static_cast<const SHAPE_LINE_CHAIN *> (b), clearance, needMTV, aMTV );
				default:
					break;
			}
		default:
			break;
	}

	bool unsupported_collision = true;

	assert(unsupported_collision == false);
	return false;
}


bool SHAPE::Collide ( const SHAPE *aShape, int aClerance, VECTOR2I& aMTV ) const
{
	return CollideShapes( this, aShape, aClerance, true, aMTV);
}

bool SHAPE::Collide ( const SHAPE *aShape, int aClerance ) const
{
	VECTOR2I dummy;
	return CollideShapes( this, aShape, aClerance, false, dummy);
}
		
 No newline at end of file
Loading