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