--- pcb-gtk-springs/src/autoplace.c	2005-04-02 10:31:26.000000000 +0200
+++ pcb-gtk/src/autoplace.c	2005-03-28 22:42:27.000000000 +0200
@@ -7,9 +7,6 @@
  *  Copyright (C) 1994,1995,1996 Thomas Nau
  *  Copyright (C) 1998,1999,2000,2001 harry eaton
  *
- *
- *  Includes Spring-Mass autplacer algorithm Copyright by Peter Reiter 2005
- *
  *  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
@@ -38,8 +35,6 @@
 /* functions used to autoplace elements.
  */
 
-//#define DEBUG_SPRINGS 1
-
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -72,8 +67,8 @@
 RCSID("$Id: autoplace.c,v 1.12 2005/01/03 12:56:58 danmc Exp $");
 
 #define EXPANDRECTXY(r1, x1, y1, x2, y2) { \
-  (r1)->X1=MIN((r1)->X1, (x1)); (r1)->Y1=MIN((r1)->Y1, (y1)); \
-  (r1)->X2=MAX((r1)->X2, (x2)); (r1)->Y2=MAX((r1)->Y2, (y2)); \
+  r1->X1=MIN(r1->X1, x1); r1->Y1=MIN(r1->Y1, y1); \
+  r1->X2=MAX(r1->X2, x2); r1->Y2=MAX(r1->Y2, y2); \
 }
 #define EXPANDRECT(r1, r2) EXPANDRECTXY(r1, r2->X1, r2->Y1, r2->X2, r2->Y2)
 
@@ -209,7 +204,7 @@
   return list;
 }
 
-#if 1				/* only for debugging box lists */
+#if 0				/* only for debugging box lists */
 #include "create.h"
 /* makes a line on the solder layer surrounding all boxes in blist */
 static void
@@ -217,7 +212,6 @@
 {
   Cardinal i;
   LayerTypePtr SLayer = &(PCB->Data->Layer[MAX_LAYER + SOLDER_LAYER]);
-  printf("Showing %d boxes\n", blist->BoxN);
   for (i = 0; i < blist->BoxN; i++)
     {
       CreateNewLineOnLayer (SLayer, blist->Box[i].X1, blist->Box[i].Y1,
@@ -329,7 +323,6 @@
  *  "Placement and Routing of Electronic Modules" edited by Michael Pecht
  *  Marcel Dekker, Inc. 1993.  ISBN: 0-8247-8916-4 TK7868.P7.P57 1993
  */
-
 static double
 ComputeCost (NetListTypePtr Nets, double T0, double T)
 {
@@ -442,7 +435,7 @@
 	  {
 	    thickness = pad->Thickness / 2;
 	    clearance = pad->Clearance * 2;
-            EXPANDRECTXY (box,
+	  EXPANDRECTXY (box,
 			  MIN (pad->Point1.X,
 				 pad->Point2.X) - (thickness +
 						     clearance),
@@ -614,475 +607,6 @@
   return W + (delta1 + delta2 + delta3 - delta4 + delta5);
 }
 
-//====================================================================================== CUT SPRINGS ====
-
-double spr_k = 0.001;
-double spr_k_multiplier = 0.99;
-
-typedef struct spr_pin_list_elem {
-  LocationType x,y;
-  LocationType w,h;
-  PinTypePtr pin;
-  struct spr_pin_list_elem *next;
-} spr_pin_list_elem;
-
-typedef struct spr_elem {
-  LocationType x,y;
-  LocationType w,h;
-  double vx,vy;
-  double mx,my;
-  double fx,fy;
-  double mass;
-  Boolean can_move;
-  ElementTypePtr element;
-  Boolean component_side;
-  struct spr_pin_list_elem *other_side;
-  struct spr_elem *next;
-} spr_elem;
-
-typedef struct spr_spring {
-  spr_elem *elem_a;
-  spr_elem *elem_b;
-  LocationType ax,ay;
-  LocationType bx,by;
-  double k;
-  struct spr_spring *next;
-} spr_spring;
-
-spr_elem *new_elem( spr_elem **list ) {
-  spr_elem *n;
-  n = malloc(sizeof(spr_elem));
-  memset(n,0,sizeof(spr_elem));
-  n->next = *list;
-  *list = n;
-  return n;
-}
-
-spr_spring *new_spring( spr_spring **list ) {
-  spr_spring *n;
-  n = malloc(sizeof(spr_spring));
-  memset(n,0,sizeof(spr_spring));
-  n->next = *list;
-  *list = n;
-  return n;
-}
-
-void spr_free_elements( spr_elem **list ) {
-  while( *list ) {
-    spr_elem *s = (*list)->next;
-    spr_pin_list_elem **ple = &(*list)->other_side;
-    while( *ple ) {
-      spr_pin_list_elem *p = (*ple)->next;
-      free(*ple);
-      *ple = p;
-    }
-    free(*list);
-    *list = s;
-  }
-}
-
-void spr_free_springs( spr_spring **list ) {
-  while( *list ) {
-    spr_spring *s = (*list)->next;
-    free(*list);
-    *list = s;
-  }
-}
-
-int spr_update_collision_forces( spr_elem *elm, spr_spring *spr ) {
-  int found = 0;
-  if( !elm ) return 0;
-  while( elm->next ) {
-    spr_elem *e = elm->next;
-    while( e ) {
-      double mass_involved;
-      BoxListType bl;
-      BoxListTypePtr blptr = &bl;
-      bl.BoxN = 2;
-      bl.Box = malloc(sizeof(BoxType)*2);
-      bl.Box[0] = elm->element->BoundingBox;
-      bl.Box[1] = e->element->BoundingBox;
-      mass_involved = fabs( ComputeIntersectionArea( blptr ) );
-      
-      if( mass_involved > 0.01 )  {
-        if( elm->component_side == e->component_side ) {
-          //elements are on the same side
-          spr_spring *s;
-          double fx = e->x - elm->x;
-          double fy = e->y - elm->y;
-          double len = sqrt( fx*fx + fy*fy );
-          fx *= mass_involved;
-          fy *= mass_involved;
-          fx /= len;
-          fy /= len;
-          elm->fx -= fx;
-          elm->fy -= fy;
-          e->fx += fx;
-          e->fy += fy;
-          found = 1;
-
-          s = spr;
-          while( s ) {
-            if( (s->elem_a == elm && s->elem_b == e) ||
-                (s->elem_b == elm && s->elem_a == e) )
-            {
-              //weaken springs that connect intersecting elements
-              s->k *= spr_k_multiplier;
-//              spr_k *= spr_k_multiplier;
-            }
-
-            s = s->next;
-          }
-#if DEBUG_SPRINGS
-          printf("Intersection found [%s / %s], force %f %f\n", e->element->Name[1].TextString, elm->element->Name[1].TextString, fx,fy);
-#endif
-        } else {
-          //interacting elements are on different sides
-          //need to check other_side lists
-          spr_pin_list_elem *ple;
-        
- //         printf("Collision on different sides %f\n", mass_involved);
- //         showboxes(blptr);
-         
-          bl.Box[0] = elm->element->BoundingBox;
-          ple = e->other_side;          
-   //TODO: replace by PIN_LOOP( e->element );
-          while( ple ) {
-//          PIN_LOOP( e->element );
-//          {
-            bl.Box[1] = ple->pin->BoundingBox;
-            mass_involved = fabs(ComputeIntersectionArea(blptr));
-            if( mass_involved > 0.01 ) {
-              double fx = ple->pin->X - elm->x;
-              double fy = ple->pin->Y - elm->y;
-              double len = sqrt( fx*fx + fy*fy );
-              fx *= mass_involved;
-              fy *= mass_involved;
-              fx /= len;
-              fy /= len;
-              elm->fx -= fx;
-              elm->fy -= fy;
-              e->fx += fx;
-              e->fy += fy;
-              found = 1;
-//              showboxes(blptr);
-            };
-            ple = ple->next;
-          }
-//          END_LOOP;
-          
-          bl.Box[0] = e->element->BoundingBox;
-          ple = elm->other_side;
-          while( ple ) {
-            bl.Box[1] = ple->pin->BoundingBox;
-            mass_involved = fabs(ComputeIntersectionArea(blptr));
-            if( mass_involved > 0.01 ) {
-              double fx = ple->pin->X - elm->x;
-              double fy = ple->pin->Y - elm->y;
-              double len = sqrt( fx*fx + fy*fy );
-              fx *= mass_involved;
-              fy *= mass_involved;
-              fx /= len;
-              fy /= len;
-              elm->fx -= fx;
-              elm->fy -= fy;
-              e->fx += fx;
-              e->fy += fy;
-              found = 1;
-//              showboxes(blptr);
-            }
-            ple = ple->next;
-          };
-        }
-      }
-      free(bl.Box);
-      e = e->next;
-    }
-    elm = elm->next;
-  }
-  return found;
-}
-
-int spr_update_spring_forces( spr_elem *elm, spr_spring *spr ) {
-  while( spr ) {
-    //FIXME for now i ignore momentum, maybe later
-    LocationType x1 = spr->ax + spr->elem_a->x;
-    LocationType y1 = spr->ay + spr->elem_a->y;
-    LocationType x2 = spr->bx + spr->elem_b->x;
-    LocationType y2 = spr->by + spr->elem_b->y;
-    spr->elem_a->fx += spr->k * (x2-x1);
-    spr->elem_a->fy += spr->k * (y2-y1);
-    spr->elem_b->fx -= spr->k * (x2-x1);
-    spr->elem_b->fy -= spr->k * (y2-y1);
-//    printf("Spring [%d,%d] to [%d,%d] k=%f\n",x1,y1,x2,y2,spr->k);
-//    printf("element a force %f,%f; element b force %f,%f\n",
-//                spr->elem_a->fx,spr->elem_a->fy,
-//                spr->elem_b->fx,spr->elem_b->fy);
-    spr = spr->next;
-  }
-  return 0;
-}
-
-void spr_create_spring( spr_elem **eroot, spr_spring **sroot, ConnectionTypePtr c1, ConnectionTypePtr c2 ) {
-  
-                spr_elem *e;
-                spr_elem *a = NULL;
-                spr_elem *b = NULL;
-                spr_spring *s = NULL;
-
-                e = *eroot;
-                while( e ) 
-                {
-                  if( e->element == (ElementTypePtr)c1->ptr1 )
-                    a = e;
-                  if( e->element == (ElementTypePtr)c2->ptr1 )
-                    b = e;
-                  e = e->next;
-                }
-                    
-                if( a != NULL && b != NULL && a != b && (a->can_move || b->can_move) )
-                {
-                  s = new_spring( sroot );
-                  s->k = spr_k;
-
-                  switch( c1->type ) 
-                  {
-                    case PAD_TYPE: s->ax = (((PadTypePtr)c1->ptr2)->Point1.X +
-                                            ((PadTypePtr)c1->ptr2)->Point2.X) / 2;
-                                   s->ay = (((PadTypePtr)c1->ptr2)->Point1.Y +
-                                            ((PadTypePtr)c1->ptr2)->Point2.Y) / 2;
-                                   break;
-                    case PIN_TYPE: s->ax = ((PinTypePtr)c1->ptr2)->X;
-                                   s->ay = ((PinTypePtr)c1->ptr2)->Y;
-                                   break;
-                  }
-                  
-                  switch( c2->type ) 
-                  {
-                    case PAD_TYPE: s->bx = (((PadTypePtr)c2->ptr2)->Point1.X +
-                                            ((PadTypePtr)c2->ptr2)->Point2.X) / 2;
-                                   s->by = (((PadTypePtr)c2->ptr2)->Point1.Y +
-                                            ((PadTypePtr)c2->ptr2)->Point2.Y) / 2;
-                                   break;
-                    case PIN_TYPE: s->bx = ((PinTypePtr)c2->ptr2)->X;
-                                   s->by = ((PinTypePtr)c2->ptr2)->Y;
-                                   break;
-                  }
-
-                  s->ax -= a->x;
-                  s->ay -= a->y;
-                  s->bx -= b->x;
-                  s->by -= b->y;
-
-                  s->elem_a = a;
-                  s->elem_b = b;
-#if DEBUG_SPRINGS
-                  printf("Net %d, connection %d,%d [%d,%d] [%d,%d]\n", i,j,k, s->ax,s->ay, s->bx,s->by);
-#endif
-                }
-}
-
-void SpringAlg( NetListTypePtr Nets ) {
-        //selected = PtrN, (ElementType *)Ptr
-        spr_elem *eroot = NULL;
-        spr_spring *sroot = NULL;
-        Cardinal i,j,k;
-        
-        ELEMENT_LOOP(PCB->Data); 
-          { 
-            BoxType b;
-            //twórz obiekty
-            spr_elem *e = new_elem(&eroot);
-
-            b = element->BoundingBox;
-            e->element = element;
-            e->can_move = TEST_FLAG( SELECTEDFLAG, element );
-            e->component_side = ! TEST_FLAG( ONSOLDERFLAG, element );
-            
-            //grow bounding boxes to allow tracks in between
-            PIN_LOOP( element ) ;
-              {
-                LocationType thickness = pin->Thickness / 2;
-                LocationType clearance = pin->Clearance * 2;
-	        EXPANDRECTXY (&b,
-			  pin->X - (thickness + clearance),
-			  pin->Y - (thickness + clearance),
-			  pin->X + (thickness + clearance),
-			  pin->Y + (thickness + clearance));
-                
-                spr_pin_list_elem *ple;
-                ple = malloc(sizeof(spr_pin_list_elem));
-                memset(ple,0,sizeof(spr_pin_list_elem));
-                ple->x = pin->X - e->x;
-                ple->y = pin->Y - e->y;
-                ple->w = ple->h = pin->Thickness + pin->Clearance;
-                ple->next = e->other_side;
-                ple->pin = pin;
-                e->other_side = ple;
-                //new spr_pin_list_elem @ pin->X - e->x, pin->Y - e->y
-//                printf("PIN @ %d %d\n", ple->x, ple->y );
-              }
-            END_LOOP;
-            PAD_LOOP( element ) ;
-              {
-	        LocationType thickness = pad->Thickness / 2;
-	        LocationType clearance = pad->Clearance * 2;
-	        EXPANDRECTXY (&b,
-			  MIN (pad->Point1.X,
-				 pad->Point2.X) - (thickness +
-						     clearance),
-			  MIN (pad->Point1.Y,
-				 pad->Point2.Y) - (thickness +
-						     clearance),
-			  MAX (pad->Point1.X,
-				 pad->Point2.X) + (thickness +
-						     clearance),
-			  MAX (pad->Point1.Y,
-				 pad->Point2.Y) + (thickness +
-						     clearance));
-//                printf("PAD @ %d %d\n", pad->Point1.X, pad->Point1.Y );
-              }
-            END_LOOP;
-            e->w = fabs(b.X2 - b.X1);
-            e->h = fabs(b.Y2 - b.Y1);
-            e->x = fabs(b.X2 + b.X1)/2;
-            e->y = fabs(b.Y2 + b.Y1)/2;
-            e->mass = ((double)e->w/10)*((double)e->h/10);
-            if( !e->mass ) e->mass = 1;
-#if 0
-            if( e->can_move ) {
-              printf("Box [%d,%d]x[%d,%d]\n",element->BoundingBox.X1,element->BoundingBox.Y1,element->BoundingBox.X2,element->BoundingBox.Y2);
-              printf("New element %s (%s) [%d,%d,%d,%d]\n", 
-                e->element->Name[1].TextString, e->can_move ? "movable" : "static",
-                e->x, e->y, e->w, e->h );
-            }
-#endif
-          } 
-        END_LOOP;
-
-        {
-          spr_elem *e;
-          double max = 0;
-          double min;
-          //find max mass
-          e = eroot;
-          while(e) {
-            if( e->mass > max )
-              max = e->mass;
-            e = e->next;
-          }
-          //find min mass
-          e = eroot;
-          min = max;
-          while(e) {
-            if( e->mass < min )
-              min = e->mass;
-            e = e->next;
-          }
-          //normalize
-          e = eroot;
-          while(e) {
-            e->mass /= min;
-            e = e->next;
-          }
-        }
-        
-        for( i=0; i<Nets->NetN; i++ ) 
-          {
-            long double min_distance = -1;
-            Cardinal closest = -1;
-            for( j=0; j<Nets->Net[i].ConnectionN-1; j++ )
-              {
-                for( k=j+1; k<Nets->Net[i].ConnectionN; k++ )
-                  {
-                    ConnectionTypePtr c1 = (ConnectionTypePtr)&Nets->Net[i].Connection[j];
-                    ConnectionTypePtr c2 = (ConnectionTypePtr)&Nets->Net[i].Connection[closest];
-                    long double distance;
-
-                    distance = sqrt((c1->X - c2->X)*(c1->X - c2->X) + (c1->Y - c2->Y)*(c1->Y - c2->Y));
-
-                    if( distance < min_distance || min_distance == -1 ) {
-                      min_distance = distance;
-                      closest = k;
-                    }
-                  }
-                  
-                ConnectionTypePtr c1 = (ConnectionTypePtr)&Nets->Net[i].Connection[j];
-                ConnectionTypePtr c2 = (ConnectionTypePtr)&Nets->Net[i].Connection[closest];
-
-                spr_create_spring( &eroot, &sroot, c1, c2 );
-              }
-          }
-
-        for(i=0;i<50;i++)
-        {
-          //initialize forces
-          //speeds too for now
-          spr_elem *e;
-          e = eroot;
-          while( e ) {
-            e->vx = e->vy = e->fx = e->fy = 0;
-            e = e->next;
-          }
-          spr_update_collision_forces(eroot,sroot);
-          spr_update_spring_forces(eroot,sroot);
-          //update speeds
-          e = eroot;
-          while( e ) {
-            if( e->can_move ) {
-//              printf("Element mass is %f\n",e->mass);
-//              printf("Velocity before: %d %d\n",(int)e->vx,(int)e->vy);
-              e->vx += e->fx/e->mass;
-              e->vy += e->fy/e->mass;
-//              printf("Velocity after: %d %d\n",(int)e->vx,(int)e->vy);
-            }
-            e = e->next;
-          }
-          //update locations
-          e = eroot;
-          while( e ) {
-            if( e->can_move ) {
-//              printf("Location before: %d %d\n",(int)e->x,(int)e->y);
-              MoveElementLowLevel (PCB->Data, e->element, e->vx, e->vy);
-              e->x += e->vx; 
-              e->y += e->vy;
-//              printf("Loaction after: %d %d\n",(int)e->x,(int)e->y);
-            }
-            e = e->next;
-          }
-        }
-
-        spr_free_elements(&eroot);
-        spr_free_springs(&sroot);
-}
-
-Boolean SpringMassSelected (void) {
-  NetListTypePtr Nets;
-  Boolean changed = False;
-  
-  Nets = ProcNetlist (&PCB->NetlistLib);
-  if (!Nets)
-    {
-      Message (_("Can't add rat lines because no netlist is loaded.\n"));
-      goto done;
-    }
-
-  changed = True;
-  SpringAlg(Nets);
-//  changed = True;
-//  goto done;
-done:
-
-  if (changed)
-    {
-      DeleteRats (False);
-      AddAllRats (False, NULL);
-      ClearAndRedrawOutput ();
-    }
-  return (changed);
-}
-//====================================================================================== CUT SPRINGS ====
-
 /* ---------------------------------------------------------------------------
  * Perturb:
  *  1) flip SMD from solder side to component side or vice-versa.
@@ -1150,7 +674,6 @@
     default:
       assert (0);
     }
-
   return pt;
 }
 
@@ -1273,12 +796,12 @@
   }
   /* now anneal in earnest */
   {
-    double T = T0;   
+    double T = T0;
     long steps = 0;
     int good_moves = 0, moves = 0;
     const int good_move_cutoff = CostParameter.m * Selected.PtrN;
     const int move_cutoff = 2 * good_move_cutoff;
-    printf ("Starting cost is %.0f\n",ComputeCost (Nets, T0, 5));    
+    printf ("Starting cost is %.0f\n",ComputeCost (Nets, T0, 5));
     C0 = ComputeCost (Nets, T0, T);
     while (1)
       {
