-
Notifications
You must be signed in to change notification settings - Fork 39
/
terrain2stl.js
218 lines (183 loc) · 6.62 KB
/
terrain2stl.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var map;
var gridlines;
var rectangle;
var latBox;
var lngBox;
var mapCenter;
var minBoxWidth = 0.03333; //width of box in degrees = 40 arc seconds
var boxWidth = 0.03333;
var boxHeight = 0.03333;
var boxRotation = 0;
var sizeSlider;
var sizeLabel;
var scaleSlider;
var scaleLabel;
var rotationSlider;
var rotationLabel;
var vScaleSlider;
var vScaleLabel;
var waterDropSlider;
var waterDropLabel;
var baseHeightSlider;
var baseHeightLabel;
function initializeMap(){
mapCenter = new google.maps.LatLng(44.191442, -69.074608);
var mapOptions = {
center: mapCenter,
zoom: 6,
minZoom: 3,
maxZoom: 13,
streetViewControl: false,
mapTypeControlOptions: {
mapTypeIds: [google.maps.MapTypeId.TERRAIN,"moon-visible","moon-elevation","mars-visible","mars-elevation"]
},
mapTypeId:google.maps.MapTypeId.TERRAIN,
gestureHandling: 'greedy'
};
latBox = document.getElementById("c-lat");
lngBox = document.getElementById("c-lng");
google.maps.visualRefresh = true;
map = new google.maps.Map(document.getElementById("gmap"),
mapOptions);
//addMoonMaps(map);
google.maps.event.addListener(map, 'maptypeid_changed', function() {
console.log(map.getMapTypeId());
});
var rectCorners = [
{lat: mapCenter.lat()-boxHeight/2, lng:mapCenter.lng()-boxWidth/2},
{lat: mapCenter.lat()-boxHeight/2, lng:mapCenter.lng()+boxWidth/2},
{lat: mapCenter.lat()+boxHeight/2, lng:mapCenter.lng()+boxWidth/2},
{lat: mapCenter.lat()+boxHeight/2, lng:mapCenter.lng()-boxWidth/2},
];
rectangle = new google.maps.Polygon({
strokeColor: '#FF0000',
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: '#FF0000',
fillOpacity: 0.35,
map: map,
paths: rectCorners,
draggable:true,
geodesic:true
});
widthSlider = document.getElementsByName("boxWidth")[0];
widthLabel = document.getElementById("boxWidthLabel");
heightSlider = document.getElementsByName("boxHeight")[0];
heightLabel = document.getElementById("boxHeightLabel");
scaleSlider = document.getElementsByName("boxScale")[0];
scaleLabel = document.getElementById("boxScaleLabel");
rotationSlider = document.getElementsByName("rotation")[0];
rotationLabel = document.getElementById("rotationLabel");
vScaleSlider = document.getElementsByName("vScale")[0];
vScaleLabel = document.getElementById("vScaleLabel");
waterDropSlider = document.getElementsByName("waterDrop")[0];
waterDropLabel = document.getElementById("waterDropLabel");
baseHeightSlider = document.getElementsByName("baseHeight")[0];
baseHeightLabel = document.getElementById("baseHeightLabel");
google.maps.event.addListener(rectangle, 'dragend', postDrag); //call function after rect is dragged
initializeForm();
}
//https://developer.mozilla.org/en-US/docs/Learn/HTML/Forms/Sending_forms_through_JavaScript#Sending_form_data
function initializeForm(){
var form = document.getElementById("paramForm");
var downloadButton = document.getElementById("downloadbtn");
//var downloadLink = document.getElementById("downloadlink");
$( "form" ).submit(function( event ) {
var str = $( this ).serialize();
//console.log( str );
sendData(str);
event.preventDefault();
});
function sendData(str) {
$("#genButton").addClass("disabled");
$("#genButton").html("<i>Generating...</i>");
downloadButton.style = "visibility:hidden";
var XHR = new XMLHttpRequest();
// Define what happens on successful data submission
XHR.addEventListener("load", function(event) {
var modelNumber = event.target.responseText;
var modelName = "stls/terrain-"+modelNumber+".zip";
//make download button visible
downloadButton.style = "visibility:visible";
//give link the right href
downloadButton.href = modelName;
$("#genButton").removeClass("disabled");
$("#genButton").html("Generate Model");
});
// Define what happens in case of error
XHR.addEventListener("error", function(event) {
console.log('Oops! Something went wrong.');
});
// Set up our request
XHR.open("POST", "gen",true);
XHR.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
// The data sent is what the user provided in the form
XHR.send(str);
}
}
function centerToView(){
mapCenter = map.getCenter();
var _lat = mapCenter.lat()+boxHeight/2;
var _lng = mapCenter.lng()-boxWidth/2;
updateRectangle(
[ {lat: _lat-boxHeight, lng: _lng},
{lat: _lat-boxHeight, lng: _lng+boxWidth},
{lat: _lat, lng:_lng+boxWidth},
{lat: _lat, lng: _lng},
]);
}
function updateRectangle(corners){
rectangle.setPath(corners);
postDrag();
}
function postDrag(){ //called after rectangle is dragged
var _lat = rectangle.getPath().getAt(3).lat();
var _lng = rectangle.getPath().getAt(3).lng();
latBox.value = _lat.toFixed(4);
lngBox.value = _lng.toFixed(4);
}
function changeSize(){
var boxScale = scaleSlider.value;
scaleLabel.innerHTML = scaleSlider.value;
boxWidth=minBoxWidth*widthSlider.value*boxScale/120;
boxHeight=minBoxWidth*heightSlider.value*boxScale/120;
widthLabel.innerHTML = (widthSlider.value*boxScale /3600).toFixed(2)+"\xB0";
heightLabel.innerHTML = (heightSlider.value*boxScale/3600).toFixed(2)+"\xB0";
centerToView();
}
function changeRotation(){
rotationLabel.innerHTML = rotationSlider.value;
boxRotation = rotationSlider.value*Math.PI/180;
var base = rectangle.getPath().getAt(3);
var rotLat = {lat: Math.sin(boxRotation)*boxHeight,lng:Math.cos(boxRotation)*boxWidth};
var rotLng = {lat: Math.cos(boxRotation)*boxHeight, lng:Math.sin(boxRotation)*boxWidth};
updateRectangle(
[ {lat: base.lat()+rotLat.lat, lng: base.lng()+rotLat.lng},
{lat: base.lat()+rotLat.lat-rotLng.lat, lng: base.lng()+rotLat.lng+rotLng.lng},
{lat: base.lat()-rotLng.lat, lng:base.lng()+rotLng.lng},
{lat: base.lat(), lng: base.lng()},
]);
}
function changeVScale(){
vScaleLabel.innerHTML = vScaleSlider.value;
}
function changeWaterDrop(){
waterDropLabel.innerHTML = waterDropSlider.value;
}
function changeBaseHeight(){
baseHeightLabel.innerHTML = baseHeightSlider.value;
}
function updateLatLng(){
var _lat = Math.min(Math.max(parseFloat(document.getElementById('c-lat').value),-69),69);
var _lng = Math.min(Math.max(parseFloat(document.getElementById('c-lng').value),-179),180);
document.getElementById('c-lat').value = _lat;
document.getElementById('c-lng').value = _lng;
if(_lat && _lng){
updateRectangle([ {lat: _lat-boxHeight, lng: _lng},
{lat: _lat-boxHeight, lng: _lng+boxWidth},
{lat: _lat, lng:_lng+boxWidth},
{lat: _lat, lng: _lng},
]);
map.setCenter({lat: _lat, lng: _lng});
}
}