コード例 #1
0
ファイル: Matrix3D.cpp プロジェクト: SimoneNardi/base_math
/// Returns a rotation matrix whose third column is equal to the given vector.
Matrix3D Matrix3D::rotationMatrixFromZAxisForDXF(const Real3D& dir)
{
	// Normalize the target direction
	// If it is degenerate, returns the identity matrix
	Real3D target = dir;
	REAL len = target.mod();
	if (len == 0) return Matrix3D(1);
	target /= len;
	if (!target.isfinite()) return Matrix3D(1);

	// Choice of xAxis: see AutoCAD documentation.
	Real3D v;
	if( fabs(target[0])*64 < 1 && fabs(target[1])*64 < 1 )
		v = Real3D(0,1,0) ^ target;
	else
		v = Real3D(0,0,1) ^ target;

	v = v.vers();
	Real3D w = (target^v).vers();
	Matrix3D rot;
	rot.setCols(v, w, target);
	// Debug Check
	assert(fabs(rot.det()-1) < Math::TOLERANCE);
	return rot;
}
コード例 #2
0
ファイル: Real3D.cpp プロジェクト: SimoneNardi/base_math
// Reflects a point w.r.t. a plane through "origin",
// whose normal vector is "normal".
Real3D Real3D::reflect(const Real3D& origin, const Real3D& normal) const
{
	Real3D v(*this);
	REAL sqrmod = normal.sqrmod();
	REAL scal_prod;

	// If the normal vector is null returns this with no reflection at all.
	if(sqrmod >= 1E-20) {
		v -= origin;

		if(fabs(1. - sqrmod) < Math::TOLERANCE) {
			scal_prod = normal*v;
			v -= 2*scal_prod*normal;
		} else {
			// If the normal vector is not normalized computes the normalized vector.
			Real3D actualNormal(normal.vers());
			scal_prod = actualNormal*v;
			v -= 2*scal_prod*actualNormal;
		}
	}
	return v;
}