-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathGraphConverter.ts
More file actions
186 lines (157 loc) · 5.7 KB
/
GraphConverter.ts
File metadata and controls
186 lines (157 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import { ClusterContract } from '../contracts/ClusterContract';
import { MeshContract } from '../contracts/MeshContract';
import { CoreImageEnt } from '../ents/CoreImageEnt';
import { SpatialImageEnt } from '../ents/SpatialImageEnt';
import { LngLat } from '../interfaces/LngLat';
import { LngLatAlt } from '../interfaces/LngLatAlt';
import { GraphClusterContract } from './GraphContracts';
import {
GraphCoreImageEnt,
GraphGeometry,
GraphSpatialImageEnt,
} from './GraphEnts';
const UNDISTORTION_MARGIN_FACTOR = 3;
const MIN_DEPTH = 5;
const MAX_DEPTH = 200;
export type MeshParameters = {
scale: number;
perspective: boolean;
};
type ProjectionModel = {
cameraType: string;
parameters: number[];
}
export function convertCameraType(cameraType: string, parameters: number[]): ProjectionModel {
switch (cameraType) {
case "equirectangular":
case "spherical":
return {cameraType: "spherical", parameters};
case "perspective":
case "fisheye":
return {cameraType, parameters};
default:
return {cameraType: "perspective", parameters: [0.85, 0, 0]};
}
}
export class GraphConverter {
public clusterReconstruction(
source: GraphClusterContract)
: ClusterContract {
const id: string = null;
const colors: number[] = [];
const coordinates: number[] = [];
const pointIds: string[] = [];
const points = source.points;
const normalizer = 1 / 255;
for (const pointId in points) {
if (!points.hasOwnProperty(pointId)) {
continue;
}
pointIds.push(pointId);
const point = points[pointId];
const color = point.color;
colors.push(color[0] * normalizer);
colors.push(color[1] * normalizer);
colors.push(color[2] * normalizer);
coordinates.push(...point.coordinates);
}
const lla = source.reference_lla;
const reference: LngLatAlt = {
alt: lla.altitude,
lat: lla.latitude,
lng: lla.longitude,
};
return {
colors,
coordinates,
id,
pointIds,
reference,
rotation: [0, 0, 0],
};
}
public coreImage(
source: GraphCoreImageEnt)
: CoreImageEnt {
const geometry = this._geometry(source.geometry);
const computedGeometry = this._geometry(source.computed_geometry);
const sequence = { id: source.sequence };
const id = source.id;
return {
computed_geometry: computedGeometry,
geometry,
id,
sequence,
};
}
/**
* Clamps the depth of the points to the [5, 200] meters interval to avoid
* strange appearance.
*
* @param source Source mesh.
* @param params Parameters.
* @returns Converted mesh.
*/
public mesh(source: MeshContract, params?: MeshParameters): MeshContract {
const { vertices } = source;
const scale = params && params.scale != null ? params.scale : 1;
const scaleInv = 1 / scale;
const perspective = params ? params.perspective : true;
const zMin = scale * MIN_DEPTH;
const zMax = scale * MAX_DEPTH;
const numVertices = vertices.length / 3;
for (let i = 0; i < numVertices; ++i) {
const index = 3 * i;
let x = vertices[index + 0];
let y = vertices[index + 1];
let z = vertices[index + 2];
if (perspective) {
// Workaround for corner points not being undistorted
// during processing for perspective cameras.
if (i < 4) {
x *= UNDISTORTION_MARGIN_FACTOR;
y *= UNDISTORTION_MARGIN_FACTOR;
}
const zBounded = Math.max(zMin, Math.min(z, zMax));
const factor = zBounded / z;
vertices[index + 0] = factor * x * scaleInv;
vertices[index + 1] = factor * y * scaleInv;
vertices[index + 2] = zBounded * scaleInv;
} else {
const l = Math.sqrt(x * x + y * y + z * z);
const lBounded = Math.max(zMin, Math.min(l, zMax));
const factor = lBounded / l;
vertices[index + 0] = factor * x * scaleInv;
vertices[index + 1] = factor * y * scaleInv;
vertices[index + 2] = factor * z * scaleInv;
}
}
return source;
}
public spatialImage(
source: GraphSpatialImageEnt)
: SpatialImageEnt {
const {cameraType, parameters} = convertCameraType(source.camera_type, source.camera_parameters);
source.camera_type = cameraType;
source.camera_parameters = parameters;
source.merge_id = source.merge_cc ? source.merge_cc.toString() : null;
source.private = null;
const thumbUrl = source.camera_type === 'spherical' ?
source.thumb_2048_url : source.thumb_1024_url;
source.thumb = source.thumb ?? { id: null, url: thumbUrl };
source.cluster = source.sfm_cluster ?? { id: null, url: null };
source.creator = source.creator ?? { id: null, username: null };
source.owner = source.organization ?? { id: null };
source.mesh = source.mesh ?? { id: null, url: null };
return source;
}
private _geometry(geometry: GraphGeometry): LngLat {
const coords = geometry?.coordinates;
const lngLat: LngLat = coords ?
{
lat: coords[1],
lng: coords[0],
} : null;
return lngLat;
}
}