Skip to content

Commit

Permalink
ScatteringData can be now loaded from single row and then converted i…
Browse files Browse the repository at this point in the history
…nto square matrix.
  • Loading branch information
vidanovic committed Oct 25, 2024
1 parent 27e71f8 commit 4a9eb0d
Show file tree
Hide file tree
Showing 3 changed files with 206 additions and 12 deletions.
43 changes: 31 additions & 12 deletions src/bsdfdata/Serializers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ namespace BSDFData
}

template<typename NodeAdapter>
NodeAdapter & operator<<(NodeAdapter & node, const BSDFData::LengthWithCavity & lengthWithCavity)
NodeAdapter & operator<<(NodeAdapter & node,
const BSDFData::LengthWithCavity & lengthWithCavity)
{
using FileParse::operator<<; // operators for basic C++ types are in the FileParse
// namespace
Expand Down Expand Up @@ -389,25 +390,43 @@ namespace BSDFData

return result;
}

std::vector<std::vector<double>> convertToSquareMatrix(std::vector<double> const & v)
{
double intPart;
if(std::modf(std::sqrt(v.size()), &intPart) != 0)
{
throw std::runtime_error("Non-square matrix");
}
size_t size = static_cast<size_t>(intPart);
std::vector<double> inner;
inner.resize(size);
std::vector<std::vector<double>> m(size, inner);
for(size_t row = 0; row < size; ++row)
{
for(size_t col = 0; col < size; ++col)
{
m[row][col] = v[row * size + col];
}
}
return m;
}
} // namespace

/// Parses a string representation of scattering data into a vector of vectors of doubles.
inline BSDFData::ScatteringData parseScatteringData(const std::string & value)
{
BSDFData::ScatteringData scatteringData;
std::vector<double> data;
std::istringstream inputStream(value);
std::string line;

while(std::getline(inputStream, line))
{
auto row = parseRow(line);
if(!row.empty())
{
scatteringData.push_back(row);
}
data.insert(data.end(), row.begin(), row.end());
}

return scatteringData;
return convertToSquareMatrix(data);
}

template<typename NodeAdapter>
Expand All @@ -430,10 +449,10 @@ namespace BSDFData
std::ostringstream rowStream;

// Transform each element into a formatted string and join them with ", "
std::transform(row.begin(), row.end(), std::ostream_iterator<std::string>(rowStream, ", "),
[](double value) {
return FileParse::formatDouble(value, 5, 1, 0.99);
});
std::transform(row.begin(),
row.end(),
std::ostream_iterator<std::string>(rowStream, ", "),
[](double value) { return FileParse::formatDouble(value, 5, 1, 0.99); });

return rowStream.str();
}
Expand Down Expand Up @@ -587,4 +606,4 @@ namespace BSDFData
return node;
}

} // namespace BSDFXML
} // namespace BSDFData
38 changes: 38 additions & 0 deletions test/bsdf_xml_file_serialization.unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,41 @@ TEST(BSDFXMLFileSerialization, Save2011SA1Small)

std::filesystem::remove(temp_path);
}

TEST(BSDFXMLFileSerialization, Save2011SA1Small_SingleColumnScatteringData)
{
SCOPED_TRACE("Begin Test: Save 2011-SA1-Small-CommaSeparated-SingleColumnScattering.XML");
std::filesystem::path product_path(TEST_DATA_DIR);
product_path /= "products";
product_path /= "2011-SA1-Small-CommaSeparated-SingleColumnScattering.XML";

// Load the XML file to create a product object
auto product = BSDFData::loadWindowElementFromFile(product_path.string());
ASSERT_TRUE(product.has_value());

// Save the product object to a temporary file
std::filesystem::path temp_path = "temp_2011-SA1-Small.xml";
//std::filesystem::temp_directory_path() / "temp_2011-SA1-Small.xml";
ASSERT_TRUE(BSDFData::saveToFile(*product, temp_path.string()) == 0);

// Load the serialized file back
auto serialized_product = BSDFData::loadWindowElementFromFile(temp_path.string());
ASSERT_TRUE(serialized_product.has_value());

// Compare the original product with the serialized product
EXPECT_EQ(product->windowElementType, serialized_product->windowElementType);
Helper::compareMaterial(product->optical.layer.material.value(),
serialized_product->optical.layer.material.value());
Helper::compareDataDefinition(product->optical.layer.dataDefinition.value(),
serialized_product->optical.layer.dataDefinition.value());

ASSERT_EQ(product->optical.layer.wavelengthData.size(),
serialized_product->optical.layer.wavelengthData.size());
for(size_t i = 0; i < product->optical.layer.wavelengthData.size(); ++i)
{
Helper::compareWavelengthData(product->optical.layer.wavelengthData[i],
serialized_product->optical.layer.wavelengthData[i]);
}

std::filesystem::remove(temp_path);
}
137 changes: 137 additions & 0 deletions test/products/2011-SA1-Small-CommaSeparated-SingleColumnScattering.XML
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="UTF-8"?>
<WindowElement xmlns="http://windows.lbl.gov" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://windows.lbl.gov
BSDF-v1.4.xsd">
<WindowElementType>System</WindowElementType>
<Optical>
<Layer>
<Material>
<Name>Satine 5500 5%, White Pearl </Name>
<Manufacturer>Nysan</Manufacturer>
<Thickness unit="Millimeter">1</Thickness>
<DeviceType>Other</DeviceType>
<ThermalConductivity>0.15</ThermalConductivity>
<EmissivityFront>0.79626</EmissivityFront>
<EmissivityBack>0.79626</EmissivityBack>
<TIR>0.10916</TIR>
<PermeabilityFactor>0.049855</PermeabilityFactor>
<AERCAcceptance>@</AERCAcceptance>
</Material>
<DataDefinition>
<IncidentDataStructure>Columns</IncidentDataStructure>
<AngleBasis>
<AngleBasisName>LBNL/Klems Full</AngleBasisName>
<AngleBasisBlock>
<Theta>0</Theta>
<ThetaBounds>
<LowerTheta>0</LowerTheta>
<UpperTheta>5</UpperTheta>
</ThetaBounds>
<nPhis>1</nPhis>
</AngleBasisBlock>
<AngleBasisBlock>
<Theta>10</Theta>
<ThetaBounds>
<LowerTheta>5</LowerTheta>
<UpperTheta>15</UpperTheta>
</ThetaBounds>
<nPhis>8</nPhis>
</AngleBasisBlock>
<AngleBasisBlock>
<Theta>20</Theta>
<ThetaBounds>
<LowerTheta>15</LowerTheta>
<UpperTheta>25</UpperTheta>
</ThetaBounds>
<nPhis>16</nPhis>
</AngleBasisBlock>
<AngleBasisBlock>
<Theta>30</Theta>
<ThetaBounds>
<LowerTheta>25</LowerTheta>
<UpperTheta>35</UpperTheta>
</ThetaBounds>
<nPhis>20</nPhis>
</AngleBasisBlock>
<AngleBasisBlock>
<Theta>40</Theta>
<ThetaBounds>
<LowerTheta>35</LowerTheta>
<UpperTheta>45</UpperTheta>
</ThetaBounds>
<nPhis>24</nPhis>
</AngleBasisBlock>
<AngleBasisBlock>
<Theta>50</Theta>
<ThetaBounds>
<LowerTheta>45</LowerTheta>
<UpperTheta>55</UpperTheta>
</ThetaBounds>
<nPhis>24</nPhis>
</AngleBasisBlock>
<AngleBasisBlock>
<Theta>60</Theta>
<ThetaBounds>
<LowerTheta>55</LowerTheta>
<UpperTheta>65</UpperTheta>
</ThetaBounds>
<nPhis>24</nPhis>
</AngleBasisBlock>
<AngleBasisBlock>
<Theta>70</Theta>
<ThetaBounds>
<LowerTheta>65</LowerTheta>
<UpperTheta>75</UpperTheta>
</ThetaBounds>
<nPhis>16</nPhis>
</AngleBasisBlock>
<AngleBasisBlock>
<Theta>82.5</Theta>
<ThetaBounds>
<LowerTheta>75</LowerTheta>
<UpperTheta>90</UpperTheta>
</ThetaBounds>
<nPhis>12</nPhis>
</AngleBasisBlock>
</AngleBasis>
</DataDefinition>
<WavelengthData>
<LayerNumber>System</LayerNumber>
<Wavelength unit="Integral">Visible</Wavelength>
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
<WavelengthDataBlock>
<WavelengthDataDirection>Transmission Front</WavelengthDataDirection>
<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
<RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
<ScatteringDataType>BTDF</ScatteringDataType>
<ScatteringData>
2.063833,
0.014938,

0.014954,
2.027935,
</ScatteringData>
</WavelengthDataBlock>
</WavelengthData>
<WavelengthData>
<LayerNumber>System</LayerNumber>
<Wavelength unit="Integral">Visible</Wavelength>
<SourceSpectrum>CIE Illuminant D65 1nm.ssp</SourceSpectrum>
<DetectorSpectrum>ASTM E308 1931 Y.dsp</DetectorSpectrum>
<WavelengthDataBlock>
<WavelengthDataDirection>Transmission Back</WavelengthDataDirection>
<ColumnAngleBasis>LBNL/Klems Full</ColumnAngleBasis>
<RowAngleBasis>LBNL/Klems Full</RowAngleBasis>
<ScatteringDataType>BTDF</ScatteringDataType>
<ScatteringData>
2.063833,
0.014938,

0.014954,
2.027935,
</ScatteringData>
</WavelengthDataBlock>
</WavelengthData>
</Layer>
</Optical>
</WindowElement>

0 comments on commit 4a9eb0d

Please sign in to comment.