#815 Preliminary box calculations lacking some clipping

This commit is contained in:
Jacob Støren 2016-09-21 14:21:45 +02:00
parent a9d1f52005
commit 33296e4424
4 changed files with 261 additions and 24 deletions

View File

@ -20,12 +20,17 @@
#include "cvfDrawableGeo.h"
#include "cvfPrimitiveSetDirect.h"
#include "cvfPlane.h"
#include <array>
#include "RimIntersectionBox.h"
#include "cvfStructGrid.h"
#include "cafHexGridIntersectionTools/cafHexGridIntersectionTools.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RivIntersectionBoxGeometryGenerator::RivIntersectionBoxGeometryGenerator(const RimIntersectionBox* intersectionBox, const RivIntersectionHexGridInterface* grid)
: m_crossSection(intersectionBox),
: m_intersectionBoxDefinition(intersectionBox),
m_hexGrid(grid)
{
m_triangleVxes = new cvf::Vec3fArray;
@ -108,10 +113,221 @@ const std::vector<RivIntersectionVertexWeights>& RivIntersectionBoxGeometryGener
return m_triVxToCellCornerWeights;
}
class Box
{
public:
using FaceType = cvf::StructGridInterface::FaceType;
Box(const cvf::Mat4d& origin, const cvf::Vec3d& size) : m_origin(origin), m_size(size) {}
std::array<cvf::Plane, 6> planes()
{
std::array<cvf::Plane, 6> boxPlanes;
boxPlanes[FaceType::POS_I].setFromPointAndNormal(cvf::Vec3d(m_origin.col(0)), m_origin.translation() + m_size);
boxPlanes[FaceType::NEG_I].setFromPointAndNormal(-cvf::Vec3d(m_origin.col(0)), m_origin.translation());
boxPlanes[FaceType::POS_J].setFromPointAndNormal(cvf::Vec3d(m_origin.col(1)), m_origin.translation() + m_size);
boxPlanes[FaceType::NEG_J].setFromPointAndNormal(-cvf::Vec3d(m_origin.col(1)), m_origin.translation());
boxPlanes[FaceType::POS_K].setFromPointAndNormal(cvf::Vec3d(m_origin.col(2)), m_origin.translation() + m_size);
boxPlanes[FaceType::NEG_K].setFromPointAndNormal(-cvf::Vec3d(m_origin.col(2)), m_origin.translation());
return boxPlanes;
}
std::array<cvf::Vec3d, 4> faceCorners(FaceType face)
{
std::array<cvf::Vec3d, 4> corners;
cvf::Vec3d sx = cvf::Vec3d(m_origin.col(0)) * m_size[0] ;
cvf::Vec3d sy = cvf::Vec3d(m_origin.col(1)) * m_size[1] ;
cvf::Vec3d sz = cvf::Vec3d(m_origin.col(2)) * m_size[2] ;
switch(face)
{
case FaceType::POS_I:
corners[0] = m_origin.translation() + sx;
corners[1] = m_origin.translation() + sx + sy;
corners[2] = m_origin.translation() + sx + sy + sz;
corners[3] = m_origin.translation() + sx + sz;
break;
case FaceType::NEG_I:
corners[0] = m_origin.translation();
corners[1] = m_origin.translation() + sz;
corners[2] = m_origin.translation() + sy + sz;
corners[3] = m_origin.translation() + sy;
break;
case FaceType::POS_J:
corners[0] = m_origin.translation() + sy;
corners[1] = m_origin.translation() + sy + sz;
corners[2] = m_origin.translation() + sy + sx + sz;
corners[3] = m_origin.translation() + sy + sx;
break;
case FaceType::NEG_J:
corners[0] = m_origin.translation() ;
corners[1] = m_origin.translation() + sx;
corners[2] = m_origin.translation() + sx + sz;
corners[3] = m_origin.translation() + sz;
break;
case FaceType::POS_K:
corners[0] = m_origin.translation() + sz;
corners[1] = m_origin.translation() + sz + sx;
corners[2] = m_origin.translation() + sz + sx + sy;
corners[3] = m_origin.translation() + sz + sy;
break;
case FaceType::NEG_K:
corners[0] = m_origin.translation();
corners[1] = m_origin.translation() + sy;
corners[2] = m_origin.translation() + sx + sy;
corners[3] = m_origin.translation() + sx;
break;
}
return corners;
}
private:
cvf::Mat4d m_origin;
cvf::Vec3d m_size;
};
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RivIntersectionBoxGeometryGenerator::calculateArrays()
{
if(m_triangleVxes->size()) return;
std::vector<cvf::Vec3f> triangleVertices;
std::vector<cvf::Vec3f> cellBorderLineVxes;
cvf::Vec3d displayOffset = m_hexGrid->displayOffset();
cvf::BoundingBox gridBBox = m_hexGrid->boundingBox();
Box box(m_intersectionBoxDefinition->boxOrigin(), m_intersectionBoxDefinition->boxSize());
std::array<cvf::Plane, 6> boxPlanes = box.planes();
for (int faceIdx = 0; faceIdx < 6; ++faceIdx)
{
cvf::Plane plane = boxPlanes[faceIdx];
int clipPlaneIdx = faceIdx;
clipPlaneIdx++; if (clipPlaneIdx >= 6) clipPlaneIdx = 0; // Skip the opposite face
clipPlaneIdx++; if (clipPlaneIdx >= 6) clipPlaneIdx = 0;
cvf::Plane p1Plane = boxPlanes[faceIdx + 2];
clipPlaneIdx++; if (clipPlaneIdx >= 6) clipPlaneIdx = 0;
cvf::Plane p2Plane = boxPlanes[faceIdx + 3];
clipPlaneIdx++; if (clipPlaneIdx >= 6) clipPlaneIdx = 0;
cvf::Plane p3Plane = boxPlanes[faceIdx + 4];
clipPlaneIdx++; if (clipPlaneIdx >= 6) clipPlaneIdx = 0;
cvf::Plane p4Plane = boxPlanes[faceIdx + 5];
std::array<cvf::Vec3d, 4> faceCorners = box.faceCorners((Box::FaceType)faceIdx);
cvf::BoundingBox sideBBox;
for (cvf::Vec3d& corner : faceCorners) sideBBox.add(corner);
std::vector<size_t> columnCellCandidates;
m_hexGrid->findIntersectingCells(sideBBox, &columnCellCandidates);
// Same code as IntersectionGenerator :
std::vector<caf::HexGridIntersectionTools::ClipVx> hexPlaneCutTriangleVxes;
hexPlaneCutTriangleVxes.reserve(5*3);
std::vector<bool> isTriangleEdgeCellContour;
isTriangleEdgeCellContour.reserve(5*3);
cvf::Vec3d cellCorners[8];
size_t cornerIndices[8];
for(size_t cccIdx = 0; cccIdx < columnCellCandidates.size(); ++cccIdx)
{
size_t globalCellIdx = columnCellCandidates[cccIdx];
if(!m_hexGrid->useCell(globalCellIdx)) continue;
hexPlaneCutTriangleVxes.clear();
m_hexGrid->cellCornerVertices(globalCellIdx, cellCorners);
m_hexGrid->cellCornerIndices(globalCellIdx, cornerIndices);
caf::HexGridIntersectionTools::planeHexIntersectionMC(plane,
cellCorners,
cornerIndices,
&hexPlaneCutTriangleVxes,
&isTriangleEdgeCellContour);
std::vector<caf::HexGridIntersectionTools::ClipVx> clippedTriangleVxes;
std::vector<bool> isClippedTriEdgeCellContour;
caf::HexGridIntersectionTools::clipTrianglesBetweenTwoParallelPlanes(hexPlaneCutTriangleVxes, isTriangleEdgeCellContour, p1Plane, p2Plane,
&clippedTriangleVxes, &isClippedTriEdgeCellContour);
size_t clippedTriangleCount = clippedTriangleVxes.size()/3;
for(uint tIdx = 0; tIdx < clippedTriangleCount; ++tIdx)
{
uint triVxIdx = tIdx*3;
// Accumulate triangle vertices
cvf::Vec3f p0(clippedTriangleVxes[triVxIdx+0].vx - displayOffset);
cvf::Vec3f p1(clippedTriangleVxes[triVxIdx+1].vx - displayOffset);
cvf::Vec3f p2(clippedTriangleVxes[triVxIdx+2].vx - displayOffset);
triangleVertices.push_back(p0);
triangleVertices.push_back(p1);
triangleVertices.push_back(p2);
// Accumulate mesh lines
if(isClippedTriEdgeCellContour[triVxIdx])
{
cellBorderLineVxes.push_back(p0);
cellBorderLineVxes.push_back(p1);
}
if(isClippedTriEdgeCellContour[triVxIdx+1])
{
cellBorderLineVxes.push_back(p1);
cellBorderLineVxes.push_back(p2);
}
if(isClippedTriEdgeCellContour[triVxIdx+2])
{
cellBorderLineVxes.push_back(p2);
cellBorderLineVxes.push_back(p0);
}
// Mapping to cell index
m_triangleToCellIdxMap.push_back(globalCellIdx);
// Interpolation from nodes
for(int i = 0; i < 3; ++i)
{
caf::HexGridIntersectionTools::ClipVx cvx = clippedTriangleVxes[triVxIdx + i];
if(cvx.isVxIdsNative)
{
m_triVxToCellCornerWeights.push_back(
RivIntersectionVertexWeights(cvx.clippedEdgeVx1Id, cvx.clippedEdgeVx2Id, cvx.normDistFromEdgeVx1));
}
else
{
caf::HexGridIntersectionTools::ClipVx cvx1 = hexPlaneCutTriangleVxes[cvx.clippedEdgeVx1Id];
caf::HexGridIntersectionTools::ClipVx cvx2 = hexPlaneCutTriangleVxes[cvx.clippedEdgeVx2Id];
m_triVxToCellCornerWeights.push_back(
RivIntersectionVertexWeights(cvx1.clippedEdgeVx1Id, cvx1.clippedEdgeVx2Id, cvx1.normDistFromEdgeVx1,
cvx2.clippedEdgeVx1Id, cvx2.clippedEdgeVx2Id, cvx2.normDistFromEdgeVx1,
cvx.normDistFromEdgeVx1));
}
}
}
}
}
m_cellBorderLineVxes->assign(cellBorderLineVxes);
m_triangleVxes->assign(triangleVertices);
}

View File

@ -42,33 +42,33 @@ class RivIntersectionBoxGeometryGenerator : public cvf::Object
{
public:
RivIntersectionBoxGeometryGenerator(const RimIntersectionBox* intersectionBox,
const RivIntersectionHexGridInterface* grid);
const RivIntersectionHexGridInterface* grid);
~RivIntersectionBoxGeometryGenerator();
bool isAnyGeometryPresent() const;
// Generate geometry
cvf::ref<cvf::DrawableGeo> generateSurface();
cvf::ref<cvf::DrawableGeo> createMeshDrawable();
bool isAnyGeometryPresent() const;
// Generate geometry
cvf::ref<cvf::DrawableGeo> generateSurface();
cvf::ref<cvf::DrawableGeo> createMeshDrawable();
// Mapping between cells and geometry
const std::vector<size_t>& triangleToCellIndex() const;
const std::vector<size_t>& triangleToCellIndex() const;
const std::vector<RivIntersectionVertexWeights>& triangleVxToCellCornerInterpolationWeights() const;
//const RimCrossSection* crossSection() const;
private:
void calculateArrays();
void calculateArrays();
cvf::cref<RivIntersectionHexGridInterface> m_hexGrid;
// Output arrays
cvf::ref<cvf::Vec3fArray> m_triangleVxes;
cvf::ref<cvf::Vec3fArray> m_cellBorderLineVxes;
std::vector<size_t> m_triangleToCellIdxMap;
std::vector<RivIntersectionVertexWeights> m_triVxToCellCornerWeights;
cvf::ref<cvf::Vec3fArray> m_triangleVxes;
cvf::ref<cvf::Vec3fArray> m_cellBorderLineVxes;
std::vector<size_t> m_triangleToCellIdxMap;
std::vector<RivIntersectionVertexWeights> m_triVxToCellCornerWeights;
const RimIntersectionBox* m_crossSection;
const RimIntersectionBox* m_intersectionBoxDefinition;
};

View File

@ -64,6 +64,24 @@ RimIntersectionBox::~RimIntersectionBox()
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Mat4d RimIntersectionBox::boxOrigin() const
{
cvf::Mat4d mx(cvf::Mat4d::IDENTITY);
mx.setTranslation(cvf::Vec3d(minXCoord, minYCoord, minZCoord));
return mx;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
cvf::Vec3d RimIntersectionBox::boxSize() const
{
return cvf::Vec3d(maxXCoord, maxYCoord, maxZCoord) - cvf::Vec3d(minXCoord, minYCoord, minZCoord);
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@ -38,10 +38,13 @@ public:
~RimIntersectionBox();
// Fields
caf::PdmField<QString> name;
caf::PdmField<bool> isActive;
caf::PdmField<QString> name;
caf::PdmField<bool> isActive;
void setModelBoundingBox(cvf::BoundingBox& boundingBox);
cvf::Mat4d boxOrigin() const;
cvf::Vec3d boxSize() const;
void setModelBoundingBox(cvf::BoundingBox& boundingBox);
protected:
virtual caf::PdmFieldHandle* userDescriptionField() override;
@ -55,14 +58,14 @@ private:
void rebuildGeometryAndScheduleCreateDisplayModel();
private:
caf::PdmField<double> minXCoord;
caf::PdmField<double> minYCoord;
caf::PdmField<double> minZCoord;
caf::PdmField<double> minXCoord;
caf::PdmField<double> minYCoord;
caf::PdmField<double> minZCoord;
caf::PdmField<double> maxXCoord;
caf::PdmField<double> maxYCoord;
caf::PdmField<double> maxZCoord;
caf::PdmField<double> maxXCoord;
caf::PdmField<double> maxYCoord;
caf::PdmField<double> maxZCoord;
cvf::BoundingBox m_boundingBox;
cvf::BoundingBox m_boundingBox;
};