Skip to content

Commit

Permalink
Minor cleanup - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgenherje committed Mar 8, 2024
1 parent 6a10b6f commit 0ed0d81
Show file tree
Hide file tree
Showing 12 changed files with 275 additions and 378 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ void RicPointTangentManipulatorPartMgr::originAndTangent( cvf::Vec3d* origin, cv
//--------------------------------------------------------------------------------------------------
void RicPointTangentManipulatorPartMgr::setPolyline( const std::vector<cvf::Vec3d>& polyline )
{
m_polylineUtm = polyline;
m_polyline = polyline;
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -192,10 +192,10 @@ void RicPointTangentManipulatorPartMgr::updateManipulatorFromRay( const cvf::Ray
double closestDistance = std::numeric_limits<double>::max();
cvf::Vec3d closestPoint;

for ( size_t i = 1; i < m_polylineUtm.size(); i++ )
for ( size_t i = 1; i < m_polyline.size(); i++ )
{
const auto& p1 = m_polylineUtm[i];
const auto& p2 = m_polylineUtm[i - 1];
const auto& p1 = m_polyline[i];
const auto& p2 = m_polyline[i - 1];

double normalizedIntersection;
const auto pointOnLine = cvf::GeometryTools::projectPointOnLine( p1, p2, newOrigin, &normalizedIntersection );
Expand Down Expand Up @@ -256,7 +256,7 @@ void RicPointTangentManipulatorPartMgr::endManipulator()
//--------------------------------------------------------------------------------------------------
void RicPointTangentManipulatorPartMgr::recreateAllGeometryAndParts()
{
if ( m_polylineUtm.empty() )
if ( m_polyline.empty() )
{
createHorizontalPlaneHandle();
createVerticalAxisHandle();
Expand All @@ -272,7 +272,7 @@ void RicPointTangentManipulatorPartMgr::recreateAllGeometryAndParts()
//--------------------------------------------------------------------------------------------------
void RicPointTangentManipulatorPartMgr::createGeometryOnly()
{
if ( m_polylineUtm.empty() )
if ( m_polyline.empty() )
{
m_handleParts[HandleType::HORIZONTAL_PLANE]->setDrawable( createHorizontalPlaneGeo().p() );
m_handleParts[HandleType::VERTICAL_AXIS]->setDrawable( createVerticalAxisGeo().p() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class RicPointTangentManipulatorPartMgr : public cvf::Object
double m_handleSize;
bool m_isGeometryUpdateNeeded;

std::vector<cvf::Vec3d> m_polylineUtm;
std::vector<cvf::Vec3d> m_polyline;

HandleType m_activeHandle;
cvf::Vec3d m_initialPickPoint;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,10 @@ void PolygonVertexWelder::reserveVertices( cvf::uint vertexCount )
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<cvf::uint> PolygonVertexWelder::weldVerticesAndGetIndices( const std::vector<cvf::Vec3f>& vertices )
{
return {};
}

/// Add a vertex to the welder. If the vertex is within the tolerance of an existing vertex, the existing
// vertex index is returned
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::uint PolygonVertexWelder::weldVertexAndGetIndex( const cvf::Vec3f& vertex )
cvf::uint PolygonVertexWelder::weldVertexAndGetIndex( const cvf::Vec3d& vertex )
{
// Call function to step through linked list of bucket, testing
// if vertex is within the epsilon of one of the vertices in the bucket
Expand All @@ -69,30 +62,30 @@ cvf::uint PolygonVertexWelder::weldVertexAndGetIndex( const cvf::Vec3f& vertex )
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
const cvf::Vec3f& PolygonVertexWelder::vertex( cvf::uint index ) const
const cvf::Vec3d& PolygonVertexWelder::vertex( cvf::uint index ) const
{
return m_vertex[index];
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::ref<cvf::Vec3fArray> PolygonVertexWelder::createVertexArray() const
cvf::ref<cvf::Vec3dArray> PolygonVertexWelder::createVertexArray() const
{
cvf::ref<cvf::Vec3fArray> vertexArray = new cvf::Vec3fArray( m_vertex );
cvf::ref<cvf::Vec3dArray> vertexArray = new cvf::Vec3dArray( m_vertex );
return vertexArray;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::uint PolygonVertexWelder::locateVertexInPolygon( const cvf::Vec3f& vertex ) const
cvf::uint PolygonVertexWelder::locateVertexInPolygon( const cvf::Vec3d& vertex ) const
{
cvf::uint currentIndex = m_first;
while ( currentIndex != cvf::UNDEFINED_UINT )
{
// Weld point within tolerance
float distanceSquared = ( m_vertex[currentIndex] - vertex ).lengthSquared();
const auto distanceSquared = ( m_vertex[currentIndex] - vertex ).lengthSquared();
if ( distanceSquared < m_epsilonSquared )
{
return currentIndex;
Expand All @@ -107,7 +100,7 @@ cvf::uint PolygonVertexWelder::locateVertexInPolygon( const cvf::Vec3f& vertex )
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::uint PolygonVertexWelder::addVertexToPolygon( const cvf::Vec3f& vertex )
cvf::uint PolygonVertexWelder::addVertexToPolygon( const cvf::Vec3d& vertex )
{
// Add vertex and update linked list
m_vertex.push_back( vertex );
Expand All @@ -131,7 +124,7 @@ cvf::uint PolygonVertexWelder::addVertexToPolygon( const cvf::Vec3f& vertex )
//--------------------------------------------------------------------------------------------------

RivEnclosingPolygonGenerator::RivEnclosingPolygonGenerator()
: m_polygonVertexWelder( 1e-6 )
: m_polygonVertexWelder( 1e-3 )
{
}

Expand Down Expand Up @@ -169,6 +162,7 @@ void RivEnclosingPolygonGenerator::constructEnclosingPolygon()
for ( const auto& edgeKey : m_allEdgeKeys )
{
// If edge is already in the set, it occurs more than once and is not a boundary edge
// Assuming no degenerate triangles, i.e. triangle with two or more vertices at the same position
if ( edgeKeysAndCount.contains( edgeKey ) )
{
edgeKeysAndCount[edgeKey]++;
Expand Down Expand Up @@ -255,7 +249,7 @@ cvf::EdgeKey RivEnclosingPolygonGenerator::findNextEdge( cvf::uint vertexIndex,
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<cvf::Vec3f> RivEnclosingPolygonGenerator::getPolygonVertices() const
std::vector<cvf::Vec3d> RivEnclosingPolygonGenerator::getPolygonVertices() const
{
return m_polygonVertices;
}
Expand All @@ -269,14 +263,22 @@ bool RivEnclosingPolygonGenerator::isValidPolygon() const
}

//--------------------------------------------------------------------------------------------------
/// Add triangle vertices to the polygon. The vertices are welded to prevent duplicated vertices
///
/// Assumes that the vertices are given in counter-clockwise order
//--------------------------------------------------------------------------------------------------
void RivEnclosingPolygonGenerator::addTriangleVertices( const cvf::Vec3f& p0, const cvf::Vec3f& p1, const cvf::Vec3f& p2 )
void RivEnclosingPolygonGenerator::addTriangleVertices( const cvf::Vec3d& p0, const cvf::Vec3d& p1, const cvf::Vec3d& p2 )
{
cvf::uint i0 = m_polygonVertexWelder.weldVertexAndGetIndex( p0 );
cvf::uint i1 = m_polygonVertexWelder.weldVertexAndGetIndex( p1 );
cvf::uint i2 = m_polygonVertexWelder.weldVertexAndGetIndex( p2 );

// Verify three unique vertices - i.e. no degenerate triangle
if ( i0 == i1 || i0 == i2 || i1 == i2 )
{
return;
}

// Add edges keys to list of all edges
m_allEdgeKeys.emplace_back( cvf::EdgeKey( i0, i1 ).toKeyVal() );
m_allEdgeKeys.emplace_back( cvf::EdgeKey( i1, i2 ).toKeyVal() );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,21 @@ class PolygonVertexWelder

void reserveVertices( cvf::uint vertexCount );

// Add a vertex to the welder. If the vertex is within the tolerance of an existing vertex, the existing vertex index is returned
// Size of returned index array is equal size of input array
std::vector<cvf::uint> weldVerticesAndGetIndices( const std::vector<cvf::Vec3f>& vertices ); // TODO: Remove?
cvf::uint weldVertexAndGetIndex( const cvf::Vec3f& vertex );
cvf::uint weldVertexAndGetIndex( const cvf::Vec3d& vertex );

const cvf::Vec3f& vertex( cvf::uint index ) const;
cvf::ref<cvf::Vec3fArray> createVertexArray() const;
const cvf::Vec3d& vertex( cvf::uint index ) const;
cvf::ref<cvf::Vec3dArray> createVertexArray() const;

private:
cvf::uint locateVertexInPolygon( const cvf::Vec3f& vertex ) const;
cvf::uint addVertexToPolygon( const cvf::Vec3f& vertex );
cvf::uint locateVertexInPolygon( const cvf::Vec3d& vertex ) const;
cvf::uint addVertexToPolygon( const cvf::Vec3d& vertex );

private:
const double m_epsilonSquared; // Tolerance for vertex welding, radius around vertex defining welding neighborhood

cvf::uint m_first; // Start of linked list
std::vector<cvf::uint> m_next; // Links each vertex to next in linked list. Always numVertices long, will grow as vertices are added
std::vector<cvf::Vec3f> m_vertex; // Unique vertices within tolerance
std::vector<cvf::Vec3d> m_vertex; // Unique vertices within tolerance
};

/*
Expand All @@ -70,11 +67,11 @@ class RivEnclosingPolygonGenerator
public:
RivEnclosingPolygonGenerator();

std::vector<cvf::Vec3f> getPolygonVertices() const;
std::vector<cvf::Vec3d> getPolygonVertices() const;

bool isValidPolygon() const;

void addTriangleVertices( const cvf::Vec3f& p0, const cvf::Vec3f& p1, const cvf::Vec3f& p2 );
void addTriangleVertices( const cvf::Vec3d& p0, const cvf::Vec3d& p1, const cvf::Vec3d& p2 );
void constructEnclosingPolygon();

private:
Expand All @@ -83,5 +80,5 @@ class RivEnclosingPolygonGenerator
private:
PolygonVertexWelder m_polygonVertexWelder; // Add and weld vertices for a polygon, provides vertex index
std::vector<cvf::int64> m_allEdgeKeys; // Create edge defined by vertex indices when adding triangle. Using cvf::EdgeKey::toKeyVal()
std::vector<cvf::Vec3f> m_polygonVertices; // List polygon vertices counter clock-wise
std::vector<cvf::Vec3d> m_polygonVertices; // List polygon vertices counter clock-wise
};
Loading

0 comments on commit 0ed0d81

Please sign in to comment.