Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 63 additions & 4 deletions cgogn/io/surface/obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
#include <cgogn/io/surface/surface_import.h>
#include <cgogn/io/utils.h>

#include <cgogn/core/functions/attributes.h>
#include <cgogn/core/functions/mesh_info.h>

#include <fstream>
using namespace std::literals::string_literals;

Expand Down Expand Up @@ -335,14 +338,70 @@ bool import_OBJ_tn(MESH& m_p, MESH& m_tc, MESH& m_n, const std::string& filename
return true;
}


// Changes connectivity between obj files but when you load again the file the new connectivity works
// It works but it also kinda doesn't
template <typename MESH>
void export_OBJ(MESH& m, const typename mesh_traits<MESH>::template Attribute<geometry::Vec3>* vertex_position,
const std::string& filename)
void export_OBJ(MESH& m_p, MESH& m_no, MESH& m_tc, const typename mesh_traits<MESH>::template Attribute<geometry::Vec3>* vertex_position,
const typename mesh_traits<MESH>::template Attribute<geometry::Vec3>* normal,
const typename mesh_traits<MESH>::template Attribute<geometry::Vec2>* texture_coord,
const std::string& filename)
{
static_assert(mesh_traits<MESH>::dimension == 2, "MESH dimension should be 2");

// TODO
using Vertex = typename mesh_traits<MESH>::Vertex;
using Face = typename mesh_traits<MESH>::Face;

using Vec3 = geometry::Vec3;
using Vec2 = geometry::Vec2;

auto vertex_id = add_attribute<uint32, Vertex>(m_p, "__vertex_id");
auto normal_id = add_attribute<uint32, Vertex>(m_no, "__normal_id");
auto texture_id = add_attribute<uint32, Vertex>(m_tc, "__texture_id");

std::ofstream out_file;
out_file.open(filename);
out_file << "mtllib "<< filename.substr(0,filename.size() - 4) << ".mtl" << "\n";
out_file << "g default\n";

uint32 id = 1;
foreach_cell(m_p, [&](Vertex v) -> bool {
const geometry::Vec3& p = value<geometry::Vec3>(m_p, vertex_position, v);
value<uint32>(m_p, vertex_id, v) = id++;
out_file << "v " << p[0] << " " << p[1] << " " << p[2] << "\n";
return true;
});

id = 1;
foreach_cell(m_tc, [&](Vertex v) -> bool {
const geometry::Vec2& p = value<geometry::Vec2>(m_tc, texture_coord, v);
value<uint32>(m_tc, texture_id, v) = id++;
out_file << "vt " << p[0] << " " << p[1] << "\n";
return true;
});

id = 1;
foreach_cell(m_no, [&](Vertex v) -> bool {
const geometry::Vec3& p = value<geometry::Vec3>(m_no, normal, v);
value<uint32>(m_no, normal_id, v) = id++;
out_file << "vn " << p[0] << " " << p[1] << " " << p[2] << "\n";
return true;
});

foreach_cell(m_p, [&](Face f) -> bool {
out_file << "f";
foreach_incident_vertex(m_p, f, [&](Vertex v) -> bool {
out_file << " " << value<uint32>(m_p, vertex_id, v) << "/" << value<uint32>(m_tc, texture_id, v) << "/" <<value<uint32>(m_no, normal_id, v) ;
return true;
});
out_file << "\n";
return true;
});

remove_attribute<Vertex>(m_p, vertex_id);
remove_attribute<Vertex>(m_tc, texture_id);
remove_attribute<Vertex>(m_no, normal_id);

out_file.close();
}

} // namespace io
Expand Down
1 change: 1 addition & 0 deletions cgogn/modeling/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ set(src_list
"${CMAKE_CURRENT_LIST_DIR}/ui_modules/tubular_mesh_module.h"
"${CMAKE_CURRENT_LIST_DIR}/ui_modules/volume_deformation.h"
"${CMAKE_CURRENT_LIST_DIR}/ui_modules/volume_mr_modeling.h"
"${CMAKE_CURRENT_LIST_DIR}/ui_modules/action_unit_change.h"
)
target_sources(${PROJECT_NAME} PRIVATE ${src_list})
source_group(TREE ${CMAKE_CURRENT_LIST_DIR} FILES ${src_list})
Expand Down
141 changes: 141 additions & 0 deletions cgogn/modeling/algos/blending.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
/*******************************************************************************
* CGoGN: Combinatorial and Geometric modeling with Generic N-dimensional Maps *
* Copyright (C), IGG Group, ICube, University of Strasbourg, France *
* *
* This library 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 2.1 of the License, or (at your *
* option) any later version. *
* *
* This library 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 this library; if not, write to the Free Software Foundation, *
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
* *
* Web site: http://cgogn.unistra.fr/ *
* Contact information: cgogn@unistra.fr *
* *
*******************************************************************************/

#ifndef CGOGN_BLENDING_H_
#define CGOGN_BLENDING_H_

#include <cgogn/geometry/types/vector_traits.h>
#include <cgogn/core/types/mesh_views/cell_cache.h>
#include <memory>

namespace cgogn
{

namespace modeling
{

using geometry::Vec3;

static bool ends_with(const std::string& str, const std::string& suffix)
{
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
}

template <typename MESH>
// Blending function
// Currently it's a sum of vectors of each different attributes that will be blent
void blending(MESH& m, std::vector<std::shared_ptr<typename mesh_traits<MESH>::template Attribute<Vec3>>> attributes_to_blend, std::vector<float> weight_list , std::string name_attribute_to_change)
{
using Vertex = typename mesh_traits<MESH>::Vertex;
std::shared_ptr<typename mesh_traits<MESH>::template Attribute<Vec3>> vertex_position = cgogn::get_attribute<Vec3, Vertex>(m, name_attribute_to_change);
std::shared_ptr<typename mesh_traits<MESH>::template Attribute<Vec3>> repos_position = cgogn::get_attribute<Vec3, Vertex>(m, "AU00");
typename mesh_traits<MESH>::template Attribute<Vec3>* new_vertex_pos_value = vertex_position.get();
Vec3 diff_distance_repos = Vec3(0, 0, 0);
float epsilon = 0.0001;

// need to check if parallel_foreach_cell messes with the calculations
foreach_cell(m, [&](Vertex v) -> bool {
value<Vec3>(m, vertex_position, v) = value<Vec3>(m, repos_position, v);
Vec3 result = Vec3(0, 0, 0);
//float nb_au_influence = 0.;

for (int i = 0; i < attributes_to_blend.size(); i++)
{
diff_distance_repos = value<Vec3>(m, attributes_to_blend[i], v) - value<Vec3>(m, repos_position, v);
diff_distance_repos *= weight_list[i];

// if(abs(diff_distance_repos[0]) > 0. || abs(diff_distance_repos[1]) > 0. || abs(diff_distance_repos[2]) > 0.){
// nb_au_influence++;
// }
result += diff_distance_repos;
}

// if (nb_au_influence != 0.)
// result = result / nb_au_influence;

result[0] = (abs(result[0]) > epsilon) ? result[0] : 0. ;
result[1] = (abs(result[1]) > epsilon) ? result[1] : 0. ;
result[2] = (abs(result[2]) > epsilon) ? result[2] : 0. ;

value<Vec3>(m, vertex_position, v) += result ;
return true;
});
}

template <typename MESH>
// Function called each frame after clicking on Apply CSV
// Setup the differents AUs and weights to calculate the frames
void blending_csv(MESH& m, int incr, float poids_frame , std::map<std::string, std::vector<float>> csv_ , std::string pos_attr_name, Eigen::MatrixXd csv_weights_detected)
{
using Vertex = typename mesh_traits<MESH>::Vertex;

std::vector<std::shared_ptr<typename mesh_traits<MESH>::template Attribute<Vec3>>> attributes_csv;
std::vector<float> weight_list;

for (auto& it : csv_)
{
if (ends_with(it.first, "_r"))
{
attributes_csv.push_back(
cgogn::get_attribute<Vec3, Vertex>(m, it.first.substr(0, it.first.size() - 2)));
}
}
for (int i = 1; i < attributes_csv.size() + 1; i++)
{
float weight_attribute;
if (incr < 8)
{
weight_list.push_back((csv_weights_detected(incr, i - 1) * (1 - poids_frame)) +
(csv_weights_detected(incr + 1, i - 1) * poids_frame));
}
else
{
float weight_smooth = 0;
int j = 1;
for(; j < 7 ; j++)
{
weight_smooth += csv_weights_detected(incr - j, i - 1);
}

if (incr + 1 < csv_weights_detected.rows())
{
weight_smooth += (csv_weights_detected(incr, i - 1) * (1 - poids_frame)) + (csv_weights_detected(incr + 1, i - 1) * poids_frame);
weight_smooth = weight_smooth/float(j);
weight_list.push_back(weight_smooth);
}
else
{
weight_smooth += (csv_weights_detected(incr - 1, i - 1) * (1 - poids_frame)) + (csv_weights_detected(incr, i - 1) * poids_frame);
weight_smooth = weight_smooth/float(j);
weight_list.push_back(weight_smooth);
}
}
}
modeling::blending(m, attributes_csv, weight_list , pos_attr_name);
}

} // namespace modeling

} // namespace cgogn

#endif // CGOGN_BLENDING_H_
16 changes: 16 additions & 0 deletions cgogn/modeling/apps/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,17 @@ target_link_libraries(convex_hull
${CMAKE_DL_LIBS}
)

add_executable(action_unit_switch action_unit_switch.cpp)
target_link_libraries(action_unit_switch
cgogn::core
cgogn::ui
cgogn::io
cgogn::rendering
cgogn::modeling
Blossom5
${CMAKE_DL_LIBS}
)

if(APPLE)
find_library(CORE_FOUNDATION CoreFoundation)
find_library(CARBON Carbon)
Expand Down Expand Up @@ -107,4 +118,9 @@ if(APPLE)
${CORE_FOUNDATION}
${CARBON}
)

target_link_libraries(action_unit_switch
${CORE_FOUNDATION}
${CARBON}
)
endif()
Loading