u8g_clip.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. u8g_clip.c
  3. procedures for clipping
  4. taken over from procs in u8g_pb.c
  5. Universal 8bit Graphics Library
  6. Copyright (c) 2012, olikraus@gmail.com
  7. All rights reserved.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. * Redistributions of source code must retain the above copyright notice, this list
  11. of conditions and the following disclaimer.
  12. * Redistributions in binary form must reproduce the above copyright notice, this
  13. list of conditions and the following disclaimer in the documentation and/or other
  14. materials provided with the distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  16. CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  17. INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  20. CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  21. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  22. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  25. STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  27. ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. Notes
  29. This is one of the most critical parts of u8glib. It must be fast, but still reliable.
  30. Based on the intersection program (see tools folder), there is minimized version of
  31. the condition for the intersaction test:
  32. minimized version
  33. ---1----0 1 b1 <= a2 && b1 > b2
  34. -----1--0 1 b2 >= a1 && b1 > b2
  35. ---1-1--- 1 b1 <= a2 && b2 >= a1
  36. It includes the assumption, that a1 <= a2 is always true (correct, because
  37. a1, a2 are the page dimensions.
  38. The direct implementation of the above result is done in:
  39. uint8_t u8g_is_intersection_boolean(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1)
  40. However, this is slower than a decision tree version:
  41. static uint8_t u8g_is_intersection_decision_tree(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1)
  42. Also suprising is, that the macro implementation is slower than the inlined version.
  43. The decision tree is based on the expansion of the truth table.
  44. */
  45. #include "u8g.h"
  46. #ifdef __GNUC__
  47. #define U8G_ALWAYS_INLINE __inline__ __attribute__((always_inline))
  48. #else
  49. #define U8G_ALWAYS_INLINE
  50. #endif
  51. /*
  52. intersection assumptions:
  53. a1 <= a2 is always true
  54. minimized version
  55. ---1----0 1 b1 <= a2 && b1 > b2
  56. -----1--0 1 b2 >= a1 && b1 > b2
  57. ---1-1--- 1 b1 <= a2 && b2 >= a1
  58. */
  59. #ifdef OLD_CODE_WHICH_IS_TOO_SLOW
  60. static uint8_t u8g_is_intersection_boolean(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1)
  61. {
  62. uint8_t c1, c2, c3, tmp;
  63. c1 = v0 <= a1;
  64. c2 = v1 >= a0;
  65. c3 = v0 > v1;
  66. tmp = c1;
  67. c1 &= c2;
  68. c2 &= c3;
  69. c3 &= tmp;
  70. c1 |= c2;
  71. c1 |= c3;
  72. return c1 & 1;
  73. }
  74. #endif
  75. #define U8G_IS_INTERSECTION_MACRO(a0,a1,v0,v1) ((uint8_t)( (v0) <= (a1) ) ? ( ( (v1) >= (a0) ) ? ( 1 ) : ( (v0) > (v1) ) ) : ( ( (v1) >= (a0) ) ? ( (v0) > (v1) ) : ( 0 ) ))
  76. //static uint8_t u8g_is_intersection_decision_tree(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1) U8G_ALWAYS_INLINE;
  77. static uint8_t U8G_ALWAYS_INLINE u8g_is_intersection_decision_tree(u8g_uint_t a0, u8g_uint_t a1, u8g_uint_t v0, u8g_uint_t v1)
  78. {
  79. /* surprisingly the macro leads to larger code */
  80. /* return U8G_IS_INTERSECTION_MACRO(a0,a1,v0,v1); */
  81. if ( v0 <= a1 )
  82. {
  83. if ( v1 >= a0 )
  84. {
  85. return 1;
  86. }
  87. else
  88. {
  89. if ( v0 > v1 )
  90. {
  91. return 1;
  92. }
  93. else
  94. {
  95. return 0;
  96. }
  97. }
  98. }
  99. else
  100. {
  101. if ( v1 >= a0 )
  102. {
  103. if ( v0 > v1 )
  104. {
  105. return 1;
  106. }
  107. else
  108. {
  109. return 0;
  110. }
  111. }
  112. else
  113. {
  114. return 0;
  115. }
  116. }
  117. }
  118. uint8_t u8g_IsBBXIntersection(u8g_t *u8g, u8g_uint_t x, u8g_uint_t y, u8g_uint_t w, u8g_uint_t h)
  119. {
  120. register u8g_uint_t tmp;
  121. tmp = y;
  122. tmp += h;
  123. tmp--;
  124. if ( u8g_is_intersection_decision_tree(u8g->current_page.y0, u8g->current_page.y1, y, tmp) == 0 )
  125. return 0;
  126. tmp = x;
  127. tmp += w;
  128. tmp--;
  129. return u8g_is_intersection_decision_tree(u8g->current_page.x0, u8g->current_page.x1, x, tmp);
  130. }