Files
OrcaSlicer/deps_src/libigl/igl/readSTL.cpp
Donovan Baarda dc5897d7b5 Update eigen to v5.0.1 and libigl to v2.6.0. (#11311)
* Update eigen from v3.3.7 to v5.0.1.

This updates eigen from v3.3.7 released on  December 11, 2018-12-11 to v5.0.1
released on 2025-11-11. There have be a large number of bug-fixes,
optimizations, and improvements between these releases. See the details at;

https://gitlab.com/libeigen/eigen/-/releases

It retains the previous custom minimal `CMakeLists.txt`, and adds a
README-OrcaSlicer.md that explains what version and parts of the upstream
eigen release have been included, and where the full release can be found.

* Update libigl from v2.0.0 (or older) to v2.6.0.

This updates libigl from what was probably v2.0.0 released on 2018-10-16 to
v2.6.0 released on 2025-05-15. It's possible the old version was even older
than that but there is no version indicators in the code and I ran out of
patience identifying missing changes and only went back as far as v2.0.0.

There have been a large number of bug-fixes, optimizations, and improvements
between these versions. See the following for details;

https://github.com/libigl/libigl/releases

I retained the minimal custom `CMakeLists.txt`, added `README.md` from the
libigl distribution which identifies the version, and added a
README-OrcaSlicer.md that details the version and parts that have been
included.

* Update libslic3r for libigl v2.6.0 changes.

This updates libslic3r for all changes moving to eigen v5.0.1 and libigl
v2.6.0. Despite the large number of updates to both dependencies, no changes
were required for the eigen update, and only one change was required for the
libigl update.

For libigl, `igl::Hit` was changed to a template taking the Scalar type to
use. Previously it was hard-coded to `float`, so to minimize possible impact
I've updated all places it is used from `igl::Hit` to `igl::Hit<float>`.

* Add compiler option `-DNOMINMAX` for libigl with MSVC.

MSVC by default defines `min(()` and `max()` macros that break
`std::numeric_limits<>::max()`. The upstream cmake that we don't include
adds `-DNOMINMAX` for the libigl module when compiling with MSVC, so we need
to add the same thing here.

* Fix src/libslic3r/TriangleMeshDeal.cpp for the unmodified upstream libigl.

This fixes `TriangleMeshDeal.cpp` to work with the unmodified upstream
libigl v2.6.0. loop.{h,cpp} implementation.

This file and feature was added in PR "BBS Port: Mesh Subdivision" (#12150)
which included changes to `loop.{h,cpp}` in the old version of libigl. This PR
avoids modifying the included dependencies, and uses the updated upstream
versions of those files without any modifications, which requires fixing
TriangleMeshDeal.cpp to work with them.

In particular, the modifications made to `loop.{h,cpp}` included changing the
return type from void to bool, adding additional validation checking of the
input meshes, and returning false if they failed validation. These added
checks looked unnecessary and would only have caught problems if the input
mesh was very corrupt.

To make `TriangleMeshDeal.cpp` work without this built-in checking
functionality, I removed checking/handling of any `false` return value.

There was also a hell of a lot of redundant copying and casting back and forth
between float and double, so I cleaned that up. The input and output meshs use
floats for the vertexes, and there would be no accuracy benefits from casting
to and from doubles for the simple weighted average operations done by
igl::loop(). So this just uses `Eigen:Map` to use the original input mesh
vertex data directly without requiring any copy or casting.

* Move eigen from included `deps_src` to externaly fetched `deps`.

This copys what PrusaSlicer did and moved it from an included dependency under
`deps_src` to an externaly fetched dependency under `deps`. This requires
updating some `CMakeList.txt` configs and removing the old and obsolete
`cmake/modules/FindEigen3.cmake`. The details of when this was done in
PrusaSlicer and the followup fixes are at;

* 21116995d7
* https://github.com/prusa3d/PrusaSlicer/issues/13608
* https://github.com/prusa3d/PrusaSlicer/pull/13609
* e3c277b9ee

For some reason I don't fully understand this also required fixing
`src/slic3r/GUI/GUI_App.cpp` by adding `#include <boost/nowide/cstdio.hpp>` to
fix an `error: ‘remove’ is not a member of ‘boost::nowide'`. The main thing I
don't understand is how it worked before. Note that this include is in the
PrusaSlicer version of this file, but it also significantly deviates from what
is currently in OrcaSlicer in many other ways.

* Whups... I missed adding the deps/Eigen/Eigen.cmake file...

* Tidy some whitespace indenting in CMakeLists.txt.

* Ugh... tabs indenting needing fixes.

* Change the include order of deps/Eigen.

It turns out that although Boost includes some references to Eigen, Eigen also
includes some references to Boost for supporting some of it's additional
numeric types.

I don't think it matters much since we are not using these features, but I
think technically its more correct to say Eigen depends on Boost than the
other way around, so I've re-ordered them.

* Add source for Eigen 5.0.1 download to flatpak yml config.

* Add explicit `DEPENDS dep_Boost to deps/Eigen.

I missed this before. This ensures we don't rely on include orders to make
sure Boost is installed before we configure Eigen.

* Add `DEPENDS dep_Boost dep_GMP dep_MPFR` to deps/Eigen.

It turns out Eigen can also use GMP and MPFR for multi-precision and
multi-precision-rounded numeric types if they are available.

Again, I don't think we are using these so it doesn't really matter, but it is
technically correct and ensures they are there if we ever do need them.

* Fix deps DEPENDENCY ordering for GMP, MPFR, Eigen, and CGAL.

I think this is finally correct. Apparently CGAL also optionally depends on
Eigen, so the correct dependency order from lowest to highest is GMP, MPFR, Eigen, and CGAL.

---------

Co-authored-by: Donovan Baarda <dbaarda@google.com>
Co-authored-by: Noisyfox <timemanager.rick@gmail.com>
2026-05-12 15:09:13 +08:00

330 lines
13 KiB
C++

// This file is part of libigl, a simple c++ geometry processing library.
//
// Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
// Copyright (C) 2018 Qingnan Zhou <qnzhou@gmail.com>
// Copyright (C) 2020 Jérémie Dumas <jeremie.dumas@ens-lyon.org>
//
// This Source Code Form is subject to the terms of the Mozilla Public License
// v. 2.0. If a copy of the MPL was not distributed with this file, You can
// obtain one at http://mozilla.org/MPL/2.0/.
#include "readSTL.h"
#include "IGL_ASSERT.h"
#include "list_to_matrix.h"
#include "string_utils.h"
#include "read_file_binary.h"
#include "FileMemoryStream.h"
#include <iostream>
#include <cstdint>
namespace igl {
template <typename DerivedV, typename DerivedF, typename DerivedN>
IGL_INLINE bool readSTL(std::istream &input,
Eigen::PlainObjectBase<DerivedV> &V,
Eigen::PlainObjectBase<DerivedF> &F,
Eigen::PlainObjectBase<DerivedN> &N) {
std::vector<std::array<typename DerivedV::Scalar, 3>> vV;
std::vector<std::array<typename DerivedN::Scalar, 3>> vN;
std::vector<std::array<typename DerivedF::Scalar, 3>> vF;
if (!readSTL(input, vV, vF, vN)) {
return false;
}
if (!list_to_matrix(vV, V)) {
return false;
}
if (!list_to_matrix(vF, F)) {
return false;
}
if (!list_to_matrix(vN, N)) {
return false;
}
return true;
}
IGL_INLINE bool is_stl_binary(std::istream &input) {
std::streampos start_pos = input.tellg();
constexpr size_t HEADER_SIZE = 80;
char header[HEADER_SIZE];
input.read(header, HEADER_SIZE);
if (!starts_with(header, "solid")) {
input.seekg(start_pos);
return true;
}
if (!input.good()) {
input.seekg(start_pos);
return false;
}
// Check if filesize matches the number of faces claimed.
char buf[4];
input.read(buf, 4);
size_t num_faces = *reinterpret_cast<std::uint32_t *>(buf);
input.seekg(0, input.end);
size_t file_size = input.tellg();
input.seekg(start_pos);
if (file_size == 80 + 4 + (4 * 12 + 2) * num_faces) {
return true;
} else {
return false;
}
}
template <typename TypeV, typename TypeF, typename TypeN>
IGL_INLINE bool read_stl_ascii(std::istream &input,
std::vector<std::array<TypeV, 3>> &V,
std::vector<std::array<TypeF, 3>> &F,
std::vector<std::array<TypeN, 3>> &N) {
constexpr size_t LINE_SIZE = 256;
char line[LINE_SIZE];
bool success = true;
if (!input) {
throw std::runtime_error("Failed to open file");
}
// skip header line.
input.getline(line, LINE_SIZE);
auto parse_ascii_normal = [&N](const char *line) {
double x, y, z;
size_t n = sscanf(line, " facet normal %lf %lf %lf", &x, &y, &z);
IGL_ASSERT(n == 3);
if (n != 3) {
return false;
}
N.push_back({{static_cast<TypeN>(x), static_cast<TypeN>(y),
static_cast<TypeN>(z)}});
return true;
};
auto parse_ascii_vertex = [&V](const char *line) {
double x, y, z;
size_t n = sscanf(line, " vertex %lf %lf %lf", &x, &y, &z);
IGL_ASSERT(n == 3);
if (n != 3) {
return false;
}
V.push_back({{static_cast<TypeV>(x), static_cast<TypeV>(y),
static_cast<TypeV>(z)}});
return true;
};
auto parse_ascii_facet = [&parse_ascii_vertex, &parse_ascii_normal](std::istream &fin) {
constexpr size_t LINE_SIZE = 256;
constexpr size_t WORD_SIZE = 128;
char line[LINE_SIZE];
char first_word[WORD_SIZE];
const char *face_begin = "facet";
const char *face_end = "endfacet";
const char *loop_begin = "outer";
const char *loop_end = "endloop";
const char *vertex_flag = "vertex";
bool reading_facet = false;
bool reading_loop = false;
bool success = true;
size_t num_vts = 0;
while (!fin.eof()) {
fin.getline(line, LINE_SIZE);
size_t n = sscanf(line, " %s", first_word);
if (n == 0)
continue;
if (starts_with(first_word, face_begin)) {
success = parse_ascii_normal(line);
IGL_ASSERT(success);
reading_facet = true;
} else if (starts_with(first_word, face_end)) {
IGL_ASSERT(reading_facet);
reading_facet = false;
} else if (starts_with(first_word, loop_begin)) {
reading_loop = true;
} else if (starts_with(first_word, loop_end)) {
IGL_ASSERT(reading_loop);
reading_loop = false;
} else if (starts_with(first_word, vertex_flag)) {
IGL_ASSERT(reading_facet);
IGL_ASSERT(reading_loop);
success = parse_ascii_vertex(line);
IGL_ASSERT(success);
num_vts += 1;
}
if (!success) {
return false;
}
if (!reading_facet) {
break;
}
}
if (num_vts == 0) {
return true;
}
IGL_ASSERT(num_vts == 3);
if (num_vts != 3) {
std::cerr << "Warning: mesh contain face made of " << num_vts
<< " vertices" << std::endl;
return false;
}
return true;
};
while (!input.eof()) {
success = parse_ascii_facet(input);
if (!success) {
return false;
}
}
F.resize(V.size() / 3);
for (size_t f = 0; f < F.size(); ++f) {
auto v = static_cast<TypeF>(f * 3);
F[f] = {{v, v + 1, v + 2}};
}
return success;
}
template <typename TypeV, typename TypeF, typename TypeN>
IGL_INLINE bool read_stl_binary(std::istream &input,
std::vector<std::array<TypeV, 3>> &V,
std::vector<std::array<TypeF, 3>> &F,
std::vector<std::array<TypeN, 3>> &N) {
if (!input) {
throw std::runtime_error("Failed to open file");
}
constexpr size_t FLOAT_SIZE = sizeof(float);
static_assert(FLOAT_SIZE == 4, "float type is not 4 bytes");
constexpr size_t LINE_SIZE = 256;
char buf[LINE_SIZE];
// 80 bytes header, no data significance.
input.read(buf, 80);
if (!input.good()) {
throw std::runtime_error("Unable to parse STL header.");
}
input.read(buf, 4);
const size_t num_faces = *reinterpret_cast<std::uint32_t *>(buf);
if (!input.good()) {
throw std::runtime_error("Unable to parse STL number of faces.");
}
for (size_t i = 0; i < num_faces; i++) {
// Parse normal
input.read(buf, FLOAT_SIZE * 3);
auto nx = static_cast<TypeN>(*reinterpret_cast<float *>(buf));
auto ny = static_cast<TypeN>(*reinterpret_cast<float *>(buf + FLOAT_SIZE));
auto nz =
static_cast<TypeN>(*reinterpret_cast<float *>(buf + FLOAT_SIZE * 2));
IGL_ASSERT(input.good());
// vertex 1
input.read(buf, FLOAT_SIZE * 3);
auto v1x = static_cast<TypeV>(*reinterpret_cast<float *>(buf));
auto v1y = static_cast<TypeV>(*reinterpret_cast<float *>(buf + FLOAT_SIZE));
auto v1z =
static_cast<TypeV>(*reinterpret_cast<float *>(buf + FLOAT_SIZE * 2));
IGL_ASSERT(input.good());
// vertex 2
input.read(buf, FLOAT_SIZE * 3);
auto v2x = static_cast<TypeV>(*reinterpret_cast<float *>(buf));
auto v2y = static_cast<TypeV>(*reinterpret_cast<float *>(buf + FLOAT_SIZE));
auto v2z =
static_cast<TypeV>(*reinterpret_cast<float *>(buf + FLOAT_SIZE * 2));
IGL_ASSERT(input.good());
// vertex 3
input.read(buf, FLOAT_SIZE * 3);
auto v3x = static_cast<TypeV>(*reinterpret_cast<float *>(buf));
auto v3y = static_cast<TypeV>(*reinterpret_cast<float *>(buf + FLOAT_SIZE));
auto v3z =
static_cast<TypeV>(*reinterpret_cast<float *>(buf + FLOAT_SIZE * 2));
IGL_ASSERT(input.good());
// attribute (2 bytes), not sure what purpose they serve.
input.read(buf, 2);
N.push_back({{nx, ny, nz}});
V.push_back({{v1x, v1y, v1z}});
V.push_back({{v2x, v2y, v2z}});
V.push_back({{v3x, v3y, v3z}});
IGL_ASSERT(input.good());
if (!input.good()) {
std::stringstream err_msg;
err_msg << "Failed to parse face " << i << " from STL file";
throw std::runtime_error(err_msg.str());
}
}
std::for_each(V.begin(), V.end(), [](const std::array<TypeV, 3> &v) {
for (auto x : v) {
if (!std::isfinite(x)) {
throw std::runtime_error("NaN or Inf detected in input file.");
}
}
});
if (!V.empty()) {
F.resize(V.size() / 3);
for (size_t f = 0; f < F.size(); ++f) {
auto v = static_cast<TypeF>(f * 3);
F[f] = {{v, v + 1, v + 2}};
}
}
return true;
}
template <typename TypeV, typename TypeF, typename TypeN>
IGL_INLINE bool readSTL(std::istream &input,
std::vector<std::array<TypeV, 3>> &V,
std::vector<std::array<TypeF, 3>> &F,
std::vector<std::array<TypeN, 3>> &N) {
bool success = false;
if (is_stl_binary(input)) {
success = read_stl_binary(input, V, F, N);
} else {
success = read_stl_ascii(input, V, F, N);
}
return success;
}
template <typename DerivedV, typename DerivedF, typename DerivedN>
IGL_INLINE bool readSTL(
FILE * fp,
Eigen::PlainObjectBase<DerivedV> & V,
Eigen::PlainObjectBase<DerivedF> & F,
Eigen::PlainObjectBase<DerivedN> & N)
{
std::vector<std::uint8_t> fileBufferBytes;
read_file_binary(fp,fileBufferBytes);
FileMemoryStream stream((char*)fileBufferBytes.data(), fileBufferBytes.size());
return readSTL(stream, V, F, N);
}
} // namespace igl
#ifdef IGL_STATIC_LIBRARY
// Explicit template instantiation
// generated by autoexplicit.sh
template bool igl::readSTL<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::readSTL<Eigen::Matrix<double, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::readSTL<Eigen::Matrix<double, -1, -1, 1, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 1, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::readSTL<Eigen::Matrix<double, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::readSTL<Eigen::Matrix<double, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::readSTL<Eigen::Matrix<float, -1, -1, 0, -1, -1>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::readSTL<Eigen::Matrix<float, -1, 3, 0, -1, 3>, Eigen::Matrix<int, -1, 3, 0, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 0, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::readSTL<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, -1, 0, -1, -1>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, -1, 0, -1, -1> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::readSTL<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<int, -1, 3, 1, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<int, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
template bool igl::readSTL<Eigen::Matrix<float, -1, 3, 1, -1, 3>, Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3>, Eigen::Matrix<double, -1, -1, 0, -1, -1> >(FILE*, Eigen::PlainObjectBase<Eigen::Matrix<float, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<unsigned int, -1, 3, 1, -1, 3> >&, Eigen::PlainObjectBase<Eigen::Matrix<double, -1, -1, 0, -1, -1> >&);
#endif