I have the following function to calculate the distance between two Cartesian points, and the mirror of one of the points about the z axis.
inline std::vector< Real64 >
distances(
MyCartesian const & point_i,
MyCartesian const & point_j
)
{
std::vector< Real64 > sumVals;
// Calculate the distance between points
sumVals.push_back( pow_2( point_i.x - point_j.x ) );
sumVals.push_back( pow_2( point_i.y - point_j.y ) );
sumVals.push_back( pow_2( point_i.z - point_j.z ) );
Real64 sumTot = 0;
std::vector< Real64 > retVals;
std::for_each( sumVals.begin(), sumVals.end(), [&] ( Real64 n ) { sumTot += n; } );
retVals.push_back( std::sqrt( sumTot ) );
// Calculate distance to mirror point
sumVals.pop_back();
sumVals.push_back( pow_2( point_i.z - ( -point_j.z ) ) );
sumTot = 0;
std::for_each( sumVals.begin(), sumVals.end(), [&] ( Real64 n ) { sumTot += n; } );
retVals.push_back( std::sqrt( sumTot ) );
return retVals;
};
Are there any benefits from pop_back
, then push_back
, or is there a better way to replace the last element in the vector?
In my opinion, using the vector is overcomplicating it.
Write a distance function that you can use.
Something like this:
Real64 distance(MyCartesian const & i, MyCartesian const & j)
{
return std::sqrt(pow_2(i.x - j.x)
+ pow_2(i.y - j.y)
+ pow_2(i.z - j.z));
}
MyCartesian mirror(MyCartesian pt)
{
pt.z *= -1;
return pt;
}
std::vector<Real64>
distances(MyCartesian const & i,
MyCartesian const & j)
{
return std::vector { distance(i, j), distance(i, mirror(j)) };
}
Since there's always exactly two results, you might prefer to return a pair
instead of a vector
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.