1510ea49d188944aa8154e03fa21ecee1e8ba523
[aukio-3d.git] /
1 /*
2  * Aukio 3D engine. Author: Svjatoslav Agejenko.
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.aukio.e3d.renderer.raster.shapes.composite.wireframe;
6
7 import eu.svjatoslav.aukio.e3d.geometry.Point3D;
8 import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.basic.line.Line;
9 import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.basic.line.LineAppearance;
10 import eu.svjatoslav.aukio.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
11
12 /**
13  * A freeform polyline drawing tool that connects sequential points with line
14  * segments. Points are added one at a time via {@link #addPoint(Point3D)};
15  * each new point is connected to the previously added point by a line.
16  *
17  * <p>The first point added establishes the starting position without drawing
18  * a line. Each subsequent point creates a new line segment from the previous
19  * point to the new one.</p>
20  *
21  * <p>This shape is useful for drawing paths, trails, trajectories, or
22  * arbitrary wireframe shapes that are defined as a sequence of vertices.</p>
23  *
24  * <p><b>Usage example:</b></p>
25  * <pre>{@code
26  * LineAppearance appearance = new LineAppearance(2, Color.YELLOW);
27  * WireframeDrawing drawing = new WireframeDrawing(appearance);
28  * drawing.addPoint(new Point3D(0, 0, 0));
29  * drawing.addPoint(new Point3D(100, 50, 0));
30  * drawing.addPoint(new Point3D(200, 0, 0));
31  * shapeCollection.addShape(drawing);
32  * }</pre>
33  *
34  * @see LineAppearance
35  * @see AbstractCompositeShape
36  */
37 public class WireframeDrawing extends AbstractCompositeShape {
38
39     /** The line appearance used for all segments in this drawing. */
40     final private LineAppearance lineAppearance;
41
42     /** The most recently added point, used as the start of the next line segment. */
43     Point3D currentPoint;
44
45     /**
46      * Constructs a new empty wireframe drawing with the given line appearance.
47      *
48      * @param lineAppearance the line appearance (color, width) used for all
49      *                       line segments added to this drawing
50      */
51     public WireframeDrawing(final LineAppearance lineAppearance) {
52         super();
53         this.lineAppearance = lineAppearance;
54     }
55
56     /**
57      * Adds a new point to the drawing. If this is the first point, it sets the
58      * starting position. Otherwise, a line segment is created from the previous
59      * point to this new point.
60      *
61      * <p>The point is defensively copied, so subsequent modifications to the
62      * passed {@code point3d} object will not affect the drawing.</p>
63      *
64      * @param point3d the point to add to the polyline
65      */
66     public void addPoint(final Point3D point3d) {
67         if (currentPoint != null) {
68             final Line line = lineAppearance.getLine(currentPoint, point3d);
69             addShape(line);
70         }
71
72         currentPoint = new Point3D(point3d);
73     }
74
75 }