2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.math;
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
11 * It is used to store and apply transforms to points.
12 * Transforms are applied in the reverse order they were added to the pipeline.
14 * Transforms are stacked and then every point is affected by every transform in this stack.
15 * This is needed to support feature where objects in the world are attached to and are relative to each other.
18 * There is a ship it the sea. Ship moves along the sea and every object on the ship moves with it.
19 * Inside the ship there is a car. Car moves along the ship and every object on the car moves with it.
21 * So to calculate an absolute location in the world of particular object within a car,
22 * we must take into account object location relative to the car,
23 * car location relative to the ship,
24 * and ship location relative to the world.
27 public class TransformsStack {
31 * Transforms that are currently in the pipeline.
32 * Array is used for efficiency to avoid memory allocation during the rendering
33 * because depth of the pipeline needs to change very often.
35 private final Transform[] transforms = new Transform[100];
38 * The number of transforms in the pipeline.
40 private int transformsCount = 0;
43 * Adds a transform to the pipeline.
45 * @param transform The transform to add.
47 public void addTransform(final Transform transform) {
48 transforms[transformsCount] = transform;
53 * Clears the pipeline.
60 * Drops the last transform from the pipeline.
62 public void dropTransform() {
69 * @param coordinate Coordinate to be transformed into result. Original input coordinate is not modified.
70 * @param result Resulting transformed point. For efficiency reasons, result is stored into preexisting object
71 * that is passed here as an argument to avoid memory allocation.
73 public void transform(final Point3D coordinate, final Point3D result) {
75 result.clone(coordinate);
77 // Apply transforms in reverse order.
78 for (int i = transformsCount - 1; i >= 0; i--)
79 transforms[i].transform(result);