Skip to content

Commit 8412a87

Browse files
committed
Improvements to tessellator unit test + death to qIsNaN()
1 parent 2c24690 commit 8412a87

File tree

3 files changed

+61
-9
lines changed

3 files changed

+61
-9
lines changed

src/3d/qgscameracontroller.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ void QgsCameraController::frameTriggered( float dt )
236236
mCameraData.y -= p2.y() - p1.y();
237237
}
238238

239-
if ( qIsNaN( mCameraData.x ) || qIsNaN( mCameraData.y ) )
239+
if ( std::isnan( mCameraData.x ) || std::isnan( mCameraData.y ) )
240240
{
241241
// something went horribly wrong but we need to at least try to fix it somehow
242242
qDebug() << "camera position got NaN!";

src/3d/qgstessellator.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ static QVector3D _calculateNormal( const QgsCurve *curve, bool &hasValidZ )
127127
curve->pointAt( i, pt1, vt );
128128
curve->pointAt( i + 1, pt2, vt );
129129

130-
if ( qIsNaN( pt1.z() ) || qIsNaN( pt2.z() ) )
130+
if ( std::isnan( pt1.z() ) || std::isnan( pt2.z() ) )
131131
continue;
132132

133133
hasValidZ = true;
@@ -219,7 +219,7 @@ void QgsTessellator::addPolygon( const QgsPolygonV2 &polygon, float extrusionHei
219219
}
220220

221221
const QgsPoint ptFirst( exterior->startPoint() );
222-
QVector3D pOrigin( ptFirst.x(), ptFirst.y(), qIsNaN( ptFirst.z() ) ? 0 : ptFirst.z() );
222+
QVector3D pOrigin( ptFirst.x(), ptFirst.y(), std::isnan( ptFirst.z() ) ? 0 : ptFirst.z() );
223223
QVector3D pXVector;
224224
// Here we define the two perpendicular vectors that define the local
225225
// 2D space on the plane. They will act as axis for which we will
@@ -242,7 +242,7 @@ void QgsTessellator::addPolygon( const QgsPolygonV2 &polygon, float extrusionHei
242242
for ( int i = 0; i < pCount - 1; ++i )
243243
{
244244
exterior->pointAt( i, pt, vt );
245-
QVector3D tempPt( pt.x(), pt.y(), ( qIsNaN( pt.z() ) ? 0 : pt.z() ) );
245+
QVector3D tempPt( pt.x(), pt.y(), ( std::isnan( pt.z() ) ? 0 : pt.z() ) );
246246
const float x = QVector3D::dotProduct( tempPt - pOrigin, pXVector );
247247
const float y = QVector3D::dotProduct( tempPt - pOrigin, pYVector );
248248

@@ -256,7 +256,7 @@ void QgsTessellator::addPolygon( const QgsPolygonV2 &polygon, float extrusionHei
256256
p2t::Point *pt2 = new p2t::Point( x, y );
257257
polyline.push_back( pt2 );
258258

259-
z[pt2] = qIsNaN( pt.z() ) ? 0 : pt.z();
259+
z[pt2] = std::isnan( pt.z() ) ? 0 : pt.z();
260260
}
261261
polylinesToDelete << polyline;
262262

@@ -271,7 +271,7 @@ void QgsTessellator::addPolygon( const QgsPolygonV2 &polygon, float extrusionHei
271271
QVector3D nPoint = pOrigin + pXVector * p->x + pYVector * p->y;
272272
const double fx = nPoint.x() - mOriginX;
273273
const double fy = nPoint.y() - mOriginY;
274-
const double fz = extrusionHeight + ( qIsNaN( zPt ) ? 0 : zPt );
274+
const double fz = extrusionHeight + ( std::isnan( zPt ) ? 0 : zPt );
275275
mData << fx << fz << -fy;
276276
if ( mAddNormals )
277277
mData << pNormal.x() << pNormal.z() << - pNormal.y();
@@ -290,7 +290,7 @@ void QgsTessellator::addPolygon( const QgsPolygonV2 &polygon, float extrusionHei
290290
for ( int j = 0; j < hole->numPoints() - 1; ++j )
291291
{
292292
hole->pointAt( j, pt, vt );
293-
QVector3D tempPt( pt.x(), pt.y(), ( qIsNaN( pt.z() ) ? 0 : pt.z() ) );
293+
QVector3D tempPt( pt.x(), pt.y(), ( std::isnan( pt.z() ) ? 0 : pt.z() ) );
294294

295295
const float x = QVector3D::dotProduct( tempPt - pOrigin, pXVector );
296296
const float y = QVector3D::dotProduct( tempPt - pOrigin, pYVector );
@@ -305,7 +305,7 @@ void QgsTessellator::addPolygon( const QgsPolygonV2 &polygon, float extrusionHei
305305
p2t::Point *pt2 = new p2t::Point( x, y );
306306
holePolyline.push_back( pt2 );
307307

308-
z[pt2] = qIsNaN( pt.z() ) ? 0 : pt.z();
308+
z[pt2] = std::isnan( pt.z() ) ? 0 : pt.z();
309309
}
310310
cdt->AddHole( holePolyline );
311311
polylinesToDelete << holePolyline;
@@ -326,7 +326,7 @@ void QgsTessellator::addPolygon( const QgsPolygonV2 &polygon, float extrusionHei
326326
QVector3D nPoint = pOrigin + pXVector * p->x + pYVector * p->y;
327327
float fx = nPoint.x() - mOriginX;
328328
float fy = nPoint.y() - mOriginY;
329-
float fz = extrusionHeight + ( qIsNaN( zPt ) ? 0 : zPt );
329+
float fz = extrusionHeight + ( std::isnan( zPt ) ? 0 : zPt );
330330
mData << fx << fz << -fy;
331331
if ( mAddNormals )
332332
mData << pNormal.x() << pNormal.z() << - pNormal.y();

tests/src/3d/testqgstessellator.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,56 @@
1515

1616
#include "qgstest.h"
1717

18+
#include <QVector3D>
19+
1820
#include "qgspoint.h"
1921
#include "qgspolygon.h"
2022
#include "qgstessellator.h"
2123

24+
/**
25+
* Simple structure to record an expected triangle from tessellator.
26+
* Triangle vertices are expected to be in counter-clockwise order.
27+
*/
28+
struct TriangleCoords
29+
{
30+
//! Constructs from expected triangle coordinates
31+
TriangleCoords( const QVector3D &a, const QVector3D &b, const QVector3D &c,
32+
const QVector3D &na = QVector3D(), const QVector3D &nb = QVector3D(), const QVector3D &nc = QVector3D() )
33+
{
34+
pts[0] = a; pts[1] = b; pts[2] = c;
35+
normals[0] = na; normals[1] = nb; normals[2] = nc;
36+
}
37+
38+
//! Constructs from tessellator output. Note: tessellator outputs (X,-Z,Y) tuples for (X,Y,Z) input coords
39+
TriangleCoords( const float *data, bool withNormal )
40+
{
41+
pts[0] = QVector3D( data[0], -data[2], data[1] );
42+
pts[1] = QVector3D( data[3], -data[5], data[4] );
43+
pts[2] = QVector3D( data[6], -data[8], data[7] );
44+
if ( withNormal )
45+
{
46+
data += 9;
47+
normals[0] = QVector3D( data[0], -data[2], data[1] );
48+
normals[1] = QVector3D( data[3], -data[5], data[4] );
49+
normals[2] = QVector3D( data[6], -data[8], data[7] );
50+
}
51+
else
52+
{
53+
normals[0] = normals[1] = normals[2] = QVector3D();
54+
}
55+
}
56+
57+
//! Compares two triangles
58+
bool operator==( const TriangleCoords &other ) const
59+
{
60+
// TODO: allow that the two triangles have coordinates shifted (but still in the same order)
61+
return pts[0] == other.pts[0] && pts[1] == other.pts[1] && pts[2] == other.pts[2] &&
62+
normals[0] == other.normals[0] && normals[1] == other.normals[1] && normals[2] == other.normals[2];
63+
}
64+
65+
QVector3D pts[3];
66+
QVector3D normals[3];
67+
};
2268

2369
/**
2470
* \ingroup UnitTests
@@ -60,8 +106,14 @@ void TestQgsTessellator::testBasic()
60106
QgsTessellator t( 0, 0, false );
61107
t.addPolygon( polygon, 0 );
62108

109+
TriangleCoords tcA( QVector3D( 1, 2, 0 ), QVector3D( 2, 1, 0 ), QVector3D( 3, 2, 0 ) );
110+
TriangleCoords tcB( QVector3D( 1, 2, 0 ), QVector3D( 1, 1, 0 ), QVector3D( 2, 1, 0 ) );
111+
63112
QVector<float> polygonData = t.data();
64113
QCOMPARE( polygonData.count(), 2 * 3 * 3 ); // two triangles (3 points with x/y/z coords)
114+
// TODO: allow arbitrary order of triangles in output
115+
QVERIFY( tcA == TriangleCoords( polygonData.constData(), false ) );
116+
QVERIFY( tcB == TriangleCoords( polygonData.constData() + 9, false ) );
65117
}
66118

67119

0 commit comments

Comments
 (0)