]> Shamusworld >> Repos - architektonas/blob - src/line.cpp
Fixed Line rendering to keep attached Dimensions correct length with 'Fix Len'
[architektonas] / src / line.cpp
1 // line.cpp: Line 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  03/22/2011  Created this file
12 // JLH  04/11/2011  Fixed attached dimensions to stay at correct length when
13 //                  "Fixed Length" button is down
14 //
15
16 #include "line.h"
17
18 #include <QtGui>
19 #include "dimension.h"
20
21 Line::Line(Vector p1, Vector p2, Object * p/*= NULL*/): Object(p1, p), endpoint(p2),
22         dragging(false), draggingHandle1(false), draggingHandle2(false), //needUpdate(false),
23         length(p2.Magnitude())
24 {
25 }
26
27 Line::~Line()
28 {
29 }
30
31 /*virtual*/ void Line::Draw(QPainter * painter)
32 {
33         if (state == OSSelected)
34                 painter->setPen(QPen(Qt::red, 2.0, Qt::DotLine));
35         else
36                 painter->setPen(QPen(Qt::black, 1.0, Qt::SolidLine));
37
38 //      if (draggingHandle1)
39         if (state == OSSelected)
40                 painter->drawEllipse(QPointF(position.x, position.y), 4.0, 4.0);
41
42 //      if (draggingHandle2)
43         if (state == OSSelected)
44                 painter->drawEllipse(QPointF(endpoint.x, endpoint.y), 4.0, 4.0);
45
46         if (Object::fixedLength && (draggingHandle1 || draggingHandle2))
47         {
48                 Vector point1 = (draggingHandle1 ? endpoint : position);
49                 Vector point2 = (draggingHandle1 ? position : endpoint);
50
51                 Vector current(point2 - point1);
52                 Vector v = current.Unit() * length;
53                 Vector v2 = point1 + v;
54                 painter->drawLine((int)point1.x, (int)point1.y, (int)v2.x, (int)v2.y);
55
56                 if (current.Magnitude() > length)
57                 {
58                         painter->setPen(QPen(QColor(128, 0, 0), 1.0, Qt::DashLine));
59                         painter->drawLine((int)v2.x, (int)v2.y, (int)point2.x, (int)point2.y);
60                 }
61         }
62         else
63                 painter->drawLine((int)position.x, (int)position.y, (int)endpoint.x, (int)endpoint.y);
64 }
65
66 /*virtual*/ Vector Line::Center(void)
67 {
68         // Technically, this is the midpoint but who are we to quibble? :-)
69         Vector v((position.x - endpoint.x) / 2.0, (position.y - endpoint.y) / 2.0);
70         return endpoint + v;
71 }
72
73 /*virtual*/ bool Line::Collided(Vector point)
74 {
75         objectWasDragged = false;
76         Vector lineSegment = endpoint - position;
77         Vector v1 = point - position;
78         Vector v2 = point - endpoint;
79         double parameterizedPoint = lineSegment.Dot(v1) / lineSegment.Magnitude(), distance;
80
81         // Geometric interpretation:
82         // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
83         // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
84         // then the perpendicular lies beyond the 2nd endpoint.
85
86         if (parameterizedPoint < 0.0)
87                 distance = v1.Magnitude();
88         else if (parameterizedPoint > lineSegment.Magnitude())
89                 distance = v2.Magnitude();
90         else                                    // distance = ?Det?(ls, v1) / |ls|
91                 distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y) / lineSegment.Magnitude());
92
93         // If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
94         // intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
95         // {e-s}.{e-p} are both non-negative.  Perpendicular distance from the point to the segment is
96         // computed by first computing the area of the triangle the three points form, then dividing by the
97         // length of the segment.  Distances are done just by the Pythagorean theorem.  Twice the area of the
98         // triangle formed by three points is the determinant of the following matrix:
99         //
100         // sx sy 1
101         // ex ey 1
102         // px py 1
103         //
104         // By translating the start point to the origin, this can be rewritten as:
105         // By subtracting row 1 from all rows, you get the following:
106         // [because sx = sy = 0. you could leave out the -sx/y terms below. because we subtracted
107         // row 1 from all rows (including row 1) row 1 turns out to be zero. duh!]
108         //
109         // 0         0         0        0  0  0
110         // (ex - sx) (ey - sy) 0   ==>  ex ey 0
111         // (px - sx) (py - sy) 0        px py 0
112         //
113         // which greatly simplifies the calculation of the determinant.
114
115         if (state == OSInactive)
116         {
117 //printf("Line: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance);
118 //printf("      v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude());
119 //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);
120 //printf("      \n", );
121 //How to translate this into pixels from Document space???
122 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
123 //the caller knows about the zoom factor and all that good kinda crap
124                 if (v1.Magnitude() < 10.0)
125                 {
126                         oldState = state;
127                         state = OSSelected;
128                         oldPoint = position; //maybe "position"?
129                         draggingHandle1 = true;
130                         return true;
131                 }
132                 else if (v2.Magnitude() < 10.0)
133                 {
134                         oldState = state;
135                         state = OSSelected;
136                         oldPoint = endpoint; //maybe "position"?
137                         draggingHandle2 = true;
138                         return true;
139                 }
140                 else if (distance < 2.0)
141                 {
142                         oldState = state;
143                         state = OSSelected;
144                         oldPoint = point;
145                         dragging = true;
146                         return true;
147                 }
148         }
149         else if (state == OSSelected)
150         {
151                 // Here we test for collision with handles as well! (SOON!)
152 /*
153 Like so:
154                 if (v1.Magnitude() < 2.0) // Handle #1
155                 else if (v2.Magnitude() < 2.0) // Handle #2
156 */
157                 if (distance < 2.0)
158                 {
159                         oldState = state;
160 //                      state = OSInactive;
161                         oldPoint = point;
162                         dragging = true;
163                         return true;
164                 }
165         }
166
167         state = OSInactive;
168         return false;
169 }
170
171 /*virtual*/ void Line::PointerMoved(Vector point)
172 {
173         // We know this is true because mouse move messages don't come here unless
174         // the object was actually clicked on--therefore we *know* we're being
175         // dragged...
176         objectWasDragged = true;
177
178         if (dragging)
179         {
180                 // Here we need to check whether or not we're dragging a handle or the object itself...
181                 Vector delta = point - oldPoint;
182
183                 position += delta;
184                 endpoint += delta;
185
186                 oldPoint = point;
187                 needUpdate = true;
188         }
189         else if (draggingHandle1)
190         {
191                 Vector delta = point - oldPoint;
192
193                 position += delta;
194
195                 oldPoint = point;
196                 needUpdate = true;
197         }
198         else if (draggingHandle2)
199         {
200                 Vector delta = point - oldPoint;
201
202                 endpoint += delta;
203
204                 oldPoint = point;
205                 needUpdate = true;
206         }
207         else
208                 needUpdate = false;
209
210         if (needUpdate)
211         {
212 // should only do this if "Fixed Length" is set... !!! FIX !!!
213                 Vector point1 = (draggingHandle1 ? endpoint : position);
214                 Vector point2 = (draggingHandle1 ? position : endpoint);
215
216                 Vector current(point2 - point1);
217                 Vector v = current.Unit() * length;
218                 Vector v2 = point1 + v;
219
220                 if (dimPoint1)
221                         dimPoint1->SetPoint1(draggingHandle1 ? v2 : position);
222                 
223                 if (dimPoint2)
224                         dimPoint2->SetPoint2(draggingHandle2 ? v2 : endpoint);
225         }
226 }
227
228 /*virtual*/ void Line::PointerReleased(void)
229 {
230         if (draggingHandle1 || draggingHandle2)
231         {
232                 // Set the length (in case the global state was set to fixed (or not))
233                 if (Object::fixedLength)
234                 {
235
236                         if (draggingHandle1)    // startpoint
237                         {
238                                 Vector v = Vector(position - endpoint).Unit() * length;
239                                 position = endpoint + v;
240                         }
241                         else                                    // endpoint
242                         {
243 //                              Vector v1 = endpoint - position;
244                                 Vector v = Vector(endpoint - position).Unit() * length;
245                                 endpoint = position + v;
246                         }
247                 }
248                 else
249                 {
250                         // Otherwise, we calculate the new length, just in case on the next move
251                         // it turns out to have a fixed length. :-)
252                         length = Vector(endpoint - position).Magnitude();
253                 }
254         }
255
256         dragging = false;
257         draggingHandle1 = false;
258         draggingHandle2 = false;
259
260         // Here we check for just a click: If object was clicked and dragged, then
261         // revert to the old state (OSInactive). Otherwise, keep the new state that
262         // we set.
263 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
264 about keeping track of old states...
265 */
266         if (objectWasDragged)
267                 state = oldState;
268 }
269
270 void Line::SetDimensionOnPoint1(Dimension * dimension)
271 {
272         dimPoint1 = dimension;
273
274         if (dimension)
275                 dimension->SetPoint1(position);
276 }
277
278 void Line::SetDimensionOnPoint2(Dimension * dimension)
279 {
280         dimPoint2 = dimension;
281
282         if (dimension)
283                 dimension->SetPoint2(endpoint);
284 }
285
286 /*
287 Intersection of two lines:
288
289 Find where the lines with equations r = i + j + t (3i - j) and r = -i + s (j) intersect.
290
291 When they intersect, we can set the equations equal to one another:
292
293 i + j + t (3i - j) = -i + s (j)
294
295 Equating coefficients:
296 1 + 3t = -1 and 1 - t = s
297 So t = -2/3 and s = 5/3
298
299 The position vector of the intersection point is therefore given by putting t = -2/3 or s = 5/3 into one of the above equations. This gives -i +5j/3 .
300
301
302 so, let's say we have two lines, l1 and l2. Points are v0(p0x, p0y), v1(p1x, p1y) for l1
303 and v2(p2x, p2y), v3(p3x, p3y) for l2.
304
305 d1 = v1 - v0, d2 = v3 - v2
306
307 Our parametric equations for the line then are:
308
309 r1 = v0 + t(d1)
310 r2 = v2 + s(d2)
311
312 Set r1 = r2, thus we have:
313
314 v0 + t(d1) = v2 + s(d2)
315
316 Taking coefficients, we have:
317
318 p0x + t(d1x) = p2x + s(d2x)
319 p0y + t(d1y) = p2y + s(d2y)
320
321 rearranging we get:
322
323 t(d1x) - s(d2x) = p2x - p0x
324 t(d1y) - s(d2y) = p2y - p0y
325
326 Determinant D is ad - bc where the matrix look like:
327
328 a b
329 c d
330
331 so D = (d1x)(d2y) - (d2x)(d1y)
332 if D = 0, the lines are parallel.
333 Dx = (p2x - p0x)(d2y) - (d2x)(p2y - p0y)
334 Dy = (d1x)(p2y - p0y) - (p2x - p0x)(d1y)
335 t = Dx/D, s = Dy/D
336
337 We only need to calculate t, as we can then multiply it by d1 to get the intersection point.
338
339 ---------------------------------------------------------------------------------------------------
340
341 The first and most preferred method for intersection calculation is the perp-product calculation. There are two vectors, v1 and v2. Create a third vector vector between the starting points of these vectors, and calculate the perp product of v2 and the two other vectors. These two scalars have to be divided to get the mulitplication ratio of v1 to reach intersection point. So:
342
343 v1 ( bx1 , by1 );
344 v2 ( bx2 , by2 );
345 v3 ( bx3 , by3 );
346
347 Perp product is equal with dot product of normal of first vector and the second vector, so we need normals:
348
349 n1 ( -by1 , bx1 );
350 n3 ( -by3 , bx3 );
351
352 Dot products:
353
354 dp1 = n3.v2 = -by3*bx2 + bx3*by2;
355 dp2 = n1.v2 = -by1*bx2 + bx1*by2;
356
357 ratio = dp1/dp2;
358 crossing vector = v1*rat;
359
360 And that's it.
361 */
362