]> Shamusworld >> Repos - architektonas/blob - src/dimension.cpp
First step towards resizable grid and sane zoom setting.
[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 // JLH  03/14/2013  Updated to new connection system
13 //
14
15 #include "dimension.h"
16
17 #include <QtGui>
18 #include "mathconstants.h"
19 #include "painter.h"
20
21
22 Dimension::Dimension(Vector p1, Vector p2, DimensionType dt/*= DTLinear*/ ,Object * p/*= NULL*/):
23         Object(p1, p), endpoint(p2),
24         dragging(false), draggingHandle1(false), draggingHandle2(false),
25         length(p2.Magnitude()), dimensionType(dt), point1(NULL), point2(NULL),
26         size(0.25)
27 {
28         // We set the size to 1/4 base unit. Could be anything.
29         type = OTDimension;
30 }
31
32
33 // This is bad, p1 & p2 could be NULL, causing much consternation...
34 Dimension::Dimension(Connection p1, Connection p2, DimensionType dt/*= DTLinear*/ , Object * p/*= NULL*/):
35         dragging(false), draggingHandle1(false), draggingHandle2(false),
36         length(0), dimensionType(dt), point1(p1), point2(p2),
37         size(0.25)
38 {
39         type = OTDimension;
40 }
41
42
43 Dimension::~Dimension()
44 {
45 }
46
47
48 /*virtual*/ void Dimension::Draw(Painter * painter)
49 {
50         // If there are valid Vector pointers in here, use them to update the internal
51         // positions. Otherwise, we just use the internal positions by default.
52         if (point1.object)
53                 position = point1.object->GetPointAtParameter(point1.t);
54
55         if (point2.object)
56                 endpoint = point2.object->GetPointAtParameter(point2.t);
57
58         if (state == OSSelected)
59                 painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
60         else
61                 painter->SetPen(QPen(Qt::blue, 1.0, Qt::SolidLine));
62
63         // Draw an aligned dimension line
64         double angle = Vector(endpoint - position).Angle();
65         double orthoAngle = angle + (PI / 2.0);
66         Vector orthogonal = Vector(cos(orthoAngle), sin(orthoAngle));
67         Vector unit = Vector(endpoint - position).Unit();
68
69 #if 0
70 //NOTE: SCREEN_ZOOM is our kludge factor... We need to figure out a better
71 //      way of doing this...
72         // Get our line parallel to our points
73         Point p1 = position + (orthogonal * 10.0 * SCREEN_ZOOM);
74         Point p2 = endpoint + (orthogonal * 10.0 * SCREEN_ZOOM);
75
76         // Draw main dimension line
77         painter->DrawLine(p1, p2);
78
79         Point p3 = position + (orthogonal * 16.0 * SCREEN_ZOOM);
80         Point p4 = endpoint + (orthogonal * 16.0 * SCREEN_ZOOM);
81         Point p5 = position + (orthogonal * 4.0 * SCREEN_ZOOM);
82         Point p6 = endpoint + (orthogonal * 4.0 * SCREEN_ZOOM);
83 #else
84 /*
85 The numbers hardcoded into here, what are they?
86 I believe they are pixels.
87 */
88
89         // Get our line parallel to our points
90         Point p1 = position + (orthogonal * 10.0 * size);
91         Point p2 = endpoint + (orthogonal * 10.0 * size);
92
93         // Draw main dimension line
94         painter->DrawLine(p1, p2);
95
96         Point p3 = position + (orthogonal * 16.0 * size);
97         Point p4 = endpoint + (orthogonal * 16.0 * size);
98         Point p5 = position + (orthogonal * 4.0 * size);
99         Point p6 = endpoint + (orthogonal * 4.0 * size);
100 #endif
101
102         // Draw extension lines
103         painter->DrawLine(p3, p5);
104         painter->DrawLine(p4, p6);
105
106         painter->SetBrush(QBrush(QColor(Qt::blue)));
107 //      painter->DrawArrowhead(p1, p2);
108 //      painter->DrawArrowhead(p2, p1);
109         painter->DrawArrowhead(p1, p2, size);
110         painter->DrawArrowhead(p2, p1, size);
111
112         // Draw length of dimension line...
113 //      painter->SetFont(QFont("Arial", 10.0 * Painter::zoom * SCREEN_ZOOM));
114         painter->SetFont(QFont("Arial", 10.0 * Painter::zoom * size));
115         Vector v1((p1.x - p2.x) / 2.0, (p1.y - p2.y) / 2.0);
116         Point ctr = p2 + v1;
117         QString dimText = QString("%1\"").arg(Vector(endpoint - position).Magnitude());
118 //      painter->DrawAngledText(ctr, angle, dimText);
119         painter->DrawAngledText(ctr, angle, dimText, size);
120 }
121
122
123 /*virtual*/ Vector Dimension::Center(void)
124 {
125         // Technically, this is the midpoint but who are we to quibble? :-)
126         Vector v((position.x - endpoint.x) / 2.0, (position.y - endpoint.y) / 2.0);
127         return endpoint + v;
128 }
129
130
131 /*virtual*/ bool Dimension::Collided(Vector /*point*/)
132 {
133 #if 0
134         objectWasDragged = false;
135         Vector lineSegment = endpoint - position;
136         Vector v1 = point - position;
137         Vector v2 = point - endpoint;
138         double parameterizedPoint = lineSegment.Dot(v1) / lineSegment.Magnitude(), distance;
139
140         // Geometric interpretation:
141         // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
142         // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
143         // then the perpendicular lies beyond the 2nd endpoint.
144
145         if (parameterizedPoint < 0.0)
146                 distance = v1.Magnitude();
147         else if (parameterizedPoint > lineSegment.Magnitude())
148                 distance = v2.Magnitude();
149         else                                    // distance = ?Det?(ls, v1) / |ls|
150                 distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y) / lineSegment.Magnitude());
151
152         // If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
153         // intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
154         // {e-s}.{e-p} are both non-negative.  Perpendicular distance from the point to the segment is
155         // computed by first computing the area of the triangle the three points form, then dividing by the
156         // length of the segment.  Distances are done just by the Pythagorean theorem.  Twice the area of the
157         // triangle formed by three points is the determinant of the following matrix:
158         //
159         // sx sy 1
160         // ex ey 1
161         // px py 1
162         //
163         // By translating the start point to the origin, this can be rewritten as:
164         // By subtracting row 1 from all rows, you get the following:
165         // [because sx = sy = 0. you could leave out the -sx/y terms below. because we subtracted
166         // row 1 from all rows (including row 1) row 1 turns out to be zero. duh!]
167         //
168         // 0         0         0        0  0  0
169         // (ex - sx) (ey - sy) 0   ==>  ex ey 0
170         // (px - sx) (py - sy) 0        px py 0
171         //
172         // which greatly simplifies the calculation of the determinant.
173
174         if (state == OSInactive)
175         {
176 //printf("Line: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance);
177 //printf("      v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude());
178 //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);
179 //printf("      \n", );
180 //How to translate this into pixels from Document space???
181 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
182 //the caller knows about the zoom factor and all that good kinda crap
183                 if (v1.Magnitude() < 10.0)
184                 {
185                         oldState = state;
186                         state = OSSelected;
187                         oldPoint = position; //maybe "position"?
188                         draggingHandle1 = true;
189                         return true;
190                 }
191                 else if (v2.Magnitude() < 10.0)
192                 {
193                         oldState = state;
194                         state = OSSelected;
195                         oldPoint = endpoint; //maybe "position"?
196                         draggingHandle2 = true;
197                         return true;
198                 }
199                 else if (distance < 2.0)
200                 {
201                         oldState = state;
202                         state = OSSelected;
203                         oldPoint = point;
204                         dragging = true;
205                         return true;
206                 }
207         }
208         else if (state == OSSelected)
209         {
210                 // Here we test for collision with handles as well! (SOON!)
211 /*
212 Like so:
213                 if (v1.Magnitude() < 2.0) // Handle #1
214                 else if (v2.Magnitude() < 2.0) // Handle #2
215 */
216                 if (distance < 2.0)
217                 {
218                         oldState = state;
219 //                      state = OSInactive;
220                         oldPoint = point;
221                         dragging = true;
222                         return true;
223                 }
224         }
225 #endif
226
227         state = OSInactive;
228         return false;
229 }
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
273 /*virtual*/ void Dimension::PointerReleased(void)
274 {
275         if (draggingHandle1 || draggingHandle2)
276         {
277                 // Set the length (in case the global state was set to fixed (or not))
278                 if (Object::fixedLength)
279                 {
280
281                         if (draggingHandle1)    // startpoint
282                         {
283                                 Vector v = Vector(position - endpoint).Unit() * length;
284                                 position = endpoint + v;
285                         }
286                         else                                    // endpoint
287                         {
288 //                              Vector v1 = endpoint - position;
289                                 Vector v = Vector(endpoint - position).Unit() * length;
290                                 endpoint = position + v;
291                         }
292                 }
293                 else
294                 {
295                         // Otherwise, we calculate the new length, just in case on the next move
296                         // it turns out to have a fixed length. :-)
297                         length = Vector(endpoint - position).Magnitude();
298                 }
299         }
300
301         dragging = false;
302         draggingHandle1 = false;
303         draggingHandle2 = false;
304
305         // Here we check for just a click: If object was clicked and dragged, then
306         // revert to the old state (OSInactive). Otherwise, keep the new state that
307         // we set.
308 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
309 about keeping track of old states...
310 */
311         if (objectWasDragged)
312                 state = oldState;
313 }
314
315
316 /*virtual*/ void Dimension::Enumerate(FILE * file)
317 {
318         fprintf(file, "DIMENSION (%lf,%lf) (%lf,%lf) %i\n", position.x, position.y, endpoint.x, endpoint.y, type);
319 }
320
321
322 // Dimensions are special: they contain exactly *two* points. Here, we check
323 // only for zero/non-zero in returning the correct points.
324 /*virtual*/ Vector Dimension::GetPointAtParameter(double parameter)
325 {
326         if (parameter == 0)
327                 return position;
328
329         return endpoint;
330 }
331
332
333 /*virtual*/ void Dimension::Connect(Object * obj, double param)
334 {
335         // There are four possibilities here...
336         // The param is only looking for 0 or 1 here.
337         if (point1.object == NULL && point2.object == NULL)
338         {
339                 point1.object = obj;
340                 point1.t = param;
341         }
342         else if (point1.object == NULL && point2.object != NULL)
343         {
344                 if (point2.t == param)
345                         point2.object = obj;
346                 else
347                 {
348                         point1.object = obj;
349                         point1.t = param;
350                 }
351         }
352         else if (point1.object != NULL && point2.object == NULL)
353         {
354                 if (point1.t == param)
355                         point1.object = obj;
356                 else
357                 {
358                         point2.object = obj;
359                         point2.t = param;
360                 }
361         }
362         else if (point1.object != NULL && point2.object != NULL)
363         {
364                 if (point1.t == param)
365                         point1.object = obj;
366                 else
367                         point2.object = obj;
368         }
369 }
370
371
372 /*virtual*/ void Dimension::Disconnect(Object * obj, double param)
373 {
374         if (point1.object == obj && point1.t == param)
375                 point1.object = NULL;
376         else if (point2.object == obj && point2.t == param)
377                 point2.object = NULL;
378 }
379
380
381 /*virtual*/ void Dimension::DisconnectAll(Object * obj)
382 {
383         if (point1.object == obj)
384                 point1.object = NULL;
385
386         if (point2.object == obj)
387                 point2.object = NULL;
388 }
389
390
391 /*virtual*/ QRectF Dimension::Extents(void)
392 {
393         Point p1 = position;
394         Point p2 = endpoint;
395
396         if (point1.object)
397                 p1 = point1.object->GetPointAtParameter(point1.t);
398
399         if (point2.object)
400                 p2 = point2.object->GetPointAtParameter(point2.t);
401
402         return QRectF(QPointF(p1.x, p1.y), QPointF(p2.x, p2.y));
403 }
404
405
406 #if 0
407 /*virtual*/ ObjectType Dimension::Type(void)
408 {
409         return OTDimension;
410 }
411 #endif
412
413
414 void Dimension::FlipSides(void)
415 {
416 #if 0
417         Vector tmp = position;
418         position = endpoint;
419         endpoint = tmp;
420 #else
421         Connection tmp = point1;
422         point1 = point2;
423         point2 = tmp;
424 //      double tmp = point1.t;
425 //      point1.t = point2.t;
426 //      point2.t = tmp;
427 //      Object * tmp = point1.object;
428 //      point1.object = point2.object;
429 //      point2.object = tmp;
430 #endif
431         needUpdate = true;
432 }
433