/* * Copyright (C) 2018 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include #include #include #include #include #include #include #include using namespace filament::math; using namespace std::complex_literals; static float g_incidenceAngle = 81.7f; static void printUsage(const char* name) { std::string execName(utils::Path(name).getName()); std::string usage( "SPECULAR_COLOR computes the base color of a conductor from spectral data\n" "Usage:\n" " SPECULAR_COLOR [options] \n" "\n" "Options:\n" " --help, -h\n" " Print this message\n\n" " --license\n" " Print copyright and license information\n\n" ); const std::string from("SPECULAR_COLOR"); for (size_t pos = usage.find(from); pos != std::string::npos; pos = usage.find(from, pos)) { usage.replace(pos, from.length(), execName); } printf("%s", usage.c_str()); } static void license() { static const char *license[] = { #include "licenses/licenses.inc" nullptr }; const char **p = &license[0]; while (*p) std::cout << *p++ << std::endl; } static int handleArguments(int argc, char* argv[]) { static constexpr const char* OPTSTR = "hla:"; static const struct option OPTIONS[] = { { "help", no_argument, nullptr, 'h' }, { "license", no_argument, nullptr, 'l' }, { "angle", required_argument, nullptr, 'a' }, { nullptr, 0, nullptr, 0 } // termination of the option list }; int opt; int optionIndex = 0; while ((opt = getopt_long(argc, argv, OPTSTR, OPTIONS, &optionIndex)) >= 0) { std::string arg(optarg ? optarg : ""); switch (opt) { default: case 'h': printUsage(argv[0]); exit(0); case 'l': license(); exit(0); case 'a': g_incidenceAngle = std::stof(arg); break; } } return optind; } // CIE 1931 10-deg color matching functions (CMFs), from 360nm to 830nm, at 1nm intervals // // Data source: // http://cvrl.ioo.ucl.ac.uk/cmfs.htm // http://cvrl.ioo.ucl.ac.uk/database/text/cmfs/ciexyz64.htm const size_t CIE_XYZ_START = 360; const size_t CIE_XYZ_COUNT = 471; const float3 CIE_XYZ[CIE_XYZ_COUNT] = { // NOLINT { 0.000000122200f, 0.000000013398f, 0.000000535027f }, { 0.000000185138f, 0.000000020294f, 0.000000810720f }, { 0.000000278830f, 0.000000030560f, 0.000001221200f }, { 0.000000417470f, 0.000000045740f, 0.000001828700f }, { 0.000000621330f, 0.000000068050f, 0.000002722200f }, { 0.000000919270f, 0.000000100650f, 0.000004028300f }, { 0.000001351980f, 0.000000147980f, 0.000005925700f }, { 0.000001976540f, 0.000000216270f, 0.000008665100f }, { 0.000002872500f, 0.000000314200f, 0.000012596000f }, { 0.000004149500f, 0.000000453700f, 0.000018201000f }, { 0.000005958600f, 0.000000651100f, 0.000026143700f }, { 0.000008505600f, 0.000000928800f, 0.000037330000f }, { 0.000012068600f, 0.000001317500f, 0.000052987000f }, { 0.000017022600f, 0.000001857200f, 0.000074764000f }, { 0.000023868000f, 0.000002602000f, 0.000104870000f }, { 0.000033266000f, 0.000003625000f, 0.000146220000f }, { 0.000046087000f, 0.000005019000f, 0.000202660000f }, { 0.000063472000f, 0.000006907000f, 0.000279230000f }, { 0.000086892000f, 0.000009449000f, 0.000382450000f }, { 0.000118246000f, 0.000012848000f, 0.000520720000f }, { 0.000159952000f, 0.000017364000f, 0.000704776000f }, { 0.000215080000f, 0.000023327000f, 0.000948230000f }, { 0.000287490000f, 0.000031150000f, 0.001268200000f }, { 0.000381990000f, 0.000041350000f, 0.001686100000f }, { 0.000504550000f, 0.000054560000f, 0.002228500000f }, { 0.000662440000f, 0.000071560000f, 0.002927800000f }, { 0.000864500000f, 0.000093300000f, 0.003823700000f }, { 0.001121500000f, 0.000120870000f, 0.004964200000f }, { 0.001446160000f, 0.000155640000f, 0.006406700000f }, { 0.001853590000f, 0.000199200000f, 0.008219300000f }, { 0.002361600000f, 0.000253400000f, 0.010482200000f }, { 0.002990600000f, 0.000320200000f, 0.013289000000f }, { 0.003764500000f, 0.000402400000f, 0.016747000000f }, { 0.004710200000f, 0.000502300000f, 0.020980000000f }, { 0.005858100000f, 0.000623200000f, 0.026127000000f }, { 0.007242300000f, 0.000768500000f, 0.032344000000f }, { 0.008899600000f, 0.000941700000f, 0.039802000000f }, { 0.010870900000f, 0.001147800000f, 0.048691000000f }, { 0.013198900000f, 0.001390300000f, 0.059210000000f }, { 0.015929200000f, 0.001674000000f, 0.071576000000f }, { 0.019109700000f, 0.002004400000f, 0.086010900000f }, { 0.022788000000f, 0.002386000000f, 0.102740000000f }, { 0.027011000000f, 0.002822000000f, 0.122000000000f }, { 0.031829000000f, 0.003319000000f, 0.144020000000f }, { 0.037278000000f, 0.003880000000f, 0.168990000000f }, { 0.043400000000f, 0.004509000000f, 0.197120000000f }, { 0.050223000000f, 0.005209000000f, 0.228570000000f }, { 0.057764000000f, 0.005985000000f, 0.263470000000f }, { 0.066038000000f, 0.006833000000f, 0.301900000000f }, { 0.075033000000f, 0.007757000000f, 0.343870000000f }, { 0.084736000000f, 0.008756000000f, 0.389366000000f }, { 0.095041000000f, 0.009816000000f, 0.437970000000f }, { 0.105836000000f, 0.010918000000f, 0.489220000000f }, { 0.117066000000f, 0.012058000000f, 0.542900000000f }, { 0.128682000000f, 0.013237000000f, 0.598810000000f }, { 0.140638000000f, 0.014456000000f, 0.656760000000f }, { 0.152893000000f, 0.015717000000f, 0.716580000000f }, { 0.165416000000f, 0.017025000000f, 0.778120000000f }, { 0.178191000000f, 0.018399000000f, 0.841310000000f }, { 0.191214000000f, 0.019848000000f, 0.906110000000f }, { 0.204492000000f, 0.021391000000f, 0.972542000000f }, { 0.217650000000f, 0.022992000000f, 1.038900000000f }, { 0.230267000000f, 0.024598000000f, 1.103100000000f }, { 0.242311000000f, 0.026213000000f, 1.165100000000f }, { 0.253793000000f, 0.027841000000f, 1.224900000000f }, { 0.264737000000f, 0.029497000000f, 1.282500000000f }, { 0.275195000000f, 0.031195000000f, 1.338200000000f }, { 0.285301000000f, 0.032927000000f, 1.392600000000f }, { 0.295143000000f, 0.034738000000f, 1.446100000000f }, { 0.304869000000f, 0.036654000000f, 1.499400000000f }, { 0.314679000000f, 0.038676000000f, 1.553480000000f }, { 0.324355000000f, 0.040792000000f, 1.607200000000f }, { 0.333570000000f, 0.042946000000f, 1.658900000000f }, { 0.342243000000f, 0.045114000000f, 1.708200000000f }, { 0.350312000000f, 0.047333000000f, 1.754800000000f }, { 0.357719000000f, 0.049602000000f, 1.798500000000f }, { 0.364482000000f, 0.051934000000f, 1.839200000000f }, { 0.370493000000f, 0.054337000000f, 1.876600000000f }, { 0.375727000000f, 0.056822000000f, 1.910500000000f }, { 0.380158000000f, 0.059399000000f, 1.940800000000f }, { 0.383734000000f, 0.062077000000f, 1.967280000000f }, { 0.386327000000f, 0.064737000000f, 1.989100000000f }, { 0.387858000000f, 0.067285000000f, 2.005700000000f }, { 0.388396000000f, 0.069764000000f, 2.017400000000f }, { 0.387978000000f, 0.072218000000f, 2.024400000000f }, { 0.386726000000f, 0.074704000000f, 2.027300000000f }, { 0.384696000000f, 0.077272000000f, 2.026400000000f }, { 0.382006000000f, 0.079979000000f, 2.022300000000f }, { 0.378709000000f, 0.082874000000f, 2.015300000000f }, { 0.374915000000f, 0.086000000000f, 2.006000000000f }, { 0.370702000000f, 0.089456000000f, 1.994800000000f }, { 0.366089000000f, 0.092947000000f, 1.981400000000f }, { 0.361045000000f, 0.096275000000f, 1.965300000000f }, { 0.355518000000f, 0.099535000000f, 1.946400000000f }, { 0.349486000000f, 0.102829000000f, 1.924800000000f }, { 0.342957000000f, 0.106256000000f, 1.900700000000f }, { 0.335893000000f, 0.109901000000f, 1.874100000000f }, { 0.328284000000f, 0.113835000000f, 1.845100000000f }, { 0.320150000000f, 0.118167000000f, 1.813900000000f }, { 0.311475000000f, 0.122932000000f, 1.780600000000f }, { 0.302273000000f, 0.128201000000f, 1.745370000000f }, { 0.292858000000f, 0.133457000000f, 1.709100000000f }, { 0.283502000000f, 0.138323000000f, 1.672300000000f }, { 0.274044000000f, 0.143042000000f, 1.634700000000f }, { 0.264263000000f, 0.147787000000f, 1.595600000000f }, { 0.254085000000f, 0.152761000000f, 1.554900000000f }, { 0.243392000000f, 0.158102000000f, 1.512200000000f }, { 0.232187000000f, 0.163941000000f, 1.467300000000f }, { 0.220488000000f, 0.170362000000f, 1.419900000000f }, { 0.208198000000f, 0.177425000000f, 1.370000000000f }, { 0.195618000000f, 0.185190000000f, 1.317560000000f }, { 0.183034000000f, 0.193025000000f, 1.262400000000f }, { 0.170222000000f, 0.200313000000f, 1.205000000000f }, { 0.157348000000f, 0.207156000000f, 1.146600000000f }, { 0.144650000000f, 0.213644000000f, 1.088000000000f }, { 0.132349000000f, 0.219940000000f, 1.030200000000f }, { 0.120584000000f, 0.226170000000f, 0.973830000000f }, { 0.109456000000f, 0.232467000000f, 0.919430000000f }, { 0.099042000000f, 0.239025000000f, 0.867460000000f }, { 0.089388000000f, 0.245997000000f, 0.818280000000f }, { 0.080507000000f, 0.253589000000f, 0.772125000000f }, { 0.072034000000f, 0.261876000000f, 0.728290000000f }, { 0.063710000000f, 0.270643000000f, 0.686040000000f }, { 0.055694000000f, 0.279645000000f, 0.645530000000f }, { 0.048117000000f, 0.288694000000f, 0.606850000000f }, { 0.041072000000f, 0.297665000000f, 0.570060000000f }, { 0.034642000000f, 0.306469000000f, 0.535220000000f }, { 0.028896000000f, 0.315035000000f, 0.502340000000f }, { 0.023876000000f, 0.323335000000f, 0.471400000000f }, { 0.019628000000f, 0.331366000000f, 0.442390000000f }, { 0.016172000000f, 0.339133000000f, 0.415254000000f }, { 0.013300000000f, 0.347860000000f, 0.390024000000f }, { 0.010759000000f, 0.358326000000f, 0.366399000000f }, { 0.008542000000f, 0.370001000000f, 0.344015000000f }, { 0.006661000000f, 0.382464000000f, 0.322689000000f }, { 0.005132000000f, 0.395379000000f, 0.302356000000f }, { 0.003982000000f, 0.408482000000f, 0.283036000000f }, { 0.003239000000f, 0.421588000000f, 0.264816000000f }, { 0.002934000000f, 0.434619000000f, 0.247848000000f }, { 0.003114000000f, 0.447601000000f, 0.232318000000f }, { 0.003816000000f, 0.460777000000f, 0.218502000000f }, { 0.005095000000f, 0.474340000000f, 0.205851000000f }, { 0.006936000000f, 0.488200000000f, 0.193596000000f }, { 0.009299000000f, 0.502340000000f, 0.181736000000f }, { 0.012147000000f, 0.516740000000f, 0.170281000000f }, { 0.015444000000f, 0.531360000000f, 0.159249000000f }, { 0.019156000000f, 0.546190000000f, 0.148673000000f }, { 0.023250000000f, 0.561180000000f, 0.138609000000f }, { 0.027690000000f, 0.576290000000f, 0.129096000000f }, { 0.032444000000f, 0.591500000000f, 0.120215000000f }, { 0.037465000000f, 0.606741000000f, 0.112044000000f }, { 0.042956000000f, 0.622150000000f, 0.104710000000f }, { 0.049114000000f, 0.637830000000f, 0.098196000000f }, { 0.055920000000f, 0.653710000000f, 0.092361000000f }, { 0.063349000000f, 0.669680000000f, 0.087088000000f }, { 0.071358000000f, 0.685660000000f, 0.082248000000f }, { 0.079901000000f, 0.701550000000f, 0.077744000000f }, { 0.088909000000f, 0.717230000000f, 0.073456000000f }, { 0.098293000000f, 0.732570000000f, 0.069268000000f }, { 0.107949000000f, 0.747460000000f, 0.065060000000f }, { 0.117749000000f, 0.761757000000f, 0.060709000000f }, { 0.127839000000f, 0.775340000000f, 0.056457000000f }, { 0.138450000000f, 0.788220000000f, 0.052609000000f }, { 0.149516000000f, 0.800460000000f, 0.049122000000f }, { 0.161041000000f, 0.812140000000f, 0.045954000000f }, { 0.172953000000f, 0.823330000000f, 0.043050000000f }, { 0.185209000000f, 0.834120000000f, 0.040368000000f }, { 0.197755000000f, 0.844600000000f, 0.037839000000f }, { 0.210538000000f, 0.854870000000f, 0.035384000000f }, { 0.223460000000f, 0.865040000000f, 0.032949000000f }, { 0.236491000000f, 0.875211000000f, 0.030451000000f }, { 0.249633000000f, 0.885370000000f, 0.028029000000f }, { 0.262972000000f, 0.895370000000f, 0.025862000000f }, { 0.276515000000f, 0.905150000000f, 0.023920000000f }, { 0.290269000000f, 0.914650000000f, 0.022174000000f }, { 0.304213000000f, 0.923810000000f, 0.020584000000f }, { 0.318361000000f, 0.932550000000f, 0.019127000000f }, { 0.332705000000f, 0.940810000000f, 0.017740000000f }, { 0.347232000000f, 0.948520000000f, 0.016403000000f }, { 0.361926000000f, 0.955600000000f, 0.015064000000f }, { 0.376772000000f, 0.961988000000f, 0.013676000000f }, { 0.391683000000f, 0.967540000000f, 0.012308000000f }, { 0.406594000000f, 0.972230000000f, 0.011056000000f }, { 0.421539000000f, 0.976170000000f, 0.009915000000f }, { 0.436517000000f, 0.979460000000f, 0.008872000000f }, { 0.451584000000f, 0.982200000000f, 0.007918000000f }, { 0.466782000000f, 0.984520000000f, 0.007030000000f }, { 0.482147000000f, 0.986520000000f, 0.006223000000f }, { 0.497738000000f, 0.988320000000f, 0.005453000000f }, { 0.513606000000f, 0.990020000000f, 0.004714000000f }, { 0.529826000000f, 0.991761000000f, 0.003988000000f }, { 0.546440000000f, 0.993530000000f, 0.003289000000f }, { 0.563426000000f, 0.995230000000f, 0.002646000000f }, { 0.580726000000f, 0.996770000000f, 0.002063000000f }, { 0.598290000000f, 0.998090000000f, 0.001533000000f }, { 0.616053000000f, 0.999110000000f, 0.001091000000f }, { 0.633948000000f, 0.999770000000f, 0.000711000000f }, { 0.651901000000f, 1.000000000000f, 0.000407000000f }, { 0.669824000000f, 0.999710000000f, 0.000184000000f }, { 0.687632000000f, 0.998850000000f, 0.000047000000f }, { 0.705224000000f, 0.997340000000f, 0.000000000000f }, { 0.722773000000f, 0.995260000000f, 0.000000000000f }, { 0.740483000000f, 0.992740000000f, 0.000000000000f }, { 0.758273000000f, 0.989750000000f, 0.000000000000f }, { 0.776083000000f, 0.986300000000f, 0.000000000000f }, { 0.793832000000f, 0.982380000000f, 0.000000000000f }, { 0.811436000000f, 0.977980000000f, 0.000000000000f }, { 0.828822000000f, 0.973110000000f, 0.000000000000f }, { 0.845879000000f, 0.967740000000f, 0.000000000000f }, { 0.862525000000f, 0.961890000000f, 0.000000000000f }, { 0.878655000000f, 0.955552000000f, 0.000000000000f }, { 0.894208000000f, 0.948601000000f, 0.000000000000f }, { 0.909206000000f, 0.940981000000f, 0.000000000000f }, { 0.923672000000f, 0.932798000000f, 0.000000000000f }, { 0.937638000000f, 0.924158000000f, 0.000000000000f }, { 0.951162000000f, 0.915175000000f, 0.000000000000f }, { 0.964283000000f, 0.905954000000f, 0.000000000000f }, { 0.977068000000f, 0.896608000000f, 0.000000000000f }, { 0.989590000000f, 0.887249000000f, 0.000000000000f }, { 1.001910000000f, 0.877986000000f, 0.000000000000f }, { 1.014160000000f, 0.868934000000f, 0.000000000000f }, { 1.026500000000f, 0.860164000000f, 0.000000000000f }, { 1.038800000000f, 0.851519000000f, 0.000000000000f }, { 1.051000000000f, 0.842963000000f, 0.000000000000f }, { 1.062900000000f, 0.834393000000f, 0.000000000000f }, { 1.074300000000f, 0.825623000000f, 0.000000000000f }, { 1.085200000000f, 0.816764000000f, 0.000000000000f }, { 1.095200000000f, 0.807544000000f, 0.000000000000f }, { 1.104200000000f, 0.797947000000f, 0.000000000000f }, { 1.112000000000f, 0.787893000000f, 0.000000000000f }, { 1.118520000000f, 0.777405000000f, 0.000000000000f }, { 1.123800000000f, 0.766490000000f, 0.000000000000f }, { 1.128000000000f, 0.755309000000f, 0.000000000000f }, { 1.131100000000f, 0.743845000000f, 0.000000000000f }, { 1.133200000000f, 0.732190000000f, 0.000000000000f }, { 1.134300000000f, 0.720353000000f, 0.000000000000f }, { 1.134300000000f, 0.708281000000f, 0.000000000000f }, { 1.133300000000f, 0.696055000000f, 0.000000000000f }, { 1.131200000000f, 0.683621000000f, 0.000000000000f }, { 1.128100000000f, 0.671048000000f, 0.000000000000f }, { 1.123990000000f, 0.658341000000f, 0.000000000000f }, { 1.118900000000f, 0.645545000000f, 0.000000000000f }, { 1.112900000000f, 0.632718000000f, 0.000000000000f }, { 1.105900000000f, 0.619815000000f, 0.000000000000f }, { 1.098000000000f, 0.606887000000f, 0.000000000000f }, { 1.089100000000f, 0.593878000000f, 0.000000000000f }, { 1.079200000000f, 0.580781000000f, 0.000000000000f }, { 1.068400000000f, 0.567653000000f, 0.000000000000f }, { 1.056700000000f, 0.554490000000f, 0.000000000000f }, { 1.044000000000f, 0.541228000000f, 0.000000000000f }, { 1.030480000000f, 0.527963000000f, 0.000000000000f }, { 1.016000000000f, 0.514634000000f, 0.000000000000f }, { 1.000800000000f, 0.501363000000f, 0.000000000000f }, { 0.984790000000f, 0.488124000000f, 0.000000000000f }, { 0.968080000000f, 0.474935000000f, 0.000000000000f }, { 0.950740000000f, 0.461834000000f, 0.000000000000f }, { 0.932800000000f, 0.448823000000f, 0.000000000000f }, { 0.914340000000f, 0.435917000000f, 0.000000000000f }, { 0.895390000000f, 0.423153000000f, 0.000000000000f }, { 0.876030000000f, 0.410526000000f, 0.000000000000f }, { 0.856297000000f, 0.398057000000f, 0.000000000000f }, { 0.836350000000f, 0.385835000000f, 0.000000000000f }, { 0.816290000000f, 0.373951000000f, 0.000000000000f }, { 0.796050000000f, 0.362311000000f, 0.000000000000f }, { 0.775610000000f, 0.350863000000f, 0.000000000000f }, { 0.754930000000f, 0.339554000000f, 0.000000000000f }, { 0.733990000000f, 0.328309000000f, 0.000000000000f }, { 0.712780000000f, 0.317118000000f, 0.000000000000f }, { 0.691290000000f, 0.305936000000f, 0.000000000000f }, { 0.669520000000f, 0.294737000000f, 0.000000000000f }, { 0.647467000000f, 0.283493000000f, 0.000000000000f }, { 0.625110000000f, 0.272222000000f, 0.000000000000f }, { 0.602520000000f, 0.260990000000f, 0.000000000000f }, { 0.579890000000f, 0.249877000000f, 0.000000000000f }, { 0.557370000000f, 0.238946000000f, 0.000000000000f }, { 0.535110000000f, 0.228254000000f, 0.000000000000f }, { 0.513240000000f, 0.217853000000f, 0.000000000000f }, { 0.491860000000f, 0.207780000000f, 0.000000000000f }, { 0.471080000000f, 0.198072000000f, 0.000000000000f }, { 0.450960000000f, 0.188748000000f, 0.000000000000f }, { 0.431567000000f, 0.179828000000f, 0.000000000000f }, { 0.412870000000f, 0.171285000000f, 0.000000000000f }, { 0.394750000000f, 0.163059000000f, 0.000000000000f }, { 0.377210000000f, 0.155151000000f, 0.000000000000f }, { 0.360190000000f, 0.147535000000f, 0.000000000000f }, { 0.343690000000f, 0.140211000000f, 0.000000000000f }, { 0.327690000000f, 0.133170000000f, 0.000000000000f }, { 0.312170000000f, 0.126400000000f, 0.000000000000f }, { 0.297110000000f, 0.119892000000f, 0.000000000000f }, { 0.282500000000f, 0.113640000000f, 0.000000000000f }, { 0.268329000000f, 0.107633000000f, 0.000000000000f }, { 0.254590000000f, 0.101870000000f, 0.000000000000f }, { 0.241300000000f, 0.096347000000f, 0.000000000000f }, { 0.228480000000f, 0.091063000000f, 0.000000000000f }, { 0.216140000000f, 0.086010000000f, 0.000000000000f }, { 0.204300000000f, 0.081187000000f, 0.000000000000f }, { 0.192950000000f, 0.076583000000f, 0.000000000000f }, { 0.182110000000f, 0.072198000000f, 0.000000000000f }, { 0.171770000000f, 0.068024000000f, 0.000000000000f }, { 0.161920000000f, 0.064052000000f, 0.000000000000f }, { 0.152568000000f, 0.060281000000f, 0.000000000000f }, { 0.143670000000f, 0.056697000000f, 0.000000000000f }, { 0.135200000000f, 0.053292000000f, 0.000000000000f }, { 0.127130000000f, 0.050059000000f, 0.000000000000f }, { 0.119480000000f, 0.046998000000f, 0.000000000000f }, { 0.112210000000f, 0.044096000000f, 0.000000000000f }, { 0.105310000000f, 0.041345000000f, 0.000000000000f }, { 0.098786000000f, 0.038750700000f, 0.000000000000f }, { 0.092610000000f, 0.036297800000f, 0.000000000000f }, { 0.086773000000f, 0.033983200000f, 0.000000000000f }, { 0.081260600000f, 0.031800400000f, 0.000000000000f }, { 0.076048000000f, 0.029739500000f, 0.000000000000f }, { 0.071114000000f, 0.027791800000f, 0.000000000000f }, { 0.066454000000f, 0.025955100000f, 0.000000000000f }, { 0.062062000000f, 0.024226300000f, 0.000000000000f }, { 0.057930000000f, 0.022601700000f, 0.000000000000f }, { 0.054050000000f, 0.021077900000f, 0.000000000000f }, { 0.050412000000f, 0.019650500000f, 0.000000000000f }, { 0.047006000000f, 0.018315300000f, 0.000000000000f }, { 0.043823000000f, 0.017068600000f, 0.000000000000f }, { 0.040850800000f, 0.015905100000f, 0.000000000000f }, { 0.038072000000f, 0.014818300000f, 0.000000000000f }, { 0.035468000000f, 0.013800800000f, 0.000000000000f }, { 0.033031000000f, 0.012849500000f, 0.000000000000f }, { 0.030753000000f, 0.011960700000f, 0.000000000000f }, { 0.028623000000f, 0.011130300000f, 0.000000000000f }, { 0.026635000000f, 0.010355500000f, 0.000000000000f }, { 0.024781000000f, 0.009633200000f, 0.000000000000f }, { 0.023052000000f, 0.008959900000f, 0.000000000000f }, { 0.021441000000f, 0.008332400000f, 0.000000000000f }, { 0.019941300000f, 0.007748800000f, 0.000000000000f }, { 0.018544000000f, 0.007204600000f, 0.000000000000f }, { 0.017241000000f, 0.006697500000f, 0.000000000000f }, { 0.016027000000f, 0.006225100000f, 0.000000000000f }, { 0.014896000000f, 0.005785000000f, 0.000000000000f }, { 0.013842000000f, 0.005375100000f, 0.000000000000f }, { 0.012862000000f, 0.004994100000f, 0.000000000000f }, { 0.011949000000f, 0.004639200000f, 0.000000000000f }, { 0.011100000000f, 0.004309300000f, 0.000000000000f }, { 0.010311000000f, 0.004002800000f, 0.000000000000f }, { 0.009576880000f, 0.003717740000f, 0.000000000000f }, { 0.008894000000f, 0.003452620000f, 0.000000000000f }, { 0.008258100000f, 0.003205830000f, 0.000000000000f }, { 0.007666400000f, 0.002976230000f, 0.000000000000f }, { 0.007116300000f, 0.002762810000f, 0.000000000000f }, { 0.006605200000f, 0.002564560000f, 0.000000000000f }, { 0.006130600000f, 0.002380480000f, 0.000000000000f }, { 0.005690300000f, 0.002209710000f, 0.000000000000f }, { 0.005281900000f, 0.002051320000f, 0.000000000000f }, { 0.004903300000f, 0.001904490000f, 0.000000000000f }, { 0.004552630000f, 0.001768470000f, 0.000000000000f }, { 0.004227500000f, 0.001642360000f, 0.000000000000f }, { 0.003925800000f, 0.001525350000f, 0.000000000000f }, { 0.003645700000f, 0.001416720000f, 0.000000000000f }, { 0.003385900000f, 0.001315950000f, 0.000000000000f }, { 0.003144700000f, 0.001222390000f, 0.000000000000f }, { 0.002920800000f, 0.001135550000f, 0.000000000000f }, { 0.002713000000f, 0.001054940000f, 0.000000000000f }, { 0.002520200000f, 0.000980140000f, 0.000000000000f }, { 0.002341100000f, 0.000910660000f, 0.000000000000f }, { 0.002174960000f, 0.000846190000f, 0.000000000000f }, { 0.002020600000f, 0.000786290000f, 0.000000000000f }, { 0.001877300000f, 0.000730680000f, 0.000000000000f }, { 0.001744100000f, 0.000678990000f, 0.000000000000f }, { 0.001620500000f, 0.000631010000f, 0.000000000000f }, { 0.001505700000f, 0.000586440000f, 0.000000000000f }, { 0.001399200000f, 0.000545110000f, 0.000000000000f }, { 0.001300400000f, 0.000506720000f, 0.000000000000f }, { 0.001208700000f, 0.000471110000f, 0.000000000000f }, { 0.001123600000f, 0.000438050000f, 0.000000000000f }, { 0.001044760000f, 0.000407410000f, 0.000000000000f }, { 0.000971560000f, 0.000378962000f, 0.000000000000f }, { 0.000903600000f, 0.000352543000f, 0.000000000000f }, { 0.000840480000f, 0.000328001000f, 0.000000000000f }, { 0.000781870000f, 0.000305208000f, 0.000000000000f }, { 0.000727450000f, 0.000284041000f, 0.000000000000f }, { 0.000676900000f, 0.000264375000f, 0.000000000000f }, { 0.000629960000f, 0.000246109000f, 0.000000000000f }, { 0.000586370000f, 0.000229143000f, 0.000000000000f }, { 0.000545870000f, 0.000213376000f, 0.000000000000f }, { 0.000508258000f, 0.000198730000f, 0.000000000000f }, { 0.000473300000f, 0.000185115000f, 0.000000000000f }, { 0.000440800000f, 0.000172454000f, 0.000000000000f }, { 0.000410580000f, 0.000160678000f, 0.000000000000f }, { 0.000382490000f, 0.000149730000f, 0.000000000000f }, { 0.000356380000f, 0.000139550000f, 0.000000000000f }, { 0.000332110000f, 0.000130086000f, 0.000000000000f }, { 0.000309550000f, 0.000121290000f, 0.000000000000f }, { 0.000288580000f, 0.000113106000f, 0.000000000000f }, { 0.000269090000f, 0.000105501000f, 0.000000000000f }, { 0.000250969000f, 0.000098428000f, 0.000000000000f }, { 0.000234130000f, 0.000091853000f, 0.000000000000f }, { 0.000218470000f, 0.000085738000f, 0.000000000000f }, { 0.000203910000f, 0.000080048000f, 0.000000000000f }, { 0.000190350000f, 0.000074751000f, 0.000000000000f }, { 0.000177730000f, 0.000069819000f, 0.000000000000f }, { 0.000165970000f, 0.000065222000f, 0.000000000000f }, { 0.000155020000f, 0.000060939000f, 0.000000000000f }, { 0.000144800000f, 0.000056942000f, 0.000000000000f }, { 0.000135280000f, 0.000053217000f, 0.000000000000f }, { 0.000126390000f, 0.000049737000f, 0.000000000000f }, { 0.000118100000f, 0.000046491000f, 0.000000000000f }, { 0.000110370000f, 0.000043464000f, 0.000000000000f }, { 0.000103150000f, 0.000040635000f, 0.000000000000f }, { 0.000096427000f, 0.000038000000f, 0.000000000000f }, { 0.000090151000f, 0.000035540500f, 0.000000000000f }, { 0.000084294000f, 0.000033244800f, 0.000000000000f }, { 0.000078830000f, 0.000031100600f, 0.000000000000f }, { 0.000073729000f, 0.000029099000f, 0.000000000000f }, { 0.000068969000f, 0.000027230700f, 0.000000000000f }, { 0.000064525800f, 0.000025486000f, 0.000000000000f }, { 0.000060376000f, 0.000023856100f, 0.000000000000f }, { 0.000056500000f, 0.000022333200f, 0.000000000000f }, { 0.000052880000f, 0.000020910400f, 0.000000000000f }, { 0.000049498000f, 0.000019580800f, 0.000000000000f }, { 0.000046339000f, 0.000018338400f, 0.000000000000f }, { 0.000043389000f, 0.000017177700f, 0.000000000000f }, { 0.000040634000f, 0.000016093400f, 0.000000000000f }, { 0.000038060000f, 0.000015080000f, 0.000000000000f }, { 0.000035657000f, 0.000014133600f, 0.000000000000f }, { 0.000033411700f, 0.000013249000f, 0.000000000000f }, { 0.000031315000f, 0.000012422600f, 0.000000000000f }, { 0.000029355000f, 0.000011649900f, 0.000000000000f }, { 0.000027524000f, 0.000010927700f, 0.000000000000f }, { 0.000025811000f, 0.000010251900f, 0.000000000000f }, { 0.000024209000f, 0.000009619600f, 0.000000000000f }, { 0.000022711000f, 0.000009028100f, 0.000000000000f }, { 0.000021308000f, 0.000008474000f, 0.000000000000f }, { 0.000019994000f, 0.000007954800f, 0.000000000000f }, { 0.000018764000f, 0.000007468600f, 0.000000000000f }, { 0.000017611500f, 0.000007012800f, 0.000000000000f }, { 0.000016532000f, 0.000006585800f, 0.000000000000f }, { 0.000015521000f, 0.000006185700f, 0.000000000000f }, { 0.000014574000f, 0.000005810700f, 0.000000000000f }, { 0.000013686000f, 0.000005459000f, 0.000000000000f }, { 0.000012855000f, 0.000005129800f, 0.000000000000f }, { 0.000012075000f, 0.000004820600f, 0.000000000000f }, { 0.000011345000f, 0.000004531200f, 0.000000000000f }, { 0.000010659000f, 0.000004259100f, 0.000000000000f }, { 0.000010017000f, 0.000004004200f, 0.000000000000f }, { 0.000009413630f, 0.000003764730f, 0.000000000000f }, { 0.000008847900f, 0.000003539950f, 0.000000000000f }, { 0.000008317100f, 0.000003329140f, 0.000000000000f }, { 0.000007819000f, 0.000003131150f, 0.000000000000f }, { 0.000007351600f, 0.000002945290f, 0.000000000000f }, { 0.000006913000f, 0.000002770810f, 0.000000000000f }, { 0.000006501500f, 0.000002607050f, 0.000000000000f }, { 0.000006115300f, 0.000002453290f, 0.000000000000f }, { 0.000005752900f, 0.000002308940f, 0.000000000000f }, { 0.000005412700f, 0.000002173380f, 0.000000000000f }, { 0.000005093470f, 0.000002046130f, 0.000000000000f }, { 0.000004793800f, 0.000001926620f, 0.000000000000f }, { 0.000004512500f, 0.000001814400f, 0.000000000000f }, { 0.000004248300f, 0.000001708950f, 0.000000000000f }, { 0.000004000200f, 0.000001609880f, 0.000000000000f }, { 0.000003767100f, 0.000001516770f, 0.000000000000f }, { 0.000003548000f, 0.000001429210f, 0.000000000000f }, { 0.000003342100f, 0.000001346860f, 0.000000000000f }, { 0.000003148500f, 0.000001269450f, 0.000000000000f }, { 0.000002966500f, 0.000001196620f, 0.000000000000f }, { 0.000002795310f, 0.000001128090f, 0.000000000000f }, { 0.000002634500f, 0.000001063680f, 0.000000000000f }, { 0.000002483400f, 0.000001003130f, 0.000000000000f }, { 0.000002341400f, 0.000000946220f, 0.000000000000f }, { 0.000002207800f, 0.000000892630f, 0.000000000000f }, { 0.000002082000f, 0.000000842160f, 0.000000000000f }, { 0.000001963600f, 0.000000794640f, 0.000000000000f }, { 0.000001851900f, 0.000000749780f, 0.000000000000f }, { 0.000001746500f, 0.000000707440f, 0.000000000000f }, { 0.000001647100f, 0.000000667480f, 0.000000000000f }, { 0.000001553140f, 0.000000629700f, 0.000000000000f } }; // CIE Standard Illuminant D65 relative spectral power distribution, // from 300nm to 830, at 5nm intervals // // Data source: // https://en.wikipedia.org/wiki/Illuminant_D65 // https://cielab.xyz/pdf/CIE_sel_colorimetric_tables.xls const size_t CIE_D65_INTERVAL = 5; const size_t CIE_D65_START = 300; const size_t CIE_D65_END = 830; const size_t CIE_D65_COUNT = 107; const float CIE_D65[CIE_D65_COUNT] = { 0.0341f, 1.6643f, 3.2945f, 11.7652f, 20.2360f, 28.6447f, 37.0535f, 38.5011f, 39.9488f, 42.4302f, 44.9117f, 45.7750f, 46.6383f, 49.3637f, 52.0891f, 51.0323f, 49.9755f, 52.3118f, 54.6482f, 68.7015f, 82.7549f, 87.1204f, 91.4860f, 92.4589f, 93.4318f, 90.0570f, 86.6823f, 95.7736f, 104.865f, 110.936f, 117.008f, 117.410f, 117.812f, 116.336f, 114.861f, 115.392f, 115.923f, 112.367f, 108.811f, 109.082f, 109.354f, 108.578f, 107.802f, 106.296f, 104.790f, 106.239f, 107.689f, 106.047f, 104.405f, 104.225f, 104.046f, 102.023f, 100.000f, 98.1671f, 96.3342f, 96.0611f, 95.7880f, 92.2368f, 88.6856f, 89.3459f, 90.0062f, 89.8026f, 89.5991f, 88.6489f, 87.6987f, 85.4936f, 83.2886f, 83.4939f, 83.6992f, 81.8630f, 80.0268f, 80.1207f, 80.2146f, 81.2462f, 82.2778f, 80.2810f, 78.2842f, 74.0027f, 69.7213f, 70.6652f, 71.6091f, 72.9790f, 74.3490f, 67.9765f, 61.6040f, 65.7448f, 69.8856f, 72.4863f, 75.0870f, 69.3398f, 63.5927f, 55.0054f, 46.4182f, 56.6118f, 66.8054f, 65.0941f, 63.3828f, 63.8434f, 64.3040f, 61.8779f, 59.4519f, 55.7054f, 51.9590f, 54.6998f, 57.4406f, 58.8765f, 60.3125f }; // The wavelength w must be between 300nm and 830nm static float illuminantD65(float w) { auto i0 = size_t((w - CIE_D65_START) / CIE_D65_INTERVAL); uint2 indexBounds{i0, std::min(i0 + 1, CIE_D65_END)}; float2 wavelengthBounds = CIE_D65_START + float2{indexBounds} * CIE_D65_INTERVAL; float t = (w - wavelengthBounds.x) / (wavelengthBounds.y - wavelengthBounds.x); return lerp(CIE_D65[indexBounds.x], CIE_D65[indexBounds.y], t); } struct Sample { float w = 0.0f; // wavelength std::complex ior; // complex IOR, n + ik }; // For std::lower_bound bool operator<(const Sample& lhs, const Sample& rhs) { return lhs.w < rhs.w; } // The wavelength w must be between 360nm and 830nm static std::complex findSample(const std::vector& samples, float w) { auto i1 = std::lower_bound(samples.begin(), samples.end(), Sample{w, 0.0f + 0.0if}); auto i0 = i1 - 1; // Interpolate the complex IORs float t = (w - i0->w) / (i1->w - i0->w); float n = lerp(i0->ior.real(), i1->ior.real(), t); float k = lerp(i0->ior.imag(), i1->ior.imag(), t); return {n, k}; } // Fresnel equation for complex IORs at normal incidence static float fresnel(const std::complex& sample) { return (((sample - (1.0f + 0if)) * (std::conj(sample) - (1.0f + 0if))) / ((sample + (1.0f + 0if)) * (std::conj(sample) + (1.0f + 0if)))).real(); } // Fresnel equation for complex IORs at a specified angle static float fresnel(const std::complex& sample, float cosTheta) { float cosTheta2 = cosTheta * cosTheta; float sinTheta2 = 1.0f - cosTheta2; float eta2 = sample.real() * sample.real(); float etak2 = sample.imag() * sample.imag(); float t0 = eta2 - etak2 - sinTheta2; float a2plusb2 = sqrt(t0 * t0 + 4 * eta2 * etak2); float t1 = a2plusb2 + cosTheta2; float a = std::sqrt(0.5f * (a2plusb2 + t0)); float t2 = 2 * a * cosTheta; float Rs = (t1 - t2) / (t1 + t2); float t3 = cosTheta2 * a2plusb2 + sinTheta2 * sinTheta2; float t4 = t2 * sinTheta2; float Rp = Rs * (t3 - t4) / (t3 + t4); return 0.5f * (Rp + Rs); } static float3 linear_to_sRGB(float3 linear) noexcept { float3 sRGB{linear}; #pragma nounroll for (size_t i = 0; i < sRGB.size(); i++) { sRGB[i] = (sRGB[i] <= 0.0031308f) ? sRGB[i] * 12.92f : (powf(sRGB[i], 1.0f / 2.4f) * 1.055f) - 0.055f; } return sRGB; } static float3 XYZ_to_sRGB(const float3& v) { const mat3f XYZ_sRGB{ 3.2404542f, -0.9692660f, 0.0556434f, -1.5371385f, 1.8760108f, -0.2040259f, -0.4985314f, 0.0415560f, 1.0572252f }; return XYZ_sRGB * v; } struct Reflectance { float3 f0{0.0f}; float3 f82{0.0f}; }; static Reflectance computeColor(const std::vector& samples) { Reflectance xyz; float y = 0.0f; // We default to 81.7° but this can be specified by the user, so we use the // notation theta82/f81 in the code float cosTheta82 = std::cos(g_incidenceAngle * F_PI / 180.0f); // We need to evaluate the Fresnel equation at each spectral sample of // complex IOR over the visible spectrum. For each spectral sample, we // obtain a spectral reflectance sample. To find the RGB color at normal // incidence (f0, or baseColor in our material system), we must multiply // each sample by the CIE XYZ CMFs (color matching functions) and the // spectral power distribution of the desired illuminant. We choose the // standard illuminant D65 because we want to compute a color in the // sRGB color space. // We then sum (integrate) and normalize all the samples to obtain f0 // in the XYZ color space. From there, a simple color space conversion // yields a linear sRGB color. for (size_t i = 0; i < CIE_XYZ_COUNT; i++) { // Current wavelength float w = float(CIE_XYZ_START + i); // Find most appropriate CIE XYZ sample for the wavelength auto sample = findSample(samples, w); // Compute Fresnel reflectance at normal incidence float f0 = fresnel(sample); // Compute Fresnel reflectance at the peak angle for the Lazanyi error term float f82 = fresnel(sample, cosTheta82); // We need to multiply by the spectral power distribution of the illuminant float d65 = illuminantD65(w); xyz.f0 += f0 * CIE_XYZ[i] * d65; xyz.f82 += f82 * CIE_XYZ[i] * d65; y += CIE_XYZ[i].y * d65; } xyz.f0 /= y; xyz.f82 /= y; xyz.f0 = XYZ_to_sRGB(xyz.f0); xyz.f82 = XYZ_to_sRGB(xyz.f82); // Rescale values that are outside of the sRGB gamut (gold for instance) // We should provide an option to compute f0 in wide gamut color spaces if (any(greaterThan(xyz.f0, float3{1.0f}))) xyz.f0 *= 1.0f / max(xyz.f0); if (any(greaterThan(xyz.f82, float3{1.0f}))) xyz.f82 *= 1.0f / max(xyz.f82); return xyz; } static bool parseSpectralData(std::ifstream& in, std::vector& samples) { std::string value; in >> value; if (value != "wl") return false; in >> value; if (value != "n") return false; while (in.good()) { in >> value; if (value == "wl") { in >> value; if (value != "k") return false; break; } Sample sample = { 0 }; sample.w = std::strtof(value.c_str(), nullptr) * 1000.0f; // um to nm float n; in >> n; sample.ior.real(n); samples.push_back(sample); } size_t i = 0; while (in.good() && i < samples.size()) { Sample& sample = samples[i++]; float w; in >> w; if (w * 1000.0f != sample.w) return false; float k; in >> k; sample.ior.imag(k); } return true; } void printColor(const float3& linear) { float3 sRGB = linear_to_sRGB(saturate(linear)); std::cout << std::setfill(' ') << std::setw(12) << "linear: "; std::cout << std::fixed << std::setprecision(3) << linear.r << ", "; std::cout << std::fixed << std::setprecision(3) << linear.g << ", "; std::cout << std::fixed << std::setprecision(3) << linear.b; std::cout << std::endl; std::cout << std::setfill(' ') << std::setw(12) << "sRGB: "; std::cout << std::fixed << std::setprecision(3) << sRGB.r << ", "; std::cout << std::fixed << std::setprecision(3) << sRGB.g << ", "; std::cout << std::fixed << std::setprecision(3) << sRGB.b; std::cout << std::endl; std::cout << std::setfill(' ') << std::setw(12) << "sRGB: "; std::cout << std::setprecision(3) << int(sRGB.r * 255.99f) << ", "; std::cout << std::setprecision(3) << int(sRGB.g * 255.99f) << ", "; std::cout << std::setprecision(3) << int(sRGB.b * 255.99f); std::cout << std::endl; std::cout << std::setfill(' ') << std::setw(12) << "hex: "; std::cout << "#"; std::cout << std::hex << std::setfill('0') << std::setw(2) << int(sRGB.r * 255.99f); std::cout << std::hex << std::setfill('0') << std::setw(2) << int(sRGB.g * 255.99f); std::cout << std::hex << std::setfill('0') << std::setw(2) << int(sRGB.b * 255.99f); std::cout << std::endl; // Reset decimal numbers std::cout << std::dec; } int main(int argc, char* argv[]) { int optionIndex = handleArguments(argc, argv); int numArgs = argc - optionIndex; if (numArgs < 1) { printUsage(argv[0]); return 1; } utils::Path src(argv[optionIndex]); if (!src.exists()) { std::cerr << "The spectral data " << src << " does not exist." << std::endl; return 1; } std::ifstream in(src.c_str(), std::ifstream::in); if (in.is_open()) { std::vector samples; if (!parseSpectralData(in, samples)) { std::cerr << "Invalid spectral data file " << src << std::endl; return 1; } Reflectance reflectance = computeColor(samples); std::cout << "Material: " << src.getNameWithoutExtension() << std::endl; std::cout << std::endl; std::cout << "Reflectance at 0.0° (f0):" << std::endl; printColor(reflectance.f0); std::cout << std::endl; std::cout << "Reflectance at " << std::fixed << std::setprecision(1) << g_incidenceAngle << "°:" << std::endl; printColor(reflectance.f82); } else { std::cerr << "Could not open the source material " << src << std::endl; return 1; } return 0; }