Skip to content

Commit

Permalink
Fix projection of sphere
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 18, 2023
1 parent a64083a commit 7f95b6e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
40 changes: 31 additions & 9 deletions src/core/tiledmesh/qgstiledmeshboundingvolume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,20 +233,42 @@ QgsTiledMeshNodeBoundingVolumeSphere *QgsTiledMeshNodeBoundingVolumeSphere::clon

QgsAbstractGeometry *QgsTiledMeshNodeBoundingVolumeSphere::as2DGeometry( const QgsCoordinateTransform &transform, Qgis::TransformDirection direction ) const
{
std::unique_ptr< QgsCircularString > exterior( mSphere.toCircle().toCircularString() );
std::unique_ptr< QgsCurvePolygon > polygon = std::make_unique< QgsCurvePolygon >();
if ( transform.isValid() && !transform.isShortCircuited() )
{
exterior->addZValue( mSphere.centerZ() );
// have to segmentize prior to transforming
std::unique_ptr< QgsCurve > segmentized( exterior->segmentize( ) );
segmentized->transform( transform, direction, true );
segmentized->dropZValue();
polygon->setExteriorRing( segmentized.release() );
QgsVector3D normal = mSphere.centerVector();
normal.normalize();

QgsVector3D axis1 = QgsVector3D::crossProduct( normal, QgsVector3D( 1, 0, 0 ) );
if ( axis1.length() == 0 )
{
axis1 = QgsVector3D::crossProduct( normal, QgsVector3D( 0, 1, 0 ) );
}
axis1.normalize();
const QgsVector3D axis2 = QgsVector3D::crossProduct( normal, axis1 );
QVector< double > circleXInPlane;
QVector< double > circleYInPlane;
QVector< double > circleZInPlane;

for ( int i = 0; i < 48; ++i )
{
const double alpha = 2 * i / 48.0 * M_PI;
circleXInPlane.append( mSphere.centerX() + mSphere.radius() * ( axis1.x() * std::cos( alpha ) + axis2.x()* std::sin( alpha ) ) );
circleYInPlane.append( mSphere.centerY() + mSphere.radius() * ( axis1.y() * std::cos( alpha ) + axis2.y()* std::sin( alpha ) ) );
circleZInPlane.append( mSphere.centerZ() + mSphere.radius() * ( axis1.z() * std::cos( alpha ) + axis2.z()* std::sin( alpha ) ) );
}
transform.transformInPlace( circleXInPlane, circleYInPlane, circleZInPlane, direction );

std::unique_ptr< QgsLineString > exterior = std::make_unique< QgsLineString>( circleXInPlane, circleYInPlane );
exterior->close();
std::unique_ptr< QgsPolygon > polygon = std::make_unique< QgsPolygon >();
polygon->setExteriorRing( exterior.release() );
return polygon.release();
}
else
{
std::unique_ptr< QgsCurvePolygon > polygon = std::make_unique< QgsCurvePolygon >();
std::unique_ptr< QgsCircularString > exterior( mSphere.toCircle().toCircularString() );
polygon->setExteriorRing( exterior.release() );
return polygon.release();
}
return polygon.release();
}
4 changes: 2 additions & 2 deletions tests/src/python/test_qgscesiumtiledmeshlayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ def test_source_bounding_sphere(self):
self.assertAlmostEqual(layer.extent().xMaximum(),
149.5989376, 3)
self.assertAlmostEqual(layer.extent().yMinimum(),
-33.4298657, 3)
-33.4378807, 3)
self.assertAlmostEqual(layer.extent().yMaximum(),
-33.41016810, 3)
-33.402147, 3)

self.assertAlmostEqual(layer.dataProvider().boundingVolume().sphere().centerX(), -4595750.5786, 1)
self.assertAlmostEqual(layer.dataProvider().boundingVolume().sphere().centerY(), 2698725.128252, 1)
Expand Down
6 changes: 3 additions & 3 deletions tests/src/python/test_qgstiledmeshboundingvolume.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ def test_sphere(self):
bounds = volume.bounds(transform)
self.assertAlmostEqual(bounds.xMinimum(), 149.5562, 3)
self.assertAlmostEqual(bounds.xMaximum(), 149.5989, 3)
self.assertAlmostEqual(bounds.yMinimum(), -33.42986, 3)
self.assertAlmostEqual(bounds.yMaximum(), -33.41017, 3)
self.assertAlmostEqual(bounds.yMinimum(), -33.4378835, 3)
self.assertAlmostEqual(bounds.yMaximum(), -33.40215, 3)
self.assertAlmostEqual(bounds.zMinimum(), -393.63504, 3)
self.assertAlmostEqual(bounds.zMaximum(), 1790.72811, 3)

geometry_2d = volume.as2DGeometry(transform)
self.assertEqual(geometry_2d.asWkt(3),
'CurvePolygon ((149.559 -33.415, 149.559 -33.415, 149.559 -33.415, 149.559 -33.415, 149.559 -33.416, 149.558 -33.416, 149.558 -33.416, 149.558 -33.416, 149.558 -33.416, 149.558 -33.416, 149.558 -33.417, 149.558 -33.417, 149.557 -33.417, 149.557 -33.417, 149.557 -33.417, 149.557 -33.417, 149.557 -33.418, 149.557 -33.418, 149.557 -33.418, 149.557 -33.418, 149.557 -33.418, 149.557 -33.418, 149.557 -33.419, 149.556 -33.419, 149.556 -33.419, 149.556 -33.419, 149.556 -33.419, 149.556 -33.419, 149.556 -33.42, 149.556 -33.42, 149.556 -33.42, 149.556 -33.42, 149.556 -33.42, 149.556 -33.42, 149.556 -33.421, 149.556 -33.421, 149.556 -33.421, 149.556 -33.421, 149.556 -33.421, 149.557 -33.421, 149.557 -33.422, 149.557 -33.422, 149.557 -33.422, 149.557 -33.422, 149.557 -33.422, 149.557 -33.422, 149.557 -33.423, 149.557 -33.423, 149.557 -33.423, 149.557 -33.423, 149.558 -33.423, 149.558 -33.423, 149.558 -33.424, 149.558 -33.424, 149.558 -33.424, 149.558 -33.424, 149.558 -33.424, 149.559 -33.424, 149.559 -33.425, 149.559 -33.425, 149.559 -33.425, 149.559 -33.425, 149.559 -33.425, 149.56 -33.425, 149.56 -33.425, 149.56 -33.426, 149.56 -33.426, 149.56 -33.426, 149.561 -33.426, 149.561 -33.426, 149.561 -33.426, 149.561 -33.426, 149.562 -33.427, 149.562 -33.427, 149.562 -33.427, 149.562 -33.427, 149.563 -33.427, 149.563 -33.427, 149.563 -33.427, 149.564 -33.427, 149.564 -33.428, 149.564 -33.428, 149.564 -33.428, 149.565 -33.428, 149.565 -33.428, 149.565 -33.428, 149.566 -33.428, 149.566 -33.428, 149.566 -33.428, 149.566 -33.428, 149.567 -33.429, 149.567 -33.429, 149.567 -33.429, 149.568 -33.429, 149.568 -33.429, 149.568 -33.429, 149.569 -33.429, 149.569 -33.429, 149.569 -33.429, 149.57 -33.429, 149.57 -33.429, 149.571 -33.429, 149.571 -33.429, 149.571 -33.429, 149.572 -33.429, 149.572 -33.43, 149.572 -33.43, 149.573 -33.43, 149.573 -33.43, 149.573 -33.43, 149.574 -33.43, 149.574 -33.43, 149.574 -33.43, 149.575 -33.43, 149.575 -33.43, 149.576 -33.43, 149.576 -33.43, 149.576 -33.43, 149.577 -33.43, 149.577 -33.43, 149.577 -33.43, 149.578 -33.43, 149.578 -33.43, 149.579 -33.43, 149.579 -33.43, 149.579 -33.43, 149.58 -33.43, 149.58 -33.43, 149.58 -33.43, 149.581 -33.43, 149.581 -33.43, 149.582 -33.43, 149.582 -33.43, 149.582 -33.43, 149.583 -33.43, 149.583 -33.43, 149.583 -33.43, 149.584 -33.429, 149.584 -33.429, 149.584 -33.429, 149.585 -33.429, 149.585 -33.429, 149.585 -33.429, 149.586 -33.429, 149.586 -33.429, 149.586 -33.429, 149.587 -33.429, 149.587 -33.429, 149.587 -33.429, 149.588 -33.429, 149.588 -33.429, 149.588 -33.428, 149.589 -33.428, 149.589 -33.428, 149.589 -33.428, 149.59 -33.428, 149.59 -33.428, 149.59 -33.428, 149.591 -33.428, 149.591 -33.428, 149.591 -33.428, 149.591 -33.427, 149.592 -33.427, 149.592 -33.427, 149.592 -33.427, 149.593 -33.427, 149.593 -33.427, 149.593 -33.427, 149.593 -33.427, 149.594 -33.427, 149.594 -33.426, 149.594 -33.426, 149.594 -33.426, 149.595 -33.426, 149.595 -33.426, 149.595 -33.426, 149.595 -33.426, 149.595 -33.425, 149.596 -33.425, 149.596 -33.425, 149.596 -33.425, 149.596 -33.425, 149.596 -33.425, 149.597 -33.425, 149.597 -33.424, 149.597 -33.424, 149.597 -33.424, 149.597 -33.424, 149.597 -33.424, 149.597 -33.424, 149.598 -33.423, 149.598 -33.423, 149.598 -33.423, 149.598 -33.423, 149.598 -33.423, 149.598 -33.423, 149.598 -33.422, 149.598 -33.422, 149.598 -33.422, 149.599 -33.422, 149.599 -33.422, 149.599 -33.422, 149.599 -33.421, 149.599 -33.421, 149.599 -33.421, 149.599 -33.421, 149.599 -33.421, 149.599 -33.421, 149.599 -33.42, 149.599 -33.42, 149.599 -33.42, 149.599 -33.42, 149.599 -33.42, 149.599 -33.42, 149.599 -33.419, 149.599 -33.419, 149.599 -33.419, 149.599 -33.419, 149.599 -33.419, 149.599 -33.419, 149.599 -33.418, 149.599 -33.418, 149.598 -33.418, 149.598 -33.418, 149.598 -33.418, 149.598 -33.418, 149.598 -33.417, 149.598 -33.417, 149.598 -33.417, 149.598 -33.417, 149.598 -33.417, 149.598 -33.417, 149.597 -33.416, 149.597 -33.416, 149.597 -33.416, 149.597 -33.416, 149.597 -33.416, 149.597 -33.416, 149.597 -33.415, 149.596 -33.415, 149.596 -33.415, 149.596 -33.415, 149.596 -33.415, 149.596 -33.415, 149.595 -33.415, 149.595 -33.414, 149.595 -33.414, 149.595 -33.414, 149.595 -33.414, 149.594 -33.414, 149.594 -33.414, 149.594 -33.414, 149.594 -33.413, 149.593 -33.413, 149.593 -33.413, 149.593 -33.413, 149.593 -33.413, 149.592 -33.413, 149.592 -33.413, 149.592 -33.413, 149.591 -33.413, 149.591 -33.412, 149.591 -33.412, 149.591 -33.412, 149.59 -33.412, 149.59 -33.412, 149.59 -33.412, 149.589 -33.412, 149.589 -33.412, 149.589 -33.412, 149.588 -33.412, 149.588 -33.411, 149.588 -33.411, 149.587 -33.411, 149.587 -33.411, 149.587 -33.411, 149.586 -33.411, 149.586 -33.411, 149.586 -33.411, 149.585 -33.411, 149.585 -33.411, 149.585 -33.411, 149.584 -33.411, 149.584 -33.411, 149.584 -33.411, 149.583 -33.411, 149.583 -33.41, 149.583 -33.41, 149.582 -33.41, 149.582 -33.41, 149.581 -33.41, 149.581 -33.41, 149.581 -33.41, 149.58 -33.41, 149.58 -33.41, 149.58 -33.41, 149.579 -33.41, 149.579 -33.41, 149.579 -33.41, 149.578 -33.41, 149.578 -33.41, 149.577 -33.41, 149.577 -33.41, 149.577 -33.41, 149.576 -33.41, 149.576 -33.41, 149.576 -33.41, 149.575 -33.41, 149.575 -33.41, 149.574 -33.41, 149.574 -33.41, 149.574 -33.41, 149.573 -33.41, 149.573 -33.41, 149.573 -33.41, 149.572 -33.41, 149.572 -33.411, 149.572 -33.411, 149.571 -33.411, 149.571 -33.411, 149.57 -33.411, 149.57 -33.411, 149.57 -33.411, 149.569 -33.411, 149.569 -33.411, 149.569 -33.411, 149.568 -33.411, 149.568 -33.411, 149.568 -33.411, 149.567 -33.411, 149.567 -33.411, 149.567 -33.412, 149.566 -33.412, 149.566 -33.412, 149.566 -33.412, 149.566 -33.412, 149.565 -33.412, 149.565 -33.412, 149.565 -33.412, 149.564 -33.412, 149.564 -33.412, 149.564 -33.413, 149.563 -33.413, 149.563 -33.413, 149.563 -33.413, 149.563 -33.413, 149.562 -33.413, 149.562 -33.413, 149.562 -33.413, 149.562 -33.414, 149.561 -33.414, 149.561 -33.414, 149.561 -33.414, 149.561 -33.414, 149.56 -33.414, 149.56 -33.414, 149.56 -33.414, 149.56 -33.415, 149.56 -33.415, 149.559 -33.415, 149.559 -33.415))')
'Polygon ((149.592 -33.433, 149.594 -33.431, 149.596 -33.429, 149.597 -33.427, 149.598 -33.425, 149.599 -33.423, 149.599 -33.421, 149.599 -33.418, 149.598 -33.416, 149.598 -33.414, 149.596 -33.412, 149.595 -33.41, 149.593 -33.408, 149.591 -33.406, 149.589 -33.405, 149.586 -33.404, 149.584 -33.403, 149.581 -33.402, 149.578 -33.402, 149.576 -33.402, 149.573 -33.403, 149.57 -33.403, 149.568 -33.404, 149.565 -33.405, 149.563 -33.407, 149.561 -33.409, 149.56 -33.411, 149.558 -33.413, 149.557 -33.415, 149.557 -33.417, 149.556 -33.419, 149.556 -33.422, 149.557 -33.424, 149.558 -33.426, 149.559 -33.428, 149.56 -33.43, 149.562 -33.432, 149.564 -33.434, 149.566 -33.435, 149.569 -33.436, 149.571 -33.437, 149.574 -33.438, 149.577 -33.438, 149.58 -33.438, 149.582 -33.437, 149.585 -33.437, 149.588 -33.436, 149.59 -33.435, 149.592 -33.433))')


if __name__ == '__main__':
Expand Down

0 comments on commit 7f95b6e

Please sign in to comment.