API Documentation
Loading...
Searching...
No Matches
TimePath.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Design
28File: TimePath
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/Buffer.h>
34#include <NDEVR/Time.h>
35#include <NDEVR/TimeSpan.h>
36namespace NDEVR
37{
38 /**--------------------------------------------------------------------------------------------------
39 \brief An interpolated path in 3D space.
40 **/
41 class Path
42 {
43 public:
45 {}
46 Path(const Buffer<Time>& time, const Buffer<fltp04>& y, const Buffer<fltp04>& m)
47 : mX(time)
48 , mY(y)
49 , mM(m)
50 {}
51
52 Path(const Path& path)
53 : mX(path.mX)
54 , mY(path.mY)
55 , mM(path.mM)
56 {}
57
58 Path(const Path&& path)
59 : mX(std::move(path.mX))
60 , mY(std::move(path.mY))
61 , mM(std::move(path.mM))
62 {}
63
64 fltp08 interpolateX(const Time& current_time)
65 {
66 // Handle the boundary cases.
67 uint04 n = mX.size();
68 if (IsInvalid(current_time) || n == 0) {
70 }
71 if (current_time <= mX[0]) {
72 return mY[0];
73 }
74 if (current_time >= mX.last()) {
75 return mY.last();
76 }
77 // Find the index 'i' of the last point with smaller X.
78 // We know this will be within the spline due to the boundary tests.
79 int i = 0;
80 while (current_time >= mX[i + 1])
81 {
82 i += 1;
83 if (current_time == mX[i])
84 return mY[i];
85 }
86 // Perform cubic Hermite spline interpolation.
87 fltp08 h = (mX[i + 1] - mX[i]).elapsedSeconds();
88 fltp08 t = (current_time - mX[i]).elapsedSeconds() / h;
89 return (mY[i] * (1 + 2 * t) + h * mM[i] * t) * (1 - t) * (1 - t)
90 + (mY[i + 1] * (3 - 2 * t) + h * mM[i + 1] * (t - 1)) * t * t;
91 }
92
94 {
95 if (time.size() != y.size() || time.size() < 2)
96 throw Exception("There must be at least two control points and the arrays must be of equal length.");
97 uint04 n = time.size();
98 fltp04 object = 0.0f;
99 Buffer<fltp04> dx(n - 1, object); // could optimize this out
100 Buffer<fltp04> mx(n, object);
101 // Compute slopes of secant lines between successive points.
102 for (uint04 i = 0; i < n - 1; i++)
103 {
104 fltp08 h = (time[i + 1] - time[i]).elapsedSeconds();
105 if (h < 0.0)
106 throw Exception("The control points must all have strictly increasing X values:" + String(time[i]) + " " + String(time[i + 1]));
107 dx[i] = (y[i + 1] - y[i]) / cast<fltp04>(h);
108
109 }
110 // Initialize the tangents as the average of the secants.
111 mx[0] = dx[0];
112 for (uint04 i = 1; i < n - 1; i++)
113 {
114 mx[i] = (dx[i - 1] + dx[i]) * 0.5f;
115 }
116 mx[n - 1] = dx[n - 2];
117
118 // Update the tangents to preserve monotonicity.
119 for (uint04 i = 0; i < n - 1; i++)
120 {
121 if (dx[i] == 0.0f)
122 { // successive Y values are equal
123 mx[i] = 0.0f;
124 mx[i + 1] = 0.0f;
125 }
126 else
127 {
128 float a = mx[i] / dx[i];
129 float b = mx[i + 1] / dx[i];
130 if (a < 0.0f || b < 0.0f) {
131 //throw new IllegalArgumentException("The control points must have "
132 //+ "monotonic Y values.");
133 }
134 float h = (float)hypot(a, b);
135 if (h > 9.0f) {
136 float t = 3.0f / h;
137 mx[i] = t * a * dx[i];
138 mx[i + 1] = t * b * dx[i];
139 }
140 }
141 }
142 return Path(time, y, mx);
143 }
144
146 {
147 if (time.size() != y.size() || time.size() < 2)
148 {
149 throw new Exception("There must be at least two control points and the arrays must be of equal length.");
150 }
151 Buffer<Time> temp_time(y.size() * 2);
152 Buffer<fltp04> temp_y(y.size() * 2);
153 for (uint04 i = 0; i < time.size() - 1; i++)
154 {
155 temp_y.add(cast<fltp04>(y[i].as<DEGREES>()));
156 temp_time.add(time[i]);
157 if((y[i].as<DEGREES>() - y[i + 1].as<DEGREES>()) > 180.0)
158 {
159 temp_y.add( 179.99f);
160 temp_y.add(-179.99f);
161 temp_time.add(time[i] + (time[i + 1] - time[i]) / 2.0);
162 temp_time.add(time[i] + (time[i + 1] - time[i]) / 2.0);
163 }
164 if ((y[i].as<DEGREES>() - y[i + 1].as<DEGREES>()) > 180.0)
165 {
166 temp_y.add(-179.99f);
167 temp_y.add( 179.99f);
168 temp_time.add(time[i] + (time[i + 1] - time[i]) / 2.0);
169 temp_time.add(time[i] + (time[i + 1] - time[i]) / 2.0);
170 }
171 }
172 return createMonotoneCubicPath(temp_time, temp_y);
173 }
174
176 {
177 Buffer<std::pair<Time, fltp04>> sorted_path(time.size());
178 for (uint04 i = 0; i < time.size(); i++)
179 {
180 sorted_path.add(std::pair<Time, fltp04>(time[i], y[i]));
181 }
182 std::sort(sorted_path.begin(), sorted_path.end(), [](const std::pair<Time, fltp04>& p1, const std::pair<Time, fltp04>& p2)
183 {
184 return p1.first > p2.first;
185 });
186 Buffer<Time> corrected_time(time.size());
187 Buffer<fltp04> corrected_value(time.size());
188 for (uint04 i = 0; i < time.size(); i++)
189 {
190 corrected_value.add(sorted_path[i].second);
191 corrected_time.add(sorted_path[i].first);
192 }
193 return createMonotoneCubicPath(corrected_time, corrected_value);
194 }
196 {
197 Buffer<std::pair<Time, Angle<fltp08>>> sorted_path(time.size());
198 for (uint04 i = 0; i < time.size(); i++)
199 {
200 sorted_path.add(std::pair<Time, Angle<fltp08>>(time[i], y[i]));
201 }
202 std::sort(sorted_path.begin(), sorted_path.end(), [](const std::pair<Time, Angle<fltp08>>& p1, const std::pair<Time, Angle<fltp08>>& p2)
203 {
204 return p1.first > p2.first;
205 });
206 Buffer<Time> corrected_time(time.size());
207 Buffer<Angle<fltp08>> corrected_value(time.size());
208 for (uint04 i = 0; i < time.size(); i++)
209 {
210 corrected_value.add(sorted_path[i].second);
211 corrected_time.add(sorted_path[i].first);
212 }
213 return createMonotoneCubicPathAzimuth(corrected_time, corrected_value);
214 }
215
216 Path& operator=(const Path& path)
217 {
218 mX = path.mX;
219 mY = path.mY;
220 mM = path.mM;
221 return *this;
222 }
223 protected:
227 };
228}
229
230
231
232
The primary angle storage class for this API. Stores an angle in an optimized format.
Definition StringStream.h:540
The equivelent of std::vector but with a bit more control. The basic array unit of the library.
Definition Buffer.hpp:56
void add(t_type &&object)
Adds object to the end of the buffer.
Definition Buffer.hpp:186
constexpr t_index_type size() const
Definition Buffer.hpp:823
decltype(auto) last()
Definition Buffer.hpp:588
decltype(auto) end()
Definition Buffer.hpp:507
decltype(auto) begin()
Definition Buffer.hpp:402
Provides consistent interface to handle errors through the throw expression. All exceptions generated...
Definition Exception.hpp:47
An interpolated path in 3D space.
Definition TimePath.h:42
static Path createMonotoneCubicPathSorted(const Buffer< Time > &time, const Buffer< fltp04 > &y)
Definition TimePath.h:175
Path & operator=(const Path &path)
Definition TimePath.h:216
static Path createMonotoneAngleCubicPathSorted(const Buffer< Time > &time, const Buffer< Angle< fltp08 > > &y)
Definition TimePath.h:195
Path(const Path &path)
Definition TimePath.h:52
Buffer< Time > mX
Definition TimePath.h:224
static Path createMonotoneCubicPath(const Buffer< Time > &time, const Buffer< fltp04 > &y)
Definition TimePath.h:93
static Path createMonotoneCubicPathAzimuth(const Buffer< Time > &time, const Buffer< Angle< fltp08 > > &y)
Definition TimePath.h:145
Path(const Buffer< Time > &time, const Buffer< fltp04 > &y, const Buffer< fltp04 > &m)
Definition TimePath.h:46
fltp08 interpolateX(const Time &current_time)
Definition TimePath.h:64
Path()
Definition TimePath.h:44
Buffer< fltp04 > mM
Definition TimePath.h:226
Buffer< fltp04 > mY
Definition TimePath.h:225
Path(const Path &&path)
Definition TimePath.h:58
The core String class for the NDEVR API.
Definition String.h:69
Represents a timestamp with utilities for manipulation and conversion.
Definition Time.h:54
Definition ACIColor.h:37
constexpr bool IsInvalid(const t_type &value)
Query if 'value' is valid or invalid. Invalid values should return invalid if used for calculations o...
Definition BaseFunctions.hpp:170
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
Definition BaseValues.hpp:127
uint32_t uint04
-Defines an alias representing a 4 byte, unsigned integer -Can represent exact integer values 0 throu...
Definition BaseValues.hpp:96
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149
Definition File.h:211
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233