summaryrefslogtreecommitdiff
path: root/lily/collision.cc
blob: db99d7b4c701fa07a2f723b8a280b3f3e0a58e19 (about) (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
  collision.cc -- implement Collision

  source file of the GNU LilyPond music typesetter

  (c)  1997--1998 Han-Wen Nienhuys <hanwen@cs.uu.nl>
*/
#include "debug.hh"
#include "collision.hh"
#include "note-column.hh"
#include "note-head.hh"
#include "paper-def.hh"

Collision::Collision()
{
}

void
Collision::add_column (Note_column* ncol_l)
{
  // ugh.  Fixme.
  clash_l_arr_.push (ncol_l);
  add_element (ncol_l);
  add_dependency (ncol_l);
}
/**
  should derive of Array.
 */
static
int idx (int dir, bool h_shift_b)
{
  assert (abs (dir) == 1);
  int j = dir > 0 ? 0 : 3;
  if (h_shift_b)
	j += dir;
  return j;
}

/** This complicated routine moves note columns around horizontally
  (and rests vertically) to ensure that notes don't clash.

  This should be done better, probably.

  This routine is dedicated to Stine Randmael :-)

  */
void
Collision::do_pre_processing()
{
  if (clash_l_arr_.size() <= 1)
	return;

  /*
    [stem up, stem up shifted, stem down shifted, stem down]
    */
  Array<Note_column*> clash_group_arr_a[4];

  for (int i=0; i < clash_l_arr_.size(); i++)
    {
      Note_column* c_l = clash_l_arr_[i];
      if (! c_l->dir_)
	{
	  warning (_ ("No stem direction set. Ignoring column in clash."));
	  continue;
	}
      int d = (c_l->dir_);

      clash_group_arr_a[idx (d, c_l->h_shift_b_)].push (c_l);
    }


  for (int j=0; j < 4; j++)
    {
      if (clash_group_arr_a[j].size() > 1)
	{
	  warning (_ ("Too many clashing notecolumns. Ignoring them."));
	  return;
	}
    }
  int d = 1;
  do
    {
      if (!clash_group_arr_a[idx (d, false)].size())
	{
	  clash_group_arr_a[idx (d,  false)] = clash_group_arr_a[idx (d, true)];
	  clash_group_arr_a[idx (d, true)].clear();
	}
    }
  while ((d *= -1) != 1);


  Interval_t<int> y_extent[4];
  Note_column * col_l_a[4];
  Real x_off [4];
  int y_off[4];

  for (int j =0 ; j < 4; j++)
    {
      if (clash_group_arr_a[j].size())
	col_l_a[j] = clash_group_arr_a[j][0];
      else
	col_l_a[j] = 0;

      if (col_l_a[j])
	{
	  y_extent[j] = col_l_a[j]->head_positions_interval();
	}


      x_off [j] = 0.0;
      y_off[j] = 0;
    }

  do
    {
      x_off[idx (d, true)] = d*0.5;
    }
  while ((d *= -1) != 1);


  // y_extent: smallest y-pos noteball interval containing all balls
  // 4 (0..3) groups: stem up/down; shift on/off;
  Interval_t<int> middle (y_extent[idx (-1,0)].max(),
			  y_extent[idx (1,0)].min());
  Interval_t<int> open_middle (y_extent[idx (-1,0)].max()+1, y_extent[idx (1,0)].min ()-1);
  do
    {
      if (!open_middle.contains_b (y_extent[idx (d,true)]))
	x_off[idx (d, true)] = d *1.0 ;
    } while ((d *= -1) != 1);

  if (!middle.empty_b()
      && middle.length() < 2 && col_l_a[idx (1,0)] && col_l_a[idx (-1,0)]) {
    // reproduction of bugfix at 3am ?
    Note_head * nu_l= col_l_a[idx (1,0)]->head_l_arr_[0];
    Note_head * nd_l = col_l_a[idx (-1,0)]->head_l_arr_.top();
    if (! (nu_l->balltype_i_ == nd_l->balltype_i_
	   && nu_l->dots_i_ == nd_l->dots_i_  && middle.length() == 0))
      {
	x_off[idx (1,0)] -= 0.5;
	x_off[idx (1,1)] -= 0.5;
	x_off[idx (-1,1)] += 0.5;
	x_off[idx (-1,0)] += 0.5;
      }

  }
  Real inter_f = paper()->internote_f ();
  Real wid_f = paper()->note_width ();
  for (int j=0; j < 4; j++)
    {
      if (col_l_a[j])
	{
	  /* collision.cc:138: request for method `translate' is ambiguous

	     (shaddup)
	     */
	  Offset o (x_off[j] * wid_f, y_off[j] * inter_f);
	  col_l_a[j]->translate (o);
	  //	  ((Score_element*)col_l_a[j])->translate (o);
	}
    }
}




void
Collision::do_substitute_dependency (Score_element*o_l,Score_element*n_l)
{
  if (o_l)
    {
      clash_l_arr_.substitute (dynamic_cast<Note_column *> (o_l),
			       dynamic_cast <Note_column *> (n_l));

    }
}