Loading...
Searching...
No Matches
fit.h
Go to the documentation of this file.
1#ifndef _BBM_FIT_H_
2#define _BBM_FIT_H_
3
4#include <map>
5#include <fstream>
6
7#include "util/string_util.h"
8#include "bbm/bsdf_ptr.h"
9#include "bbm/bsdf_import.h"
10
11/************************************************************************/
12/*! \file fit.h
13 \brief read and write BSDF fits to a 'fit' file.
14
15 FIT file format is a text file where:
16
17 + a comment starts with '#', and the remainder of the line is ignored.
18
19 + a valid BSDF start with a 'key' identifier string, followed by '='
20 followed by the BSDF in text format (e.g., as produced by bbm::toString).
21
22*************************************************************************/
23
24namespace bbm {
25 namespace io {
26
27 /********************************************************************/
28 /*! \brief Import a 'fit' file
29
30 \param filename = FIT filename to read from
31 \param data = std::map<std::string, bsdf_ptr> to store the fit data to
32
33 *********************************************************************/
34 template<typename CONF> requires concepts::config<CONF>
35 void importFIT(const std::string& filename, std::map<std::string, bsdf_ptr<CONF>>& data)
36 {
37 // open
38 std::ifstream ifs(filename.c_str());
39
40 // read each line and add to map
41 for(std::string line; std::getline(ifs, line);)
42 {
43 // remove comment
44 line =bbm::string::remove_comment(line, "#");
45 auto [key, bsdf] = bbm::string::split_eq(line);
46 if(!key.empty())
47 data.emplace(key, bbm::bsdf_import<CONF>(bsdf));
48 }
49
50 // Done.
51 }
52
53 /********************************************************************/
54 /*! \brief Export a 'fit' file
55
56 \param filename = FIT filename to write to
57 \param data = std::map<std::string, bsdf_ptr> of fitted data
58 \param comment = string of comment to add to the start of the fit file (ignored if empty)
59
60 *********************************************************************/
61 template<typename CONF> requires concepts::config<CONF>
62 void exportFIT(const std::string& filename, const std::map<std::string, bsdf_ptr<CONF>>& data, const std::string& comment)
63 {
64 // open
65 std::ofstream ofs(filename.c_str());
66
67 // write comments
68 std::stringstream ss(comment);
69 for(std::string line; std::getline(ss, line);)
70 ofs << "# " << line << std::endl;
71
72 // write each bsdf in data
73 for(const auto& bsdf : data)
74 ofs << bsdf.first << " = " << bbm::toString(bsdf.second) << std::endl;
75
76 // Done.
77 }
78
79 } // end io namespace
80} // end bbm namespace
81
82#endif /* _BBM_FIT_H_ */
A shared_ptr wrapper for bsdfs.
Import a BSDF from a string using the BBM_DEFAULT_BSDF_IMPORTER method specified in the configuration...
BSDF implementation of a BSDF model.
Definition: bsdf.h:30
config concept
Definition: config.h:31
void exportFIT(const std::string &filename, const std::map< std::string, bsdf_ptr< CONF > > &data, const std::string &comment)
Export a 'fit' file.
Definition: fit.h:62
void importFIT(const std::string &filename, std::map< std::string, bsdf_ptr< CONF > > &data)
Import a 'fit' file.
Definition: fit.h:35
std::pair< std::string, std::string > split_eq(const std::string &str)
split a string of the form "key = val" in key and value. If no '=', then return an empty key.
Definition: string_util.h:77
std::string remove_comment(const std::string &str, const std::string &comment_marker)
Remove comments from string.
Definition: string_util.h:53
Definition: aggregatebsdf.h:29
std::string toString(const T &)
toString alias
Definition: stringconvert.h:594
Helper method for processing strings.
Definition: bsdf_ptr.h:22