You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.9 KiB
C++
55 lines
1.9 KiB
C++
#include "nmVolumeSliceSettings.h"
|
|
#include "nmVolumeData.h"
|
|
#include <QCoreApplication>
|
|
#include <cmath>
|
|
#include <vtkMath.h>
|
|
|
|
nmVolumeSliceSettings::nmVolumeSliceSettings()
|
|
: m_bShowPlane(true), m_bShowSource(false)
|
|
{
|
|
for(int nAxis = 0; nAxis < 3; ++nAxis) {
|
|
m_dOrigin[nAxis] = 0.0;
|
|
m_dNormal[nAxis] = nAxis == 2 ? 1.0 : 0.0;
|
|
}
|
|
}
|
|
|
|
nmVolumeSliceSettings nmVolumeSliceSettings::fromData(const nmVolumeData& oData)
|
|
{
|
|
nmVolumeSliceSettings oSettings;
|
|
double dBounds[6];
|
|
oData.getBounds(dBounds);
|
|
for(int nAxis = 0; nAxis < 3; ++nAxis) {
|
|
// 避免两个同号大坐标相加溢出。
|
|
oSettings.m_dOrigin[nAxis] = dBounds[2 * nAxis] * 0.5 + dBounds[2 * nAxis + 1] * 0.5;
|
|
}
|
|
oSettings.m_sPropertyId = oData.getInitialPropertyId();
|
|
return oSettings;
|
|
}
|
|
|
|
bool nmVolumeSliceSettings::normalize(QString* pError)
|
|
{
|
|
if(pError != nullptr) pError->clear();
|
|
double dMaximum = 0.0;
|
|
for(int nAxis = 0; nAxis < 3; ++nAxis) {
|
|
if(!vtkMath::IsFinite(m_dOrigin[nAxis]) || !vtkMath::IsFinite(m_dNormal[nAxis])) {
|
|
if(pError != nullptr) *pError = QCoreApplication::translate("nmVolumeSliceSettings", "Origin and normal must contain finite numbers.");
|
|
return false;
|
|
}
|
|
dMaximum = qMax(dMaximum, std::fabs(m_dNormal[nAxis]));
|
|
}
|
|
if(dMaximum == 0.0) {
|
|
if(pError != nullptr) *pError = QCoreApplication::translate("nmVolumeSliceSettings", "The plane normal must not be zero.");
|
|
return false;
|
|
}
|
|
// 先缩放再求模,兼容科学计数法输入的极大、极小非零法向。
|
|
double dNormal[3];
|
|
double dLengthSquared = 0.0;
|
|
for(int nAxis = 0; nAxis < 3; ++nAxis) {
|
|
dNormal[nAxis] = m_dNormal[nAxis] / dMaximum;
|
|
dLengthSquared += dNormal[nAxis] * dNormal[nAxis];
|
|
}
|
|
const double dLength = std::sqrt(dLengthSquared);
|
|
for(int nAxis = 0; nAxis < 3; ++nAxis) m_dNormal[nAxis] = dNormal[nAxis] / dLength;
|
|
return true;
|
|
}
|