AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
Polynomial.h
Go to the documentation of this file.
1/*
2Copyright 2007-2024. Algoryx Simulation AB.
3
4All AGX source code, intellectual property, documentation, sample code,
5tutorials, scene files and technical white papers, are copyrighted, proprietary
6and confidential material of Algoryx Simulation AB. You may not download, read,
7store, distribute, publish, copy or otherwise disseminate, use or expose this
8material unless having a written signed agreement with Algoryx Simulation AB, or having been
9advised so by Algoryx Simulation AB for a time limited evaluation, or having purchased a
10valid commercial license from Algoryx Simulation AB.
11
12Algoryx Simulation AB disclaims all responsibilities for loss or damage caused
13from using this software, unless otherwise stated in written agreements with
14Algoryx Simulation AB.
15*/
16
17#ifndef AGXCOLLIDE_POLYNOMIAL_H
18#define AGXCOLLIDE_POLYNOMIAL_H
19
20#include <agx/Math.h>
22#include <agx/StackArray.h>
23
24
25namespace agxCollide
26{
27 template <size_t N>
28 class Polynomial {
29
30 public:
31
34 Polynomial( const agx::StackArray<agx::Real, N>& coefficients );
35
43 const agx::Real intervalStart,
44 const agx::Real intervalEnd,
46
49
52
56 const agx::Real a,
57 const agx::Real b,
58 const agx::Real intervalStart,
59 const agx::Real intervalEnd,
61
65 const agx::Real a,
66 const agx::Real b,
67 const agx::Real c,
68 const agx::Real intervalStart,
69 const agx::Real intervalEnd,
71
72 private:
73 agx::StackArray<agx::Real, N> m_coefficients;
74
75 };
76
77
79
80 template <size_t N>
82 m_coefficients = coefficients;
83 }
84
85 template<>
86 inline void Polynomial<size_t(2)>::findRealRootsInInterval(
87 const agx::Real intervalStart,
88 const agx::Real intervalEnd,
90 {
91 switch(m_coefficients.size()) {
92 case 2:
93 {
94 // linear case
96 findRootsOfLinearInInterval( m_coefficients[0], m_coefficients[1],
97 intervalStart, intervalEnd, rootsMinor );
98 roots = rootsMinor;
99 break;
100 }
101 default:
102 break;
103 }
104 return;
105 }
106
107
108 template <size_t N>
110 const agx::Real intervalStart,
111 const agx::Real intervalEnd,
113 {
114 switch (m_coefficients.size()) {
115 case 0:
116 break;
117 case 1:
118 break;
119 case 2:
120 {
121 // linear case
123 findRootsOfLinearInInterval( m_coefficients[0], m_coefficients[1],
124 intervalStart, intervalEnd, rootsMinor );
125 roots = rootsMinor;
126 }
127 break;
128 default:
129 {
130 if (agx::equalsZero( m_coefficients[0] )) { // leading coefficient zero
131 agx::StackArray<agx::Real, N - 1> coefficientsWithoutLeading;
132 for (size_t i = 1; i < m_coefficients.size(); ++i)
133 coefficientsWithoutLeading.push_back( m_coefficients[i] );
134 Polynomial<N - 1> withoutLeadingCoefficient( coefficientsWithoutLeading );
135 agx::StackArray<agx::Real, N - 2> rootsMinor;
136 withoutLeadingCoefficient.findRealRootsInInterval( intervalStart, intervalEnd, rootsMinor );
137 roots = rootsMinor;
138 return;
139 }
140
141 Polynomial<N - 1> polyDer = derive();
142 agx::StackArray<agx::Real, N - 2> rootsOfDerivative;
143 polyDer.findRealRootsInInterval( intervalStart, intervalEnd, rootsOfDerivative );
144
145 // calculate interval borders
146 agx::StackArray<agx::Real, N> intervalBorders;
147 intervalBorders.push_back( intervalStart );
148 if (rootsOfDerivative.size() > 0) {
149 intervalBorders.push_back( rootsOfDerivative[0] );
150 for (size_t i = 1; i < rootsOfDerivative.size(); ++i) {
151 if (!agx::equalsZero( rootsOfDerivative[i] - rootsOfDerivative[i - 1] ))
152 intervalBorders.push_back( rootsOfDerivative[i] ); // not interested in multiples of roots
153 }
154 }
155 intervalBorders.push_back( intervalEnd );
156
157 const agx::Real eps(agx::REAL_SQRT_EPSILON * 10); // For evaluating derivatives along interval.
158 const agx::Real zeroTolerance(agx::REAL_SQRT_EPSILON * 0.001);
159 const size_t maxIter = 50;
160
161 for (size_t intervalNr = 0; intervalNr < intervalBorders.size() - 1; intervalNr++) {
162 const agx::Real localMin = intervalBorders[intervalNr];
163 const agx::Real localMax = intervalBorders[intervalNr + 1];
164 const agx::Real localEps = std::min(eps, (localMax - localMin) * 0.001);
165 // check at interval borders (derivative might be zero here)
166 if (agx::equalsZero( evaluateAt( localMin ), zeroTolerance ) && (roots.size() == 0 || !agx::equivalent(roots[roots.size() - 1], localMin, eps))) {
167 roots.push_back( localMin );
168 }
169 else {
170
171 agx::Real fAtMin = evaluateAt( localMin + localEps);
172 agx::Real fAtMax = evaluateAt( localMax - localEps);
173 if (agx::sign( fAtMin ) != agx::sign( fAtMax )) { // only interested in intervals where sign changes happen
174 agx::Real xTmp = agx::Real(0.5) * (localMin + localMax);
175 agx::Real f = evaluateAt( xTmp );
176 size_t localIter = 0;
177 // For cycle detection: Make update factor smaller if in cycle.
178 agx::Real fPrevious = f;
179 agx::Real factor = 1.0;
180 while (!agx::equalsZero( f, zeroTolerance ) && localIter < maxIter) {
181 agx::Real df = polyDer.evaluateAt( xTmp );
182 xTmp -= factor * f/df; // we never allow df to become zero, by construction
183 xTmp = agx::clamp(xTmp, localMin + localEps, localMax - localEps);
184 fPrevious = f;
185 f = evaluateAt( xTmp );
186 if (agx::geq(agx::absolute(f), agx::absolute(fPrevious)))
187 factor *= 0.9;
188 ++localIter;
189 }
190 if (agx::equalsZero( f, zeroTolerance ) && (roots.size() == 0 || !agx::equivalent(roots[roots.size() - 1], xTmp, eps))) {
191 roots.push_back( xTmp );
192 }
193 }
194 }
195 }
196 }
197 }
198 }
199
200
201 template <size_t N>
203 {
204 agx::StackArray<agx::Real, N - 1> deriveCoefficients;
205
206 for (int i = 0; i < int(m_coefficients.size()) - 1; ++i)
207 deriveCoefficients.push_back( (agx::Real)(m_coefficients.size() - 1 - i) * m_coefficients[i] );
208
209 return Polynomial<N - 1>( deriveCoefficients );
210 }
211
212
213 template <size_t N>
215 {
216 // evaluating using Horner's scheme
217 agx::Real polyVal = 0;
218 for (size_t i = 0; i < m_coefficients.size(); ++i)
219 polyVal = m_coefficients[i] + polyVal * x;
220
221 return polyVal;
222 }
223
224
225 template <size_t N>
227 const agx::Real a,
228 const agx::Real b,
229 const agx::Real intervalStart,
230 const agx::Real intervalEnd,
232 {
233 if (!agx::equalsZero( a )) { // non-constant case
234 agx::Real xTmp = -b / a;
235 if (xTmp >= intervalStart && xTmp <= intervalEnd)
236 root.push_back(xTmp);
237 }
238 return;
239 }
240
241
242 template <size_t N>
244 const agx::Real a,
245 const agx::Real b,
246 const agx::Real c,
247 const agx::Real intervalStart,
248 const agx::Real intervalEnd,
250 {
251 if (agx::equalsZero( a )) { // linear case
253 findRootsOfLinearInInterval( b, c, intervalStart, intervalEnd, rootsMinor );
254 roots = rootsMinor;
255 }
256 else {
257 agx::Real D = b * b - 4 * a * c;
258
259 if (agx::equalsZero( D )) { // determinant equals zero, 1 solution
260 agx::Real xTmp = -agx::Real(0.5) * b / a;
261 if (xTmp >= intervalStart && xTmp <= intervalEnd)
262 roots.push_back( xTmp );
263 }
264 else {
265 if (D > 0) {
266 agx::Real aInv = 1 / a; // two agx::Real solutions
267 agx::Real sqrtD = std::sqrt(D);
268 agx::Real xTmp = agx::Real(0.5) * (-b - sqrtD) * aInv;
269 if (xTmp >= intervalStart && xTmp <= intervalEnd)
270 roots.push_back( xTmp );
271 agx::Real xTmp2 = agx::Real(0.5) * (-b + sqrtD) * aInv;
272 if (xTmp2 >= intervalStart && xTmp2 <= intervalEnd)
273 roots.push_back( xTmp2 );
274 }
275 }
276 }
277 }
278
279}
280
281
282#endif // AGXCOLLIDE_POLYNOMIAL_H
Polynomial(const agx::StackArray< agx::Real, N > &coefficients)
Constructor: creates a Polynomial with given coefficients.
Definition Polynomial.h:81
static void findRootsOfLinearInInterval(const agx::Real a, const agx::Real b, const agx::Real intervalStart, const agx::Real intervalEnd, agx::StackArray< agx::Real, 1 > &root)
Solves linear function ax + b = 0; returns only agx::Real solutions within given interval Assume it h...
Definition Polynomial.h:226
static void findRootsOfQuadraticInInterval(const agx::Real a, const agx::Real b, const agx::Real c, const agx::Real intervalStart, const agx::Real intervalEnd, agx::StackArray< agx::Real, 2 > &roots)
Solves quadratic function a x^2 + bx + c, returns only agx::Real solutions within time interval.
Definition Polynomial.h:243
agx::Real evaluateAt(const agx::Real x) const
Evaluates polynomial given by polyCoefficients at value x.
Definition Polynomial.h:214
Polynomial< N-1 > derive() const
Calculates derivation of a polygon.
Definition Polynomial.h:202
void findRealRootsInInterval(const agx::Real intervalStart, const agx::Real intervalEnd, agx::StackArray< agx::Real, N - 1 > &roots) const
Calculates agx::Real roots of a single variable polynomial within a given boundary.
Definition Polynomial.h:109
Templated stack array class.
size_t size() const
Get the size of the array (number of filled slots).
void push_back(const T &value)
#define AGX_FORCE_INLINE
Definition macros.h:58
This namespace consists of a set of classes for handling geometric intersection tests including boole...
T sign(T v)
Definition Math.h:328
T absolute(T v)
return the absolute value.
Definition Math.h:291
bool geq(double a, double b, double eps=(double) AGX_EQUIVALENT_EPSILON)
Definition Math.h:381
AGXCORE_EXPORT const Real REAL_SQRT_EPSILON
T1 clamp(T1 v, T2 minimum, T3 maximum)
Definition Math.h:318
AGXPHYSICS_EXPORT agx::Bool equalsZero(const agx::AddedMassInteraction::Matrix6x6 &matrix, agx::Real eps=agx::RealEpsilon)
double Real
Definition Real.h:42
AGXPHYSICS_EXPORT agx::Bool equivalent(const agx::AddedMassInteraction::Matrix6x6 &lhs, const agx::AddedMassInteraction::Matrix6x6 &rhs, agx::Real eps=agx::RealEpsilon)