]> Shamusworld >> Repos - architektonas/blob - src/base/rs_ellipse.h
2c3e83e1e07fefed4e943d323c1c88da0f0d5c15
[architektonas] / src / base / rs_ellipse.h
1 /****************************************************************************
2 ** $Id: rs_ellipse.h 2367 2005-04-04 16:57:36Z andrew $
3 **
4 ** Copyright (C) 2001-2003 RibbonSoft. All rights reserved.
5 **
6 ** This file is part of the qcadlib Library project.
7 **
8 ** This file may be distributed and/or modified under the terms of the
9 ** GNU General Public License version 2 as published by the Free Software
10 ** Foundation and appearing in the file LICENSE.GPL included in the
11 ** packaging of this file.
12 **
13 ** Licensees holding valid qcadlib Professional Edition licenses may use
14 ** this file in accordance with the qcadlib Commercial License
15 ** Agreement provided with the Software.
16 **
17 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
18 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 **
20 ** See http://www.ribbonsoft.com for further details.
21 **
22 ** Contact info@ribbonsoft.com if any conditions of this licensing are
23 ** not clear to you.
24 **
25 **********************************************************************/
26
27
28 #ifndef RS_ELLIPSE_H
29 #define RS_ELLIPSE_H
30
31 #include "rs_atomicentity.h"
32
33 /**
34  * Holds the data that defines an ellipse.
35  */
36 class RS_EllipseData {
37 public:
38     RS_EllipseData(const Vector& center,
39                    const Vector& majorP,
40                    double ratio,
41                    double angle1, double angle2,
42                    bool reversed) {
43
44         this->center = center;
45         this->majorP = majorP;
46         this->ratio = ratio;
47         this->angle1 = angle1;
48         this->angle2 = angle2;
49                 this->reversed = reversed;
50     }
51
52     friend class RS_Ellipse;
53
54     friend std::ostream& operator << (std::ostream& os, const RS_EllipseData& ed) {
55         os << "(" << ed.center <<
56         "/" << ed.majorP <<
57         " " << ed.ratio <<
58         " " << ed.angle1 <<
59         "," << ed.angle2 <<
60         ")";
61         return os;
62     }
63
64 private:
65     //! Ellipse center
66     Vector center;
67     //! Endpoint of major axis relative to center.
68     Vector majorP;
69     //! Ratio of minor axis to major axis.
70     double ratio;
71     //! Start angle
72     double angle1;
73     //! End angle
74     double angle2;
75         //! Reversed (cw) flag
76         bool reversed;
77 };
78
79
80
81
82 /**
83  * Class for an ellipse entity. All angles are in Rad.
84  *
85  * @author Andrew Mustun
86  */
87 class RS_Ellipse : public RS_AtomicEntity {
88 public:
89     RS_Ellipse(RS_EntityContainer* parent,
90                const RS_EllipseData& d);
91     virtual ~RS_Ellipse() {}
92
93     virtual RS_Entity* clone() {
94         RS_Ellipse* e = new RS_Ellipse(*this);
95         e->initId();
96         return e;
97     }
98
99     /** @return RS2::EntityEllipse */
100     virtual RS2::EntityType rtti() const {
101         return RS2::EntityEllipse;
102     }
103
104
105     /**
106          * @return Start point of the entity.
107          */
108     virtual Vector getStartpoint() const {
109                 Vector p;
110         p.set(data.center.x + cos(data.angle1) * getMajorRadius(),
111               data.center.y + sin(data.angle1) * getMinorRadius());
112         p.rotate(data.center, getAngle());
113         return p;
114     }
115     /**
116          * @return End point of the entity.
117          */
118     virtual Vector getEndpoint() const {
119                 Vector p;
120         p.set(data.center.x + cos(data.angle2) * getMajorRadius(),
121               data.center.y + sin(data.angle2) * getMinorRadius());
122         p.rotate(data.center, getAngle());
123         return p;
124     }
125
126         virtual void moveStartpoint(const Vector& pos);
127         virtual void moveEndpoint(const Vector& pos);
128
129         virtual RS2::Ending getTrimPoint(const Vector& coord,
130                   const Vector& trimPoint);
131
132         double getEllipseAngle(const Vector& pos);
133
134     /** @return Copy of data that defines the ellipse. **/
135     RS_EllipseData getData() {
136         return data;
137     }
138
139         virtual VectorSolutions getRefPoints();
140
141     /**
142      * @retval true if the arc is reversed (clockwise),
143      * @retval false otherwise
144      */
145     bool isReversed() const {
146         return data.reversed;
147     }
148         /** sets the reversed status. */
149         void setReversed(bool r) {
150                 data.reversed = r;
151         }
152
153     /** @return The rotation angle of this ellipse */
154     double getAngle() const {
155         return data.majorP.angle();
156     }
157
158     /** @return The start angle of this arc */
159     double getAngle1() {
160         return data.angle1;
161     }
162     /** Sets new start angle. */
163         void setAngle1(double a1) {
164                 data.angle1 = a1;
165         }
166     /** @return The end angle of this arc */
167     double getAngle2() {
168         return data.angle2;
169     }
170     /** Sets new end angle. */
171         void setAngle2(double a2) {
172                 data.angle2 = a2;
173         }
174
175
176     /** @return The center point (x) of this arc */
177     Vector getCenter() {
178         return data.center;
179     }
180     /** Sets new center. */
181         void setCenter(const Vector& c) {
182                 data.center = c;
183         }
184
185     /** @return The endpoint of the major axis (relative to center). */
186     Vector getMajorP() {
187         return data.majorP;
188     }
189     /** Sets new major point (relative to center). */
190         void setMajorP(const Vector& p) {
191                 data.majorP = p;
192         }
193
194     /** @return The ratio of minor to major axis */
195     double getRatio() {
196         return data.ratio;
197     }
198     /** Sets new ratio. */
199         void setRatio(double r) {
200                 data.ratio = r;
201         }
202
203
204     /**
205      * @return Angle length in rad.
206      */
207     virtual double getAngleLength() const {
208         if (isReversed()) {
209             return data.angle1-data.angle2;
210         } else {
211             return data.angle2-data.angle1;
212         }
213     }
214
215     /** @return The major radius of this ellipse. Same as getRadius() */
216     double getMajorRadius() const {
217         return data.majorP.magnitude();
218     }
219
220     /** @return The minor radius of this ellipse */
221     double getMinorRadius() const {
222         return data.majorP.magnitude()*data.ratio;
223     }
224
225     virtual Vector getNearestEndpoint(const Vector& coord,
226                                          double* dist = NULL);
227     virtual Vector getNearestPointOnEntity(const Vector& coord,
228             bool onEntity = true, double* dist = NULL, RS_Entity** entity=NULL);
229     virtual Vector getNearestCenter(const Vector& coord,
230                                        double* dist = NULL);
231     virtual Vector getNearestMiddle(const Vector& coord,
232                                        double* dist = NULL);
233     virtual Vector getNearestDist(double distance,
234                                      const Vector& coord,
235                                      double* dist = NULL);
236     virtual double getDistanceToPoint(const Vector& coord,
237                                       RS_Entity** entity=NULL,
238                                       RS2::ResolveLevel level=RS2::ResolveNone,
239                                                                           double solidDist = RS_MAXDOUBLE);
240     virtual bool isPointOnEntity(const Vector& coord,
241                                  double tolerance=RS_TOLERANCE);
242
243     virtual void move(Vector offset);
244     virtual void rotate(Vector center, double angle);
245     virtual void scale(Vector center, Vector factor);
246     virtual void mirror(Vector axisPoint1, Vector axisPoint2);
247         virtual void moveRef(const Vector& ref, const Vector& offset);
248
249 //    virtual void draw(RS_Painter* painter, RS_GraphicView* view, double patternOffset=0.0);
250     virtual void draw(PaintInterface * painter, RS_GraphicView * view, double patternOffset = 0.0);
251
252     friend std::ostream & operator<<(std::ostream & os, const RS_Ellipse & a);
253
254     //virtual void calculateEndpoints();
255     virtual void calculateBorders();
256
257 protected:
258     RS_EllipseData data;
259 };
260
261 #endif