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
|
/************************************************************************
*
* Copyright (C) 2018-2024 IRCAD France
* Copyright (C) 2018-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/>.
*
***********************************************************************/
#include "module/geometry/__/matrix_list.hpp"
#include <core/com/signal.hxx>
#include <core/com/slots.hxx>
#include <iomanip>
namespace sight::module::geometry
{
//-----------------------------------------------------------------------------
static const core::com::signals::key_t MATRIX_ADDED_SIG = "matrixAdded";
static const core::com::signals::key_t MATRIX_REMOVED_SIG = "matrixRemoved";
static const core::com::slots::key_t SELECT_MATRIX_SLOT = "selectMatrix";
static const core::com::slots::key_t REMOVE_MATRIX_SLOT = "removeMatrix";
//-----------------------------------------------------------------------------
matrix_list::matrix_list() noexcept
{
new_signal<matrix_added_signal_t>(MATRIX_ADDED_SIG);
new_signal<matrix_removed_signal_t>(MATRIX_REMOVED_SIG);
new_slot(SELECT_MATRIX_SLOT, &matrix_list::select_matrix, this);
new_slot(REMOVE_MATRIX_SLOT, &matrix_list::remove_matrix, this);
}
//-----------------------------------------------------------------------------
matrix_list::~matrix_list() noexcept =
default;
//-----------------------------------------------------------------------------
void matrix_list::starting()
{
// get inputs
[[maybe_unused]] const std::size_t num_matrices = m_input_vector.size();
[[maybe_unused]] const std::size_t num_selected = m_selected_vector.size();
[[maybe_unused]] const std::size_t num_output = m_output_vector.size();
SIGHT_ASSERT(
"the numbers of matrices, vectors and selected matrices should be the same",
num_matrices == num_output && num_matrices == num_selected
);
SIGHT_ASSERT(
"the numbers of matrices, vectors and selected matrices should be superior to one",
num_matrices > 0 && num_output > 0 && num_selected > 0
);
}
//-----------------------------------------------------------------------------
void matrix_list::stopping()
{
}
//-----------------------------------------------------------------------------
void matrix_list::configuring()
{
}
//-----------------------------------------------------------------------------
void matrix_list::updating()
{
// Get the computed matrix from input group vector
data::vector::sptr computed_vector;
if(m_input_vector.size() > 0)
{
for(std::size_t i = 0 ; i < m_input_vector.size() ; ++i)
{
data::matrix4::sptr computed_matrix = std::make_shared<data::matrix4>();
{
const auto input = m_input_vector[i].lock();
computed_matrix->deep_copy(input.get_shared());
}
// Fill the output vector group with the matrix
auto computed_vector_ptr = m_output_vector[i].lock();
computed_vector = computed_vector_ptr.get_shared();
if(nullptr == computed_vector)
{
computed_vector = std::make_shared<data::vector>();
}
computed_vector->push_back(computed_matrix);
this->set_output(computed_vector, VECTOR_INOUT, i);
auto sig = computed_vector->signal<data::vector::added_signal_t>
(data::vector::ADDED_OBJECTS_SIG);
sig->async_emit(computed_vector->get_content());
}
}
// create string containing matrix values
std::string str;
data::matrix4::sptr computed_matrix = std::make_shared<data::matrix4>();
{
const auto input = m_input_vector[0].lock();
computed_matrix->deep_copy(input.get_shared());
}
for(std::size_t i = 0 ; i < 4 ; ++i)
{
str += "[ ";
for(std::size_t j = 0 ; j < 4 ; j++)
{
std::ostringstream out;
out << std::setprecision(3) << (*computed_matrix)[i * 4 + j];
str += out.str();
str += " ";
}
str += "]";
if(i < 3)
{
str += ", ";
}
}
// notify
const int index = static_cast<int>(computed_vector->size() - 1);
// push the selected matrix
this->select_matrix(index);
this->signal<matrix_added_signal_t>(MATRIX_ADDED_SIG)->async_emit(index, str);
}
//-----------------------------------------------------------------------------
void matrix_list::select_matrix(int _index)
{
for(std::size_t i = 0 ; i < m_input_vector.size() ; ++i)
{
auto selected_matrix = m_selected_vector[i].lock();
auto output_vector = m_output_vector[i].lock();
selected_matrix->deep_copy(std::dynamic_pointer_cast<data::matrix4>((*output_vector)[std::size_t(_index)]));
auto sig = selected_matrix->signal<data::matrix4::modified_signal_t>(data::matrix4::MODIFIED_SIG);
sig->async_emit();
}
}
//-----------------------------------------------------------------------------
void matrix_list::remove_matrix(int _index)
{
if(m_input_vector.size() > 0)
{
for(std::size_t i = 0 ; i < m_input_vector.size() ; ++i)
{
auto output_vector = m_output_vector[i].lock();
output_vector->erase(output_vector->begin() + _index);
auto sig = output_vector->signal<data::vector::removed_signal_t>
(data::vector::REMOVED_OBJECTS_SIG);
sig->async_emit(output_vector->get_content());
}
this->signal<matrix_removed_signal_t>(MATRIX_REMOVED_SIG)->async_emit(_index);
}
}
//-----------------------------------------------------------------------------
} // namespace sight::module::geometry
|