Exemplo n.º 1
0
void SeqAnalyzer::PrintPillars()
{	
	int layer_size = ptr_frame_->SizeOfLayer();

	/* ranked by x */
	multimap<double, WF_edge*>base_queue;
	multimap<double, WF_edge*>::iterator it;
	for (int dual_i = 0; dual_i < Nd_; dual_i++)
	{
		WF_edge *e = ptr_frame_->GetEdge(ptr_wholegraph_->e_orig_id(dual_i));
		if (e->isPillar())
		{
			point center = e->CenterPos();
			base_queue.insert(make_pair(center.x(), e));
			UpdateStructure(e);
		}
	}

	for (it = base_queue.begin(); it != base_queue.end(); it++)
	{
		print_queue_.push_back(it->second);
	}

	printf("Size of base queue: %d\n", base_queue.size());

	/* angle state with pillars */
	for (int dual_i = 0; dual_i < Nd_; dual_i++)
	{
		WF_edge *e = ptr_frame_->GetEdge(ptr_wholegraph_->e_orig_id(dual_i));
		if (!ptr_dualgraph_->isExistingEdge(e))
		{
			ptr_collision_->DetectCollision(e, ptr_dualgraph_, angle_state_[dual_i]);
		}
	}
}
Exemplo n.º 2
0
bool SeqAnalyzer::TestifyStiffness(WF_edge *e)
{
	test_stiff_.Start();

	/* insert a trail edge */
	UpdateStructure(e);

	/* examinate stiffness on printing subgraph */
	ptr_stiffness_->Init();

	int Ns = ptr_dualgraph_->SizeOfFreeFace();
	VX D(Ns * 6);
	D.setZero();

	bool bSuccess = ptr_stiffness_->CalculateD(D);

	if (bSuccess)
	{
		for (int k = 0; k < Ns; k++)
		{
			VX offset(3);
			for (int t = 0; t < 3; t++)
			{
				offset[t] = D[k * 6 + t];
			}

			if (offset.norm() >= D_tol_)
			{
				//printf("$$$Stiffness offset: %lf\n", offset.norm());
				//ptr_stiffness_->WriteData(D, ptr_dualgraph_->e_orig_id(k) / 2);
				bSuccess = false;
				break;
			}
		}
	}

	D0_ = D;

	/* remove the trail edge */
	RecoverStructure(e);

	test_stiff_.Stop();
	return bSuccess;
}
Exemplo n.º 3
0
bool FFAnalyzer::GenerateSeq(int l, int h, int t)
{
	/* last edge */
	assert(h != 0);						// there must be pillars
	WF_edge *ei = print_queue_[h - 1];

	if (debug_)
	{
		printf("---searching edge #%d in layer %d, head %d, (tail %d)\n",
			ei->ID() / 2, l + 1, h, t);
	}

	/* exit */
	if (h == t)
	{
		if (debug_)
		{
			printf("***searching at layer %d finishes.\n", l + 1);
		}

		return true;
	}

	/* next choice */
	multimap<double, WF_edge*> choice;
	multimap<double, WF_edge*>::iterator it;

	/* next edge in current layer */
	int Nl = layers_[l].size();
	for (int j = 0; j < Nl; j++)
	{
		WF_edge *ej = layers_[l][j];
		/* cost weight */
		double cost = GenerateCost(ei, ej);
		if (cost != -1)
		{
			choice.insert(pair<double, WF_edge*>(cost, ej));
		}
	}

	/* ranked by weight */
	for (it = choice.begin(); it != choice.end(); it++)
	{
		WF_edge *ej = it->second;
		print_queue_.push_back(ej);

		/* update printed subgraph */
		UpdateStructure(ej);

		/* update collision */
		vector<vector<lld>> tmp_angle(3);
		UpdateStateMap(ej, tmp_angle);

		if (debug_)
		{
			printf("^^^choose edge #%d in layer %d with cost %lf\n",
				ej->ID() / 2, l + 1, it->first);
			printf("^^^entering next searching state.\n");
		}

		if (GenerateSeq(l, h + 1, t))
		{
			return true;
		}

		RecoverStateMap(ej, tmp_angle);
		RecoverStructure(ej);
		print_queue_.pop_back();
	}

	if (debug_)
	{
		printf("---searching at layer %d, head %d, (tail %d) ended.\n", l + 1, h, t);
	}

	return false;
}
Exemplo n.º 4
0
void FFAnalyzer::WriteRenderPath(int min_layer, int max_layer, char *ptr_path)
{
	int layer_size = ptr_frame_->SizeOfLayer();
	if (layer_size == 0)
	{
		return;
	}

	Init();
	min_layer--;
	max_layer--;

	layers_.resize(layer_size);
	for (int dual_i = 0; dual_i < Nd_; dual_i++)
	{
		WF_edge *e = ptr_frame_->GetEdge(ptr_wholegraph_->e_orig_id(dual_i));
		layers_[e->Layer()].push_back(e);
	}

	/* skip pillars */
	int hi = 0;
	for (; hi < Nd_; hi++)
	{
		WF_edge *e = print_queue_[hi];
		if (!e->isPillar())
		{
			break;
		}
		UpdateStructure(e);
	}

	/* skip printed structure */
	for (; hi < Nd_; hi++)
	{
		WF_edge *e = print_queue_[hi];
		if (e->Layer() >= min_layer)
		{
			break;
		}
		UpdateStructure(e);
	}

	/* angle state with printed structure */
	for (int dual_i = 0; dual_i < Nd_; dual_i++)
	{
		WF_edge *e = ptr_frame_->GetEdge(ptr_wholegraph_->e_orig_id(dual_i));
		if (!ptr_dualgraph_->isExistingEdge(e))
		{
			ptr_collision_->DetectCollision(e, ptr_dualgraph_, angle_state_[dual_i]);
		}
	}

	/* print starting from min_layer */
	WF_edge *ei = NULL;
	for (; hi < Nd_ - 1; hi++)
	{
		WF_edge *ej = print_queue_[hi];
		int dual_j = ptr_wholegraph_->e_dual_id(ej->ID());
		int l = ej->Layer();
		int Nl = layers_[l].size();

		// init the next layer
		if (ei == NULL || ei->Layer() != l)
		{
			/* max_z_ and min_z_ in current layer */
			min_z_ = 1e20;
			max_z_ = -min_z_;
			for (int k = 0; k < Nl; k++)
			{
				WF_edge *ek = layers_[l][k];
				point u = ek->pvert_->Position();
				point v = ek->ppair_->pvert_->Position();
				min_z_ = min(min_z_, (double)min(u.z(), v.z()));
				max_z_ = max(max_z_, (double)max(u.z(), v.z()));
			}
		}

		vector<double> cost(Nd_);
		double min_cost = 1e20;
		double max_cost = -min_cost;
		for (int k = 0; k < Nl; k++)
		{
			WF_edge *ek = layers_[l][k];
			int dual_k = ptr_wholegraph_->e_dual_id(ek->ID());
			cost[dual_k] = GenerateCost(ei, ek);
			if (cost[dual_k] != -1)
			{
				if (cost[dual_k] < min_cost)
				{
					min_cost = cost[dual_k];
				}
				if (cost[dual_k] > max_cost)
				{
					max_cost = cost[dual_k];
				}
			}
		}

		char id[10];
		sprintf(id, "%d", hi);

		string path = ptr_path;
		string file = path + "/PathRender_" + id + ".txt";
		FILE *fp = fopen(file.c_str(), "w+");
		//vector<vector<double>> writer;
	/*	double cost_max = 0;
		double cost_min = 1;*/
		for (int dual_k = 0; dual_k < Nd_; dual_k++)
		{
			double r;
			double g;
			double b;
			WF_edge *e = ptr_frame_->GetEdge(ptr_wholegraph_->e_orig_id(dual_k));

			if (ptr_dualgraph_->isExistingEdge(e))
			{
				r = 0.5;
				g = 0.5;
				b = 0.5;
			}
			else
			if (e->Layer() != l)
			{
				r = 1.0;
				g = 1.0;
				b = 1.0;
			}
			else
			if (cost[dual_k] == -1)
			{
				r = 0;
				g = 0;
				b = 0;
			}
			else
			{
				double cost_k;
				if (max_cost == -1e20 || max_cost == min_cost)
				{
					cost_k = 0.0;
				}
				else
				{
					cost_k = (cost[dual_k] - min_cost) / (max_cost - min_cost);
				}

				r = 1.0;
				g = cost_k;
				b = 0.0;
			}
			point u = e->pvert_->RenderPos();
			point v = e->ppair_->pvert_->RenderPos();
			fprintf(fp, "%lf %lf %lf %lf %lf %lf %lf %lf %lf\n",
				u.x(), u.y(), u.z(), v.x(), v.y(), v.z(), r, g, b);
		/*	if (g > cost_max)
				cost_max = g;
			if (g < cost_min)
				cost_min = g;

			point u = e->pvert_->RenderPos();
			point v = e->ppair_->pvert_->RenderPos();
			vector<double> temp_9;
			temp_9.push_back(u.x());
			temp_9.push_back(u.y());
			temp_9.push_back(u.z());
			temp_9.push_back(v.x());
			temp_9.push_back(v.y());
			temp_9.push_back(v.z());
			temp_9.push_back(r);
			temp_9.push_back(g);
			temp_9.push_back(b);	
			writer.push_back(temp_9);*/
		}
		//for (int i = 0; i < writer.size();i++)
		//{
		//fprintf(fp, "%lf %lf %lf %lf %lf %lf %lf %lf %lf\n",
		//	writer[i][0], writer[i][1], writer[i][2], writer[i][3], writer[i][4], writer[i][5], writer[i][6], (writer[i][7]-cost_min)/(cost_max-cost_min), writer[i][8]);//mapping to [0,1]
		//}
		fclose(fp);

		vector<vector<lld>> tmp_angle(3);
		UpdateStateMap(ej, tmp_angle);

		UpdateStructure(ej);
		ei = ej;
	}
}
void VineCopulaStructureSelect(double *bounds, int type, double *Structure, double *Families, double *Rotations, std::vector<double>& Thetas, double *U, int d, unsigned int n, int StructuringRule, double *familyset, int m)
{
    std::vector<int> families((d-1)*d/2);
    std::vector<int> rotations((d-1)*d/2);
    std::vector<double> thetas;
    
    int i,j,k,I;
    
    switch (type)
    {
        case 0: // C-Vine
        {
            std::vector<int> structure(d);
            
            for (i=0;i<d;i++)
            {
                structure[i] = i;
            }
            
            switch (StructuringRule)
            {
                case 0: // Maximizing the sum of absolute Kendall's tau in every tree
                {
                    I = UpdateStructure(structure, 0, U, d, d, n);
                    
                    std::vector<double> V(n*d);
                    
                    for (i=0;i<(int)n;i++)
                    {
                        V[i] = U[I*n+i];
                    }
                    
                    for (j=0;j<I;j++)
                    {
                        for (i=0;i<(int)n;i++)
                        {
                            V[(j+1)*n+i] = U[j*n+i];
                        }
                    }
                    
                    for (j=I+1;j<d;j++)
                    {
                        for (i=0;i<(int)n;i++)
                        {
                            V[j*n+i] = U[j*n+i];
                        }
                    }
                    
                    TreeSelect(bounds,type, &families[0], thetas, &rotations[0], &V[0], d, n, familyset, m);
                    
                    GetPseudoObs(&families[0], thetas, &rotations[0], &V[0], d, n);
                    
                    for (k=1;k<d-2;k++)
                    {
                        I = UpdateStructure(structure, 0, &V[n], d, d-k, n);
                        
                        for (i=0;i<(int)n;i++)
                        {
                            V[i] = V[(I+1)*n+i];
                        }
                        
                        for (j=I+1;j<d-k;j++)
                        {
                            for (i=0;i<(int)n;i++)
                            {
                                V[j*n+i] = V[(j+1)*n+i];
                            }
                        }
                        
                        TreeSelect(bounds,type, &families[d*k-k*(k+1)/2], thetas, &rotations[d*k-k*(k+1)/2], &V[0], d-k, n, familyset, m);
                        
                        GetPseudoObs(&families[d*k-k*(k+1)/2], thetas, &rotations[d*k-k*(k+1)/2], &V[0], d-k, n);
                    }
                    
                    TreeSelect(bounds,type, &families[d*(d-1)/2-1], thetas, &rotations[d*(d-1)/2-1], &V[n], 2, n, familyset, m);
                    break;
                }
// case 2: Choose the most dependent variable as the root node and list the other variables by their dependence to the root node in increasing order (Nikoloulopoulos et al. 2012, p.3665)
// case 3: Choose the most dependent variable as the root node and list the other variables sequentially by choosing the variable which is least dependent with the previously selcted one (Nikoloulopoulos et al. 2012, p.3665)
// case 1: Choose the most dependent variable as the root node and list the other variables by their dependence to the root node in decreasing order (Nikoloulopoulos et al. 2012, p.3665)
                default:
                {
                    I = UpdateStructure(structure, StructuringRule, U, d, d, n);
                    
                    std::vector<double> V(n*d);
                    
                    for (j=0;j<d;j++)
                    {
                        for (i=0;i<(int)n;i++)
                        {
                            V[j*n+i] = U[structure[j]*n+i];
                        }
                    }
                    
                    TreeSelect(bounds,type, &families[0], thetas, &rotations[0], &V[0], d, n, familyset, m);
                    
                    GetPseudoObs(&families[0], thetas, &rotations[0], &V[0], d, n);
                    
                    
                    for (k=1;k<d-2;k++)
                    {
                        TreeSelect(bounds,type, &families[d*k-k*(k+1)/2], thetas, &rotations[d*k-k*(k+1)/2], &V[k*n], d-k, n, familyset, m);
                        
                        GetPseudoObs(&families[d*k-k*(k+1)/2], thetas, &rotations[d*k-k*(k+1)/2], &V[k*n], d-k, n);
                    }
                    
                    TreeSelect(bounds,type, &families[d*(d-1)/2-1], thetas, &rotations[d*(d-1)/2-1], &V[(d-2)*n], 2, n, familyset, m);
                }
            }
            
            
            Thetas.resize(thetas.size());
            
            switch (StructuringRule)
            {
                case 0:
                {
                    std::vector<int> NumbParams((d-1)*d/2+1);
                    int J=0;
                    
                    for (i=0;i<(d-1)*d/2;i++)
                    {
                        switch((int) families[i]){
                            case 0:
                            {
                                NumbParams[i+1] = NumbParams[i];
                                break;
                            }
                            case 18:
                            {
                                NumbParams[i+1] = NumbParams[i] +3;
                                break;
                            }
                            case 3: case 4: case 5: case 6: case 12: case 16: case 17: case 19:
                            {
                                NumbParams[i+1] = NumbParams[i] +2;
                                break;
                            }
                            default:
                            {
                                NumbParams[i+1] = NumbParams[i] +1;
                            }
                            
                        }
                    }
                    std::vector<std::pair<int, int> > R(d-1);
                    std::vector<int> Ranks(d-1);
                    
                    for (k=0;k<d-1;k++)
                    {
                        // Computing ranks
                        for (i=0;i<d-k-1;i++)
                        {
                            R[i] = std::make_pair(structure[i+1+k],i);
                        }
                        
                        std::sort(&R[0],&R[d-k-1]);
                        
                        for (i=0;i<d-k-1;i++)
                        {
                            Ranks[R[i].second] = i;
                        }
                        
                        for (i=0;i<d-k-1;i++)
                        {
                            Families[d*k-k*(k+1)/2+i] = (double) families[d*k-k*(k+1)/2+Ranks[i]];
                            Rotations[d*k-k*(k+1)/2+i] = (double) rotations[d*k-k*(k+1)/2+Ranks[i]];
                            switch(NumbParams[d*k-k*(k+1)/2+Ranks[i]+1]-NumbParams[d*k-k*(k+1)/2+Ranks[i]]){
                                case 0:
                                {
                                    break;
                                }
                                case 1:
                                {
                                    Thetas[J] = thetas[NumbParams[d*k-k*(k+1)/2+Ranks[i]]];
                                    J++;
                                    break;
                                }
                                case 2:
                                {
                                    Thetas[J] = thetas[NumbParams[d*k-k*(k+1)/2+Ranks[i]]];
                                    Thetas[J+1] = thetas[NumbParams[d*k-k*(k+1)/2+Ranks[i]]+1];
                                    J = J+2;
                                    break;
                                }
                                default:
                                {
                                    Thetas[J] = thetas[NumbParams[d*k-k*(k+1)/2+Ranks[i]]];
                                    Thetas[J+1] = thetas[NumbParams[d*k-k*(k+1)/2+Ranks[i]]+1];
                                    Thetas[J+2] = thetas[NumbParams[d*k-k*(k+1)/2+Ranks[i]]+2];
                                    J = J+3;
                                }
                            }
                        }
                    }
                    break;
                }
                default:
                {
                    for (i=0;i<(d-1)*d/2;i++)
                    {
                        Families[i] = (double) families[i];
                        Rotations[i] = (double) rotations[i];
                    }
                    
                    for (i=0;i<(int) thetas.size();i++)
                    {
                        Thetas[i] = thetas[i];
                    }
                    
                }
            }
            
            for (i=0;i<d;i++)
            {
                Structure[i] = (double) structure[i];
            }
            break;
        }
        case 1: // D-Vine
        {
            std::vector<double> V((d-2)*n);
            std::vector<double> H((d-2)*n);
            std::vector<double> U1(n),V1(n);
            
            TreeSelect(bounds,type, &families[0], thetas, &rotations[0], &U[0], d, n, familyset, m);
            
            int J =0;
            
            for (i=0;i<d-1;i++)
            {
                if (rotations[i]>0)
                {
                    Rotate_Obs(&U[i*n],&U[(i+1)*n],&U1[0],&V1[0],rotations[i],n);
                    if (i<d-2) {
                        PairCopulaHfun_Rotated_Obs(families[i], rotations[i], &thetas[J], &U1[0], &V1[0], &H[i*n], n);
                    }
                    if (i>0) {
                        PairCopulaVfun_Rotated_Obs(families[i], rotations[i], &thetas[J], &U1[0], &V1[0], &V[(i-1)*n], n);
                    }
                }
                else
                {
                    if (i<d-2) {
                        PairCopulaHfun(families[i], &thetas[J], &U[i*n], &U[(i+1)*n], &H[i*n], n);
                    }
                    if (i>0) {
                        PairCopulaVfun(families[i], &thetas[J], &U[i*n], &U[(i+1)*n], &V[(i-1)*n], n);
                    }
                }
                switch(families[i]){
                    case 0:
                    {
                        break;
                    }
                    case 18:
                    {
                        J += 3;
                        break;
                    }
                    case 3: case 4: case 5: case 6: case 12: case 16: case 17: case 19:
                    {
                        J += 2;
                        break;
                    }
                    default:
                    {
                        J += 1;
                    }
                }
            }

            
            for (k=1;k<d-2;k++)
            {
                TreeSelect(bounds,type, &families[d*k-k*(k+1)/2], thetas, &rotations[d*k-k*(k+1)/2], &H[0], &V[0], d-k, n, familyset, m);
                
                for (i=0;i<d-k-1;i++)
                {
                    if (rotations[d*k-k*(k+1)/2+i]>0)
                    {
                        Rotate_Obs(&H[i*n],&V[i*n],&U1[0],&V1[0],rotations[d*k-k*(k+1)/2+i],n);
                        if (i>0) {
                            PairCopulaVfun_Rotated_Obs(families[d*k-k*(k+1)/2+i], rotations[d*k-k*(k+1)/2+i], &thetas[J], &U1[0], &V1[0], &V[(i-1)*n], n);
                        }
                        if (i<d-k-2) {
                            PairCopulaHfun_Rotated_Obs(families[d*k-k*(k+1)/2+i], rotations[d*k-k*(k+1)/2+i], &thetas[J], &U1[0], &V1[0], &H[i*n], n);
                        }
                    }
                    else
                    {
                        if (i>0) {
                            PairCopulaVfun(families[d*k-k*(k+1)/2+i], &thetas[J], &H[i*n], &V[i*n], &V[(i-1)*n], n);
                        }
                        if (i<d-k-2) {
                            PairCopulaHfun(families[d*k-k*(k+1)/2+i], &thetas[J], &H[i*n], &V[i*n], &H[i*n], n);
                        }
                    }
                    switch(families[d*k-k*(k+1)/2+i]){
                        case 0:
                        {
                            break;
                        }
                        case 18:
                        {
                            J += 3;
                            break;
                        }
                        case 3: case 4: case 5: case 6: case 12: case 16: case 17: case 19:
                        {
                            J += 2;
                            break;
                        }
                        default:
                        {
                            J += 1;
                        }
                    }
                }
            }

            TreeSelect(bounds,type, &families[d*(d-1)/2-1], thetas, &rotations[d*(d-1)/2-1], &H[0], &V[0], 2, n, familyset, m);
            
            Thetas.resize(thetas.size());
            
            for (i=0;i<(d-1)*d/2;i++)
            {
                Families[i] = (double) families[i];
                Rotations[i] = (double) rotations[i];
            }
            
            for (i=0;i<(int) thetas.size();i++)
            {
                Thetas[i] = thetas[i];
            }
        }
    }
    
    return;
}