|
| 1 | +/* Copyright 2021 The TensorFlow Authors. All Rights Reserved. |
| 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 | + |
| 16 | +#include "tensorflow/core/framework/op_kernel.h" |
| 17 | +#include "tensorflow/core/platform/logging.h" |
| 18 | +#include "tiny_obj_loader.h" |
| 19 | + |
| 20 | +namespace tensorflow { |
| 21 | +namespace io { |
| 22 | +namespace { |
| 23 | + |
| 24 | +class DecodeObjOp : public OpKernel { |
| 25 | + public: |
| 26 | + explicit DecodeObjOp(OpKernelConstruction* context) : OpKernel(context) {} |
| 27 | + |
| 28 | + void Compute(OpKernelContext* context) override { |
| 29 | + const Tensor* input_tensor; |
| 30 | + OP_REQUIRES_OK(context, context->input("input", &input_tensor)); |
| 31 | + OP_REQUIRES(context, TensorShapeUtils::IsScalar(input_tensor->shape()), |
| 32 | + errors::InvalidArgument("input must be scalar, got shape ", |
| 33 | + input_tensor->shape().DebugString())); |
| 34 | + const tstring& input = input_tensor->scalar<tstring>()(); |
| 35 | + |
| 36 | + tinyobj::ObjReader reader; |
| 37 | + |
| 38 | + if (!reader.ParseFromString(input.c_str(), "")) { |
| 39 | + OP_REQUIRES( |
| 40 | + context, false, |
| 41 | + errors::Internal("Unable to read obj file: ", reader.Error())); |
| 42 | + } |
| 43 | + |
| 44 | + if (!reader.Warning().empty()) { |
| 45 | + LOG(WARNING) << "TinyObjReader: " << reader.Warning(); |
| 46 | + } |
| 47 | + |
| 48 | + auto& attrib = reader.GetAttrib(); |
| 49 | + |
| 50 | + int64 count = attrib.vertices.size() / 3; |
| 51 | + |
| 52 | + Tensor* output_tensor = nullptr; |
| 53 | + OP_REQUIRES_OK(context, context->allocate_output(0, TensorShape({count, 3}), |
| 54 | + &output_tensor)); |
| 55 | + // Loop over attrib.vertices: |
| 56 | + for (int64 i = 0; i < count; i++) { |
| 57 | + tinyobj::real_t x = attrib.vertices[i * 3 + 0]; |
| 58 | + tinyobj::real_t y = attrib.vertices[i * 3 + 1]; |
| 59 | + tinyobj::real_t z = attrib.vertices[i * 3 + 2]; |
| 60 | + output_tensor->tensor<float, 2>()(i, 0) = x; |
| 61 | + output_tensor->tensor<float, 2>()(i, 1) = y; |
| 62 | + output_tensor->tensor<float, 2>()(i, 2) = z; |
| 63 | + } |
| 64 | + } |
| 65 | +}; |
| 66 | +REGISTER_KERNEL_BUILDER(Name("IO>DecodeObj").Device(DEVICE_CPU), DecodeObjOp); |
| 67 | + |
| 68 | +} // namespace |
| 69 | +} // namespace io |
| 70 | +} // namespace tensorflow |
0 commit comments