1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.geometry.io.euclidean;
18
19 import java.nio.file.Path;
20 import java.nio.file.Paths;
21 import java.util.stream.Stream;
22
23 import org.apache.commons.geometry.euclidean.EuclideanTestUtils;
24 import org.apache.commons.geometry.euclidean.threed.AffineTransformMatrix3D;
25 import org.apache.commons.geometry.euclidean.threed.RegionBSPTree3D;
26 import org.apache.commons.geometry.euclidean.threed.Triangle3D;
27 import org.apache.commons.geometry.euclidean.threed.Vector3D;
28 import org.apache.commons.geometry.euclidean.threed.mesh.TriangleMesh;
29 import org.apache.commons.geometry.euclidean.threed.shape.Parallelepiped;
30 import org.apache.commons.geometry.euclidean.threed.shape.Sphere;
31 import org.apache.commons.geometry.io.core.input.FileGeometryInput;
32 import org.apache.commons.geometry.io.core.input.GeometryInput;
33 import org.apache.commons.geometry.io.core.output.FileGeometryOutput;
34 import org.apache.commons.geometry.io.core.output.GeometryOutput;
35 import org.apache.commons.geometry.io.euclidean.threed.IO3D;
36 import org.apache.commons.numbers.core.Precision;
37 import org.junit.jupiter.api.Assertions;
38 import org.junit.jupiter.api.Test;
39 import org.junit.jupiter.api.io.TempDir;
40
41 class DocumentationExamplesTest {
42
43 private static final double TEST_EPS = 1e-12;
44
45 @TempDir
46 Path tempDir;
47
48 @Test
49 void testIndexPageExample() {
50
51 final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-6);
52
53
54 final RegionBSPTree3D tree = Parallelepiped.unitCube(precision).toTree();
55
56
57 final Sphere sphere = Sphere.from(Vector3D.ZERO, 0.65, precision);
58
59
60
61 tree.difference(sphere.toTree(3));
62
63
64 final double size = tree.getSize();
65 final Vector3D centroid = tree.getCentroid();
66
67
68 final TriangleMesh mesh = tree.toTriangleMesh(precision);
69
70
71 IO3D.write(mesh, Paths.get("target/cube-minus-sphere.obj"));
72
73
74 Assertions.assertEquals(0.11509505362599505, size, TEST_EPS);
75 EuclideanTestUtils.assertCoordinatesEqual(Vector3D.ZERO, centroid, TEST_EPS);
76 }
77
78 @Test
79 void testIO3DExample() throws Exception {
80
81 final Path inputPath = Paths.get(EuclideanIOTestUtils.resource("/models/cube.obj").toURI());
82 final Path outputPath = tempDir.resolve("scaled.csv");
83
84 final Precision.DoubleEquivalence precision = Precision.doubleEquivalenceOfEpsilon(1e-6);
85
86 final GeometryInput input = new FileGeometryInput(inputPath);
87 final GeometryOutput scaledOutput = new FileGeometryOutput(outputPath);
88 final AffineTransformMatrix3D transform = AffineTransformMatrix3D.createScale(2);
89
90
91
92 try (Stream<Triangle3D> stream = IO3D.triangles(input, null, precision)) {
93 IO3D.write(stream.map(t -> t.transform(transform)), scaledOutput, null);
94 }
95
96
97 final RegionBSPTree3D result = IO3D.read(outputPath, precision).toTree();
98
99 Assertions.assertEquals(8.0, result.getSize(), TEST_EPS);
100
101 }
102 }