JAL-2349 JAL-3855 highlight residues associated with elements under mouse - Jmol...
[jalview.git] / src / jalview / renderer / ContactGeometry.java
1 package jalview.renderer;
2
3 import java.util.Iterator;
4
5 import jalview.datamodel.ContactListI;
6
7 public class ContactGeometry
8 {
9   final int pixels_step;
10
11   final double contacts_per_pixel;
12
13   final int contact_height;
14
15   final int graphHeight;
16
17   public ContactGeometry(ContactListI contacts, int graphHeight)
18   {
19     this.graphHeight = graphHeight;
20     contact_height = contacts.getContactHeight();
21     // fractional number of contacts covering each pixel
22     contacts_per_pixel = (graphHeight < 1) ? contact_height
23             : ((double) contact_height) / ((double) graphHeight);
24
25     if (contacts_per_pixel >= 1)
26     {
27       // many contacts rendered per pixel
28       pixels_step = 1;
29     }
30     else
31     {
32       // pixel height for each contact
33       pixels_step = (int) Math
34               .ceil(((double) graphHeight) / (double) contact_height);
35     }
36   }
37
38   public class contactInterval
39   {
40     public contactInterval(int cStart, int cEnd, int pStart, int pEnd)
41     {
42       this.cStart = cStart;
43       this.cEnd = cEnd;
44       this.pStart = pStart;
45       this.pEnd = pEnd;
46     }
47
48     // range on contact list
49     public final int cStart;
50
51     public final int cEnd;
52
53     // range in pixels
54     public final int pStart;
55
56     public final int pEnd;
57   }
58
59   /**
60    * 
61    * @param pStart
62    * @param pEnd
63    * @return range for
64    */
65   public contactInterval mapFor(int pStart, int pEnd)
66   {
67     int cStart = (int) Math.floor(pStart * contacts_per_pixel);
68     contactInterval ci = new contactInterval(cStart,
69             (int) Math.min(contact_height,
70                     Math.ceil(
71                             cStart + (pEnd - pStart) * contacts_per_pixel)),
72             pStart, pEnd);
73
74     return ci;
75   }
76
77   /**
78    * return the cell containing given pixel
79    * 
80    * @param pCentre
81    * @return range for pCEntre
82    */
83   public contactInterval mapFor(int pCentre)
84   {
85     int pStart = Math.max(pCentre - pixels_step, 0);
86     int pEnd = Math.min(pStart + pixels_step, graphHeight);
87     int cStart = (int) Math.floor(pStart * contacts_per_pixel);
88     contactInterval ci = new contactInterval(cStart,
89             (int) Math.min(contact_height,
90                     Math.ceil(cStart + (pixels_step) * contacts_per_pixel)),
91             pStart, pEnd);
92
93     return ci;
94   }
95
96   public Iterator<contactInterval> iterateOverContactIntervals(
97           int graphHeight)
98   {
99     // NOT YET IMPLEMENTED
100     return null;
101     // int cstart = 0, cend;
102     //
103     // for (int ht = y2,
104     // eht = y2 - graphHeight; ht >= eht; ht -= pixels_step)
105     // {
106     // cstart = (int) Math.floor(((double) y2 - ht) * contacts_per_pixel);
107     // cend = (int) Math.min(contact_height,
108     // Math.ceil(cstart + contacts_per_pixel * pixels_step));
109     //
110     // return new Iterator<contactIntervals>() {
111     //
112     // @Override
113     // public boolean hasNext()
114     // {
115     // // TODO Auto-generated method stub
116     // return false;
117     // }
118     //
119     // @Override
120     // public contactIntervals next()
121     // {
122     // // TODO Auto-generated method stub
123     // return null;
124     // }
125     //
126     // }
127   }
128 }