-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcross_section.js
More file actions
140 lines (93 loc) · 5.22 KB
/
cross_section.js
File metadata and controls
140 lines (93 loc) · 5.22 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
/**
* This class calculates cross sections
* A cross section is a vertical cut of the soil along a user-defined line.
* It reveals the vertical geometry of a trench item.
*/
class CrossSection {
/**
* Initializes
* @arg {object} _ItemData the data of a trench item. This is an object created from parsed json data.
* @arg {int} _section_x1 horizontal coordinate of the 1st point of the cross-section line segment
* @arg {int} _section_y1 vertical coordinate of the 1st point of the cross-section line segment
* @arg {int} _section_x2 horizontal coordinate of the 2nd point of the cross-section line segment
* @arg {int} _section_y2 vertical coordinate of the 2nd point of the cross-section line segment
*/
constructor( _ItemData, _section_x1, _section_y1, _section_x2, _section_y2 ) {
this.ItemData = _ItemData;
this.section_x1 = _section_x1;
this.section_y1 = _section_y1;
this.section_x2 = _section_x2;
this.section_y2 = _section_y2;
this.SectionPoints = [];
}
/**
* Parses the "CoverageXYZ" field of a trench item and returns the polygons described in the following format:
* [ [ {X:3, Y:4}, {X:7, Y:6}, {X:8, Y:11}, {X:9, Y:2} ] , [ {X:0, Y:1}, {X:2, Y:4}, {X:8, Y:6} ] ]
*/
getItemPolygons() {
var Polygons = [];
var polygon_idx = 0;
try {
if( this.ItemData.hasOwnProperty("CoverageXYZ") ) {
var S = this.ItemData["CoverageXYZ"].split("POLYGON Z");
for ( var i=1; i<S.length; i++ ) {
S[i] = S[i].substring( S[i].indexOf('('), S[i].indexOf(')') );
S[i] = S[i].replaceAll( '(', '' );
S[i] = S[i].replaceAll( ')', '' );
S[i] = S[i].replaceAll( ',', '' );
var coordinates = S[i].split(' ');
Polygons[polygon_idx] = [];
for(let idx=0; idx<coordinates.length; idx+=3) {
Polygons[polygon_idx].push( {"X":parseFloat(coordinates[idx]), "Y":parseFloat(coordinates[idx+1]), "Z":parseFloat(coordinates[idx+2]) } );
console.log("ZORO A " + parseFloat(coordinates[idx]) + " " + parseFloat(coordinates[idx+1]) + " " + parseFloat(coordinates[idx+2]) );
}
polygon_idx++;
}
} else if( this.ItemData.hasOwnProperty("Location") ) {
Polygons[polygon_idx] = [];
for(let idx=0; idx<this.ItemData["Location"].length; idx+=1) {
var Point = this.ItemData["Location"][idx];
Polygons[polygon_idx].push( {"X":Point["X"], "Y":Point["Y"], "Z":Point["Z"] } );
console.log("ZORO B " + Point["X"] + " " + Point["Y"] + " " + Point["Z"]);
}
polygon_idx++;
}
} catch(ex) { console.log("Error during polygon parsing for " + this.ItemData["Identifier"] + ": " + ex); }
return Polygons;
}
/**
* Calculates the coordinates of the intersection points between the cross-section line defined by the arguments and each polygon side of the the selected items.
* @returns {Array} an array of JSON entities. Each entity represents a shape and contains the coordinates of the corners. Format example: [ {"X":3, "Y":4, "Z":5}, {"X":7, "Y":6, "Z":2} ]
*/
CalcCrossSection( ) { // section_x1=this.section_x1, section_y1=this.section_y1, section_x2=this.section_x2, section_y2=this.section_y2
var section_x1 = this.section_x1;
var section_y1 = this.section_y1;
var section_x2 = this.section_x2;
var section_y2 = this.section_y2;
this.SectionPoints = [];
if( typeof this.ItemData["Location"] != "undefined" && this.ItemData["Location"].length <= 1 ) return this.SectionPoints; // <<<<<<<<
var Polygons = this.getItemPolygons();
for( let poly_idx=0; poly_idx<Polygons.length; poly_idx++) { // for each polygon which describes the item
var idx = poly_idx;
for(let pointIdx=1; pointIdx<Polygons[idx].length; pointIdx++) { // for each polygon edge calculate the coordinates
var edge_x1 = Polygons[idx][pointIdx-1]["X"];
var edge_y1 = Polygons[idx][pointIdx-1]["Y"];
var edge_x2 = Polygons[idx][pointIdx]["X"];
var edge_y2 = Polygons[idx][pointIdx]["Y"];
edge_x1 = map.map_range(edge_x1, PlanMinX, PlanMaxX, 0, PlanImageWidth) * ZoomFactor + CanvasOffsetX;
edge_y1 = map.map_range(edge_y1, PlanMinY, PlanMaxY, PlanImageHeight, 0) * ZoomFactor + CanvasOffsetY;
edge_x2 = map.map_range(edge_x2, PlanMinX, PlanMaxX, 0, PlanImageWidth) * ZoomFactor + CanvasOffsetX;
edge_y2 = map.map_range(edge_y2, PlanMinY, PlanMaxY, PlanImageHeight, 0) * ZoomFactor + CanvasOffsetY;
// check if and where the item's edge and the user-drawn cross-section intersect
var [intersectionX, intersectionY] = Lines.CalculateIntersection( edge_x1, edge_y1, edge_x2, edge_y2, section_x1, section_y1, section_x2, section_y2 );
if( intersectionX != null ) { // the cross-section intersects with this edge
var edge_z1 = Polygons[idx][pointIdx-1]["Z"];
var edge_z2 = Polygons[idx][pointIdx]["Z"];
var intersectionZ = (edge_z2-edge_z1)*(intersectionX-edge_x1)/(edge_x2-edge_x1) + edge_z1; // calculate the depth of the intersection
this.SectionPoints.push( {"X":parseFloat(intersectionX), "Y":parseFloat(intersectionY), "Z":parseFloat(intersectionZ)} );
}
}
}
return this.SectionPoints;
}
}