export function getCoords(lat, lng, flag) {
  var gamma = (90  - lat) * Math.PI / 180;
  var theta = (180 - lng) * Math.PI / 180;

  var x = 200 * Math.sin(gamma) * Math.cos(theta);
  var y = 200 * Math.cos(gamma);
  var z = 200 * Math.sin(gamma) * Math.sin(theta);

  if (flag) {
    return new THREE.Vector3(x, y, z);
  }

  return {x: x, y: y, z: z};
}

var colorScale = d3.scale.quantile()
  .range(['#fff5eb','#fdd0a2','#fd8d3c','#d94801','#7f2704','#ff0000'])
  .domain([0,10]);

export var getColor = (function () {
  var cache = {};

  return function (value) {
    var color = colorScale(value);

    if (!cache[color]) {
      cache[color] = new THREE.MeshPhongMaterial({color: color});
    }
    return cache[color];
  };
}())

// Reference: http://nisatapps.prio.org/armsglobe/

var origin = new THREE.Vector3(0,0,0);

export function arc(beg, end, detail){

  var distance = beg.distanceTo(end);
  
  var mid = beg.clone().lerp(end, 0.5);
  var midLength = mid.length();

  mid.normalize();
  mid.multiplyScalar(midLength + distance * 0.7);

  var normal = (new THREE.Vector3()).subVectors(beg, end);
  normal.normalize();

  var distanceHalf = distance * 0.5;

  var begAnchor    = beg;
  var midbegAnchor = mid.clone().add(normal.clone().multiplyScalar( distanceHalf));
  var midEndAnchor = mid.clone().add(normal.clone().multiplyScalar(-distanceHalf));
  var endAnchor    = end;

  var splineCurveA = new THREE.CubicBezierCurve3(beg, begAnchor, midbegAnchor, mid);
  var splineCurveB = new THREE.CubicBezierCurve3(mid, midEndAnchor, endAnchor, end);

  var vertexCount = Math.floor(distance * 0.02 + 6 ) * detail;

  var points = splineCurveA.getPoints(vertexCount);
  points = points.splice(0, points.length - 1); // Avoid Duplicate
  points = points.concat(splineCurveB.getPoints(vertexCount));

  var geometry = new THREE.Geometry();
  geometry.vertices = points;

  return geometry;
}






@@ -354,170 +354,330 @@
 %0A%7D%0A%0A
-var colorScale = d3.scale.quantile()%0A  .range(%5B'#fff5eb','#fdd0a2','#fd8d3c','#d94801','#7f2704','#ff0000'%5D)%0A  .domain(%5B0,10%5D);%0A%0Aexport var getColor = (function (
+export function materialsCache(colorScale, func) %7B%0A  var cache = %7B%7D;%0A%0A  return function (value) %7B%0A    var color = colorScale(value);%0A%0A    if (!cache%5Bcolor%5D) %7B%0A      cache%5Bcolor%5D = new THREE.MeshPhongMaterial(%7Bcolor: color, shininess: 80%7D);%0A    %7D%0A    return cache%5Bcolor%5D;%0A  %7D;%0A%7D%0A%0A%0Aexport function lineCache(colorScale, func
 ) %7B%0A
@@ -711,32 +711,38 @@
  function (value
+, size
 ) %7B%0A    var colo
@@ -757,32 +757,55 @@
 rScale(value);%0A%0A
+    size = size %7C%7C 1;%0A%0A
     if (!cache%5Bc
@@ -840,33 +840,33 @@
 = new THREE.
-MeshPhong
+LineBasic
 Material(%7Bco
@@ -875,16 +875,33 @@
 r: color
+, linewidth: size
 %7D);%0A    
@@ -933,19 +933,16 @@
 ;%0A  %7D;%0A%7D
-())
 %0A%0A// Ref
@@ -1250,9 +1250,9 @@
 * 0.
-7
+5
 );%0A%0A
@@ -1813,17 +1813,16 @@
 0.02 + 6
- 
 ) * deta

refactor material cache