-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Added binary STL support. #802
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
3845568
Added binary STL support.
rhulha d3cf136
Merge branch 'master' into STL_Support
rhulha 7021840
Changed the Copyright date from 2016 to 2022.
rhulha 9a786c2
Merge branch 'master' into STL_Support
rhulha b528647
Added normal vector calculation in case normal data is missing from t…
rhulha 8975317
Merge remote-tracking branch 'origin/STL_Support' into STL_Support
rhulha 28e94bd
Return directly from the stl_decoder in mesh_io.cc.
rhulha ceb7f8f
Added test code.
rhulha 2315e26
Merge branch 'master' into STL_Support
rhulha 8bf6783
Renamed statusOrMesh to status_or_mesh.
rhulha 03a42a5
Merge branch 'master' into STL_Support
tomfinegan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // Copyright 2022 The Draco Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| #include "draco/io/stl_decoder.h" | ||
|
|
||
| #include "draco/core/macros.h" | ||
| #include "draco/core/status.h" | ||
| #include "draco/core/status_or.h" | ||
| #include "draco/io/file_utils.h" | ||
| #include "draco/mesh/triangle_soup_mesh_builder.h" | ||
|
|
||
| namespace draco { | ||
|
|
||
| StatusOr<std::unique_ptr<Mesh>> StlDecoder::DecodeFromFile( | ||
| const std::string &file_name) { | ||
| std::vector<char> data; | ||
| if (!ReadFileToBuffer(file_name, &data)) { | ||
| return Status(Status::IO_ERROR, "Unable to read input file."); | ||
| } | ||
| DecoderBuffer buffer; | ||
| buffer.Init(data.data(), data.size()); | ||
| return DecodeFromBuffer(&buffer); | ||
| } | ||
|
|
||
| StatusOr<std::unique_ptr<Mesh>> StlDecoder::DecodeFromBuffer( | ||
| DecoderBuffer *buffer) { | ||
| if (!strncmp(buffer->data_head() , "solid ", 6)) { | ||
| return Status(Status::IO_ERROR, | ||
| "Currently only binary STL files are supported."); | ||
| } | ||
| buffer->Advance(80); | ||
| uint32_t face_count; | ||
| buffer->Decode(&face_count, 4); | ||
|
|
||
| TriangleSoupMeshBuilder builder; | ||
| builder.Start(face_count); | ||
|
|
||
| const int32_t pos_att_id = builder.AddAttribute(GeometryAttribute::POSITION, | ||
| 3, DT_FLOAT32); | ||
| const int32_t norm_att_id = builder.AddAttribute(GeometryAttribute::NORMAL, | ||
| 3, DT_FLOAT32); | ||
|
|
||
| for (uint32_t i = 0; i < face_count; i++) { | ||
| float data[48]; | ||
| buffer->Decode(data, 48); | ||
| uint16_t unused; | ||
| buffer->Decode(&unused, 2); | ||
|
|
||
| builder.SetPerFaceAttributeValueForFace(norm_att_id, draco::FaceIndex(i), | ||
| draco::Vector3f(data[0], data[1], data[2]).data()); | ||
|
|
||
| builder.SetAttributeValuesForFace(pos_att_id, draco::FaceIndex(i), | ||
| draco::Vector3f(data[3], data[4], data[5]).data(), | ||
| draco::Vector3f(data[6], data[7], data[8]).data(), | ||
| draco::Vector3f(data[9], data[10], data[11]).data()); | ||
|
|
||
| } | ||
|
|
||
| std::unique_ptr<Mesh> mesh = builder.Finalize(); | ||
| return mesh; | ||
| } | ||
|
|
||
| } // namespace draco | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Copyright 2022 The Draco Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| #ifndef DRACO_IO_STL_DECODER_H_ | ||
| #define DRACO_IO_STL_DECODER_H_ | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "draco/core/decoder_buffer.h" | ||
| #include "draco/core/status.h" | ||
| #include "draco/core/status_or.h" | ||
| #include "draco/draco_features.h" | ||
| #include "draco/mesh/mesh.h" | ||
|
|
||
| namespace draco { | ||
|
|
||
| // Decodes an STL file into draco::Mesh (or draco::PointCloud if the | ||
| // connectivity data is not needed). | ||
| class StlDecoder { | ||
| public: | ||
| StatusOr<std::unique_ptr<Mesh>> DecodeFromFile(const std::string &file_name); | ||
| StatusOr<std::unique_ptr<Mesh>> DecodeFromBuffer(DecoderBuffer *buffer); | ||
| }; | ||
|
|
||
| } // namespace draco | ||
|
|
||
| #endif // DRACO_IO_STL_DECODER_H_ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| // Copyright 2022 The Draco Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| #include "draco/io/stl_decoder.h" | ||
| #include "draco/core/draco_test_base.h" | ||
| #include "draco/core/draco_test_utils.h" | ||
|
|
||
| namespace draco { | ||
|
|
||
| class StlDecoderTest : public ::testing::Test { | ||
| protected: | ||
|
|
||
| void test_decoding(const std::string &file_name) { | ||
| const std::string path = GetTestFileFullPath(file_name); | ||
| StlDecoder decoder; | ||
| DRACO_ASSIGN_OR_ASSERT(std::unique_ptr<Mesh> mesh, | ||
| decoder.DecodeFromFile(path)); | ||
| ASSERT_GT(mesh->num_faces(), 0); | ||
| ASSERT_GT(mesh->num_points(), 0); | ||
| } | ||
|
|
||
| void test_decoding_should_fail(const std::string &file_name) { | ||
| StlDecoder decoder; | ||
| StatusOr<std::unique_ptr<Mesh>> statusOrMesh = | ||
|
rhulha marked this conversation as resolved.
|
||
| decoder.DecodeFromFile(GetTestFileFullPath(file_name)); | ||
| ASSERT_FALSE(statusOrMesh.ok()); | ||
| } | ||
| }; | ||
|
|
||
| TEST_F(StlDecoderTest, TestStlDecoding) { | ||
| test_decoding("STL/bunny.stl"); | ||
| test_decoding("STL/test_sphere.stl"); | ||
| test_decoding_should_fail("STL/test_sphere_ascii.stl"); | ||
| } | ||
|
|
||
| } // namespace draco | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| // Copyright 2022 The Draco Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| #include "draco/io/stl_encoder.h" | ||
|
|
||
| #include <memory> | ||
| #include <iomanip> | ||
| #include <sstream> | ||
|
|
||
| #include "draco/io/file_writer_factory.h" | ||
| #include "draco/io/file_writer_interface.h" | ||
|
|
||
| namespace draco { | ||
|
|
||
| StlEncoder::StlEncoder() | ||
| : out_buffer_(nullptr), in_point_cloud_(nullptr), in_mesh_(nullptr) {} | ||
|
|
||
| Status StlEncoder::EncodeToFile(const Mesh &mesh, const std::string &file_name) { | ||
| in_mesh_ = &mesh; | ||
| std::unique_ptr<FileWriterInterface> file = | ||
| FileWriterFactory::OpenWriter(file_name); | ||
| if (!file) { | ||
| return Status(Status::IO_ERROR, "File couldn't be opened"); | ||
| } | ||
| // Encode the mesh into a buffer. | ||
| EncoderBuffer buffer; | ||
| DRACO_RETURN_IF_ERROR(EncodeToBuffer(mesh, &buffer)); | ||
| // Write the buffer into the file. | ||
| file->Write(buffer.data(), buffer.size()); | ||
| return OkStatus(); | ||
| } | ||
|
|
||
| Status StlEncoder::EncodeToBuffer(const Mesh &mesh, EncoderBuffer *out_buffer) { | ||
| in_mesh_ = &mesh; | ||
| out_buffer_ = out_buffer; | ||
| Status s = EncodeInternal(); | ||
| in_mesh_ = nullptr; // cleanup | ||
| in_point_cloud_ = nullptr; | ||
| out_buffer_ = nullptr; | ||
| return s; | ||
| } | ||
|
|
||
| Status StlEncoder::EncodeInternal() { | ||
| // Write STL header. | ||
| std::stringstream out; | ||
| out << std::left << std::setw(80) << "generated using Draco"; // header is 80 bytes fixed size. | ||
| const std::string header_str = out.str(); | ||
| buffer()->Encode(header_str.data(), header_str.length()); | ||
|
rhulha marked this conversation as resolved.
|
||
|
|
||
| uint32_t num_faces = in_mesh_->num_faces(); | ||
| buffer()->Encode(&num_faces, 4); | ||
|
|
||
| std::vector<uint8_t> stl_face; | ||
|
|
||
| const int pos_att_id = | ||
| in_mesh_->GetNamedAttributeId(GeometryAttribute::POSITION); | ||
|
|
||
| if (pos_att_id < 0) { | ||
| return ErrorStatus("Mesh is missing the position attribute."); | ||
| } | ||
|
|
||
| if (in_mesh_->attribute(pos_att_id)->data_type() != | ||
| DT_FLOAT32) { | ||
| return ErrorStatus("Mesh position attribute is not of type float32."); | ||
| } | ||
|
|
||
| uint16_t unused = 0; | ||
|
|
||
| if (in_mesh_) { | ||
| for (FaceIndex i(0); i < in_mesh_->num_faces(); ++i) { | ||
|
|
||
| const auto &f = in_mesh_->face(i); | ||
| const auto *const pos_att = in_mesh_->attribute(pos_att_id); | ||
|
|
||
| // The normal attribute can contain arbitrary normals that may not | ||
| // correspond to the winding of the face. | ||
| // Therefor we simply always calculate them | ||
| // using the points of the triangle face: norm(cross(p2-p1, p3-p1)) | ||
|
|
||
| Vector3f pos[3]; | ||
| pos_att->GetMappedValue(f[0], &pos[0][0]); | ||
| pos_att->GetMappedValue(f[1], &pos[1][0]); | ||
| pos_att->GetMappedValue(f[2], &pos[2][0]); | ||
| Vector3f norm = CrossProduct(pos[1] - pos[0], pos[2] - pos[0]); | ||
| norm.Normalize(); | ||
| buffer()->Encode(norm.data(), sizeof(float) * 3); | ||
|
|
||
| for (int c = 0; c < 3; ++c) { | ||
| buffer()->Encode(pos_att->GetAddress(pos_att->mapped_index(f[c])), | ||
| pos_att->byte_stride()); | ||
| } | ||
|
|
||
| buffer()->Encode(&unused, 2); | ||
|
|
||
| } | ||
| } | ||
| return OkStatus(); | ||
| } | ||
|
|
||
| } // namespace draco | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| // Copyright 2022 The Draco Authors. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
| #ifndef DRACO_IO_STL_ENCODER_H_ | ||
| #define DRACO_IO_STL_ENCODER_H_ | ||
|
|
||
| #include "draco/core/encoder_buffer.h" | ||
| #include "draco/mesh/mesh.h" | ||
|
|
||
| namespace draco { | ||
|
|
||
| // Class for encoding draco::Mesh into the STL file format. | ||
| class StlEncoder { | ||
| public: | ||
| StlEncoder(); | ||
|
|
||
| // Encodes the mesh and saves it into a file. | ||
| // Returns false when either the encoding failed or when the file couldn't be | ||
| // opened. | ||
| Status EncodeToFile(const Mesh &mesh, const std::string &file_name); | ||
|
|
||
| // Encodes the mesh into a buffer. | ||
| Status EncodeToBuffer(const Mesh &mesh, EncoderBuffer *out_buffer); | ||
|
|
||
| protected: | ||
| Status EncodeInternal(); | ||
| EncoderBuffer *buffer() const { return out_buffer_; } | ||
|
|
||
| private: | ||
|
|
||
| EncoderBuffer *out_buffer_; | ||
|
|
||
| const PointCloud *in_point_cloud_; | ||
| const Mesh *in_mesh_; | ||
| }; | ||
|
|
||
| } // namespace draco | ||
|
|
||
| #endif // DRACO_IO_STL_ENCODER_H_ |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.