~metacollin/kicad/osx_distribution

« back to all changes in this revision

Viewing changes to pcbnew/exporters/idf_common.cpp

  • Committer: jean-pierre charras
  • Author(s): Cirilo Bernardo
  • Date: 2014-02-05 09:27:21 UTC
  • mto: This revision was merged to the branch mainline in revision 4660.
  • Revision ID: jp.charras@wanadoo.fr-20140205092721-l33hnnhoqogdaajq
Apply IDF tools patch from Cirilo Bernardo

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * file: idf_common.cpp
 
3
 *
 
4
 * This program source code file is part of KiCad, a free EDA CAD application.
 
5
 *
 
6
 * Copyright (C) 2013-2014  Cirilo Bernardo
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or
 
9
 * modify it under the terms of the GNU General Public License
 
10
 * as published by the Free Software Foundation; either version 2
 
11
 * of the License, or (at your option) any later version.
 
12
 *
 
13
 * This program is distributed in the hope that it will be useful,
 
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 * GNU General Public License for more details.
 
17
 *
 
18
 * You should have received a copy of the GNU General Public License
 
19
 * along with this program; if not, you may find one here:
 
20
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 
21
 * or you may search the http://www.gnu.org website for the version 2 license,
 
22
 * or you may write to the Free Software Foundation, Inc.,
 
23
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 
24
 */
 
25
 
 
26
#include <list>
 
27
#include <string>
 
28
#include <iostream>
 
29
#include <cstdio>
 
30
#include <cmath>
 
31
#include <richio.h>
 
32
#include <idf_common.h>
 
33
#include <build_version.h>
 
34
 
 
35
#ifdef DEBUG_IDF
 
36
void IDF3::PrintSeg( IDF_SEGMENT* aSegment )
 
37
{
 
38
    if( aSegment->IsCircle() )
 
39
    {
 
40
        fprintf(stdout, "printSeg(): CIRCLE: C(%.3f, %.3f) P(%.3f, %.3f) rad. %.3f\n",
 
41
                aSegment->startPoint.x, aSegment->startPoint.y,
 
42
                aSegment->endPoint.x, aSegment->endPoint.y,
 
43
                aSegment->radius );
 
44
        return;
 
45
    }
 
46
 
 
47
    if( aSegment->angle < -MIN_ANG || aSegment->angle > MIN_ANG )
 
48
    {
 
49
        fprintf(stdout, "printSeg(): ARC: p1(%.3f, %.3f) p2(%.3f, %.3f) ang. %.3f\n",
 
50
                aSegment->startPoint.x, aSegment->startPoint.y,
 
51
                aSegment->endPoint.x, aSegment->endPoint.y,
 
52
                aSegment->angle );
 
53
        return;
 
54
    }
 
55
 
 
56
    fprintf(stdout, "printSeg(): LINE: p1(%.3f, %.3f) p2(%.3f, %.3f)\n",
 
57
            aSegment->startPoint.x, aSegment->startPoint.y,
 
58
            aSegment->endPoint.x, aSegment->endPoint.y );
 
59
 
 
60
    return;
 
61
}
 
62
#endif
 
63
 
 
64
 
 
65
bool IDF_POINT::Matches( const IDF_POINT& aPoint, double aRadius )
 
66
{
 
67
    double dx = x - aPoint.x;
 
68
    double dy = y - aPoint.y;
 
69
 
 
70
    double d2 = dx * dx + dy * dy;
 
71
 
 
72
    if( d2 <= aRadius * aRadius )
 
73
        return true;
 
74
 
 
75
    return false;
 
76
}
 
77
 
 
78
 
 
79
double IDF_POINT::CalcDistance( const IDF_POINT& aPoint ) const
 
80
{
 
81
    double dx   = aPoint.x - x;
 
82
    double dy   = aPoint.y - y;
 
83
    double dist = sqrt( dx * dx + dy * dy );
 
84
 
 
85
    return dist;
 
86
}
 
87
 
 
88
 
 
89
double IDF3::CalcAngleRad( const IDF_POINT& aStartPoint, const IDF_POINT& aEndPoint )
 
90
{
 
91
    return atan2( aEndPoint.y - aStartPoint.y, aEndPoint.x - aStartPoint.x );
 
92
}
 
93
 
 
94
 
 
95
double IDF3::CalcAngleDeg( const IDF_POINT& aStartPoint, const IDF_POINT& aEndPoint )
 
96
{
 
97
    double ang = CalcAngleRad( aStartPoint, aEndPoint );
 
98
 
 
99
    // round to thousandths of a degree
 
100
    int iang = int (ang / M_PI * 1800000.0);
 
101
 
 
102
    ang = iang / 10000.0;
 
103
 
 
104
    return ang;
 
105
}
 
106
 
 
107
 
 
108
void IDF3::GetOutline( std::list<IDF_SEGMENT*>& aLines,
 
109
                       IDF_OUTLINE& aOutline )
 
110
{
 
111
    aOutline.Clear();
 
112
 
 
113
    // NOTE: To tell if the point order is CCW or CW,
 
114
    // sum all:  (endPoint.X[n] - startPoint.X[n])*(endPoint[n] + startPoint.Y[n])
 
115
    // If the result is >0, the direction is CW, otherwise
 
116
    // it is CCW. Note that the result cannot be 0 unless
 
117
    // we have a bounded area of 0.
 
118
 
 
119
    // First we find the segment with the leftmost point
 
120
    std::list<IDF_SEGMENT*>::iterator bl    = aLines.begin();
 
121
    std::list<IDF_SEGMENT*>::iterator el    = aLines.end();
 
122
    std::list<IDF_SEGMENT*>::iterator idx   = bl++;       // iterator for the object with minX
 
123
 
 
124
    double minx = (*idx)->GetMinX();
 
125
    double curx;
 
126
 
 
127
    while( bl != el )
 
128
    {
 
129
        curx = (*bl)->GetMinX();
 
130
 
 
131
        if( curx < minx )
 
132
        {
 
133
            minx = curx;
 
134
            idx = bl;
 
135
        }
 
136
 
 
137
        ++bl;
 
138
    }
 
139
 
 
140
    aOutline.push( *idx );
 
141
#ifdef DEBUG_IDF
 
142
    PrintSeg( *idx );
 
143
#endif
 
144
    aLines.erase( idx );
 
145
 
 
146
    // If the item is a circle then we're done
 
147
    if( aOutline.front()->IsCircle() )
 
148
        return;
 
149
 
 
150
    // Assemble the loop
 
151
    bool complete = false;  // set if loop is complete
 
152
    bool matched;           // set if a segment's end point was matched
 
153
 
 
154
    while( !complete )
 
155
    {
 
156
        matched = false;
 
157
        bl  = aLines.begin();
 
158
        el  = aLines.end();
 
159
 
 
160
        while( bl != el && !matched )
 
161
        {
 
162
            if( (*bl)->MatchesStart( aOutline.back()->endPoint ) )
 
163
            {
 
164
                if( (*bl)->IsCircle() )
 
165
                {
 
166
                    // a circle on the perimeter is pathological but we just ignore it
 
167
                    ++bl;
 
168
                }
 
169
                else
 
170
                {
 
171
                    matched = true;
 
172
#ifdef DEBUG_IDF
 
173
                    PrintSeg( *bl );
 
174
#endif
 
175
                    aOutline.push( *bl );
 
176
                    aLines.erase( bl );
 
177
                }
 
178
 
 
179
                continue;
 
180
            }
 
181
 
 
182
            ++bl;
 
183
        }
 
184
 
 
185
        if( !matched )
 
186
        {
 
187
            // attempt to match the end points
 
188
            bl  = aLines.begin();
 
189
            el  = aLines.end();
 
190
 
 
191
            while( bl != el && !matched )
 
192
            {
 
193
                if( (*bl)->MatchesEnd( aOutline.back()->endPoint ) )
 
194
                {
 
195
                    if( (*bl)->IsCircle() )
 
196
                    {
 
197
                        // a circle on the perimeter is pathological but we just ignore it
 
198
                        ++bl;
 
199
                    }
 
200
                    else
 
201
                    {
 
202
                        matched = true;
 
203
                        (*bl)->SwapEnds();
 
204
#ifdef DEBUG_IDF
 
205
                        printSeg( *bl );
 
206
#endif
 
207
                        aOutline.push( *bl );
 
208
                        aLines.erase( bl );
 
209
                    }
 
210
 
 
211
                    continue;
 
212
                }
 
213
 
 
214
                ++bl;
 
215
            }
 
216
        }
 
217
 
 
218
        if( !matched )
 
219
        {
 
220
            // still no match - attempt to close the loop
 
221
            if( (aOutline.size() > 1) || ( aOutline.front()->angle < -MIN_ANG )
 
222
                || ( aOutline.front()->angle > MIN_ANG ) )
 
223
            {
 
224
                // close the loop
 
225
                IDF_SEGMENT* seg = new IDF_SEGMENT( aOutline.back()->endPoint,
 
226
                                                    aOutline.front()->startPoint );
 
227
 
 
228
                if( seg )
 
229
                {
 
230
                    complete = true;
 
231
#ifdef DEBUG_IDF
 
232
                    printSeg( seg );
 
233
#endif
 
234
                    aOutline.push( seg );
 
235
                    break;
 
236
                }
 
237
            }
 
238
 
 
239
            // the outline is bad; drop the segments
 
240
            aOutline.Clear();
 
241
 
 
242
            return;
 
243
        }
 
244
 
 
245
        // check if the loop is complete
 
246
        if( aOutline.front()->MatchesStart( aOutline.back()->endPoint ) )
 
247
        {
 
248
            complete = true;
 
249
            break;
 
250
        }
 
251
    }
 
252
}
 
253
 
 
254
 
 
255
IDF_SEGMENT::IDF_SEGMENT()
 
256
{
 
257
    angle = 0.0;
 
258
    offsetAngle = 0.0;
 
259
    radius = 0.0;
 
260
}
 
261
 
 
262
 
 
263
IDF_SEGMENT::IDF_SEGMENT( const IDF_POINT& aStartPoint, const IDF_POINT& aEndPoint )
 
264
{
 
265
    angle = 0.0;
 
266
    offsetAngle = 0.0;
 
267
    radius = 0.0;
 
268
    startPoint = aStartPoint;
 
269
    endPoint = aEndPoint;
 
270
}
 
271
 
 
272
 
 
273
IDF_SEGMENT::IDF_SEGMENT( const IDF_POINT& aStartPoint,
 
274
                          const IDF_POINT& aEndPoint,
 
275
                          double aAngle,
 
276
                          bool aFromKicad )
 
277
{
 
278
    double diff = abs( aAngle ) - 360.0;
 
279
 
 
280
    if( ( diff < MIN_ANG
 
281
        && diff > -MIN_ANG ) || ( aAngle < MIN_ANG && aAngle > -MIN_ANG ) || (!aFromKicad) )
 
282
    {
 
283
        angle = 0.0;
 
284
        startPoint = aStartPoint;
 
285
        endPoint = aEndPoint;
 
286
 
 
287
        if( diff < MIN_ANG && diff > -MIN_ANG )
 
288
        {
 
289
            angle = 360.0;
 
290
            center = aStartPoint;
 
291
            offsetAngle = 0.0;
 
292
            radius = aStartPoint.CalcDistance( aEndPoint );
 
293
        }
 
294
        else if( aAngle < MIN_ANG && aAngle > -MIN_ANG )
 
295
        {
 
296
            CalcCenterAndRadius();
 
297
        }
 
298
 
 
299
        return;
 
300
    }
 
301
 
 
302
    // we need to convert from the KiCad arc convention
 
303
    angle = aAngle;
 
304
 
 
305
    center = aStartPoint;
 
306
 
 
307
    offsetAngle = IDF3::CalcAngleDeg( aStartPoint, aEndPoint );
 
308
 
 
309
    radius = aStartPoint.CalcDistance( aEndPoint );
 
310
 
 
311
    startPoint = aEndPoint;
 
312
 
 
313
    double ang = offsetAngle + aAngle;
 
314
    ang = (ang / 180.0) * M_PI;
 
315
 
 
316
    endPoint.x  = ( radius * cos( ang ) ) + center.x;
 
317
    endPoint.y  = ( radius * sin( ang ) ) + center.y;
 
318
}
 
319
 
 
320
 
 
321
bool IDF_SEGMENT::MatchesStart( const IDF_POINT& aPoint, double aRadius )
 
322
{
 
323
    return startPoint.Matches( aPoint, aRadius );
 
324
}
 
325
 
 
326
 
 
327
bool IDF_SEGMENT::MatchesEnd( const IDF_POINT& aPoint, double aRadius )
 
328
{
 
329
    return endPoint.Matches( aPoint, aRadius );
 
330
}
 
331
 
 
332
 
 
333
void IDF_SEGMENT::CalcCenterAndRadius( void )
 
334
{
 
335
    // NOTE:  this routine does not check if the points are the same
 
336
    // or too close to be sensible in a production setting.
 
337
 
 
338
    double offAng = IDF3::CalcAngleRad( startPoint, endPoint );
 
339
    double d = startPoint.CalcDistance( endPoint ) / 2.0;
 
340
    double xm   = ( startPoint.x + endPoint.x ) * 0.5;
 
341
    double ym   = ( startPoint.y + endPoint.y ) * 0.5;
 
342
 
 
343
    radius = d / sin( angle * M_PI / 180.0 );
 
344
 
 
345
    if( radius < 0.0 )
 
346
    {
 
347
        radius = -radius;
 
348
    }
 
349
 
 
350
    // calculate the height of the triangle with base d and hypotenuse r
 
351
    double dh2 = radius * radius - d * d;
 
352
 
 
353
    if( dh2 < 0 )
 
354
    {
 
355
        // this should only ever happen due to rounding errors when r == d
 
356
        dh2 = 0;
 
357
    }
 
358
 
 
359
    double h = sqrt( dh2 );
 
360
 
 
361
    if( angle > 0.0 )
 
362
        offAng += M_PI2;
 
363
    else
 
364
        offAng -= M_PI2;
 
365
 
 
366
    if( ( angle > M_PI ) || ( angle < -M_PI ) )
 
367
        offAng += M_PI;
 
368
 
 
369
    center.x = h * cos( offAng ) + xm;
 
370
    center.y = h * sin( offAng ) + ym;
 
371
 
 
372
    offsetAngle = IDF3::CalcAngleDeg( center, startPoint );
 
373
}
 
374
 
 
375
 
 
376
bool IDF_SEGMENT::IsCircle( void )
 
377
{
 
378
    double diff = abs( angle ) - 360.0;
 
379
 
 
380
    if( ( diff < MIN_ANG ) && ( diff > -MIN_ANG ) )
 
381
        return true;
 
382
 
 
383
    return false;
 
384
}
 
385
 
 
386
 
 
387
double IDF_SEGMENT::GetMinX( void )
 
388
{
 
389
    if( angle == 0.0 )
 
390
        return std::min( startPoint.x, endPoint.x );
 
391
 
 
392
    // Calculate the leftmost point of the circle or arc
 
393
 
 
394
    if( IsCircle() )
 
395
    {
 
396
        // if only everything were this easy
 
397
        return center.x - radius;
 
398
    }
 
399
 
 
400
    // cases:
 
401
    // 1. CCW arc: if offset + included angle >= 180 deg then
 
402
    // MinX = center.x - radius, otherwise MinX is the
 
403
    // same as for the case of a line.
 
404
    // 2. CW arc: if offset + included angle <= -180 deg then
 
405
    // MinX = center.x - radius, otherwise MinX is the
 
406
    // same as for the case of a line.
 
407
 
 
408
    if( angle > 0 )
 
409
    {
 
410
        // CCW case
 
411
        if( ( offsetAngle + angle ) >= 180.0 )
 
412
        {
 
413
            return center.x - radius;
 
414
        }
 
415
        else
 
416
        {
 
417
            return std::min( startPoint.x, endPoint.x );
 
418
        }
 
419
    }
 
420
 
 
421
    // CW case
 
422
    if( ( offsetAngle + angle ) <= -180.0 )
 
423
    {
 
424
        return center.x - radius;
 
425
    }
 
426
 
 
427
    return std::min( startPoint.x, endPoint.x );
 
428
}
 
429
 
 
430
 
 
431
void IDF_SEGMENT::SwapEnds( void )
 
432
{
 
433
    if( IsCircle() )
 
434
    {
 
435
        // reverse the direction
 
436
        angle = -angle;
 
437
        return;
 
438
    }
 
439
 
 
440
    IDF_POINT tmp = startPoint;
 
441
    startPoint = endPoint;
 
442
    endPoint = tmp;
 
443
 
 
444
    if( ( angle < MIN_ANG ) && ( angle > -MIN_ANG ) )
 
445
        return;         // nothing more to do
 
446
 
 
447
        // change the direction of the arc
 
448
        angle = -angle;
 
449
    // calculate the new offset angle
 
450
    offsetAngle = IDF3::CalcAngleDeg( center, startPoint );
 
451
}
 
452
 
 
453
 
 
454
void IDF_OUTLINE::push( IDF_SEGMENT* item )
 
455
{
 
456
    if( !outline.empty() )
 
457
    {
 
458
        if( item->IsCircle() )
 
459
        {
 
460
            // not allowed
 
461
            wxString msg = wxT( "INVALID GEOMETRY: a circle is being added to a non-empty outline" );
 
462
            THROW_IO_ERROR( msg );
 
463
        }
 
464
        else
 
465
        {
 
466
            if( outline.back()->IsCircle() )
 
467
            {
 
468
                // we can't add lines to a circle
 
469
                wxString msg = wxT( "INVALID GEOMETRY: a line is being added to a circular outline" );
 
470
                THROW_IO_ERROR( msg );
 
471
            }
 
472
            else if( !item->MatchesStart( outline.back()->endPoint ) )
 
473
            {
 
474
                // startPoint[N] != endPoint[N -1]
 
475
                wxString msg = wxT( "INVALID GEOMETRY: disjoint segments" );
 
476
                THROW_IO_ERROR( msg );
 
477
            }
 
478
        }
 
479
    }
 
480
 
 
481
    outline.push_back( item );
 
482
    dir += ( outline.back()->endPoint.x - outline.back()->startPoint.x )
 
483
    * ( outline.back()->endPoint.y + outline.back()->startPoint.y );
 
484
}