Skip to content

Commit 9f6dfd6

Browse files
committed
Create CalcHeightLine that supports distance of one.
1 parent 4cb3f35 commit 9f6dfd6

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

alg/viewshed/viewshed_executor.cpp

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,24 @@ bool invalid(int i)
5656
/// \param Za Height at the line one unit previous to the target point.
5757
double CalcHeightLine(int nDistance, double Za)
5858
{
59-
nDistance = std::abs(nDistance);
60-
assert(nDistance != 1);
59+
assert(nDistance > 1);
6160
return Za * nDistance / (nDistance - 1);
6261
}
6362

63+
/// Calculate the height at nDistance units along a line through the origin given the height
64+
/// at nDistance - 1 units along the line.
65+
/// \param nDistance Distance along the line for the target point.
66+
/// \param Zcur Height at the line at the target point.
67+
/// \param Za Height at the line one unit previous to the target point.
68+
double CalcHeightLine(int nDistance, double Zcur, double Za)
69+
{
70+
nDistance = std::abs(nDistance);
71+
assert(nDistance > 0);
72+
if (nDistance == 1)
73+
return Zcur;
74+
return CalcHeightLine(nDistance, Za);
75+
}
76+
6477
// Calculate the height Zc of a point (i, j, Zc) given a line through the origin (0, 0, 0)
6578
// and passing through the line connecting (i - 1, j, Za) and (i, j - 1, Zb).
6679
// In other words, the origin and the two points form a plane and we're calculating Zc
@@ -795,7 +808,6 @@ void ViewshedExecutor::processLineLeft(int nYOffset, LineLimits &ll,
795808
}
796809
iStart = oCurExtent.clampX(iStart);
797810

798-
nYOffset = std::abs(nYOffset);
799811
double *pThis = lines.cur.data() + iStart;
800812
double *pLast = lines.prev.data() + iStart;
801813

@@ -815,17 +827,13 @@ void ViewshedExecutor::processLineLeft(int nYOffset, LineLimits &ll,
815827
}
816828

817829
// Go from the observer to the left, calculating Z as we go.
830+
nYOffset = std::abs(nYOffset);
818831
for (int iPixel = iStart; iPixel > iEnd; iPixel--, pThis--, pLast--)
819832
{
820833
int nXOffset = std::abs(iPixel - m_nX);
821834
double dfZ;
822835
if (nXOffset == nYOffset)
823-
{
824-
if (nYOffset == 1)
825-
dfZ = *pThis;
826-
else
827-
dfZ = CalcHeightLine(nYOffset, *(pLast + 1));
828-
}
836+
dfZ = CalcHeightLine(nYOffset, *pThis, *(pLast + 1));
829837
else
830838
dfZ =
831839
oZcalc(nXOffset, nYOffset, *(pThis + 1), *pLast, *(pLast + 1));
@@ -855,7 +863,6 @@ void ViewshedExecutor::processLineRight(int nYOffset, LineLimits &ll,
855863
}
856864
iStart = oCurExtent.clampX(iStart);
857865

858-
nYOffset = std::abs(nYOffset);
859866
double *pThis = lines.cur.data() + iStart;
860867
double *pLast = lines.prev.data() + iStart;
861868

@@ -874,17 +881,13 @@ void ViewshedExecutor::processLineRight(int nYOffset, LineLimits &ll,
874881
}
875882

876883
// Go from the observer to the right, calculating Z as we go.
884+
nYOffset = std::abs(nYOffset);
877885
for (int iPixel = iStart; iPixel < iEnd; iPixel++, pThis++, pLast++)
878886
{
879887
int nXOffset = std::abs(iPixel - m_nX);
880888
double dfZ;
881889
if (nXOffset == nYOffset)
882-
{
883-
if (nYOffset == 1)
884-
dfZ = *pThis;
885-
else
886-
dfZ = CalcHeightLine(nYOffset, *(pLast - 1));
887-
}
890+
dfZ = CalcHeightLine(nYOffset, *pThis, *(pLast - 1));
888891
else
889892
dfZ =
890893
oZcalc(nXOffset, nYOffset, *(pThis - 1), *pLast, *(pLast - 1));
@@ -934,11 +937,8 @@ bool ViewshedExecutor::processLine(int nLine, Lines &lines)
934937
{
935938
if (ll.left < ll.right && ll.leftMin == ll.rightMin)
936939
{
937-
double dfZ;
938-
if (std::abs(nYOffset) == 1)
939-
dfZ = lines.cur[m_nX];
940-
else
941-
dfZ = CalcHeightLine(nYOffset, lines.prev[m_nX]);
940+
double dfZ = CalcHeightLine(std::abs(nYOffset), lines.cur[m_nX],
941+
lines.prev[m_nX]);
942942
setOutput(lines.result[m_nX], lines.cur[m_nX], dfZ);
943943
}
944944
else

0 commit comments

Comments
 (0)