File: get_mesh.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 (151 lines) | stat: -rw-r--r-- 5,582 bytes parent folder | download
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
/************************************************************************
 *
 * Copyright (C) 2022-2025 IRCAD France
 *
 * 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/>.
 *
 ***********************************************************************/

#include "get_mesh.hpp"

#include <regex>

namespace sight::module::data
{

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

void get_mesh::configuring()
{
    service::config_t config  = this->get_config();
    const config_t config_out = config.get_child("out");
    const auto key_cfg        = config_out.equal_range("key");
    size_t output_var_index   = 0;

    // All the configuration is parsed
    for(auto it_cfg = key_cfg.first ; it_cfg != key_cfg.second ; ++it_cfg, ++output_var_index)
    {
        // A configuration can be a constraint either on the index or on the type
        auto type_cfg  = it_cfg->second.get_child_optional("<xmlattr>.type");
        auto index_cfg = it_cfg->second.get_child_optional("<xmlattr>.index");

        if(index_cfg)
        {
            // There is a constraint on the index.
            // The index of the mesh required from the reconstructionDB and the index of the output var are stored.
            m_index_configurations.push_back(
                {
                    index_cfg->get_value<std::size_t>(),
                    output_var_index
                });
        }
        else if(type_cfg)
        {
            // There is a constraint on the index.
            // The type, the name matching (if there is one) and the index of the output var are stored
            const auto matching_cfg = it_cfg->second.get_child_optional("<xmlattr>.matching");

            m_type_configuration.push_back(
                {
                    type_cfg->get_value<std::string>(),
                    matching_cfg ? matching_cfg->get<std::string>("") : "",
                    output_var_index
                });
        }
    }
}

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

void get_mesh::starting()
{
}

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

void get_mesh::updating()
{
    //Get the reconstruction database from which the mesh are extracted
    auto model_series = m_model_series.lock();
    if(model_series == nullptr)
    {
        SIGHT_THROW_EXCEPTION(sight::data::exception("Missing input models series"));
    }

    const sight::data::model_series::reconstruction_vector_t recs = model_series->get_reconstruction_db();

    // do all index requests
    for(index_config index_cfg : m_index_configurations)
    {
        // Get the mesh from the reconstructionDB which is located at the position stored in
        // indexCfg.inputIndex
        if(index_cfg.input_index >= recs.size())
        {
            SIGHT_THROW_EXCEPTION(sight::data::exception("Mesh index is out of bound"));
        }

        const auto mesh = model_series->get_reconstruction_db()[index_cfg.input_index]->get_mesh();

        // put the mesh in the appropriate output variable
        m_mesh[index_cfg.output_index] = mesh;
    }

    // do all type requests
    for(const type_config& type_cfg : m_type_configuration)
    {
        //go through the whole reconstructionDB, and check if a mesh which respects the type and regex is found.
        bool found = false;
        for(const auto& reconstruction : model_series->get_reconstruction_db())
        {
            // check if the type of the reconstruction corresponds to the requested one.
            if(reconstruction->get_structure_type() == type_cfg.type)
            {
                // the reconstruction has the right type. Check if the name match the regex, if there is one.
                if(type_cfg.matching.empty()
                   || std::regex_match(reconstruction->get_organ_name(), std::regex(type_cfg.matching)))
                {
                    //the type and name are appropriate. It is the mesh required.
                    auto mesh = reconstruction->get_mesh();

                    // put the mesh in the appropriate output variable.
                    m_mesh[type_cfg.output_index] = mesh;

                    //When the appropriate mesh is found for a given type-regex, there is no need to go further in the
                    // reconstructionDB.
                    found = true;
                    break;
                }
            }
        }

        // if the whole reconstructionDB has been parsed, and no mesh corresponds to the given type/name, throw
        // exception.
        if(!found)
        {
            SIGHT_THROW_EXCEPTION(sight::data::exception("Type of mesh does not match"));
        }
    }
}

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

void get_mesh::stopping()
{
}

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

} // namespace sight::module::data