Skip to content

Commit

Permalink
Merge pull request #19 from vallettea/master
Browse files Browse the repository at this point in the history
proper postioning and first draft of sunpostition
  • Loading branch information
vallettea committed Aug 20, 2014
2 parents 9d4dec5 + 747b13d commit 6e1914d
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 49 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ node tools/unzipCUB3ds.js --out front/data/ --zip path/to/3Ddata/BATI3D_NT.zip

It should take about 5 minutes in normal hardware. This will extract all the buildings and other 3d objects from the open data in [.3ds format](http://en.wikipedia.org/wiki/.3ds). It will create thousands of binary files in `front/data/` as well as a file names `metadata.json`.

1. `browserify front/src/main.js -o front/app.js -v -d`
1. `browserify front/src/main.js -o front/app.js`

1. Start the server

Expand Down
3 changes: 2 additions & 1 deletion front/src/3dviz.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ scene.add(camera);


var light = new THREE.DirectionalLight(0xffffff, 1);
light.lookAt([0,0,0]);
light.lookAt([10000,24100,0]);
light.castShadow = true;
light.shadowDarkness = 0.6;
light.shadowMapWidth = 2048;
Expand All @@ -58,5 +58,6 @@ scene.add(light);
module.exports = {
scene: scene,
camera: camera,
light: light,
renderer: renderer
};
2 changes: 1 addition & 1 deletion front/src/buildingMap.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict';

var Map = require("harmony-collections").Map;
module.exports = new Map();
22 changes: 6 additions & 16 deletions front/src/controls.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
'use strict';

var THREE = require('three');
var moveCamera = require('./moveCamera.js');

module.exports = function(camera){

// add controls
var keys = { LEFT: 37, UP: 38, RIGHT: 39, BOTTOM: 40, ROTATE: 65, ZOOM: 83, PAN: 68 };
var userPanSpeed = 200.0;
var userPanSpeed = 50.0;
function pan ( distance ) {
var camx = camera.position.x + distance.x*userPanSpeed;
var camy = camera.position.y + distance.y*userPanSpeed;
moveCamera(camx, camy, camz);
moveCamera(camera)(camx, camy, undefined);
};

function onKeyDown( event ) {
Expand All @@ -28,19 +31,6 @@ module.exports = function(camera){
}
}

function onKeyUp( event ) {

switch ( event.keyCode ) {

case keys.ROTATE:
case keys.ZOOM:
case keys.PAN:
state = STATE.NONE;
break;
}

}
window.addEventListener( 'keydown', onKeyDown, false );
window.addEventListener( 'keyup', onKeyUp, false );
window.addEventListener( 'keydown', onKeyDown );
}

2 changes: 2 additions & 0 deletions front/src/createBuildingMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ module.exports = function createBuildingMesh(buffer, X, Y) {
}));

mesh.position.set(X*200, (MAXY - Y)*200, 0);
mesh.castShadow = true;
mesh.receiveShadow = true;

return mesh;
}
90 changes: 90 additions & 0 deletions front/src/geoConverter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
'use strict';

// http://fr.wikipedia.org/wiki/Projection_conique_conforme_de_Lambert
// http://geodesie.ign.fr/contenu/fichiers/documentation/rgf93/cc9zones.pdf

// stellar parameters
var a = 6378137; //demi grand axe
var f = 1.0/298.257222101; //applatissement
var b = (1-f)*a;
var e = Math.sqrt(Math.pow(a,2)-Math.pow(b,2))/a;
var pi = Math.PI;
var lon0 = 3*pi/180;



module.exports = function(CC, deltaX, deltaY){
// CC is the number of the lamber projection
// deltaX, deltaY are the shifts from the original center
var NZ = CC - 41;

// lambert 94 parameters
var phi0 = (41.0 + NZ);
var phi1 = (phi0 - 0.75);
var phi2 = (phi0 + 0.75);
phi0 = phi0*pi/180;
phi1 = phi1*pi/180;
phi2 = phi2*pi/180;
var E0 = 1700000;
var N0 = (NZ * 1000000) + 200000;


function L(phi) {
return 0.5*Math.log( (1+Math.sin(phi))/(1-Math.sin(phi)) )
- e/2*Math.log( (1+e*Math.sin(phi))/(1-e*Math.sin(phi)) );
}

var n = Math.log( Math.cos(phi2)/Math.cos(phi1) * Math.sqrt(1- Math.pow(e*Math.sin(phi1),2)) /
Math.sqrt(1- Math.pow(e*Math.sin(phi2),2)) ) / (L(phi1) - L(phi2));

var C = a*Math.cos(phi1) * Math.exp(n*L(phi1)) / (n*Math.sqrt(1-Math.pow(e*Math.sin(phi1),2)));

return {

toLambert: function(lon, lat) {

lon = lon * pi/180;
lat = lat * pi/180;

var YS = N0 + C * Math.exp( -n * L(phi0) );

var R = C * Math.exp( -n * L(lat) );

var gamma = n * (lon - lon0);

var X = E0 + R * Math.sin(gamma);
var Y = YS - R * Math.cos(gamma);

return {X : X - deltaX, Y : Y - deltaY};

},

// this function takes X, Y in a lamber cc projection and outputs lon,lat in rgf93 fomat which we assume to be equal to WGS94 (GPS)
toLonLat: function(X, Y) {


var dX = X + deltaX - E0;
var dY = Y + deltaY - N0 - C * Math.exp(-n * L(phi0));

var R = Math.sqrt(dX * dX + dY * dY );

var gamma = Math.atan(-dX / dY)

var lon = gamma/n + lon0;

// dichotomy
var p0 = 2 * Math.atan( Math.pow(C/R, 1/n) ) - pi/2;
var p1 = 2 * Math.atan( Math.pow(C/R, 1/n) * Math.pow((1+e*Math.sin(p0))/(1-e*Math.sin(p0)), e/2) ) - pi/2;
var delta = Math.abs( p1 - p0 );
while( delta > 0.000001 ){
p0 = p1;
p1 = 2 * Math.atan( Math.pow(R/C, 1/n) * Math.pow((1+e*Math.sin(p0))/(1-e*Math.sin(p0)), e/2) ) - pi/2;
delta = Math.abs( p1 - p0 );
}

return {lon:lon*180/pi, lat:p1*180/pi};
}
}

}

7 changes: 4 additions & 3 deletions front/src/gui.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ var dat = require('dat-gui');
var guiControls = {
address : "Place Peyberland, Bordeaux",
altitude : 500,
hour : 14,
winter : false
hour : 14
};
var gui = new dat.GUI();
var addressControler = gui.add(guiControls, 'address');
var altitudeControler = gui.add(guiControls, 'altitude',100,3000);
var hourControler = gui.add(guiControls, 'hour',0, 24);

module.exports = {
guiControls: guiControls,
addressControler: addressControler,
altitudeControler: altitudeControler
altitudeControler: altitudeControler,
hourControler: hourControler
};
2 changes: 0 additions & 2 deletions front/src/loadTiles.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ module.exports = function loadTiles(south, north, east, west) {
// query the rtree to know what building are needed
var results = rTree.search([west, south, east, north]);

console.log("query results", results);

//remove all buildings from scene
buildingMap.forEach(function(building){
building.visible = false;
Expand Down
46 changes: 31 additions & 15 deletions front/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ var loadTiles = require('./loadTiles.js');
var _3dviz = require('./3dviz.js');
var scene = _3dviz.scene;
var camera = _3dviz.camera;
var light = _3dviz.light;
var renderer = _3dviz.renderer;

var controls = require('./controls.js')(camera);
var moveCamera = require('./moveCamera.js')(camera, function(camera){
// visible bounding box
var moveCamera = require('./moveCamera.js')(camera, function(camera){// visible bounding box
var L = 2 * camera.position.z * Math.tan(3.14*camera.fov/(2*180));
var l = L * WIDTH / HEIGHT;
// console.log(camera.position.x,camera.position.z);
Expand All @@ -29,8 +29,10 @@ var moveCamera = require('./moveCamera.js')(camera, function(camera){
loadTiles(south, north, east, west);
});

var MAX_Y = require('./MAX_Y');
var MAX_Y = require('./MAX_Y.js');

var GeoConverter = require('./geoConverter.js');
var SunCalc = require('suncalc');

// TODO change values on resize
var WIDTH = window.innerWidth,
Expand All @@ -45,12 +47,14 @@ window.addEventListener('resize', function() {
camera.updateProjectionMatrix();
});

function invLinX(x) {
return (x + 0.575803)*(123*200-125*200)/(-0.575803+0.570726) + 123*200;
}
function invLinY(y) {
return (y - 44.839642)*((MAX_Y - 112)*200 - (MAX_Y - 113)*200)/(44.841441 - 44.839642) + (MAX_Y - 113)*200;
}
// initialise the geoconverter that will pass from a shifted lambert cc 45 to lon, lat and reverse
// the map is shifted
// -0.583232, 44.839270 corresponds to 1416800.1046884255, 4188402.562212417 in lambert 45
// and to (X=119) * 200 + (x=100), (MAX_Y-(Y=115))*200 + (y=100) in the map
var deltaX = 1416800.1046884255 - 119*200 - 100;
var deltaY = 4188402.562212417 - (MAX_Y-115)*200 - 100;
var geoConverter = new GeoConverter(45, deltaX, deltaY);


serverCommunication.metadataP.then(function(metadata) {

Expand All @@ -62,16 +66,15 @@ serverCommunication.metadataP.then(function(metadata) {
rTree.insert(item);
});

geoCode("peyberland bordeaux").then(function(coords) {
console.log("moving to", invLinX(coords.lon), invLinY(coords.lat), 300);
moveCamera(invLinX(coords.lon), invLinY(coords.lat), 300);
})
geoCode(guiControls.address).then(function(coords) {
var newPosition = geoConverter.toLambert(coords.lon, coords.lat);
moveCamera(newPosition.X, newPosition.Y, 300); })
});

gui.addressControler.onFinishChange(function(value) {
geoCode(value).then(function(coords) {
console.log("moving to", invLinX(coords.lon), invLinY(coords.lat), camz)
moveCamera(invLinX(coords.lon), invLinY(coords.lat), camz);
var newPosition = geoConverter.toLambert(coords.lon, coords.lat);
moveCamera(newPosition.X, newPosition.Y, 300);
})
});

Expand All @@ -81,5 +84,18 @@ gui.altitudeControler.onFinishChange(function(value) {
});


gui.hourControler.onChange(function(value) {
// get today's sunlight times for Bordeaux
var date = new Date();
date.setHours(value);

var sunPos = SunCalc.getPosition(date, -0.573781, 44.840484);

var radius = 30000;
var lightX = radius * Math.cos(sunPos.azimuth);
var lightY = radius * Math.sin(sunPos.azimuth);
var lightZ = radius * Math.tan(sunPos.altitude);
light.position.set(lightX, lightY, lightZ);
});


14 changes: 7 additions & 7 deletions front/src/moveCamera.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ var THREE = require('three');

module.exports = function(camera, cameraMovedCallback){

return function moveCamera(ncamx, ncamy, ncamz) {
if(typeof ncamx === 'number')
camera.position.x = ncamx;
return function moveObject(x, y, z) {
if(typeof x === 'number')
camera.position.x = x;

if(typeof ncamy === 'number')
camera.position.y = ncamy;
if(typeof y === 'number')
camera.position.y = y;

if(typeof ncamz === 'number')
camera.position.z = ncamz;
if(typeof z === 'number')
camera.position.z = z;

camera.lookAt(new THREE.Vector3( camera.position.x, camera.position.y, 0 ));
if(cameraMovedCallback)
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@
"author": "",
"license": "MIT",
"dependencies": {
"dat-gui": "~0.5.0",
"es6-map": "^0.1.0",
"es6-promise": "^1.0.0",
"express": "^4.8.3",
"socket.io": "^1.0.6",
"three": "~0.68.0",
"harmony-collections": "~0.3.8",
"socket.io": "^1.0.6",
"socket.io-client": "~1.0.6",
"dat-gui": "~0.5.0"
"suncalc": "^1.5.2",
"three": "~0.68.0"
},
"devDependencies": {
"grunt": "^0.4.5",
Expand Down

0 comments on commit 6e1914d

Please sign in to comment.