]> Shamusworld >> Repos - architektonas/blob - src/dimension.cpp
3060ff1b930c8682409f10993b73622562fbb098
[architektonas] / src / dimension.cpp
1 // dimension.cpp: Dimension object
2 //
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
6 //
7 // JLH = James Hammons <jlhamm@acm.org>
8 //
9 // WHO  WHEN        WHAT
10 // ---  ----------  ------------------------------------------------------------
11 // JLH  04/04/2011  Created this file, basic rendering
12 //
13
14 #include "dimension.h"
15
16 #include <QtGui>
17 #include "mathconstants.h"
18 #include "painter.h"
19
20
21 Dimension::Dimension(Vector p1, Vector p2, DimensionType dt/*= DTLinear*/ ,Object * p/*= NULL*/):
22         Object(p1, p), endpoint(p2),
23         dragging(false), draggingHandle1(false), draggingHandle2(false),
24         length(p2.Magnitude()), type(dt), point1(NULL), point2(NULL)
25 {
26 }
27
28 // This is bad, p1 & p2 could be NULL, causing much consternation...
29 Dimension::Dimension(Vector * p1, Vector * p2, DimensionType dt/*= DTLinear*/ , Object * p/*= NULL*/):
30         Object(*p1, p), endpoint(*p2),
31         dragging(false), draggingHandle1(false), draggingHandle2(false),
32         length(p2->Magnitude()), type(dt), point1(p1), point2(p2)
33 {
34 }
35
36 Dimension::~Dimension()
37 {
38 }
39
40 /*virtual*/ void Dimension::Draw(Painter * painter)
41 {
42         // If there are valid Vector pointers in here, use them to update the internal
43         // positions. Otherwise, we just use the internal positions by default.
44         if (point1)
45                 position = *point1;
46
47         if (point2)
48                 endpoint = *point2;
49
50         if (state == OSSelected)
51                 painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
52         else
53                 painter->SetPen(QPen(Qt::blue, 1.0, Qt::SolidLine));
54
55         // Draw an aligned dimension line
56         double angle = Vector(endpoint - position).Angle();
57         double orthoAngle = angle + (PI / 2.0);
58         Vector orthogonal = Vector(cos(orthoAngle), sin(orthoAngle));
59         Vector unit = Vector(endpoint - position).Unit();
60
61 //NOTE: SCREEN_ZOOM is our kludge factor... We need to figure out a better
62 //      way of doing this...
63         // Get our line parallel to our points
64         Point p1 = position + (orthogonal * 10.0 * SCREEN_ZOOM);
65         Point p2 = endpoint + (orthogonal * 10.0 * SCREEN_ZOOM);
66
67         // Draw main dimension line
68         painter->DrawLine(p1, p2);
69
70         Point p3 = position + (orthogonal * 16.0 * SCREEN_ZOOM);
71         Point p4 = endpoint + (orthogonal * 16.0 * SCREEN_ZOOM);
72         Point p5 = position + (orthogonal * 4.0 * SCREEN_ZOOM);
73         Point p6 = endpoint + (orthogonal * 4.0 * SCREEN_ZOOM);
74
75         // Draw extension lines
76         painter->DrawLine(p3, p5);
77         painter->DrawLine(p4, p6);
78
79         painter->SetBrush(QBrush(QColor(Qt::blue)));
80         painter->DrawArrowhead(p1, p2);
81         painter->DrawArrowhead(p2, p1);
82
83         // Draw length of dimension line...
84         painter->SetFont(QFont("Arial", 10.0 * Painter::zoom * SCREEN_ZOOM));
85         Vector v1((p1.x - p2.x) / 2.0, (p1.y - p2.y) / 2.0);
86         Point ctr = p2 + v1;
87         // This is in pixels, which isn't even remotely correct... !!! FIX !!!
88         QString dimText = QString("%1\"").arg(Vector(endpoint - position).Magnitude());
89 //      int textWidth = QFontMetrics(painter->font()).width(dimText);
90 //      int textHeight = QFontMetrics(painter->font()).height();
91 #if 0
92 //We have to do transformation voodoo to make the text come out readable and in correct orientation...
93 //Some things to note here: if angle > 90 degrees, then we need to take the negative of the angle
94 //for our text.
95 painter->save();
96 painter->translate(ctr.x, ctr.y);
97 int yOffset = -8;
98 //16 : printf("textHeight: %d\n", textHeight);
99
100 //Fix text so it isn't upside down...
101 if ((angle > PI * 0.5) && (angle < PI * 1.5))
102 {
103         angle += PI;
104         yOffset = 18;
105 }
106
107 painter->rotate(angle * RADIANS_TO_DEGREES);
108 painter->scale(1.0, -1.0);
109 //painter->translate(-textWidth / 2, -24);
110 //      painter->drawText(0, 0, textWidth, 20, Qt::AlignCenter, dimText);
111         // This version draws the y-coord from the baseline of the font
112         painter->DrawText(-textWidth / 2, yOffset, dimText);
113 //painter->setPen(QPen(QColor(0xFF, 0x20, 0x20), 1.0, Qt::SolidLine));
114 //painter->drawLine(20, 0, -20, 0);
115 //painter->drawLine(0, 20, 0, -20);
116 painter->restore();
117 #else
118 //      painter->DrawText(QRectF(QPointF(ctr.x, ctr.y), QPointF(ctr.x + textWidth, ctr.y + textHeight)), Qt::AlignVCenter, dimText);
119 // Now that we've taken our own good advice, maybe we should have the painter class
120 // do a nice abstracted text draw routine? :-)
121         painter->DrawAngledText(ctr, angle, dimText);
122 #endif
123
124 /*
125 All of the preceeding makes me think that rather than try to compensate for Qt's unbelieveably
126 AWFUL decision to go with a wrong-handed graphics subsystem, it may be better to just stuff
127 all of that crap into some kind of subclass that handles all the nastiness behind the scenes.
128 I mean, really, all this crap just to get some proplerly rendered text on the screen? How
129 retarded is that? :-/
130 */
131 }
132
133 /*virtual*/ Vector Dimension::Center(void)
134 {
135         // Technically, this is the midpoint but who are we to quibble? :-)
136         Vector v((position.x - endpoint.x) / 2.0, (position.y - endpoint.y) / 2.0);
137         return endpoint + v;
138 }
139
140 /*virtual*/ bool Dimension::Collided(Vector /*point*/)
141 {
142 #if 0
143         objectWasDragged = false;
144         Vector lineSegment = endpoint - position;
145         Vector v1 = point - position;
146         Vector v2 = point - endpoint;
147         double parameterizedPoint = lineSegment.Dot(v1) / lineSegment.Magnitude(), distance;
148
149         // Geometric interpretation:
150         // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
151         // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
152         // then the perpendicular lies beyond the 2nd endpoint.
153
154         if (parameterizedPoint < 0.0)
155                 distance = v1.Magnitude();
156         else if (parameterizedPoint > lineSegment.Magnitude())
157                 distance = v2.Magnitude();
158         else                                    // distance = ?Det?(ls, v1) / |ls|
159                 distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y) / lineSegment.Magnitude());
160
161         // If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
162         // intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
163         // {e-s}.{e-p} are both non-negative.  Perpendicular distance from the point to the segment is
164         // computed by first computing the area of the triangle the three points form, then dividing by the
165         // length of the segment.  Distances are done just by the Pythagorean theorem.  Twice the area of the
166         // triangle formed by three points is the determinant of the following matrix:
167         //
168         // sx sy 1
169         // ex ey 1
170         // px py 1
171         //
172         // By translating the start point to the origin, this can be rewritten as:
173         // By subtracting row 1 from all rows, you get the following:
174         // [because sx = sy = 0. you could leave out the -sx/y terms below. because we subtracted
175         // row 1 from all rows (including row 1) row 1 turns out to be zero. duh!]
176         //
177         // 0         0         0        0  0  0
178         // (ex - sx) (ey - sy) 0   ==>  ex ey 0
179         // (px - sx) (py - sy) 0        px py 0
180         //
181         // which greatly simplifies the calculation of the determinant.
182
183         if (state == OSInactive)
184         {
185 //printf("Line: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance);
186 //printf("      v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude());
187 //printf("      point = %lf,%lf,%lf; p1 = %lf,%lf,%lf; p2 = %lf,%lf,%lf\n", point.x, point.y, point.z, position.x, position.y, position.z, endpoint.x, endpoint.y, endpoint.z);
188 //printf("      \n", );
189 //How to translate this into pixels from Document space???
190 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
191 //the caller knows about the zoom factor and all that good kinda crap
192                 if (v1.Magnitude() < 10.0)
193                 {
194                         oldState = state;
195                         state = OSSelected;
196                         oldPoint = position; //maybe "position"?
197                         draggingHandle1 = true;
198                         return true;
199                 }
200                 else if (v2.Magnitude() < 10.0)
201                 {
202                         oldState = state;
203                         state = OSSelected;
204                         oldPoint = endpoint; //maybe "position"?
205                         draggingHandle2 = true;
206                         return true;
207                 }
208                 else if (distance < 2.0)
209                 {
210                         oldState = state;
211                         state = OSSelected;
212                         oldPoint = point;
213                         dragging = true;
214                         return true;
215                 }
216         }
217         else if (state == OSSelected)
218         {
219                 // Here we test for collision with handles as well! (SOON!)
220 /*
221 Like so:
222                 if (v1.Magnitude() < 2.0) // Handle #1
223                 else if (v2.Magnitude() < 2.0) // Handle #2
224 */
225                 if (distance < 2.0)
226                 {
227                         oldState = state;
228 //                      state = OSInactive;
229                         oldPoint = point;
230                         dragging = true;
231                         return true;
232                 }
233         }
234 #endif
235
236         state = OSInactive;
237         return false;
238 }
239
240 /*virtual*/ void Dimension::PointerMoved(Vector point)
241 {
242         // We know this is true because mouse move messages don't come here unless
243         // the object was actually clicked on--therefore we *know* we're being
244         // dragged...
245         objectWasDragged = true;
246
247         if (dragging)
248         {
249                 // Here we need to check whether or not we're dragging a handle or the object itself...
250                 Vector delta = point - oldPoint;
251
252                 position += delta;
253                 endpoint += delta;
254
255                 oldPoint = point;
256                 needUpdate = true;
257         }
258         else if (draggingHandle1)
259         {
260                 Vector delta = point - oldPoint;
261
262                 position += delta;
263
264                 oldPoint = point;
265                 needUpdate = true;
266         }
267         else if (draggingHandle2)
268         {
269                 Vector delta = point - oldPoint;
270
271                 endpoint += delta;
272
273                 oldPoint = point;
274                 needUpdate = true;
275         }
276         else
277                 needUpdate = false;
278 }
279
280 /*virtual*/ void Dimension::PointerReleased(void)
281 {
282         if (draggingHandle1 || draggingHandle2)
283         {
284                 // Set the length (in case the global state was set to fixed (or not))
285                 if (Object::fixedLength)
286                 {
287
288                         if (draggingHandle1)    // startpoint
289                         {
290                                 Vector v = Vector(position - endpoint).Unit() * length;
291                                 position = endpoint + v;
292                         }
293                         else                                    // endpoint
294                         {
295 //                              Vector v1 = endpoint - position;
296                                 Vector v = Vector(endpoint - position).Unit() * length;
297                                 endpoint = position + v;
298                         }
299                 }
300                 else
301                 {
302                         // Otherwise, we calculate the new length, just in case on the next move
303                         // it turns out to have a fixed length. :-)
304                         length = Vector(endpoint - position).Magnitude();
305                 }
306         }
307
308         dragging = false;
309         draggingHandle1 = false;
310         draggingHandle2 = false;
311
312         // Here we check for just a click: If object was clicked and dragged, then
313         // revert to the old state (OSInactive). Otherwise, keep the new state that
314         // we set.
315 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
316 about keeping track of old states...
317 */
318         if (objectWasDragged)
319                 state = oldState;
320 }
321
322 void Dimension::SetPoint1(Vector * v)
323 {
324         point1 = v;
325         needUpdate = true;
326 }
327
328 void Dimension::SetPoint2(Vector * v)
329 {
330         point2 = v;
331         needUpdate = true;
332 }
333
334 Vector Dimension::GetPoint1(void)
335 {
336         return position;
337 }
338
339 Vector Dimension::GetPoint2(void)
340 {
341         return endpoint;
342 }
343
344 void Dimension::FlipSides(void)
345 {
346 #if 0
347         Vector tmp = position;
348         position = endpoint;
349         endpoint = tmp;
350 #else
351         Vector * tmp = point1;
352         point1 = point2;
353         point2 = tmp;
354 #endif
355         needUpdate = true;
356 }
357