Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Interval.h « MT « include « solid « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c6ba2fc1681c8d69997282d92844d1ca3f4f2780 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
 * SOLID - Software Library for Interference Detection
 * 
 * Copyright (C) 2001-2003  Dtecta.  All rights reserved.
 *
 * This library may be distributed under the terms of the Q Public License
 * (QPL) as defined by Trolltech AS of Norway and appearing in the file
 * LICENSE.QPL included in the packaging of this file.
 *
 * This library may be distributed and/or modified under the terms of the
 * GNU General Public License (GPL) version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * This library is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Commercial use or any other use of this library not covered by either 
 * the QPL or the GPL requires an additional license from Dtecta. 
 * Please contact info@dtecta.com for enquiries about the terms of commercial
 * use of this library.
 */

#ifndef INTERVAL_H
#define INTERVAL_H

#if defined (__sgi)
#include <assert.h>
#else
#include <cassert>
#endif

#include <iostream>
#include <algorithm>

namespace MT {

	template <typename Scalar>
	class Interval {
	public:
		Interval() {}
		

#if _MSC_VER <= 1200
        explicit Interval(const Scalar& x) 
		    : m_lb(x), m_ub(x)
	    {}
        
 
		Interval(const Scalar& lb, const Scalar& ub) 
			: m_lb(lb), m_ub(ub)
		{
			assert(lb <= ub);
		}
#else
		template <typename Scalar2>
		explicit Interval(const Scalar2& x) 
			: m_lb(x), m_ub(x)
		{}
		
		template <typename Scalar2>
		Interval(const Scalar2& lb, const Scalar2& ub) 
			: m_lb(lb), m_ub(ub)
		{
			assert(lb <= ub);
		}
		
		template <typename Scalar2>
		Interval(const Interval<Scalar2>& z) 
		{ 
			*this = z; 
		}
		
		template <typename Scalar2>
		Interval<Scalar>& operator=(const Interval<Scalar2>& z) 
		{ 
			m_lb = Scalar(z.lower()); 
			m_ub = Scalar(z.upper()); 
			return *this;
		}
#endif
      
		

		Scalar&       lower()       { return m_lb; }
		const Scalar& lower() const { return m_lb; }
		
		Scalar&       upper()       { return m_ub; }
		const Scalar& upper() const { return m_ub; }
		 
		Scalar center() const { return (m_lb + m_ub) * Scalar(0.5); } 
		Scalar extent() const { return (m_ub - m_lb) * Scalar(0.5); } 

	
	protected:
		Scalar m_lb, m_ub;
	};

	template <typename Scalar>
	inline Interval<Scalar> 
	operator+(const Interval<Scalar>& z1, const Interval<Scalar>& z2)
	{
		return Interval<Scalar>(z1.lower() + z2.lower(), 
								z1.upper() + z2.upper());
	}

	template <typename Scalar>
	inline Interval<Scalar> 
	operator-(const Interval<Scalar>& z1, const Interval<Scalar>& z2)
	{
		return Interval<Scalar>(z1.lower() - z2.upper(), 
								z1.upper() - z2.lower());
	}
	
	template <typename Scalar>
	inline std::ostream& 
	operator<<(std::ostream& os, const Interval<Scalar>& z)
	{
		return os << '[' << z.lower() << ", " << z.upper() << ']';
	}

	template <typename Scalar>
	inline Scalar 
	median(const Interval<Scalar>& z) 
	{
		return (z.lower() + z.upper()) * Scalar(0.5);
	}
	
	template <typename Scalar>
	inline Scalar 
	width(const Interval<Scalar>& z) 
	{
		return z.upper() - z.lower();
	}
	
	template <typename Scalar>
	inline bool 
	overlap(const Interval<Scalar>& z1, const Interval<Scalar>& z2) 
	{
		return z1.lower() <= z2.upper() && z2.lower() <= z1.upper();
	}

	template <typename Scalar>
	inline bool 
	in(const Interval<Scalar>& z1, const Interval<Scalar>& z2) 
	{
		return z2.lower() <= z1.lower() && z1.upper() <= z2.upper();
	}

	template <typename Scalar>
	inline bool 
	in(Scalar x, const Interval<Scalar>& z) 
	{
		return z.lower() <= x && x <= z.upper();
	}
	
	template <typename Scalar>
	inline Interval<Scalar> 
	widen(const Interval<Scalar>& z, const Scalar& x) 
	{
		return Interval<Scalar>(z.lower() - x, z.upper() + x);
	}	
		
	template<typename Scalar>
	inline Interval<Scalar>
	hull(const Interval<Scalar>& z1, const Interval<Scalar>& z2)
	{
		return Interval<Scalar>(GEN_min(z1.lower(), z2.lower()), 
								GEN_max(z1.upper(), z2.upper()));
	}	
   
   template<typename Scalar>
	inline Interval<Scalar>
	operator+(Scalar x, const Interval<Scalar>& z)
	{
		return Interval<Scalar>(x + z.lower(), x + z.upper());
	}
}

#endif