Пример #1
0
void
toggle_polyline_polygon(F_line *line, F_point *previous_point, F_point *selected_point)
{
  F_point *point, *last_pt;
  
  last_pt = last_point(line->points);

  if (line->type == T_POLYLINE)
    {

      if (line->points->next == NULL || line->points->next->next == NULL) {
	  put_msg("Not enough points for a polygon");
	  beep();
	  return;  /* less than 3 points - don't close the polyline */
      }
      if ((point = create_point()) == NULL)
	  return;
      
      point->x = last_pt->x;
      point->y = last_pt->y;
      point->next = line->points;
      line->points = point;
      
      line->type = T_POLYGON;
      clean_up();
      set_last_arrows(line->for_arrow, line->back_arrow);
      line->back_arrow = line->for_arrow = NULL;
    }
  else if (line->type == T_POLYGON)
    {
      point = line->points;
      line->points = point->next;           /* unchain the first point */
      free((char *) point);
            
      if ((line->points != selected_point) && (previous_point != NULL))
	{
	  last_pt->next = line->points;     /* let selected point become */
	  previous_point->next = NULL;      /* first point */
	  line->points = selected_point;
	}
      line->type = T_POLYLINE;
      clean_up();
    }
  redisplay_line(line);
  set_action_object(F_OPEN_CLOSE, O_POLYLINE);
  set_last_selectedpoint(line->points);
  set_last_prevpoint(NULL);
  set_latestline(line);
  set_modifiedflag();
}
Пример #2
0
double simple_seg_weight(vector<MyMarker*> & seg1, vector<MyMarker*> & seg2, vector<pair<int, int> > & matching_res)
{
    if (seg1.size()<=1 || seg2.size()<=1) //single-point branch can map to any branch without constrains
        return 0;
    int k=0, l=0;
    vector<vector<double> > matrix(seg1.size(), vector<double>(seg2.size(), MAX_DOUBLE));
    vector<vector<pair<int, int> > > last_point(seg1.size(), vector<pair<int, int> >(seg2.size(), pair<int, int>(-1, -1)));
//    vector<vector<double> > matrix(seg1.size()-1, vector<double>(seg2.size()-1, MAX_DOUBLE));
//    vector<vector<pair<int, int> > > last_point(seg1.size(), vector<pair<int, int> >(seg2.size(), pair<int, int>(-1, -1)));
    double distance, last_dist;
    //matrix[0][0] = euc_dist(seg1, seg2, 0, 1, 0, 1);
    matrix[0][0] = dist(*(seg1[0]), *(seg2[0]));

    for (int i=0;i<seg1.size();i++)
//    for (int i=0;i<seg1.size()-1;i++)
    {
        for (int j=0;j<seg2.size();j++)
//        for (int j=0;j<seg2.size()-1;j++)
        {
           // printf("%i %i\n",i,j);
            k = 1; l = 0;
            if (i-k>=0 && j-l>=0){
                last_dist = matrix[i-k][j-l];
                distance = dist(*(seg1[i]), *(seg2[j]));
//                distance = euc_dist(seg1, seg2, i-k, i+1, j-l, j+1);
                if (last_dist + distance <= matrix[i][j])
                {
                    matrix[i][j]  = last_dist + distance;
                    last_point[i][j].first = i-k;
                    last_point[i][j].second = j-l;
                }
            }
            
            k = 0; l = 1;
            if (i-k>=0 && j-l>=0){
                last_dist = matrix[i-k][j-l];
                distance = dist(*(seg1[i]), *(seg2[j]));
                //                distance = euc_dist(seg1, seg2, i-k, i+1, j-l, j+1);
                if (last_dist + distance <= matrix[i][j])
                {
                    matrix[i][j]  = last_dist + distance;
                    last_point[i][j].first = i-k;
                    last_point[i][j].second = j-l;
                }
            }
            
            k = 1; l = 1;
            if (i-k>=0 && j-l>=0){
                last_dist = matrix[i-k][j-l];
                distance = dist(*(seg1[i]), *(seg2[j]));
                //                distance = euc_dist(seg1, seg2, i-k, i+1, j-l, j+1);
                if (last_dist + distance <= matrix[i][j])
                {
                    matrix[i][j]  = last_dist + distance;
                    last_point[i][j].first = i-k;
                    last_point[i][j].second = j-l;
                }
            }
        }
    }
    matching_res.push_back(pair<int, int>(seg1.size()-1, seg2.size()-1));
    
    
    int lastp1 = seg1.size()-1;
    int lastp2 = seg2.size()-1;
    int p1, p2;
    do
    {
        p1 = last_point[lastp1][lastp2].first;
        p2 = last_point[lastp1][lastp2].second;
        if (p1<0 || p2<0){
            //matching_res.push_back(pair<int,int>(0, 0));
            break;
        }
        matching_res.push_back(pair<int,int>(p1, p2));
        lastp1 = p1;
        lastp2 = p2;
    }
    while (true);
    
    return matrix.back().back();
};
Пример #3
0
double local_align_position_and_angle(vector<MyMarker*> const seg1, vector<MyMarker*> const seg2, std::vector<MyMarker*> const vectors1, std::vector<MyMarker*> const vectors2, pair<std::vector<int>,std::vector<int> > &matching_res, std::vector<double> &position_scores, float const euclidean_distance_normalization, float const cos_angle_distance_normalization, float const gap_cost)
{
    if (seg1.size() < 1 || seg2.size() < 1) //single-point branch can map to any branch without constrains
        return 0;
    int k=0, l=0;
    // Dynamic programming matrix
    vector<vector<double> > matrix(seg1.size()+1, vector<double>(seg2.size()+1, 0));
    vector<vector<double> > contrib_matrix(seg1.size()+1, vector<double>(seg2.size()+1, 0));
    // Path through matrix to produce point alignment
    vector<vector<pair<int, int> > > last_point(seg1.size()+1, vector<pair<int, int> >(seg2.size()+1, pair<int, int>(-1, -1)));
    
    // Consider tracking additional local alignments?
    double best_score = 0, new_score;
    pair<int,int> best_position(-1,-1);
    bool use_vector = true;
    //   printf("starting local align seg1 size %i seg2 size %i\n",seg1.size(),seg2.size());
    //    matrix[0][0] = euc_dist(seg1, seg2, 0, 1, 0, 1); // Should this just be 0?
    for (int i=1;i<seg1.size()+1;i++)
    {
        /*
        MyMarker avg_vect1;
        use_vector = use_vector & apply_kernel(vectors1, i, alignment_kernel, avg_vect1);
         */
        for (int j=1;j<seg2.size()+1;j++)
        {
            //printf("%i %i\n",i,j);
            // Gap/skip seg 1
            new_score = matrix[i][j-1] + gap_cost;
            if (new_score > matrix[i][j]){ // new_score could be less than 0
                last_point[i][j].first = i;
                last_point[i][j].second = j-1;
                contrib_matrix[i][j] = gap_cost;
            }
            
            // Gap/skip seg 2
            new_score = matrix[i-1][j] + gap_cost;
            if (new_score > matrix[i][j]){
                matrix[i][j] = new_score;
                last_point[i][j].first = i-1;
                last_point[i][j].second = j;
                contrib_matrix[i][j] = gap_cost;
            }
            
            // Match
            //new_score = matrix[i-1][j-1] + average_dist - euc_dist(seg1, seg2, i-1, i, j-1, j); // For
            //printf("markers %p %p\n",seg1[i-1],seg2[j-1]);
            MyMarker avg_vect2;
            
            double match_score;
            match_score = position_and_angle_score(seg1[i-1], seg2[j-1], vectors1[i-1], vectors2[j-1], euclidean_distance_normalization, cos_angle_distance_normalization);
/*            if (vectors[i-1] && vectors2[j-1])
                match_score = position_and_angle_score(seg1[i-1], seg2[j-1], vectors1[i-1], vectors2[j-1], combined_dist_threshold);
            else
                match_score = position_score(seg1[i-1], seg2[j-1], vectors1[i-1], vectors2[j-1], euclidean_dist_threshold);*/

            new_score = matrix[i-1][j-1] + match_score;
            //printf("after dist\n");
            if (new_score > matrix[i][j]){
                matrix[i][j] = new_score;
                last_point[i][j].first = i-1;
                last_point[i][j].second = j-1;
                contrib_matrix[i][j] = match_score;
            }
            
            // Keep track of best local alignment
            if (matrix[i][j] > best_score){
                best_score = matrix[i][j];
                best_position.first = i;
                best_position.second = j;
            }
        }
    }
    //printf("Done aligning, now on to backtracing, best score %f at %i %i\n",best_score,best_position.first,best_position.second);
    
    if (best_score == 0){
        return 0;
    }
    
    // From global alignment
    //matching_res.push_back(pair<int, int>(seg1.size()-1, seg2.size()-1));
    
    // Find optimal local alignment
    //matching_res.push_back(pair<int,int>(best_position.first-1,best_position.second-1));
    matching_res.first.push_back(best_position.first-1);
    matching_res.second.push_back(best_position.second-1);
    position_scores.push_back(contrib_matrix[best_position.first][best_position.second]);
    
    int lastp1 = best_position.first;
    int lastp2 = best_position.second;
    int p1, p2;
    do
    {
        //printf("backtrace pos %i %i\n",lastp1,lastp2);
        p1 = last_point[lastp1][lastp2].first;
        p2 = last_point[lastp1][lastp2].second;
        //printf("next pos %i %i\n",p1,p2);
        if (p1<=0 || p2<=0 || matrix[p1][p2] <= 0)
            break;
        //        matching_res.push_back(pair<int,int>(p1-1, p2-1)); // Segment indices
        matching_res.first.push_back(p1-1); // Segment indices
        matching_res.second.push_back(p2-1);
        position_scores.push_back(contrib_matrix[p1][p2]);
        lastp1 = p1;
        lastp2 = p2;
    }
    while (true);
    //printf("done backtracing\n");
    // Reverse alignment so it starts at the beginning of the segment
    //std::reverse(matching_res.begin(),matching_res.end());
    std::reverse(matching_res.first.begin(),matching_res.first.end());
    std::reverse(matching_res.second.begin(),matching_res.second.end());
    std::reverse(position_scores.begin(),position_scores.end());

    /*
    // Delete vectors
    for (int i = 0; i < vectors1.size(); i++){
        if (vectors1[i]) delete(vectors1[i]);
    }
    for (int i = 0; i < vectors2.size(); i++){
        if (vectors2[i]) delete(vectors2[i]);
    }
     */
    
    //return matrix.back().back();
    return best_score;
};
Пример #4
0
double local_align(float average_dist, vector<MyMarker*> & seg1, vector<MyMarker*> & seg2, pair<std::vector<int>,std::vector<int> > & matching_res, float const gap_cost)
{
    if (seg1.size() < 1 || seg2.size() < 1) //single-point branch can map to any branch without constrains
        return 0;
    int k=0, l=0;
    // Dynamic programming matrix
    vector<vector<double> > matrix(seg1.size()+1, vector<double>(seg2.size()+1, 0));
    // Path through matrix to produce point alignment
    vector<vector<pair<int, int> > > last_point(seg1.size()+1, vector<pair<int, int> >(seg2.size()+1, pair<int, int>(-1, -1)));
    
    // Consider tracking additional local alignments?
    double best_score = 0, new_score;
    pair<int,int> best_position(-1,-1);
 //   printf("starting local align seg1 size %i seg2 size %i\n",seg1.size(),seg2.size());
//    matrix[0][0] = euc_dist(seg1, seg2, 0, 1, 0, 1); // Should this just be 0?
    for (int i=1;i<seg1.size()+1;i++)
    {
        for (int j=1;j<seg2.size()+1;j++)
        {
            //printf("%i %i\n",i,j);
            // Gap/skip seg 1
            new_score = matrix[i][j-1] + gap_cost;
            if (new_score > matrix[i][j]){ // new_score could be less than 0
                last_point[i][j].first = i;
                last_point[i][j].second = j-1;
            }
            
            // Gap/skip seg 2
            new_score = matrix[i-1][j] + gap_cost;
            if (new_score > matrix[i][j]){
                matrix[i][j] = new_score;
                last_point[i][j].first = i-1;
                last_point[i][j].second = j;
            }
            
            // Match
            //new_score = matrix[i-1][j-1] + average_dist - euc_dist(seg1, seg2, i-1, i, j-1, j); // For
            //printf("markers %p %p\n",seg1[i-1],seg2[j-1]);
            new_score = matrix[i-1][j-1] + (average_dist - dist(*(seg1[i-1]), *(seg2[j-1])));
            //printf("after dist\n");
            if (new_score > matrix[i][j]){
                matrix[i][j] = new_score;
                last_point[i][j].first = i-1;
                last_point[i][j].second = j-1;
            }

            // Keep track of best local alignment
            if (matrix[i][j] > best_score){
                best_score = matrix[i][j];
                best_position.first = i;
                best_position.second = j;
            }
        }
    }
    //printf("Done aligning, now on to backtracing, best score %f at %i %i\n",best_score,best_position.first,best_position.second);
    
    if (best_score == 0){
        return 0;
    }
    
    // From global alignment
    //matching_res.push_back(pair<int, int>(seg1.size()-1, seg2.size()-1));
    
    // Find optimal local alignment
    //matching_res.push_back(pair<int,int>(best_position.first-1,best_position.second-1));
    matching_res.first.push_back(best_position.first-1);
    matching_res.second.push_back(best_position.second-1);
    
    int lastp1 = best_position.first;
    int lastp2 = best_position.second;
    int p1, p2;
    do
    {
        //printf("backtrace pos %i %i\n",lastp1,lastp2);
        p1 = last_point[lastp1][lastp2].first;
        p2 = last_point[lastp1][lastp2].second;
        //printf("next pos %i %i\n",p1,p2);
        if (p1<=0 || p2<=0)
            break;
//        matching_res.push_back(pair<int,int>(p1-1, p2-1)); // Segment indices
        matching_res.first.push_back(p1-1); // Segment indices
        matching_res.second.push_back(p2-1);
        lastp1 = p1;
        lastp2 = p2;
    }
    while (true);
    //printf("done backtracing\n");
    // Reverse alignment so it starts at the beginning of the segment
    //std::reverse(matching_res.begin(),matching_res.end());
    std::reverse(matching_res.first.begin(),matching_res.first.end());
    std::reverse(matching_res.second.begin(),matching_res.second.end());

    //return matrix.back().back();
    return best_score;
};
Пример #5
0
void
toggle_open_closed_spline(F_spline *spline, F_point *previous_point, F_point *selected_point)
{
  F_point *last_pt;
  F_sfactor *last_sfactor, *previous_sfactor, *selected_sfactor;

  if (spline->points->next == NULL || spline->points->next->next == NULL) {
      put_msg("Not enough points for a spline");
      beep();
      return;  /* less than 3 points - don't close the spline */
  }

  last_pt = last_point(spline->points);
  last_sfactor = search_sfactor(spline, last_pt);

  if (previous_point == NULL)
    {
      previous_sfactor = NULL;
      selected_sfactor = spline->sfactors;
    }
  else
    {
      previous_sfactor = search_sfactor(spline, previous_point);
      selected_sfactor = previous_sfactor->next;
      set_last_tension(selected_sfactor->s, previous_sfactor->s);
    }

  draw_spline(spline, ERASE);

  if (closed_spline(spline))
    {      
      if (spline->points != selected_point)
	{
	  last_pt->next = spline->points;
	  last_sfactor->next = spline->sfactors;
	  previous_point->next = NULL;
	  previous_sfactor->next = NULL;
	  previous_sfactor->s = S_SPLINE_ANGULAR;
	  spline->points = selected_point;
	  spline->sfactors = selected_sfactor;
	}
      else
	{
	  last_sfactor->s = S_SPLINE_ANGULAR;
	}
      spline->sfactors->s = S_SPLINE_ANGULAR;
      spline->type = (x_spline(spline)) ? T_OPEN_XSPLINE :
	(int_spline(spline)) ? T_OPEN_INTERP : T_OPEN_APPROX;
      clean_up();
    }
  else
    {
      int type_tmp;
      double s_tmp;

      if(int_spline(spline))
	{
	  s_tmp = S_SPLINE_INTERP;
	  type_tmp = T_CLOSED_INTERP;
	}
      else
	if (x_spline(spline))
	  {
	      s_tmp = S_SPLINE_INTERP;
	      type_tmp = T_CLOSED_XSPLINE;
	    }
	else
	  {
	    s_tmp = S_SPLINE_APPROX;
	    type_tmp = T_CLOSED_APPROX;
	  }
      spline->sfactors->s = last_sfactor->s = s_tmp;
      spline->type = type_tmp;
      clean_up();
      set_last_arrows(spline->for_arrow, spline->back_arrow);
      spline->back_arrow = spline->for_arrow = NULL;
    }
  draw_spline(spline, PAINT);
  set_action_object(F_OPEN_CLOSE, O_SPLINE);
  set_last_selectedpoint(spline->points);
  set_last_prevpoint(NULL);
  set_latestspline(spline);
  set_modifiedflag();
}
Пример #6
0
void
spline_line(F_spline *s)
{
    F_line	   *l;
    F_point        *tmppoint;

    /* Now we turn s into a line */
    if ((l = create_line()) == NULL)
	return;

    if (open_spline(s)) {
	l->type = T_POLYLINE;
	l->points = s->points;
    } else {
	l->type = T_POLYGON;
	if ((l->points = create_point())==NULL)
	    return;
	tmppoint = last_point(s->points);
	l->points->x = tmppoint->x;
	l->points->y = tmppoint->y;
	l->points->next = copy_points(s->points);
    }
    l->style = s->style;
    l->thickness = s->thickness;
    l->pen_color = s->pen_color;
    l->fill_color = s->fill_color;
    l->depth = s->depth;
    l->style_val = s->style_val;
    l->cap_style = s->cap_style;
    l->join_style = cur_joinstyle;
    l->pen_style = s->pen_style;
    l->radius = DEFAULT;
    l->fill_style = s->fill_style;
    if (s->for_arrow) {
	l->for_arrow = create_arrow();
	l->for_arrow->type = s->for_arrow->type;
	l->for_arrow->style = s->for_arrow->style;
	l->for_arrow->thickness = s->for_arrow->thickness;
	l->for_arrow->wd = s->for_arrow->wd;
	l->for_arrow->ht = s->for_arrow->ht;
    } else {
	l->for_arrow = NULL;
    }
    if (s->back_arrow) {
	l->back_arrow = create_arrow();
	l->back_arrow->type = s->back_arrow->type;
	l->back_arrow->style = s->back_arrow->style;
	l->back_arrow->thickness = s->back_arrow->thickness;
	l->back_arrow->wd = s->back_arrow->wd;
	l->back_arrow->ht = s->back_arrow->ht;
    } else {
	l->back_arrow = NULL;
    }

    /* now we have finished creating the line, we can get rid of the spline */
    delete_spline(s);

    /* and put in the new line */
    mask_toggle_linemarker(l);
    list_add_line(&objects.lines, l);
    redisplay_line(l);
    set_action_object(F_CONVERT, O_SPLINE);
    set_latestline(l);
    set_modifiedflag();
    return;
}