-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathallBuildingsSTL.js
More file actions
167 lines (146 loc) · 5.07 KB
/
allBuildingsSTL.js
File metadata and controls
167 lines (146 loc) · 5.07 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
var fs = require('fs'),
stl = require('stl'),
poly2tri = require('poly2tri');
module.exports.allBuildingsSTL = allBuildingsSTL;
//Lat/Long and Coordinate Stuff
Number.prototype.toRadians = function () {
return this * Math.PI / 180;
};
Number.prototype.toDegrees = function () {
return this * 180 / Math.PI;
};
//Create Lat/Lng object
function latLon(lat, lng) {
this.latitude = Number(lat);
this.longitude = Number(lng);
}
//Latitude and Longitude to X,Y Coord
latLon.prototype.coordinatesTo = function (point) {
var radius = 6371;
var phi1 = this.latitude.toRadians(),
lambda1 = this.longitude.toRadians(),
phi2 = point.latitude.toRadians(),
lambda2 = point.longitude.toRadians();
var deltaPhi = phi2 - phi1,
deltaLambda = lambda2 - lambda1;
var a = Math.pow(Math.sin(deltaPhi / 2), 2) + Math.cos(phi1) * Math.cos(phi2) * Math.pow(Math.sin(deltaLambda / 2), 2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
var d = radius * c * 1000;
var x = Math.cos(phi1) * Math.sin(phi2) - Math.sin(phi1) * Math.cos(phi2) * Math.cos(deltaLambda),
y = Math.sin(deltaLambda) * Math.cos(phi2);
var theta = Math.atan2(y, x);
var X = d * Math.sin(theta),
Y = d * Math.cos(theta);
return [X, Y];
}
//Coordinates from Basic Lat/lng point to Lat/Lng
latLon.prototype.destinationPoint = function (X, Y) {
var radius = 6371;
var brng = Math.atan(X / Y),
dist = Math.sqrt(Math.pow(X, 2) + Math.pow(Y, 2));
var theta = Number(brng).toRadians();
var delta = Number(dist) / radius; // angular distance in radians
var phi1 = this.latitude.toRadians();
var lambda1 = this.longitude.toRadians();
var phi2 = Math.asin(Math.sin(phi1) * Math.cos(delta) +
Math.cos(phi1) * Math.sin(delta) * Math.cos(theta));
var lambda2 = lambda1 + Math.atan2(Math.sin(theta) * Math.sin(delta) * Math.cos(phi1),
Math.cos(delta) - Math.sin(phi1) * Math.sin(phi2));
lambda2 = (lambda2 + 3 * Math.PI) % (2 * Math.PI) - Math.PI; // normalise to -180..+180º
return new latLon(phi2.toDegrees(), lambda2.toDegrees());
}
//Create STL format
function createFacet(verts) {
return {
verts: verts
}
}
function createPlane(p1, p2, h) {
var tri1 = [[p1[0], p1[1], 0], [p2[0], p2[1], 0], [p2[0], p2[1], h]];
var tri2 = [[p1[0], p1[1], 0], [p2[0], p2[1], h], [p1[0], p1[1], h]];
var facets = [{
verts: tri1
}, {
verts: tri2
}];
return facets;
}
function createSTL(points, height, buildingName) {
//Add facets
var facets = [];
//Walls
for (var i = 1; i < points.length; i++) {
var tri = createPlane(points[i - 1], points[i], height);
facets.push(tri[0]);
facets.push(tri[1]);
}
var tri = createPlane(points[points.length - 1], points[0], height);
facets.push(tri[0]);
facets.push(tri[1]);
//Top and Bottom
var contour = [];
points.forEach(function (point) {
contour.push(new poly2tri.Point(point[0], point[1]));
});
var swctx = new poly2tri.SweepContext(contour);
swctx.triangulate();
var triangles = swctx.getTriangles();
//Create Bottom Plane
triangles.forEach(function (tri) {
var verts = [];
tri.points_.reverse();
tri.points_.forEach(function (points) {
verts.push([points.x, points.y, 0]);
});
facets.push(createFacet(verts));
});
//Create Top Plane
triangles.forEach(function (tri) {
var verts = [];
tri.points_.reverse();
tri.points_.forEach(function (points) {
verts.push([points.x, points.y, height]);
});
facets.push(createFacet(verts));
});
var stlObj = {
description: buildingName,
facets: facets
};
var buildingSTL = stl.fromObject(stlObj);
return buildingSTL;
//fs.writeFileSync("stlFiles/" + buildingName + '.stl', stl.fromObject(stlObj));
}
//All Buildings STL function for Export
function allBuildingsSTL(buildings) {
//Average Lat and Long
var lat = 0,
lng = 0,
count = 0;
buildings.forEach(function (building) {
building.path.forEach(function (point) {
lat += point.latitude;
lng += point.longitude;
count++;
});
});
var centerLat = lat / count,
centerLng = lng / count,
buildingsSTL = '';
var origin = new latLon(centerLat, centerLng);
buildings.forEach(function (building) {
var coords = [];
for (var i = 0; i < building.path.length; i++) {
var point = new latLon(building.path[i].latitude, building.path[i].longitude);
coords.push(origin.coordinatesTo(point))
};
building.coords = coords;
});
var fileName = new String;
buildings.forEach(function (building) {
var buildingSTL = createSTL(building.coords, building.buildingHeight, building.name, buildingsSTL);
buildingsSTL += '\n' + buildingSTL;
fileName += building.name + '_'
});
fs.writeFileSync("stlFiles/" + fileName + ".stl", buildingsSTL);
}