1 // constructionline.cpp
3 // Part of the Architektonas Project
4 // Originally part of QCad Community Edition by Andrew Mustun
5 // Extensively rewritten and refactored by James L. Hammons
6 // Portions copyright (C) 2001-2003 RibbonSoft
7 // Copyright (C) 2010 Underground Software
8 // See the README and GPLv2 files for licensing and warranty information
10 // JLH = James L. Hammons <jlhamm@acm.org>
13 // --- ---------- -----------------------------------------------------------
14 // JLH 05/28/2010 Added this text. :-)
15 // JLH 06/02/2010 Moved implementation from header to here WHERE IT BELONGS.
18 #include "constructionline.h"
25 ConstructionLine::ConstructionLine(EntityContainer * parent,
26 const ConstructionLineData & d): AtomicEntity(parent), data(d)
34 ConstructionLine::~ConstructionLine()
38 Entity * ConstructionLine::clone()
40 ConstructionLine * c = new ConstructionLine(*this);
45 /** @return RS2::EntityConstructionLine */
46 /*virtual*/ RS2::EntityType ConstructionLine::rtti() const
48 return RS2::EntityConstructionLine;
53 * @return Start point of the entity.
55 /*virtual*/ Vector ConstructionLine::getStartpoint() const
62 * @return End point of the entity.
64 /*virtual*/ Vector ConstructionLine::getEndpoint() const
69 /** @return Copy of data that defines the line. */
70 ConstructionLineData ConstructionLine::getData() const
75 /** @return First definition point. */
76 Vector ConstructionLine::getPoint1() const
80 /** @return Second definition point. */
81 Vector ConstructionLine::getPoint2() const
86 void ConstructionLine::calculateBorders()
88 minV = Vector::minimum(data.point1, data.point2);
89 maxV = Vector::maximum(data.point1, data.point2);
92 Vector ConstructionLine::getNearestEndpoint(const Vector & coord, double * dist)
97 dist1 = data.point1.distanceTo(coord);
98 dist2 = data.point2.distanceTo(coord);
105 nearerPoint = &data.point2;
112 nearerPoint = &data.point1;
118 Vector ConstructionLine::getNearestPointOnEntity(const Vector & coord,
119 bool /*onEntity*/, double * /*dist*/, Entity ** entity)
124 Vector ae = data.point2-data.point1;
125 Vector ea = data.point1-data.point2;
126 Vector ap = coord-data.point1;
127 Vector ep = coord-data.point2;
129 if (ae.magnitude() < 1.0e-6 || ea.magnitude() < 1.0e-6)
130 return Vector(false);
132 // Orthogonal projection from both sides:
133 Vector ba = ae * Vector::dotP(ae, ap) / (ae.magnitude() * ae.magnitude());
134 Vector be = ea * Vector::dotP(ea, ep) / (ea.magnitude() * ea.magnitude());
136 return data.point1 + ba;
139 Vector ConstructionLine::getNearestCenter(const Vector & /*coord*/, double * dist)
142 *dist = RS_MAXDOUBLE;
144 return Vector(false);
147 Vector ConstructionLine::getNearestMiddle(const Vector & /*coord*/, double * dist)
150 *dist = RS_MAXDOUBLE;
152 return Vector(false);
155 Vector ConstructionLine::getNearestDist(double /*distance*/, const Vector & /*coord*/,
159 *dist = RS_MAXDOUBLE;
161 return Vector(false);
164 double ConstructionLine::getDistanceToPoint(const Vector& coord,
166 RS2::ResolveLevel /*level*/, double /*solidDist*/)
168 DEBUG->print("ConstructionLine::getDistanceToPoint");
173 double dist = RS_MAXDOUBLE;
174 Vector ae = data.point2-data.point1;
175 Vector ea = data.point1-data.point2;
176 Vector ap = coord-data.point1;
177 Vector ep = coord-data.point2;
179 if (ae.magnitude() < 1.0e-6 || ea.magnitude() < 1.0e-6)
182 // Orthogonal projection from both sides:
183 Vector ba = ae * Vector::dotP(ae, ap) / Math::pow(ae.magnitude(), 2);
184 Vector be = ea * Vector::dotP(ea, ep) / Math::pow(ea.magnitude(), 2);
186 DEBUG->print("ba: %f", ba.magnitude());
187 DEBUG->print("ae: %f", ae.magnitude());
189 Vector cp = Vector::crossP(ap, ae);
190 dist = cp.magnitude() / ae.magnitude();
195 void ConstructionLine::move(Vector offset)
197 data.point1.move(offset);
198 data.point2.move(offset);
199 //calculateBorders();
202 void ConstructionLine::rotate(Vector center, double angle)
204 data.point1.rotate(center, angle);
205 data.point2.rotate(center, angle);
206 //calculateBorders();
209 void ConstructionLine::scale(Vector center, Vector factor)
211 data.point1.scale(center, factor);
212 data.point2.scale(center, factor);
213 //calculateBorders();
216 void ConstructionLine::mirror(Vector axisPoint1, Vector axisPoint2)
218 data.point1.mirror(axisPoint1, axisPoint2);
219 data.point2.mirror(axisPoint1, axisPoint2);
222 /*virtual*/ void ConstructionLine::draw(PaintInterface *, GraphicView *, double)
227 * Dumps the point's data to stdout.
229 std::ostream & operator<<(std::ostream & os, const ConstructionLine & l)
231 os << " ConstructionLine: " << l.getData() << "\n";