ObjBoundaryReadHandler3D.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.geometry.io.euclidean.threed.obj;

  18. import java.io.Reader;
  19. import java.nio.charset.Charset;
  20. import java.nio.charset.StandardCharsets;

  21. import org.apache.commons.geometry.euclidean.threed.mesh.TriangleMesh;
  22. import org.apache.commons.geometry.io.core.GeometryFormat;
  23. import org.apache.commons.geometry.io.core.input.GeometryInput;
  24. import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
  25. import org.apache.commons.geometry.io.euclidean.threed.AbstractBoundaryReadHandler3D;
  26. import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
  27. import org.apache.commons.geometry.io.euclidean.threed.GeometryFormat3D;
  28. import org.apache.commons.numbers.core.Precision;

  29. /** {@link org.apache.commons.geometry.io.euclidean.threed.BoundaryReadHandler3D BoundaryReadHandler3D}
  30.  * implementation for reading OBJ data. Input is read using the UTF-8 charset by default.
  31.  */
  32. public class ObjBoundaryReadHandler3D extends AbstractBoundaryReadHandler3D {

  33.     /** Charset for reading text input. */
  34.     private Charset defaultCharset = StandardCharsets.UTF_8;

  35.     /** {@inheritDoc} */
  36.     @Override
  37.     public GeometryFormat getFormat() {
  38.         return GeometryFormat3D.OBJ;
  39.     }

  40.     /** Get the text input default charset, used if the input does not
  41.      * specify a charset.
  42.      * @return text input default charset
  43.      */
  44.     public Charset getDefaultCharset() {
  45.         return defaultCharset;
  46.     }

  47.     /** Set the text input default charset, used if the input does not
  48.      * specify a charset.
  49.      * @param charset text input default charset
  50.      */
  51.     public void setDefaultCharset(final Charset charset) {
  52.         this.defaultCharset = charset;
  53.     }

  54.     /** {@inheritDoc} */
  55.     @Override
  56.     public FacetDefinitionReader facetDefinitionReader(final GeometryInput in) {
  57.         return new ObjFacetDefinitionReader(createReader(in));
  58.     }

  59.     /** {@inheritDoc} */
  60.     @Override
  61.     public TriangleMesh readTriangleMesh(final GeometryInput in, final Precision.DoubleEquivalence precision) {
  62.         try (ObjTriangleMeshReader meshReader = new ObjTriangleMeshReader(createReader(in), precision)) {
  63.             return meshReader.readTriangleMesh();
  64.         }
  65.     }

  66.     /** Create a {@link Reader} for reading character data from the given input.
  67.      * @param in input to read from
  68.      * @return reader instance
  69.      * @throws java.io.UncheckedIOException if an I/O error occurs
  70.      */
  71.     private Reader createReader(final GeometryInput in) {
  72.         return GeometryIOUtils.createBufferedReader(in, defaultCharset);
  73.     }
  74. }