//== INCLUDES =================================================================


#include "Energy_term_landmarks_skinned_twoscans.h"
#include <graphene/surface_mesh/algorithms/templatefitting/utility/my_helper.h>

#include "../settings.h"


//== NAMESPACE ================================================================


namespace graphene {
namespace surface_mesh {


//== IMPLEMENTATION ===========================================================

#if 0

Energy_term_landmarks_skinned_twoscans::
Energy_term_landmarks_skinned_twoscans(const Energy_term_data& energy_term_data, double weight) :
    Energy_term(energy_term_data, weight, "Landmarks Skinned Two Scans")
{
}


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


Energy_term_landmarks_skinned_twoscans::
~Energy_term_landmarks_skinned_twoscans()
{}


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


void
Energy_term_landmarks_skinned_twoscans::
pre_minimize_action()
{
    if (!energy_term_data_.character)
    {
        std::cerr << "[ERROR] - Energy_term_landmarks_skinned_twoscans::!energy_term_data_.character - EXIT!" << std::endl;
        exit(1);
    }


    Surface_mesh::Vertex_property<Vec4f> vdepends = energy_term_data_.template_mesh->get_vertex_property<Vec4f>("v:skin_depend");
    Surface_mesh::Vertex_property<Vec4f> vweights = energy_term_data_.template_mesh->get_vertex_property<Vec4f>("v:skin_weight");
    Surface_mesh::Vertex_property<Vec4f> vdepends2 = energy_term_data_.template_mesh->get_vertex_property<Vec4f>("v:skin_depend2");
    Surface_mesh::Vertex_property<Vec4f> vweights2 = energy_term_data_.template_mesh->get_vertex_property<Vec4f>("v:skin_weight2");

    if (!(vdepends && vweights && vdepends2 && vweights2))
    {
        std::cerr << "[ERROR] - Energy_term_landmarks_skinned_twoscans::pre_minimize_action() - EXIT!" << std::endl;
        exit(1);
    }

    const std::vector<Mat4f>* pose_vector_front_body = energy_term_data_.pose_vector_front_body;
    const std::vector<Mat4f>* pose_vector_back_body  = energy_term_data_.pose_vector_back_body;
    if (!pose_vector_front_body || !pose_vector_back_body)
    {
        std::cerr << "[ERROR] - Energy_term_landmarks_skinned_twoscans::pre_minimize_action() - !pose_vector_front_body || !pose_vector_back_body" << std::endl;
        exit(1);
    }
}


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


double
Energy_term_landmarks_skinned_twoscans::
evaluate()
{
    const Landmarks_manager& landmarks_manager = energy_term_data_.landmarks_manager;
    const Point_set*      point_set_front_body = energy_term_data_.point_set;
    const Point_set*       point_set_back_body = energy_term_data_.point_set_back_body;
    const Surface_mesh* template_mesh = energy_term_data_.template_mesh;

    Surface_mesh::Vertex_property<Vec4f> vdepends = template_mesh->get_vertex_property<Vec4f>("v:skin_depend");
    Surface_mesh::Vertex_property<Vec4f> vweights = template_mesh->get_vertex_property<Vec4f>("v:skin_weight");
    Surface_mesh::Vertex_property<Vec4f> vdepends2 = template_mesh->get_vertex_property<Vec4f>("v:skin_depend2");
    Surface_mesh::Vertex_property<Vec4f> vweights2 = template_mesh->get_vertex_property<Vec4f>("v:skin_weight2");
    if (!(vdepends && vweights && vdepends2 && vweights2))
    {
        std::cerr << "[ERROR] - Energy_term_landmarks_skinned_twoscans::evaluate() - EXIT!" << std::endl;
        exit(1);
    }

    if (!landmarks_manager.check_dimension(point_set_front_body, template_mesh, "m:landmarks_front_body") ||
        !landmarks_manager.check_dimension(point_set_back_body, template_mesh, "m:landmarks_back_body"))
    {
        std::cerr << "Energy_term_landmarks_skinned_twoscans::evaluate(): [ERROR] Number of landmarks do not match for front_body and/or back_body." << std::endl;
        return DBL_MAX;
    }

    std::vector<Point> lm_ps_front_body;
    landmarks_manager.get_landmarks_point_set_stdvec(point_set_front_body, lm_ps_front_body);
    std::vector<Point> lm_templ_front_body;
    landmarks_manager.get_landmarks_template_stdvec(template_mesh, lm_templ_front_body, "m:landmarks_front_body");
    std::vector<Surface_mesh::Vertex> tm_landmarks_front_body;
    landmarks_manager.get_indices_template(template_mesh, tm_landmarks_front_body, "m:landmarks_front_body");

    Surface_mesh::Vertex_property<double> local_landmark_weight_front_body = template_mesh->get_vertex_property<double>("v:landmark_weight_front_body");
    const std::vector<Surface_mesh::Vertex>& landmark_prop_front_body = template_mesh->get_mesh_property<std::vector<Surface_mesh::Vertex> >("m:landmarks_front_body")[0];

    std::vector<Point> lm_ps_back_body;
    landmarks_manager.get_landmarks_point_set_stdvec(point_set_back_body, lm_ps_back_body);
    std::vector<Point> lm_templ_back_body;
    landmarks_manager.get_landmarks_template_stdvec(template_mesh, lm_templ_back_body, "m:landmarks_back_body");
    std::vector<Surface_mesh::Vertex> tm_landmarks_back_body;
    landmarks_manager.get_indices_template(template_mesh, tm_landmarks_back_body, "m:landmarks_back_body");

    Surface_mesh::Vertex_property<double> local_landmark_weight_back_body = template_mesh->get_vertex_property<double>("v:landmark_weight_back_body");
    const std::vector<Surface_mesh::Vertex>& landmark_prop_back_body = template_mesh->get_mesh_property<std::vector<Surface_mesh::Vertex> >("m:landmarks_back_body")[0];

    const std::vector<Mat4f>* pose_vector_front_body = energy_term_data_.pose_vector_front_body;
    surface_mesh::set_pose_vector(*pose_vector_front_body, energy_term_data_.character->skeleton());
    const std::vector<Mat4f> skinning_tpose_to_scanpose_front_body = energy_term_data_.character->skeleton().skinning_matrices_;
    if (skinning_tpose_to_scanpose_front_body.size() == 0)
    {
        std::cerr << "[ERROR] - Energy_term_pca_landmarks_skinned_twoscans::evaluate() - skinning_tpose_to_scanpose_front_body.size(): " << skinning_tpose_to_scanpose_front_body.size() << std::endl;
        exit(1);
    }

    const std::vector<Mat4f>* pose_vector_back_body = energy_term_data_.pose_vector_back_body;
    surface_mesh::set_pose_vector(*pose_vector_back_body, energy_term_data_.character->skeleton());
    const std::vector<Mat4f> skinning_tpose_to_scanpose_back_body = energy_term_data_.character->skeleton().skinning_matrices_;
    if (skinning_tpose_to_scanpose_back_body.size() == 0)
    {
        std::cerr << "[ERROR] - Energy_term_pca_landmarks_skinned_twoscans::evaluate() - skinning_tpose_to_scanpose_back_body.size(): " << skinning_tpose_to_scanpose_back_body.size() << std::endl;
        exit(1);
    }

    double error_match_landmarks = 0.0;

    const unsigned int L_front(lm_ps_front_body.size());
    const unsigned int L_back(lm_ps_back_body.size());
    const unsigned int L_total = L_front + L_back;


    for (unsigned int i = 0; i < L_front; ++i)
    {
        double local_weight = 1.0;
        if (local_landmark_weight_front_body)
        {
            local_weight = local_landmark_weight_front_body[landmark_prop_front_body[i]];
            if (local_weight < 1e-5)
                continue;
            local_weight = sqrt(local_weight);
        }

        const int idx = tm_landmarks_front_body[i].idx();

        Surface_mesh::Vertex v_idx(idx);
        int dep0 = (int)vdepends[v_idx][0];
        float w0 = vweights[v_idx][0];
        int dep1 = (int)vdepends[v_idx][1];
        float w1 = vweights[v_idx][1];
        int dep2 = (int)vdepends[v_idx][2];
        float w2 = vweights[v_idx][2];
        int dep3 = (int)vdepends[v_idx][3];
        float w3 = vweights[v_idx][3];
        int dep4 = (int)vdepends2[v_idx][0];
        float w4 = vweights2[v_idx][0];
        int dep5 = (int)vdepends2[v_idx][1];
        float w5 = vweights2[v_idx][1];
        int dep6 = (int)vdepends2[v_idx][2];
        float w6 = vweights2[v_idx][2];
        int dep7 = (int)vdepends2[v_idx][3];
        float w7 = vweights2[v_idx][3];

        Mat4f blend_mat;
        blend_mat  = w0 * skinning_tpose_to_scanpose_front_body[dep0];
        blend_mat += w1 * skinning_tpose_to_scanpose_front_body[dep1];
        blend_mat += w2 * skinning_tpose_to_scanpose_front_body[dep2];
        blend_mat += w3 * skinning_tpose_to_scanpose_front_body[dep3];
        blend_mat += w4 * skinning_tpose_to_scanpose_front_body[dep4];
        blend_mat += w5 * skinning_tpose_to_scanpose_front_body[dep5];
        blend_mat += w6 * skinning_tpose_to_scanpose_front_body[dep6];
        blend_mat += w7 * skinning_tpose_to_scanpose_front_body[dep7];

        Point the_point = affine_transform(blend_mat, lm_templ_front_body[i]);

        double u = norm( lm_ps_front_body[i] - the_point );
        if ( robustness_parameters_.solver_type == SOLVER_TYPE_L2 )
        {
            error_match_landmarks += u * u * local_weight;
        }
        else if ( robustness_parameters_.solver_type == SOLVER_TYPE_L1 )
        {
            error_match_landmarks += std::pow( u, robustness_parameters_.parameter ) * local_weight;
        }
        else if ( robustness_parameters_.solver_type == SOLVER_TYPE_HUBER )
        {
            std::cerr << "ERSTMAL NICHT " << std::endl; exit(1);
            double huber_thr = robustness_parameters_.parameter;
            if ( u > huber_thr )
            {
                error_match_landmarks += ( huber_thr*u - huber_thr*huber_thr/2.0 ) * local_weight;
            }
            else
            {
                error_match_landmarks += ( u*u/2.0 ) * local_weight;
            }
        }
        else if ( robustness_parameters_.solver_type == SOLVER_TYPE_GEMANMCCLURE )
        {
            std::cerr << "ERSTMAL NICHT " << std::endl; exit(1);
            double scale_parameter = robustness_parameters_.parameter;
//                error_match_landmarks += ( u*u / (scale_parameter*scale_parameter + u*u) ); // VERSION 2
            error_match_landmarks += ( ( u*u / ( 2.0 * ( 1.0 + (u*u/(scale_parameter*scale_parameter)) ) ) ) ) * local_weight; // VERSION 3
        }
        else // default to L2
        {
            error_match_landmarks += u * u * local_weight;
        }
    }


    for (unsigned int i = 0; i < L_back; ++i)
    {
        double local_weight = 1.0;
        if (local_landmark_weight_back_body)
        {
            local_weight = local_landmark_weight_back_body[landmark_prop_back_body[i]];
            if (local_weight < 1e-5)
                continue;
            local_weight = sqrt(local_weight);
        }

        const int idx = tm_landmarks_back_body[i].idx();

        Surface_mesh::Vertex v_idx(idx);
        int dep0 = (int)vdepends[v_idx][0];
        float w0 = vweights[v_idx][0];
        int dep1 = (int)vdepends[v_idx][1];
        float w1 = vweights[v_idx][1];
        int dep2 = (int)vdepends[v_idx][2];
        float w2 = vweights[v_idx][2];
        int dep3 = (int)vdepends[v_idx][3];
        float w3 = vweights[v_idx][3];
        int dep4 = (int)vdepends2[v_idx][0];
        float w4 = vweights2[v_idx][0];
        int dep5 = (int)vdepends2[v_idx][1];
        float w5 = vweights2[v_idx][1];
        int dep6 = (int)vdepends2[v_idx][2];
        float w6 = vweights2[v_idx][2];
        int dep7 = (int)vdepends2[v_idx][3];
        float w7 = vweights2[v_idx][3];

        Mat4f blend_mat;
        blend_mat  = w0 * skinning_tpose_to_scanpose_back_body[dep0];
        blend_mat += w1 * skinning_tpose_to_scanpose_back_body[dep1];
        blend_mat += w2 * skinning_tpose_to_scanpose_back_body[dep2];
        blend_mat += w3 * skinning_tpose_to_scanpose_back_body[dep3];
        blend_mat += w4 * skinning_tpose_to_scanpose_back_body[dep4];
        blend_mat += w5 * skinning_tpose_to_scanpose_back_body[dep5];
        blend_mat += w6 * skinning_tpose_to_scanpose_back_body[dep6];
        blend_mat += w7 * skinning_tpose_to_scanpose_back_body[dep7];

        Point the_point = affine_transform(blend_mat, lm_templ_back_body[i]);

        double u = norm( lm_ps_back_body[i] - the_point );
        if ( robustness_parameters_.solver_type == SOLVER_TYPE_L2 )
        {
            error_match_landmarks += u * u * local_weight;
        }
        else if ( robustness_parameters_.solver_type == SOLVER_TYPE_L1 )
        {
            error_match_landmarks += std::pow( u, robustness_parameters_.parameter ) * local_weight;
        }
        else if ( robustness_parameters_.solver_type == SOLVER_TYPE_HUBER )
        {
            std::cerr << "ERSTMAL NICHT " << std::endl; exit(1);
            double huber_thr = robustness_parameters_.parameter;
            if ( u > huber_thr )
            {
                error_match_landmarks += ( huber_thr*u - huber_thr*huber_thr/2.0 ) * local_weight;
            }
            else
            {
                error_match_landmarks += ( u*u/2.0 ) * local_weight;
            }
        }
        else if ( robustness_parameters_.solver_type == SOLVER_TYPE_GEMANMCCLURE )
        {
            std::cerr << "ERSTMAL NICHT " << std::endl; exit(1);
            double scale_parameter = robustness_parameters_.parameter;
//                error_match_landmarks += ( u*u / (scale_parameter*scale_parameter + u*u) ); // VERSION 2
            error_match_landmarks += ( ( u*u / ( 2.0 * ( 1.0 + (u*u/(scale_parameter*scale_parameter)) ) ) ) ) * local_weight; // VERSION 3
        }
        else // default to L2
        {
            error_match_landmarks += u * u * local_weight;
        }
    }


    error_match_landmarks *= (weight_ / L_total);

    return error_match_landmarks;
}


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


void
Energy_term_landmarks_skinned_twoscans::
add_rows_big_lse(std::vector<Tripl>& coeffs,
                 Eigen::VectorXd& b,
                 unsigned int& r)
{
    //std::cout << "[INFO] Energy_term_landmarks_skinned_twoscans::add_rows_big_lse(): weight_: " << weight_ << std::endl;

    const Landmarks_manager& landmarks_manager = energy_term_data_.landmarks_manager;
    const Column_offsets&  column_offsets = energy_term_data_.column_offsets;
    const Point_set* point_set_front_body = energy_term_data_.point_set;
    const Point_set* point_set_back_body  = energy_term_data_.point_set_back_body;
    const Surface_mesh* template_mesh = energy_term_data_.template_mesh;

    Surface_mesh::Vertex_property<Vec4f> vdepends = template_mesh->get_vertex_property<Vec4f>("v:skin_depend");
    Surface_mesh::Vertex_property<Vec4f> vweights = template_mesh->get_vertex_property<Vec4f>("v:skin_weight");
    Surface_mesh::Vertex_property<Vec4f> vdepends2 = template_mesh->get_vertex_property<Vec4f>("v:skin_depend2");
    Surface_mesh::Vertex_property<Vec4f> vweights2 = template_mesh->get_vertex_property<Vec4f>("v:skin_weight2");
    if (!(vdepends && vweights && vdepends2 && vweights2))
    {
        std::cerr << "[ERROR] - Energy_term_landmarks_skinned_twoscans::add_rows_big_lse() - EXIT!" << std::endl;
        exit(1);
    }

    if (!landmarks_manager.check_dimension(point_set_front_body, template_mesh, "m:landmarks_front_body") ||
        !landmarks_manager.check_dimension(point_set_back_body, template_mesh, "m:landmarks_back_body"))
    {
        std::cerr << "Energy_term_landmarks_skinned_twoscans::evaluate(): [ERROR] Number of landmarks do not match for front_body and/or back_body." << std::endl;
        return;
    }

    double local_weight = 1.0;
    Surface_mesh::Vertex_property<double> local_landmark_weight_front_body = template_mesh->get_vertex_property<double>("v:landmark_weight_front_body");
    Surface_mesh::Vertex_property<double> local_landmark_weight_back_body  = template_mesh->get_vertex_property<double>("v:landmark_weight_back_body");

    std::vector<Surface_mesh::Vertex> tm_landmarks_front_body;
    landmarks_manager.get_indices_template(template_mesh, tm_landmarks_front_body, "m:landmarks_front_body");
    std::vector<Point> lm_ps_front_body;
    landmarks_manager.get_landmarks_point_set_stdvec(point_set_front_body, lm_ps_front_body);

    std::vector<Surface_mesh::Vertex> tm_landmarks_back_body;
    landmarks_manager.get_indices_template(template_mesh, tm_landmarks_back_body, "m:landmarks_back_body");
    std::vector<Point> lm_ps_back_body;
    landmarks_manager.get_landmarks_point_set_stdvec(point_set_back_body, lm_ps_back_body);

    const std::vector<Mat4f>* pose_vector_front_body = energy_term_data_.pose_vector_front_body;
    surface_mesh::set_pose_vector(*pose_vector_front_body, energy_term_data_.character->skeleton());
    const std::vector<Mat4f> skinning_tpose_to_scanpose_front_body = energy_term_data_.character->skeleton().skinning_matrices_;
    if (skinning_tpose_to_scanpose_front_body.size() == 0)
    {
        std::cerr << "[ERROR] - Energy_term_pca_landmarks_skinned_twoscans::evaluate() - skinning_tpose_to_scanpose_front_body.size(): " << skinning_tpose_to_scanpose_front_body.size() << std::endl;
        exit(1);
    }

    const std::vector<Mat4f>* pose_vector_back_body = energy_term_data_.pose_vector_back_body;
    surface_mesh::set_pose_vector(*pose_vector_back_body, energy_term_data_.character->skeleton());
    const std::vector<Mat4f> skinning_tpose_to_scanpose_back_body = energy_term_data_.character->skeleton().skinning_matrices_;
    if (skinning_tpose_to_scanpose_back_body.size() == 0)
    {
        std::cerr << "[ERROR] - Energy_term_pca_landmarks_skinned_twoscans::evaluate() - skinning_tpose_to_scanpose_back_body.size(): " << skinning_tpose_to_scanpose_back_body.size() << std::endl;
        exit(1);
    }


    const unsigned int L_front(lm_ps_front_body.size());
    const unsigned int L_back(lm_ps_back_body.size());
    const unsigned int L_total = L_front + L_back;


    const double weight_match_landmarks = sqrt(weight_ / L_total);


    for (unsigned int i = 0; i < L_front; ++i)
    {
        const int idx = tm_landmarks_front_body[i].idx();
        const Point t = lm_ps_front_body[i];

        if (local_landmark_weight_front_body)
        {
            local_weight = local_landmark_weight_front_body[tm_landmarks_front_body[i]];
            if (local_weight < 1e-5)
            {
                continue;
            }
            local_weight = sqrt(local_weight);
        }

        Surface_mesh::Vertex v_idx(idx);
        int dep0 = (int)vdepends[v_idx][0];
        float w0 = vweights[v_idx][0];
        int dep1 = (int)vdepends[v_idx][1];
        float w1 = vweights[v_idx][1];
        int dep2 = (int)vdepends[v_idx][2];
        float w2 = vweights[v_idx][2];
        int dep3 = (int)vdepends[v_idx][3];
        float w3 = vweights[v_idx][3];
        int dep4 = (int)vdepends2[v_idx][0];
        float w4 = vweights2[v_idx][0];
        int dep5 = (int)vdepends2[v_idx][1];
        float w5 = vweights2[v_idx][1];
        int dep6 = (int)vdepends2[v_idx][2];
        float w6 = vweights2[v_idx][2];
        int dep7 = (int)vdepends2[v_idx][3];
        float w7 = vweights2[v_idx][3];

        Mat4f blend_mat;
        blend_mat  = w0 * skinning_tpose_to_scanpose_front_body[dep0];
        blend_mat += w1 * skinning_tpose_to_scanpose_front_body[dep1];
        blend_mat += w2 * skinning_tpose_to_scanpose_front_body[dep2];
        blend_mat += w3 * skinning_tpose_to_scanpose_front_body[dep3];
        blend_mat += w4 * skinning_tpose_to_scanpose_front_body[dep4];
        blend_mat += w5 * skinning_tpose_to_scanpose_front_body[dep5];
        blend_mat += w6 * skinning_tpose_to_scanpose_front_body[dep6];
        blend_mat += w7 * skinning_tpose_to_scanpose_front_body[dep7];

        // right hand side
        b(r) = local_weight * weight_match_landmarks * (t[0] - blend_mat(0, 3));

        // begin_row
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 0, local_weight * weight_match_landmarks * blend_mat(0, 0)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 1, local_weight * weight_match_landmarks * blend_mat(0, 1)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 2, local_weight * weight_match_landmarks * blend_mat(0, 2)) );
        ++r;

        // right hand side
        b(r) = local_weight * weight_match_landmarks * (t[1] - blend_mat(1, 3));

        // begin_row
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 0, local_weight * weight_match_landmarks * blend_mat(1, 0)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 1, local_weight * weight_match_landmarks * blend_mat(1, 1)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 2, local_weight * weight_match_landmarks * blend_mat(1, 2)) );
        ++r;

        // right hand side
        b(r) = local_weight * weight_match_landmarks * (t[2] - blend_mat(2, 3));

        // begin_row
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 0, local_weight * weight_match_landmarks * blend_mat(2, 0)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 1, local_weight * weight_match_landmarks * blend_mat(2, 1)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 2, local_weight * weight_match_landmarks * blend_mat(2, 2)) );
        ++r;
    }


    for (unsigned int i = 0; i < L_back; ++i)
    {
        const int idx = tm_landmarks_back_body[i].idx();
        const Point t = lm_ps_back_body[i];

        if (local_landmark_weight_back_body)
        {
            local_weight = local_landmark_weight_back_body[tm_landmarks_back_body[i]];
            if (local_weight < 1e-5)
            {
                continue;
            }
            local_weight = sqrt(local_weight);
        }

        Surface_mesh::Vertex v_idx(idx);
        int dep0 = (int)vdepends[v_idx][0];
        float w0 = vweights[v_idx][0];
        int dep1 = (int)vdepends[v_idx][1];
        float w1 = vweights[v_idx][1];
        int dep2 = (int)vdepends[v_idx][2];
        float w2 = vweights[v_idx][2];
        int dep3 = (int)vdepends[v_idx][3];
        float w3 = vweights[v_idx][3];
        int dep4 = (int)vdepends2[v_idx][0];
        float w4 = vweights2[v_idx][0];
        int dep5 = (int)vdepends2[v_idx][1];
        float w5 = vweights2[v_idx][1];
        int dep6 = (int)vdepends2[v_idx][2];
        float w6 = vweights2[v_idx][2];
        int dep7 = (int)vdepends2[v_idx][3];
        float w7 = vweights2[v_idx][3];

        Mat4f blend_mat;
        blend_mat  = w0 * skinning_tpose_to_scanpose_back_body[dep0];
        blend_mat += w1 * skinning_tpose_to_scanpose_back_body[dep1];
        blend_mat += w2 * skinning_tpose_to_scanpose_back_body[dep2];
        blend_mat += w3 * skinning_tpose_to_scanpose_back_body[dep3];
        blend_mat += w4 * skinning_tpose_to_scanpose_back_body[dep4];
        blend_mat += w5 * skinning_tpose_to_scanpose_back_body[dep5];
        blend_mat += w6 * skinning_tpose_to_scanpose_back_body[dep6];
        blend_mat += w7 * skinning_tpose_to_scanpose_back_body[dep7];

        // right hand side
        b(r) = local_weight * weight_match_landmarks * (t[0] - blend_mat(0, 3));

        // begin_row
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 0, local_weight * weight_match_landmarks * blend_mat(0, 0)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 1, local_weight * weight_match_landmarks * blend_mat(0, 1)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 2, local_weight * weight_match_landmarks * blend_mat(0, 2)) );
        ++r;

        // right hand side
        b(r) = local_weight * weight_match_landmarks * (t[1] - blend_mat(1, 3));

        // begin_row
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 0, local_weight * weight_match_landmarks * blend_mat(1, 0)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 1, local_weight * weight_match_landmarks * blend_mat(1, 1)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 2, local_weight * weight_match_landmarks * blend_mat(1, 2)) );
        ++r;

        // right hand side
        b(r) = local_weight * weight_match_landmarks * (t[2] - blend_mat(2, 3));

        // begin_row
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 0, local_weight * weight_match_landmarks * blend_mat(2, 0)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 1, local_weight * weight_match_landmarks * blend_mat(2, 1)) );
        coeffs.push_back( Tripl(r, column_offsets.offset_match + 3*idx + 2, local_weight * weight_match_landmarks * blend_mat(2, 2)) );
        ++r;
    }
}


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


unsigned int
Energy_term_landmarks_skinned_twoscans::
n_rows_big_sle()
{
    //std::cout << "[INFO] Energy_term_landmarks_skinned_twoscans::n_rows_big_sle()" << std::endl;

    unsigned int c = 0;

    const Surface_mesh* template_mesh = energy_term_data_.template_mesh;

    //
    // front_body
    //
    Surface_mesh::Mesh_property< std::vector<Surface_mesh::Vertex> > lm_prop_front_body = template_mesh->get_mesh_property< std::vector<Surface_mesh::Vertex> >("m:landmarks_front_body");
    if (lm_prop_front_body)
    {
        const std::vector<Surface_mesh::Vertex>& tm_landmarks = lm_prop_front_body[0];
        Surface_mesh::Vertex_property<double> local_landmark_weight = template_mesh->get_vertex_property<double>("v:landmark_weight_front_body");

        if (local_landmark_weight)
        {
            for (size_t i = 0; i < tm_landmarks.size(); ++i)
            {
                if (local_landmark_weight[tm_landmarks[i]] >= 1e-5)
                {
                    ++c;
                }
                else
                {
                    //std::cout << "landmark ignored " << tm_landmarks[i] << std::endl;
                }
            }
        }
        else
        {
            c += (unsigned int) tm_landmarks.size();
        }
    }

    //
    // back_body
    //
    Surface_mesh::Mesh_property< std::vector<Surface_mesh::Vertex> > lm_prop_back_body = template_mesh->get_mesh_property< std::vector<Surface_mesh::Vertex> >("m:landmarks_back_body");
    if (lm_prop_back_body)
    {
        const std::vector<Surface_mesh::Vertex>& tm_landmarks = lm_prop_back_body[0];
        Surface_mesh::Vertex_property<double> local_landmark_weight = template_mesh->get_vertex_property<double>("v:landmark_weight_back_body");

        if (local_landmark_weight)
        {
            for (size_t i = 0; i < tm_landmarks.size(); ++i)
            {
                if (local_landmark_weight[tm_landmarks[i]] >= 1e-5)
                {
                    ++c;
                }
                else
                {
                    //std::cout << "landmark ignored " << tm_landmarks[i] << std::endl;
                }
            }
        }
        else
        {
            c += (unsigned int) tm_landmarks.size();
        }
    }

    return c*3;
}


#endif

//=============================================================================
} // namespace surface_mesh
} // namespace graphene
//=============================================================================
