Skip to content

Commit

Permalink
Merge pull request #105 from occamlabs/linearization-test-fixes
Browse files Browse the repository at this point in the history
Fixed double comparison problems
  • Loading branch information
hwbllmnn committed Apr 30, 2013
2 parents a5d0157 + 41c4513 commit 146489d
Showing 1 changed file with 69 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@
import static org.slf4j.LoggerFactory.getLogger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import junit.framework.Assert;

import org.deegree.cs.persistence.CRSManager;
import org.deegree.geometry.Geometry;
import org.deegree.geometry.io.WKTReader;
import org.deegree.geometry.points.Points;
import org.deegree.geometry.primitive.Curve;
import org.deegree.geometry.primitive.Point;
Expand All @@ -55,6 +57,7 @@
import org.deegree.geometry.standard.points.PointsList;
import org.deegree.geometry.standard.primitive.DefaultCurve;
import org.deegree.geometry.standard.primitive.DefaultPoint;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
Expand All @@ -64,6 +67,7 @@
import com.vividsolutions.jts.geom.GeometryFactory;
import com.vividsolutions.jts.geom.LineString;
import com.vividsolutions.jts.geom.impl.CoordinateArraySequence;
import com.vividsolutions.jts.io.ParseException;

/**
* Tests for {@link CurveLinearizer}.
Expand Down Expand Up @@ -132,7 +136,7 @@ private void testLinearizationWithErrorCriterion( Point p0, Point p1, Point p2,
Points positions = linearizer.linearize( arc, new MaxErrorCriterion( error, maxNumPoints ) ).getControlPoints();

Point start = null;
for ( Point point : positions ) {
for ( Point point : positions ) {
double dist = getDistance( center, point );
// every segment start/end point must be *on* arc
Assert.assertEquals( radius, dist, 0.00000001 );
Expand Down Expand Up @@ -301,10 +305,10 @@ public void testLinearizeCollinearArc() {
Points positions = createLinearArc( p0, p1, p2, false );

Assert.assertEquals( 2, positions.size() );
Assert.assertEquals( p0[0], positions.get( 0 ).get0() );
Assert.assertEquals( p0[1], positions.get( 0 ).get1() );
Assert.assertEquals( p2[0], positions.get( 1 ).get0() );
Assert.assertEquals( p2[1], positions.get( 1 ).get1() );
Assert.assertEquals( p0[0], positions.get( 0 ).get0(), 1.0E-9 );
Assert.assertEquals( p0[1], positions.get( 0 ).get1(), 1.0E-9 );
Assert.assertEquals( p2[0], positions.get( 1 ).get0(), 1.0E-9 );
Assert.assertEquals( p2[1], positions.get( 1 ).get1(), 1.0E-9 );
}

/**
Expand All @@ -320,12 +324,12 @@ public void testLinearizeCollinearCircle() {
Points positions = createLinearArc( p0, p1, p2, true );

Assert.assertEquals( 3, positions.size() );
Assert.assertEquals( p0[0], positions.get( 0 ).get0() );
Assert.assertEquals( p0[1], positions.get( 0 ).get1() );
Assert.assertEquals( p1[0], positions.get( 1 ).get0() );
Assert.assertEquals( p1[1], positions.get( 1 ).get1() );
Assert.assertEquals( p0[0], positions.get( 2 ).get0() );
Assert.assertEquals( p0[1], positions.get( 2 ).get1() );
Assert.assertEquals( p0[0], positions.get( 0 ).get0(), 1.0E-9 );
Assert.assertEquals( p0[1], positions.get( 0 ).get1(), 1.0E-9 );
Assert.assertEquals( p1[0], positions.get( 1 ).get0(), 1.0E-9 );
Assert.assertEquals( p1[1], positions.get( 1 ).get1(), 1.0E-9 );
Assert.assertEquals( p0[0], positions.get( 2 ).get0(), 1.0E-9 );
Assert.assertEquals( p0[1], positions.get( 2 ).get1(), 1.0E-9 );
}

/**
Expand Down Expand Up @@ -357,6 +361,18 @@ private Points createLinearArc( double[] first, double[] second, double[] third,
return output2;
}

private void testPoints( Points actual, String expected )
throws ParseException {
Iterator<Point> it = actual.iterator();
Geometry g = new WKTReader( CRSManager.getCRSRef( "EPSG:4326" ) ).read( expected );
Points ps = ( (Curve) g ).getControlPoints();
for ( Point p : ps ) {
Point act = it.next();
Assert.assertEquals( p.get0(), act.get0(), 1.0E-9 );
Assert.assertEquals( p.get1(), act.get1(), 1.0E-9 );
}
}

/**
* Test an arc over quadrants 3 and 4 <code>
* |
Expand All @@ -366,20 +382,23 @@ private Points createLinearArc( double[] first, double[] second, double[] third,
* 3 | 2
* |
* </code>
*
* @throws ParseException
*/
@Test
public void testVisualLinArcQDs34() {
public void testVisualLinArcQDs34()
throws ParseException {
double[] p0 = new double[] { -1.7, 2.5 };
double[] p1 = new double[] { -3, 0.5 };
double[] p2 = new double[] { -2.3, -2.5 };
Points positions = createLinearArc( p0, p1, p2, false );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (-1.7 2.5, -2.006268887232975 2.240298830592298, -2.280689527287858 1.9471458995298252, -2.5196312406291193 1.6244197210066154, -2.719932746135047 1.2763900734610205, -2.8789439859352446 0.9076615089371216, -2.994561186588522 0.5231124332760927, -3.0652546927297304 0.127830563127568, -3.0900892049368194 -0.272954386293051, -3.0687361540641627 -0.6739398921371922, -3.001478048325461 -1.069820778125759, -2.8892047356128234 -1.4553594038575817, -2.7334016305028315 -1.8254549605949406, -2.536130061710344 -2.1752109567191, -2.3 -2.5)" );
testPoints( positions,
"LINESTRING (-1.7 2.5, -2.006268887232975 2.240298830592298, -2.280689527287858 1.9471458995298252, -2.5196312406291193 1.6244197210066154, -2.719932746135047 1.2763900734610205, -2.8789439859352446 0.9076615089371216, -2.994561186588522 0.5231124332760927, -3.0652546927297304 0.127830563127568, -3.0900892049368194 -0.272954386293051, -3.0687361540641627 -0.6739398921371922, -3.001478048325461 -1.069820778125759, -2.8892047356128234 -1.4553594038575817, -2.7334016305028315 -1.8254549605949406, -2.536130061710344 -2.1752109567191, -2.3 -2.5)" );

// inverse
positions = createLinearArc( p2, p1, p0, false );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (-2.3 -2.5, -2.5361300617103426 -2.1752109567191003, -2.7334016305028297 -1.8254549605949428, -2.8892047356128225 -1.4553594038575826, -3.0014780483254597 -1.0698207781257598, -3.068736154064162 -0.6739398921371934, -3.0900892049368185 -0.2729543862930522, -3.06525469272973 0.1278305631275666, -2.9945611865885216 0.5231124332760911, -2.878943985935244 0.9076615089371203, -2.7199327461350467 1.276390073461019, -2.519631240629119 1.6244197210066136, -2.280689527287859 1.9471458995298225, -2.0062688872329755 2.240298830592297, -1.7 2.5)" );
testPoints( positions,
"LINESTRING (-2.3 -2.5, -2.5361300617103426 -2.1752109567191003, -2.7334016305028297 -1.8254549605949428, -2.8892047356128225 -1.4553594038575826, -3.0014780483254597 -1.0698207781257598, -3.068736154064162 -0.6739398921371934, -3.0900892049368185 -0.2729543862930522, -3.06525469272973 0.1278305631275666, -2.9945611865885216 0.5231124332760911, -2.878943985935244 0.9076615089371203, -2.7199327461350467 1.276390073461019, -2.519631240629119 1.6244197210066136, -2.280689527287859 1.9471458995298225, -2.0062688872329755 2.240298830592297, -1.7 2.5)" );
}

/**
Expand All @@ -391,20 +410,23 @@ public void testVisualLinArcQDs34() {
* 3 | 2
* |
* </code>
*
* @throws ParseException
*/
@Test
public void testVisualLinArcQDs12() {
public void testVisualLinArcQDs12()
throws ParseException {
double[] p0 = new double[] { 1.7, 2.5 };
double[] p1 = new double[] { 3, 0.5 };
double[] p2 = new double[] { 2.3, -2.5 };
Points positions = createLinearArc( p0, p1, p2, false );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (1.7 2.5, 2.0062688872329746 2.2402988305922986, 2.2806895272878585 1.9471458995298248, 2.5196312406291193 1.6244197210066147, 2.7199327461350467 1.2763900734610203, 2.878943985935245 0.9076615089371205, 2.994561186588522 0.5231124332760916, 3.0652546927297304 0.1278305631275672, 3.090089204936819 -0.2729543862930513, 3.0687361540641622 -0.6739398921371922, 3.00147804832546 -1.0698207781257603, 2.8892047356128225 -1.4553594038575823, 2.73340163050283 -1.8254549605949426, 2.5361300617103426 -2.175210956719101, 2.3 -2.5)" );
testPoints( positions,
"LINESTRING (1.7 2.5, 2.0062688872329746 2.2402988305922986, 2.2806895272878585 1.9471458995298248, 2.5196312406291193 1.6244197210066147, 2.7199327461350467 1.2763900734610203, 2.878943985935245 0.9076615089371205, 2.994561186588522 0.5231124332760916, 3.0652546927297304 0.1278305631275672, 3.090089204936819 -0.2729543862930513, 3.0687361540641622 -0.6739398921371922, 3.00147804832546 -1.0698207781257603, 2.8892047356128225 -1.4553594038575823, 2.73340163050283 -1.8254549605949426, 2.5361300617103426 -2.175210956719101, 2.3 -2.5)" );

// inverse
positions = createLinearArc( p2, p1, p0, false );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (2.3 -2.5, 2.536130061710342 -2.175210956719101, 2.73340163050283 -1.8254549605949422, 2.8892047356128225 -1.4553594038575821, 3.0014780483254597 -1.0698207781257598, 3.068736154064162 -0.6739398921371922, 3.0900892049368185 -0.2729543862930511, 3.0652546927297295 0.1278305631275674, 2.994561186588521 0.5231124332760915, 2.878943985935244 0.9076615089371205, 2.7199327461350458 1.2763900734610198, 2.5196312406291184 1.6244197210066145, 2.280689527287857 1.9471458995298243, 2.0062688872329737 2.240298830592298, 1.7 2.5)" );
testPoints( positions,
"LINESTRING (2.3 -2.5, 2.536130061710342 -2.175210956719101, 2.73340163050283 -1.8254549605949422, 2.8892047356128225 -1.4553594038575821, 3.0014780483254597 -1.0698207781257598, 3.068736154064162 -0.6739398921371922, 3.0900892049368185 -0.2729543862930511, 3.0652546927297295 0.1278305631275674, 2.994561186588521 0.5231124332760915, 2.878943985935244 0.9076615089371205, 2.7199327461350458 1.2763900734610198, 2.5196312406291184 1.6244197210066145, 2.280689527287857 1.9471458995298243, 2.0062688872329737 2.240298830592298, 1.7 2.5)" );

}

Expand All @@ -417,20 +439,23 @@ public void testVisualLinArcQDs12() {
* 3 | 2
* |
* </code>
*
* @throws ParseException
*/
@Test
public void testVisualLinArcQDs14() {
public void testVisualLinArcQDs14()
throws ParseException {
double[] p0 = new double[] { 1.7, 2.5 };
double[] p1 = new double[] { 1, 3.5 };
double[] p2 = new double[] { -2.3, 2.3 };
Points positions = createLinearArc( p0, p1, p2, false );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (1.6999999999999997 2.5, 1.5473395210137384 2.855706401706767, 1.331217640693683 3.1768350346662317, 1.0591681063567635 3.452191743185483, 0.740674235686257 3.672177925489757, 0.3868383397488122 3.829125129369394, 0.0099947098605335 3.9175623651477514, -0.3767203408881685 3.934406817763368, -0.7598263936166152 3.879071309973894, -1.1259688347495056 3.7534847706444783, -1.4623843816917867 3.562024994620855, -1.7573459954208674 3.311366038096601, -2.0005716708813686 3.0102455690874788, -2.183582855243771 2.6691602828938916, -2.3 2.3)" );
testPoints( positions,
"LINESTRING (1.6999999999999997 2.5, 1.5473395210137384 2.855706401706767, 1.331217640693683 3.1768350346662317, 1.0591681063567635 3.452191743185483, 0.740674235686257 3.672177925489757, 0.3868383397488122 3.829125129369394, 0.0099947098605335 3.9175623651477514, -0.3767203408881685 3.934406817763368, -0.7598263936166152 3.879071309973894, -1.1259688347495056 3.7534847706444783, -1.4623843816917867 3.562024994620855, -1.7573459954208674 3.311366038096601, -2.0005716708813686 3.0102455690874788, -2.183582855243771 2.6691602828938916, -2.3 2.3)" );

// inverse
positions = createLinearArc( p2, p1, p0, false );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (-2.3 2.3, -2.1835828552437717 2.669160282893891, -2.0005716708813686 3.0102455690874796, -1.7573459954208674 3.311366038096602, -1.4623843816917872 3.562024994620855, -1.1259688347495054 3.7534847706444787, -0.7598263936166152 3.8790713099738943, -0.3767203408881679 3.934406817763368, 0.0099947098605339 3.9175623651477514, 0.3868383397488126 3.8291251293693946, 0.7406742356862576 3.6721779254897573, 1.0591681063567644 3.4521917431854825, 1.331217640693684 3.1768350346662317, 1.5473395210137393 2.8557064017067675, 1.7000000000000002 2.5)" );
testPoints( positions,
"LINESTRING (-2.3 2.3, -2.1835828552437717 2.669160282893891, -2.0005716708813686 3.0102455690874796, -1.7573459954208674 3.311366038096602, -1.4623843816917872 3.562024994620855, -1.1259688347495054 3.7534847706444787, -0.7598263936166152 3.8790713099738943, -0.3767203408881679 3.934406817763368, 0.0099947098605339 3.9175623651477514, 0.3868383397488126 3.8291251293693946, 0.7406742356862576 3.6721779254897573, 1.0591681063567644 3.4521917431854825, 1.331217640693684 3.1768350346662317, 1.5473395210137393 2.8557064017067675, 1.7000000000000002 2.5)" );

}

Expand All @@ -443,20 +468,23 @@ public void testVisualLinArcQDs14() {
* 3 | 2
* |
* </code>
*
* @throws ParseException
*/
@Test
public void testVisualLinArcQDs23() {
public void testVisualLinArcQDs23()
throws ParseException {
double[] p0 = new double[] { 2.3, -2.5 };
double[] p1 = new double[] { 1, -3.5 };
double[] p2 = new double[] { -2.3, -2.5 };
Points positions = createLinearArc( p0, p1, p2, false );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (2.3 -2.5, 2.056305411080411 -2.7960391691981443, 1.774800678026383 -3.0563876426647565, 1.4606619456235812 -3.2762582845717096, 1.1196654135971806 -3.451608241840547, 0.7580811272214822 -3.5792132817660605, 0.3825576873857044 -3.6567270773260736, 0.0000000000000004 -3.682724350073748, -0.3825576873857036 -3.6567270773260736, -0.7580811272214821 -3.5792132817660605, -1.1196654135971804 -3.4516082418405465, -1.460661945623581 -3.276258284571709, -1.774800678026382 -3.0563876426647565, -2.0563054110804115 -2.796039169198144, -2.3 -2.5)" );
testPoints( positions,
"LINESTRING (2.3 -2.5, 2.056305411080411 -2.7960391691981443, 1.774800678026383 -3.0563876426647565, 1.4606619456235812 -3.2762582845717096, 1.1196654135971806 -3.451608241840547, 0.7580811272214822 -3.5792132817660605, 0.3825576873857044 -3.6567270773260736, 0.0000000000000004 -3.682724350073748, -0.3825576873857036 -3.6567270773260736, -0.7580811272214821 -3.5792132817660605, -1.1196654135971804 -3.4516082418405465, -1.460661945623581 -3.276258284571709, -1.774800678026382 -3.0563876426647565, -2.0563054110804115 -2.796039169198144, -2.3 -2.5)" );

// inverse
positions = createLinearArc( p2, p1, p0, false );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (-2.3 -2.5, -2.0563054110804107 -2.7960391691981448, -1.7748006780263823 -3.0563876426647565, -1.4606619456235799 -3.2762582845717096, -1.1196654135971802 -3.451608241840547, -0.7580811272214817 -3.5792132817660605, -0.3825576873857038 -3.6567270773260736, 0.0000000000000002 -3.682724350073748, 0.3825576873857042 -3.6567270773260736, 0.7580811272214827 -3.57921328176606, 1.119665413597181 -3.4516082418405465, 1.460661945623581 -3.2762582845717096, 1.774800678026383 -3.056387642664756, 2.056305411080411 -2.796039169198144, 2.3 -2.5)" );
testPoints( positions,
"LINESTRING (-2.3 -2.5, -2.0563054110804107 -2.7960391691981448, -1.7748006780263823 -3.0563876426647565, -1.4606619456235799 -3.2762582845717096, -1.1196654135971802 -3.451608241840547, -0.7580811272214817 -3.5792132817660605, -0.3825576873857038 -3.6567270773260736, 0.0000000000000002 -3.682724350073748, 0.3825576873857042 -3.6567270773260736, 0.7580811272214827 -3.57921328176606, 1.119665413597181 -3.4516082418405465, 1.460661945623581 -3.2762582845717096, 1.774800678026383 -3.056387642664756, 2.056305411080411 -2.796039169198144, 2.3 -2.5)" );

}

Expand All @@ -469,21 +497,24 @@ public void testVisualLinArcQDs23() {
* 3 | 2
* |
* </code>
*
* @throws ParseException
*/
@Test
public void testLinearizeArcFourQDs14() {
public void testLinearizeArcFourQDs14()
throws ParseException {
double[] p0 = new double[] { 1.7, 2.5 };
double[] p1 = new double[] { 3, 0.5 };
double[] p2 = new double[] { -2.3, 2.5 };
Points positions = createLinearArc( p0, p1, p2, false );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (1.7 2.5, 2.527698122123212 1.62577746297488, 2.9962725289439467 0.5168197311917764, 3.046213267629344 -0.6860331892554772, 2.671177757448691 -1.8300164022933552, 1.9187963111049964 -2.7698415907229377, 0.8846229830504084 -3.386148943550589, -0.3 -3.600666107520471, -1.4846229830504085 -3.386148943550589, -2.5187963111049965 -2.7698415907229386, -3.271177757448691 -1.8300164022933556, -3.646213267629344 -0.6860331892554776, -3.5962725289439472 0.5168197311917756, -3.1276981221232143 1.6257774629748782, -2.3 2.5)" );
testPoints( positions,
"LINESTRING (1.7 2.5, 2.527698122123212 1.62577746297488, 2.9962725289439467 0.5168197311917764, 3.046213267629344 -0.6860331892554772, 2.671177757448691 -1.8300164022933552, 1.9187963111049964 -2.7698415907229377, 0.8846229830504084 -3.386148943550589, -0.3 -3.600666107520471, -1.4846229830504085 -3.386148943550589, -2.5187963111049965 -2.7698415907229386, -3.271177757448691 -1.8300164022933556, -3.646213267629344 -0.6860331892554776, -3.5962725289439472 0.5168197311917756, -3.1276981221232143 1.6257774629748782, -2.3 2.5)" );

// inverse
positions = createLinearArc( p2, p1, p0, false );
LOG.debug( exportToWKT( positions ) );
Assert.assertEquals( exportToWKT( positions ),
"LINESTRING (-2.3 2.5, -3.127698122123212 1.62577746297488, -3.5962725289439463 0.5168197311917766, -3.6462132676293435 -0.6860331892554763, -3.2711777574486907 -1.8300164022933543, -2.518796311104997 -2.769841590722937, -1.4846229830504076 -3.3861489435505883, -0.300000000000001 -3.6006661075204693, 0.8846229830504059 -3.386148943550589, 1.918796311104995 -2.769841590722937, 2.67117775744869 -1.8300164022933538, 3.0462132676293425 -0.6860331892554776, 2.9962725289439462 0.5168197311917739, 2.5276981221232138 1.6257774629748767, 1.7000000000000002 2.5)" );
testPoints( positions,
"LINESTRING (-2.3 2.5, -3.127698122123212 1.62577746297488, -3.5962725289439463 0.5168197311917766, -3.6462132676293435 -0.6860331892554763, -3.2711777574486907 -1.8300164022933543, -2.518796311104997 -2.769841590722937, -1.4846229830504076 -3.3861489435505883, -0.300000000000001 -3.6006661075204693, 0.8846229830504059 -3.386148943550589, 1.918796311104995 -2.769841590722937, 2.67117775744869 -1.8300164022933538, 3.0462132676293425 -0.6860331892554776, 2.9962725289439462 0.5168197311917739, 2.5276981221232138 1.6257774629748767, 1.7000000000000002 2.5)" );

}

Expand Down

0 comments on commit 146489d

Please sign in to comment.