You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

230 lines
8.0 KiB

14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
14 years ago
// Dick Hollenbeck's KiROUND R&D // This provides better project control over rounding to int from double // than wxRound() did. This scheme provides better logging in Debug builds // and it provides for compile time calculation of constants. #include <stdio.h> #include <assert.h> #include <limits.h> //-----<KiROUND KIT>------------------------------------------------------------ /** * KiROUND * rounds a floating point number to an int using * "round halfway cases away from zero". * In Debug build an assert fires if will not fit into an int. */ #if defined( DEBUG ) // DEBUG: a macro to capture line and file, then calls this inline static inline int KiRound( double v, int line, const char* filename ) { v = v < 0 ? v - 0.5 : v + 0.5; if( v > INT_MAX + 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' > 0 ' for int\n", __FUNCTION__, filename, line, v ); } else if( v < INT_MIN - 0.5 ) { printf( "%s: in file %s on line %d, val: %.16g too ' < 0 ' for int\n", __FUNCTION__, filename, line, v ); } return int( v ); } #define KiROUND( v ) KiRound( v, __LINE__, __FILE__ ) #else // RELEASE: a macro so compile can pre-compute constants. #define KiROUND( v ) int( (v) < 0 ? (v) - 0.5 : (v) + 0.5 ) #endif //-----</KiROUND KIT>----------------------------------------------------------- // Only a macro is compile time calculated, an inline function causes a static constructor // in a situation like this. // Therefore the Release build is best done with a MACRO not an inline function. int Computed = KiROUND( 14.3 * 8 ); int main( int argc, char** argv ) { for( double d = double(INT_MAX)-1; d < double(INT_MAX)+8; d += 2.0 ) { int i = KiROUND( d ); printf( "t: %d %.16g\n", i, d ); } return 0; }
14 years ago
14 years ago
  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
  5. * Copyright (C) 1992-2011 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. /**
  25. * @file class_drawsegment.h
  26. * @brief Class to handle a graphic segment.
  27. */
  28. #ifndef CLASS_DRAWSEGMENT_H_
  29. #define CLASS_DRAWSEGMENT_H_
  30. #include <class_board_item.h>
  31. #include <PolyLine.h>
  32. #include <math_for_graphics.h>
  33. #include <trigo.h>
  34. class LINE_READER;
  35. class EDA_DRAW_FRAME;
  36. class MODULE;
  37. class MSG_PANEL_ITEM;
  38. class DRAWSEGMENT : public BOARD_ITEM
  39. {
  40. protected:
  41. int m_Width; ///< thickness of lines ...
  42. wxPoint m_Start; ///< Line start point or Circle and Arc center
  43. wxPoint m_End; ///< Line end point or circle and arc start point
  44. STROKE_T m_Shape; ///< Shape: line, Circle, Arc
  45. int m_Type; ///< Used in complex associations ( Dimensions.. )
  46. double m_Angle; ///< Used only for Arcs: Arc angle in 1/10 deg
  47. wxPoint m_BezierC1; ///< Bezier Control Point 1
  48. wxPoint m_BezierC2; ///< Bezier Control Point 2
  49. std::vector<wxPoint> m_BezierPoints;
  50. std::vector<wxPoint> m_PolyPoints;
  51. public:
  52. DRAWSEGMENT( BOARD_ITEM* aParent = NULL, KICAD_T idtype = PCB_LINE_T );
  53. // Do not create a copy constructor. The one generated by the compiler is adequate.
  54. ~DRAWSEGMENT();
  55. /// skip the linked list stuff, and parent
  56. const DRAWSEGMENT& operator = ( const DRAWSEGMENT& rhs );
  57. DRAWSEGMENT* Next() const { return (DRAWSEGMENT*) Pnext; }
  58. DRAWSEGMENT* Back() const { return (DRAWSEGMENT*) Pback; }
  59. void SetWidth( int aWidth ) { m_Width = aWidth; }
  60. int GetWidth() const { return m_Width; }
  61. /**
  62. * Function SetAngle
  63. * sets the angle for arcs, and normalizes it within the range 0 - 360 degrees.
  64. * @param aAngle is tenths of degrees, but will soon be degrees.
  65. */
  66. void SetAngle( double aAngle ); // encapsulates the transition to degrees
  67. double GetAngle() const { return m_Angle; }
  68. void SetType( int aType ) { m_Type = aType; }
  69. int GetType() const { return m_Type; }
  70. void SetShape( STROKE_T aShape ) { m_Shape = aShape; }
  71. STROKE_T GetShape() const { return m_Shape; }
  72. void SetBezControl1( const wxPoint& aPoint ) { m_BezierC1 = aPoint; }
  73. const wxPoint& GetBezControl1() const { return m_BezierC1; }
  74. void SetBezControl2( const wxPoint& aPoint ) { m_BezierC2 = aPoint; }
  75. const wxPoint& GetBezControl2() const { return m_BezierC2; }
  76. void SetPosition( const wxPoint& aPos ) { m_Start = aPos; } // override
  77. const wxPoint& GetPosition() const { return m_Start; } // override
  78. /**
  79. * Function GetStart
  80. * returns the starting point of the graphic
  81. */
  82. const wxPoint& GetStart() const { return m_Start; }
  83. void SetStart( const wxPoint& aStart ) { m_Start = aStart; }
  84. void SetStartY( int y ) { m_Start.y = y; }
  85. void SetStartX( int x ) { m_Start.x = x; }
  86. /**
  87. * Function GetEnd
  88. * returns the ending point of the graphic
  89. */
  90. const wxPoint& GetEnd() const { return m_End; }
  91. void SetEnd( const wxPoint& aEnd ) { m_End = aEnd; }
  92. void SetEndY( int y ) { m_End.y = y; }
  93. void SetEndX( int x ) { m_End.x = x; }
  94. // Arc attributes are read only, since they are "calculated" from
  95. // m_Start, m_End, and m_Angle. No Set...() functions.
  96. const wxPoint& GetCenter() const { return m_Start; }
  97. const wxPoint& GetArcStart() const { return m_End; }
  98. const wxPoint GetArcEnd() const;
  99. /**
  100. * function GetArcAngleStart()
  101. * @return the angle of the stating point of this arc, between 0 and 3600 in 0.1 deg
  102. */
  103. const double GetArcAngleStart() const;
  104. /**
  105. * Function GetRadius
  106. * returns the radius of this item
  107. * Has meaning only for arc and circle
  108. */
  109. int GetRadius() const
  110. {
  111. double radius = GetLineLength( m_Start, m_End );
  112. return KiROUND( radius );
  113. }
  114. /**
  115. * Function GetParentModule
  116. * returns a pointer to the parent module, or NULL if DRAWSEGMENT does not
  117. * belong to a module.
  118. * @return MODULE* - pointer to the parent module or NULL.
  119. */
  120. MODULE* GetParentModule() const;
  121. const std::vector<wxPoint>& GetBezierPoints() const { return m_BezierPoints; };
  122. const std::vector<wxPoint>& GetPolyPoints() const { return m_PolyPoints; };
  123. void SetBezierPoints( const std::vector<wxPoint>& aPoints )
  124. {
  125. m_BezierPoints = aPoints;
  126. }
  127. void SetPolyPoints( const std::vector<wxPoint>& aPoints )
  128. {
  129. m_PolyPoints = aPoints;
  130. }
  131. void Copy( DRAWSEGMENT* source );
  132. void Draw( EDA_DRAW_PANEL* panel, wxDC* DC,
  133. GR_DRAWMODE aDrawMode, const wxPoint& aOffset = ZeroOffset );
  134. virtual void GetMsgPanelInfo( std::vector< MSG_PANEL_ITEM >& aList );
  135. virtual EDA_RECT GetBoundingBox() const;
  136. virtual bool HitTest( const wxPoint& aPosition );
  137. virtual bool HitTest( const EDA_RECT& aRect ) const;
  138. wxString GetClass() const
  139. {
  140. return wxT( "DRAWSEGMENT" );
  141. }
  142. /**
  143. * Function GetLength
  144. * returns the length of the track using the hypotenuse calculation.
  145. * @return double - the length of the track
  146. */
  147. double GetLength() const
  148. {
  149. return GetLineLength( GetStart(), GetEnd() );
  150. }
  151. virtual void Move( const wxPoint& aMoveVector )
  152. {
  153. m_Start += aMoveVector;
  154. m_End += aMoveVector;
  155. }
  156. virtual void Rotate( const wxPoint& aRotCentre, double aAngle );
  157. virtual void Flip( const wxPoint& aCentre );
  158. /**
  159. * Function TransformShapeWithClearanceToPolygon
  160. * Convert the track shape to a closed polygon
  161. * Used in filling zones calculations
  162. * Circles and arcs are approximated by segments
  163. * @param aCornerBuffer = a buffer to store the polygon
  164. * @param aClearanceValue = the clearance around the pad
  165. * @param aCircleToSegmentsCount = the number of segments to approximate a circle
  166. * @param aCorrectionFactor = the correction to apply to circles radius to keep
  167. * clearance when the circle is approximated by segment bigger or equal
  168. * to the real clearance value (usually near from 1.0)
  169. */
  170. void TransformShapeWithClearanceToPolygon( CPOLYGONS_LIST& aCornerBuffer,
  171. int aClearanceValue,
  172. int aCircleToSegmentsCount,
  173. double aCorrectionFactor ) const;
  174. virtual wxString GetSelectMenuText() const;
  175. virtual BITMAP_DEF GetMenuImage() const { return add_dashed_line_xpm; }
  176. virtual EDA_ITEM* Clone() const;
  177. #if defined(DEBUG)
  178. void Show( int nestLevel, std::ostream& os ) const { ShowDummy( os ); } // override
  179. #endif
  180. };
  181. #endif // CLASS_DRAWSEGMENT_H_