ies_loader.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // +----------------------------------------------------------------------
  2. // | Project : ray.
  3. // | All rights reserved.
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2017.
  6. // +----------------------------------------------------------------------
  7. // | * Redistribution and use of this software in source and binary forms,
  8. // | with or without modification, are permitted provided that the following
  9. // | conditions are met:
  10. // |
  11. // | * Redistributions of source code must retain the above
  12. // | copyright notice, this list of conditions and the
  13. // | following disclaimer.
  14. // |
  15. // | * Redistributions in binary form must reproduce the above
  16. // | copyright notice, this list of conditions and the
  17. // | following disclaimer in the documentation and/or other
  18. // | materials provided with the distribution.
  19. // |
  20. // | * Neither the name of the ray team, nor the names of its
  21. // | contributors may be used to endorse or promote products
  22. // | derived from this software without specific prior
  23. // | written permission of the ray team.
  24. // |
  25. // | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. // | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. // | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  28. // | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  29. // | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  30. // | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  31. // | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  33. // | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  34. // | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. // | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. // +----------------------------------------------------------------------
  37. #ifndef _H_IES_LOADER_H_
  38. #define _H_IES_LOADER_H_
  39. #include <cstdint>
  40. #include <vector>
  41. #include <string>
  42. // https://knowledge.autodesk.com/support/3ds-max/learn-explore/caas/CloudHelp/cloudhelp/2016/ENU/3DSMax/files/GUID-EA0E3DE0-275C-42F7-83EC-429A37B2D501-htm.html
  43. class IESFileInfo
  44. {
  45. public:
  46. IESFileInfo();
  47. bool valid() const;
  48. const std::string& error() const;
  49. public:
  50. float totalLights;
  51. float totalLumens;
  52. float candalaMult;
  53. std::int32_t typeOfPhotometric;
  54. std::int32_t typeOfUnit;
  55. std::int32_t anglesNumH;
  56. std::int32_t anglesNumV;
  57. float width;
  58. float length;
  59. float height;
  60. float ballastFactor;
  61. float futureUse;
  62. float inputWatts;
  63. private:
  64. friend class IESLoadHelper;
  65. float _cachedIntegral;
  66. std::string _error;
  67. std::string _version;
  68. std::vector<float> _anglesH;
  69. std::vector<float> _anglesV;
  70. std::vector<float> _candalaValues;
  71. };
  72. class IESLoadHelper final
  73. {
  74. public:
  75. IESLoadHelper();
  76. ~IESLoadHelper();
  77. bool load(const std::string& data, IESFileInfo& info);
  78. bool load(const char* data, std::size_t dataLength, IESFileInfo& info);
  79. bool saveAs1D(const IESFileInfo& info, float* data, std::uint32_t width = 256, std::uint8_t channel = 3) noexcept;
  80. bool saveAs2D(const IESFileInfo& info, float* data, std::uint32_t width = 256, std::uint32_t height = 256, std::uint8_t channel = 3) noexcept;
  81. bool saveAsPreview(const IESFileInfo& info, std::uint8_t* data, std::uint32_t width = 64, std::uint32_t height = 64, std::uint8_t channel = 3) noexcept;
  82. private:
  83. float computeInvMax(const std::vector<float>& candalaValues) const;
  84. float computeFilterPos(float value, const std::vector<float>& angle) const;
  85. float interpolate1D(const IESFileInfo& info, float angle) const;
  86. float interpolate2D(const IESFileInfo& info, float angleV, float angleH) const;
  87. float interpolatePoint(const IESFileInfo& info, std::uint32_t x, std::uint32_t y) const;
  88. float interpolateBilinear(const IESFileInfo& info, float x, float y) const;
  89. private:
  90. static void skipSpaceAndLineEnd(const std::string& data, std::string& out, bool stopOnComma = false);
  91. static void getLineContent(const std::string& data, std::string& next, std::string& line, bool stopOnWhiteSpace, bool stopOnComma);
  92. static void getFloat(const std::string& data, std::string& next, float& ret, bool stopOnWhiteSpace = true, bool stopOnComma = false);
  93. static void getInt(const std::string& data, std::string& next, std::int32_t& ret, bool stopOnWhiteSpace = true, bool stopOnComma = false);
  94. };
  95. #endif