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