Revision 48

View differences:

org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsAlgorithmUtils.java
458 458
        }
459 459
    }
460 460

  
461
    static protected Double calculateM(Double maxValue, Double minValue,
462
        Double relativeDistance, Double totalLength) {
463
        // mValue=((newMax-newMin)*(mCoordinate-oldMin)/(oldMax-oldMin))+newMin;
464
        if (totalLength.equals(Double.valueOf(0)))
465
            return Double.POSITIVE_INFINITY;
466
        return ((maxValue - minValue) * (relativeDistance) / (totalLength))
467
            + minValue;
461
    /**
462
     * Reduced versi?n of straight line through two points equation to calculate M's
463
     *
464
     * @param totalLength
465
     * @param minValue
466
     * @param maxValue
467
     * @param relativeDistance
468
     * @return
469
     */
470
    static protected double calculateM(Double totalLength, Double minValue, Double maxValue, Double relativeDistance) {
471
        return LrsAlgorithmUtils.straightLineThroughTwoPointsEquation(0, totalLength, minValue, maxValue, relativeDistance);
468 472
    }
469 473

  
470
    static protected double calculateNewM(double oldMax, double oldMin, double newMax, double newMin, double mCoordinate){
471
        double result;
472
        if (oldMax==oldMin){
474
    /**
475
     * Same as straightLineThroughTwoPointsEquation but with more human readable arguments
476
     *
477
     * @param oldMin
478
     * @param oldMax
479
     * @param newMin
480
     * @param newMax
481
     * @param m
482
     * @return double
483
     *
484
     * @deprecated Use {@link #straightLineThroughTwoPointsEquation()} instead.
485
     */
486
    @Deprecated
487
    static protected double calculateNewM(double oldMin, double oldMax, double newMin, double newMax, double m){
488
        //TO REMOVE
489
        return straightLineThroughTwoPointsEquation(oldMin, oldMax, newMin, newMax, m);
490
    }
491

  
492

  
493
    /**
494
     * Straight line through two points equation.
495
     *
496
     * @param x1
497
     * @param x2
498
     * @param y1
499
     * @param y2
500
     * @param x
501
     * @return
502
     */
503
    static protected double straightLineThroughTwoPointsEquation(double x1, double x2, double y1, double y2, double x) {
504
        if (x2 - x1 == 0.0) {
473 505
            return Double.POSITIVE_INFINITY;
474 506
        }
475
        result=((newMax-newMin)*(mCoordinate-oldMin)/(oldMax-oldMin))+newMin;
476

  
477
        return result;
507
        return ((y2 - y1) * (x - x1) / (x2 - x1)) + y1;
478 508
    }
479 509

  
480 510
}
org.gvsig.lrs/trunk/org.gvsig.lrs/org.gvsig.lrs.lib/org.gvsig.lrs.lib.impl/src/main/java/org/gvsig/lrs/lib/impl/LrsCreateRouteAlgorithm.java
280 280
                            Point fork = getPossibleFork(auxMultiLine, vertex);
281 281
                            if(fork!=null){
282 282
                                previousPoint = fork;
283
                                // OJO, aqu? previousPoint pasa a ser 2DM
283 284
                            }
284
                            // OJO, aqu? previousPoint pasa a ser 2DM
285 285
                        } else {
286 286
                            // En caso de salto, calculamos el previousPoint
287 287
                            // buscando el v?rtice m?s cercano
......
298 298
                            if(areInSame2DLocation(previousPoint,previousVertex)){
299 299
                                Point nextVertex = getNextVertexToPoint(auxMultiLine, previousPoint);
300 300
                                double distanceToNextPoint = nextVertex.distance(previousPoint);
301
                                previousM = strightLineThroughTwoPointsEquation(0, distanceToNextPoint,
301
                                previousM = LrsAlgorithmUtils.straightLineThroughTwoPointsEquation(0, distanceToNextPoint,
302 302
                                    previousM, nextVertex.getCoordinateAt(previousVertex.getDimension() - 1),
303 303
                                        previousPoint.distance(point));
304 304
                            } else {
305 305
                                double distanceToPreviousPoint = previousVertex.distance(previousPoint);
306
                                previousM = strightLineThroughTwoPointsEquation(0, distanceToPreviousPoint,
306
                                previousM = LrsAlgorithmUtils.straightLineThroughTwoPointsEquation(0, distanceToPreviousPoint,
307 307
                                        previousVertex.getCoordinateAt(previousVertex.getDimension() - 1), previousM,
308 308
                                        distanceToPreviousPoint + previousPoint.distance(point));
309 309
                            }
......
314 314
                        distance += previousPoint.distance(vertex);
315 315
                    }
316 316
                    double m =
317
                        strightLineThroughTwoPointsEquation(0, geometryLength, firstM, firstM + mSegment.fromValue,
317
                        LrsAlgorithmUtils.straightLineThroughTwoPointsEquation(0, geometryLength, firstM, firstM + mSegment.fromValue,
318 318
                            distance);
319 319
                    point.setCoordinateAt(point.getDimension() - 1, m);
320 320
                    auxLine.addVertex(point);
......
342 342
                                // OJO, aqu? previousPoint pasa a ser 2DM
343 343
                            } else {
344 344
                                // En caso de salto, calculamos el previousPoint
345
                                // buscando el v?rtice m?s cercano
345 346
                                previousPoint = getClosestVertex(auxMultiLine, vertex);
346 347
                                // OJO, aqu? previousPoint pasa a ser 2DM
347 348
                                gap = true;
......
354 355
                                Point previousVertex = getPreviousVertexToPoint(auxMultiLine, previousPoint);
355 356
                                previousVertex.getCoordinateAt(previousVertex.getDimension() - 1);
356 357
                                previousM =
357
                                    strightLineThroughTwoPointsEquation(0, previousVertex.distance(previousPoint),
358
                                    LrsAlgorithmUtils.straightLineThroughTwoPointsEquation(0, previousVertex.distance(previousPoint),
358 359
                                        previousVertex.getCoordinateAt(previousVertex.getDimension() - 1), previousM,
359 360
                                        previousVertex.distance(previousPoint) + previousPoint.distance(point));
360 361
                                firstM = previousM;
......
364 365
                            distance += previousPoint.distance(vertex);
365 366
                        }
366 367
                        double m =
367
                            strightLineThroughTwoPointsEquation(0, geometryLength, firstM, firstM + mSegment.fromValue,
368
                            LrsAlgorithmUtils.straightLineThroughTwoPointsEquation(0, geometryLength, firstM, firstM + mSegment.fromValue,
368 369
                                distance);
369 370
                        point.setCoordinateAt(point.getDimension() - 1, m);
370 371
                        auxLine.addVertex(point);
......
604 605
            Double distance = Double.valueOf(0);
605 606
            for (Line line : geometryLines) {
606 607
                Double lineLength = getLineLength(line);
607
                Double mFirstPoint = calculateM(geometryMLastPoint, geometryMFirstPoint, distance, geometryLength);
608
                Double mFirstPoint = LrsAlgorithmUtils.calculateM(geometryLength, geometryMFirstPoint, geometryMLastPoint, distance);
608 609
                distance += lineLength;
609
                Double mLastPoint = calculateM(geometryMLastPoint, geometryMFirstPoint, distance, geometryLength);
610
                Double mLastPoint = LrsAlgorithmUtils.calculateM(geometryLength, geometryMFirstPoint, geometryMLastPoint, distance);
610 611
                Line mLine = lineToMLine(line, mFirstPoint, mLastPoint);
611 612
                mGeometry.addPrimitive(mLine);
612 613
            }
......
646 647
            else {
647 648
                Point previousVertex = line.getVertex(i - 1);
648 649
                inLineDistance += vertex.distance(previousVertex);
649
                mValue = calculateM(maxMValue, minMValue, inLineDistance, lineLength);
650
                mValue = LrsAlgorithmUtils.calculateM(lineLength, minMValue, maxMValue, inLineDistance);
650 651
            }
651 652

  
652 653
            mVertex.setCoordinateAt(mVertex.getDimension() - 1, mValue);
......
655 656
        return lineM;
656 657
    }
657 658

  
658
    /**
659
     * Reduced versi?n of stright line through two points equation to calculate M's
660
     *
661
     * @param maxValue
662
     * @param minValue
663
     * @param relativeDistance
664
     * @param totalLength
665
     * @return
666
     */
667
    private Double calculateM(Double maxValue, Double minValue, Double relativeDistance, Double totalLength) {
668
        if (totalLength.equals(Double.valueOf(0)))
669
            return Double.POSITIVE_INFINITY;
670
        return ((maxValue - minValue) * (relativeDistance) / (totalLength)) + minValue;
671
    }
672 659

  
673 660
    /**
674
     * Stright line through two points equation.
675
     *
676
     * @param x1
677
     * @param x2
678
     * @param y1
679
     * @param y2
680
     * @param x
681
     * @return
682
     */
683
    private double strightLineThroughTwoPointsEquation(double x1, double x2, double y1, double y2, double x) {
684
        if (x2 - x1 == 0.0) {
685
            return Double.POSITIVE_INFINITY;
686
        }
687
        return ((y2 - y1) * (x - x1) / (x2 - x1)) + y1;
688
    }
689

  
690
    /**
691 661
     * Simplify the multilines in mList calling simplifyMultiline method
692 662
     *
693 663
     * @param mList

Also available in: Unified diff