@@ -176,6 +176,25 @@ void InitDefaultMaterials() {
176176 default_materials_initialized = true ;
177177}
178178
179+ boost::optional<IfcGeom::SurfaceStyle::ColorComponent> read_colour_component (const boost::optional<pt::ptree&> list) {
180+ if (!list) {
181+ return boost::none;
182+ }
183+ double rgb[3 ];
184+ int i = 0 ;
185+ for (pt::ptree::value_type &colour : list.get ()) {
186+ if (3 <= i) {
187+ throw std::runtime_error (" rgb array over 3 elements large" );
188+ }
189+ rgb[i] = colour.second .get_value <double >();
190+ i++;
191+ }
192+ if (i != 3 ) {
193+ throw std::runtime_error (" rgb array less than 3 elements large (was " + std::to_string (i) + " )" );
194+ }
195+ return IfcGeom::SurfaceStyle::ColorComponent (rgb[0 ], rgb[1 ], rgb[2 ]);
196+ }
197+
179198void IfcGeom::set_default_style (const std::string& json_file) {
180199 if (!default_materials_initialized) InitDefaultMaterials ();
181200 default_materials.clear ();
@@ -189,30 +208,17 @@ void IfcGeom::set_default_style(const std::string& json_file) {
189208
190209 pt::ptree material = material_pair.second ;
191210 boost::optional<pt::ptree&> diffuse = material.get_child_optional (" diffuse" );
192- if (diffuse) {
193- double rgb[3 ];
194- int i = 0 ;
195- for (pt::ptree::value_type &colour : diffuse.get ()) {
196- rgb[i] = colour.second .get_value <double >();
197- i++;
198- }
199- default_materials[name].Diffuse ().reset (IfcGeom::SurfaceStyle::ColorComponent (rgb[0 ], rgb[1 ], rgb[2 ]));
200- }
211+ default_materials[name].Diffuse () = read_colour_component (diffuse);
212+
201213 boost::optional<pt::ptree&> specular = material.get_child_optional (" specular" );
202- if (specular) {
203- double rgb[3 ];
204- int i = 0 ;
205- for (pt::ptree::value_type &colour : specular.get ()) {
206- rgb[i] = colour.second .get_value <double >();
207- i++;
208- }
209- default_materials[name].Specular ().reset (IfcGeom::SurfaceStyle::ColorComponent (rgb[0 ], rgb[1 ], rgb[2 ]));
214+ default_materials[name].Specular () = read_colour_component (specular);
215+
216+ if (material.get_child_optional (" specular-roughness" )) {
217+ default_materials[name].Specularity ().reset (1.0 / material.get <double >(" specular-roughness" ));
210218 }
211- boost::optional<double > specular_roughness = material.get_optional <double >(" specular-roughness" );
212- if (specular_roughness) {
213- default_materials[name].Specularity ().reset (1.0 / specular_roughness.get ());
219+ if (material.get_child_optional (" transparency" )) {
220+ default_materials[name].Transparency () = material.get <double >(" transparency" );
214221 }
215- default_materials[name].Transparency () = material.get_optional <double >(" transparency" );
216222 }
217223
218224 // Is "*" present? If yes, remove it and make it the default style.
0 commit comments