]> Shamusworld >> Repos - architektonas/blob - src/dimension.cpp
Initial stab at text object. Nonfunctional ATM.
[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()), type(dt), point1(NULL), point2(NULL)
26 {
27 }
28
29
30 // This is bad, p1 & p2 could be NULL, causing much consternation...
31 Dimension::Dimension(Connection p1, Connection p2, DimensionType dt/*= DTLinear*/ , Object * p/*= NULL*/):
32         dragging(false), draggingHandle1(false), draggingHandle2(false),
33         length(0), 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.object)
48                 position = point1.object->GetPointAtParameter(point1.t);
49
50         if (point2.object)
51                 endpoint = point2.object->GetPointAtParameter(point2.t);
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         QString dimText = QString("%1\"").arg(Vector(endpoint - position).Magnitude());
91         painter->DrawAngledText(ctr, angle, dimText);
92 }
93
94
95 /*virtual*/ Vector Dimension::Center(void)
96 {
97         // Technically, this is the midpoint but who are we to quibble? :-)
98         Vector v((position.x - endpoint.x) / 2.0, (position.y - endpoint.y) / 2.0);
99         return endpoint + v;
100 }
101
102
103 /*virtual*/ bool Dimension::Collided(Vector /*point*/)
104 {
105 #if 0
106         objectWasDragged = false;
107         Vector lineSegment = endpoint - position;
108         Vector v1 = point - position;
109         Vector v2 = point - endpoint;
110         double parameterizedPoint = lineSegment.Dot(v1) / lineSegment.Magnitude(), distance;
111
112         // Geometric interpretation:
113         // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
114         // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
115         // then the perpendicular lies beyond the 2nd endpoint.
116
117         if (parameterizedPoint < 0.0)
118                 distance = v1.Magnitude();
119         else if (parameterizedPoint > lineSegment.Magnitude())
120                 distance = v2.Magnitude();
121         else                                    // distance = ?Det?(ls, v1) / |ls|
122                 distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y) / lineSegment.Magnitude());
123
124         // If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
125         // intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
126         // {e-s}.{e-p} are both non-negative.  Perpendicular distance from the point to the segment is
127         // computed by first computing the area of the triangle the three points form, then dividing by the
128         // length of the segment.  Distances are done just by the Pythagorean theorem.  Twice the area of the
129         // triangle formed by three points is the determinant of the following matrix:
130         //
131         // sx sy 1
132         // ex ey 1
133         // px py 1
134         //
135         // By translating the start point to the origin, this can be rewritten as:
136         // By subtracting row 1 from all rows, you get the following:
137         // [because sx = sy = 0. you could leave out the -sx/y terms below. because we subtracted
138         // row 1 from all rows (including row 1) row 1 turns out to be zero. duh!]
139         //
140         // 0         0         0        0  0  0
141         // (ex - sx) (ey - sy) 0   ==>  ex ey 0
142         // (px - sx) (py - sy) 0        px py 0
143         //
144         // which greatly simplifies the calculation of the determinant.
145
146         if (state == OSInactive)
147         {
148 //printf("Line: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance);
149 //printf("      v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude());
150 //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);
151 //printf("      \n", );
152 //How to translate this into pixels from Document space???
153 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
154 //the caller knows about the zoom factor and all that good kinda crap
155                 if (v1.Magnitude() < 10.0)
156                 {
157                         oldState = state;
158                         state = OSSelected;
159                         oldPoint = position; //maybe "position"?
160                         draggingHandle1 = true;
161                         return true;
162                 }
163                 else if (v2.Magnitude() < 10.0)
164                 {
165                         oldState = state;
166                         state = OSSelected;
167                         oldPoint = endpoint; //maybe "position"?
168                         draggingHandle2 = true;
169                         return true;
170                 }
171                 else if (distance < 2.0)
172                 {
173                         oldState = state;
174                         state = OSSelected;
175                         oldPoint = point;
176                         dragging = true;
177                         return true;
178                 }
179         }
180         else if (state == OSSelected)
181         {
182                 // Here we test for collision with handles as well! (SOON!)
183 /*
184 Like so:
185                 if (v1.Magnitude() < 2.0) // Handle #1
186                 else if (v2.Magnitude() < 2.0) // Handle #2
187 */
188                 if (distance < 2.0)
189                 {
190                         oldState = state;
191 //                      state = OSInactive;
192                         oldPoint = point;
193                         dragging = true;
194                         return true;
195                 }
196         }
197 #endif
198
199         state = OSInactive;
200         return false;
201 }
202
203
204 /*virtual*/ void Dimension::PointerMoved(Vector point)
205 {
206         // We know this is true because mouse move messages don't come here unless
207         // the object was actually clicked on--therefore we *know* we're being
208         // dragged...
209         objectWasDragged = true;
210
211         if (dragging)
212         {
213                 // Here we need to check whether or not we're dragging a handle or the object itself...
214                 Vector delta = point - oldPoint;
215
216                 position += delta;
217                 endpoint += delta;
218
219                 oldPoint = point;
220                 needUpdate = true;
221         }
222         else if (draggingHandle1)
223         {
224                 Vector delta = point - oldPoint;
225
226                 position += delta;
227
228                 oldPoint = point;
229                 needUpdate = true;
230         }
231         else if (draggingHandle2)
232         {
233                 Vector delta = point - oldPoint;
234
235                 endpoint += delta;
236
237                 oldPoint = point;
238                 needUpdate = true;
239         }
240         else
241                 needUpdate = false;
242 }
243
244
245 /*virtual*/ void Dimension::PointerReleased(void)
246 {
247         if (draggingHandle1 || draggingHandle2)
248         {
249                 // Set the length (in case the global state was set to fixed (or not))
250                 if (Object::fixedLength)
251                 {
252
253                         if (draggingHandle1)    // startpoint
254                         {
255                                 Vector v = Vector(position - endpoint).Unit() * length;
256                                 position = endpoint + v;
257                         }
258                         else                                    // endpoint
259                         {
260 //                              Vector v1 = endpoint - position;
261                                 Vector v = Vector(endpoint - position).Unit() * length;
262                                 endpoint = position + v;
263                         }
264                 }
265                 else
266                 {
267                         // Otherwise, we calculate the new length, just in case on the next move
268                         // it turns out to have a fixed length. :-)
269                         length = Vector(endpoint - position).Magnitude();
270                 }
271         }
272
273         dragging = false;
274         draggingHandle1 = false;
275         draggingHandle2 = false;
276
277         // Here we check for just a click: If object was clicked and dragged, then
278         // revert to the old state (OSInactive). Otherwise, keep the new state that
279         // we set.
280 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
281 about keeping track of old states...
282 */
283         if (objectWasDragged)
284                 state = oldState;
285 }
286
287
288 /*virtual*/ void Dimension::Enumerate(FILE * file)
289 {
290         fprintf(file, "DIMENSION (%lf,%lf) (%lf,%lf) %i\n", position.x, position.y, endpoint.x, endpoint.y, type);
291 }
292
293
294 // Dimensions are special: they contain exactly *two* points. Here, we check
295 // only for zero/non-zero in returning the correct points.
296 /*virtual*/ Vector Dimension::GetPointAtParameter(double parameter)
297 {
298         if (parameter == 0)
299                 return position;
300
301         return endpoint;
302 }
303
304
305 /*virtual*/ void Dimension::Connect(Object * obj, double param)
306 {
307         // There are four possibilities here...
308         // The param is only looking for 0 or 1 here.
309         if (point1.object == NULL && point2.object == NULL)
310         {
311                 point1.object = obj;
312                 point1.t = param;
313         }
314         else if (point1.object == NULL && point2.object != NULL)
315         {
316                 if (point2.t == param)
317                         point2.object = obj;
318                 else
319                 {
320                         point1.object = obj;
321                         point1.t = param;
322                 }
323         }
324         else if (point1.object != NULL && point2.object == NULL)
325         {
326                 if (point1.t == param)
327                         point1.object = obj;
328                 else
329                 {
330                         point2.object = obj;
331                         point2.t = param;
332                 }
333         }
334         else if (point1.object != NULL && point2.object != NULL)
335         {
336                 if (point1.t == param)
337                         point1.object = obj;
338                 else
339                         point2.object = obj;
340         }
341 }
342
343
344 /*virtual*/ void Dimension::Disconnect(Object * obj, double param)
345 {
346         if (point1.object == obj && point1.t == param)
347                 point1 = NULL;
348         else if (point2.object == obj && point2.t == param)
349                 point2 = NULL;
350 }
351
352
353 /*virtual*/ void Dimension::DisconnectAll(Object * obj)
354 {
355         if (point1.object == obj)
356                 point1 = NULL;
357
358         if (point2.object == obj)
359                 point2 = NULL;
360 }
361
362
363 void Dimension::FlipSides(void)
364 {
365 #if 0
366         Vector tmp = position;
367         position = endpoint;
368         endpoint = tmp;
369 #else
370         Connection tmp = point1;
371         point1 = point2;
372         point2 = tmp;
373 //      double tmp = point1.t;
374 //      point1.t = point2.t;
375 //      point2.t = tmp;
376 //      Object * tmp = point1.object;
377 //      point1.object = point2.object;
378 //      point2.object = tmp;
379 #endif
380         needUpdate = true;
381 }
382