View Javadoc
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  
19  import java.io.Reader;
20  import java.nio.charset.Charset;
21  import java.nio.charset.StandardCharsets;
22  
23  import org.apache.commons.geometry.euclidean.threed.mesh.TriangleMesh;
24  import org.apache.commons.geometry.io.core.GeometryFormat;
25  import org.apache.commons.geometry.io.core.input.GeometryInput;
26  import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
27  import org.apache.commons.geometry.io.euclidean.threed.AbstractBoundaryReadHandler3D;
28  import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader;
29  import org.apache.commons.geometry.io.euclidean.threed.GeometryFormat3D;
30  import org.apache.commons.numbers.core.Precision;
31  
32  /** {@link org.apache.commons.geometry.io.euclidean.threed.BoundaryReadHandler3D BoundaryReadHandler3D}
33   * implementation for reading OBJ data. Input is read using the UTF-8 charset by default.
34   */
35  public class ObjBoundaryReadHandler3D extends AbstractBoundaryReadHandler3D {
36  
37      /** Charset for reading text input. */
38      private Charset defaultCharset = StandardCharsets.UTF_8;
39  
40      /** {@inheritDoc} */
41      @Override
42      public GeometryFormat getFormat() {
43          return GeometryFormat3D.OBJ;
44      }
45  
46      /** Get the text input default charset, used if the input does not
47       * specify a charset.
48       * @return text input default charset
49       */
50      public Charset getDefaultCharset() {
51          return defaultCharset;
52      }
53  
54      /** Set the text input default charset, used if the input does not
55       * specify a charset.
56       * @param charset text input default charset
57       */
58      public void setDefaultCharset(final Charset charset) {
59          this.defaultCharset = charset;
60      }
61  
62      /** {@inheritDoc} */
63      @Override
64      public FacetDefinitionReader facetDefinitionReader(final GeometryInput in) {
65          return new ObjFacetDefinitionReader(createReader(in));
66      }
67  
68      /** {@inheritDoc} */
69      @Override
70      public TriangleMesh readTriangleMesh(final GeometryInput in, final Precision.DoubleEquivalence precision) {
71          try (ObjTriangleMeshReader meshReader = new ObjTriangleMeshReader(createReader(in), precision)) {
72              return meshReader.readTriangleMesh();
73          }
74      }
75  
76      /** Create a {@link Reader} for reading character data from the given input.
77       * @param in input to read from
78       * @return reader instance
79       * @throws java.io.UncheckedIOException if an I/O error occurs
80       */
81      private Reader createReader(final GeometryInput in) {
82          return GeometryIOUtils.createBufferedReader(in, defaultCharset);
83      }
84  }