Skip to content

Commit

Permalink
Fix #150 Add panorama_roll option
Browse files Browse the repository at this point in the history
  • Loading branch information
mistic100 committed Dec 6, 2017
1 parent c29befc commit f76c869
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 29 deletions.
2 changes: 1 addition & 1 deletion src/js/PSVMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ PSVMarker.prototype._updatePolygon = function() {
this.polygon_rad = this.polygon_rad.map(function(coord) {
return [
PSVUtils.parseAngle(coord[0]),
PSVUtils.bound(PSVUtils.parseAngle(coord[1], -Math.PI), -PSVUtils.HalfPI, PSVUtils.HalfPI)
PSVUtils.parseAngle(coord[1], true)
];
});
}
Expand Down
25 changes: 10 additions & 15 deletions src/js/PSVUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -441,12 +441,12 @@ PSVUtils.parseSpeed = function(speed) {
/**
* @summary Parses an angle value in radians or degrees and returns a normalized value in radians
* @param {string|number} angle - eg: 3.14, 3.14rad, 180deg
* @param {float|boolean} [reference=0] - base value for normalization, false to disable
* @param {boolean} [zeroCenter=false] - normalize between -Pi/2 - Pi/2 instead of 0 - 2*Pi
* @returns {float}
* @throws {PSVError} when the angle cannot be parsed
*/
PSVUtils.parseAngle = function(angle, reference) {
if (typeof angle == 'string') {
PSVUtils.parseAngle = function(angle, zeroCenter) {
if (typeof angle === 'string') {
var match = angle.toLowerCase().trim().match(/^(-?[0-9]+(?:\.[0-9]*)?)(.*)$/);

if (!match) {
Expand All @@ -470,23 +470,18 @@ PSVUtils.parseAngle = function(angle, reference) {
throw new PSVError('unknown angle unit "' + unit + '"');
}
}
}

if (reference !== false) {
if (reference === undefined) {
reference = 0;
else {
angle = value;
}
}

angle = (angle - reference) % PSVUtils.TwoPI;

if (angle < 0) {
angle = PSVUtils.TwoPI + angle;
}
angle = (zeroCenter ? angle + Math.PI : angle) % PSVUtils.TwoPI;

angle += reference;
if (angle < 0) {
angle = PSVUtils.TwoPI + angle;
}

return angle;
return zeroCenter ? PSVUtils.bound(angle - Math.PI, -PSVUtils.HalfPI, PSVUtils.HalfPI) : angle;
};

/**
Expand Down
1 change: 1 addition & 0 deletions src/js/PhotoSphereViewer.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ PhotoSphereViewer.prototype._createSphere = function() {

this.mesh = new THREE.Mesh(geometry, material);
this.mesh.scale.x = -1;
this.mesh.rotation.z = this.config.panorama_roll;

this.scene.add(this.mesh);
};
Expand Down
1 change: 1 addition & 0 deletions src/js/PhotoSphereViewer.defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ PhotoSphereViewer.DEFAULTS = {
default_fov: null,
default_long: 0,
default_lat: 0,
panorama_roll: 0,
longitude_range: null,
latitude_range: null,
move_speed: 1,
Expand Down
12 changes: 6 additions & 6 deletions src/js/PhotoSphereViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,18 @@ function PhotoSphereViewer(options) {
this.config.default_long = PSVUtils.parseAngle(this.config.default_long);

// parse default_lat, is between -PI/2 and PI/2
this.config.default_lat = PSVUtils.parseAngle(this.config.default_lat, -Math.PI);
this.config.default_lat = PSVUtils.bound(this.config.default_lat, -PSVUtils.HalfPI, PSVUtils.HalfPI);
this.config.default_lat = PSVUtils.parseAngle(this.config.default_lat, true);

// parse panorama_roll, is between -PI/2 and PI/2
this.config.panorama_roll = PSVUtils.parseAngle(this.config.panorama_roll, true);

// default anim_lat is default_lat
if (this.config.anim_lat === null) {
this.config.anim_lat = this.config.default_lat;
}
// parse anim_lat, is between -PI/2 and PI/2
else {
this.config.anim_lat = PSVUtils.parseAngle(this.config.anim_lat, -Math.PI);
this.config.anim_lat = PSVUtils.bound(this.config.anim_lat, -PSVUtils.HalfPI, PSVUtils.HalfPI);
this.config.anim_lat = PSVUtils.parseAngle(this.config.anim_lat, true);
}

// parse longitude_range, between 0 and 2*PI
Expand All @@ -190,8 +191,7 @@ function PhotoSphereViewer(options) {
// parse latitude_range, between -PI/2 and PI/2
if (this.config.latitude_range) {
this.config.latitude_range = this.config.latitude_range.map(function(angle) {
angle = PSVUtils.parseAngle(angle, -Math.PI);
return PSVUtils.bound(angle, -PSVUtils.HalfPI, PSVUtils.HalfPI);
return PSVUtils.parseAngle(angle, true);
});
}

Expand Down
6 changes: 3 additions & 3 deletions src/js/PhotoSphereViewer.utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ PhotoSphereViewer.prototype.cleanPosition = function(position) {
}

position.longitude = PSVUtils.parseAngle(position.longitude);
position.latitude = PSVUtils.bound(PSVUtils.parseAngle(position.latitude, -Math.PI), -PSVUtils.HalfPI, PSVUtils.HalfPI);
position.latitude = PSVUtils.parseAngle(position.latitude, true);
};

/**
Expand Down Expand Up @@ -214,8 +214,8 @@ PhotoSphereViewer.prototype.applyRanges = function(position) {
range = PSVUtils.clone(this.config.latitude_range);
offset = THREE.Math.degToRad(this.prop.vFov) / 2;

range[0] = PSVUtils.parseAngle(Math.min(range[0] + offset, range[1]), -Math.PI);
range[1] = PSVUtils.parseAngle(Math.max(range[1] - offset, range[0]), -Math.PI);
range[0] = PSVUtils.parseAngle(Math.min(range[0] + offset, range[1]), true);
range[1] = PSVUtils.parseAngle(Math.max(range[1] - offset, range[0]), true);

if (position.latitude < range[0]) {
position.latitude = range[0];
Expand Down
8 changes: 4 additions & 4 deletions tests/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ describe('PSVUtils::parseAngle', function() {
}
});

it('should normalize angles between -Pi and Pi', function() {
it('should normalize angles between -Pi/2 and Pi/2', function() {
var values = {
'270deg': -Math.PI / 2,
'-4': 2 * Math.PI - 4
'45deg': Math.PI / 4,
'-4': Math.PI / 2
};

for (var pos in values) {
assert.equal(PSVUtils.parseAngle(pos, -Math.PI).toFixed(16), values[pos].toFixed(16), pos);
assert.equal(PSVUtils.parseAngle(pos, true).toFixed(16), values[pos].toFixed(16), pos);
}
});

Expand Down

0 comments on commit f76c869

Please sign in to comment.