io/ies.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-License-Identifier: BSD-3-Clause | ||
| 2 | /// @file ies.cpp | ||
| 3 | /// @brief IES LM-63 parsing, sampling and tilt. | ||
| 4 | #include "ernie/io/ies.h" | ||
| 5 | |||
| 6 | #include "ernie/util/file.h" | ||
| 7 | #include "ernie/util/math.h" | ||
| 8 | #include "ernie/util/text.h" | ||
| 9 | |||
| 10 | #include <algorithm> | ||
| 11 | #include <cmath> | ||
| 12 | #include <functional> | ||
| 13 | #include <limits> | ||
| 14 | #include <stdexcept> | ||
| 15 | #include <string> | ||
| 16 | |||
| 17 | namespace ernie { | ||
| 18 | |||
| 19 | namespace { | ||
| 20 | |||
| 21 | constexpr double kAngleSlack = 1e-9; | ||
| 22 | |||
| 23 | 56 | bool Near(double a, double b) { | |
| 24 | 56 | return std::abs(a - b) < 1e-3; | |
| 25 | } | ||
| 26 | |||
| 27 | /// Whether `x` lies within the angles `angles` span. | ||
| 28 | 259250 | bool Within(const std::vector<double>& angles, double x) { | |
| 29 | 259250 | return x >= angles.front() - kAngleSlack && x <= angles.back() + kAngleSlack; | |
| 30 | } | ||
| 31 | |||
| 32 | /// Whether `angles` strictly increase, within [`low`, `high`]. | ||
| 33 | 20 | bool Increasing(const std::vector<double>& angles, double low, double high) { | |
| 34 | 20 | return std::adjacent_find(angles.begin(), angles.end(), std::greater_equal<>()) == | |
| 35 | 20 | angles.end() && | |
| 36 | 20 | angles.front() >= low - kAngleSlack && angles.back() <= high + kAngleSlack; | |
| 37 | } | ||
| 38 | |||
| 39 | /// Type C's horizontal angle `phi` in the planes a file gives: mirrored into a quadrant or a half | ||
| 40 | /// of a symmetric luminaire; else a whole turn, past the last plane across the gap to the first. | ||
| 41 | 12 | double FoldPlane(const std::vector<double>& planes, double phi) { | |
| 42 | 12 | phi = Mod(phi, 360.0); | |
| 43 | 12 | const double first = planes.front(); | |
| 44 | 12 | const double last = planes.back(); | |
| 45 | 12 | if (Near(first, 0.0) && Near(last, 90.0)) { | |
| 46 | 12 | phi = phi > 180.0 ? 360.0 - phi : phi; | |
| 47 | 12 | return phi > 90.0 ? 180.0 - phi : phi; | |
| 48 | } | ||
| 49 | ✗ | if (Near(first, 0.0) && Near(last, 180.0)) { | |
| 50 | ✗ | return phi > 180.0 ? 360.0 - phi : phi; | |
| 51 | } | ||
| 52 | ✗ | if (Near(first, 90.0) && Near(last, 270.0)) { | |
| 53 | ✗ | return phi < 90.0 ? 180.0 - phi : phi > 270.0 ? 540.0 - phi : phi; | |
| 54 | } | ||
| 55 | ✗ | return phi < first ? phi + 360.0 : phi; | |
| 56 | } | ||
| 57 | |||
| 58 | constexpr const char* kFormat = "IES LM-63 file"; | ||
| 59 | |||
| 60 | /// Tilt data: the lamp-to-luminaire geometry, the number of pairs, the angles, the factors. | ||
| 61 | 5 | IesTilt ReadTilt(NumberReader& numbers) { | |
| 62 | 5 | IesTilt tilt; | |
| 63 | 5 | tilt.geometry = static_cast<int>(numbers.Next()); | |
| 64 | 5 | const std::size_t pairs = numbers.Count(); | |
| 65 | 5 | tilt.angles = numbers.Values(pairs); | |
| 66 | 5 | tilt.factors = numbers.Values(pairs); | |
| 67 | 10 | if (tilt.geometry < 1 || tilt.geometry > 3 || pairs == 0 || | |
| 68 | 5 | !std::is_sorted(tilt.angles.begin(), tilt.angles.end())) { | |
| 69 | ✗ | throw std::invalid_argument("IES LM-63 file has bad tilt data"); | |
| 70 | } | ||
| 71 | 5 | return tilt; | |
| 72 | ✗ | } | |
| 73 | |||
| 74 | 12 | IesType ReadType(double type) { | |
| 75 | 12 | if (type != 1.0 && type != 2.0 && type != 3.0) { | |
| 76 | 1 | throw std::invalid_argument("IES photometric type must be 1 (C), 2 (B) or 3 (A)"); | |
| 77 | } | ||
| 78 | 11 | return static_cast<IesType>(static_cast<int>(type)); | |
| 79 | } | ||
| 80 | |||
| 81 | } // namespace | ||
| 82 | |||
| 83 | 4 | double IesTilt::Angle(const glm::dvec3& x, const glm::dvec3& y, const glm::dvec3& z) const { | |
| 84 | 4 | switch (geometry) { | |
| 85 | 1 | case 1: | |
| 86 | 2 | return glm::degrees(std::acos(std::clamp(z.z, -1.0, 1.0))); | |
| 87 | 3 | case 2: | |
| 88 | 3 | return glm::degrees(std::asin(std::min(std::abs(x.z), 1.0))); | |
| 89 | ✗ | case 3: | |
| 90 | ✗ | return glm::degrees(std::asin(std::min(std::abs(y.z), 1.0))); | |
| 91 | default: | ||
| 92 | return 0.0; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | |||
| 96 | 8 | double IesTilt::Factor(double degrees) const { | |
| 97 | 8 | if (angles.empty()) { | |
| 98 | return 1.0; | ||
| 99 | } | ||
| 100 | // Data to 90 degrees mirrors about it. | ||
| 101 | 7 | if (angles.back() <= 90.0 + kAngleSlack && degrees > 90.0) { | |
| 102 | 2 | degrees = 180.0 - degrees; | |
| 103 | } | ||
| 104 | 7 | return Interpolate<double>(angles, factors, degrees); | |
| 105 | } | ||
| 106 | |||
| 107 | 259234 | double IesProfile::Sample(double theta, double phi) const { | |
| 108 | 259234 | if (type == IesType::C) { | |
| 109 | 259218 | return Table(theta, phi); | |
| 110 | } | ||
| 111 | // The direction in the luminaire's frame, then the file's angles for it. | ||
| 112 | 16 | const double t = glm::radians(theta); | |
| 113 | 16 | const double p = glm::radians(phi); | |
| 114 | 16 | const glm::dvec3 e(std::sin(t) * std::cos(p), std::sin(t) * std::sin(p), -std::cos(t)); | |
| 115 | 16 | if (type == IesType::B) { | |
| 116 | 26 | return Table(glm::degrees(std::atan2(e.x, -e.z)), | |
| 117 | 26 | glm::degrees(std::asin(std::clamp(-e.y, -1.0, 1.0)))); | |
| 118 | } | ||
| 119 | 6 | return Table(glm::degrees(std::atan2(e.y, -e.z)), | |
| 120 | 6 | glm::degrees(std::asin(std::clamp(e.x, -1.0, 1.0)))); | |
| 121 | } | ||
| 122 | |||
| 123 | 259234 | double IesProfile::Table(double v, double h) const { | |
| 124 | 259234 | if (vertical.empty() || horizontal.empty()) { | |
| 125 | return 0.0; | ||
| 126 | } | ||
| 127 | 259234 | if (type != IesType::C) { | |
| 128 | // Types A and B give one side of a symmetric luminaire from 0 degrees. | ||
| 129 | 16 | v = Near(vertical.front(), 0.0) ? std::abs(v) : v; | |
| 130 | 16 | h = Near(horizontal.front(), 0.0) ? std::abs(h) : h; | |
| 131 | 16 | if (horizontal.size() > 1 && !Within(horizontal, h)) { | |
| 132 | return 0.0; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | 259234 | if (!Within(vertical, v)) { | |
| 136 | return 0.0; | ||
| 137 | } | ||
| 138 | 129631 | const std::size_t columns = vertical.size(); | |
| 139 | 259289 | const auto row = [&](std::size_t plane) { | |
| 140 | 129658 | if (columns == 1) { | |
| 141 | ✗ | return candela[plane]; | |
| 142 | } | ||
| 143 | 129658 | const auto [i, t] = Bracket(vertical, v); | |
| 144 | 129658 | return std::lerp(candela[plane * columns + i], candela[plane * columns + i + 1], t); | |
| 145 | 129631 | }; | |
| 146 | // One plane: rotationally symmetric, or a single plane of data. | ||
| 147 | 129631 | if (horizontal.size() == 1) { | |
| 148 | 129604 | return row(0); | |
| 149 | } | ||
| 150 | 27 | const double phi = type == IesType::C ? FoldPlane(horizontal, h) : h; | |
| 151 | 27 | const double last = horizontal.back(); | |
| 152 | 27 | if (phi > last) { | |
| 153 | ✗ | const double gap = horizontal.front() + 360.0 - last; | |
| 154 | ✗ | return std::lerp(row(horizontal.size() - 1), row(0), gap > 0.0 ? (phi - last) / gap : 0.0); | |
| 155 | } | ||
| 156 | 27 | const auto [plane, t] = Bracket(horizontal, phi); | |
| 157 | 27 | return std::lerp(row(plane), row(plane + 1), t); | |
| 158 | } | ||
| 159 | |||
| 160 | 1 | double IesProfile::Flux() const { | |
| 161 | 1 | constexpr int kTheta = 720; | |
| 162 | 1 | constexpr int kPhi = 360; | |
| 163 | 1 | const double dTheta = 180.0 / kTheta; | |
| 164 | 1 | const double dPhi = 360.0 / kPhi; | |
| 165 | 1 | double sum = 0.0; | |
| 166 | 721 | for (int i = 0; i < kTheta; ++i) { | |
| 167 | 720 | const double theta = (i + 0.5) * dTheta; | |
| 168 | 720 | double ring = 0.0; | |
| 169 | 259920 | for (int j = 0; j < kPhi; ++j) { | |
| 170 | 259200 | ring += Sample(theta, (j + 0.5) * dPhi); | |
| 171 | } | ||
| 172 | 720 | sum += ring * std::sin(glm::radians(theta)); | |
| 173 | } | ||
| 174 | 1 | return sum * glm::radians(dTheta) * glm::radians(dPhi); | |
| 175 | } | ||
| 176 | |||
| 177 | 14 | IesProfile ParseIes(std::string_view text, const std::filesystem::path& directory) { | |
| 178 | // Keywords up to TILT=; numbers after its line, separated by blanks, line breaks or commas. | ||
| 179 | 14 | const std::size_t tiltAt = text.find("TILT="); | |
| 180 | 14 | if (tiltAt == std::string_view::npos) { | |
| 181 | 1 | throw std::invalid_argument("Not an IES LM-63 file: no TILT= line"); | |
| 182 | } | ||
| 183 | 13 | const std::size_t lineEnd = std::min(text.find('\n', tiltAt), text.size()); | |
| 184 | 13 | const std::string tilt = Trim(text.substr(tiltAt + 5, lineEnd - tiltAt - 5)); | |
| 185 | 18 | NumberReader numbers(text.substr(lineEnd), kFormat); | |
| 186 | |||
| 187 | 13 | IesProfile profile; | |
| 188 | 13 | if (tilt == "INCLUDE") { | |
| 189 | 6 | profile.tilt = ReadTilt(numbers); | |
| 190 | 10 | } else if (tilt != "NONE" && !tilt.empty()) { | |
| 191 | 11 | NumberReader file(ReadTextFile(directory / tilt), "IES tilt file"); | |
| 192 | 4 | profile.tilt = ReadTilt(file); | |
| 193 | 2 | } | |
| 194 | |||
| 195 | 12 | numbers.Skip(2); // lamps, lumens per lamp (-1: absolute photometry) | |
| 196 | 12 | const double multiplier = numbers.Next(); | |
| 197 | 12 | const std::size_t verticals = numbers.Count(); | |
| 198 | 12 | const std::size_t horizontals = numbers.Count(); | |
| 199 | 12 | profile.type = ReadType(numbers.Next()); | |
| 200 | 11 | numbers.Skip(4); // units, width, length, height | |
| 201 | 11 | const double ballast = numbers.Next(); | |
| 202 | 11 | numbers.Skip(2); // file generation type (formerly the ballast-lamp factor), input watts | |
| 203 | 11 | if (verticals == 0 || horizontals == 0) { | |
| 204 | ✗ | throw std::invalid_argument("IES LM-63 file has no angles"); | |
| 205 | } | ||
| 206 | 11 | profile.vertical = numbers.Values(verticals); | |
| 207 | 11 | profile.horizontal = numbers.Values(horizontals); | |
| 208 | |||
| 209 | // Type C's vertical angles run from the nadir to the zenith; types A and B's both run from -90 | ||
| 210 | // to 90 degrees about the beam's axis. | ||
| 211 | 11 | constexpr double kAny = std::numeric_limits<double>::infinity(); | |
| 212 | 11 | const bool c = profile.type == IesType::C; | |
| 213 | 24 | if (!Increasing(profile.vertical, c ? 0.0 : -90.0, c ? 180.0 : 90.0) || | |
| 214 | 12 | !Increasing(profile.horizontal, c ? -kAny : -90.0, c ? kAny : 90.0)) { | |
| 215 | 2 | throw std::invalid_argument("IES LM-63 file's angles are out of order or range"); | |
| 216 | } | ||
| 217 | |||
| 218 | 9 | profile.candela = numbers.Values(verticals * horizontals); | |
| 219 | 76 | for (double& value : profile.candela) { | |
| 220 | 68 | value = std::max(value, 0.0) * multiplier * ballast; | |
| 221 | } | ||
| 222 | 8 | return profile; | |
| 223 | 18 | } | |
| 224 | |||
| 225 | 4 | IesProfile ReadIes(const std::filesystem::path& path) { | |
| 226 | 10 | return ParseIes(ReadTextFile(path), path.parent_path()); | |
| 227 | } | ||
| 228 | |||
| 229 | } // namespace ernie | ||
| 230 |