void tst_GeoCpp::testVector() { CVector3d u3(1, 2, 3); CVector3d v3(5, 8, 9); QCOMPARE(u3, CVector3d(1, 2, 3)); //QCOMPARE(u3.length(), 3.74166); QCOMPARE(u3.x(), 1.0); QCOMPARE(u3.y(), 2.0); QCOMPARE(u3.z(), 3.0); u3.setX(5); QCOMPARE(u3, CVector3d(5, 2, 3)); QCOMPARE(u3.x(), 5.0); QCOMPARE(u3.y(), 2.0); QCOMPARE(u3.z(), 3.0); QCOMPARE(v3, CVector3d(5, 8, 9)); QCOMPARE(-v3, CVector3d(-5, -8, -9)); QCOMPARE(u3 + v3, CVector3d(10, 10, 12)); QCOMPARE(u3 - v3, CVector3d(0, -6, -6)); QCOMPARE(u3 * 2, CVector3d(10, 4, 6)); QCOMPARE(2 * u3, CVector3d(10, 4, 6)); u3[2] = 456; QCOMPARE(u3, CVector3d(5, 2, 456)); QCOMPARE(u3.x(), 5.0); QCOMPARE(u3.y(), 2.0); QCOMPARE(u3.z(), 456.0); }
/** Compute the distance from a point pta * to the line defined by the 2 points ptb and ptc * The two points ptb and ptc defining the baseline also define * its positive direction from point ptb toward point ptc. * It is assumed that the 3 points pta, ptb, ptc define a plane * not far from the X-Y plane. * The sign of d is positive if the point pta * is left of the line ptb=>ptc * The sign of d is negative if pta is right of the line. * * @author Robert Laine alias Sailcuter */ real Distance3d(const CPoint3d &pta, const CPoint3d &ptb, const CPoint3d &ptc) { real d; CVector3d Va = CVector3d( pta - ptb ); CVector3d Vb = CVector3d( ptc - ptb).normalized(); CVector3d Vd = CVector3d::crossProduct(Vb, Va); d = Vd.length(); if ( Vd.z() < 0 ) d = -d; // return d; }
/** Returns a rotated copy of the object by a given azimuth and elevation. */ CPanelGroup CSailDisp::dispObject() const { const CVector3d center = baseRect.center(); CMatrix4x4 matrix; matrix.translate(center); matrix.rotate(degreesToRadians(m_elevation), CVector3d(1, 0, 0)); matrix.rotate(degreesToRadians(m_azimuth), CVector3d(0, 1, 0)); matrix.translate(-center); return baseObject.transformed(matrix); }
//******************************************** // Operator ^ // Returns the cross product of u and v. //******************************************** CVector3d operator^(const CVector3d& u, const CVector3d& v) { return CVector3d(u.vec[1] * v.vec[2] - u.vec[2] * v.vec[1], u.vec[2] * v.vec[0] - u.vec[0] * v.vec[2], u.vec[0] * v.vec[1] - u.vec[1] * v.vec[0]); }
//******************************************** // Cross //******************************************** CVector3d CVector3d::Cross(const CVector3d& v) const { return CVector3d(y() * v.z() - z() * v.y(), z() * v.x() - x() * v.z(), x() * v.y() - y() * v.x()); }
//******************************************** // Cross //******************************************** CVector3d CVector3d::Cross(const CVector3d* pV) const { return CVector3d(y() * pV->z() - z() * pV->y(), z() * pV->x() - x() * pV->z(), x() * pV->y() - y() * pV->x()); }
//******************************************** // Projection // by Haeyoung Lee //******************************************** CVector3d CVector3d::Projection(const CVector3d* pV) const { double alpha = Dot(pV)/pV->Dot(pV); return CVector3d(x()-alpha* pV->x(), y()-alpha*pV->y(), z()-alpha*pV->z()); }
/** Returns corresponding unit length vector for non-zero vectors * and zero vector otherwise. */ CVector3d CVector3d::normalized() const { const real n = length(); if (n<EPS) return CVector3d(); else return *this*(1/n); }
/** Converts screen coordinates to logical coordinates. */ CPoint3d CSailDisp::screenToLogical( const int x, const int y ) const { // avoid division by zero if ((viewRect.width()==0)||(viewRect.height()==0)) return m_center; const CRect3d logicalRect = this->logicalRect(); return m_center + CVector3d( logicalRect.width() * ( real(x) / viewRect.width() - 0.5 ), logicalRect.height() * ( 0.5 - real(y) / viewRect.height() ), 0); }
//******************************************** // Rotate this vector around pAround by angle // by Haeyoung Lee //******************************************** CVector3d CVector3d::Rotate(double angle, CVector3d Around) { double f1, f2, f3; CVector3d t1, t2; f1 = (double)cos((double)angle); f2 = (double)sin((double)angle); t1 = Projection(&Around); t2 = Around.Cross(this); f3 = Dot(Around); return CVector3d((double)(f1*t1.x()+f2*t2.x()+f3*Around.x()), (double)(f1*t1.y()+f2*t2.y()+f3*Around.y()), (double)(f1*t1.z()+f2*t2.z()+f3*Around.z())); }
void tst_GeoCpp::testSubSpace(void) { const CSubSpace L3A = CSubSpace::line(CPoint3d(0,0,1), CVector3d(1,1,0)); const CSubSpace L3B = CSubSpace::line(CPoint3d(0,0,0), CVector3d(1,1,1)); const CSubSpace P3A = CSubSpace::plane(CPoint3d(2,3,7), CVector3d(1,0,5), CVector3d(0,1,0)); const CSubSpace P3B = CSubSpace::plane(CPoint3d(4,-5,1), CVector3d(-3,0,1), CVector3d(0,1,0)); const CPoint3d p3(2,2,2),q3(2,1,2),r3(4,5,7),s3(2,2,1); CSubSpace h; // lines QCOMPARE(L3A.contains(p3), false); QCOMPARE(L3A.contains(s3), true); QCOMPARE(L3B.contains(p3), true); QCOMPARE(L3B.contains(q3), false); // planes QCOMPARE(P3A.contains(p3), false); QCOMPARE(P3A.contains(r3), false); QCOMPARE(P3B.contains(p3), false); QCOMPARE(P3B.contains(r3), false); // line / line intersection h = L3A.intersect(L3B); QCOMPARE(h.getdim(), 0); QCOMPARE(L3A.contains(h.getp()), true); QCOMPARE(L3B.contains(h.getp()), true); // plane / plane intersection h = P3A.intersect(P3B); QCOMPARE(h.getdim(), 1); QCOMPARE(P3A.contains(h.getp()), true); QCOMPARE(P3B.contains(h.getp()), true); // plane / line intersection h = P3A.intersect(L3A); QCOMPARE(h.getdim(), 0); QCOMPARE(P3A.contains(h.getp()), true); QCOMPARE(L3A.contains(h.getp()), true); // line / plane intersection h = L3A.intersect(P3A); QCOMPARE(h.getdim(), 0); QCOMPARE(P3A.contains(h.getp()), true); QCOMPARE(L3A.contains(h.getp()), true); }
//******************************************** // Constructor //******************************************** void CTransform::Clear(void) { SetScale(CVector3d(1.0f,1.0f,1.0f)); SetTranslation(CVector3d(0.0f,0.0f,0.0f)); SetValueRotation(0.0f); }
CVector3d CVector3d::operator/(double val) { return CVector3d(x / val, y / val, z / val); }
CVector3d CVector3d::operator-(const CVector3d& rhs) { return CVector3d(x - rhs.x, y - rhs.y, z - rhs.z); }
CVector3d CVector3d::operator+(const CVector3d& rhs) { return CVector3d(x + rhs.x, y + rhs.y, z + rhs.z); }
CVector3d CVector3d::operator*(const CVector3d& rhs) { return CVector3d(x * rhs.x, y * rhs.y, z * rhs.z); }
CVector3d CVector3d::operator-() const { return CVector3d(-x, -y, -z); }
//******************************************** // Operator * //******************************************** CVector3d operator*(double s, const CVector3d& u) { return CVector3d(u.vec[0] * s, u.vec[1] * s, u.vec[2] * s); }
CVector3d CVector3d::operator/(const CVector3d& rhs) { return CVector3d(x / rhs.x, y / rhs.y, z / rhs.z); }
//******************************************** // Operator + //******************************************** CVector3d operator+(const CVector3d& u, const CVector3d& v) { return CVector3d(u.vec[0]+v.vec[0],u.vec[1]+v.vec[1],u.vec[2]+v.vec[2]); }
//******************************************** // Operator - //******************************************** CVector3d operator-(const CVector3d& u, const CVector3d& v) { return CVector3d(u.vec[0]-v.vec[0],u.vec[1]-v.vec[1],u.vec[2]-v.vec[2]); }
//******************************************** // Operator - // Nondestructive unary - // Returns a new vector. //******************************************** CVector3d CVector3d::operator -() const { return CVector3d(-vec[0],-vec[1],-vec[2]); }
CVector3d CVector3d::cross(const CVector3d& rhs) const { return CVector3d(y * rhs.z - z * rhs.y, z * rhs.x - x * rhs.z, x * rhs.y - y * rhs.x); }
CVector3d CVector3d::operator+(double val) { return CVector3d(x + val, y + val, z + val); }
CVector3d CVector3d::operator*(double val) { return CVector3d(x * val, y * val, z * val); }
CVector3d CVector3d::operator-(double val) { return CVector3d(x - val, y - val, z - val); }