First state of a geom package.

This commit is contained in:
Marc Jansen
2012-06-19 18:21:21 +02:00
parent 712070c1f0
commit 450f8f7b74
10 changed files with 384 additions and 1 deletions

12
src/api/geom/geometry.js Normal file
View File

@@ -0,0 +1,12 @@
goog.provide('ol.geom.geometry');
goog.require('ol.geom.Geometry');
/**
* @export
* @return {ol.geom.Geometry} Geometry..
*/
ol.geom.geometry = function(){
var g = new ol.geom.Geometry();
return g;
};

43
src/api/geom/point.js Normal file
View File

@@ -0,0 +1,43 @@
goog.provide('ol.geom.point');
goog.require('ol.geom.Point');
/**
* @typedef {ol.PointLike|Array.<number>|Object} point Point.
*/
ol.PointLike;
/**
* @export
* @param {ol.PointLike} opt_arg Point.
* @return {ol.geom.Point} Point.
*/
ol.geom.point = function(opt_arg){
if (opt_arg instanceof ol.geom.Point) {
return opt_arg;
}
var x = 0;
var y = 0;
var z;
if (arguments.length == 1 && goog.isDef(opt_arg)) {
if (goog.isArray(opt_arg)) {
x = opt_arg[0];
y = opt_arg[1];
z = opt_arg[2];
} else if (goog.isObject(opt_arg)) {
x = opt_arg.x;
y = opt_arg.y;
z = opt_arg.z;
} else {
throw new Error('ol.geom.point');
}
}
var p = new ol.geom.Point(x,y,z);
return p;
};
goog.inherits(ol.geom.point, ol.geom.geometry);

View File

@@ -3,6 +3,7 @@ goog.require('ol.bounds');
goog.require("ol.map");
goog.require("ol.loc");
goog.require("ol.projection");
goog.require("ol.Tile");
goog.require("ol.TileSet");
goog.require("ol.geom.geometry");
goog.require("ol.geom.point");

52
src/ol/geom/Geometry.js Normal file
View File

@@ -0,0 +1,52 @@
goog.provide('ol.geom.Geometry');
goog.require('ol.Bounds');
goog.require('ol.mixins.coordinate');
/**
* Creates ol.Geometry objects.
*
* @constructor
*/
ol.geom.Geometry = function() {
/**
* @private
* @type {ol.Bounds|undefined}
*/
this.bounds_ = undefined;
};
/**
* @return {ol.Bounds|undefined} The ol.Bounds.
*/
ol.geom.Geometry.prototype.getBounds = function() {
return this.bounds_;
};
/**
* @param {ol.Bounds} bounds The new ol.Bounds.
* @return {ol.geom.Geometry} This.
*/
ol.geom.Geometry.prototype.setBounds = function(bounds) {
this.bounds_ = bounds;
return this;
};
/**
* @export
* @param {ol.Bounds=} opt_arg new Bounds.
* @return {ol.geom.Geometry|ol.Bounds|undefined} either a Geometry (when used as
* setter) or a Bounds/undefined (if used as getter).
*/
ol.geom.Geometry.prototype.bounds = function(opt_arg){
if (arguments.length == 1 && goog.isDef(opt_arg)) {
return this.setBounds(opt_arg);
} else {
return this.getBounds();
}
};

65
src/ol/geom/Point.js Normal file
View File

@@ -0,0 +1,65 @@
goog.provide('ol.geom.Point');
goog.require('ol.geom.Geometry');
goog.require('ol.mixins.coordinate');
goog.require('ol.interfaces.coordinateAccessor');
/**
* Creates ol.geom.Point objects.
*
* @extends {ol.geom.Geometry}
* @param {number} x X.
* @param {number} y Y.
* @param {number=} opt_z Z.
*
* @implements {ol.interfaces.coordinateAccessor}
*
* @constructor
*/
ol.geom.Point = function(x, y, opt_z) {
/**
* @private
* @type {number}
*/
this.x_ = x;
/**
* @private
* @type {number}
*/
this.y_ = y;
/**
* @private
* @type {number|undefined}
*/
this.z_ = opt_z;
};
goog.inherits(ol.geom.Point, ol.geom.Geometry);
goog.mixin(ol.geom.Point.prototype, ol.mixins.coordinate);
///**
// * @override
// */
//ol.geom.Point.prototype.getX = ol.geom.Point.prototype.getX;
///**
// * @override
// */
//ol.geom.Point.prototype.setX = ol.geom.Point.prototype.setX;
///**
// * @override
// */
//ol.geom.Point.prototype.getY = ol.geom.Point.prototype.getY;
///**
// * @override
// */
//ol.geom.Point.prototype.setY = ol.geom.Point.prototype.setY;
///**
// * @override
// */
//ol.geom.Point.prototype.getZ = ol.geom.Point.prototype.getZ;
///**
// * @override
// */
//ol.geom.Point.prototype.setZ = ol.geom.Point.prototype.setZ;

View File

@@ -0,0 +1,48 @@
goog.provide('ol.interfaces.coordinateAccessor');
/**
* The coordinateAccessor interface
*
* @lends {ol.geom.Point#}
* @interface
*
* @param {number} x X.
* @param {number} y Y.
* @param {number=} opt_z Z.
*/
ol.interfaces.coordinateAccessor = function(x, y, opt_z){
};
/**
* @return {number} X.
*/
ol.interfaces.coordinateAccessor.prototype.getX = function(){};
/**
* @return {number} Y.
*/
ol.interfaces.coordinateAccessor.prototype.getY = function(){};
/**
* @return {number|undefined} Z.
*/
ol.interfaces.coordinateAccessor.prototype.getZ = function(){};
/**
* @param {number} x X.
* @return {Object} This.
*/
ol.interfaces.coordinateAccessor.prototype.setX = function(x){};
/**
* @param {number} y Y.
* @return {Object} This.
*/
ol.interfaces.coordinateAccessor.prototype.setY = function(y){};
/**
* @param {number|undefined} z Z.
* @return {Object} This.
*/
ol.interfaces.coordinateAccessor.prototype.setZ = function(z){};

View File

@@ -0,0 +1,31 @@
goog.provide('ol.mixins.coordinate');
goog.require('goog.object');
goog.object.extend(ol.mixins.coordinate, {
getX : function() {
return this.x_;
},
getY : function() {
return this.y_;
},
getZ : function() {
return this.z_;
},
setX : function(x) {
this.x_ = x;
return this;
},
setY : function(y) {
this.y_ = y;
return this;
},
setZ: function(z) {
this.z_ = z;
return this;
}
});

View File

@@ -18,6 +18,8 @@
<script type="text/javascript" src="spec/ol/Projection.test.js"></script>
<script type="text/javascript" src="spec/ol/Tile.test.js"></script>
<script type="text/javascript" src="spec/ol/TileSet.test.js"></script>
<script type="text/javascript" src="spec/ol/geom/Geometry.test.js"></script>
<script type="text/javascript" src="spec/ol/geom/Point.test.js"></script>
<script type="text/javascript">
(function() {

View File

@@ -0,0 +1,56 @@
describe("ol.geom.Geometry", function() {
var g;
beforeEach(function() {
g = ol.geom.geometry();
});
afterEach(function() {
g = null;
});
it("constructs instances", function() {
expect(g).toEqual(jasmine.any(ol.geom.Geometry));
console.log(g);
console.log(g.foo);
console.log(g.foo());
});
it("can set bounds", function() {
var oldBounds = g.bounds();
expect(oldBounds).toBeUndefined();
var b = ol.bounds([0,1,2,3]);
g.bounds(b);
var gotBounds = g.bounds();
expect(gotBounds).not.toBeUndefined();
});
it("can get bounds", function() {
var b = ol.bounds([0,1,2,3]);
g.bounds(b);
var gotBounds = g.bounds();
expect(gotBounds).toEqual(jasmine.any(ol.Bounds));
});
it("sets and gets the correct bounds", function() {
var b = ol.bounds([0,1,2,3]);
g.bounds(b);
var gotBounds = g.bounds();
expect(gotBounds.getMinX()).toEqual(0);
expect(gotBounds.getMinY()).toEqual(1);
expect(gotBounds.getMaxX()).toEqual(2);
expect(gotBounds.getMaxY()).toEqual(3);
});
//
});

View File

@@ -0,0 +1,73 @@
describe("ol.geom.Point", function() {
var pNoArgs,
pNoZ,
p;
var instances = {
"no arguments passed": ol.geom.point(),
"only two arguments [x,y] passed": ol.geom.point([21, 4]),
"all arguments passed": ol.geom.point([21, 4, 8])
};
beforeEach(function() {
instances = {
"no arguments passed": ol.geom.point(),
"only two arguments [x,y] passed": ol.geom.point([21, 4]),
"all arguments passed": ol.geom.point([21, 4, 8])
};
pNoArgs = instances['no arguments passed'];
pNoZ = instances['only two arguments [x,y] passed'];
p = instances['all arguments passed'];
});
afterEach(function() {
pNoArgs = pNoZ = p = null;
instances = {
"no arguments passed": pNoArgs,
"only two arguments [x,y] passed": pNoZ,
"all arguments passed": p
};
});
for (instancesDesc in instances) {
if (instances.hasOwnProperty(instancesDesc)) {
var instance = instances[instancesDesc];
it("constructs instances (" + instancesDesc + ")", function() {
expect(instance).toEqual(jasmine.any(ol.geom.Point));
});
it("constructs instances of ol.geom.Geometry (" + instancesDesc + ")", function() {
expect(instance).toEqual(jasmine.any(ol.geom.Geometry));
});
it("has the coordinate mixin methods (" + instancesDesc + ")", function() {
expect(instance.getX).not.toBeUndefined();
expect(instance.getY).not.toBeUndefined();
expect(instance.getZ).not.toBeUndefined();
expect(instance.setX).not.toBeUndefined();
expect(instance.setY).not.toBeUndefined();
expect(instance.setZ).not.toBeUndefined();
});
}
}
it("has functional getters (no arguments passed)", function(){
expect(pNoArgs.getX()).toBe(0);
expect(pNoArgs.getY()).toBe(0);
expect(pNoArgs.getZ()).toBeUndefined();
});
it("has functional getters (only two arguments [x,y] passed)", function(){
expect(pNoZ.getX()).toBe(21);
expect(pNoZ.getY()).toBe(4);
expect(pNoZ.getZ()).toBeUndefined();
});
it("has functional getters (all arguments passed)", function(){
expect(p.getX()).toBe(21);
expect(p.getY()).toBe(4);
expect(p.getZ()).toBe(8);
});
});