File: dictionary_reader.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (387 lines) | stat: -rw-r--r-- 13,094 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/************************************************************************
 *
 * Copyright (C) 2009-2024 IRCAD France
 * Copyright (C) 2012-2020 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

// cspell:ignore NOLINTNEXTLINE NOLINT

#include <iostream>
#include <fstream>
#include <vector>
#include <sstream>

#ifdef DEBUG
static std::stringstream spiritDebugStream;
  #define BOOST_SPIRIT_DEBUG_OUT spiritDebugStream // NOLINT(cppcoreguidelines-macro-usage): needed by Boost
  #define BOOST_SPIRIT_DEBUG                       // NOLINT(cppcoreguidelines-macro-usage): needed by Boost
#endif

#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/detail/case_conv.hpp>

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_eol.hpp>
#include <boost/phoenix.hpp>
#include <boost/phoenix/core.hpp>
#include <boost/phoenix/operator.hpp>
#include <boost/phoenix/bind.hpp>
#include <boost/phoenix/statement.hpp>
#include <boost/phoenix/stl.hpp>

#include <boost/fusion/include/adapt_struct.hpp>

#include <boost/fusion/container.hpp>
#include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/include/vector.hpp>

#include <core/exceptionmacros.hpp>

#include <data/color.hpp>
#include <data/structure_traits_dictionary.hpp>
#include <data/structure_traits.hpp>
#include <data/structure_traits_helper.hpp>

#include <core/runtime/path.hpp>

#include "io/__/reader/dictionary_reader.hpp"

namespace sight::io
{

struct line
{
    std::string type;
    double red {};
    double green {};
    double blue {};
    double alpha {};
    std::string category;
    std::string organ_class;
    std::string attachment;
    std::string native_exp;
    std::string native_exp_geo;
    std::string anatomic_region;
    std::string property_category;
    std::string property_type;
};

} // namespace sight::io

BOOST_FUSION_ADAPT_STRUCT(
    sight::io::line,
    (std::string, type)
    (double, red)
    (double, green)
    (double, blue)
    (double, alpha)
    (std::string, category)
    (std::string, organ_class)
    (std::string, attachment)
    (std::string, native_exp)
    (std::string, native_exp_geo)
    (std::string, anatomic_region)
    (std::string, property_category)
    (std::string, property_type)
)

//------------------------------------------------------------------------------

inline std::string trim(std::string& _s)
{
    return boost::algorithm::trim_copy(_s);
}

//------------------------------------------------------------------------------

/// Reformat string in the following way :first letter is uppercase and the rest is lowercase).
std::string reformat_string(std::string& _expr)
{
    std::string trim_str = boost::algorithm::trim_copy(_expr);
    std::string result   = boost::algorithm::to_upper_copy(trim_str.substr(0, 1))
                           + boost::algorithm::to_lower_copy(trim_str.substr(1));
    return result;
}

//------------------------------------------------------------------------------
/// Return the list of availabe value for the key of the map m.

template<typename map_t>
std::string get_values(const map_t& _m)
{
    std::stringstream str;
    using const_iterator = typename map_t::const_iterator;
    const_iterator iter = _m.begin();
    str << "( " << iter->first;
    for( ; iter != _m.end() ; ++iter)
    {
        str << ", " << iter->first;
    }

    str << ") ";
    return str.str();
}

//------------------------------------------------------------------------------

namespace sight::io
{

namespace qi    = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

template<typename Iterator>
struct line_parser : qi::grammar<Iterator,
                                 std::vector<line>()>
{
    line_parser() :
        line_parser::base_type(lines)
    {
        using qi::int_;
        using qi::lit;
        using qi::_1;
        using qi::double_;
        using qi::blank;
        using qi::alnum;
        using qi::omit;
        using ascii::char_;
        using boost::spirit::qi::eol;
        using boost::spirit::qi::eoi;
        using boost::phoenix::construct;
        namespace phx = boost::phoenix;

        error.clear();

        // NOLINTNEXTLINE(cppcoreguidelines-prefer-member-initializer)
        lines   = +(line[phx::push_back(qi::_val, qi::_1)] | comment) >> eoi;
        comment = *blank >> lit('#') >> *(char_ - eol) >> +qi::eol;

        line = trimmed_string >> lit(';')
               >> omit[*blank] >> lit('(')
               >> dbl >> lit(',')
               >> dbl >> lit(',')
               >> dbl >> lit(',')
               >> dbl
               >> lit(')') >> omit[*blank]
               >> lit(';')
               >> string_set >> lit(';')
               >> trimmed_string >> lit(';')
               >> trimmed_string >> lit(';')
               >> trimmed_string_expr >> lit(';')
               >> trimmed_string_expr >> lit(';')
               >> trimmed_string >> lit(';')
               >> trimmed_string >> lit(';')
               >> trimmed_string
               >> +qi::eol;

        trimmed_string = str[qi::_val = phx::bind(trim, qi::_1)];
        str            = *((alnum | char_("_"))[qi::_val += qi::_1] | blank[qi::_val += " "]);

        trimmed_string_expr = string_expr[qi::_val = phx::bind(trim, qi::_1)];
        string_expr         = *((alnum | char_("()_,.+-"))[qi::_val += qi::_1] | blank[qi::_val += " "]);

        string_set        = string_with_comma[qi::_val = phx::bind(trim, qi::_1)];
        string_with_comma = *((alnum | char_(",_"))[qi::_val += qi::_1] | blank[qi::_val += " "]);

        dbl = omit[*blank] >> double_ >> omit[*blank];

    #ifdef BOOST_SPIRIT_DEBUG
        BOOST_SPIRIT_DEBUG_NODE(comment);
        BOOST_SPIRIT_DEBUG_NODE(trimmed_string);
        BOOST_SPIRIT_DEBUG_NODE(trimmed_string_expr);
        BOOST_SPIRIT_DEBUG_NODE(string_set);
        BOOST_SPIRIT_DEBUG_NODE(dbl);
        BOOST_SPIRIT_DEBUG_NODE(line);
        BOOST_SPIRIT_DEBUG_NODE(lines);
        SIGHT_DEBUG(spiritDebugStream.str());
        spiritDebugStream.str(std::string());
    #endif

        qi::on_error<qi::fail>
        (
            line
            ,
            phx::ref((std::ostream&) error)
            << phx::val("Error! Expecting ")
            << qi::_4 // what failed?
            << phx::val(" here: \"")
            << phx::construct<std::string>(qi::_3, qi::_2) // iterators to error-pos, end
            << phx::val("\"")
            << std::endl
        );
    }

    qi::rule<Iterator, double()> dbl;
    qi::rule<Iterator, std::string()> str;
    qi::rule<Iterator, std::string()> string_expr;
    qi::rule<Iterator, std::string()> string_with_comma;

    qi::rule<Iterator, std::string()> comment;
    qi::rule<Iterator, std::string()> trimmed_string_expr;
    qi::rule<Iterator, std::string()> trimmed_string;
    qi::rule<Iterator, std::string()> string_set;

    qi::rule<Iterator, io::line()> line;
    qi::rule<Iterator, std::vector<io::line>()> lines;
    std::stringstream error;
};

namespace reader
{

//------------------------------------------------------------------------------

std::pair<bool, std::string> parse(std::string& _buf, std::vector<io::line>& _lines)
{
    using boost::spirit::ascii::space;
    using boost::spirit::ascii::blank;
    using boost::spirit::qi::eol;

    using boost::spirit::qi::phrase_parse;

    using iterator_type = std::string::const_iterator;
    using line_parser   = io::line_parser<iterator_type>;

    iterator_type iter = _buf.begin();
    iterator_type end  = _buf.end();

    line_parser grammar; // Our grammar

    bool result     = phrase_parse(iter, end, grammar, space - blank - eol, _lines);
    bool success    = result && (iter == end);
    std::string msg = grammar.error.str();
    return std::make_pair(success, msg);
}

//------------------------------------------------------------------------------

void dictionary_reader::read()
{
    std::filesystem::path path = this->get_file();

    SIGHT_INFO("[dictionary_reader::read] dictionary file: " << path.string());
    SIGHT_ASSERT("Empty path for dictionary file", !path.empty());

    // Reading of the file
    std::string buf;
    std::ifstream file;
    file.open(path.string().c_str(), std::ios::binary);

    std::string error_open = "Unable to open " + path.string();
    SIGHT_THROW_IF(error_open, !file.is_open());

    file.seekg(0, std::ios::end);
    const auto length = file.tellg();
    file.seekg(0, std::ios::beg);

    buf.resize(static_cast<std::size_t>(length));
    char* buffer = buf.data();

    file.read(buffer, static_cast<std::streamsize>(length));
    file.close();

    std::vector<io::line> dico_lines;
    std::pair<bool, std::string> result = parse(buf, dico_lines);

    std::string error = "Unable to parse " + path.string() + " : Bad file format.Error : " + result.second;
    SIGHT_THROW_IF(error, !result.first);

    // File the dictionary Structure
    data::structure_traits_dictionary::sptr struct_dico = get_concrete_object();

    for(io::line line : dico_lines)
    {
        data::structure_traits::sptr new_organ = std::make_shared<data::structure_traits>();
        new_organ->set_type(line.type);

        std::string class_reformated = reformat_string(
            line.organ_class
        );
        data::structure_traits_helper::class_translator_t::right_const_iterator str_class_iter =
            data::structure_traits_helper::CLASSTRANSLATOR.right.find(class_reformated);
        std::string available_values = get_values(data::structure_traits_helper::CLASSTRANSLATOR.right);
        error =
            std::string("Organ class ") + class_reformated + " isn't available. Authorized type are "
            + available_values;
        SIGHT_THROW_IF(error, !(str_class_iter != data::structure_traits_helper::CLASSTRANSLATOR.right.end()));
        new_organ->set_class(str_class_iter->second);

        new_organ->set_color(
            std::make_shared<data::color>(
                static_cast<float>(line.red) / 255.0F,
                static_cast<float>(line.green) / 255.0F,
                static_cast<float>(line.blue) / 255.0F,
                static_cast<float>(line.alpha) / 100.0F
            )
        );
        std::vector<std::string> categorylist;
        boost::algorithm::split(categorylist, line.category, boost::algorithm::is_any_of(","));
        data::structure_traits::category_container_t categories;
        for(std::string category : categorylist)
        {
            std::string cat_reformated = reformat_string(
                category
            );
            data::structure_traits_helper::category_translator_t::right_const_iterator str_category_iter =
                data::structure_traits_helper::CATEGORYTRANSLATOR.right.find(cat_reformated);
            available_values = get_values(
                data::structure_traits_helper::CATEGORYTRANSLATOR.right
            );
            error =
                std::string("Category ") + cat_reformated + " isn't available. Authorized type are "
                + available_values;
            SIGHT_THROW_IF(
                error,
                !(str_category_iter != data::structure_traits_helper::CATEGORYTRANSLATOR.right.end())
            );
            categories.push_back(str_category_iter->second);
        }

        new_organ->set_categories(categories);
        new_organ->set_attachment_type(line.attachment);
        new_organ->set_native_exp(line.native_exp);
        new_organ->set_native_geometric_exp(line.native_exp_geo);
        new_organ->set_anatomic_region(line.anatomic_region);
        new_organ->set_property_category(line.property_category);
        new_organ->set_property_type(line.property_type);
        struct_dico->add_structure(new_organ);
    }
}

//------------------------------------------------------------------------------

std::string dictionary_reader::extension() const
{
    return ".dic";
}

//------------------------------------------------------------------------------

std::filesystem::path dictionary_reader::get_default_dictionary_path()
{
    return core::runtime::get_library_resource_file_path("io/OrganDictionary.dic");
}

//------------------------------------------------------------------------------

} // namespace reader

} // namespace sight::io