1 package org.djutils.draw;
2
3 import java.util.Locale;
4
5 import org.djutils.draw.line.LineSegment2d;
6 import org.djutils.draw.line.LineSegment3d;
7 import org.djutils.draw.line.PolyLine2d;
8 import org.djutils.draw.line.PolyLine3d;
9 import org.djutils.draw.line.Polygon2d;
10 import org.djutils.draw.line.Polygon3d;
11
12 /**
13 * Export methods for djutils-draw objects.
14 * <p>
15 * Copyright (c) 2023-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16 * for project information <a href="https://djutils.org" target="_blank"> https://djutils.org</a>. The DJUTILS project is
17 * distributed under a three-clause BSD-style license, which can be found at
18 * <a href="https://djutils.org/docs/license.html" target="_blank"> https://djutils.org/docs/license.html</a>.
19 * </p>
20 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
21 * @author <a href="https://github.com/peter-knoppers">Peter Knoppers</a>
22 * @author <a href="https://github.com/wjschakel">Wouter Schakel</a>
23 */
24 public final class Export
25 {
26 /**
27 * Utility class; do not instantiate.
28 */
29 private Export()
30 {
31 // Do not instantiate
32 }
33
34 /**
35 * Convert a LineSegment2d to Peter's plot format.
36 * @param lineSegment the line segment to convert
37 * @return the line segment in Peter's plot format
38 * @throws NullPointerException when <code>lineSegment</code> is <code>null</code>
39 */
40 public static String toPlot(final LineSegment2d lineSegment)
41 {
42 return String.format(Locale.US, "M%.3f,%.3fL%.3f,%.3f\n", lineSegment.startX, lineSegment.startY, lineSegment.endX,
43 lineSegment.endY);
44 }
45
46 /**
47 * Convert a PolyLine2d to Peter's plot format.
48 * @param polyLine the polyline to convert
49 * @return the polyline in Peter's plot format
50 * @throws NullPointerException when <code>polyLine</code> is <code>null</code>
51 */
52 public static String toPlot(final PolyLine2d polyLine)
53 {
54 StringBuffer result = new StringBuffer();
55 for (int i = 0; i < polyLine.size(); i++)
56 {
57 result.append(String.format(Locale.US, "%s%.3f,%.3f", 0 == result.length() ? "M" : " L", polyLine.getX(i),
58 polyLine.getY(i)));
59 }
60 result.append("\n");
61 return result.toString();
62 }
63
64 /**
65 * Convert a Polygon2d into Peter's plot format.
66 * @param polygon the polygon to convert
67 * @return the polygon in Peter's plot format
68 * @throws NullPointerException when <code>polygon</code> is <code>null</code>
69 */
70 public static String toPlot(final Polygon2d polygon)
71 {
72 StringBuffer result = new StringBuffer();
73 for (int i = 0; i < polygon.size(); i++)
74 {
75 result.append(String.format(Locale.US, "%s%.3f,%.3f", 0 == result.length() ? "M" : " L", polygon.getX(i),
76 polygon.getY(i)));
77 }
78 result.append(String.format(Locale.US, " L%.3f,%.3f", polygon.getX(0), polygon.getY(0)));
79 result.append("\n");
80 return result.toString();
81 }
82
83 /**
84 * Convert a LineSegment2d into something that a TSV parser can handle.
85 * @param lineSegment the line segment to convert
86 * @return the line segment in TSV format
87 * @throws NullPointerException when <code>lineSegment</code> is <code>null</code>
88 */
89 public static String toTsv(final LineSegment2d lineSegment)
90 {
91 return lineSegment.startX + "\t" + lineSegment.startY + "\n" + lineSegment.endX + "\t" + lineSegment.endY + "\n";
92 }
93
94 /**
95 * Convert a PolyLine2d into something that a TSV parser can handle.
96 * @param polyLine the polyline to convert
97 * @return the polyline in TSV format
98 * @throws NullPointerException when <code>polyLine</code> is <code>null</code>
99 */
100 public static String toTsv(final PolyLine2d polyLine)
101 {
102 StringBuffer s = new StringBuffer();
103 for (int i = 0; i < polyLine.size(); i++)
104 {
105 s.append(polyLine.getX(i) + "\t" + polyLine.getY(i) + "\n");
106 }
107 return s.toString();
108 }
109
110 /**
111 * Convert a Polygon2d into something that a TSV parser can handle.
112 * @param polygon the polygon to convert
113 * @return the polygon in TSV format
114 * @throws NullPointerException when <code>polygon</code> is <code>null</code>
115 */
116 public static String toTsv(final Polygon2d polygon)
117 {
118 StringBuffer s = new StringBuffer();
119 for (int i = 0; i < polygon.size(); i++)
120 {
121 s.append(polygon.getX(i) + "\t" + polygon.getY(i) + "\n");
122 }
123 s.append(polygon.getX(0) + "\t" + polygon.getY(0) + "\n");
124 return s.toString();
125 }
126
127 /**
128 * Convert a LineSegment3d into something that a TSV parser can handle.
129 * @param lineSegment the line segment to convert
130 * @return the line segment in TSV format
131 * @throws NullPointerException when <code>lineSegment</code> is <code>null</code>
132 */
133 public static String toTsv(final LineSegment3d lineSegment)
134 {
135 return lineSegment.startX + "\t" + lineSegment.startY + "\t" + lineSegment.startZ + "\n" + lineSegment.endX + "\t"
136 + lineSegment.endY + "\t" + lineSegment.endZ + "\n";
137 }
138
139 /**
140 * Convert a PolyLine3d into something that a TSV parser can handle.
141 * @param polyline the polyline to convert
142 * @return the polyline in TSV format
143 * @throws NullPointerException when <code>polyline</code> is <code>null</code>
144 */
145 public static String toTsv(final PolyLine3d polyline)
146 {
147 StringBuffer s = new StringBuffer();
148 for (int i = 0; i < polyline.size(); i++)
149 {
150 s.append(polyline.getX(i) + "\t" + polyline.getY(i) + "\t" + polyline.getZ(i) + "\n");
151 }
152 return s.toString();
153 }
154
155 /**
156 * Convert a Polygon3d into something that a TSV parser can handle.
157 * @param polygon the polygon to convert
158 * @return the polygon in TSV format
159 * @throws NullPointerException when <code>polygon</code> is <code>null</code>
160 */
161 public static String toTsv(final Polygon3d polygon)
162 {
163 StringBuffer s = new StringBuffer();
164 for (int i = 0; i < polygon.size(); i++)
165 {
166 s.append(polygon.getX(i) + "\t" + polygon.getY(i) + "\t" + polygon.getZ(i) + "\n");
167 }
168 s.append(polygon.getX(0) + "\t" + polygon.getY(0) + "\t" + polygon.getZ(0) + "\n");
169 return s.toString();
170 }
171
172 }