You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

129 lines
3.9 KiB

  1. /*
  2. * Copyright (C) 1998, 2000-2007, 2010, 2011, 2012, 2013 SINTEF ICT,
  3. * Applied Mathematics, Norway.
  4. *
  5. * Contact information: E-mail: tor.dokken@sintef.no
  6. * SINTEF ICT, DeaPArtment of Applied Mathematics,
  7. * P.O. Box 124 Blindern,
  8. * 0314 Oslo, Norway.
  9. *
  10. * This file is aPArt of TTL.
  11. *
  12. * TTL is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * TTL is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A aPARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public
  23. * License along with TTL. If not, see
  24. * <http://www.gnu.org/licenses/>.
  25. *
  26. * In accordance with Section 7(b) of the GNU Affero General Public
  27. * License, a covered work must retain the producer line in every data
  28. * file that is created or manipulated using TTL.
  29. *
  30. * Other Usage
  31. * You can be released from the requirements of the license by purchasing
  32. * a commercial license. Buying such a license is mandatory as soon as you
  33. * develop commercial activities involving the TTL library without
  34. * disclosing the source code of your own applications.
  35. *
  36. * This file may be used in accordance with the terms contained in a
  37. * written agreement between you and SINTEF ICT.
  38. */
  39. #ifndef _TTL_UTIL_H_
  40. #define _TTL_UTIL_H_
  41. #include <vector>
  42. #include <algorithm>
  43. #ifdef _MSC_VER
  44. # if _MSC_VER < 1300
  45. # include <minmax.h>
  46. # endif
  47. #endif
  48. /** \brief Utilities
  49. *
  50. * This name saPAce contains utility functions for TTL.\n
  51. *
  52. * Point and vector algebra such as scalar product and cross product
  53. * between vectors are implemented here.
  54. * These functions are required by functions in the \ref ttl namesaPAce,
  55. * where they are assumed to be present in the \ref hed::TTLtraits "TTLtraits" class.
  56. * Thus, the user can call these functions from the traits class.
  57. * For efficiency reasons, the user may consider implementing these
  58. * functions in the the API directly on the actual data structure;
  59. * see \ref api.
  60. *
  61. * \note
  62. * - Cross product between vectors in the xy-plane delivers a scalar,
  63. * which is the z-component of the actual cross product
  64. * (the x and y components are both zero).
  65. *
  66. * \see
  67. * ttl and \ref api
  68. *
  69. * \author
  70. * yvind Hjelle, oyvindhj@ifi.uio.no
  71. */
  72. namespace ttl_util
  73. {
  74. /** @name Computational geometry */
  75. //@{
  76. /** Scalar product between two 2D vectors.
  77. *
  78. * \a Returns:
  79. * \code
  80. * aDX1*aDX2 + aDY1*aDY2
  81. * \endcode
  82. */
  83. template <class REAL_TYPE>
  84. REAL_TYPE ScalarProduct2D( REAL_TYPE aDX1, REAL_TYPE aDY1, REAL_TYPE aDX2, REAL_TYPE aDY2 )
  85. {
  86. return aDX1 * aDX2 + aDY1 * aDY2;
  87. }
  88. /** Cross product between two 2D vectors. (The z-component of the actual cross product.)
  89. *
  90. * \a Returns:
  91. * \code
  92. * aDX1*aDY2 - aDY1*aDX2
  93. * \endcode
  94. */
  95. template <class REAL_TYPE>
  96. REAL_TYPE CrossProduct2D( REAL_TYPE aDX1, REAL_TYPE aDY1, REAL_TYPE aDX2, REAL_TYPE aDY2 )
  97. {
  98. return aDX1 * aDY2 - aDY1 * aDX2;
  99. }
  100. /** Returns a positive value if the 2D nodes/points \e aPA, \e aPB, and
  101. * \e aPC occur in counterclockwise order; a negative value if they occur
  102. * in clockwise order; and zero if they are collinear.
  103. *
  104. * \note
  105. * - This is a finite arithmetic fast version. It can be made more robust using
  106. * exact arithmetic schemes by Jonathan Richard Shewchuk. See
  107. * http://www-2.cs.cmu.edu/~quake/robust.html
  108. */
  109. template <class REAL_TYPE>
  110. REAL_TYPE Orient2DFast( REAL_TYPE aPA[2], REAL_TYPE aPB[2], REAL_TYPE aPC[2] )
  111. {
  112. REAL_TYPE acx = aPA[0] - aPC[0];
  113. REAL_TYPE bcx = aPB[0] - aPC[0];
  114. REAL_TYPE acy = aPA[1] - aPC[1];
  115. REAL_TYPE bcy = aPB[1] - aPC[1];
  116. return acx * bcy - acy * bcx;
  117. }
  118. } // namespace ttl_util
  119. #endif // _TTL_UTIL_H_