CLine2d.h
Go to the documentation of this file.
1 #ifndef i2d_CLine2d_included
2 #define i2d_CLine2d_included
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 
13 namespace i2d
14 {
15 
16 
17 class CRectangle;
18 
19 
24 class CLine2d: public CObject2dBase
25 {
26 public:
27  static QByteArray GetTypeName();
28 
29  CLine2d();
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  bool GetIntersection(const CLine2d& line, CVector2d& result) const;
104 
112  bool GetExtendedIntersection(const CLine2d& line, CVector2d& result) const;
113 
117  double GetDistance(const CVector2d& position) const;
118 
122  double GetMaxDistance(const CVector2d& position) const;
123 
128  double GetExtendedDistance(const CVector2d& position) const;
129 
134  CVector2d GetPositionFromAlpha(double alpha) const;
135 
139  CVector2d GetDiffVector() const;
140 
144  double GetLength() const;
145 
149  double GetLength2() const;
150 
155  double GetDirectionAngle() const;
156 
160  CLine2d GetClipped(const CRectangle& rect) const;
161 
165  CLine2d GetSwapped() const;
166 
170  double GetCutXPos(double linePosY) const;
171 
175  double GetCutYPos(double linePosX) const;
176 
180  void PushBeginPoint(const i2d::CVector2d& newBeginPoint);
181 
185  void PushEndPoint(const i2d::CVector2d& newEndPoint);
186 
190  void PushBeginPointQuiet(const i2d::CVector2d& newBeginPoint);
191  void PushEndPointQuiet(const i2d::CVector2d& newEndPoint);
192 
193  void SetPoint1Quiet(const CVector2d& point);
194  void SetPoint2Quiet(const CVector2d& point);
195 
199  double GetCutAlpha(const CLine2d& line) const;
200 
201  double GetCastAlpha(const i2d::CVector2d& point) const;
202 
208  QPair<double, double> GetAlphaAndCastDistance(const i2d::CVector2d& point) const;
209 
214  bool GetCutPoint(const CLine2d& otherLine, i2d::CVector2d& cutPoint) const;
215 
219  i2d::CVector2d GetNearestPoint(const i2d::CVector2d& point) const;
220 
226 
230  CLine2d GetShortestEndConnection(const CLine2d& line) const;
231 
237  CLine2d GetShortestConnectionToNext(const CLine2d& line) const;
238 
242  CLine2d GetShortestConnection(const i2d::CVector2d& point) const;
243 
247  CLine2d GetShortestConnection(const CLine2d& line) const;
248 
252  bool ApproxFromPoints(const QSet<i2d::CVector2d>& points);
253 
254  // reimplemented (i2d::IObject2d)
255  virtual CVector2d GetCenter() const;
256  virtual void MoveCenterTo(const CVector2d& position);
257  virtual CRectangle GetBoundingBox() const;
258  virtual bool Transform(
259  const ITransformation2d& transformation,
261  double* errorFactorPtr = NULL);
262  virtual bool InvTransform(
263  const ITransformation2d& transformation,
265  double* errorFactorPtr = NULL);
266  virtual bool GetTransformed(
267  const ITransformation2d& transformation,
268  IObject2d& result,
270  double* errorFactorPtr = NULL) const;
271  virtual bool GetInvTransformed(
272  const ITransformation2d& transformation,
273  IObject2d& result,
275  double* errorFactorPtr = NULL) const;
276 
277  // reimplemented (iser::IObject)
278  virtual QByteArray GetFactoryId() const;
279 
280  // reimplemented (iser::ISerializable)
281  virtual bool Serialize(iser::IArchive& archive);
282 
283  // reimplemented (istd::IChangeable)
284  virtual int GetSupportedOperations() const;
285  virtual bool CopyFrom(const IChangeable& object, CompatibilityMode mode = CM_WITHOUT_REFS);
287 
288 private:
289  CVector2d m_point1;
290  CVector2d m_point2;
291 };
292 
293 
294 // public inline methods
295 
296 inline bool CLine2d::operator == (const CLine2d& line) const
297 {
298  return ((m_point1 == line.m_point1) && (m_point2 == line.m_point2));
299 }
300 
301 
302 inline bool CLine2d::operator != (const CLine2d& line) const
303 {
304  return !operator == (line);
305 }
306 
307 
308 inline bool CLine2d::IsNull() const
309 {
310  return (m_point1 == m_point2);
311 }
312 
313 
315 {
316  return m_point2 - m_point1;
317 }
318 
319 
320 inline const CVector2d& CLine2d::GetPoint1() const
321 {
322  return m_point1;
323 }
324 
325 
327 {
328  return m_point1;
329 }
330 
331 
332 inline const CVector2d& CLine2d::GetPoint2() const
333 {
334  return m_point2;
335 }
336 
337 
339 {
340  return m_point2;
341 }
342 
343 
344 inline double CLine2d::GetCutXPos(double linePosY) const
345 {
346  CVector2d diff = GetDiffVector();
347 
348  return (linePosY - m_point1.GetY()) * diff.GetX() / diff.GetY() + m_point1.GetX();
349 }
350 
351 
352 inline double CLine2d::GetCutYPos(double linePosX) const
353 {
354  CVector2d diff = GetDiffVector();
355 
356  return (linePosX - m_point1.GetX()) * diff.GetY() / diff.GetX() + m_point1.GetY();
357 }
358 
359 
360 inline double CLine2d::GetCutAlpha(const CLine2d& line) const
361 {
362  const i2d::CVector2d& delta = GetDiffVector();
363  const i2d::CVector2d& lineDelta = line.GetDiffVector();
364  const i2d::CVector2d& beginPoint = GetPoint1();
365  const i2d::CVector2d& lineBeginPoint = line.GetPoint1();
366 
367  double crossProductZ = lineDelta.GetCrossProductZ(delta);
368 
369  return (beginPoint.GetCrossProductZ(delta) + delta.GetCrossProductZ(lineBeginPoint)) / crossProductZ;
370 }
371 
372 
373 inline double CLine2d::GetCastAlpha(const i2d::CVector2d& point) const
374 {
375  i2d::CVector2d delta = GetDiffVector();
376  i2d::CVector2d deltaToPoint = point - m_point1;
377 
378  double dotProduct = delta.GetDotProduct(deltaToPoint);
379  return dotProduct / delta.GetLength2();
380 }
381 
382 
383 inline QPair<double, double> CLine2d::GetAlphaAndCastDistance(const i2d::CVector2d& point) const
384 {
385  i2d::CVector2d delta = m_point2 - m_point1;
386  i2d::CVector2d deltaToPoint = point - m_point1;
387 
388  double dotProduct = delta.GetDotProduct(deltaToPoint);
389  double field = delta.GetCrossProductZ(deltaToPoint);
390  double deltaLength2 = delta.GetLength2();
391 
392  return QPair<double, double>(dotProduct / deltaLength2, field / ::sqrt(deltaLength2));
393 }
394 
395 
396 inline void CLine2d::PushBeginPointQuiet(const i2d::CVector2d& newBeginPoint)
397 {
398  m_point2 = m_point1;
399  m_point1 = newBeginPoint;
400 }
401 
402 
403 inline void CLine2d::PushEndPointQuiet(const i2d::CVector2d& newEndPoint)
404 {
405  m_point1 = m_point2;
406  m_point2 = newEndPoint;
407 }
408 
409 
410 inline void CLine2d::SetPoint1Quiet(const CVector2d& point)
411 {
412  m_point1 = point;
413 }
414 
415 
416 inline void CLine2d::SetPoint2Quiet(const CVector2d& point)
417 {
418  m_point2 = point;
419 }
420 
421 
422 // related global functions
423 
424 inline uint qHash(const CLine2d& key, uint seed = 0)
425 {
426  return qHash(key.GetPoint1(), seed) ^ qHash(key.GetPoint2(), seed);
427 }
428 
429 
430 } // namespace i2d
431 
432 
433 #endif // !i2d_CLine2d_included
434 
435 
CVector2d & GetPoint2Ref()
Get reference to second line point.
Definition: CLine2d.h:338
virtual CVector2d GetCenter() const
Returns center of this 2D-object.
virtual bool CopyFrom(const IChangeable &object, CompatibilityMode mode=CM_WITHOUT_REFS)
bool IsNull() const
Returns true, if the line has a length equal 0.
Definition: CLine2d.h:308
double GetDistance(const CVector2d &position) const
Get distance from nearest point of this line to specified position.
void PushBeginPoint(const i2d::CVector2d &newBeginPoint)
Copy begin point to end point and set new begin point.
double GetIntercept() const
Get intercept of this line.
i2d::CVector2d GetNearestPoint(const i2d::CVector2d &point) const
Return a line point, which is the nearest to the specified point.
External references are simple ignored.
Definition: IChangeable.h:197
CompatibilityMode
Control how relationship betweeen objects are interpreted.
Definition: IChangeable.h:186
virtual bool InvTransform(const ITransformation2d &transformation, ITransformation2d::ExactnessMode mode=ITransformation2d::EM_NONE, double *errorFactorPtr=NULL)
Do inverse transformation of this object.
virtual bool Serialize(iser::IArchive &archive)
Load or store state of this object as a archive stream.
double GetExtendedDistance(const CVector2d &position) const
Get distance from nearest point of extended line to specified position.
virtual void MoveCenterTo(const CVector2d &position)
Move object to position position.
CVector2d GetPositionFromAlpha(double alpha) const
Get position of point using normed 'alpha' value.
CVector2d & GetPoint1Ref()
Get reference to first line point.
Definition: CLine2d.h:326
Element GetLength2() const
Return euclidian length square.
Definition: TVector.h:351
virtual int GetSupportedOperations() const
Get set of flags for supported operations.
double GetCrossProductZ(const imath::TVector< 2 > &vector) const
Return Z coordinate of two vectors cross product.
Definition: CVector2d.h:214
bool GetExtendedIntersection(const CLine2d &line, CVector2d &result) const
Get intersection position of extended lines.
const CVector2d & GetPoint2() const
Get value of second line point.
Definition: CLine2d.h:332
Definition of a line in 2D-space.
Definition: CLine2d.h:24
void SetPoint2Quiet(const CVector2d &point)
Definition: CLine2d.h:416
Common interface for all calibration objects.
double GetY() const
Get Y position of this vector.
Definition: CVector2d.h:184
double GetCastAlpha(const i2d::CVector2d &point) const
Definition: CLine2d.h:373
CLine2d GetShortestConnection(const i2d::CVector2d &point) const
Return a line, which connects the nearest point of line and specified point.
virtual istd::IChangeable * CloneMe(CompatibilityMode mode=CM_WITHOUT_REFS) const
Make a copy of this object.
double GetLength2() const
Get quadratic distance between line points.
void PushEndPointQuiet(const i2d::CVector2d &newEndPoint)
Definition: CLine2d.h:403
CLine2d GetSwapped() const
Get the line with replaced points (p1 = p2 and p2 = p1)
virtual CRectangle GetBoundingBox() const
Get bounding box of this shape.
There are no preferences, should be automatically selected.
bool operator!=(const CLine2d &line) const
Definition: CLine2d.h:302
const CVector2d & GetPoint1() const
Get value of first line point.
Definition: CLine2d.h:320
bool IsParallel(const CLine2d &line) const
Check if this line is parallel to another one.
CLine2d GetShortestConnectionToNext(const CLine2d &line) const
Return a line, which connects the nearest points of two lines.
uint qHash(const CLine2d &key, uint seed=0)
Definition: CLine2d.h:424
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...
Represent input/output persistence archive.
Definition: IArchive.h:30
Common interface for data model objects, which can be changed.
Definition: IChangeable.h:24
Definition of rectangle area orthogonal to axis of coordination system.
Definition: CRectangle.h:26
double GetMaxDistance(const CVector2d &position) const
Get maximal distance from points of this line to specified position.
double GetSlope() const
Get slope of this line.
virtual bool Transform(const ITransformation2d &transformation, ITransformation2d::ExactnessMode mode=ITransformation2d::EM_NONE, double *errorFactorPtr=NULL)
Transform this object using some transformation.
Definition of position or mathematical vector on 2D plane.
Definition: CVector2d.h:28
bool IsIntersectedBy(const CLine2d &line) const
Check if two lines intersect.
bool ApproxFromPoints(const QSet< i2d::CVector2d > &points)
Calculate this line as approximation of set of points.
Base class for 2D-objects implementing interface i2d::IObject2d.
Definition: CObject2dBase.h:18
void SetPoint1Quiet(const CVector2d &point)
Definition: CLine2d.h:410
void SetPoint1(const CVector2d &point)
Set value of first line point.
double GetCutYPos(double linePosX) const
Get X position of cutting this line by specified vertical line.
Definition: CLine2d.h:352
#define NULL
Definition: istd.h:64
double GetCutXPos(double linePosY) const
Get Y position of cutting this line by specified horizontal line.
Definition: CLine2d.h:344
double GetDirectionAngle() const
Get direction angle of this line.
bool GetCutPoint(const CLine2d &otherLine, i2d::CVector2d &cutPoint) const
Get position where this line intersects the second one.
CVector2d GetDiffVector() const
Get difference vector from point 1 to point 2.
Definition: CLine2d.h:314
double GetDotProduct(const CVector2d &vector) const
Definition: CVector2d.h:220
bool GetIntersection(const CLine2d &line, CVector2d &result) const
Get intersection position of two lines.
virtual bool GetTransformed(const ITransformation2d &transformation, IObject2d &result, ITransformation2d::ExactnessMode mode=ITransformation2d::EM_NONE, double *errorFactorPtr=NULL) const
Calulate transformation of the object into second one.
double GetLength() const
Get distance between line points.
Common interface for describing the 2D-objects.
Definition: IObject2d.h:22
double GetCutAlpha(const CLine2d &line) const
Get a proportion of lines cut point to line length (called also 'alpha value').
Definition: CLine2d.h:360
virtual QByteArray GetFactoryId() const
void PushBeginPointQuiet(const i2d::CVector2d &newBeginPoint)
Quiet methods do not trigger any notification changes (to be used inside of algorithms) ...
Definition: CLine2d.h:396
CLine2d GetShortestEndConnection(const CLine2d &line) const
Return a line, which connects the nearest end points of two lines.
virtual bool GetInvTransformed(const ITransformation2d &transformation, IObject2d &result, ITransformation2d::ExactnessMode mode=ITransformation2d::EM_NONE, double *errorFactorPtr=NULL) const
Calulate inverse transformation of the object into second one.
double GetX() const
Get X position of this vector.
Definition: CVector2d.h:172
static QByteArray GetTypeName()
CLine2d GetClipped(const CRectangle &rect) const
Get part of line intersecting specified rectangle.
bool operator==(const CLine2d &line) const
Definition: CLine2d.h:296
void SetPoint2(const CVector2d &point)
Set value of second line point.
void PushEndPoint(const i2d::CVector2d &newEndPoint)
Copy end point to begin point and set new end point.
QPair< double, double > GetAlphaAndCastDistance(const i2d::CVector2d &point) const
Get projection position 'alpha value' and orthogonal distance to line.
Definition: CLine2d.h:383

© 2007-2017 Witold Gantzke and Kirill Lepskiy