18#include <Inventor/C/errors/debugerror.h>
19#include <Inventor/nodes/SoTransform.h>
28 static bool isBad(
const double&
x,
const QString& ctxStr,
const double& limit = 1.0e20 )
30 if (
x==
x && fabs(
x) < limit)
32 if (!ctxStr.isEmpty())
42 HepGeom::Vector3D<double> translation = transformation.getTranslation();
43 CLHEP::HepRotation rotation = transformation.getRotation();
44 SoTransform *myTransform = tin ? tin :
new SoTransform();
45 myTransform->translation = SbVec3f(translation.x(),
48 myTransform->rotation = SbRotation(SbMatrix(rotation.xx(),rotation.yx(),rotation.zx(),0,
49 rotation.xy(),rotation.yy(),rotation.zy(),0,
50 rotation.xz(),rotation.yz(),rotation.zz(),0,
53 bool inputok =
isSane( transformation,
"input HepGeom::Transform3D" );
54 bool outputok =
isSane( myTransform,
"output SoTransform" );
56 VP1Msg::messageVerbose(
"VP1LinAlgUtils::toSoTransform ERROR: Problems observed in input HepGeom::Transform3D");
59 if (inputok!=outputok)
60 VP1Msg::messageVerbose(
"VP1LinAlgUtils::toSoTransform Error introduced in HepGeom::Transform3D -> SoTransform conversion!");
72 SoTransform *myTransform = tin ? tin :
new SoTransform();
73 myTransform->translation = SbVec3f(translation.x(),
80 myTransform->rotation = SbRotation(SbMatrix(rotation(0,0),rotation(1,0),rotation(2,0),0,
81 rotation(0,1),rotation(1,1),rotation(2,1),0,
82 rotation(0,2),rotation(1,2),rotation(2,2),0,
87 bool inputok =
isSane( transformation,
"input HepGeom::Transform3D" );
88 bool outputok =
isSane( myTransform,
"output SoTransform" );
90 VP1Msg::messageVerbose(
"VP1LinAlgUtils::toSoTransform ERROR: Problems observed in input HepGeom::Transform3D");
93 if (inputok!=outputok)
94 VP1Msg::messageVerbose(
"VP1LinAlgUtils::toSoTransform Error introduced in HepGeom::Transform3D -> SoTransform conversion!");
103 result.setTransform(xf->translation.getValue(), xf->rotation.getValue(),
104 xf->scaleFactor.getValue(), xf->scaleOrientation.getValue(), xf->center.getValue());
106 bool inputok =
isSane( xf,
"input SoTransform" );
109 VP1Msg::messageVerbose(
"VP1LinAlgUtils::transformToMatrix(SoTransform*,..) ERROR: Problems observed in input SoTransform");
111 VP1Msg::messageVerbose(
"VP1LinAlgUtils::transformToMatrix(SoTransform*,..) ERROR: Problems observed in output SbMatrix");
112 if (inputok!=outputok)
113 VP1Msg::messageVerbose(
"VP1LinAlgUtils::transformToMatrix(SoTransform*,..) Error introduced in SoTransform -> SbMatrix conversion!");
120 result = SbMatrix(heptr.xx(),heptr.yx(),heptr.zx(),0,
121 heptr.xy(),heptr.yy(),heptr.zy(),0,
122 heptr.xz(),heptr.yz(),heptr.zz(),0,
123 heptr.dx(),heptr.dy(),heptr.dz(),1);
125 bool inputok =
isSane( heptr,
"input HepGeom::Transform3D" );
128 VP1Msg::messageVerbose(
"VP1LinAlgUtils::transformToMatrix(HepGeom::Transform3D,..) ERROR: Problems observed in input HepGeom::Transform3D");
130 VP1Msg::messageVerbose(
"VP1LinAlgUtils::transformToMatrix(HepGeom::Transform3D,..) ERROR: Problems observed in output SbMatrix");
131 if (inputok!=outputok)
132 VP1Msg::messageVerbose(
"VP1LinAlgUtils::transformToMatrix(HepGeom::Transform3D,..) Error introduced in HepGeom::Transform3D -> SbMatrix conversion!");
139 SbVec3f translation, scaleFactor;
140 SbRotation rotation, scaleOrientation;
141 matr.getTransform(translation, rotation, scaleFactor, scaleOrientation, SbVec3f(0,0,0));
142 SoTransform * t = tin ? tin :
new SoTransform;
143 t->translation.setValue(translation);
144 t->rotation.setValue(rotation);
145 t->scaleFactor.setValue(scaleFactor);
146 t->scaleOrientation.setValue(scaleOrientation);
147 t->center.setValue(0,0,0);
149 bool inputok =
isSane( matr,
"input SbMatrix" );
150 bool outputok =
isSane( t,
"output SoTransform" );
155 if (inputok!=outputok)
156 VP1Msg::messageVerbose(
"VP1LinAlgUtils::toSoTransform Error introduced in SoTransform -> SbMatrix conversion!");
163 float& translation_x,
float& translation_y,
float& translation_z,
164 float& rotaxis_x,
float& rotaxis_y,
float& rotaxis_z,
float& rotangle_radians )
166 SbVec3f translation, scaleFactor, rotaxis;
167 SbRotation rotation, scaleOrientation;
168 matr.getTransform (translation, rotation,scaleFactor,scaleOrientation,SbVec3f(0,0,0));
169 translation.getValue(translation_x,translation_y,translation_z);
170 rotation.getValue(rotaxis,rotangle_radians);
171 rotaxis.getValue(rotaxis_x,rotaxis_y,rotaxis_z);
173 if (!
isSane( matr,
"input SbMatrix" ))
174 VP1Msg::messageVerbose(
"VP1LinAlgUtils::decodeTransformation ERROR: Problems observed in output SbMatrix");
181#define BAD(x) { ok=false; if (!ctxStr.isEmpty()) VP1Msg::messageVerbose("VP1LinAlgUtils ("+ctxStr+") ERROR: "+QString(x)); };
187 HepGeom::Vector3D<double> translation = t.getTranslation();
188 CLHEP::HepRotation rotation = t.getRotation();
189 CLHEP::Hep3Vector rotationAxis;
190 double rotationAngle;
191 rotation.getAngleAxis(rotationAngle,rotationAxis);
199 BAD(
"Problem in raw 4x4 transformation matrix!");
201 BAD(
"Problem in translation!");
205 BAD(
"Problem in rotation");
207 BAD(
"Problem in rotationAxis");
209 BAD(
"Problem in rotationAngle");
212 const double det = t.xx()*t.yy()*t.zz()
213 +t.zx()*t.xy()*t.yz()
214 +t.xz()*t.yx()*t.zy()
215 -t.xx()*t.zy()*t.yz()
216 -t.yx()*t.xy()*t.zz()
217 -t.zx()*t.yy()*t.xz();
218 const double eps = 1.0e-5;
219 if (det<1.0-eps||det>1.0+eps)
220 BAD(
"Determinant of rotation part is not consistent with 1 (det="+QString::number(det)+
")!");
233 double rotationAngle;
271 BAD(
"Problem in raw 4x4 transformation matrix!");
273 BAD(
"Problem in translation!");
277 BAD(
"Problem in rotation");
279 BAD(
"Problem in rotationAxis");
281 BAD(
"Problem in rotationAngle");
290 const double det = t.matrix().determinant();
291 const double eps = 1.0e-5;
292 if ( det < 1.0-eps || det > 1.0+eps )
293 BAD(
"Determinant of rotation part is not consistent with 1 (det="+QString::number(det)+
")!");
303 if (!ctxStr.isEmpty())
310 t->rotation.getValue().getValue(q0,q1, q2,q3);
312 BAD(
"Problem in quarternion representation of rotation!");
313 t->scaleOrientation.getValue().getValue(q0,q1, q2,q3);
315 BAD(
"Problem in quarternion representation of scaleOrientation!");
317 t->translation.getValue().getValue(
x,
y,
z);
319 BAD(
"Problem in translation!");
320 t->scaleFactor.getValue().getValue(
x,
y,
z);
322 BAD(
"Problem in scaleFactor!");
323 t->center.getValue().getValue(
x,
y,
z);
325 BAD(
"Problem in center!");
336 for (
int i = 0; i < 4; ++i)
337 for (
int j = 0; j < 4; ++j)
343 BAD(
"Problem in raw 4x4 matrix!");
345 SbVec3f translation, scaleFactor, rotaxis;
346 SbRotation rotation, scaleOrientation;
347 matr.getTransform(translation, rotation,scaleFactor,scaleOrientation,SbVec3f(0,0,0));
349 rotation.getValue(q0,q1, q2,q3);
351 BAD(
"Problem in quarternion representation of rotation!");
352 scaleOrientation.getValue(q0,q1, q2,q3);
354 BAD(
"Problem in quarternion representation of scaleOrientation!");
356 translation.getValue(
x,
y,
z);
358 BAD(
"Problem in translation!");
359 scaleFactor.getValue(
x,
y,
z);
361 BAD(
"Problem in scaleFactor!");
363 const float eps = 1.0e-5;
364 const float det3 = matr.det3();
365 if (det3<1.0-eps||det3>1.0+eps)
366 BAD(
"Determinant of rotation part is not consistent with 1 (det3="+QString::number(det3)+
")!");
367 const float det4 = matr.det4();
368 if (det4<1.0-eps||det4>1.0+eps)
369 BAD(
"Determinant of rotation part is not consistent with 1 (det4="+QString::number(det4)+
")!");
377 double phi = (
x == 0.0f)? (
y==0.0f?0.0:0.5*
M_PI) : std::abs(atan(std::abs(
y/
static_cast<double>(
x))));
382 else if (
x<0.0f&&
y<0.0f)
394 s = ( ((point)[0]-(point0)[0])*((point1)[0]-(point0)[0]) +
395 ((point)[1]-(point0)[1])*((point1)[1]-(point0)[1]) +
396 ((point)[2]-(point0)[2])*((point1)[2]-(point0)[2]) ) /
397 ( ((point1)[0]-(point0)[0])*((point1)[0]-(point0)[0]) +
398 ((point1)[1]-(point0)[1])*((point1)[1]-(point0)[1]) +
399 ((point1)[2]-(point0)[2])*((point1)[2]-(point0)[2]) );
427 double d3232 = p32.dot(p32);
429 double d1010 = p10.dot(p10);
430 if (d3232==0.0||d1010==0.0||d3232!=d3232||d1010!=d1010) {
434 VP1Msg::messageDebug(
"VP1LinAlgUtils distLineLineParam ERROR: One or both input lines ill defined");
438 double d3210 = p32.dot(p10);
439 double denom = d1010 * d3232 - d3210 * d3210;
441 double d0232 = p02.dot(p32);
445 VP1Msg::messageDebug(
"VP1LinAlgUtils distLineLineParam Warning: Input lines are parallel. Choosing arbitrary point of closest approach");
448 double d0210 = p02.dot(p10);
449 double numer = d0232 * d3210 - d0210 * d3232;
452 t = (d0232 + d3210 * s) / d3232;
Scalar phi() const
phi method
static bool isBad(const double &x, const QString &ctxStr, const double &limit=1.0e20)
static bool isSane(const HepGeom::Transform3D &, const QString &contextStr="")
static SoTransform * toSoTransform(const HepGeom::Transform3D &, SoTransform *t=0)
static void distPointLineParam(const Amg::Vector3D &point, const Amg::Vector3D &point0, const Amg::Vector3D &point1, double &s)
static void distLineLineParam(const Amg::Vector3D &point0, const Amg::Vector3D &point1, const Amg::Vector3D &point2, const Amg::Vector3D &point3, double &s, double &t)
static void decodeTransformation(const SbMatrix &, float &translation_x, float &translation_y, float &translation_z, float &rotaxis_x, float &rotaxis_y, float &rotaxis_z, float &rotangle_radians)
static double phiFromXY(const double &x, const double &y)
static double distPointLine2(const Amg::Vector3D &point, const Amg::Vector3D &point0, const Amg::Vector3D &point1, double &s)
static void transformToMatrix(SoTransform *xf, SbMatrix &result)
static void messageVerbose(const QString &)
static void messageDebug(const QString &)
static QString str(const QString &s)
Eigen::Matrix< double, 3, 3 > RotationMatrix3D
void getAngleAxisFromRotation(Amg::RotationMatrix3D &rotation, double &rotationAngle, Amg::Vector3D &rotationAxis)
Eigen::Affine3d Transform3D
Eigen::Matrix< double, 3, 1 > Vector3D