AGX Dynamics 2.38.0.2
Loading...
Searching...
No Matches
Interval.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 AGX_INTERVAL_H
18#define AGX_INTERVAL_H
19
21
22
23#include <agx/agxPhysics_export.h>
24
25#ifndef _WIN32
26#include <float.h>
27#endif
28
29#include <agx/agx.h>
30
31namespace agx
32{
37 class AGXPHYSICS_EXPORT Interval {
38 public:
40 Interval( Real newMinValue, Real newMaxValue );
41
43 Interval( Real valueA, Real valueB, bool sorted );
44
46 Interval();
47
49 void intersectWith( const Interval& other );
50
52 void intersectWithRightUnbounded( Real leftBound );
53
55 void intersectWithLeftUnbounded( Real rightBound );
56
62 Interval computeOverlapWith(const Interval& other) const;
63
65 bool hasOverlapWith(const Interval& other) const;
66
68 bool containsValue(const Real value) const;
69
71 Real& minValue();
72
74 Real& maxValue();
75
77 Real minValue() const;
78
80 Real maxValue() const;
81
83 bool isEmpty() const;
84
86 bool hasElements() const;
87
88 /*
89 * Computes length of interval.
90 * Will give 0 for an interval with 1 element and a negative number for empty intervals.
91 */
92 Real getLength() const;
93
94 protected:
95 Real m_values[2];
96 };
97
98
99
101
102 AGX_FORCE_INLINE Interval::Interval()
103 {
104 minValue() = -std::numeric_limits<Real>::infinity();
105 maxValue() = std::numeric_limits<Real>::infinity();
106 }
107
108
109 AGX_FORCE_INLINE void Interval::intersectWith( const Interval& other )
110 {
111 minValue() = std::max( minValue(), other.minValue() );
112 maxValue() = std::min( maxValue(), other.maxValue() );
113 }
114
115
116 AGX_FORCE_INLINE void Interval::intersectWithRightUnbounded( Real leftBound )
117 {
118 minValue() = std::max( minValue(), leftBound );
119 }
120
121
122 AGX_FORCE_INLINE void Interval::intersectWithLeftUnbounded( Real rightBound )
123 {
124 maxValue() = std::min( maxValue(), rightBound );
125 }
126
127
128 AGX_FORCE_INLINE Real& Interval::minValue()
129 {
130 return m_values[0];
131 }
132
133
134 AGX_FORCE_INLINE Real& Interval::maxValue()
135 {
136 return m_values[1];
137 }
138
139
140 AGX_FORCE_INLINE Real Interval::minValue() const
141 {
142 return m_values[0];
143 }
144
145
146 AGX_FORCE_INLINE Real Interval::maxValue() const
147 {
148 return m_values[1];
149 }
150
151
152 AGX_FORCE_INLINE bool Interval::isEmpty() const
153 {
154 return minValue() > maxValue();
155 }
156
157
158 AGX_FORCE_INLINE bool Interval::hasElements() const
159 {
160 return minValue() <= maxValue();
161 }
162
163
164 AGX_FORCE_INLINE Interval::Interval( Real newMinValue, Real newMaxValue )
165 {
166 minValue() = newMinValue;
167 maxValue() = newMaxValue;
168 }
169
170
171 AGX_FORCE_INLINE Interval::Interval( Real valueA, Real valueB, bool sorted )
172 {
173 if (sorted) {
174 minValue() = valueA;
175 maxValue() = valueB;
176 }
177 else {
178 minValue() = std::min(valueA, valueB);
179 maxValue() = std::max(valueA, valueB);
180 }
181 }
182
183 AGX_FORCE_INLINE Interval Interval::computeOverlapWith(const Interval& other) const
184 {
185 return Interval(std::max( minValue(), other.minValue() ),
186 std::min( maxValue(), other.maxValue() ));
187 }
188
189
190 AGX_FORCE_INLINE bool Interval::hasOverlapWith(const Interval& other) const
191 {
192 return !isEmpty() && !other.isEmpty() &&
193 (containsValue(other.minValue()) || containsValue(other.maxValue()) ||
194 other.containsValue(minValue()) || other.containsValue(maxValue()));
195 }
196
197
198 AGX_FORCE_INLINE bool Interval::containsValue(const Real value) const
199 {
200 return (value >= minValue() && value <= maxValue());
201 }
202
203
204 AGX_FORCE_INLINE Real Interval::getLength() const
205 {
206 return maxValue() - minValue();
207 }
208
209
210}
211
213#endif
#define AGXPHYSICS_EXPORT
#define DOXYGEN_END_INTERNAL_BLOCK()
Definition macros.h:97
#define DOXYGEN_START_INTERNAL_BLOCK()
Definition macros.h:96
#define AGX_FORCE_INLINE
Definition macros.h:58
The agx namespace contains the dynamics/math part of the AGX Dynamics API.
double Real
Definition Real.h:42