|
| 1 | +// Copyright 2022 The Draco Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +#include "draco/io/stl_encoder.h" |
| 16 | + |
| 17 | +#include <memory> |
| 18 | +#include <iomanip> |
| 19 | +#include <sstream> |
| 20 | + |
| 21 | +#include "draco/io/file_writer_factory.h" |
| 22 | +#include "draco/io/file_writer_interface.h" |
| 23 | + |
| 24 | +namespace draco { |
| 25 | + |
| 26 | +StlEncoder::StlEncoder() |
| 27 | + : out_buffer_(nullptr), in_point_cloud_(nullptr), in_mesh_(nullptr) {} |
| 28 | + |
| 29 | +Status StlEncoder::EncodeToFile(const Mesh &mesh, const std::string &file_name) { |
| 30 | + in_mesh_ = &mesh; |
| 31 | + std::unique_ptr<FileWriterInterface> file = |
| 32 | + FileWriterFactory::OpenWriter(file_name); |
| 33 | + if (!file) { |
| 34 | + return Status(Status::IO_ERROR, "File couldn't be opened"); |
| 35 | + } |
| 36 | + // Encode the mesh into a buffer. |
| 37 | + EncoderBuffer buffer; |
| 38 | + DRACO_RETURN_IF_ERROR(EncodeToBuffer(mesh, &buffer)); |
| 39 | + // Write the buffer into the file. |
| 40 | + file->Write(buffer.data(), buffer.size()); |
| 41 | + return OkStatus(); |
| 42 | +} |
| 43 | + |
| 44 | +Status StlEncoder::EncodeToBuffer(const Mesh &mesh, EncoderBuffer *out_buffer) { |
| 45 | + in_mesh_ = &mesh; |
| 46 | + out_buffer_ = out_buffer; |
| 47 | + Status s = EncodeInternal(); |
| 48 | + in_mesh_ = nullptr; // cleanup |
| 49 | + in_point_cloud_ = nullptr; |
| 50 | + out_buffer_ = nullptr; |
| 51 | + return s; |
| 52 | +} |
| 53 | + |
| 54 | +Status StlEncoder::EncodeInternal() { |
| 55 | + // Write STL header. |
| 56 | + std::stringstream out; |
| 57 | + out << std::left << std::setw(80) << "generated using Draco"; // header is 80 bytes fixed size. |
| 58 | + const std::string header_str = out.str(); |
| 59 | + buffer()->Encode(header_str.data(), header_str.length()); |
| 60 | + |
| 61 | + uint32_t num_faces = in_mesh_->num_faces(); |
| 62 | + buffer()->Encode(&num_faces, 4); |
| 63 | + |
| 64 | + std::vector<uint8_t> stl_face; |
| 65 | + |
| 66 | + const int pos_att_id = |
| 67 | + in_mesh_->GetNamedAttributeId(GeometryAttribute::POSITION); |
| 68 | + |
| 69 | + if (pos_att_id < 0) { |
| 70 | + return ErrorStatus("Mesh is missing the position attribute."); |
| 71 | + } |
| 72 | + |
| 73 | + if (in_mesh_->attribute(pos_att_id)->data_type() != |
| 74 | + DT_FLOAT32) { |
| 75 | + return ErrorStatus("Mesh position attribute is not of type float32."); |
| 76 | + } |
| 77 | + |
| 78 | + uint16_t unused = 0; |
| 79 | + |
| 80 | + if (in_mesh_) { |
| 81 | + for (FaceIndex i(0); i < in_mesh_->num_faces(); ++i) { |
| 82 | + |
| 83 | + const auto &f = in_mesh_->face(i); |
| 84 | + const auto *const pos_att = in_mesh_->attribute(pos_att_id); |
| 85 | + |
| 86 | + // The normal attribute can contain arbitrary normals that may not |
| 87 | + // correspond to the winding of the face. |
| 88 | + // Therefor we simply always calculate them |
| 89 | + // using the points of the triangle face: norm(cross(p2-p1, p3-p1)) |
| 90 | + |
| 91 | + Vector3f pos[3]; |
| 92 | + pos_att->GetMappedValue(f[0], &pos[0][0]); |
| 93 | + pos_att->GetMappedValue(f[1], &pos[1][0]); |
| 94 | + pos_att->GetMappedValue(f[2], &pos[2][0]); |
| 95 | + Vector3f norm = CrossProduct(pos[1] - pos[0], pos[2] - pos[0]); |
| 96 | + norm.Normalize(); |
| 97 | + buffer()->Encode(norm.data(), sizeof(float) * 3); |
| 98 | + |
| 99 | + for (int c = 0; c < 3; ++c) { |
| 100 | + buffer()->Encode(pos_att->GetAddress(pos_att->mapped_index(f[c])), |
| 101 | + pos_att->byte_stride()); |
| 102 | + } |
| 103 | + |
| 104 | + buffer()->Encode(&unused, 2); |
| 105 | + |
| 106 | + } |
| 107 | + } |
| 108 | + return OkStatus(); |
| 109 | +} |
| 110 | + |
| 111 | +} // namespace draco |
0 commit comments