summaryrefslogtreecommitdiff
path: root/parsecalldata.pde
blob: 5870e6dea175c10c5123509af12f45294469b2ed (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
//BT IRELAND MTM,Destination CID Name,VIA,Calls,MINS,ALOC,% User,ASR,NER 02
boolean DEBUG=true;

class country {
  String name;
  float calls,mins;
  RShape outline;
  country() {
    super();
  }
  country(String n,float c,float m,RShape shp){
    name=n;
    calls=c;
    mins=m;
    outline=shp;
  }
  void addnetwork(float c,float m) {
    calls+=c;
    mins+=m;
  }
  void printOut() {
    println(name+" "+calls+" "+mins);
  }
  RPoint getpoint(int tries){
      RPoint tl=outline.getTopLeft();
      RPoint br=outline.getBottomRight();
      RPoint np= new RPoint(0,0);
      int searching =1;
      while (searching>0&&searching<tries) {
        np=new RPoint(random(tl.x,br.x),random(tl.y,br.y));
        if(outline.contains(np)) searching=0;
        //if (DEBUG) println(name+"; try  "+searching+": "+np.x+","+np.y+" bounds: "+tl.x+","+tl.y+" - "+br.x+","+br.y+": "+(searching>0));
        if (searching==10||searching==100||searching==1000||searching==10000||searching==100000) {
          println(name+"; try  "+searching+": MISS at "+np.x+","+np.y+" bounds: "+tl.x+","+tl.y+" - "+br.x+","+br.y);
        }
      }
      if (searching==0) return np;
      else return new RPoint(0,0);
  }
}

class weightedpixel {
  float bright;
  RPoint pt;
  float index; //index weighted from 0-1
  weightedpixel(RPoint _p,float _b) {
    pt=_p;
    bright=_b;
  }
}

class bitmapcountry extends country {
  //examines a bitmap
  //builds a table of population desnsity
  
  //look at it from the POV of how its gonna be used
  //getpoints(int num) should be able to return an array of points to draw from
  //should be dithered
  //has to distribute the points around the brightest points first
  //distribute in proportion to their brightness - weighted
  
  //table of pixels: if within shape:
  //sample brightness, keep running total and store normalised weighted index for quick searching
  
  float xo,xs,yo,ys;
  Vector<weightedpixel> points = new Vector<weightedpixel>();
  bitmapcountry(String n,float c,float m,RShape shp) {
    super(n,c,m,shp);
  }
  void analyse(float _xo,float _xs,float _yo,float _ys) {
        xo=_xo;
        xs=_xs;
        yo=_yo;
        ys=_ys;
        //scan country outline by pixel
        RPoint tl=outline.getTopLeft();
        RPoint br=outline.getBottomRight();
        RPoint tlp=normpix(tl);
        RPoint brp=normpix(br);
        float pixstep=xs/lightmap.width;
        float totalbright=0;
        float bmin=255;
        float bmax=0;
        println("searching "+tl.x+","+tl.y+" -> "+(tl.x+(pixstep*(brp.x-tlp.x)))+","+(tl.y+(pixstep*(brp.y-tlp.y))));
        for (int i=0;i<brp.x-tlp.x;i++) {
          for (int j=0;j<brp.y-tlp.y;j++) {
            RPoint pn=new RPoint(tl.x+(i*pixstep),tl.y+(j*pixstep));
            if(outline.contains(pn)) {
              float bn=brightness(lightmap.get((int)brp.x+i,(int)brp.y+j));
              points.add(new weightedpixel(normalise(pn),bn));
              totalbright +=bn;
              if (bn>bmax) bmax=bn;
              if (bn<bmin) bmin=bn;
            }
          }
        }
        float runbright=0;
        float bfactor=1.0/totalbright;
        for (int i=0;i<points.size();i++) {
          runbright+=points.get(i).bright*bfactor;
          points.get(i).index=runbright;
        }
        if (DEBUG) println(name+"; bounds: "+tl.x+","+tl.y+" : "+br.x+","+br.y+" - "+points.size()+" pixels, "+bmin+"-"+bmax+" avg: "+(totalbright/points.size()));
        
  }
  RPoint normalise(RPoint p) {
    float px=(((p.x-xo)/xs)-0.5)*PI*2; //26)/736);
    float py=(((p.y-yo)/ys)-0.5)*PI; //90)/390);
        
    return new RPoint(px,py);
  }
  RPoint normpix(RPoint p) {
    float px=((p.x-xo)/xs)*lightmap.width;
    float py=((p.y-yo)/ys)*lightmap.height;
        
    return new RPoint(px,py);
  }
  RPoint getpoint(){
    float index=random(0.999);
    int i=0;
    while (points.get(i).index<index) {
      i++;
    }
    return new RPoint(points.get(i).pt.x+random(1),points.get(i).pt.y+random(1));
  }
}

class calldata {
  
  Vector<bitmapcountry> countries = new Vector<bitmapcountry>();
  HashMap<String, Integer> outlines = new HashMap<String, Integer>();
  calldata(String[][] data,RShape shp) {
    
    for (int i=0;i<shp.children.length;i++) {
      outlines.put(shp.children[i].name.substring(0, 3),i);
    }
 
    println("parsing "+data.length+" entries");
    //detect number of countries
    int num=0;
    if (outlines.containsKey(data[0][1].substring(0, 3))) {
      countries.add(new bitmapcountry(data[0][1],0.0,0.0,shp.children[outlines.get(data[0][1].substring(0, 3))]));
    }
  
    for (int i=0; i < data.length; i++) { 
      if (data[i][1].substring(0,3).equals(countries.lastElement().name.substring(0, 3))) {
        countries.lastElement().addnetwork(Float.valueOf(data[i][3].trim()).floatValue(),Float.valueOf(data[i][4].trim()).floatValue());
      }
      
      else {
        if (outlines.containsKey(data[i][1].substring(0, 3))) {
          countries.add(new bitmapcountry(data[i][1],Float.valueOf(data[i][3].trim()).floatValue(),Float.valueOf(data[i][4].trim()).floatValue(),shp.children[outlines.get(data[i][1].substring(0, 3))]));
        }
      }      
    }
    
     for (int i=0; i < countries.size(); i++) { 
        //if (DEBUG) countries.get(i).printOut();
        countries.get(i).analyse(18.279,746.302,129.314,374.293);
     }
    
  }
  country getcountry(int w) {
    return countries.get(w);
  }
}