ImfEnvmap.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. ///////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 2004, Industrial Light & Magic, a division of Lucas
  4. // Digital Ltd. LLC
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions are
  10. // met:
  11. // * Redistributions of source code must retain the above copyright
  12. // notice, this list of conditions and the following disclaimer.
  13. // * Redistributions in binary form must reproduce the above
  14. // copyright notice, this list of conditions and the following disclaimer
  15. // in the documentation and/or other materials provided with the
  16. // distribution.
  17. // * Neither the name of Industrial Light & Magic nor the names of
  18. // its contributors may be used to endorse or promote products derived
  19. // from this software without specific prior written permission.
  20. //
  21. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. //
  33. ///////////////////////////////////////////////////////////////////////////
  34. //-----------------------------------------------------------------------------
  35. //
  36. // Environment maps
  37. //
  38. //-----------------------------------------------------------------------------
  39. #include "ImfEnvmap.h"
  40. #include "ImathFun.h"
  41. #include "ImfNamespace.h"
  42. #include <algorithm>
  43. #include <math.h>
  44. using namespace std;
  45. using namespace IMATH_NAMESPACE;
  46. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_ENTER
  47. namespace LatLongMap {
  48. V2f
  49. latLong (const V3f &dir)
  50. {
  51. float r = sqrt (dir.z * dir.z + dir.x * dir.x);
  52. float latitude = (r < abs (dir.y))?
  53. acos (r / dir.length()) * sign (dir.y):
  54. asin (dir.y / dir.length());
  55. float longitude = (dir.z == 0 && dir.x == 0)? 0: atan2 (dir.x, dir.z);
  56. return V2f (latitude, longitude);
  57. }
  58. V2f
  59. latLong (const Box2i &dataWindow, const V2f &pixelPosition)
  60. {
  61. float latitude, longitude;
  62. if (dataWindow.max.y > dataWindow.min.y)
  63. {
  64. latitude = -1 * float(M_PI) *
  65. ((pixelPosition.y - dataWindow.min.y) /
  66. (dataWindow.max.y - dataWindow.min.y) - 0.5f);
  67. }
  68. else
  69. {
  70. latitude = 0;
  71. }
  72. if (dataWindow.max.x > dataWindow.min.x)
  73. {
  74. longitude = -2 * float(M_PI) *
  75. ((pixelPosition.x - dataWindow.min.x) /
  76. (dataWindow.max.x - dataWindow.min.x) - 0.5f);
  77. }
  78. else
  79. {
  80. longitude = 0;
  81. }
  82. return V2f (latitude, longitude);
  83. }
  84. V2f
  85. pixelPosition (const Box2i &dataWindow, const V2f &latLong)
  86. {
  87. float x = latLong.y / (-2 * float(M_PI)) + 0.5f;
  88. float y = latLong.x / (-1 * float(M_PI)) + 0.5f;
  89. return V2f (x * (dataWindow.max.x - dataWindow.min.x) + dataWindow.min.x,
  90. y * (dataWindow.max.y - dataWindow.min.y) + dataWindow.min.y);
  91. }
  92. V2f
  93. pixelPosition (const Box2i &dataWindow, const V3f &direction)
  94. {
  95. return pixelPosition (dataWindow, latLong (direction));
  96. }
  97. V3f
  98. direction (const Box2i &dataWindow, const V2f &pixelPosition)
  99. {
  100. V2f ll = latLong (dataWindow, pixelPosition);
  101. return V3f (sin (ll.y) * cos (ll.x),
  102. sin (ll.x),
  103. cos (ll.y) * cos (ll.x));
  104. }
  105. } // namespace LatLongMap
  106. namespace CubeMap {
  107. int
  108. sizeOfFace (const Box2i &dataWindow)
  109. {
  110. return min ((dataWindow.max.x - dataWindow.min.x + 1),
  111. (dataWindow.max.y - dataWindow.min.y + 1) / 6);
  112. }
  113. Box2i
  114. dataWindowForFace (CubeMapFace face, const Box2i &dataWindow)
  115. {
  116. int sof = sizeOfFace (dataWindow);
  117. Box2i dwf;
  118. dwf.min.x = 0;
  119. dwf.min.y = int (face) * sof;
  120. dwf.max.x = dwf.min.x + sof - 1;
  121. dwf.max.y = dwf.min.y + sof - 1;
  122. return dwf;
  123. }
  124. V2f
  125. pixelPosition (CubeMapFace face, const Box2i &dataWindow, V2f positionInFace)
  126. {
  127. Box2i dwf = dataWindowForFace (face, dataWindow);
  128. V2f pos (0, 0);
  129. switch (face)
  130. {
  131. case CUBEFACE_POS_X:
  132. pos.x = dwf.min.x + positionInFace.y;
  133. pos.y = dwf.max.y - positionInFace.x;
  134. break;
  135. case CUBEFACE_NEG_X:
  136. pos.x = dwf.max.x - positionInFace.y;
  137. pos.y = dwf.max.y - positionInFace.x;
  138. break;
  139. case CUBEFACE_POS_Y:
  140. pos.x = dwf.min.x + positionInFace.x;
  141. pos.y = dwf.max.y - positionInFace.y;
  142. break;
  143. case CUBEFACE_NEG_Y:
  144. pos.x = dwf.min.x + positionInFace.x;
  145. pos.y = dwf.min.y + positionInFace.y;
  146. break;
  147. case CUBEFACE_POS_Z:
  148. pos.x = dwf.max.x - positionInFace.x;
  149. pos.y = dwf.max.y - positionInFace.y;
  150. break;
  151. case CUBEFACE_NEG_Z:
  152. pos.x = dwf.min.x + positionInFace.x;
  153. pos.y = dwf.max.y - positionInFace.y;
  154. break;
  155. }
  156. return pos;
  157. }
  158. void
  159. faceAndPixelPosition (const V3f &direction,
  160. const Box2i &dataWindow,
  161. CubeMapFace &face,
  162. V2f &pif)
  163. {
  164. int sof = sizeOfFace (dataWindow);
  165. float absx = abs (direction.x);
  166. float absy = abs (direction.y);
  167. float absz = abs (direction.z);
  168. if (absx >= absy && absx >= absz)
  169. {
  170. if (absx == 0)
  171. {
  172. //
  173. // Special case - direction is (0, 0, 0)
  174. //
  175. face = CUBEFACE_POS_X;
  176. pif = V2f (0, 0);
  177. return;
  178. }
  179. pif.x = (direction.y / absx + 1) / 2 * (sof - 1);
  180. pif.y = (direction.z / absx + 1) / 2 * (sof - 1);
  181. if (direction.x > 0)
  182. face = CUBEFACE_POS_X;
  183. else
  184. face = CUBEFACE_NEG_X;
  185. }
  186. else if (absy >= absz)
  187. {
  188. pif.x = (direction.x / absy + 1) / 2 * (sof - 1);
  189. pif.y = (direction.z / absy + 1) / 2 * (sof - 1);
  190. if (direction.y > 0)
  191. face = CUBEFACE_POS_Y;
  192. else
  193. face = CUBEFACE_NEG_Y;
  194. }
  195. else
  196. {
  197. pif.x = (direction.x / absz + 1) / 2 * (sof - 1);
  198. pif.y = (direction.y / absz + 1) / 2 * (sof - 1);
  199. if (direction.z > 0)
  200. face = CUBEFACE_POS_Z;
  201. else
  202. face = CUBEFACE_NEG_Z;
  203. }
  204. }
  205. V3f
  206. direction (CubeMapFace face, const Box2i &dataWindow, const V2f &positionInFace)
  207. {
  208. int sof = sizeOfFace (dataWindow);
  209. V2f pos;
  210. if (sof > 1)
  211. {
  212. pos = V2f (positionInFace.x / (sof - 1) * 2 - 1,
  213. positionInFace.y / (sof - 1) * 2 - 1);
  214. }
  215. else
  216. {
  217. pos = V2f (0, 0);
  218. }
  219. V3f dir (1, 0, 0);
  220. switch (face)
  221. {
  222. case CUBEFACE_POS_X:
  223. dir.x = 1;
  224. dir.y = pos.x;
  225. dir.z = pos.y;
  226. break;
  227. case CUBEFACE_NEG_X:
  228. dir.x = -1;
  229. dir.y = pos.x;
  230. dir.z = pos.y;
  231. break;
  232. case CUBEFACE_POS_Y:
  233. dir.x = pos.x;
  234. dir.y = 1;
  235. dir.z = pos.y;
  236. break;
  237. case CUBEFACE_NEG_Y:
  238. dir.x = pos.x;
  239. dir.y = -1;
  240. dir.z = pos.y;
  241. break;
  242. case CUBEFACE_POS_Z:
  243. dir.x = pos.x;
  244. dir.y = pos.y;
  245. dir.z = 1;
  246. break;
  247. case CUBEFACE_NEG_Z:
  248. dir.x = pos.x;
  249. dir.y = pos.y;
  250. dir.z = -1;
  251. break;
  252. }
  253. return dir;
  254. }
  255. } // namespace CubeMap
  256. OPENEXR_IMF_INTERNAL_NAMESPACE_SOURCE_EXIT