/src/FreeImage/Source/OpenEXR/IlmImf/ImfEnvmap.cpp

https://bitbucket.org/cabalistic/ogredeps/ · C++ · 328 lines · 214 code · 73 blank · 41 comment · 17 complexity · 01668ae2874f065128ef727bdfd507d4 MD5 · raw file

  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 <algorithm>
  42. #include <math.h>
  43. using namespace std;
  44. using namespace Imath;
  45. namespace Imf {
  46. namespace LatLongMap {
  47. V2f
  48. latLong (const V3f &dir)
  49. {
  50. float r = sqrt (dir.z * dir.z + dir.x * dir.x);
  51. float latitude = (r < abs (dir.y))?
  52. acos (r / dir.length()) * sign (dir.y):
  53. asin (dir.y / dir.length());
  54. float longitude = (dir.z == 0 && dir.x == 0)? 0: atan2 (dir.x, dir.z);
  55. return V2f (latitude, longitude);
  56. }
  57. V2f
  58. latLong (const Box2i &dataWindow, const V2f &pixelPosition)
  59. {
  60. float latitude, longitude;
  61. if (dataWindow.max.y > dataWindow.min.y)
  62. {
  63. latitude = -M_PI *
  64. ((pixelPosition.y - dataWindow.min.y) /
  65. (dataWindow.max.y - dataWindow.min.y) - 0.5f);
  66. }
  67. else
  68. {
  69. latitude = 0;
  70. }
  71. if (dataWindow.max.x > dataWindow.min.x)
  72. {
  73. longitude = -2 * M_PI *
  74. ((pixelPosition.x - dataWindow.min.x) /
  75. (dataWindow.max.x - dataWindow.min.x) - 0.5f);
  76. }
  77. else
  78. {
  79. longitude = 0;
  80. }
  81. return V2f (latitude, longitude);
  82. }
  83. V2f
  84. pixelPosition (const Box2i &dataWindow, const V2f &latLong)
  85. {
  86. float x = latLong.y / (-2 * M_PI) + 0.5f;
  87. float y = latLong.x / -M_PI + 0.5f;
  88. return V2f (x * (dataWindow.max.x - dataWindow.min.x) + dataWindow.min.x,
  89. y * (dataWindow.max.y - dataWindow.min.y) + dataWindow.min.y);
  90. }
  91. V2f
  92. pixelPosition (const Box2i &dataWindow, const V3f &direction)
  93. {
  94. return pixelPosition (dataWindow, latLong (direction));
  95. }
  96. V3f
  97. direction (const Box2i &dataWindow, const V2f &pixelPosition)
  98. {
  99. V2f ll = latLong (dataWindow, pixelPosition);
  100. return V3f (sin (ll.y) * cos (ll.x),
  101. sin (ll.x),
  102. cos (ll.y) * cos (ll.x));
  103. }
  104. } // namespace LatLongMap
  105. namespace CubeMap {
  106. int
  107. sizeOfFace (const Box2i &dataWindow)
  108. {
  109. return min ((dataWindow.max.x - dataWindow.min.x + 1),
  110. (dataWindow.max.y - dataWindow.min.y + 1) / 6);
  111. }
  112. Box2i
  113. dataWindowForFace (CubeMapFace face, const Box2i &dataWindow)
  114. {
  115. int sof = sizeOfFace (dataWindow);
  116. Box2i dwf;
  117. dwf.min.x = 0;
  118. dwf.min.y = int (face) * sof;
  119. dwf.max.x = dwf.min.x + sof - 1;
  120. dwf.max.y = dwf.min.y + sof - 1;
  121. return dwf;
  122. }
  123. V2f
  124. pixelPosition (CubeMapFace face, const Box2i &dataWindow, V2f positionInFace)
  125. {
  126. Box2i dwf = dataWindowForFace (face, dataWindow);
  127. V2f pos (0, 0);
  128. switch (face)
  129. {
  130. case CUBEFACE_POS_X:
  131. pos.x = dwf.min.x + positionInFace.y;
  132. pos.y = dwf.max.y - positionInFace.x;
  133. break;
  134. case CUBEFACE_NEG_X:
  135. pos.x = dwf.max.x - positionInFace.y;
  136. pos.y = dwf.max.y - positionInFace.x;
  137. break;
  138. case CUBEFACE_POS_Y:
  139. pos.x = dwf.min.x + positionInFace.x;
  140. pos.y = dwf.max.y - positionInFace.y;
  141. break;
  142. case CUBEFACE_NEG_Y:
  143. pos.x = dwf.min.x + positionInFace.x;
  144. pos.y = dwf.min.y + positionInFace.y;
  145. break;
  146. case CUBEFACE_POS_Z:
  147. pos.x = dwf.max.x - positionInFace.x;
  148. pos.y = dwf.max.y - positionInFace.y;
  149. break;
  150. case CUBEFACE_NEG_Z:
  151. pos.x = dwf.min.x + positionInFace.x;
  152. pos.y = dwf.max.y - positionInFace.y;
  153. break;
  154. }
  155. return pos;
  156. }
  157. void
  158. faceAndPixelPosition (const V3f &direction,
  159. const Box2i &dataWindow,
  160. CubeMapFace &face,
  161. V2f &pif)
  162. {
  163. int sof = sizeOfFace (dataWindow);
  164. float absx = abs (direction.x);
  165. float absy = abs (direction.y);
  166. float absz = abs (direction.z);
  167. if (absx >= absy && absx >= absz)
  168. {
  169. if (absx == 0)
  170. {
  171. //
  172. // Special case - direction is (0, 0, 0)
  173. //
  174. face = CUBEFACE_POS_X;
  175. pif = V2f (0, 0);
  176. return;
  177. }
  178. pif.x = (direction.y / absx + 1) / 2 * (sof - 1);
  179. pif.y = (direction.z / absx + 1) / 2 * (sof - 1);
  180. if (direction.x > 0)
  181. face = CUBEFACE_POS_X;
  182. else
  183. face = CUBEFACE_NEG_X;
  184. }
  185. else if (absy >= absz)
  186. {
  187. pif.x = (direction.x / absy + 1) / 2 * (sof - 1);
  188. pif.y = (direction.z / absy + 1) / 2 * (sof - 1);
  189. if (direction.y > 0)
  190. face = CUBEFACE_POS_Y;
  191. else
  192. face = CUBEFACE_NEG_Y;
  193. }
  194. else
  195. {
  196. pif.x = (direction.x / absz + 1) / 2 * (sof - 1);
  197. pif.y = (direction.y / absz + 1) / 2 * (sof - 1);
  198. if (direction.z > 0)
  199. face = CUBEFACE_POS_Z;
  200. else
  201. face = CUBEFACE_NEG_Z;
  202. }
  203. }
  204. V3f
  205. direction (CubeMapFace face, const Box2i &dataWindow, const V2f &positionInFace)
  206. {
  207. int sof = sizeOfFace (dataWindow);
  208. V2f pos;
  209. if (sof > 1)
  210. {
  211. pos = V2f (positionInFace.x / (sof - 1) * 2 - 1,
  212. positionInFace.y / (sof - 1) * 2 - 1);
  213. }
  214. else
  215. {
  216. pos = V2f (0, 0);
  217. }
  218. V3f dir (1, 0, 0);
  219. switch (face)
  220. {
  221. case CUBEFACE_POS_X:
  222. dir.x = 1;
  223. dir.y = pos.x;
  224. dir.z = pos.y;
  225. break;
  226. case CUBEFACE_NEG_X:
  227. dir.x = -1;
  228. dir.y = pos.x;
  229. dir.z = pos.y;
  230. break;
  231. case CUBEFACE_POS_Y:
  232. dir.x = pos.x;
  233. dir.y = 1;
  234. dir.z = pos.y;
  235. break;
  236. case CUBEFACE_NEG_Y:
  237. dir.x = pos.x;
  238. dir.y = -1;
  239. dir.z = pos.y;
  240. break;
  241. case CUBEFACE_POS_Z:
  242. dir.x = pos.x;
  243. dir.y = pos.y;
  244. dir.z = 1;
  245. break;
  246. case CUBEFACE_NEG_Z:
  247. dir.x = pos.x;
  248. dir.y = pos.y;
  249. dir.z = -1;
  250. break;
  251. }
  252. return dir;
  253. }
  254. } // namespace CubeMap
  255. } // namespace Imf