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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
/************************************************************************
*
* Copyright (C) 2009-2023 IRCAD France
* Copyright (C) 2012-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 "model_series_writer_test.hpp"
#include <core/os/temp_path.hpp>
#include <data/activity_set.hpp>
#include <data/array.hpp>
#include <data/mesh.hpp>
#include <data/model_series.hpp>
#include <data/reconstruction.hpp>
#include <data/series_set.hpp>
#include <service/op.hpp>
#include <utest_data/generator/series_set.hpp>
#include <filesystem>
#include <string>
#include <vector>
// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::module::io::vtk::ut::model_series_writer_test);
namespace sight::module::io::vtk::ut
{
namespace fs = std::filesystem;
using file_container_t = std::vector<std::string>;
namespace point = sight::data::iterator::point;
namespace cell = sight::data::iterator::cell;
//------------------------------------------------------------------------------
void model_series_writer_test::setUp()
{
// Set up context before running a test.
}
//------------------------------------------------------------------------------
void model_series_writer_test::tearDown()
{
// Clean up after the test run.
}
//------------------------------------------------------------------------------
void run_model_series_srv(
const std::string& _impl,
const boost::property_tree::ptree& _cfg,
const SPTR(data::object)& _obj
)
{
service::base::sptr srv = service::add(_impl);
CPPUNIT_ASSERT_MESSAGE(std::string("Failed to create service ") + _impl, srv);
if(srv->is_a("sight::io::service::reader"))
{
srv->set_inout(_obj, "data");
}
else
{
srv->set_input(_obj, "data");
}
CPPUNIT_ASSERT_NO_THROW(srv->set_config(_cfg));
CPPUNIT_ASSERT_NO_THROW(srv->configure());
CPPUNIT_ASSERT_NO_THROW(srv->start().wait());
CPPUNIT_ASSERT_NO_THROW(srv->update().wait());
CPPUNIT_ASSERT_NO_THROW(srv->stop().wait());
service::remove(srv);
}
//------------------------------------------------------------------------------
boost::property_tree::ptree get_io_cfg_from_folder(const fs::path& _file)
{
service::config_t srv_cfg;
srv_cfg.add("folder", _file.string());
return srv_cfg;
}
//------------------------------------------------------------------------------
boost::property_tree::ptree get_io_cfg_from_files(const file_container_t& _files)
{
service::config_t srv_cfg;
for(const auto& file : _files)
{
srv_cfg.add("file", file);
}
return srv_cfg;
}
//------------------------------------------------------------------------------
void model_series_writer_test::test_write_meshes()
{
data::model_series::sptr model_series = utest_data::generator::series_set::create_model_series(5);
const std::vector<std::string> all_extensions = {"vtk", "vtp", "obj", "ply", "stl"};
core::os::temp_dir tmp_dir;
for(const auto& ext : all_extensions)
{
const auto& ext_dir = tmp_dir / ext;
fs::create_directories(ext_dir);
auto cfg = get_io_cfg_from_folder(ext_dir);
cfg.add("extension", ext);
run_model_series_srv(
"sight::module::io::vtk::model_series_writer",
cfg,
model_series
);
file_container_t files;
for(fs::directory_iterator it(ext_dir) ; it != fs::directory_iterator() ; ++it)
{
if(it->path().extension() == "." + ext)
{
files.push_back(it->path().string());
}
}
// Ensure reading order (modelSeries generator will prefix each file with a number).
std::sort(files.begin(), files.end());
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Number of saved files",
model_series->get_reconstruction_db().size(),
files.size()
);
auto series_set = std::make_shared<data::series_set>();
run_model_series_srv(
"sight::module::io::vtk::series_set_reader",
get_io_cfg_from_files(files),
series_set
);
CPPUNIT_ASSERT_EQUAL_MESSAGE("series_set Size", (std::size_t) 1, series_set->size());
data::model_series::sptr read_series = std::dynamic_pointer_cast<data::model_series>(series_set->at(0));
CPPUNIT_ASSERT_MESSAGE("A ModelSeries was expected", read_series);
using rec_vec_t = data::model_series::reconstruction_vector_t;
const rec_vec_t& read_recs = read_series->get_reconstruction_db();
CPPUNIT_ASSERT_EQUAL_MESSAGE("Number of reconstructions", files.size(), read_recs.size());
const rec_vec_t& ref_recs = model_series->get_reconstruction_db();
auto it_ref = ref_recs.begin();
auto it_read = read_recs.begin();
for( ; it_ref != ref_recs.end() ; ++it_ref, ++it_read)
{
data::mesh::csptr ref_mesh = (*it_ref)->get_mesh();
data::mesh::csptr read_mesh = (*it_read)->get_mesh();
const auto reflock = ref_mesh->dump_lock();
const auto read_mesh_lock = read_mesh->dump_lock();
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Number of Points.",
ref_mesh->num_points(),
read_mesh->num_points()
);
CPPUNIT_ASSERT_EQUAL_MESSAGE(
"Number of Cells.",
ref_mesh->num_cells(),
read_mesh->num_cells()
);
// Don't test internal structures for obj, ply and stl, since some of them are missing.
if(ext != "obj" && ext != "ply" && ext != "stl")
{
const auto ref_points = ref_mesh->czip_range<point::xyz, point::nxyz, point::rgba>();
const auto read_points = read_mesh->czip_range<point::xyz, point::nxyz, point::rgba>();
for(const auto& [ref, read] : boost::combine(ref_points, read_points))
{
const auto& [pt1, n1, c1] = ref;
const auto& [pt2, n2, c2] = read;
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Point.x ", pt1.x, pt2.x, 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Point.y", pt1.y, pt2.y, 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Point.z", pt1.z, pt2.z, 0.00001);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Point color R", c1.r, c2.r);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Point color G", c1.g, c2.g);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Point color B", c1.b, c2.b);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Point color A", c1.a, c2.a);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Point normal x", n1.nx, n2.nx, 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Point normal y", n1.ny, n2.ny, 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Point normal z", n1.nz, n2.nz, 0.00001);
}
const auto ref_cells = ref_mesh->czip_range<cell::triangle, cell::nxyz, cell::rgba>();
const auto read_cells = read_mesh->czip_range<cell::triangle, cell::nxyz, cell::rgba>();
for(const auto& [ref, read] : boost::combine(ref_cells, read_cells))
{
const auto& [tri1, n1, c1] = ref;
const auto& [tri2, n2, c2] = read;
for(std::size_t i = 0 ; i < 3 ; ++i)
{
CPPUNIT_ASSERT_EQUAL_MESSAGE("Cell point index", tri1.pt[i], tri2.pt[i]);
}
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Cell normal x", n1.nx, n2.nx, 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Cell normal y", n1.ny, n2.ny, 0.00001);
CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("Cell normal z", n1.nz, n2.nz, 0.00001);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Cell color R", c1.r, c2.r);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Cell color G", c1.g, c2.g);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Cell color B", c1.b, c2.b);
CPPUNIT_ASSERT_EQUAL_MESSAGE("Cell color A", c1.a, c2.a);
}
}
}
}
}
//------------------------------------------------------------------------------
void model_series_writer_test::test_write_reconstructions()
{
data::model_series::sptr model_series = utest_data::generator::series_set::create_model_series(5);
core::os::temp_dir tmp_dir;
run_model_series_srv(
"sight::module::io::vtk::model_series_obj_writer",
get_io_cfg_from_folder(tmp_dir),
model_series
);
file_container_t files;
for(fs::directory_iterator it(tmp_dir) ; it != fs::directory_iterator() ; ++it)
{
files.push_back(it->path().string());
}
// Writer generates a .mtl file for each .obj file
CPPUNIT_ASSERT_EQUAL(model_series->get_reconstruction_db().size() * 2, files.size());
}
//------------------------------------------------------------------------------
} // namespace sight::module::io::vtk::ut
|