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

IK_LineMinimizer.h « intern « iksolver « intern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 635a5972c02af468d233f9548cbcc533037d0cc4 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/**
 * $Id$
 * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version. The Blender
 * Foundation also sells licenses for use in proprietary software under
 * the Blender License.  See http://www.blender.org/BL/ for information
 * about this.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 *
 * The Original Code is: all of this file.
 *
 * Contributor(s): none yet.
 *
 * ***** END GPL/BL DUAL LICENSE BLOCK *****
 */

#ifndef NAN_INCLUDED_IK_LineMinimizer_h

#define NAN_INCLUDED_IK_LineMinimizer_h

/**
 * @author Laurence Bourn
 * @date 28/6/2001
 */

#include "MT_Scalar.h"
#include <vector>
#include "TNT/tntmath.h"
#include "MEM_NonCopyable.h"



#define GOLD 1.618034
#define GLIMIT 100.0
#define TINY 1.0e-20
#define ZEPS 1.0e-10

/** 
 * Routines for line - minization in n dimensions
 * these should be templated on the potenial function
 * p instead of using a virtual class. Please see 
 * numerical recipes in c for more details. www.nr.com
 */

class DifferentiablePotenialFunction1d {
public :
	virtual
		MT_Scalar 
	Evaluate(
		const MT_Scalar x
	) = 0;

	virtual
		MT_Scalar
	Derivative(
		const MT_Scalar x
	) = 0;
};


/**
 * TODO get rid of this class and make some
 * template functions in a seperate namespace
 */

class IK_LineMinimizer : public MEM_NonCopyable {

public :

	/**
	 * Before we proceed with line minimization 
	 * we need to construct an initial bracket
	 * of a minima of the PotenialFunction
	 */

	static
		void
	InitialBracket1d (	
		MT_Scalar &ax,
		MT_Scalar &bx,
		MT_Scalar &cx,
		MT_Scalar &fa,
		MT_Scalar &fb,
		MT_Scalar &fc,
		DifferentiablePotenialFunction1d &potenial
	) {
		MT_Scalar ulim,u,r,q,fu;

		fa = potenial.Evaluate(ax);
		fb = potenial.Evaluate(bx);

		if (fb > fa) {
			std::swap(ax,bx);
			std::swap(fa,fb);
		}
		cx = bx + GOLD*(bx-ax);
		fc = potenial.Evaluate(cx);
		
		while (fb > fc) {

			r = (bx - ax) * (fb - fc);
			q = (bx - cx) * (fb - fa);
			u = bx - ((bx - cx)*q - (bx - ax) *r)/ 
				(2 * TNT::sign(TNT::max(TNT::abs(q-r),TINY),q-r));
			ulim = bx + GLIMIT*(cx-bx);

			if ((bx-u)*(u-cx) > 0.0) {
				fu = potenial.Evaluate(u);
				if (fu < fc) {
					ax = bx;
					bx = u;
					fa = fb;
					fb = fu;
					return;
				} else if (fu > fb) {
					cx = u;
					fc = fu;
					return;
				}
				u = cx + GOLD*(cx-bx);
				fu = potenial.Evaluate(u);

			} else if ((cx - u)*(u - ulim) > 0.0) {
				fu = potenial.Evaluate(u);
 
				if (fu < fc) {
					bx = cx;
					cx = u;
					u = cx + GOLD*(cx - bx);
					fb = fc;
					fc = fu;
					fu = potenial.Evaluate(u);
				}
			} else if ((u-ulim)*(ulim-cx) >=0.0) {
				u = ulim;
				fu = potenial.Evaluate(u);
			} else {
				u = cx + GOLD*(cx-bx);
				fu = potenial.Evaluate(u);
			}
			ax = bx;
			bx = cx;
			cx = u;
			fa = fb;
			fb = fc;
			fc = fu;
		}
	};

	/**
	 * This is a 1 dimensional brent method for
	 * line-minization with derivatives
	 */


	static
		MT_Scalar
	DerivativeBrent1d(
		MT_Scalar ax,
		MT_Scalar bx,
		MT_Scalar cx,
		DifferentiablePotenialFunction1d &potenial,
		MT_Scalar &x_min,
		const MT_Scalar tol,
		int max_iter = 100
	) {
		int iter;
		bool ok1,ok2;
		MT_Scalar a,b,d,d1,d2,du,dv,dw,dx,e(0);
		MT_Scalar fu,fv,fw,fx,olde,tol1,tol2,u,u1,u2,v,w,x,xm;

		a = (ax < cx ? ax : cx);
		b = (ax > cx ? ax : cx);
		x = w = v = bx;
		fw = fv = fx = potenial.Evaluate(x);
		dw = dv = dx = potenial.Derivative(x);

		for (iter = 1; iter <= max_iter; iter++) {
			xm = 0.5*(a+b);
			tol1 = tol*fabs(x) + ZEPS;
			tol2 = 2 * tol1;
			if (fabs(x - xm) <= (tol2 - 0.5*(b-a))) {
				x_min = x;
				return fx;
			}

			if (fabs(e) > tol1) {
				d1 = 2*(b-a);
				d2 = d1;
				if (dw != dx) {
					d1 = (w-x)*dx/(dx-dw);
				}
				if (dv != dx) {
					d2 = (v-x)*dx/(dx-dv);
				}
					
				u1 = x+d1;
				u2 = x+d2;
				ok1 = ((a-u1)*(u1-b) > 0.0) && ((dx*d1) <= 0.0);
				ok2 = ((a-u2)*(u2-b) > 0.0) && ((dx*d2) <= 0.0);
				olde = e;
				e = d;

				if (ok1 || ok2) {
					if (ok1 && ok2) {
						d = fabs(d1) < fabs(d2) ? d1 : d2;
					} else if (ok1)  {
						d = d1;
					} else {
						d = d2;
					}
					if (fabs(d) <= fabs(0.5*olde)) {			
						u = x+d;
						if ((u-a < tol2) || (b-u < tol2)) {
							d = TNT::sign(tol1,xm-x);
						}
					} else {
						d = 0.5*(e = (dx >= 0.0 ? a-x : b-x));
					}
				} else {
					d = 0.5*(e = (dx >= 0.0 ? a-x : b-x));
				}
			} else {
				d = 0.5*(e = (dx >= 0.0 ? a-x : b-x));
			}

			if (fabs(d) >= tol1) {
				u = x+d;
				fu = potenial.Evaluate(u);
			} else {
				u  = x + TNT::sign(tol1,d);
				fu = potenial.Evaluate(u);
				if (fu > fx) {
					x_min = x;
					return fx;
				}
			}
			du = potenial.Derivative(u);
			if (fu <= fx) {
				if (u >= x) {
					a = x;
				} else {
					b = x;
				}
				v = w; fv = fw; dv = dw;
				w = x; fw = fx; dw = dx;
				x = u; fx = fu; dx = du;
			} else {
				if (u < x) {
					a = u;
				} else {
					b = u;
				}
				if (fu <= fw || w == x) {
					v = w; fv = fw; dv = dw;
					w = u; fw = fu; dw = du;
				} else if ( fu < fv || v == x || v == w) {
					v = u; fv = fu; dv = du;
				}
			}
		}
		// FIXME throw exception		
	
		assert(false);
		return MT_Scalar(0);
	};

private :

	/// This class just contains static helper methods so no instantiation

	IK_LineMinimizer();		

};

#undef GOLD
#undef GLIMIT
#undef TINY
#undef ZEPS


				
#endif