File size: 5,474 Bytes
871e4fa | 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 | // YEORIN-X v3 rational-camera selector. C++17, bounded storage, no optimizer.
// This is a numerical control kernel, not a game hook or presentation driver.
#include <array>
#include <algorithm>
#include <cmath>
#include <limits>
#include <cstddef>
#ifdef _WIN32
#define YR_EXPORT __declspec(dllexport)
#else
#define YR_EXPORT __attribute__((visibility("default")))
#endif
namespace {
constexpr int P=15, MAX_ACTIONS=512;
struct D {double v,x,y;D(double q=0.,double dx=0.,double dy=0.):v(q),x(dx),y(dy){}};
D operator+(D a,D b){return {a.v+b.v,a.x+b.x,a.y+b.y};}
D operator-(D a,D b){return {a.v-b.v,a.x-b.x,a.y-b.y};}
D operator-(D a){return {-a.v,-a.x,-a.y};}
D operator*(D a,D b){return {a.v*b.v,a.x*b.v+a.v*b.x,a.y*b.v+a.v*b.y};}
D operator/(D a,D b){const double q=1./b.v;return {a.v*q,(a.x-a.v*q*b.x)*q,(a.y-a.v*q*b.y)*q};}
D sn(D a){double s=std::sin(a.v),c=std::cos(a.v);return {s,c*a.x,c*a.y};}
D cs(D a){double s=std::sin(a.v),c=std::cos(a.v);return {c,-s*a.x,-s*a.y};}
D powi(D a,int k){D o(1);for(int i=0;i<k;++i)o=o*a;return o;}
int idx(int p,int q){int n=p+q;return n*(n+1)/2+p;}
void coefficients(double yaw,double pitch,std::array<D,30>& out,double& d,double& cx,double& cy){
D a(yaw,1,0),b(pitch,0,1),ca=cs(a),sa=sn(a),cb=cs(b),sb=sn(b);
D r[3][3]={{ca,sa*sb,sa*cb},{D(0),cb,-sb},{-sa,ca*sb,ca*cb}};
out.fill(D(0));D den=r[2][2],zx=r[2][0]/den,zy=r[2][1]/den;
const int binom[4][4]={{1,0,0,0},{1,1,0,0},{1,2,1,0},{1,3,3,1}};
for(int k=0;k<=3;++k)for(int p=0;p<=k;++p){
int q=k-p;D v=D((k%2?-1.:1.)*binom[k][p])*powi(zx,p)*powi(zy,q);
for(int comp=0;comp<2;++comp){
int offset=comp*P;
out[offset+idx(p,q)]=out[offset+idx(p,q)]+v*r[comp][2]/den;
out[offset+idx(p+1,q)]=out[offset+idx(p+1,q)]+v*r[comp][0]/den;
out[offset+idx(p,q+1)]=out[offset+idx(p,q+1)]+v*r[comp][1]/den;
}
}
out[idx(1,0)]=out[idx(1,0)]-D(1);out[P+idx(0,1)]=out[P+idx(0,1)]-D(1);
d=den.v;cx=r[2][0].v;cy=r[2][1].v;
}
bool finite_array(const double* a,std::size_t n){if(!a)return false;for(std::size_t i=0;i<n;++i)if(!std::isfinite(a[i]))return false;return true;}
}
extern "C" YR_EXPORT int yr3_select(
int actions,int regions,const double* moments,const double* energy,const double* maximum,
const double* centers,const double* radii,double slack,double width,double height,double fov,
double pitch_cap,double pixel_cap,double path_cost,int include_zero,double* output){
// output = [alpha, lower bound, inherited-candidate bound, max-region charge]
if(!output)return -3;
std::fill(output,output+4,0.);
const double params[]={slack,width,height,fov,pitch_cap,pixel_cap,path_cost};
if(actions<1||actions>MAX_ACTIONS||regions<1||regions>4096||!finite_array(params,7)||slack<0||width<2||height<2||fov<=1||fov>=175||pitch_cap<0||pitch_cap>3.141592653589793||pixel_cap<0||path_cost<0)return -3;
if(!finite_array(moments,std::size_t(actions)*30)||!finite_array(energy,actions)||!finite_array(maximum,actions)||!finite_array(centers,std::size_t(regions)*2)||!finite_array(radii,std::size_t(regions)*2))return -3;
for(int j=0;j<actions;++j)if(energy[j]<0||maximum[j]<0)return -3;
for(int n=0;n<2*regions;++n)if(radii[n]<0)return -3;
const double f=(width-1)/(2*std::tan(fov*3.141592653589793/360.));
const double X=(width-1)/(2*f),Y=(height-1)/(2*f),rad=std::hypot(X,Y),S=std::sqrt(1+rad*rad);
std::array<double,MAX_ACTIONS> lower,root;
lower.fill(std::numeric_limits<double>::infinity());
for(int j=0;j<actions;++j)root[j]=std::sqrt(energy[j]);
double max_charge=0;
for(int n=0;n<regions;++n){
double a=centers[2*n],b=centers[2*n+1],bx=radii[2*n]+slack,by=radii[2*n+1]+slack;
std::array<D,30> coeff;double d,cx,cy;coefficients(a,b,coeff,d,cx,cy);
double beta=(std::abs(cx)*X+std::abs(cy)*Y)/d;
double theta=std::abs(a)+std::abs(b)+bx+by,smin=1-S*theta;
if(!(d>0&&beta<1&&smin>0))return -1;
double nmax=S/d,npmax=S*(1/d+1/(d*d)),zpmax=rad/d+beta/d;
double bk=beta*beta*beta,bk1=bk*beta,om=1-beta;
double tail=f*nmax*bk1/om;
double dtail=f*(npmax*bk1/om+nmax*zpmax*(4*bk/om+bk1/(om*om)));
double ymax=S/smin,T=std::sqrt(1+ymax*ymax);
double hess=f*T*S*(1/smin+2*S/(smin*smin));
double tilt=f*T*S/smin*(std::abs(a)+bx)*2*std::sin(pitch_cap/2);
double charge=tail+dtail*(bx+by)+.5*hess*(bx+by)*(bx+by)+tilt;
max_charge=std::max(max_charge,charge);
for(int j=0;j<actions;++j){
double v=0,gx=0,gy=0;const double* m=moments+std::size_t(j)*30;
for(int k=0;k<30;++k){v+=m[k]*coeff[k].v;gx+=m[k]*coeff[k].x;gy+=m[k]*coeff[k].y;}
double bound=v-std::abs(gx)*bx-std::abs(gy)*by-charge*root[j];
lower[j]=std::min(lower[j],bound);
}
}
int best=-1;double best_bound=0,best_alpha=0,inherited=0;
for(int j=0;j<actions;++j){
double a=include_zero?std::min(lower[j],0.):lower[j];
double cap=maximum[j]>0?std::min(1.,pixel_cap/maximum[j]):1.;
double alpha=energy[j]>1e-25?std::clamp(a/energy[j],0.,cap):0.;
double bound=2*a*alpha-energy[j]*alpha*alpha-path_cost;
if(j==0)inherited=std::max(0.,bound);
if(alpha>0&&bound>std::max(1e-10,best_bound)){best=j;best_bound=bound;best_alpha=alpha;}
}
output[0]=best_alpha;output[1]=best_bound;output[2]=inherited;output[3]=max_charge;
return best;
}
|