Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 83 additions & 18 deletions packages/maptalks/src/core/util/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,77 @@ export function getMinMaxAltitude(altitude: number | number[] | number[][]): [nu
return [min, max];
}

export function pointsToCoordinates(map, points: Point[], glRes: number, altitude: number): Coordinate[] {
const ring = [];
for (let i = 0, len = points.length; i < len; i++) {
const pt = points[i];
const c = map.pointAtResToCoordinate(pt, glRes);
c.z = altitude;
ring[i] = c;
}
// ring.push(ring[0].copy());
return ring;
}

const WORLD_CENTER = new Coordinate(0, 0);

export function getEllipseGLSize(center: Coordinate, measurer, map, halfWidth: number, halfHeight: number) {
const glRes = map.getGLRes();
const c1 = measurer.locate(WORLD_CENTER, 1, 0);
// const c2 = measurer.locate(CENTER, 0, halfHeight);
const glCenter = map.coordToPointAtRes(center, glRes);
const p0 = map.coordToPointAtRes(WORLD_CENTER, glRes);
const p1 = map.coordToPointAtRes(c1, glRes);
// const p2 = map.coordToPointAtRes(c2, glRes);
const glWidth = p0.distanceTo(p1) * halfWidth;
const glHeight = glWidth * halfHeight / halfWidth;

return {
glWidth,
glHeight,
glCenter
}
}

export function getIgnoreProjectionGeometryCenter(geo) {
const ignoreProjection = geo.options.ignoreProjection;
if (ignoreProjection) {
const ring = geo.getShell ? geo.getShell() : null;
if (ring && ring.length) {
const map = geo.getMap();
if (!map) {
return;
}
const glRes = map.getGLRes();
const points = ring.map(c => {
return map.coordToPointAtRes(c, glRes);
})

let sumx = 0,
sumy = 0,
counter = 0;
const size = points.length;
for (let i = 0; i < size; i++) {
if (points[i]) {
if (isNumber(points[i].x) && isNumber(points[i].y)) {
sumx += points[i].x;
sumy += points[i].y;
counter++;
}
}
}
const p = new Point(sumx / counter, sumy / counter);
return map.pointAtResToCoordinate(p, glRes);
}
}
}

/**
* point left segment
* @param p
* @param p1
* @param p2
* @returns
* @param p
* @param p1
* @param p2
* @returns
*/
export function pointLeftSegment(p: Point, p1: Point, p2: Point) {
const x1 = p1.x, y1 = p1.y;
Expand All @@ -320,7 +385,7 @@ function pointRightSegment(p: Point, p1: Point, p2: Point) {
}

/**
*
*
* LT--------------------RT
* \ /
* \ /
Expand All @@ -329,12 +394,12 @@ function pointRightSegment(p: Point, p1: Point, p2: Point) {
* camera behind
*
* Points within a convex quadrilateral
* @param point
* @param p1
* @param p2
* @param p3
* @param p4
* @returns
* @param point
* @param p1
* @param p2
* @param p3
* @param p4
* @returns
*/
export function pointInQuadrilateral(p: Point, LT: Point, RT: Point, RB: Point, LB: Point) {
//LT-RT
Expand All @@ -352,11 +417,11 @@ export function pointInQuadrilateral(p: Point, LT: Point, RT: Point, RB: Point,
/**
* 直线和直线的交点
* Intersection of two line
* @param p1
* @param p2
* @param p3
* @param p4
* @returns
* @param p1
* @param p2
* @param p3
* @param p4
* @returns
*/
export function lineIntersection(p1: Point, p2: Point, p3: Point, p4: Point): Point | null {
const dx1 = p2.x - p1.x, dy1 = p2.y - p1.y;
Expand Down Expand Up @@ -404,8 +469,8 @@ export function lineIntersection(p1: Point, p2: Point, p3: Point, p4: Point): Po

/**
* Does it contain altitude values
* @param altitudes
* @returns
* @param altitudes
* @returns
*/
export function altitudesHasData(altitudes: number | Array<number>) {
if (isNumber(altitudes)) {
Expand Down
201 changes: 101 additions & 100 deletions packages/maptalks/src/geometry/CenterMixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,112 +12,113 @@ import type { Map } from '../map';
* @mixin CenterMixin
*/
export default function <T extends MixinConstructor>(Base: T) {
return class extends Base {
//@internal
_coordinates: Coordinate
//@internal
_pcenter: Coordinate
//@internal
_dirtyCoords: boolean
getMap?(): Map
//@internal
_getProjection?(): CommonProjectionType
onPositionChanged?(): void
//@internal
_verifyProjection?(): void
//@internal
_clearCache?(): void
//@internal
_translateRotatePivot?(coordinate: Coordinate): this;
/**
* 获取几何图形的中心点
* @english
* Get geometry's center
* @return {Coordinate} - center of the geometry
* @function CenterMixin.getCoordinates
*/
getCoordinates(): Coordinate {
return this._coordinates;
}
return class extends Base {
//@internal
_coordinates: Coordinate
//@internal
_pcenter: Coordinate
//@internal
_dirtyCoords: boolean
getMap?(): Map
//@internal
_getProjection?(): CommonProjectionType
onPositionChanged?(): void
//@internal
_verifyProjection?(): void
//@internal
_clearCache?(): void
//@internal
_translateRotatePivot?(coordinate: Coordinate): this;
getShell?();
/**
* 获取几何图形的中心点
* @english
* Get geometry's center
* @return {Coordinate} - center of the geometry
* @function CenterMixin.getCoordinates
*/
getCoordinates(): Coordinate {
return this._coordinates;
}

/**
* 设置几何图形的中心点
* @english
* Set a new center to the geometry
* @param {Coordinate|Number[]} coordinates - new center
* @return {Geometry} this
* @fires Geometry#positionchange
* @function CenterMixin.setCoordinates
*/
setCoordinates(coordinates: Coordinate | Array<number>) {
const center = (coordinates instanceof Coordinate) ? coordinates : new Coordinate(coordinates as [number, number, number]);
this._translateRotatePivot(center);
this._coordinates = center;
if (!this.getMap()) {
//When not on a layer or when creating a new one, temporarily save the coordinates,
this._dirtyCoords = true;
this.onPositionChanged();
return this;
}
const projection = this._getProjection();
this._setPrjCoordinates(projection.project(this._coordinates));
return this;
}
/**
* 设置几何图形的中心点
* @english
* Set a new center to the geometry
* @param {Coordinate|Number[]} coordinates - new center
* @return {Geometry} this
* @fires Geometry#positionchange
* @function CenterMixin.setCoordinates
*/
setCoordinates(coordinates: Coordinate | Array<number>) {
const center = (coordinates instanceof Coordinate) ? coordinates : new Coordinate(coordinates as [number, number, number]);
this._translateRotatePivot(center);
this._coordinates = center;
if (!this.getMap()) {
//When not on a layer or when creating a new one, temporarily save the coordinates,
this._dirtyCoords = true;
this.onPositionChanged();
return this;
}
const projection = this._getProjection();
this._setPrjCoordinates(projection.project(this._coordinates));
return this;
}

//Gets view point of the geometry's center
//@internal
_getCenter2DPoint(res?: number): Point {
const map = this.getMap();
if (!map) {
return null;
}
const pcenter = this._getPrjCoordinates();
if (!pcenter) { return null; }
if (!res) {
res = map._getResolution();
}
return map._prjToPointAtRes(pcenter, res);
}
//Gets view point of the geometry's center
//@internal
_getCenter2DPoint(res?: number): Point {
const map = this.getMap();
if (!map) {
return null;
}
const pcenter = this._getPrjCoordinates();
if (!pcenter) { return null; }
if (!res) {
res = map._getResolution();
}
return map._prjToPointAtRes(pcenter, res);
}

//@internal
_getPrjCoordinates(): Coordinate {
const projection = this._getProjection();
this._verifyProjection();
if (!this._pcenter && projection) {
if (this._coordinates) {
this._pcenter = projection.project(this._coordinates);
}
}
return this._pcenter;
//@internal
_getPrjCoordinates(): Coordinate {
const projection = this._getProjection();
this._verifyProjection();
if (!this._pcenter && projection) {
if (this._coordinates) {
this._pcenter = projection.project(this._coordinates);
}
}
return this._pcenter;
}

//Set center by projected coordinates
//@internal
_setPrjCoordinates(pcenter: Coordinate): void {
this._pcenter = pcenter;
this.onPositionChanged();
}
//Set center by projected coordinates
//@internal
_setPrjCoordinates(pcenter: Coordinate): void {
this._pcenter = pcenter;
this.onPositionChanged();
}

//update cached const iables if geometry is updated.
//@internal
_updateCache(): void {
this._clearCache();
const projection = this._getProjection();
if (this._pcenter && projection) {
this._coordinates = projection.unproject(this._pcenter);
}
}
//update cached const iables if geometry is updated.
//@internal
_updateCache(): void {
this._clearCache();
const projection = this._getProjection();
if (this._pcenter && projection) {
this._coordinates = projection.unproject(this._pcenter);
}
}

//@internal
_clearProjection(): void {
this._pcenter = null;
// @ts-expect-error todo
super._clearProjection();
}
//@internal
_clearProjection(): void {
this._pcenter = null;
// @ts-expect-error todo
super._clearProjection();
}

//@internal
_computeCenter(): Coordinate | null {
return this._coordinates ? this._coordinates.copy() : null;
}
};
//@internal
_computeCenter(): Coordinate | null {
return this._coordinates ? this._coordinates.copy() : null;
}
};
}
Loading