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.examples.io.threed.obj;
    18
    19  import java.io.BufferedWriter;
    20  import java.io.IOException;
    21  import java.io.InputStream;
    22  import java.io.InputStreamReader;
    23  import java.io.OutputStream;
    24  import java.io.OutputStreamWriter;
    25  import java.nio.charset.Charset;
    26  import java.nio.charset.StandardCharsets;
    27
    28  import org.apache.commons.geometry.core.precision.DoublePrecisionContext;
    29  import org.apache.commons.geometry.euclidean.threed.BoundarySource3D;
    30  import org.apache.commons.geometry.euclidean.threed.mesh.TriangleMesh;
    31  import org.apache.commons.geometry.examples.io.threed.AbstractModelIOHandler;
    32  import org.apache.commons.geometry.examples.io.threed.ModelIO;
    33
    34  /** {@link org.apache.commons.geometry.examples.io.threed.ModelIOHandler ModelIOHandler}
    35   * implementation for the OBJ file format.
    36   */
    37  public class OBJModelIOHandler extends AbstractModelIOHandler {
    38
    39      /** Charset for use with OBJ files. */
    40      private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;
    41
    42      /** {@inheritDoc} */
    43      @Override
    44      public boolean handlesType(final String type) {
    45          return ModelIO.OBJ.equalsIgnoreCase(type);
    46      }
    47
    48      /** {@inheritDoc} */
    49      @Override
    50      protected TriangleMesh readInternal(final String type, final InputStream in,
    51              final DoublePrecisionContext precision) throws IOException {
    52          try (InputStreamReader reader = new InputStreamReader(in, DEFAULT_CHARSET)) {
    53              final OBJReader objReader = new OBJReader();
    54              return objReader.readTriangleMesh(reader, precision);
    55          }
    56      }
    57
    58      /** {@inheritDoc} */
    59      @Override
    60      protected void writeInternal(final BoundarySource3D model, final String type, final OutputStream out)
    61              throws IOException {
    62          try (OBJWriter objWriter = new OBJWriter(new BufferedWriter(
    63                  new OutputStreamWriter(out, DEFAULT_CHARSET)))) {
    64              objWriter.writeBoundaries(model);
    65          }
    66      }
    67  }