implement scaling
Stefan Schuermans

Stefan Schuermans commited on 2013-07-06 20:31:45
Showing 8 changed files, with 151 additions and 16 deletions.

... ...
@@ -483,11 +483,12 @@ write_ngc logo.ngc]]></programlisting>
483 483
 
484 484
       <para>
485 485
 
486
-        The coordinates from the drawing are rotated around the origin and the
487
-        offsets in X and Y direction are added afterwards.  The resulting
488
-        coordinate is then used for G-code generation.  This allows to adapt
489
-        the coordinate system between the drawing and the CNC mill.  The offset
490
-        can also be used to cut a workpiece drawn once at multiple positions.
486
+        The coordinates from the drawing are scaled using different factors
487
+        for X and Y directions, rotated around the origin and the offsets in
488
+        X and Y direction are added afterwards.  The resulting coordinate is
489
+        then used for G-code generation.  This allows to adapt the coordinate
490
+        system between the drawing and the CNC mill.  The offset can also be
491
+        used to cut a workpiece drawn once at multiple positions.
491 492
 
492 493
       </para>
493 494
 
... ...
@@ -507,11 +508,12 @@ write_ngc logo.ngc]]></programlisting>
507 508
 
508 509
       <para>
509 510
 
510
-        The coordinates from the drawing are rotated around the origin and
511
-        the offsets in X and Y direction are added afterwards.  The resulting
512
-        coordinate is then used for G-code generation.  This allows to adapt
513
-        the coordinate system between the drawing and the CNC mill.  The offset
514
-        can also be used to cut a workpiece drawn once at multiple positions.
511
+        The coordinates from the drawing are scaled using different factors
512
+        for X and Y directions, rotated around the origin and the offsets in
513
+        X and Y direction are added afterwards.  The resulting coordinate is
514
+        then used for G-code generation.  This allows to adapt the coordinate
515
+        system between the drawing and the CNC mill.  The offset can also be
516
+        used to cut a workpiece drawn once at multiple positions.
515 517
 
516 518
       </para>
517 519
 
... ...
@@ -547,10 +549,61 @@ write_ngc logo.ngc]]></programlisting>
547 549
 
548 550
       <para>
549 551
 
550
-        The coordinates from the drawing are rotated around the origin and
551
-        the offsets in X and Y direction are added afterwards.  The resulting
552
-        coordinate is then used for G-code generation.  This allows to adapt
553
-        the coordinate system between the drawing and the CNC mill.
552
+        The coordinates from the drawing are scaled using different factors
553
+        for X and Y directions, rotated around the origin and the offsets in
554
+        X and Y direction are added afterwards.  The resulting coordinate is
555
+        then used for G-code generation.  This allows to adapt the coordinate
556
+        system between the drawing and the CNC mill.
557
+
558
+      </para>
559
+
560
+    </sect2>
561
+
562
+    <sect2>
563
+
564
+      <title>set_scale_x</title>
565
+
566
+      <para>
567
+
568
+        The command <code>set_scale_x &lt;x&gt;</code> sets a scaling factor
569
+        for the X direction.  All floating point values are valid, the default
570
+        is 1, i.e. no scaling.
571
+
572
+      </para>
573
+
574
+      <para>
575
+
576
+        The coordinates from the drawing are scaled using different factors
577
+        for X and Y directions, rotated around the origin and the offsets in
578
+        X and Y direction are added afterwards.  The resulting coordinate is
579
+        then used for G-code generation.  This allows to adapt the coordinate
580
+        system between the drawing and the CNC mill.  The scaling can also be
581
+        used to mirror a drawing by setting it to -1.
582
+
583
+      </para>
584
+
585
+    </sect2>
586
+
587
+    <sect2>
588
+
589
+      <title>set_scale_y</title>
590
+
591
+      <para>
592
+
593
+        The command <code>set_scale_y &lt;x&gt;</code> sets a scaling factor
594
+        for  the Y direction.  All floating point values are valid, the default
595
+        is 1, i.e. no scaling.
596
+
597
+      </para>
598
+
599
+      <para>
600
+
601
+        The coordinates from the drawing are scaled using different factors
602
+        for X and Y directions, rotated around the origin and the offsets in
603
+        X and Y direction are added afterwards.  The resulting coordinate is
604
+        then used for G-code generation.  This allows to adapt the coordinate
605
+        system between the drawing and the CNC mill.  The scaling can also be
606
+        used to mirror a drawing by setting it to -1.
554 607
 
555 608
       </para>
556 609
 
... ...
@@ -523,6 +523,46 @@ bool CmdParser::procCmd_set_rotation_z(std::istream &strm)
523 523
   return true;
524 524
 }
525 525
 
526
+/**
527
+ * @brief process set_scale_x command
528
+ * @param[in] strm stream to read command arguments from
529
+ * @return if processing command was successful
530
+ */
531
+bool CmdParser::procCmd_set_scale_x(std::istream &strm)
532
+{
533
+  // get arguments
534
+  double x;
535
+  strm >> x;
536
+  if (strm.fail()) {
537
+    std::cerr << "missing x scale factor" << std::endl;
538
+    return false;
539
+  }
540
+
541
+  // update settings
542
+  mSettings.scale_x = x;
543
+  return true;
544
+}
545
+
546
+/**
547
+ * @brief process set_scale_y command
548
+ * @param[in] strm stream to read command arguments from
549
+ * @return if processing command was successful
550
+ */
551
+bool CmdParser::procCmd_set_scale_y(std::istream &strm)
552
+{
553
+  // get arguments
554
+  double y;
555
+  strm >> y;
556
+  if (strm.fail()) {
557
+    std::cerr << "missing y scale factor" << std::endl;
558
+    return false;
559
+  }
560
+
561
+  // update settings
562
+  mSettings.scale_y = y;
563
+  return true;
564
+}
565
+
526 566
 /**
527 567
  * @brief process set_tool_diameter command
528 568
  * @param[in] strm stream to read command arguments from
... ...
@@ -633,6 +673,10 @@ bool CmdParser::procLine(const std::string &strLine)
633 673
     return procCmd_set_precision(strm);
634 674
   else if (cmd == "set_rotation_z")
635 675
     return procCmd_set_rotation_z(strm);
676
+  else if (cmd == "set_scale_x")
677
+    return procCmd_set_scale_x(strm);
678
+  else if (cmd == "set_scale_y")
679
+    return procCmd_set_scale_y(strm);
636 680
   else if (cmd == "set_tool_diameter")
637 681
     return procCmd_set_tool_diameter(strm);
638 682
   else if (cmd == "write_ngc")
... ...
@@ -172,6 +172,20 @@ public:
172 172
    */
173 173
   bool procCmd_set_rotation_z(std::istream &strm);
174 174
 
175
+  /**
176
+   * @brief process set_scale_x command
177
+   * @param[in] strm stream to read command arguments from
178
+   * @return if processing command was successful
179
+   */
180
+  bool procCmd_set_scale_x(std::istream &strm);
181
+
182
+  /**
183
+   * @brief process set_scale_y command
184
+   * @param[in] strm stream to read command arguments from
185
+   * @return if processing command was successful
186
+   */
187
+  bool procCmd_set_scale_y(std::istream &strm);
188
+
175 189
   /**
176 190
    * @brief process set_tool_diameter command
177 191
    * @param[in] strm stream to read command arguments from
... ...
@@ -94,6 +94,7 @@ void Path::removeEqPoints(double eqDist)
94 94
  */
95 95
 void Path::toGCode(const Settings &settings, double z, GCode &gcode) const
96 96
 {
97
+  double sx = settings.scale_x, sy = settings.scale_y;
97 98
   double rot = settings.rotation_z * M_PI / 180.0;
98 99
   Point offset(settings.offset_x, settings.offset_y);
99 100
 
... ...
@@ -105,7 +106,7 @@ void Path::toGCode(const Settings &settings, double z, GCode &gcode) const
105 106
   gcode.appendUp(settings.move_z);
106 107
 
107 108
   // move to start
108
-  gcode.appendFast(mPoints.front().rotate(rot) + offset);
109
+  gcode.appendFast(mPoints.front().scale_xy(sx, sy).rotate(rot) + offset);
109 110
 
110 111
   // dwell
111 112
   if (settings.dwell_time > 0.0)
... ...
@@ -119,7 +120,7 @@ void Path::toGCode(const Settings &settings, double z, GCode &gcode) const
119 120
   gcode.appendFeed(settings.feed_mill);
120 121
   Points::const_iterator pt = mPoints.begin();
121 122
   for (++pt; pt != mPoints.end(); ++pt)
122
-    gcode.appendLinear(pt->rotate(rot) + offset);
123
+    gcode.appendLinear(pt->scale_xy(sx, sy).rotate(rot) + offset);
123 124
 
124 125
   // move up
125 126
   gcode.appendUp(settings.move_z);
... ...
@@ -106,3 +106,14 @@ Point Point::rotate(double rad) const
106 106
   return Point(rx, ry);
107 107
 }
108 108
 
109
+/**
110
+ * @brief scale differently in X and Y direction
111
+ * @param[in] fx scale factor in X direction
112
+ * @param[in] fy scale factor in Y direction
113
+ * @return scaled point
114
+ */
115
+Point Point::scale_xy(double fx, double fy) const
116
+{
117
+  return Point(fx * mX, fy * mY);
118
+}
119
+
... ...
@@ -37,6 +37,14 @@ public:
37 37
    */
38 38
   Point rotate(double rad) const;
39 39
 
40
+  /**
41
+   * @brief scale differently in X and Y direction
42
+   * @param[in] fx scale factor in X direction
43
+   * @param[in] fy scale factor in Y direction
44
+   * @return scaled point
45
+   */
46
+  Point scale_xy(double fx, double fy) const;
47
+
40 48
   double mX; ///< x coordinate
41 49
   double mY; ///< y coordinate
42 50
 };
... ...
@@ -19,6 +19,8 @@ Settings::Settings():
19 19
   offset_y(0.0),
20 20
   precision(0.001),
21 21
   rotation_z(0.0),
22
+  scale_x(1.0),
23
+  scale_y(1.0),
22 24
   tool_diameter(1.0)
23 25
 {
24 26
 }
... ...
@@ -30,6 +30,8 @@ public:
30 30
   double    offset_y;      ///< offset to add to Y coordinate
31 31
   double    precision;     ///< precision, >= 1.0e-8 , <= 1.0
32 32
   double    rotation_z;    ///< rotation around Z axis in degrees
33
+  double    scale_x;       ///< scale to apply to X coordinate
34
+  double    scale_y;       ///< scale to apply to Y coordinate
33 35
   double    tool_diameter; ///< diameter of cutting tool, >= 0.0
34 36
 };
35 37
 
36 38