@@ -18,7 +18,7 @@ namespace opentime {
1818 */
1919
2020/* *
21- * This default epsilon value is used in comparison between floating numbers.
21+ * This default epsilon_s value is used in comparison between floating numbers.
2222 * It is computed to be twice 192khz, the fastest commonly used audio rate.
2323 * It can be changed in the future if necessary due to higher sampling rates
2424 * or some other kind of numeric tolerance detected in the library.
@@ -92,8 +92,9 @@ class TimeRange {
9292 */
9393
9494 /* *
95- * In the relations that follow, epsilon indicates the tolerance,in the sense that if abs(a-b) < epsilon,
96- * we consider a and b to be equal
95+ * In the relations that follow, epsilon_s indicates the tolerance,in the sense that if abs(a-b) < epsilon_s,
96+ * we consider a and b to be equal.
97+ * The time comparison is done in double seconds.
9798 */
9899
99100 /* *
@@ -117,8 +118,12 @@ class TimeRange {
117118 * The converse would be <em>other.contains(this)</em>
118119 * @param other
119120 */
120- bool contains (TimeRange other) const {
121- return _start_time <= other._start_time && end_time_exclusive () >= other.end_time_exclusive ();
121+ bool contains (TimeRange other, double epsilon_s = DEFAULT_EPSILON_s) const {
122+ double thisStart = _start_time.to_seconds ();
123+ double thisEnd = end_time_exclusive ().to_seconds ();
124+ double otherStart = other._start_time .to_seconds ();
125+ double otherEnd = other.end_time_exclusive ().to_seconds ();
126+ return greater_than (otherStart, thisStart, epsilon_s) && lesser_than (otherEnd, thisEnd, epsilon_s);
122127 }
123128
124129 /* *
@@ -134,48 +139,49 @@ class TimeRange {
134139 }
135140
136141 /* *
137- * The start of <b>this</b> strictly precedes end of <b>other</b> by a value >= <b>epsilon </b>.
138- * The end of <b>this</b> strictly antecedes start of <b>other</b> by a value >= <b>epsilon </b>.
142+ * The start of <b>this</b> strictly precedes end of <b>other</b> by a value >= <b>epsilon_s </b>.
143+ * The end of <b>this</b> strictly antecedes start of <b>other</b> by a value >= <b>epsilon_s </b>.
139144 * [ this ]
140145 * [ other ]
141146 * The converse would be <em>other.overlaps(this)</em>
142147 * @param other
143- * @param epsilon
148+ * @param epsilon_s
144149 */
145- bool overlaps (TimeRange other, double epsilon = DEFAULT_EPSILON_s) const {
150+ bool overlaps (TimeRange other, double epsilon_s = DEFAULT_EPSILON_s) const {
146151 double thisStart = _start_time.to_seconds ();
147152 double thisEnd = end_time_exclusive ().to_seconds ();
148153 double otherStart = other._start_time .to_seconds ();
149154 double otherEnd = other.end_time_exclusive ().to_seconds ();
150- return (otherEnd - thisStart >= epsilon) &&
151- (thisEnd - otherStart >= epsilon);
155+ return lesser_than (thisStart, otherStart, epsilon_s) &&
156+ greater_than (thisEnd, otherStart, epsilon_s) &&
157+ greater_than (otherEnd, thisEnd, epsilon_s);
152158 }
153159
154160 /* *
155- * The end of <b>this</b> strictly precedes the start of <b>other</b> by a value >= <b>epsilon </b>.
161+ * The end of <b>this</b> strictly precedes the start of <b>other</b> by a value >= <b>epsilon_s </b>.
156162 * [ this ] [ other ]
157163 * The converse would be <em>other.before(this)</em>
158164 * @param other
159- * @param epsilon
165+ * @param epsilon_s
160166 */
161- bool before (TimeRange other, double epsilon = DEFAULT_EPSILON_s) const {
167+ bool before (TimeRange other, double epsilon_s = DEFAULT_EPSILON_s) const {
162168 double thisEnd = end_time_exclusive ().to_seconds ();
163169 double otherStart = other._start_time .to_seconds ();
164- return otherStart - thisEnd >= epsilon ;
170+ return greater_than ( otherStart, thisEnd, epsilon_s) ;
165171 }
166172
167173 /* *
168- * The end of <b>this</b> strictly precedes <b>other</b> by a value >= <b>epsilon </b>.
174+ * The end of <b>this</b> strictly precedes <b>other</b> by a value >= <b>epsilon_s </b>.
169175 * other
170176 * ↓
171177 * [ this ] *
172178 * @param other
173- * @param epsilon
179+ * @param epsilon_s
174180 */
175- bool before (RationalTime other, double epsilon = DEFAULT_EPSILON_s) const {
181+ bool before (RationalTime other, double epsilon_s = DEFAULT_EPSILON_s) const {
176182 double thisEnd = end_time_exclusive ().to_seconds ();
177183 double otherTime = other.to_seconds ();
178- return otherTime - thisEnd >= epsilon ;
184+ return lesser_than ( thisEnd, otherTime, epsilon_s) ;
179185 }
180186
181187 /* *
@@ -184,29 +190,29 @@ class TimeRange {
184190 * [this][other]
185191 * The converse would be <em>other.meets(this)</em>
186192 * @param other
187- * @param epsilon
193+ * @param epsilon_s
188194 */
189- bool meets (TimeRange other, double epsilon = DEFAULT_EPSILON_s) const {
195+ bool meets (TimeRange other, double epsilon_s = DEFAULT_EPSILON_s) const {
190196 double thisEnd = end_time_exclusive ().to_seconds ();
191197 double otherStart = other._start_time .to_seconds ();
192- return otherStart - thisEnd <= epsilon && otherStart - thisEnd >= 0 ;
198+ return otherStart - thisEnd <= epsilon_s && otherStart - thisEnd >= 0 ;
193199 }
194200
195201 /* *
196202 * The start of <b>this</b> strictly equals the start of <b>other</b>.
197- * The end of <b>this</b> strictly precedes the end of <b>other</b> by a value >= <b>epsilon </b>.
203+ * The end of <b>this</b> strictly precedes the end of <b>other</b> by a value >= <b>epsilon_s </b>.
198204 * [ this ]
199205 * [ other ]
200206 * The converse would be <em>other.begins(this)</em>
201207 * @param other
202- * @param epsilon
208+ * @param epsilon_s
203209 */
204- bool begins (TimeRange other, double epsilon = DEFAULT_EPSILON_s) const {
210+ bool begins (TimeRange other, double epsilon_s = DEFAULT_EPSILON_s) const {
205211 double thisStart = _start_time.to_seconds ();
206212 double thisEnd = end_time_exclusive ().to_seconds ();
207213 double otherStart = other._start_time .to_seconds ();
208214 double otherEnd = other.end_time_exclusive ().to_seconds ();
209- return fabs (otherStart - thisStart) <= epsilon && otherEnd - thisEnd >= epsilon ;
215+ return fabs (otherStart - thisStart) <= epsilon_s && lesser_than ( thisEnd, otherEnd, epsilon_s) ;
210216 }
211217
212218 /* *
@@ -217,27 +223,27 @@ class TimeRange {
217223 * [ this ]
218224 * @param other
219225 */
220- bool begins (RationalTime other, double epsilon = DEFAULT_EPSILON_s) const {
226+ bool begins (RationalTime other, double epsilon_s = DEFAULT_EPSILON_s) const {
221227 double thisStart = _start_time.to_seconds ();
222228 double otherStart = other.to_seconds ();
223- return fabs (otherStart - thisStart) <= epsilon ;
229+ return fabs (otherStart - thisStart) <= epsilon_s ;
224230 }
225231
226232 /* *
227- * The start of <b>this</b> strictly antecedes the start of <b>other</b> by a value >= <b>epsilon </b>.
233+ * The start of <b>this</b> strictly antecedes the start of <b>other</b> by a value >= <b>epsilon_s </b>.
228234 * The end of <b>this</b> strictly equals the end of <b>other</b>.
229235 * [ this ]
230236 * [ other ]
231237 * The converse would be <em>other.finishes(this)</em>
232238 * @param other
233- * @param epsilon
239+ * @param epsilon_s
234240 */
235- bool finishes (TimeRange other, double epsilon = DEFAULT_EPSILON_s) const {
241+ bool finishes (TimeRange other, double epsilon_s = DEFAULT_EPSILON_s) const {
236242 double thisStart = _start_time.to_seconds ();
237243 double thisEnd = end_time_exclusive ().to_seconds ();
238244 double otherStart = other._start_time .to_seconds ();
239245 double otherEnd = other.end_time_exclusive ().to_seconds ();
240- return fabs (thisEnd - otherEnd) <= epsilon && thisStart - otherStart >= epsilon ;
246+ return fabs (thisEnd - otherEnd) <= epsilon_s && greater_than ( thisStart, otherStart, epsilon_s) ;
241247 }
242248
243249 /* *
@@ -247,12 +253,29 @@ class TimeRange {
247253 * *
248254 * [ this ]
249255 * @param other
250- * @param epsilon
256+ * @param epsilon_s
251257 */
252- bool finishes (RationalTime other, double epsilon = DEFAULT_EPSILON_s) const {
258+ bool finishes (RationalTime other, double epsilon_s = DEFAULT_EPSILON_s) const {
253259 double thisEnd = end_time_exclusive ().to_seconds ();
254260 double otherEnd = other.to_seconds ();
255- return fabs (thisEnd - otherEnd) <= epsilon;
261+ return fabs (thisEnd - otherEnd) <= epsilon_s;
262+ }
263+
264+ /* *
265+ * The start of <b>this</b> precedes or equals the end of <b>other</b> by a value >= <b>epsilon_s</b>.
266+ * The end of <b>this</b> antecedes or equals the start of <b>other</b> by a value >= <b>epsilon_s</b>.
267+ * [ this ] OR [ other ]
268+ * [ other ] [ this ]
269+ * The converse would be <em>other.finishes(this)</em>
270+ * @param other
271+ * @param epsilon_s
272+ */
273+ bool intersects (TimeRange other, double epsilon_s = DEFAULT_EPSILON_s) const {
274+ double thisStart = _start_time.to_seconds ();
275+ double thisEnd = end_time_exclusive ().to_seconds ();
276+ double otherStart = other._start_time .to_seconds ();
277+ double otherEnd = other.end_time_exclusive ().to_seconds ();
278+ return lesser_than (thisStart, otherEnd, epsilon_s) && greater_than (thisEnd, otherStart, epsilon_s);
256279 }
257280
258281
@@ -288,6 +311,14 @@ class TimeRange {
288311private:
289312 RationalTime _start_time, _duration;
290313 friend class TimeTransform ;
314+
315+ inline bool greater_than (double lhs, double rhs, double epsilon) const {
316+ return lhs - rhs >= epsilon;
317+ }
318+
319+ inline bool lesser_than (double lhs, double rhs, double epsilon) const {
320+ return rhs - lhs >= epsilon;
321+ }
291322};
292323
293324} }
0 commit comments