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.txt;
    18
    19  import java.io.ByteArrayOutputStream;
    20  import java.nio.charset.StandardCharsets;
    21  import java.text.DecimalFormat;
    22  import java.text.DecimalFormatSymbols;
    23  import java.util.Arrays;
    24  import java.util.List;
    25  import java.util.Locale;
    26
    27  import org.apache.commons.geometry.euclidean.threed.BoundarySource3D;
    28  import org.apache.commons.geometry.euclidean.threed.Planes;
    29  import org.apache.commons.geometry.euclidean.threed.Vector3D;
    30  import org.apache.commons.geometry.io.core.output.StreamGeometryOutput;
    31  import org.apache.commons.geometry.io.core.test.CloseCountOutputStream;
    32  import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;
    33  import org.apache.commons.geometry.io.euclidean.threed.GeometryFormat3D;
    34  import org.apache.commons.geometry.io.euclidean.threed.SimpleFacetDefinition;
    35  import org.apache.commons.numbers.core.Precision;
    36  import org.junit.jupiter.api.Assertions;
    37  import org.junit.jupiter.api.Test;
    38
    39  class TextBoundaryWriteHandler3DTest {
    40
    41      private static final double TEST_EPS = 1e-10;
    42
    43      private static final Precision.DoubleEquivalence TEST_PRECISION =
    44              Precision.doubleEquivalenceOfEpsilon(TEST_EPS);
    45
    46      private static final List<FacetDefinition> TRI_FACETS = Arrays.asList(new SimpleFacetDefinition(
    47              Arrays.asList(Vector3D.ZERO, Vector3D.of(1.0 / 3.0, 0, 0), Vector3D.of(1, 1, 0))));
    48
    49      private static final List<FacetDefinition> QUAD_FACETS = Arrays.asList(new SimpleFacetDefinition(
    50              Arrays.asList(Vector3D.ZERO, Vector3D.of(1.0 / 3.0, 0, 0), Vector3D.of(1, 1, 0), Vector3D.of(0, 1, 0))));
    51
    52      private static final BoundarySource3D QUAD_SRC = BoundarySource3D.of(
    53              Planes.convexPolygonFromVertices(Arrays.asList(
    54                      Vector3D.ZERO, Vector3D.of(1.0 / 3.0, 0, 0), Vector3D.of(1, 1, 0), Vector3D.of(0, 1, 0)),
    55                      TEST_PRECISION));
    56
    57      private final ByteArrayOutputStream out = new ByteArrayOutputStream();
    58
    59      @Test
    60      void testProperties() {
    61          // arrange
    62          final TextBoundaryWriteHandler3D handler = new TextBoundaryWriteHandler3D();
    63
    64          // act/assert
    65          Assertions.assertEquals(GeometryFormat3D.TXT, handler.getFormat());
    66          Assertions.assertEquals(StandardCharsets.UTF_8, handler.getDefaultCharset());
    67          Assertions.assertEquals("\n", handler.getLineSeparator());
    68          Assertions.assertEquals(" ", handler.getVertexComponentSeparator());
    69          Assertions.assertEquals("; ", handler.getVertexSeparator());
    70          Assertions.assertNotNull(handler.getDoubleFormat());
    71          Assertions.assertEquals(-1, handler.getFacetVertexCount());
    72      }
    73
    74      @Test
    75      void testWriteFacets() {
    76          // arrange
    77          final TextBoundaryWriteHandler3D handler = new TextBoundaryWriteHandler3D();
    78          final CloseCountOutputStream closeOut = new CloseCountOutputStream(out);
    79
    80          // act
    81          handler.writeFacets(TRI_FACETS, new StreamGeometryOutput(closeOut));
    82
    83          // assert
    84          Assertions.assertEquals(1, closeOut.getCloseCount());
    85          Assertions.assertEquals(
    86                  "0.0 0.0 0.0; 0.3333333333333333 0.0 0.0; 1.0 1.0 0.0\n", new String(out.toByteArray(), StandardCharsets.UTF_8));
    87      }
    88
    89      @Test
    90      void testWriteFacets_usesOutputCharset() {
    91          // arrange
    92          final TextBoundaryWriteHandler3D handler = new TextBoundaryWriteHandler3D();
    93          final CloseCountOutputStream closeOut = new CloseCountOutputStream(out);
    94
    95          // act
    96          handler.writeFacets(TRI_FACETS, new StreamGeometryOutput(closeOut, null, StandardCharsets.UTF_16));
    97
    98          // assert
    99          Assertions.assertEquals(1, closeOut.getCloseCount());
    100         Assertions.assertEquals(
    101                 "0.0 0.0 0.0; 0.3333333333333333 0.0 0.0; 1.0 1.0 0.0\n", new String(out.toByteArray(), StandardCharsets.UTF_16));
    102     }
    103
    104     @Test
    105     void testWriteFacets_customConfiguration() {
    106         // arrange
    107         final DecimalFormat fmt =
    108                 new DecimalFormat("0.0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    109
    110         final TextBoundaryWriteHandler3D handler = new TextBoundaryWriteHandler3D();
    111         handler.setDefaultCharset(StandardCharsets.UTF_16);
    112         handler.setLineSeparator("\r\n");
    113         handler.setDoubleFormat(fmt::format);
    114         handler.setVertexComponentSeparator("|");
    115         handler.setVertexSeparator(" | ");
    116         handler.setFacetVertexCount(4);
    117
    118         final CloseCountOutputStream closeOut = new CloseCountOutputStream(out);
    119
    120         // act
    121         handler.writeFacets(QUAD_FACETS, new StreamGeometryOutput(closeOut));
    122
    123         // assert
    124         Assertions.assertEquals(1, closeOut.getCloseCount());
    125         Assertions.assertEquals(
    126                 "0.0|0.0|0.0 | 0.3|0.0|0.0 | 1.0|1.0|0.0 | 0.0|1.0|0.0\r\n", new String(out.toByteArray(), StandardCharsets.UTF_16));
    127     }
    128
    129     @Test
    130     void testWriteBoundarySource() {
    131         // arrange
    132         final TextBoundaryWriteHandler3D handler = new TextBoundaryWriteHandler3D();
    133         final CloseCountOutputStream closeOut = new CloseCountOutputStream(out);
    134
    135         // act
    136         handler.write(QUAD_SRC, new StreamGeometryOutput(closeOut));
    137
    138         // assert
    139         Assertions.assertEquals(1, closeOut.getCloseCount());
    140         Assertions.assertEquals(
    141                 "0.0 0.0 0.0; 0.3333333333333333 0.0 0.0; 1.0 1.0 0.0; 0.0 1.0 0.0\n", new String(out.toByteArray(), StandardCharsets.UTF_8));
    142     }
    143
    144     @Test
    145     void testWriteBoundarySource_customConfiguration() {
    146         // arrange
    147         // arrange
    148         final DecimalFormat fmt =
    149                 new DecimalFormat("0.0", DecimalFormatSymbols.getInstance(Locale.ENGLISH));
    150
    151         final TextBoundaryWriteHandler3D handler = new TextBoundaryWriteHandler3D();
    152         handler.setDefaultCharset(StandardCharsets.UTF_16);
    153         handler.setLineSeparator("\r\n");
    154         handler.setDoubleFormat(fmt::format);
    155         handler.setVertexComponentSeparator("|");
    156         handler.setVertexSeparator(" | ");
    157         handler.setFacetVertexCount(4);
    158
    159         final CloseCountOutputStream closeOut = new CloseCountOutputStream(out);
    160
    161         // act
    162         handler.write(QUAD_SRC, new StreamGeometryOutput(closeOut));
    163
    164         // assert
    165         Assertions.assertEquals(1, closeOut.getCloseCount());
    166         Assertions.assertEquals(
    167                 "0.0|0.0|0.0 | 0.3|0.0|0.0 | 1.0|1.0|0.0 | 0.0|1.0|0.0\r\n", new String(out.toByteArray(), StandardCharsets.UTF_16));
    168     }
    169 }