2 Copyright (C) 1999-2006 Id Software, Inc. and contributors.
3 For a list of contributors, see the accompanying CONTRIBUTORS file.
5 This file is part of GtkRadiant.
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
28 static double at,bt,ct;
29 #define PYTHAG(a,b) ((at=fabs(a)) > (bt=fabs(b)) ? \
30 (ct=bt/at,at*sqrt(1.0+ct*ct)) : (bt ? (ct=at/bt,bt*sqrt(1.0+ct*ct)): 0.0))
32 static double maxarg1,maxarg2;
33 #define MAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ?\
34 (maxarg1) : (maxarg2))
35 #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
37 void ntrerror(char *s)
43 double *allocVect(int sz)
47 ret = calloc(sizeof(double), (size_t)sz);
51 void freeVect(double *ret)
56 double **allocMatrix(int r,int c)
60 ret = calloc(sizeof(double), (size_t)(r*c));
64 void freeMatrix(double **ret,int r)
69 void svdcmp(double** a, int m, int n, double* w, double** v)
71 int flag,i,its,j,jj,k,l,nm;
73 double anorm=0.0,g=0.0,scale=0.0;
77 if (m < n) ntrerror("SVDCMP: You must augment A with extra zero rows");
84 for (k=i;k<=m;k++) scale += fabs(a[k][i]);
96 for (s=0.0,k=i;k<=m;k++) s += a[k][i]*a[k][j];
98 for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
101 for (k=i;k<=m;k++) a[k][i] *= scale;
106 if (i <= m && i != n) {
107 for (k=l;k<=n;k++) scale += fabs(a[i][k]);
111 s += a[i][k]*a[i][k];
114 g = -SIGN(sqrt(s),f);
117 for (k=l;k<=n;k++) rv1[k]=a[i][k]/h;
120 for (s=0.0,k=l;k<=n;k++) s += a[j][k]*a[i][k];
121 for (k=l;k<=n;k++) a[j][k] += s*rv1[k];
124 for (k=l;k<=n;k++) a[i][k] *= scale;
127 anorm=MAX(anorm,(fabs(w[i])+fabs(rv1[i])));
133 v[j][i]=(a[i][j]/a[i][l])/g;
135 for (s=0.0,k=l;k<=n;k++) s += a[i][k]*v[k][j];
136 for (k=l;k<=n;k++) v[k][j] += s*v[k][i];
139 for (j=l;j<=n;j++) v[i][j]=v[j][i]=0.0;
149 for (j=l;j<=n;j++) a[i][j]=0.0;
154 for (s=0.0,k=l;k<=m;k++) s += a[k][i]*a[k][j];
156 for (k=i;k<=m;k++) a[k][j] += f*a[k][i];
159 for (j=i;j<=m;j++) a[j][i] *= g;
161 for (j=i;j<=m;j++) a[j][i]=0.0;
166 for (its=1;its<=30;its++) {
170 if (fabs(rv1[l])+anorm == anorm) {
174 if (fabs(w[nm])+anorm == anorm) break;
181 if (fabs(f)+anorm != anorm) {
201 for (j=1;j<=n;j++) v[j][k]=(-v[j][k]);
205 if (its == 30) ntrerror("No convergence in 30 SVDCMP iterations");
211 f=((y-z)*(y+z)+(g-h)*(g+h))/(2.0*h*y);
213 f=((x-z)*(x+z)+h*((y/(f+SIGN(g,f)))-h))/x;
215 for (j=l;j<=nm;j++) {
229 for (jj=1;jj<=n;jj++) {
244 for (jj=1;jj<=m;jj++) {
261 void svbksb(double** u, double* w, double** v,int m, int n, double* b, double* x)
280 for (jj=1;jj<=n;jj++)
281 s += v[j][jj]*tmp[jj];
293 void DOsvd(float *a,float *res,float *comp,float *values,int nframes,int framesize,int compressedsize)
305 if (nframes>framesize)
310 da=allocMatrix(usedfs,nframes);
311 v=allocMatrix(nframes,nframes);
312 w=allocVect(nframes);
315 for (i=0;i<nframes;i++)
317 for (j=0;j<framesize;j++)
318 da[j+1][i+1]=a[i*framesize+j];
323 svdcmp(da,usedfs,nframes,w,v);
325 remap = calloc(sizeof(int), (size_t)nframes);
328 for (i=0;i<nframes;i++)
330 for (j=0;j<compressedsize;j++)
333 for (i=0;i<nframes;i++)
335 if (remap[i]<0&&fabs(w[i+1])>mx)
337 mx=(float) fabs(w[i+1]);
354 printf("Warning: To many degrees of freedom! File size may increase\n");
356 for (i=0;i<compressedsize;i++)
359 for (j=0;j<framesize;j++)
360 res[i*framesize+j]=0;
364 for (i=0;i<nframes;i++)
370 values[remap[i]]=(float) w[i+1];
371 for (j=0;j<framesize;j++)
372 res[remap[i]*framesize+j]=(float) da[j+1][i+1];
376 freeMatrix(v,nframes);
377 freeMatrix(da,framesize);
383 void DOsvd(float *a,float *res,float *comp,float *values,int nframes,int framesize,int compressedsize)
391 double **da=allocMatrix(nrows,framesize);
392 double **v=allocMatrix(framesize,framesize);
393 double *w=allocVect(framesize);
397 for (j=0;j<framesize;j++)
399 for (i=0;i<nframes;i++)
400 da[j+1][i+1]=a[i*framesize+j];
405 svdcmp(da,nrows,framesize,w,v);
407 remap=new int[framesize];
410 for (i=0;i<framesize;i++)
412 for (j=0;j<compressedsize;j++)
415 for (i=0;i<framesize;i++)
417 if (remap[i]<0&&fabs(w[i+1])>mx)
426 // josh **DO NOT** put your dof>nframes mod here
427 for (i=0;i<framesize;i++)
433 values[remap[i]]=w[i+1];
434 for (j=0;j<framesize;j++)
435 res[remap[i]*framesize+j]=v[j+1][i+1];
439 freeMatrix(v,framesize);
440 freeMatrix(da,nrows);
446 void DOsvdPlane(float *pnts,int npnts,float *n,float *base)
449 double **da=allocMatrix(npnts,3);
450 double **v=allocMatrix(3,3);
451 double *w=allocVect(3);
460 for (i=1;i<npnts;i++)
463 base[j]+=pnts[i*3+j];
465 base[0]/=(float)(npnts);
466 base[1]/=(float)(npnts);
467 base[2]/=(float)(npnts);
471 for (j=0;j<npnts;j++)
472 da[j+1][i+1]=pnts[j*3+i]-base[i];
475 svdcmp(da,npnts,3,w,v);
480 mn=(float) fabs(w[i+1]);
484 n[0]=(float) v[1][bestat+1];
485 n[1]=(float) v[2][bestat+1];
486 n[2]=(float) v[3][bestat+1];
489 freeMatrix(da,npnts);