File: weighted_mean_matrix.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 (122 lines) | stat: -rw-r--r-- 3,853 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
/************************************************************************
 *
 * Copyright (C) 2024 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/>.
 *
 ***********************************************************************/

// cspell:ignore slerp lerp

#include "module/geometry/__/weighted_mean_matrix.hpp"

#include <core/com/signal.hxx>
#include <core/com/slots.hxx>

#include <geometry/data/matrix4.hpp>

#include <glm/common.hpp>

namespace sight::module::geometry
{

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

weighted_mean_matrix::weighted_mean_matrix() :
    filter(m_signals)
{
}

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

void weighted_mean_matrix::configuring(const config_t& /*unused*/)
{
}

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

void weighted_mean_matrix::starting()
{
}

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

service::connections_t weighted_mean_matrix::auto_connections() const
{
    return {
        {m_matrix_in, data::object::MODIFIED_SIG, sight::service::slots::UPDATE}
    };
}

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

void weighted_mean_matrix::updating()
{
    auto current_mat = m_matrix_in.const_lock();

    if(!m_enabled.value())
    {
        auto output = m_matrix_out.lock();
        output->deep_copy(current_mat.get_shared());
        output->signal<data::object::modified_signal_t>(data::object::MODIFIED_SIG)->async_emit();
        this->signal<signals::computed_t>(signals::COMPUTED)->async_emit();
        m_initialized = false;
        return;
    }

    glm::dmat4 glm_matrix  = sight::geometry::data::to_glm_mat(*current_mat);
    glm::dquat orientation = glm::quat_cast(glm_matrix);
    glm::dvec3 translation = glm_matrix[3];

    if(!m_initialized)
    {
        m_current_weighted_orientation = orientation;
        m_current_weighted_translation = translation;
        m_initialized                  = true;
    }

    SIGHT_ASSERT("Weight value (" << *m_weight << ") should be > 0 and <= 1", *m_weight > 0. && *m_weight <= 1.);

    // slerp rotation
    m_current_weighted_orientation = glm::slerp(m_current_weighted_orientation, orientation, *m_weight);

    // interpolate position
    m_current_weighted_translation = glm::mix(m_current_weighted_translation, translation, *m_weight);

    // recombine to a matrix
    glm::dmat4 glm_output = glm::mat4_cast(m_current_weighted_orientation);
    glm_output[3][0] = m_current_weighted_translation[0];
    glm_output[3][1] = m_current_weighted_translation[1];
    glm_output[3][2] = m_current_weighted_translation[2];

    {
        auto output = m_matrix_out.lock();

        sight::geometry::data::from_glm_mat(*output, glm_output);
        output->signal<data::object::modified_signal_t>(data::object::MODIFIED_SIG)->async_emit();
    }

    this->signal<signals::computed_t>(signals::COMPUTED)->async_emit();
}

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

void weighted_mean_matrix::stopping()
{
    m_initialized = false;
}

} // namespace sight::module::geometry