001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.geometry.io.euclidean.threed.obj; 018 019import java.io.Reader; 020import java.nio.charset.Charset; 021import java.nio.charset.StandardCharsets; 022 023import org.apache.commons.geometry.euclidean.threed.mesh.TriangleMesh; 024import org.apache.commons.geometry.io.core.GeometryFormat; 025import org.apache.commons.geometry.io.core.input.GeometryInput; 026import org.apache.commons.geometry.io.core.internal.GeometryIOUtils; 027import org.apache.commons.geometry.io.euclidean.threed.AbstractBoundaryReadHandler3D; 028import org.apache.commons.geometry.io.euclidean.threed.FacetDefinitionReader; 029import org.apache.commons.geometry.io.euclidean.threed.GeometryFormat3D; 030import org.apache.commons.numbers.core.Precision; 031 032/** {@link org.apache.commons.geometry.io.euclidean.threed.BoundaryReadHandler3D BoundaryReadHandler3D} 033 * implementation for reading OBJ data. Input is read using the UTF-8 charset by default. 034 */ 035public class ObjBoundaryReadHandler3D extends AbstractBoundaryReadHandler3D { 036 037 /** Charset for reading text input. */ 038 private Charset defaultCharset = StandardCharsets.UTF_8; 039 040 /** {@inheritDoc} */ 041 @Override 042 public GeometryFormat getFormat() { 043 return GeometryFormat3D.OBJ; 044 } 045 046 /** Get the text input default charset, used if the input does not 047 * specify a charset. 048 * @return text input default charset 049 */ 050 public Charset getDefaultCharset() { 051 return defaultCharset; 052 } 053 054 /** Set the text input default charset, used if the input does not 055 * specify a charset. 056 * @param charset text input default charset 057 */ 058 public void setDefaultCharset(final Charset charset) { 059 this.defaultCharset = charset; 060 } 061 062 /** {@inheritDoc} */ 063 @Override 064 public FacetDefinitionReader facetDefinitionReader(final GeometryInput in) { 065 return new ObjFacetDefinitionReader(createReader(in)); 066 } 067 068 /** {@inheritDoc} */ 069 @Override 070 public TriangleMesh readTriangleMesh(final GeometryInput in, final Precision.DoubleEquivalence precision) { 071 try (ObjTriangleMeshReader meshReader = new ObjTriangleMeshReader(createReader(in), precision)) { 072 return meshReader.readTriangleMesh(); 073 } 074 } 075 076 /** Create a {@link Reader} for reading character data from the given input. 077 * @param in input to read from 078 * @return reader instance 079 * @throws java.io.UncheckedIOException if an I/O error occurs 080 */ 081 private Reader createReader(final GeometryInput in) { 082 return GeometryIOUtils.createBufferedReader(in, defaultCharset); 083 } 084}