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