ACF $AcfVersion:0$
CLine2d.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later OR GPL-2.0-or-later OR GPL-3.0-or-later OR LicenseRef-ACF-Commercial
2#pragma once
3
4
5// Qt includes
6#include <QtCore/QPair>
7
8// ACF includes
9#include <i2d/CObject2dBase.h>
10#include <i2d/CVector2d.h>
11
12
13namespace i2d
14{
15
16
17class CRectangle;
18
19
25{
26public:
27 static QByteArray GetTypeName();
28
30 CLine2d(const CVector2d& p1, const CVector2d& p2);
31 CLine2d(double x1, double y1, double x2, double y2);
32
33 bool operator==(const CLine2d& line) const;
34 bool operator!=(const CLine2d& line) const;
35
39 bool IsNull() const;
40
44 const CVector2d& GetPoint1() const;
45
51
55 void SetPoint1(const CVector2d& point);
56
60 const CVector2d& GetPoint2() const;
61
67
71 void SetPoint2(const CVector2d& point);
72
77 double GetSlope() const;
78
83 double GetIntercept() const;
84
88 bool IsParallel(const CLine2d& line) const;
89
95 bool IsIntersectedBy(const CLine2d& line) const;
96
103 static bool Intersects(const CVector2d& lhsp1, const CVector2d& lhsp2,
104 const CVector2d& rhsp1, const CVector2d& rhsp2);
105
112 bool GetIntersection(const CLine2d& line, CVector2d& result) const;
113
121 bool GetExtendedIntersection(const CLine2d& line, CVector2d& result) const;
122
126 double GetDistance(const CVector2d& position) const;
127
131 double GetMaxDistance(const CVector2d& position) const;
132
137 double GetExtendedDistance(const CVector2d& position) const;
138
143 CVector2d GetPositionFromAlpha(double alpha) const;
144
148 CVector2d GetDiffVector() const;
149
153 double GetLength() const;
154
158 double GetLength2() const;
159
164 double GetDirectionAngle() const;
165
169 CLine2d GetClipped(const CRectangle& rect) const;
170
175
179 double GetCutXPos(double linePosY) const;
180
184 double GetCutYPos(double linePosX) const;
185
189 void PushBeginPoint(const i2d::CVector2d& newBeginPoint);
190
194 void PushEndPoint(const i2d::CVector2d& newEndPoint);
195
199 void PushBeginPointQuiet(const i2d::CVector2d& newBeginPoint);
200 void PushEndPointQuiet(const i2d::CVector2d& newEndPoint);
201
202 void SetPoint1Quiet(const CVector2d& point);
203 void SetPoint2Quiet(const CVector2d& point);
204
208 double GetCutAlpha(const CLine2d& line) const;
209
210 double GetCastAlpha(const i2d::CVector2d& point) const;
211
217 QPair<double, double> GetAlphaAndCastDistance(const i2d::CVector2d& point) const;
218
223 bool GetCutPoint(const CLine2d& otherLine, i2d::CVector2d& cutPoint) const;
224
229
235
240
247
252
257
261 bool ApproxFromPoints(const QSet<i2d::CVector2d>& points);
262
263 // reimplemented (i2d::IObject2d)
264 virtual CVector2d GetCenter() const override;
265 virtual void MoveCenterTo(const CVector2d& position) override;
266 virtual CRectangle GetBoundingBox() const override;
267 virtual bool Transform(
268 const ITransformation2d& transformation,
270 double* errorFactorPtr = NULL) override;
271 virtual bool InvTransform(
272 const ITransformation2d& transformation,
274 double* errorFactorPtr = NULL) override;
275 virtual bool GetTransformed(
276 const ITransformation2d& transformation,
277 IObject2d& result,
279 double* errorFactorPtr = NULL) const override;
280 virtual bool GetInvTransformed(
281 const ITransformation2d& transformation,
282 IObject2d& result,
284 double* errorFactorPtr = NULL) const override;
285
286 // reimplemented (iser::IObject)
287 virtual QByteArray GetFactoryId() const override;
288
289 // reimplemented (iser::ISerializable)
290 virtual bool Serialize(iser::IArchive& archive) override;
291
292 // reimplemented (istd::IChangeable)
293 virtual int GetSupportedOperations() const override;
294 virtual bool CopyFrom(const IChangeable& object, CompatibilityMode mode = CM_WITHOUT_REFS) override;
296
297private:
298 CVector2d m_point1;
299 CVector2d m_point2;
300};
301
302
303// public inline methods
304
305inline bool CLine2d::operator == (const CLine2d& line) const
306{
307 return ((m_point1 == line.m_point1) && (m_point2 == line.m_point2));
308}
309
310
311inline bool CLine2d::operator != (const CLine2d& line) const
312{
313 return !operator == (line);
314}
315
316
317inline bool CLine2d::IsNull() const
318{
319 return (m_point1 == m_point2);
320}
321
322
324{
325 return m_point2 - m_point1;
326}
327
328
329inline const CVector2d& CLine2d::GetPoint1() const
330{
331 return m_point1;
332}
333
334
336{
337 return m_point1;
338}
339
340
341inline const CVector2d& CLine2d::GetPoint2() const
342{
343 return m_point2;
344}
345
346
348{
349 return m_point2;
350}
351
352
353inline double CLine2d::GetCutXPos(double linePosY) const
354{
355 CVector2d diff = GetDiffVector();
356
357 return (linePosY - m_point1.GetY()) * diff.GetX() / diff.GetY() + m_point1.GetX();
358}
359
360
361inline double CLine2d::GetCutYPos(double linePosX) const
362{
363 CVector2d diff = GetDiffVector();
364
365 return (linePosX - m_point1.GetX()) * diff.GetY() / diff.GetX() + m_point1.GetY();
366}
367
368
369inline double CLine2d::GetCutAlpha(const CLine2d& line) const
370{
371 const i2d::CVector2d& delta = GetDiffVector();
372 const i2d::CVector2d& lineDelta = line.GetDiffVector();
373 const i2d::CVector2d& beginPoint = GetPoint1();
374 const i2d::CVector2d& lineBeginPoint = line.GetPoint1();
375
376 double crossProductZ = lineDelta.GetCrossProductZ(delta);
377
378 return (beginPoint.GetCrossProductZ(delta) + delta.GetCrossProductZ(lineBeginPoint)) / crossProductZ;
379}
380
381
382inline double CLine2d::GetCastAlpha(const i2d::CVector2d& point) const
383{
385 i2d::CVector2d deltaToPoint = point - m_point1;
386
387 double dotProduct = delta.GetDotProduct(deltaToPoint);
388 return dotProduct / delta.GetLength2();
389}
390
391
392inline QPair<double, double> CLine2d::GetAlphaAndCastDistance(const i2d::CVector2d& point) const
393{
394 i2d::CVector2d delta = m_point2 - m_point1;
395 i2d::CVector2d deltaToPoint = point - m_point1;
396
397 double dotProduct = delta.GetDotProduct(deltaToPoint);
398 double field = delta.GetCrossProductZ(deltaToPoint);
399 double deltaLength2 = delta.GetLength2();
400
401 return QPair<double, double>(dotProduct / deltaLength2, field / ::sqrt(deltaLength2));
402}
403
404
405inline void CLine2d::PushBeginPointQuiet(const i2d::CVector2d& newBeginPoint)
406{
407 m_point2 = m_point1;
408 m_point1 = newBeginPoint;
409}
410
411
412inline void CLine2d::PushEndPointQuiet(const i2d::CVector2d& newEndPoint)
413{
414 m_point1 = m_point2;
415 m_point2 = newEndPoint;
416}
417
418
419inline void CLine2d::SetPoint1Quiet(const CVector2d& point)
420{
421 m_point1 = point;
422}
423
424
425inline void CLine2d::SetPoint2Quiet(const CVector2d& point)
426{
427 m_point2 = point;
428}
429
430
431// related global functions
432
433inline uint qHash(const CLine2d& key, uint seed = 0)
434{
435 return qHash(key.GetPoint1(), seed) ^ qHash(key.GetPoint2(), seed);
436}
437
438
439} // namespace i2d
440
441
442
443
Definition of a line in 2D-space.
Definition CLine2d.h:25
bool IsIntersectedBy(const CLine2d &line) const
Check if two lines intersect.
virtual bool CopyFrom(const IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS) override
virtual QByteArray GetFactoryId() const override
const CVector2d & GetPoint1() const
Get value of first line point.
Definition CLine2d.h:329
double GetCutYPos(double linePosX) const
Get X position of cutting this line by specified vertical line.
Definition CLine2d.h:361
virtual CRectangle GetBoundingBox() const override
Get bounding box of this shape.
CVector2d GetPositionFromAlpha(double alpha) const
Get position of point using normed 'alpha' value.
double GetSlope() const
Get slope of this line.
double GetCutAlpha(const CLine2d &line) const
Get a proportion of lines cut point to line length (called also 'alpha value').
Definition CLine2d.h:369
bool IsNull() const
Returns true, if the line has a length equal 0.
Definition CLine2d.h:317
static QByteArray GetTypeName()
void PushEndPointQuiet(const i2d::CVector2d &newEndPoint)
Definition CLine2d.h:412
static bool Intersects(const CVector2d &lhsp1, const CVector2d &lhsp2, const CVector2d &rhsp1, const CVector2d &rhsp2)
Check if two lines intersect - a shortcut to avoid the creation of lines, useful in performance-criti...
CLine2d GetShortestConnection(const i2d::CVector2d &point) const
Return a line, which connects the nearest point of line and specified point.
CLine2d GetShortestConnection(const CLine2d &line) const
Return a line, which connects the nearest points of two lines.
double GetDirectionAngle() const
Get direction angle of this line.
bool GetExtendedIntersection(const CLine2d &line, CVector2d &result) const
Get intersection position of extended lines.
virtual CVector2d GetCenter() const override
Returns center of this 2D-object.
double GetMaxDistance(const CVector2d &position) const
Get maximal distance from points of this line to specified position.
virtual istd::TUniqueInterfacePtr< istd::IChangeable > CloneMe(CompatibilityMode mode=CM_WITHOUT_REFS) const override
Make a copy of this object.
bool ApproxFromPoints(const QSet< i2d::CVector2d > &points)
Calculate this line as approximation of set of points.
virtual bool GetTransformed(const ITransformation2d &transformation, IObject2d &result, ITransformation2d::ExactnessMode mode=ITransformation2d::EM_NONE, double *errorFactorPtr=NULL) const override
Calulate transformation of the object into second one.
i2d::CVector2d GetExtendedNearestPoint(const i2d::CVector2d &point) const
Return a point on the straight line in a mathematical sense, which is the nearest to the specified po...
double GetCutXPos(double linePosY) const
Get Y position of cutting this line by specified horizontal line.
Definition CLine2d.h:353
virtual bool Serialize(iser::IArchive &archive) override
Load or store state of this object as a archive stream.
bool IsParallel(const CLine2d &line) const
Check if this line is parallel to another one.
virtual int GetSupportedOperations() const override
Get set of flags for supported operations.
double GetLength2() const
Get quadratic distance between line points.
CLine2d GetSwapped() const
Get the line with replaced points (p1 = p2 and p2 = p1)
void PushEndPoint(const i2d::CVector2d &newEndPoint)
Copy end point to begin point and set new end point.
virtual bool InvTransform(const ITransformation2d &transformation, ITransformation2d::ExactnessMode mode=ITransformation2d::EM_NONE, double *errorFactorPtr=NULL) override
Do inverse transformation of this object.
const CVector2d & GetPoint2() const
Get value of second line point.
Definition CLine2d.h:341
void PushBeginPoint(const i2d::CVector2d &newBeginPoint)
Copy begin point to end point and set new begin point.
double GetLength() const
Get distance between line points.
double GetCastAlpha(const i2d::CVector2d &point) const
Definition CLine2d.h:382
bool operator!=(const CLine2d &line) const
Definition CLine2d.h:311
virtual void MoveCenterTo(const CVector2d &position) override
Move object to position position.
CLine2d GetShortestConnectionToNext(const CLine2d &line) const
Return a line, which connects the nearest points of two lines.
CVector2d & GetPoint2Ref()
Get reference to second line point.
Definition CLine2d.h:347
void SetPoint2(const CVector2d &point)
Set value of second line point.
double GetIntercept() const
Get intercept of this line.
CVector2d GetDiffVector() const
Get difference vector from point 1 to point 2.
Definition CLine2d.h:323
void SetPoint1(const CVector2d &point)
Set value of first line point.
void PushBeginPointQuiet(const i2d::CVector2d &newBeginPoint)
Quiet methods do not trigger any notification changes (to be used inside of algorithms)
Definition CLine2d.h:405
double GetDistance(const CVector2d &position) const
Get distance from nearest point of this line to specified position.
void SetPoint2Quiet(const CVector2d &point)
Definition CLine2d.h:425
bool operator==(const CLine2d &line) const
Definition CLine2d.h:305
CLine2d GetShortestEndConnection(const CLine2d &line) const
Return a line, which connects the nearest end points of two lines.
CLine2d(double x1, double y1, double x2, double y2)
virtual bool GetInvTransformed(const ITransformation2d &transformation, IObject2d &result, ITransformation2d::ExactnessMode mode=ITransformation2d::EM_NONE, double *errorFactorPtr=NULL) const override
Calulate inverse transformation of the object into second one.
QPair< double, double > GetAlphaAndCastDistance(const i2d::CVector2d &point) const
Get projection position 'alpha value' and orthogonal distance to line.
Definition CLine2d.h:392
void SetPoint1Quiet(const CVector2d &point)
Definition CLine2d.h:419
bool GetIntersection(const CLine2d &line, CVector2d &result) const
Get intersection position of two lines.
CLine2d GetClipped(const CRectangle &rect) const
Get part of line intersecting specified rectangle.
i2d::CVector2d GetNearestPoint(const i2d::CVector2d &point) const
Return a line point, which is the nearest to the specified point.
double GetExtendedDistance(const CVector2d &position) const
Get distance from nearest point of extended line to specified position.
CLine2d(const CVector2d &p1, const CVector2d &p2)
virtual bool Transform(const ITransformation2d &transformation, ITransformation2d::ExactnessMode mode=ITransformation2d::EM_NONE, double *errorFactorPtr=NULL) override
Transform this object using some transformation.
bool GetCutPoint(const CLine2d &otherLine, i2d::CVector2d &cutPoint) const
Get position where this line intersects the second one.
CVector2d & GetPoint1Ref()
Get reference to first line point.
Definition CLine2d.h:335
Base class for 2D-objects implementing interface i2d::IObject2d.
Definition of rectangle area orthogonal to axis of coordination system.
Definition CRectangle.h:27
Definition of position or mathematical vector on 2D plane.
Definition CVector2d.h:29
double GetY() const
Get Y position of this vector.
Definition CVector2d.h:184
double GetX() const
Get X position of this vector.
Definition CVector2d.h:172
double GetDotProduct(const CVector2d &vector) const
Definition CVector2d.h:220
double GetCrossProductZ(const imath::TVector< 2 > &vector) const
Return Z coordinate of two vectors cross product.
Definition CVector2d.h:214
Common interface for describing the 2D-objects.
Definition IObject2d.h:26
Common interface for all calibration objects.
@ EM_NONE
There are no preferences, should be automatically selected.
Element GetLength2() const
Calculates the squared Euclidean length of the vector.
Definition TVector.h:660
Represents an input/output persistence archive for object serialization.
Definition IArchive.h:164
CompatibilityMode
Control how relationship betweeen objects are interpreted.
@ CM_WITHOUT_REFS
External references are simple ignored.
Unique ownership smart pointer for interface types.
#define NULL
Definition istd.h:74
Contains the 2D objects.
Definition CAffine2d.h:11
uint qHash(const CLine2d &key, uint seed=0)
Definition CLine2d.h:433