1 // dimension.cpp: Dimension object
3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 04/04/2011 Created this file, basic rendering
14 #include "dimension.h"
17 #include "mathconstants.h"
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)
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)
34 Dimension::~Dimension()
38 /*virtual*/ void Dimension::Draw(Painter * painter)
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.
48 if (state == OSSelected)
49 painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
51 painter->SetPen(QPen(Qt::blue, 1.0, Qt::SolidLine));
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();
59 // Get our line parallel to our points
60 Point p1 = position + (orthogonal * 10.0);
61 Point p2 = endpoint + (orthogonal * 10.0);
63 // Draw main dimension line
64 painter->DrawLine(p1, p2);
66 Point p3 = position + (orthogonal * 16.0);
67 Point p4 = endpoint + (orthogonal * 16.0);
68 Point p5 = position + (orthogonal * 4.0);
69 Point p6 = endpoint + (orthogonal * 4.0);
71 // Draw extension lines
72 painter->DrawLine(p3, p5);
73 painter->DrawLine(p4, p6);
75 // Draw length of dimension line...
76 painter->SetFont(QFont("Arial", 10));
77 Vector v1((p1.x - p2.x) / 2.0, (p1.y - p2.y) / 2.0);
79 // This is in pixels, which isn't even remotely correct... !!! FIX !!!
80 QString dimText = QString("%1\"").arg(Vector(endpoint - position).Magnitude());
81 // int textWidth = QFontMetrics(painter->font()).width(dimText);
82 // int textHeight = QFontMetrics(painter->font()).height();
84 //We have to do transformation voodoo to make the text come out readable and in correct orientation...
85 //Some things to note here: if angle > 90 degrees, then we need to take the negative of the angle
88 painter->translate(ctr.x, ctr.y);
90 //16 : printf("textHeight: %d\n", textHeight);
92 //Fix text so it isn't upside down...
93 if ((angle > PI * 0.5) && (angle < PI * 1.5))
99 painter->rotate(angle * RADIANS_TO_DEGREES);
100 painter->scale(1.0, -1.0);
101 //painter->translate(-textWidth / 2, -24);
102 // painter->drawText(0, 0, textWidth, 20, Qt::AlignCenter, dimText);
103 // This version draws the y-coord from the baseline of the font
104 painter->DrawText(-textWidth / 2, yOffset, dimText);
105 //painter->setPen(QPen(QColor(0xFF, 0x20, 0x20), 1.0, Qt::SolidLine));
106 //painter->drawLine(20, 0, -20, 0);
107 //painter->drawLine(0, 20, 0, -20);
110 // painter->DrawText(QRectF(QPointF(ctr.x, ctr.y), QPointF(ctr.x + textWidth, ctr.y + textHeight)), Qt::AlignVCenter, dimText);
111 // Now that we've taken our own good advice, maybe we should have the painter class
112 // do a nice abstracted text draw routine? :-)
113 painter->DrawAngledText(ctr, angle, dimText);
117 All of the preceeding makes me think that rather than try to compensate for Qt's unbelieveably
118 AWFUL decision to go with a wrong-handed graphics subsystem, it may be better to just stuff
119 all of that crap into some kind of subclass that handles all the nastiness behind the scenes.
120 I mean, really, all this crap just to get some proplerly rendered text on the screen? How
121 retarded is that? :-/
125 /*virtual*/ Vector Dimension::Center(void)
127 // Technically, this is the midpoint but who are we to quibble? :-)
128 Vector v((position.x - endpoint.x) / 2.0, (position.y - endpoint.y) / 2.0);
132 /*virtual*/ bool Dimension::Collided(Vector /*point*/)
135 objectWasDragged = false;
136 Vector lineSegment = endpoint - position;
137 Vector v1 = point - position;
138 Vector v2 = point - endpoint;
139 double parameterizedPoint = lineSegment.Dot(v1) / lineSegment.Magnitude(), distance;
141 // Geometric interpretation:
142 // pp is the paremeterized point on the vector ls where the perpendicular intersects ls.
143 // If pp < 0, then the perpendicular lies beyond the 1st endpoint. If pp > length of ls,
144 // then the perpendicular lies beyond the 2nd endpoint.
146 if (parameterizedPoint < 0.0)
147 distance = v1.Magnitude();
148 else if (parameterizedPoint > lineSegment.Magnitude())
149 distance = v2.Magnitude();
150 else // distance = ?Det?(ls, v1) / |ls|
151 distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y) / lineSegment.Magnitude());
153 // If the segment endpoints are s and e, and the point is p, then the test for the perpendicular
154 // intercepting the segment is equivalent to insisting that the two dot products {s-e}.{s-p} and
155 // {e-s}.{e-p} are both non-negative. Perpendicular distance from the point to the segment is
156 // computed by first computing the area of the triangle the three points form, then dividing by the
157 // length of the segment. Distances are done just by the Pythagorean theorem. Twice the area of the
158 // triangle formed by three points is the determinant of the following matrix:
164 // By translating the start point to the origin, this can be rewritten as:
165 // By subtracting row 1 from all rows, you get the following:
166 // [because sx = sy = 0. you could leave out the -sx/y terms below. because we subtracted
167 // row 1 from all rows (including row 1) row 1 turns out to be zero. duh!]
170 // (ex - sx) (ey - sy) 0 ==> ex ey 0
171 // (px - sx) (py - sy) 0 px py 0
173 // which greatly simplifies the calculation of the determinant.
175 if (state == OSInactive)
177 //printf("Line: pp = %lf, length = %lf, distance = %lf\n", parameterizedPoint, lineSegment.Magnitude(), distance);
178 //printf(" v1.Magnitude = %lf, v2.Magnitude = %lf\n", v1.Magnitude(), v2.Magnitude());
179 //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);
181 //How to translate this into pixels from Document space???
182 //Maybe we need to pass a scaling factor in here from the caller? That would make sense, as
183 //the caller knows about the zoom factor and all that good kinda crap
184 if (v1.Magnitude() < 10.0)
188 oldPoint = position; //maybe "position"?
189 draggingHandle1 = true;
192 else if (v2.Magnitude() < 10.0)
196 oldPoint = endpoint; //maybe "position"?
197 draggingHandle2 = true;
200 else if (distance < 2.0)
209 else if (state == OSSelected)
211 // Here we test for collision with handles as well! (SOON!)
214 if (v1.Magnitude() < 2.0) // Handle #1
215 else if (v2.Magnitude() < 2.0) // Handle #2
220 // state = OSInactive;
232 /*virtual*/ void Dimension::PointerMoved(Vector point)
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
237 objectWasDragged = true;
241 // Here we need to check whether or not we're dragging a handle or the object itself...
242 Vector delta = point - oldPoint;
250 else if (draggingHandle1)
252 Vector delta = point - oldPoint;
259 else if (draggingHandle2)
261 Vector delta = point - oldPoint;
272 /*virtual*/ void Dimension::PointerReleased(void)
274 if (draggingHandle1 || draggingHandle2)
276 // Set the length (in case the global state was set to fixed (or not))
277 if (Object::fixedLength)
280 if (draggingHandle1) // startpoint
282 Vector v = Vector(position - endpoint).Unit() * length;
283 position = endpoint + v;
287 // Vector v1 = endpoint - position;
288 Vector v = Vector(endpoint - position).Unit() * length;
289 endpoint = position + v;
294 // Otherwise, we calculate the new length, just in case on the next move
295 // it turns out to have a fixed length. :-)
296 length = Vector(endpoint - position).Magnitude();
301 draggingHandle1 = false;
302 draggingHandle2 = false;
304 // Here we check for just a click: If object was clicked and dragged, then
305 // revert to the old state (OSInactive). Otherwise, keep the new state that
307 /*Maybe it would be better to just check for "object was dragged" state and not have to worry
308 about keeping track of old states...
310 if (objectWasDragged)
314 void Dimension::SetPoint1(Vector * v)
320 void Dimension::SetPoint2(Vector * v)
326 void Dimension::FlipSides(void)
329 Vector tmp = position;
333 Vector * tmp = point1;