MinMax-inl.h
Go to the documentation of this file.
1 // This file is a part of the OpenSurgSim project.
2 // Copyright 2013, SimQuest Solutions Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #ifndef SURGSIM_MATH_MINMAX_INL_H
17 #define SURGSIM_MATH_MINMAX_INL_H
18 
20 
21 namespace SurgSim
22 {
23 namespace Math
24 {
25 
26 template <class T>
27 void minMax(const T& a1, const T& a2, T* minVal, T* maxVal)
28 {
29  if (a1 < a2)
30  {
31  *minVal = a1;
32  *maxVal = a2;
33  }
34  else
35  {
36  *minVal = a2;
37  *maxVal = a1;
38  }
39 }
40 
41 template <class T>
42 void minMax(const T& a1, const T& a2, const T& a3, T* minVal, T* maxVal)
43 {
44  T min = a1;
45  T max = a1;
46  if (a2 < min)
47  {
48  min = a2;
49  }
50  else if (a2 > max)
51  {
52  max = a2;
53  }
54  if (a3 < min)
55  {
56  min = a3;
57  }
58  else if (a3 > max)
59  {
60  max = a3;
61  }
62 
63  *minVal = min;
64  *maxVal = max;
65 }
66 
67 template <class T>
68 void minMax(const T& a1, const T& a2, const T& a3, const T& a4, T* minVal, T* maxVal)
69 {
70  T min = a1;
71  T max = a1;
72  if (a2 < min)
73  {
74  min = a2;
75  }
76  else if (a2 > max)
77  {
78  max = a2;
79  }
80  if (a3 < min)
81  {
82  min = a3;
83  }
84  else if (a3 > max)
85  {
86  max = a3;
87  }
88  if (a4 < min)
89  {
90  min = a4;
91  }
92  else if (a4 > max)
93  {
94  max = a4;
95  }
96 
97  *minVal = min;
98  *maxVal = max;
99 }
100 
101 template <class T>
102 void minMax(const T& a1, const T& a2, const T& a3, const T& a4, const T& a5, T* minVal, T* maxVal)
103 {
104  T min = a1;
105  T max = a1;
106  if (a2 < min)
107  {
108  min = a2;
109  }
110  else if (a2 > max)
111  {
112  max = a2;
113  }
114  if (a3 < min)
115  {
116  min = a3;
117  }
118  else if (a3 > max)
119  {
120  max = a3;
121  }
122  if (a4 < min)
123  {
124  min = a4;
125  }
126  else if (a4 > max)
127  {
128  max = a4;
129  }
130  if (a5 < min)
131  {
132  min = a5;
133  }
134  else if (a5 > max)
135  {
136  max = a5;
137  }
138 
139  *minVal = min;
140  *maxVal = max;
141 }
142 
143 template <class T>
144 void minMax(const T* values, int numValues, T* minVal, T* maxVal)
145 {
146  SURGSIM_ASSERT(numValues > 0) << "MinMax was called with a negative or null" <<
147  " number of values; the result is indeterminate.";
148  T min = values[0];
149  T max = values[0];
150  for (int i = 1; i < numValues; ++i)
151  {
152  if (values[i] < min)
153  {
154  min = values[i];
155  }
156  else if (values[i] > max)
157  {
158  max = values[i];
159  }
160  }
161  *minVal = min;
162  *maxVal = max;
163 }
164 
165 };
166 };
167 
168 #endif // SURGSIM_MATH_MINMAX_INL_H
169 
SURGSIM_ASSERT
#define SURGSIM_ASSERT(condition)
Assert that condition is true.
Definition: Assert.h:77
Assert.h
The header that provides the assertion API.
SurgSim
Definition: CompoundShapeToGraphics.cpp:30
SurgSim::Math::minMax
void minMax(const T &a1, const T &a2, T *minVal, T *maxVal)
Calculate the minimum and maximum of two values.
Definition: MinMax-inl.h:27