Debian Codegraph @

src/footleg/cavesurvey/data/model/SurveySeries.java

raw symbols SCIP
1 /**
2 * Copyright (C) 2009-2017 Paul Fretwell - aka 'Footleg' ([email protected])
3 *
4 * This file is part of Cave Converter.
5 *
6 * Cave Converter is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Cave Converter is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Cave Converter. If not, see <http://www.gnu.org/licenses/>.
18 */
19 package footleg.cavesurvey.data.model;
20
21 import java.util.ArrayList;
22 import java.util.Date;
23 import java.util.List;
24 import java.util.ListIterator;
25
26 import footleg.cavesurvey.converter.CaveConverter.BearingUnit;
27 import footleg.cavesurvey.converter.CaveConverter.GradientUnit;
28 import footleg.cavesurvey.converter.CaveConverter.LengthUnit;
29 import footleg.cavesurvey.converter.Logger;
30 import footleg.cavesurvey.tools.UtilityFunctions;
31
32 /**
33 * This class represents a series of cave survey legs joined together by
34 * sharing stations in common. Branches are allowed, but all legs in a series
35 * are required to have the same instrument calibration values and have the
36 * same date. A series can also contain other series, and details of how they
37 * are linked together.
38 *
39 * A survey series contains the survey legs in the series, the date,
40 * calibrations for instruments (these default to zero if not set)
41 * and magnetic declination (defaults to zero if not set).
42 * A series name is required. Optionally a series can also indicate that stations
43 * used in legs in the series are equivalent (i.e. Represent the same point in
44 * the cave). The series can also contain other series, and details of links
45 * between them.
46 * Data is stored and calculations done in metric units (metres and degrees), but
47 * conversion to other units on input/output is supported.
48 *
49 * @author Footleg
50 * @version 2017.01.09 (ISO 8601 YYYY.MM.DD)
51 * @since 1.6 (The Java version used)
52 *
53 * @to.do
54 * TODO Add support for calibration comments fields
55 * TODO Add support for team fields
56 * TODO Add support for instrument fields
57 */
58 public class SurveySeries {
59
60 /**
61 * Private data class to represent the LRUD data for the toStn of a leg.
62 * Used for stations which are not used as 'from' stations in any legs.
63 * This class enables both the LRUD dimensions and the vector normal to the plane
64 * of the LRUD dimensions to be stored.
65 *
66 * @author Footleg
67 *
68 */
69 public class ToStnLRUD {
70 private SurveyStation fromStn;
71
72 //LRUD applies to fromStn
73 private double left = 0.0;
74 private double right = 0.0;
75 private double up = 0.0;
76 private double down = 0.0;
77
78 //Getters and Setters
79 public SurveyStation getFromStn() {
80 return fromStn;
81 }
82 public void setFromStn(SurveyStation fromStn) {
83 this.fromStn = fromStn;
84 }
85 public double getLeft() {
86 return left;
87 }
88 public void setLeft(double left) {
89 this.left = left;
90 }
91 public double getRight() {
92 return right;
93 }
94 public void setRight(double right) {
95 this.right = right;
96 }
97 public double getUp() {
98 return up;
99 }
100 public void setUp(double up) {
101 this.up = up;
102 }
103 public double getDown() {
104 return down;
105 }
106 public void setDown(double down) {
107 this.down = down;
108 }
109 }
110
111 private String seriesName;
112 private List<SurveyLeg> legs;
113 private List<ToStnLRUD> toStnLRUDs;
114 private List<SeriesLink> links;
115 private List<SurveySeries> innerSeries;
116 private int stnRenumberSequence;
117 private List<String> stnRenameCache;
118 private double declination = 0;
119 private double tapeCalibration = 0;
120 private double compassCalibration = 0;
121 private double clinoCalibration = 0;
122 private double clinoCalScaleFactor = 1;
123 private Date surveyDate;
124 private String comment = "";
125 private static double LRUD_SPECIAL_FLAG = -999;
126 private LengthUnit lengthUnit = LengthUnit.Metres;
127 private LengthUnit depthUnit = LengthUnit.Metres;
128 private BearingUnit bearingUnit = BearingUnit.Degrees;
129 private GradientUnit gradientUnit = GradientUnit.Degrees;
130 private List<String> dataOrder;
131 private List<String> dataOrder2;
132
133 /**
134 * Create a Survey Series with the name given
135 * @param name Name of the series
136 */
137 public SurveySeries( String name ) {
138 super();
139 init();
140 this.seriesName = name;
141 }
142
143 private void init() {
144 legs = new ArrayList<SurveyLeg>();
145 toStnLRUDs = new ArrayList<ToStnLRUD>();
146 links = new ArrayList<SeriesLink>();
147 innerSeries = new ArrayList<SurveySeries>();
148 stnRenameCache = new ArrayList<String>();
149 stnRenumberSequence = 0;
150 }
151
152 public LengthUnit getLengthUnit() {
153 return lengthUnit;
154 }
155
156 public void setLengthUnit(LengthUnit lengthUnit) {
157 this.lengthUnit = lengthUnit;
158 }
159
160 public LengthUnit getDepthUnit() {
161 return depthUnit;
162 }
163
164 public void setDepthUnit(LengthUnit depthUnit) {
165 this.depthUnit = depthUnit;
166 }
167
168 public BearingUnit getBearingUnit() {
169 return bearingUnit;
170 }
171
172 public void setBearingUnit(BearingUnit bearingUnit) {
173 this.bearingUnit = bearingUnit;
174 }
175
176 public GradientUnit getGradientUnit() {
177 return gradientUnit;
178 }
179
180 public void setGradientUnit(GradientUnit gradientUnit) {
181 this.gradientUnit = gradientUnit;
182 }
183
184 public void addLeg(SurveyLeg leg) {
185 //Create duplicate of leg to break reference
186 this.legs.add(leg.clone());
187 }
188
189 public void addLeg(SurveyLeg leg, int position) {
190 //Create duplicate of leg to break reference
191 this.legs.add( position, leg.clone() );
192 }
193
194 public int legCount() {
195 return legs.size();
196 }
197
198 public List<ToStnLRUD> getToStnLRUDs() {
199 return toStnLRUDs;
200 }
201
202 /**
203 * Assigns a survey station a unique Id for the station name. The survey
204 * station name is mapped to an integer which is assigned as the station id.
205 * This can be used for file formats which do not support text station names.
206 * If the name is a string which represents
207 * a positive integer number already then that number is returned.
208 * For non-numeric station names the name is assigned
209 * a unique negative integer number. Negative numbers are used so they will
210 * not clash with any positive number station names in the series. The negative
211 * station ids also indicate which stations have names which are not numeric.
212 * A map of names to numbers already added to the series is used so that the
213 * same name is not translated into different numbers if passed in more than
214 * once. When all legs have been added to a series, the negative numbered
215 * stations could be assigned new positive number IDs which are not already
216 * used by other stations in the series.
217 *
218 * @param stn The survey station with a name property to be assigned an id
219 */
220 public void setStationIdFromName( SurveyStation stn ) {
221 int stnId = stnNameToNumber( stn.getName() );
222 stn.setId( stnId );
223 }
224
225 /**
226 * @param stnName
227 * @return a station number linked to the text name for this series
228 */
229 private int stnNameToNumber( String stnName ) {
230 int stn;
231 try {
232 stn = Integer.parseInt( stnName );
233 }
234 catch (NumberFormatException nfe) {
235 //Stn name not an integer, so translate name to a negative number
236 stn = this.getNumberToRepresentStnName( stnName );
237 }
238 return stn;
239 }
240
241 /**
242 * Returns a negative integer number for a station name string.
243 *
244 * The number will be unique to this series for this station name.
245 * This enables station names to be converted to numbers for file formats
246 * which do not support strings. The name is mapped to a number in this
247 * series by looking up the name in a cache for the series. If the name
248 * is not found in the cache then a new negative number is returned, and
249 * the new name and number mapping is added to the cache for the series.
250 *
251 * @param stnName Name of the survey station to map to a number.
252 * @return Number which can be used to represent this station.
253 */
254 public int getNumberToRepresentStnName(String stnName) {
255 int mappedStnNum = 0;
256
257 //Look for station name in cache
258 ListIterator<String> stnCacheIterator = stnRenameCache.listIterator();
259 int stnIdx = 0;
260 while ( ( mappedStnNum == 0 ) && ( stnCacheIterator.hasNext() ) ) {
261 String cachedName = stnCacheIterator.next();
262 stnIdx--;
263 if ( stnName.compareToIgnoreCase(cachedName) == 0 ) {
264 //Found matching stn in cache, so get number from index
265 mappedStnNum = stnIdx;
266 }
267 }
268
269 if ( mappedStnNum == 0 ) {
270 //No match found in cache, so create a new mapping and cache it
271 stnRenumberSequence--;
272 mappedStnNum = stnRenumberSequence;
273 /**
274 * As the mapped stns are added to the cache, the index in the
275 * cache will correspond to the mapped number of the stn:
276 * idx 0 = map -1
277 * idx 1 = map -2
278 * etc.
279 */
280 stnRenameCache.add(stnName);
281 }
282
283 return mappedStnNum;
284 }
285
286 public String getMappedStnName(int stn) {
287 //If number is negative then lookup name in map,
288 //otherwise just return station number as the name
289 if ( stn < 0 ) {
290 return stnRenameCache.get( Math.abs(stn) - 1 );
291 }
292 else {
293 return "" + stn;
294 }
295 }
296
297 //Apply instrument calibration corrections to all measurements in returned leg
298 public SurveyLeg getLegCorrected(int index) {
299 SurveyLeg originalLeg = legs.get(index);
300 SurveyLeg correctedLeg = originalLeg.clone();
301
302 //Over-ride leg length,compass and clino with calibration corrected values
303 correctedLeg.setLength( originalLeg.getLength(LengthUnit.Metres) - tapeCalibration, LengthUnit.Metres );
304 double compass = originalLeg.getCompass(BearingUnit.Degrees);
305 if ( compass >= 0 && compass <= 360 ) {
306 //Apply calibration only to valid bearings
307 compass -= (compassCalibration + declination);
308 }
309 correctedLeg.setCompass( compass, BearingUnit.Degrees );
310 double clino = originalLeg.getClino(GradientUnit.Degrees);
311 if ( clino >= -90 && clino <= 180 ) {
312 //Apply calibration only to valid clino
313 clino = (clino - clinoCalibration) * clinoCalScaleFactor;
314 }
315 correctedLeg.setClino( clino, GradientUnit.Degrees );
316
317 //Don't correct LRUD using tape calibration as these are assumed to be estimated by eye
318 return correctedLeg;
319 }
320
321 public SurveyLeg getLegRaw(int index) {
322 return legs.get(index);
323 }
324
325 public void addSeries(SurveySeries series) {
326 this.innerSeries.add(series);
327 }
328
329 public int innerSeriesCount() {
330 return innerSeries.size();
331 }
332
333 public SurveySeries getInnerSeries(int index) {
334 return innerSeries.get(index);
335 }
336
337 public List<SurveySeries> getInnerSeriesList() {
338 return innerSeries;
339 }
340
341 public SurveySeries findInnerSeriesByName(String name) {
342 SurveySeries match = null;
343 //Look in inner series
344 ListIterator<SurveySeries> innerIterator = innerSeries.listIterator();
345 while ( innerIterator.hasNext() ) {
346 SurveySeries innerSeries = innerIterator.next();
347 if ( innerSeries.getSeriesName().equalsIgnoreCase( name ) ) {
348 //Found series, so return this
349 match = innerSeries;
350 break;
351 }
352 }
353 return match;
354 }
355
356 /**
357 * Look up the child object specified in the series, and return the index of the object
358 * @param child The child object to get the index of
359 * @return The index of the object in this series
360 */
361 public int getIndexOfChild(Object child) {
362 int matchIndex = -1;
363 //Look for child in inner series
364 ListIterator<SurveySeries> innerIterator = innerSeries.listIterator();
365 int idx = -1;
366 while ( innerIterator.hasNext() ) {
367 SurveySeries innerSeries = innerIterator.next();
368 idx++;
369 if ( innerSeries.equals( child ) ) {
370 //Found series, so return this
371 matchIndex = idx;
372 break;
373 }
374 }
375 return matchIndex;
376 }
377
378 public void addLink(String series1Path, SurveyStation stn1, String series2Path, SurveyStation stn2 ) {
379 //Add link to array
380 SeriesLink link = new SeriesLink(series1Path, stn1, series2Path, stn2 );
381 links.add(link);
382 }
383
384 public List<SeriesLink> getLinks() {
385 return links;
386 }
387
388 public String getSeriesName() {
389 return seriesName;
390 }
391
392 public void setSeriesName(String seriesName) {
393 this.seriesName = seriesName;
394 }
395
396 public List<String> getDataOrder() {
397 //Make a copy to return, as we don't want a pointer to the existing list or parsers
398 //can change it
399 List<String> tmpList = new ArrayList<String>();
400 if (dataOrder != null ) {
401 tmpList.addAll(dataOrder);
402 }
403
404 return tmpList;
405 }
406
407 public List<String> getDataOrder2() {
408 //Make a copy to return, as we don't want a pointer to the existing list or parsers
409 //can change it
410 List<String> tmpList = new ArrayList<String>();
411 if (dataOrder2 != null ) {
412 tmpList.addAll(dataOrder2);
413 }
414
415 return tmpList;
416 }
417
418 /**
419 * Indicates if series has a data order defined. Method provided to avoid copying data order
420 * list just to check this from outside the series, as getter clones the list to protect it
421 * from being edited via a pointer.
422 * @return True if series has a field data order defined.
423 */
424 public boolean hasDataOrder() {
425 boolean hasIt = false;
426 if ( dataOrder != null ) {
427 hasIt = (dataOrder.size() > 0);
428 }
429 return hasIt;
430 }
431
432 /**
433 * Specifies the order of data items used in the file which was read to generate the series.
434 * Parsers use this to track the active data ordering (i.e. from to length, bearing, gradient)
435 * through nested series in files.
436 * @param dataOrder Field data order for the series
437 */
438 public void setDataOrder(List<String> dataOrder) {
439 //Make a copy to store, as we don't want a pointer to an existing list
440 List<String> tmpList = new ArrayList<String>();
441 tmpList.addAll(dataOrder);
442
443 this.dataOrder = tmpList;
444 }
445
446 /**
447 * Secondary order of data items used in the series. This is used when a series contains a mix
448 * of normal and diving data to hold the data order for both formats of data.
449 * @param dataOrder Field data order for the series for alternate data format (e.g. For mixed diving/normal data in series)
450 */
451 public void setDataOrder2(List<String> dataOrder) {
452 //Make a copy to store, as we don't want a pointer to an existing list
453 List<String> tmpList = new ArrayList<String>();
454 tmpList.addAll(dataOrder);
455
456 this.dataOrder2 = tmpList;
457 }
458
459 /**
460 * Provides a string representation of a cave survey series to display in a tree view of the data model
461 * @return String representation of survey series class
462 */
463 public String toString() {
464 String text = getSeriesName();
465 if ( ( text == null ) || ( text.equals("") ) ) {
466 text = "Survey Series";
467 }
468 return text;
469 }
470
471 public double getTapeCalibration(LengthUnit units) {
472 return UtilityFunctions.lengthFromMetres( tapeCalibration, units );
473 }
474
475 public void setTapeCalibration(double tapeCalibration, LengthUnit units) {
476 this.tapeCalibration = UtilityFunctions.lengthToMetres(tapeCalibration, units);
477 }
478
479 public double getDeclination() {
480 return declination;
481 }
482
483 public void setDeclination(double declination) {
484 this.declination = declination;
485 }
486
487 public double getCompassCalibration(BearingUnit units) {
488 return UtilityFunctions.bearingFromDegrees(compassCalibration, units) ;
489 }
490
491 public void setCompassCalibration(double compassCalibration, BearingUnit units) {
492 this.compassCalibration = UtilityFunctions.bearingToDegrees(compassCalibration, units) ;
493 }
494
495 public double getClinoCalibration(GradientUnit units) {
496 return UtilityFunctions.gradientFromDegrees(clinoCalibration, units) ;
497 }
498
499 public double getClinoScaleFactor() {
500 return clinoCalScaleFactor;
501 }
502
503 public void setClinoCalibration(double clinoCalibration, GradientUnit units) {
504 this.clinoCalibration = UtilityFunctions.gradientToDegrees(clinoCalibration, units);
505 this.clinoCalScaleFactor = 1;
506 }
507
508 public void setClinoCalibration(double clinoCalibration, GradientUnit units, double clinoCalScaleFactor) {
509 this.clinoCalibration = UtilityFunctions.gradientToDegrees(clinoCalibration, units);;
510 this.clinoCalScaleFactor = clinoCalScaleFactor;
511 }
512
513 public Date getSurveyDate() {
514 return surveyDate;
515 }
516
517 public void setSurveyDate(Date surveyDate) {
518 this.surveyDate = surveyDate;
519 }
520
521 public String getComment() {
522 return comment;
523 }
524
525 public void setComment(String comment) {
526 this.comment = comment;
527 }
528
529 public void setCalibrationFromAnotherSeries(SurveySeries series) {
530 setDeclination( series.getDeclination() );
531 setTapeCalibration( series.getTapeCalibration( LengthUnit.Metres ), LengthUnit.Metres );
532 setCompassCalibration( series.getCompassCalibration( BearingUnit.Degrees ), BearingUnit.Degrees );
533 setClinoCalibration( series.getClinoCalibration( GradientUnit.Degrees ), GradientUnit.Degrees, series.getClinoScaleFactor() );
534 }
535
536 public SurveyLeg removeLeg(int index){
537 return legs.remove(index);
538 }
539
540 public void removeSplaysUsedForLRUD() {
541 int i = legs.size() - 1;
542 while ( i >= 0 ) {
543 SurveyLeg leg = legs.get(i);
544 if ( leg.isSplay() && leg.getDown( LengthUnit.Metres ) == LRUD_SPECIAL_FLAG ) {
545 //Splay leg used for LRUD generation
546 removeLeg(i);
547 }
548 else {
549 i--;
550 }
551 }
552 }
553
554 /**
555 * Examines all splay shots from the start station for each leg and generates LRUD data
556 * from them for each leg. The algorithm examines all legs which meet at the station, and
557 * splays which are close to equivalent to any of the legs are not used (to prevent back shots and
558 * splays up side passages which were then surveyed being used to calculate passage dimensions).
559 * Up and down dimensions are calculated from the splay with the greatest vertical length
560 * up or down as appropriate. Splays which are less than 20 degrees from horizontal are
561 * ignored when determining up and down dimensions. Splays with an angle steeper than 70 degrees
562 * from horizontal are excluded from the splays used to determine horizontal passage dimensions.
563 * The passage dimension directions for left and right are calculated along the horizontal
564 * vector bisecting the angle between the forward survey leg, and the most in-line previous
565 * leg arriving at the station. The best matching splay is selected from the suitable
566 * splays by determining which splay gives the greatest distance along the left or right
567 * dimension vector.
568 * @param logger Logging class to output information, warning and error messages to
569 */
570 public void generateLRUDFromSplays( Logger logger ) {
571 List<SurveyLeg> caveLegs = new ArrayList<SurveyLeg>();
572 List<List<SurveyLeg>> splayLegGroups = new ArrayList<List<SurveyLeg>>();
573 List<List<SurveyLeg>> previousLegGroups = new ArrayList<List<SurveyLeg>>();
574
575 //Loop through all legs in series to build arrays of splays and non-splay underground legs
576 ListIterator<SurveyLeg> legsIterator = legs.listIterator();
577 while ( legsIterator.hasNext() ) {
578 SurveyLeg leg = legsIterator.next();
579
580 //Put all non-surface legs into either the cave legs (non-splays) or splay groups lists
581 if ( leg.isSurface() == false ) {
582 if ( leg.isSplay() ) {
583 //Add to splays group identified by station name
584 String stnName = leg.getFromStn().getName();
585 //Look for a group matching this station
586 int groupIdx = -1;
587 for ( int i = 0; groupIdx < 0 && i < splayLegGroups.size(); i++ ) {
588 if ( splayLegGroups.get(i).get(0).getFromStn().getName().compareTo(stnName) == 0 ) {
589 //Found group, so get index
590 groupIdx = i;
591 }
592 }
593 if ( groupIdx >= 0 ) {
594 //Add to this group
595 splayLegGroups.get(groupIdx).add(leg);
596 }
597 else {
598 //Create new group and add leg to this
599 List<SurveyLeg> splays = new ArrayList<SurveyLeg>();
600 splays.add(leg);
601 splayLegGroups.add(splays);
602 }
603 }
604 else {
605 //Add to legs array to be processed
606 caveLegs.add(leg);
607 }
608 }
609 }
610
611 //Loop through all legs non-splay legs to build arrays of legs radiating from the station of each leg
612 for ( int caveLegIdx = 0; caveLegIdx < caveLegs.size(); caveLegIdx++ ) {
613 SurveyLeg leg = caveLegs.get(caveLegIdx);
614 String legStartStn = leg.getFromStn().getName();
615
616 //Create list to hold legs radiating out from this leg fromStn
617 List<SurveyLeg> legGrp = new ArrayList<SurveyLeg>();
618
619 //Find all other legs which radiate out from the fromStn of this leg
620 for ( int otherLegIdx = 0; otherLegIdx < caveLegs.size(); otherLegIdx++ ) {
621 //Ignore ourself
622 if ( otherLegIdx != caveLegIdx ) {
623 SurveyLeg chkLeg = caveLegs.get(otherLegIdx);
624 if ( legStartStn.compareToIgnoreCase( chkLeg.getToStn().getName() ) == 0 ) {
625 //Add leg to group
626 legGrp.add(chkLeg);
627 }
628 else if ( legStartStn.compareToIgnoreCase( chkLeg.getFromStn().getName() ) == 0 ) {
629 //Reverse leg so it leads to this station and add leg to group
630 SurveyLeg revLeg = chkLeg.clone();
631 revLeg.reverseDirection();
632 legGrp.add( revLeg );
633 }
634 }
635 }
636 //Add group to list for this leg
637 previousLegGroups.add( legGrp );
638 }
639
640 //Loop through splay groups and process all the legs with a from stn matching the from stn of the splays in each group
641 ListIterator<List<SurveyLeg>> spGroupsIter = splayLegGroups.listIterator();
642 List<String> stnsUsed = new ArrayList<String>();
643 while ( spGroupsIter.hasNext() ) {
644 List<SurveyLeg> splaysGroup = spGroupsIter.next();
645 String stnName = splaysGroup.get(0).getFromStn().getName();
646
647 ListIterator<SurveyLeg> legsIter = caveLegs.listIterator();
648 int caveLegIdx = -1;
649 while ( legsIter.hasNext() ) {
650 SurveyLeg leg = legsIter.next();
651 caveLegIdx++;
652 if ( leg.getFromStn().getName().compareTo(stnName) == 0 ) {
653 //Generate LRUD data for leg, using the splays starting from that leg from station
654 generateLRUDForLeg( leg, splaysGroup, previousLegGroups.get(caveLegIdx), logger );
655 //Store station name of splays group that was used
656 stnsUsed.add(stnName);
657 //Remove leg from list now it has been processed
658 legsIter.remove();
659 //Remove corresponding previous legs group so lists remain in sync
660 previousLegGroups.remove(caveLegIdx);
661 caveLegIdx--;
662 }
663 }
664 }
665
666 //Build list of splays groups which were not used, as these will be for terminal
667 //stations in branches of the series. These splays will only be from the toStn of a leg.
668 ListIterator<List<SurveyLeg>> spGroupsIter2 = splayLegGroups.listIterator();
669 List<List<SurveyLeg>> unusedSplayGroups = new ArrayList<List<SurveyLeg>>();
670 while ( spGroupsIter2.hasNext() ) {
671 List<SurveyLeg> splaysGroup = spGroupsIter2.next();
672 String stnName = splaysGroup.get(0).getFromStn().getName();
673
674 //Check for this group in the used stations list
675 boolean foundMatch = false;
676 ListIterator<String> stnsUsedIter = stnsUsed.listIterator();
677 while ( stnsUsedIter.hasNext() ) {
678 String grpName = stnsUsedIter.next();
679 if ( stnName.compareTo( grpName ) == 0 ) {
680 //Set flag to indicate group was used
681 foundMatch = true;
682 break;
683 }
684 }
685
686 if ( foundMatch == false ) {
687 //Add group to unused groups list
688 unusedSplayGroups.add( splaysGroup );
689 }
690 }
691
692 //Loop through unused splays groups and find the leg where the toStn for these
693 //splays occur. Then add the LRUD data for the toStn to the toStnLrud cache.
694 ListIterator<List<SurveyLeg>> spGroupsIter3 = unusedSplayGroups.listIterator();
695 while ( spGroupsIter3.hasNext() ) {
696 List<SurveyLeg> splaysGroup = spGroupsIter3.next();
697 String stnName = splaysGroup.get(0).getFromStn().getName();
698
699 //Find leg with a matching toStn for these splays
700 for ( int legsIdx = 0; legsIdx < legs.size(); legsIdx++ ) {
701 SurveyLeg leg = legs.get( legsIdx );
702 if ( ( leg.isSplay() == false )
703 && ( leg.getToStn() != null )
704 && ( leg.getToStn().getName().compareTo(stnName) == 0 ) ) {
705 //Create a temporary leg to hold the LRUD for this toStn
706 SurveyLeg tempLrudLeg = new SurveyLeg();
707 tempLrudLeg.setFromStn( leg.getToStn() );
708 tempLrudLeg.setLength( -1, LengthUnit.Metres );
709 tempLrudLeg.setCompass( leg.getCompass(BearingUnit.Degrees), BearingUnit.Degrees );
710 tempLrudLeg.setClino( leg.getClino(GradientUnit.Degrees), GradientUnit.Degrees );
711 //Check for any other legs terminating at this station, as if there are any then
712 //their bearings should be used to average with the bearing for the leg we are creating here.
713 List<SurveyLeg> otherLegs = new ArrayList<SurveyLeg>();
714
715 /* Find any other legs which terminate at this station. There may be more than
716 * one leg terminating at a station which is not a 'from' station in any leg
717 * e.g. In a leap-frog survey. So we need to hold the LRUD for the station which
718 * only appears as a 'to' station, but in more than one leg. If there were 3 legs all
719 * pointing 'to' a station then the first one in the series will be the one the LRUD data
720 * is associated with really, but this is not recorded.
721 */
722 for ( int legsIdx2 = legsIdx + 1; legsIdx2 < legs.size(); legsIdx2++ ) {
723 SurveyLeg chkLeg = legs.get( legsIdx2 );
724 if ( ( chkLeg.isSplay() == false )
725 && ( chkLeg.getToStn() != null )
726 && ( chkLeg.getToStn().getName().compareTo(stnName) == 0 ) ) {
727 //Found another leg to this station, we need to reverse it as
728 //LRUD function takes group of legs from the station
729 SurveyLeg revLeg = chkLeg.clone();
730 revLeg.reverseDirection();
731 otherLegs.add(revLeg);
732 }
733 }
734 //Generate LRUD data for leg, using the splays starting from that leg from station
735 generateLRUDForLeg( tempLrudLeg, splaysGroup, otherLegs, logger );
736 //Create an LRUD object to hold the LRUD data and copy data into it
737 ToStnLRUD newLrud = new ToStnLRUD();
738 newLrud.setFromStn( tempLrudLeg.getFromStn() );
739 newLrud.setLeft( tempLrudLeg.getLeft(LengthUnit.Metres) );
740 newLrud.setRight( tempLrudLeg.getRight(LengthUnit.Metres) );
741 newLrud.setUp( tempLrudLeg.getUp(LengthUnit.Metres) );
742 newLrud.setDown( tempLrudLeg.getDown(LengthUnit.Metres) );
743 //Add the new LRUD leg in the series
744 toStnLRUDs.add(newLrud);
745 //Stop checking series once LRUD data has been accounted for or LRUD data will get
746 //duplicated in cases where the station is the toStn in more than one leg (e.g. In
747 //a leap-frog survey)
748 legsIdx = legs.size();
749 }
750 }
751 }
752
753 }
754
755 /**
756 * Generates a Left, Right, Up and Down dimension at the from station for a survey leg
757 * using the splays recorded at that station, and stores them in the leg.
758 * @param leg The survey leg to generate LRUD data for (at the From station)
759 * @param splaysGroup A list of splay legs measured from the From station of the leg
760 * @param otherLegs A list of survey legs arriving at the same station as the From station of the leg
761 */
762 private void generateLRUDForLeg( SurveyLeg masterLeg, List<SurveyLeg> splayShots, List<SurveyLeg> otherLegs, Logger logger ) {
763 int bearingTolerance = 3;
764 List<SurveyLeg> leftShots = new ArrayList<SurveyLeg>();
765 List<SurveyLeg> rightShots = new ArrayList<SurveyLeg>();
766 List<SurveyLeg> upShots = new ArrayList<SurveyLeg>();
767 List<SurveyLeg> downShots = new ArrayList<SurveyLeg>();
768
769 //Determine best previous leg (use leg with closest bearing to onward leg)
770 double bestPrevBearing = 360.0;
771 int bestPrevLegIdx = -1;
772 for ( int i = 0; i < otherLegs.size(); i++ ) {
773 //Looking for bearing closest to master leg bearing
774 double testBearing = UtilityFunctions.bearingDifferenceDegrees( otherLegs.get(i).getCompass(BearingUnit.Degrees), masterLeg.getCompass(BearingUnit.Degrees) );
775 if ( testBearing < bestPrevBearing ) {
776 //Found better match, so store value and index
777 bestPrevBearing = testBearing;
778 bestPrevLegIdx = i;
779 }
780 }
781
782 //If a previous leg was found then calculate average bearing between previous and onward legs
783 double bearing = masterLeg.getCompass(BearingUnit.Degrees);
784 if ( bestPrevLegIdx >= 0 ) {
785 double[] bearings = new double[2];
786
787 bearings[0] = masterLeg.getCompass(BearingUnit.Degrees);
788 bearings[1] = otherLegs.get(bestPrevLegIdx).getCompass(BearingUnit.Degrees);
789
790 bearing = UtilityFunctions.averageCompassBearings(bearings);
791 }
792
793 //Loop through all the splays and categorise each shot into groups of L,R,U,D
794 for ( int i = 0; i < splayShots.size(); i++ ) {
795 SurveyLeg splayLeg = splayShots.get(i);
796
797 //Ignore any splays which are along the path of any legs to/from this station
798 //This will remove backshots and splays fired up side passages which were then surveyed
799 //as side passage splays are not going to represent passage dimensions.
800 boolean backShot = false;
801 for ( int olegsIdx = -1; olegsIdx < otherLegs.size(); olegsIdx++ ) {
802 //First check against main leg
803 SurveyLeg testLeg = masterLeg;
804 if (olegsIdx > -1) {
805 //Then loop through other legs reversed (as we need bearing from this station, not to it)
806 testLeg = otherLegs.get(olegsIdx).clone();
807 testLeg.reverseDirection();
808 }
809 //Check if splay is equivalent to leg shot
810 double bearingDiff = UtilityFunctions.bearingDifferenceDegrees( testLeg.getCompass(BearingUnit.Degrees), splayLeg.getCompass(BearingUnit.Degrees) );
811 if ( bearingDiff < bearingTolerance ) {
812 //Within 3 degrees of leg, so check clino
813 double clinoDiff = UtilityFunctions.bearingDifferenceDegrees( testLeg.getClino(GradientUnit.Degrees), splayLeg.getClino(GradientUnit.Degrees) );
814 if ( clinoDiff < bearingTolerance ) {
815 //Within 3 degrees of leg, so check length
816 double lengthDiff = Math.abs( testLeg.getLength( LengthUnit.Metres ) - splayLeg.getLength( LengthUnit.Metres ) );
817 if ( lengthDiff < 0.2 ) {
818 //Under 20cm difference in length, assume a back shot for a leg
819 backShot = true;
820 logger.logMessage("Ignoring splay from " + getSeriesName() + "." + splayLeg.getFromStn().getName() +
821 " with length of " + splayLeg.getLength( LengthUnit.Metres ) + " as splay too closely matching a leg and so assumed to be a back-shot.");
822 }
823 }
824 }
825 }
826
827 if ( backShot == false ) {
828 double splayClino = splayLeg.getClino(GradientUnit.Degrees);
829 if ( splayClino > 20 ) {
830 //Up shot
831 upShots.add(splayLeg);
832 }
833 else if ( splayClino < -20 ) {
834 //Down shot
835 downShots.add(splayLeg);
836 }
837 //As well as up or down, some splays may also be the best Left or Right
838 if ( ( splayClino < 70 ) && ( splayClino > -70 ) ) {
839 //Left or right shots (not steeper than 70 deg.)
840 //Determine whether left or right shot
841 double masterBearing = masterLeg.getCompass(BearingUnit.Degrees);
842 //Normalise bearings to reference of zero degrees for onward leg
843 double splayCorrected = splayLeg.getCompass(BearingUnit.Degrees) - masterBearing;
844 if ( splayCorrected < 0 ) {
845 splayCorrected += 360;
846 }
847 if ( bestPrevLegIdx >= 0 ) {
848 //Two legs to consider
849 double prevLegBearing = otherLegs.get(bestPrevLegIdx).getCompass(BearingUnit.Degrees);
850 //Convert previous leg bearing to bearing from the station for the splay adjusted with respect to onward leg bearing
851 double prevLegBackBearingCorrected = UtilityFunctions.adjustBearingWithinDegreesRange(
852 180 + prevLegBearing - masterBearing, 0, 360 );
853 /* We now have three bearings. The onward leg is adjusted to zero deg.
854 * The back bearing for the previous leg is adjusted relative to the zero bearing of the onward leg.
855 * The splay is also adjusted relative to the zero bearing of the onward leg.
856 * Splays which are aligned with any leg from the station should already have been discarded.
857 * So now we just need to determine if this splay as a left or right splay.
858 */
859 if ( splayCorrected < prevLegBackBearingCorrected ) {
860 //Right
861 rightShots.add(splayLeg);
862 }
863 else {
864 //Left
865 leftShots.add(splayLeg);
866 }
867 }
868 else {
869 //Only the one leg to consider as first or last leg of chain of legs in series
870
871 //Angle between splay and leg bearings is now in the range 0-360 deg.
872 if ( ( splayCorrected > bearingTolerance ) || ( splayCorrected > (360 - bearingTolerance) ) ) {
873 if ( splayCorrected < ( 180 - bearingTolerance) ) {
874 //Right
875 rightShots.add(splayLeg);
876 }
877 else if ( splayCorrected > ( 180 + bearingTolerance) ) {
878 //Left
879 leftShots.add(splayLeg);
880 }
881 else {
882 //Splay is within tolerance of back bearing for leg, so too shallow an angle to use for LRUD calc.
883 logger.logMessage("Ignoring splay from " + getSeriesName() + "." + splayLeg.getFromStn().getName() +
884 " with bearing of " + splayLeg.getCompass(BearingUnit.Degrees) + " as bearing is < " + bearingTolerance + " degrees off back bearing of leg.");
885 }
886 }
887 else {
888 //Splay is within tolerance of bearing for leg, so too shallow an angle to use for LRUD calc.
889 logger.logMessage("Ignoring splay from " + getSeriesName() + "." + splayLeg.getFromStn().getName() +
890 " with bearing of " + splayLeg.getCompass(BearingUnit.Degrees) + " as bearing is < " + bearingTolerance + " degrees off bearing of leg.");
891 }
892 }
893
894 }
895 }
896 }
897
898 //Set flag for this station to say whether all splays should be output
899 boolean keepAllSplays = false;
900 if (( upShots.size() > 1 )
901 || ( downShots.size() > 1 )
902 || ( leftShots.size() > 1 )
903 || ( rightShots.size() > 1 )) {
904 keepAllSplays = true;
905 }
906
907 //Find best up shot
908 double bestValue = 0.0;
909 int bestIdx = -1;
910 for ( int i = 0; i < upShots.size(); i++ ) {
911 //Looking for highest clino reading
912 if ( upShots.get(i).getClino(GradientUnit.Degrees) > bestValue ) {
913 //Found better match, so store value and index
914 bestValue = upShots.get(i).getClino(GradientUnit.Degrees);
915 bestIdx = i;
916 }
917 }
918 //Add up shot if found
919 if ( bestIdx >= 0 ) {
920 //Found best up shot
921 masterLeg.setUp( upShots.get(bestIdx).getVerticalLength(), LengthUnit.Metres );
922 //Flag splay as used for Up
923 if ( keepAllSplays == false ) {
924 upShots.get(bestIdx).setDown( LRUD_SPECIAL_FLAG, LengthUnit.Metres );
925 }
926 }
927
928 //Find best down shot
929 bestValue = 0.0;
930 bestIdx = -1;
931 for ( int i = 0; i < downShots.size(); i++ ) {
932 //Looking for lowest clino reading
933 if ( downShots.get(i).getClino(GradientUnit.Degrees) < bestValue ) {
934 //Found better match, so store value and index
935 bestValue = downShots.get(i).getClino(GradientUnit.Degrees);
936 bestIdx = i;
937 }
938 }
939 //Add down shot if found
940 if ( bestIdx >= 0 ) {
941 //Found best down shot
942 masterLeg.setDown( downShots.get(bestIdx).getVerticalLength(), LengthUnit.Metres );
943 //Flag splay as used for Down
944 if ( keepAllSplays == false ) {
945 downShots.get(bestIdx).setDown( LRUD_SPECIAL_FLAG, LengthUnit.Metres );
946 }
947 }
948
949 //Find best left shot
950 bestIdx = findBestSplayForHorizontalDimension(leftShots, bearing, true);
951
952 //Add left shot if found
953 if ( bestIdx >= 0 ) {
954 //Found best left shot
955 double horizontalLength = leftShots.get(bestIdx).getHorizontalLength();
956 double leftOrthoganal = bearing - 90;
957 if ( leftOrthoganal < 0 ) {
958 leftOrthoganal += 360;
959 }
960 masterLeg.setLeft( extentAlongBearing( leftOrthoganal,
961 horizontalLength, leftShots.get(bestIdx).getCompass(BearingUnit.Degrees) ), LengthUnit.Metres );
962 //Flag splay as used for Left
963 if ( keepAllSplays == false ) {
964 leftShots.get(bestIdx).setDown( LRUD_SPECIAL_FLAG, LengthUnit.Metres );
965 }
966 }
967
968 //Find best right shot
969 bestIdx = findBestSplayForHorizontalDimension(rightShots, bearing, false);
970
971 //Add right shot if found
972 if ( bestIdx >= 0 ) {
973 //Found best right shot
974 double horizontalLength = rightShots.get(bestIdx).getHorizontalLength();
975 double rightOrthoganal = bearing + 90;
976 if ( rightOrthoganal >= 360 ) {
977 rightOrthoganal -= 360;
978 }
979 masterLeg.setRight( extentAlongBearing( rightOrthoganal,
980 horizontalLength, rightShots.get(bestIdx).getCompass(BearingUnit.Degrees) ), LengthUnit.Metres );
981 //Flag splay as used for Right
982 if ( keepAllSplays == false ) {
983 rightShots.get(bestIdx).setDown( LRUD_SPECIAL_FLAG, LengthUnit.Metres );
984 }
985 }
986 }
987
988 /**
989 * Determines which of the splays is the best one to use to calculate the horizontal dimension for the
990 * cave passage at this station
991 *
992 * @param splays Array of splay shots to pick the best shot from
993 * @param forwardBearing Bearing from the station to represent the passage direction
994 * @param left Flag to indicate if we want to find the best left shot or best right shot
995 * @return Index of the best matching splay shot from the input array of splays
996 */
997 private int findBestSplayForHorizontalDimension(List<SurveyLeg> splays, double forwardBearing, boolean left) {
998 //Find best shot (greatest horizontal extent along vector orthogonal to leg bearing)
999 double bestValue = 0.0;
1000 int bestIdx = -1;
1001 for ( int i = 0; i < splays.size(); i++ ) {
1002 //Calculate corrected compass bearing for splay, normalised to a passage trending due North (zero degrees)
1003 double shotCorrected = splays.get(i).getCompass(BearingUnit.Degrees) - forwardBearing;
1004 if ( shotCorrected < 0 ) {
1005 shotCorrected += 360;
1006 }
1007 //Calculate horizontal extent of leg in direction orthogonal to corrected bearing (left or right)
1008 double orthogonal;
1009 if ( left ) {
1010 orthogonal = 270.0;
1011 }
1012 else {
1013 orthogonal = 90.0;
1014 }
1015 double horizontalLength = splays.get(i).getHorizontalLength();
1016 double orthogonalExtent = extentAlongBearing( orthogonal,
1017 horizontalLength, shotCorrected);
1018
1019 if ( orthogonalExtent > bestValue ) {
1020 //Found better match, so store value and index
1021 bestValue = orthogonalExtent;
1022 bestIdx = i;
1023 }
1024 }
1025
1026 return bestIdx;
1027 }
1028
1029 /**
1030 * Calculates the distance a vector extends along a bearing. Used to work out the distance of the cave wall
1031 * for a left or right passage dimension in a given direction from a station for a horizontal length measured
1032 * along another bearing
1033 * @param bearing The direction we need the distance to the wall for
1034 * @param vectorLength The length of the horizontal measurement we have
1035 * @param vectorBearing The direction of the horizontal measurement we have
1036 * @return The calculated distance to the wall from the station in the direction of the bearing
1037 */
1038 private double extentAlongBearing( double bearing, double vectorLength,
1039 double vectorBearing) {
1040 double bearingDifference = UtilityFunctions.bearingDifferenceDegrees(bearing, vectorBearing);
1041 double distance = vectorLength * Math.cos(Math.PI * bearingDifference / 180);
1042
1043 return distance;
1044 }
1045
1046 /**
1047 * Reverses the order of all legs in the series, so in a linear chain of stations the last
1048 * station is placed first, and the first station is placed last.
1049 * Was written to Toporobot export, but then was not needed. Leaving the code here but
1050 * commented out as not currently used.
1051 public void reverseSeries() {
1052 //Loop through all legs, moving each one to the start of the series
1053 for (int idx = 0; idx < legs.size(); idx++) {
1054 SurveyLeg leg = legs.remove(idx);
1055 //Reverse leg direction
1056 SurveyStation stn = leg.getFromStn();
1057 leg.setFromStn( leg.getToStn() );
1058 leg.setToStn( stn );
1059 double compass = leg.getCompass();
1060 if ( compass > 180 ) {
1061 compass -= 180;
1062 }
1063 else {
1064 compass += 180;
1065 }
1066 leg.setCompass( compass );
1067 leg.setClino( -leg.getClino() );
1068 //Put leg at start of series
1069 legs.add(0, leg);
1070 }
1071 }
1072 */
1073
1074 }