// Private member functions: // Call this function after declare a CollageBasic instance. // This function will create a non-fixed aspect ratio image collage. bool CollageBasic::CreateCollage() { image_num_ = static_cast<int>(image_vec_.size()); if (image_num_ == 0) { std::cout << "Error: CreateCollage 1" << std::endl; return false; } if (canvas_width_ <= 0) { std::cout << "Error: CreateCollage 2" << std::endl; return false; } // A: generate a full balanced binary tree with image_num_ leaves. GenerateInitialTree(); // B: recursively calculate aspect ratio. canvas_alpha_ = CalculateAlpha(tree_root_); canvas_height_ = static_cast<int>(canvas_width_ / canvas_alpha_); // C: set the position for all the tile images in the collage. tree_root_->position_.x_ = 0; tree_root_->position_.y_ = 0; tree_root_->position_.height_ = canvas_height_; tree_root_->position_.width_ = canvas_width_; if (tree_root_->left_child_) CalculatePositions(tree_root_->left_child_); if (tree_root_->right_child_) CalculatePositions(tree_root_->right_child_); return true; };
void FAnimationActiveTransitionEntry::Update(const FAnimationUpdateContext& Context, int32 CurrentStateIndex, bool& bOutFinished) { bOutFinished = false; // Advance time if (bActive) { ElapsedTime += Context.GetDeltaTime(); if (ElapsedTime >= CrossfadeDuration) { bActive = false; bOutFinished = true; } if(CrossfadeDuration <= 0.0f) { Alpha = 1.0f; } else { Alpha = CalculateAlpha(ElapsedTime / CrossfadeDuration); } } }
// Recursively calculate aspect ratio for all the inner nodes. // The return value is the aspect ratio for the node. float CollageBasic::CalculateAlpha(TreeNode* node) { if (!node->is_leaf_) { float left_alpha = CalculateAlpha(node->left_child_); float right_alpha = CalculateAlpha(node->right_child_); if (node->split_type_ == 'v') { node->alpha_ = left_alpha + right_alpha; return node->alpha_; } else if (node->split_type_ == 'h') { node->alpha_ = (left_alpha * right_alpha) / (left_alpha + right_alpha); return node->alpha_; } else { std::cout << "Error: CalculateAlpha" << std::endl; return -1; } } else { // This is a leaf node, just return the image's aspect ratio. return node->alpha_; } }
//----------------------------------------------------------------------------- // Purpose: //----------------------------------------------------------------------------- void C_ScriptIntro::ClientThink( void ) { Assert( m_IntroData.m_Passes.Count() <= 2 ); if ( m_hCameraEntity ) { m_IntroData.m_vecCameraView = m_hCameraEntity->GetAbsOrigin(); m_IntroData.m_vecCameraViewAngles = m_hCameraEntity->GetAbsAngles(); } CalculateFOV(); CalculateAlpha(); // Calculate the blend levels of each pass float flPerc = 1.0; if ( (m_flNextBlendTime - m_flBlendStartTime) != 0 ) { flPerc = clamp( (gpGlobals->curtime - m_flBlendStartTime) / (m_flNextBlendTime - m_flBlendStartTime), 0, 1 ); } // Detect when we're finished blending if ( flPerc >= 1.0 ) { if ( m_IntroData.m_Passes.Count() == 2 ) { // We're done blending m_IntroData.m_Passes[0].m_BlendMode = m_IntroData.m_Passes[1].m_BlendMode; m_IntroData.m_Passes[0].m_Alpha = 1.0; m_IntroData.m_Passes.Remove(1); //Msg("FINISHED BLEND.\n"); } return; } /* if ( m_flNextBlendTime >= gpGlobals->curtime ) { Msg("INTRO BLENDING: Blending from mode %d to %d.\n", m_IntroData.m_Passes[0].m_BlendMode, m_IntroData.m_Passes[1].m_BlendMode ); Msg(" curtime %.2f StartedAt %.2f FinishAt: %.2f\n", gpGlobals->curtime, m_flBlendStartTime, m_flNextBlendTime ); Msg(" Perc: %.2f\n", flPerc ); } */ if ( m_IntroData.m_Passes.Count() == 2 ) { m_IntroData.m_Passes[0].m_Alpha = 1.0 - flPerc; m_IntroData.m_Passes[1].m_Alpha = flPerc; } else { m_IntroData.m_Passes[0].m_Alpha = 1.0 - flPerc; } }
// If we use CreateCollage, the generated collage may have strange aspect ratio such as // too big or too small, which seems to be difficult to be shown. We let the user to // input their expected aspect ratio and fast adjust to make the result aspect ratio // close to the user defined one. // The thresh here controls the closeness between the result aspect ratio and the expect // aspect ratio. e.g. expect_alpha is 1, thresh is 2. The result aspect ratio is around // [1 / 2, 1 * 2] = [0.5, 2]. // We also define MAX_ITER_NUM = 100, // If max iteration number is reached and we cannot find a good result aspect ratio, // this function returns false. int CollageBasic::CreateCollage(float expect_alpha, float thresh) { assert(thresh > 1); assert(expect_alpha > 0); tree_root_->alpha_expect_ = expect_alpha; float lower_bound = expect_alpha / thresh; float upper_bound = expect_alpha * thresh; int total_iter_counter = 1; // Do the initial tree generatio and calculation. // A: generate a full balanced binary tree with image_num_ leaves. GenerateInitialTree(); // B: recursively calculate aspect ratio. canvas_alpha_ = CalculateAlpha(tree_root_); while ((canvas_alpha_ < lower_bound) || (canvas_alpha_ > upper_bound)) { GenerateInitialTree(); canvas_alpha_ = CalculateAlpha(tree_root_); ++total_iter_counter; if (total_iter_counter > MAX_TREE_GENE_NUM) { std::cout << "*******************************" << std::endl; std::cout << "max iteration number reached..." << std::endl; std::cout << "*******************************" << std::endl; return -1; } } // std::cout << "Canvas generation success!" << std::endl; std::cout << "Total iteration number is: " << total_iter_counter << std::endl; // After adjustment, set the position for all the tile images. canvas_height_ = static_cast<int>(canvas_width_ / canvas_alpha_); tree_root_->position_.x_ = 0; tree_root_->position_.y_ = 0; tree_root_->position_.height_ = canvas_height_; tree_root_->position_.width_ = canvas_width_; if (tree_root_->left_child_) CalculatePositions(tree_root_->left_child_); if (tree_root_->right_child_) CalculatePositions(tree_root_->right_child_); return total_iter_counter; }
BetaDistributionWithTrend::BetaDistributionWithTrend(const NRLib::Trend * mean, const NRLib::Trend * var, const double & lower_limit, const double & upper_limit, int shared) : DistributionWithTrend(shared,true) { mean_ = mean->Clone(); var_ = var->Clone(); use_trend_cube_.resize(2); for(int i=0; i<2; i++) use_trend_cube_[i] = false; FindUseTrendCube(use_trend_cube_, mean_->GetTrendDimension(), mean_->GetReference()); FindUseTrendCube(use_trend_cube_, var_ ->GetTrendDimension(), var_ ->GetReference()); if(mean_->GetTrendDimension() == 0) { ni_ = 1; n_samples_mean_ = 1; } else { ni_ = 2; n_samples_mean_ = 100; } if(var_->GetTrendDimension() == 0) { nj_ = 1; n_samples_var_ = 1; } else { nj_ = 2; n_samples_var_ = 100; } double mean_min = mean_->GetMinValue(); double mean_max = mean_->GetMaxValue(); double var_min = var_->GetMinValue(); double var_max = var_->GetMaxValue(); double dx; if(ni_ == 1) dx = 0; else dx = (mean_max - mean_min)/(n_samples_mean_ - 1); double dy; if(nj_ == 1) dy = 0; else dy = (var_max - var_min) /(n_samples_var_ - 1); mean_sampling_.resize(n_samples_mean_); for(int i=0; i<n_samples_mean_; i++) mean_sampling_[i] = mean_min + i*dx; var_sampling_.resize(n_samples_var_); for(int j=0; j<n_samples_var_; j++) var_sampling_[j] = var_min + j*dy; double a; double b; beta_distribution_ = new NRLib::Grid2D<NRLib::Distribution<double> *>(n_samples_mean_, n_samples_var_, NULL); for(int i=0; i<n_samples_mean_; i++) { for(int j=0; j<n_samples_var_; j++) { CalculateAlpha(mean_sampling_[i], var_sampling_[j], lower_limit, upper_limit, a); CalculateBeta(mean_sampling_[i], var_sampling_[j], lower_limit, upper_limit, b); NRLib::Distribution<double> * dist = NULL; if(a > 0 && b > 0) dist = new NRLib::Beta(lower_limit, upper_limit, a, b); (*beta_distribution_)(i,j) = dist; } } }
void CalcAlphaBeta(void) { CalculateAlpha(); CalculateBeta(); }