Exemplo n.º 1
0
static void image_colour_at(IMAGE *Image, DBL xcoor, DBL ycoor, COLOUR colour, int *index)
{
  switch (Image->Interpolation_Type)
  {
    case NO_INTERPOLATION:

      no_interpolation(Image, xcoor, ycoor, colour, index);

      break;

    default:

      Interp(Image, xcoor, ycoor, colour, index);

      break;
  }
}
Exemplo n.º 2
0
SunRise(int year, int month, int day, double LocalHour, double *UTRise, double *UTSet){

    double	UT, ym, y0, yp, SinH0;
    double	xe, ye, z1, z2, SinH(), hour24();
    int		Rise, Set, nz;

    SinH0 = sin( -50.0/60.0 * RadPerDeg );


    UT = 1.0+TimeZone;
    *UTRise = -999.0;
    *UTSet = -999.0;
    Rise = Set = 0;
    ym = SinH(year, month, day, UT-1.0) - SinH0;

    while ( (UT <= 24.0+TimeZone) ) {

	y0 = SinH(year, month, day, UT) - SinH0;
	yp = SinH(year, month, day, UT+1.0) - SinH0;

	Interp(ym, y0, yp, &xe, &ye, &z1, &z2, &nz);

	switch(nz){

		case 0:
			break;
		case 1:
			if (ym < 0.0){
			    *UTRise = UT + z1;
			    Rise = 1;
			} else {
			    *UTSet = UT + z1;
			    Set = 1;
			}
			break;
		case 2:
			if (ye < 0.0){
			    *UTRise = UT + z2;
			    *UTSet = UT + z1;
			} else {
			    *UTRise = UT + z1;
			    *UTSet = UT + z2;
			}
			Rise = 1;
			Set = 1;
			break;
	}
	ym = yp;
	UT += 2.0;

    }

    if (Rise){
        *UTRise -= TimeZone;
        *UTRise = hour24(*UTRise);
    } else {
        *UTRise = -999.0;
    }

    if (Set){
        *UTSet -= TimeZone;
        *UTSet = hour24(*UTSet);
    } else {
        *UTSet = -999.0;
    }

}
Exemplo n.º 3
0
/// TMLE step 
void tmleStep(double *a_delta,double *a_tau, int *a_n, double *a_xx, double *a_vv,double *a_lambda, double *a_xGrid, double *a_vGrid, int *a_nx, int *a_nv,double *a_T1,double *a_T2, double *a_Q2,double *a_normGrad, double *a_logL, double *a_epsOpt, int *a_degree){

  double slice1[*a_nx],slice2[*a_nx];
  double hv=(a_vGrid[*a_nv-1]-a_vGrid[0])/(*a_nv);

  /// Compute the gradient on the grid
  for(int j=0;j<*a_nx;j++)
  {

      slice1[j]=a_T1[*a_nv*j]*a_Q2[*a_nv*j]+a_T1[*a_nv-1+*a_nv*j]*a_Q2[*a_nv-1+*a_nv*j];
      slice2[j]=a_T2[*a_nv*j]*a_Q2[*a_nv*j]+a_T2[*a_nv-1+*a_nv*j]*a_Q2[*a_nv-1+*a_nv*j];

      for(int k=1;k<*a_nv/2;k++)
	{
	  slice1[j]+=2*a_T1[2*k+*a_nv*j]*a_Q2[2*k+*a_nv*j]+4*a_T1[2*k-1+*a_nv*j]*a_Q2[2*k-1+*a_nv*j];
	  slice2[j]+=2*a_T2[2*k+*a_nv*j]*a_Q2[2*k+*a_nv*j]+4*a_T2[2*k-1+*a_nv*j]*a_Q2[2*k-1+*a_nv*j];
	}

      slice1[j]*=hv/3.0;
      slice2[j]*=hv/3.0;

    }  

  /// Compute the Gradient 
  double grad[*a_nx*(*a_nv)];

  for(int i=0;i<*a_nv;i++)
    {
      for(int j=0;j<*a_nx;j++)
	{
	    if(ABS(slice1[j])<sqrt(DBL_EPSILON)||ABS(slice2[j])<sqrt(DBL_EPSILON))
	    {
	      grad[i+*a_nv*j]=0.0;
	    }else
	    {
	      grad[i+*a_nv*j]=(a_T1[i+*a_nv*j]/slice1[j]-a_T2[i+*a_nv*j]/slice2[j]);
	    }


	  grad[i+*a_nv*j]= a_lambda[j]*grad[i+*a_nv*j];

	}
    }

  /// Evaluate the Gradient @ the observed data
  double gradObs[*a_n];
  Interp(a_xx,a_vv,a_n,gradObs,a_xGrid,a_vGrid,a_nx,a_nv,grad);


  /// Solve the Optimization Problem and find the epsilon
  double gradf[*a_degree+1];
  double delta[*a_degree+1];
  double Hess[(*a_degree+1)*(*a_degree+1)];

  double decrement = 10*bigN;
  double decrementOld=0.0;
  double eps[*a_degree+1];  
      
  
//  double fOld=0.0;


  //while (MIN(decrement/2,ABS(decrement-decrementOld)) >= sqrt(DBL_EPSILON))
  double m = (double)*a_nv * (double)*a_nv;
  double barr = bigN;

  printf("m = %lf : barr = %lf\n",m,barr);

  double fNew = critFun(a_epsOpt, a_xx, gradObs, *a_n, *a_degree, gradf, Hess, grad, a_xGrid, *a_nv, *a_nx,barr);

  int Iter = 0;

  while(1){

      Iter++;
      int iter = 0;
 
      while (1)
      {
	  iter++;
	  printf("[%d](%d) ",Iter,iter);

	  printf("f(theta)=%g | ",fNew);

	  /* solve the system */
	  Cholesky(Hess,*a_degree+1);
	  
	  LGauss(Hess, gradf, *a_degree+1); ///1
	  
	  decrement = 0.0;
	  for(int k=0;k<=*a_degree;k++)
	  {
	      decrement += gradf[k]*gradf[k];
	  }
	  
	  UGauss(Hess, gradf, *a_degree+1); ///2

	  printf("%lf \n",decrement);
	  if(decrement/2 < sqrt(DBL_EPSILON) && iter > 1) break;

	  double fOld = fNew;
	  double t = 1;

	  for(int k=0;k<=*a_degree;k++) delta[k] = gradf[k];

	  do{
 
	      for(int k=0;k<=*a_degree;k++) eps[k] = a_epsOpt[k] - t*delta[k];

	      fNew = critFun(eps, a_xx, gradObs, *a_n, *a_degree, gradf, Hess, grad, a_xGrid, *a_nv, *a_nx, barr);
	      t *= beta;
	      //printf("*");
	      
	  } while (fNew > fOld-alpha*t*decrement);
      
	  for(int k=0;k<=*a_degree;k++) a_epsOpt[k] = eps[k];
	  printf("[t=%g | f=%g]\n",t/beta,fNew);
      
      }
      printf("%lf\n",m/barr);
      if(m/barr < sqrt(DBL_EPSILON)) break;
      barr *= mu;
      //break;
  } 

  printf("\n");
  printf("eps: %lf %lf | ",a_epsOpt[0],a_epsOpt[1]);

  barr = 0.0;
  double fMax = -critFun(a_epsOpt, a_xx, gradObs, *a_n, *a_degree, gradf, Hess, grad, a_xGrid, *a_nv, *a_nx,barr);
  printf("f(eps)=%lf\n\n",fMax);

  ///a_epsOpt=eps;

  /// Compute Q2 @ observed data + compute empirical Mean and Variance
  double empMean[*a_degree+1];
  double empVar[*a_degree+1];
  double sumsq[*a_degree+1];
  double offSet = 0.0;


  int ii = 1;
  double q2Obs;

  for(int k=0;k<=*a_degree;k++){

      empMean[k] = 0.0;
      empVar[k] = 0.0;
      sumsq[k] = 0.0;

      double x0 = pow(a_xx[0],(double)k);

      for(int i=0;i<*a_n;i++)
      {

	  if(k==0){
	      Interp(&a_xx[i],&a_vv[i],&ii,&q2Obs,a_xGrid,a_vGrid,a_nx,a_nv,a_Q2);
	      offSet += log(q2Obs);
	  }

	  if(ABS(a_xGrid[k])>DBL_EPSILON)
	  {
	      double xi = pow(a_xx[i],(double)k);

	      empMean[k] += (xi*gradObs[i] - x0*gradObs[0]);
	      sumsq[k] += (xi*gradObs[i] - x0*gradObs[0])*(xi*gradObs[i] - x0*gradObs[0]);
	  }
      }

      empVar[k] = ((sumsq[k] - empMean[k]*empMean[k]/(*a_n))/(*a_n-1))/(*a_n);
      empMean[k] /= *a_n;
      empMean[k] += gradObs[0];
      a_normGrad[k]=empMean[k]/sqrt(empVar[k]);

    }
  offSet/=*a_n;
  *a_logL=offSet + fMax;
  
  /// Update Q2
  for(int i=0;i<*a_nv;i++)
    {
      for(int j=0;j<*a_nx;j++)
	{

	    if((a_vGrid[i] < a_xGrid[j] + *a_delta)) continue;

	    double coef=0.0;
	    if(ABS(a_xGrid[j])>DBL_EPSILON)
	    {
		for(int k=0; k<=*a_degree;k++)
		{
		    ///printf("%d ",k);
		    coef += a_epsOpt[k]*pow(a_xGrid[j],(double)k);
		}
	    }

	    a_Q2[i+*a_nv*j]*=(1.0+grad[i+*a_nv*j]*coef);
	}
    }
  
  /// Normalize Q2
  double normC;
  Norm(&normC,a_xGrid,a_vGrid,a_nx,a_nv, a_Q2);

  for(int i=0;i<*a_nv;i++)
    {
      for(int j=0;j<*a_nx;j++)
	{
	  a_Q2[i+*a_nv*j]/=normC;
	}
    }

}
Exemplo n.º 4
0
// Start from the current value and move towards the end value
void CInterpolatedValue::InitFromCurrent( float endValue, float dt, InterpType_t type /*= INTERP_LINEAR*/ )
{
	Init( Interp( gpGlobals->curtime ), endValue, dt, type );
}
Exemplo n.º 5
0
void image_colour_at(const ImageData *image, DBL xcoor, DBL ycoor, RGBFTColour& colour, int *index, bool premul)
{
    *index = -1;

    bool doProperTransmitAll = image->data->HasTransparency() &&
                               !image->AllTransmitLegacyMode &&
                               !image->data->IsIndexed() && ( (image->AllTransmit != 0.0) || (image->AllFilter != 0.0) );

    // If either source or destination uses premultiplied alpha, make sure interpolation is done in premultiplied space
    // as it makes the mathematical operations cleaner -- unless the alpha channel is modulated by "transmit all" or
    // "filter all", in which case we prefer non-premultiplied space.
    bool getPremul = doProperTransmitAll ? (premul && image->data->IsPremultiplied()) :
                                           (premul || image->data->IsPremultiplied());

    switch(image->Interpolation_Type)
    {
        case NO_INTERPOLATION:
            no_interpolation(image, xcoor, ycoor, colour, index, getPremul);
            break;
        case BICUBIC:
            InterpolateBicubic(image, xcoor, ycoor, colour, index, getPremul);
            break;
        default:
            Interp(image, xcoor, ycoor, colour, index, getPremul);
            break;
    }
    bool havePremul = getPremul;

    if (!premul && havePremul)
    {
        // We fetched premultiplied data, but caller expects it non-premultiplied, so we need to fix that.
        // As "transmit/filter all" handling also works best on non-premultiplied data, we're doing this now.
        AlphaUnPremultiply(colour.rgb(), colour.FTtoA());
        havePremul = false;
    }

    if (doProperTransmitAll)
    {
        COLC imageAlpha = colour.FTtoA();

        if (imageAlpha != 0.0)  // No need to apply "filter/transmit all" if the image is fully transparent here anyway.
        {


            colour.transm() += image->AllTransmit * imageAlpha;
            colour.filter() += image->AllFilter   * imageAlpha;

            if (havePremul)
            {
                // We have premultiplied data here, and the caller expects it to stay that way (otherwise we'd already
                // have converted to non-premultiplied by now), so we need to fix the premultiplication of the colour
                // according to our modifications to the transparency components.
                COLC alphaCorrection = colour.FTtoA() / imageAlpha;
                AlphaPremultiply(colour.rgb(), alphaCorrection);
            }
        }
    }

    if (premul && !havePremul)
    {
        // We have non-premultiplied data here, but caller expects it premultiplied, so we need to fix that
        // As "transmit/filter all" handling works best on non-premultiplied data, we haven't done this earlier.
        AlphaPremultiply(colour.rgb(), colour.FTtoA());
    }
}
Exemplo n.º 6
0
//------------------------------------------------------------------------
void CGunTurret::UpdateOrientation(float deltaTime)
{
	FUNCTION_PROFILER(GetISystem(), PROFILE_GAME);

	bool changed = false;
	bool searching = (m_targetId==0 && m_turretparams.searching);
	float speed	= searching ? m_turretparams.search_speed : m_turretparams.turn_speed;

	assert(m_goalYaw >= 0.f && m_goalYaw <= gf_PI2);

	// update turret
	Matrix34 turretTM = GetEntity()->GetSlotLocalTM(eIGS_Aux0, false);
	Ang3 turretAngles(turretTM);

	if(turretAngles.z < 0.0f)
		turretAngles.z+=gf_PI2;

	if(cry_fabsf(m_goalYaw-turretAngles.z) > gf_PI)
	{
		if(m_goalYaw >= gf_PI)
			turretAngles.z += gf_PI2;
		else
			turretAngles.z -= gf_PI2;
	}

	if(m_turretparams.yaw_range < 360.f)
	{
		// reverse, to avoid forbidden range
		if(m_goalYaw > gf_PI && turretAngles.z < gf_PI)
			turretAngles.z += gf_PI2;
		else if(m_goalYaw < gf_PI && turretAngles.z > gf_PI)
			turretAngles.z -= gf_PI2;
	}

	if(cry_fabsf(turretAngles.z-m_goalYaw) > 0.001f)
	{
		Interp(turretAngles.z, m_goalYaw, speed, deltaTime, 0.25f*speed);

		if(m_turretSound == INVALID_SOUNDID && gEnv->IsClient())
			m_turretSound = PlayAction(g_pItemStrings->turret);

		changed = true;
	}
	else if(m_turretSound != INVALID_SOUNDID)
	{
		StopSound(m_turretSound);
		m_turretSound = INVALID_SOUNDID;
	}

	if(changed)
	{
		turretTM.SetRotationXYZ(turretAngles,turretTM.GetTranslation());
		GetEntity()->SetSlotLocalTM(eIGS_Aux0, turretTM);
	}

	// update weapon
	Matrix34 weaponTM = GetEntity()->GetSlotLocalTM(eIGS_ThirdPerson, false);
	Ang3 weaponAngles(weaponTM);

	weaponAngles.z = turretAngles.z;

	if(cry_fabsf(weaponAngles.x-m_goalPitch) > 0.001f)
	{
		Interp(weaponAngles.x, m_goalPitch, speed, deltaTime, 0.25f*speed);

		if(m_cannonSound == INVALID_SOUNDID && gEnv->IsClient())
			m_cannonSound = PlayAction(g_pItemStrings->cannon);

		changed = true;
	}
	else if(m_cannonSound != INVALID_SOUNDID)
	{
		StopSound(m_cannonSound);
		m_cannonSound = INVALID_SOUNDID;
	}

	if(changed)
	{
		weaponTM.SetRotationXYZ(weaponAngles);
		Vec3 w_trans = turretTM.TransformPoint(m_radarHelperPos);
		//Vec3 w_trans = GetSlotHelperPos(eIGS_Aux0,m_radarHelper.c_str(),false);
		weaponTM.SetTranslation(w_trans);

		GetEntity()->SetSlotLocalTM(eIGS_ThirdPerson, weaponTM);

		if(GetEntity()->IsSlotValid(eIGS_Aux1))
		{
			Vec3 b_trans = weaponTM.TransformPoint(m_barrelHelperPos);
			//Vec3 b_trans = GetSlotHelperPos(eIGS_ThirdPerson,m_barrelHelper.c_str(),false);
			weaponTM.SetTranslation(b_trans);
			GetEntity()->SetSlotLocalTM(eIGS_Aux1, weaponTM*m_barrelRotation);
		}

		if(gEnv->IsClient())
		{
			for(TEffectInfoMap::const_iterator it=m_effects.begin(); it!=m_effects.end(); ++it)
			{
				Matrix34 tm(GetSlotHelperRotation(eIGS_ThirdPerson,it->second.helper.c_str(),true), GetSlotHelperPos(eIGS_ThirdPerson,it->second.helper.c_str(),true));
				SetEffectWorldTM(it->first, tm);
			}
		}
	}

	UpdatePhysics();

	if(g_pGameCVars->i_debug_turrets == eGTD_Basic)
	{
		DrawDebug();
		//gEnv->pRenderer->DrawLabel(GetEntity()->GetWorldPos(), 1.4f, "%s yaw: %.2f, goalYaw: %.2f (%.2f), goalPitch: %.2f (%.2f/%.2f)", searching?"[search]":"", RAD2DEG(turretAngles.z), RAD2DEG(m_goalYaw), 0.5f*(m_turretparams.yaw_range), RAD2DEG(m_goalPitch), m_turretparams.min_pitch, m_turretparams.max_pitch);
	}
}