船舶軟件建立三維管道模型后,需要自動生成管子加工信息,這樣就提高了設計效率。其中彎管參數主要是下料長度,彎角和轉角。
下料長度是由各管段實長,即管子中心線長度,減去彎管部分切線長再加上彎管部分。實長就是向量的模。
彎角用向量的點乘來求解,即余弦定理。
轉角用向量的叉乘來求解,比用兩面角的方法精度要高。因為向量叉乘運算時的數字運算比三角函數精度高點。
因為都是矢量代數運算,所以需要一個矢量類,類定義如下:
1: //------------------------------------------------------------------------------
2: // Copyright (c) 2011 eryar All Rights Reserved.
3: //
4: // File : Vector.h
5: // Author : eryar@163.com
6: // Date : 2011-12-2 21:34
7: // Version : 1.0v
8: //
9: // Description :
10: //
11: //==============================================================================
12:
13: #ifndef _VECTOR_H_
14: #define _VECTOR_H_
15:
16: #pragma once
17:
18: #include <cmath>
19: #include <string>
20: #include <iostream>
21: using namespace std;
22:
23: class CVector
24: {
25: public:
26: CVector();
27: CVector(const CVector& v);
28: CVector(double x, double y, double z);
29: virtual ~CVector();
30:
31: // Overload operators
32: CVector& operator = (const CVector& v);
33: bool operator == (const CVector& v) const;
34: bool operator != (const CVector& v) const;
35: CVector operator + (const CVector& v) const;
36: CVector operator - (const CVector& v) const;
37: CVector operator * (double k) const;
38: // 向量點乘
39: double operator * (const CVector& v) const;
40:
41: // 向量叉乘
42: CVector CrossProduct(const CVector& v);
43:
44: // 求向量的模
45: double Magnitude(void) const;
46:
47: //
48: void Show(void) const;
49:
50: private:
51: double m_x;
52: double m_y;
53: double m_z;
54: };
55:
56: #endif // _VECTOR_H_
求解轉角代碼如下:
1: //------------------------------------------------------------------------------
2: // Copyright (c) 2011 eryar All Rights Reserved.
3: //
4: // File : Main.cpp
5: // Author : eryar@163.com
6: // Date : 2011-12-2 21:33
7: // Version : 1.0v
8: //
9: // Description :
10: //
11: //==============================================================================
12:
13: #include "Vector.h"
14:
15: int main(int argc, char* argv[])
16: {
17: // One Pipe piece
18: CVector aVector(287, 0, 0);
19: CVector bVector(313, 204, 165);
20: CVector cVector(0, 746, 0);
21:
22: // Another pipe piece
23: // CVector aVector(150, 0, 0);
24: // CVector bVector(50, 150, 150);
25: // CVector cVector(50, 250, 0);
26:
27: CVector alpha;
28: CVector beta;
29: double phi = 0;
30:
31: aVector.Show();
32: bVector.Show();
33: cVector.Show();
34:
35: alpha = aVector.CrossProduct(bVector);
36: beta = bVector.CrossProduct(cVector);
37:
38: phi = (alpha * beta) / (alpha.Magnitude() * beta.Magnitude());
39: cout<<"Rotate : "<<acos(phi) * 180 / 3.1416<<endl;
40:
41: return 0;
42: }