functions.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * functions.cpp
  3. *
  4. * Created on: Oct 6, 2011
  5. * Author: Gapchich Vladislav
  6. */
  7. #include "functions.h"
  8. #include <QString>
  9. #include <QChar>
  10. #include <QDomDocument>
  11. #include <QDomNode>
  12. #include <QDomText>
  13. #include <QPoint>
  14. #include <QLine>
  15. #include <qmath.h>
  16. #include <QDebug>
  17. //! Gets number from a string which is located between aFirstStr and aSecondStr
  18. /*!
  19. * \param[in] aString pointer to the source string containing the number we need
  20. * \param[in] aFirstStr a string which is located to the left side of the number
  21. * \param[in] aSecondStr a string which is located to the right side of the number
  22. * \param[in,out] anOkFlag a pointer to the bool flag indicating the conversation errors
  23. *
  24. * example: aString contains "0: Poly #0; LabelID: 1; points:..."
  25. * aFirstStr "LabelID: "
  26. * aSecondStr ";"
  27. * function returns 1
  28. */
  29. int
  30. getNumFromString(
  31. QString *aString,
  32. const QString &aFirstStr,
  33. const QString &aSecondStr,
  34. bool *anOkFlag
  35. )
  36. {
  37. int numPos = aString->indexOf(aFirstStr) + aFirstStr.size();
  38. if (numPos < 0) {
  39. *anOkFlag = 0;
  40. return -1;
  41. /* NOTREACHED */
  42. }
  43. int numLength = -1;
  44. for (int i = numPos; i < aString->size(); i++) {
  45. if (aSecondStr == aString->at(i)) {
  46. numLength = i - numPos;
  47. break;
  48. }
  49. }
  50. if (numLength <= 0) {
  51. *anOkFlag = 0;
  52. return -1;
  53. /* NOTREACHED */
  54. }
  55. QString numString = aString->mid(numPos, numLength);
  56. if (numString.isEmpty()) {
  57. *anOkFlag = 0;
  58. return -1;
  59. /* NOTREACHED */
  60. }
  61. bool ok = 0;
  62. int num = numString.toInt(&ok, 10);
  63. if (!ok) {
  64. *anOkFlag = 0;
  65. return -1;
  66. /* NOTREACHED */
  67. }
  68. *anOkFlag = 1;
  69. return num;
  70. }
  71. //! Adds given suffix to the file name
  72. /*
  73. * example: /home/user/file.dot -> /home/user/file_altered.dot
  74. */
  75. QString
  76. alterFileName(const QString &aFilename, const QString &aSuffix)
  77. {
  78. /* altering the name of a new file */
  79. QString newFileName = aFilename;
  80. int dotPos = newFileName.lastIndexOf('.');
  81. if (-1 == dotPos)
  82. dotPos = newFileName.size();
  83. else
  84. newFileName.remove(dotPos, newFileName.size() - dotPos);
  85. newFileName.insert(dotPos, aSuffix);
  86. return newFileName;
  87. }
  88. //! Removes the path from filename
  89. /*!
  90. * example: /home/user/file -> file
  91. */
  92. QString
  93. removePath(const QString &aFilename)
  94. {
  95. QString newFileName = aFilename;
  96. int slashPos = newFileName.lastIndexOf('/');
  97. newFileName.remove(0, slashPos + 1);
  98. return newFileName;
  99. }
  100. //! Gets path from filename
  101. /*!
  102. * example: /home/user/file.dot -> /home/user
  103. */
  104. QString
  105. getPathFromFilename(const QString &aFilename)
  106. {
  107. QString path = aFilename;
  108. int slashPos = path.lastIndexOf('/');
  109. path = path.mid(0, slashPos + 1);
  110. return path;
  111. }
  112. //! Calculates coefficients a,b,c (ax + by + c = 0) for the straight line
  113. /*!
  114. * \see ImageHolder::posInPolygon(QPoint *aPos,QPolygon *aPoly)
  115. * \param[in] p1 first point defining the line
  116. * \param[in] p2 second point defining the line
  117. * \param[out] a pointer to the coefficient
  118. * \param[out] b pointer to the coefficient
  119. * \param[out] c pointer to the coefficient
  120. */
  121. void
  122. calcLineCoeff(
  123. const QPoint &p1,
  124. const QPoint &p2,
  125. int *a,
  126. int *b,
  127. int *c
  128. )
  129. {
  130. *a = p1.y() - p2.y();
  131. *b = p2.x() - p1.x();
  132. *c = (p1.x() * p2.y()) - (p2.x() * p1.y());
  133. }
  134. //! Returns the distance between point and a line
  135. /*!
  136. * \see calcLineCoeff(
  137. const QPoint &p1,
  138. const QPoint &p2,
  139. int *a,
  140. int *b,
  141. int *c
  142. )
  143. \see ImageHolder::posInPolygon(QPoint *aPos,QPolygon *aPoly)
  144. * \param[in] aLine line
  145. * \param[in] aPoint point
  146. */
  147. int
  148. pointToLineDistance(
  149. const QLine &aLine,
  150. const QPoint &aPoint
  151. )
  152. {
  153. int a, b, c;
  154. int distance = 0;
  155. calcLineCoeff(
  156. aLine.p1(),
  157. aLine.p2(),
  158. &a,
  159. &b,
  160. &c
  161. );
  162. distance = qAbs((a * aPoint.x()) + (b * aPoint.y()) + c);
  163. return distance;
  164. }
  165. /*
  166. *
  167. */