]> Shamusworld >> Repos - architektonas/blob - src/base/rs_solid.cpp
0b447a7319d898ee17b8374590f96980f9ad9e2e
[architektonas] / src / base / rs_solid.cpp
1 /****************************************************************************
2 ** $Id: rs_solid.cpp 1938 2004-12-09 23:09:53Z 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 #include "rs_solid.h"
28
29 #include "rs_graphicview.h"
30 //#include "rs_painter.h"
31 #include "paintintf.h"
32
33 /**
34  * Default constructor.
35  */
36 RS_Solid::RS_Solid(RS_EntityContainer* parent,
37                    const RS_SolidData& d)
38         :RS_AtomicEntity(parent), data(d) {
39     calculateBorders();
40 }
41
42
43
44 /**
45  * @return Corner number 'num'.
46  */
47 Vector RS_Solid::getCorner(int num) {
48     if (num>=0 && num<4) {
49         return data.corner[num];
50     } else {
51         RS_DEBUG->print("Illegal corner requested from Solid",
52                         RS_Debug::D_WARNING);
53         return Vector(false);
54     }
55 }
56
57
58
59 /**
60  * Shapes this Solid into a standard arrow (used in dimensions).
61  *
62  * @param point The point the arrow points to.
63  * @param angle Direction of the arrow.
64  * @param arrowSize Size of arrow (length).
65  */
66 void RS_Solid::shapeArrow(const Vector& point,
67                           double angle,
68                           double arrowSize) {
69
70     double cosv1, sinv1, cosv2, sinv2;
71     double arrowSide = arrowSize/cos(0.165);
72
73     cosv1 = cos(angle+0.165)*arrowSide;
74     sinv1 = sin(angle+0.165)*arrowSide;
75     cosv2 = cos(angle-0.165)*arrowSide;
76     sinv2 = sin(angle-0.165)*arrowSide;
77
78     data.corner[0] = point;
79     data.corner[1] = Vector(point.x - cosv1, point.y - sinv1);
80     data.corner[2] = Vector(point.x - cosv2, point.y - sinv2);
81     data.corner[3] = Vector(false);
82
83     calculateBorders();
84 }
85
86
87
88 void RS_Solid::calculateBorders() {
89     resetBorders();
90
91     for (int i=0; i<4; ++i) {
92         if (data.corner[i].valid) {
93             minV = Vector::minimum(minV, data.corner[i]);
94             maxV = Vector::maximum(maxV, data.corner[i]);
95         }
96     }
97 }
98
99
100
101 Vector RS_Solid::getNearestEndpoint(const Vector& coord, double* dist) {
102
103     double minDist = RS_MAXDOUBLE;
104     double curDist;
105     Vector ret;
106
107     for (int i=0; i<4; ++i) {
108         if (data.corner[i].valid) {
109             curDist = data.corner[i].distanceTo(coord);
110             if (curDist<minDist) {
111                 ret = data.corner[i];
112                 minDist = curDist;
113             }
114         }
115     }
116
117     if (dist!=NULL) {
118         *dist = minDist;
119     }
120
121     return ret;
122 }
123
124
125
126 /**
127  * @todo Implement this.
128  */
129 Vector RS_Solid::getNearestPointOnEntity(const Vector& /*coord*/,
130         bool /*onEntity*/, double* /*dist*/, RS_Entity** /*entity*/) {
131
132     Vector ret(false);
133     return ret;
134 }
135
136
137
138 Vector RS_Solid::getNearestCenter(const Vector& /*coord*/,
139                                      double* dist) {
140
141     if (dist!=NULL) {
142         *dist = RS_MAXDOUBLE;
143     }
144
145     return Vector(false);
146 }
147
148
149
150 Vector RS_Solid::getNearestMiddle(const Vector& /*coord*/,
151                                      double* dist) {
152     if (dist!=NULL) {
153         *dist = RS_MAXDOUBLE;
154     }
155     return Vector(false);
156 }
157
158
159
160 Vector RS_Solid::getNearestDist(double /*distance*/,
161                                    const Vector& /*coord*/,
162                                    double* dist) {
163     if (dist!=NULL) {
164         *dist = RS_MAXDOUBLE;
165     }
166     return Vector(false);
167 }
168
169
170
171 /**
172  * @return Distance from one of the boundry lines of this solid to given point.
173  *
174  * @todo implement
175  */
176 double RS_Solid::getDistanceToPoint(const Vector& /*coord*/,
177                                     RS_Entity** /*entity*/,
178                                     RS2::ResolveLevel /*level*/,
179                                                                     double /*solidDist*/) {
180     return RS_MAXDOUBLE;
181 }
182
183
184
185 void RS_Solid::move(Vector offset) {
186     for (int i=0; i<4; ++i) {
187         data.corner[i].move(offset);
188     }
189     calculateBorders();
190 }
191
192
193
194 void RS_Solid::rotate(Vector center, double angle) {
195     for (int i=0; i<4; ++i) {
196         data.corner[i].rotate(center, angle);
197     }
198     calculateBorders();
199 }
200
201
202
203 void RS_Solid::scale(Vector center, Vector factor) {
204     for (int i=0; i<4; ++i) {
205         data.corner[i].scale(center, factor);
206     }
207     calculateBorders();
208 }
209
210
211
212 void RS_Solid::mirror(Vector axisPoint1, Vector axisPoint2) {
213     for (int i=0; i<4; ++i) {
214         data.corner[i].mirror(axisPoint1, axisPoint2);
215     }
216     calculateBorders();
217 }
218
219 //void RS_Solid::draw(RS_Painter* painter, RS_GraphicView* view, double /*patternOffset*/)
220 void RS_Solid::draw(PaintInterface * painter, RS_GraphicView * view, double /*patternOffset*/)
221 {
222         if (painter == NULL || view == NULL)
223                 return;
224
225         RS_SolidData d = getData();
226
227         if (isTriangle())
228                 painter->fillTriangle(view->toGui(getCorner(0)), view->toGui(getCorner(1)),
229                         view->toGui(getCorner(2)));
230 }
231
232 /**
233  * Dumps the point's data to stdout.
234  */
235 std::ostream & operator<<(std::ostream & os, const RS_Solid & p)
236 {
237         os << " Solid: " << p.getData() << "\n";
238         return os;
239 }