sh_utils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # Copyright 2021 The PlenOctree Authors.
  2. # Redistribution and use in source and binary forms, with or without
  3. # modification, are permitted provided that the following conditions are met:
  4. #
  5. # 1. Redistributions of source code must retain the above copyright notice,
  6. # this list of conditions and the following disclaimer.
  7. #
  8. # 2. Redistributions in binary form must reproduce the above copyright notice,
  9. # this list of conditions and the following disclaimer in the documentation
  10. # and/or other materials provided with the distribution.
  11. #
  12. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  13. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  14. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  15. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
  16. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  17. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  18. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  19. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  20. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  21. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  22. # POSSIBILITY OF SUCH DAMAGE.
  23. import torch
  24. C0 = 0.28209479177387814
  25. C1 = 0.4886025119029199
  26. C2 = [
  27. 1.0925484305920792,
  28. -1.0925484305920792,
  29. 0.31539156525252005,
  30. -1.0925484305920792,
  31. 0.5462742152960396
  32. ]
  33. C3 = [
  34. -0.5900435899266435,
  35. 2.890611442640554,
  36. -0.4570457994644658,
  37. 0.3731763325901154,
  38. -0.4570457994644658,
  39. 1.445305721320277,
  40. -0.5900435899266435
  41. ]
  42. C4 = [
  43. 2.5033429417967046,
  44. -1.7701307697799304,
  45. 0.9461746957575601,
  46. -0.6690465435572892,
  47. 0.10578554691520431,
  48. -0.6690465435572892,
  49. 0.47308734787878004,
  50. -1.7701307697799304,
  51. 0.6258357354491761,
  52. ]
  53. def eval_sh(deg, sh, dirs):
  54. """
  55. Evaluate spherical harmonics at unit directions
  56. using hardcoded SH polynomials.
  57. Works with torch/np/jnp.
  58. ... Can be 0 or more batch dimensions.
  59. Args:
  60. deg: int SH deg. Currently, 0-3 supported
  61. sh: jnp.ndarray SH coeffs [..., C, (deg + 1) ** 2]
  62. dirs: jnp.ndarray unit directions [..., 3]
  63. Returns:
  64. [..., C]
  65. """
  66. assert deg <= 4 and deg >= 0
  67. coeff = (deg + 1) ** 2
  68. assert sh.shape[-1] >= coeff
  69. result = C0 * sh[..., 0]
  70. if deg > 0:
  71. x, y, z = dirs[..., 0:1], dirs[..., 1:2], dirs[..., 2:3]
  72. result = (result -
  73. C1 * y * sh[..., 1] +
  74. C1 * z * sh[..., 2] -
  75. C1 * x * sh[..., 3])
  76. if deg > 1:
  77. xx, yy, zz = x * x, y * y, z * z
  78. xy, yz, xz = x * y, y * z, x * z
  79. result = (result +
  80. C2[0] * xy * sh[..., 4] +
  81. C2[1] * yz * sh[..., 5] +
  82. C2[2] * (2.0 * zz - xx - yy) * sh[..., 6] +
  83. C2[3] * xz * sh[..., 7] +
  84. C2[4] * (xx - yy) * sh[..., 8])
  85. if deg > 2:
  86. result = (result +
  87. C3[0] * y * (3 * xx - yy) * sh[..., 9] +
  88. C3[1] * xy * z * sh[..., 10] +
  89. C3[2] * y * (4 * zz - xx - yy)* sh[..., 11] +
  90. C3[3] * z * (2 * zz - 3 * xx - 3 * yy) * sh[..., 12] +
  91. C3[4] * x * (4 * zz - xx - yy) * sh[..., 13] +
  92. C3[5] * z * (xx - yy) * sh[..., 14] +
  93. C3[6] * x * (xx - 3 * yy) * sh[..., 15])
  94. if deg > 3:
  95. result = (result + C4[0] * xy * (xx - yy) * sh[..., 16] +
  96. C4[1] * yz * (3 * xx - yy) * sh[..., 17] +
  97. C4[2] * xy * (7 * zz - 1) * sh[..., 18] +
  98. C4[3] * yz * (7 * zz - 3) * sh[..., 19] +
  99. C4[4] * (zz * (35 * zz - 30) + 3) * sh[..., 20] +
  100. C4[5] * xz * (7 * zz - 3) * sh[..., 21] +
  101. C4[6] * (xx - yy) * (7 * zz - 1) * sh[..., 22] +
  102. C4[7] * xz * (xx - 3 * yy) * sh[..., 23] +
  103. C4[8] * (xx * (xx - 3 * yy) - yy * (3 * xx - yy)) * sh[..., 24])
  104. return result
  105. def RGB2SH(rgb):
  106. return (rgb - 0.5) / C0
  107. def SH2RGB(sh):
  108. return sh * C0 + 0.5