Math Primitives and Algorithms
Overview
- Vectors and matrices
- Geometric primitives
- Math algorithms
Vectors and Matrices
기본 타입 math_Vector와 math_Matrix의 C++ 구현.
- Vectors and matrices calc.;
- Eigenvalues and eigenvectors of a square matrix;
- Linear algebraic equations;
- Roots of a set of non-linear equations;
- Minimum function of one or more independent variables.
expression, relation, or function을 표현하는 데이터 구조 제공.
벡터, 행렬은 선언 시 범위 지정. 변경 불가.
math_Vector aVec (1, 3);
// a vector of dimension 3 with range (1..3)
math_Matrix aMat (0, 2, 0, 2);
// a matrix of dimension 3x3 with range (0..2, 0..2)
math_Vector aVec (N1, N2);
// a vector of dimension N2-N1+1 with range (N1..N2)
참조가 아닌 값. 복사 가능. 공유 불가.
math_Vector aVec1 (1, 3), aVec2 (0, 2);
aVec2 = aVec1;
// aVec1 is copied into aVec2; a modification of aVec1 does not affect aVec2
index로 초기화, 접근 가능.
math_Vector aVec (1, 3);
math_Matrix aMat (1, 3, 1, 3);
Standard_Real aValue;
aVec (2) = 1.0;
aValue = aVec(1);
aMat (1, 3) = 1.0;
aValue = aMat (2, 2);
일부 연산은 illegal. 다음 예외 발생.
- Standard_DimensionError exception : 비호환되는 dimension의 행렬이나 벡터 2개를 연산.
- Standard_RangeError exception : 범위 밖 접근.
math_Vector aVec1 (1, 3), aVec2 (1, 2), aVec3 (0, 2);
aVec1 = aVec2; // error: Standard_DimensionError is raised
aVec1 = aVec3; // OK: ranges are not equal but dimensions are compatible
aVec1 (0) = 2.0; // error: Standard_RangeError is raised
Primitive Geometric Types
STEP-compliant
- primitive geometric shapes;
- Points;
- Vectors;
- Lines;
- Circles and conics;
- Planes and elementary surfaces;
- Coordinate system on space, plane;
- Geometric transformations
- Translations;
- Rotations;
- Symmetries;
- Scaling transformations;
- Composed transformations;
- Tools (coordinates and matrices) for algebraic computation.
geometric processor package gp.
2d and 3d objects는 참조가 아닌 값.
gp curves and surfaces.
no parameterization and no orientation on gp entities.
더 진화된 데이터 구조 Geom (in 3D space) and Geom2d (in the plane)
parametrization of the equivalent Geom or Geom2d entity.
projections or intersections 계산에 유용.
ElCLib and ElSLib packages;
- the point of parameter u on a 2D or 3D gp curve
- the point of parameter (u,v) on a gp elementary surface
- any derivative vector at this point.
Collections of Primitive Geometric Types
geometric object 생성 전, 2d, 3d context와 어떻게 다룰 지 선택해야 함.
instance의 집합을 다룰 때는 TColgp package가 기능 제공.
gp_XY, gp_XYZ, gp_Pnt, gp_Pnt2d, gp_Vec, gp_Vec2d, gp_Lin, gp_Lin2d, gp_Circ, gp_Circ2d.
Basic Geometric Libraries
curves, surfaces의 기본 계산 제공하는 다양한 library package들
elementary curves and surfaces libraries – ElCLib, ElSLib.
- EICLib - analytic curves.
simple computations on curves from the gp package (Lines, Circles and Conics).
compute points with a given parameter, parameter for a point. - EISLib - analytic surfaces.
simple computations on surfaces from the package gp (Planes, Cylinders, Spheres, Cones, Tori).
compute points with a given pair of parameters or to compute the parameter for a point.
calculating normals on curves and surfaces.
Bnd package - bounding boxes in 2d and 3d space.
Common Math Algorithms
C++ implementation of the most frequently used mathematical algorithms;
- linear algebraic equations,
- minimum of a function of one or more independent variables,
- roots of one, or of a set, of non-linear equations,
- eigenvalues and eigenvectors of a square matrix.
principles;
- A constructor performing all, or most of, the calculation, given the appropriate arguments. All relevant information is stored inside the resulting object, so that all subsequent calculations or interrogations will be solved in the most efficient way.
- A function IsDone returning the boolean true if the calculation was successful.
- A set of functions, specific to each algorithm, enabling all the various results to be obtained. Calling these functions is legal only if the function IsDone answers true, otherwise the exception StdFail_NotDone is raised.
math_Gauss 클래스 예제. - Gauss solution for a set of linear equations
class math_Gauss
{
public:
math_Gauss (const math_Matrix& A);
Standard_Boolean IsDone() const;
void Solve (const math_Vector& B, math_Vector& X) const;
};
a*x1=b1 and a*x2=b2:
#include <math_Vector.hxx>
#include <math_Matrix.hxx>
main()
{
math_Vector a(1, 3, 1, 3);
math_Vector b1(1, 3), b2(1, 3);
math_Vector x1(1, 3), x2(1, 3);
// a, b1 and b2 are set here to the appropriate values
...
math_Gauss aSol(a); // computation of the LU decomposition of A
if (aSol.IsDone()) // is it OK ?
{
aSol.Solve(b1, x1); // yes, so compute x1
aSol.Solve(b2, x2); // then x2
...
}
else // it is not OK:
{
// fix up
aSol.Solve(b1, x1); // error:
// StdFail_NotDone is raised
}
}
math_BissecNewton class 사용 예제 - Newton and Bissection algorithms
class math_BissecNewton
{
public:
math_BissecNewton (math_FunctionWithDerivative& f,
const Standard_Real bound1,
const Standard_Real bound2,
const Standard_Real tolx);
Standard_Boolean IsDone() const;
Standard_Real Root();
};
위에서 추상 클래스 math_FunctionWithDerivative는 함수 f를 위해 구현되어야 한다.
class math_FunctionWithDerivative
{
public:
virtual Standard_Boolean Value (const Standard_Real x, Standard_Real& f) = 0;
virtual Standard_Boolean Derivative (const Standard_Real x, Standard_Real& d) = 0;
virtual Standard_Boolean Values (const Standard_Real x, Standard_Real& f, Standard_Real& d) = 0;
};
구간 [1.5, 2.5]에서의 f(x)=x**2-4
#include <math_BissecNewton.hxx>
#include <math_FunctionWithDerivative.hxx>
class myFunction : public math_FunctionWithDerivative
{
Standard_Real myCoefA, myCoefB, myCoefC;
public:
myFunction (const Standard_Real theA, const Standard_Real theB, const Standard_Real theC)
: myCoefA(a), myCoefB(b), myCoefC(c) {}
virtual Standard_Boolean Value (const Standard_Real x, Standard_Real& f) override
{
f = myCoefA * x * x + myCoefB * x + myCoefC;
}
virtual Standard_Boolean Derivative (const Standard_Real x, Standard_Real& d) override
{
d = myCoefA * x * 2.0 + myCoefB;
}
virtual Standard_Boolean Values (const Standard_Real x, Standard_Real& f, Standard_Real& d) override
{
f = myCoefA * x * x + myCoefB * x + myCoefC;
d = myCoefA * x * 2.0 + myCoefB;
}
};
main()
{
myFunction aFunc (1.0, 0.0, 4.0);
math_BissecNewton aSol (aFunc, 1.5, 2.5, 0.000001);
if (aSol.IsDone()) // is it OK ?
{
Standard_Real x = aSol.Root(); // yes
}
else // no
{
}
Precision
Precision package
2값 비교할 때의 정밀도 설정.
if (X1 == X2) 라고 코딩하지 말고 if (Abs(X1-X2) < Precision). 라고 해야 한다.
low-level geometric algorithms ; argument로 precision을 받음.
High-level modeling algorithms ; precision setting을 low-level에 제공.
알고리즘의 필요에 따른 서로 다른 precision 설정이 제공됨.
- a real space, 3d or 2d where the lengths are measured in meters, micron, inches, etc.
- a parametric space, 1d on a curve or 2d on a surface where numbers have no dimension.
on a curve defined by the equation P(t);
Abs (t1 - t2) < ParametricPrecision
Distance (P(t1), P(t2)) < RealPrecision
The Precision package
package methods,
angles, distances, intersections, approximations, and parametric space를 다루기 위한 default precision들.
- Angular precision compares angles.
- Confusion precision compares distances.
- Intersection precision is used by intersection algorithms.
- Approximation precision is used by approximation algorithms.
- Parametric precision gets a parametric space precision from a 3D precision.
- Infinite returns a high number that can be considered to be infinite. Use -Infinite for a high negative number.
Standard Precision values
real space precisions ; 0.1 nanometer (mm 단위일 때,)
parametric precisions ; default value for a curve with [0,1] parameter space and a length less than 100 meters.
Precision::Angular
2 angle 비교.
현재 값은 Epsilon(2 * PI).
bool areEqualAngles (double theAngle1, double theAngle2)
{
return Abs(theAngle1 - theAngle2) < Precision::Angular();
}
평행 벡터?
bool areParallelVectors (const gp_Vec& theVec1, const gp_Vec& theVec2)
{
return theVec1.IsParallel (theVec2, Precision::Angular());
}
dot, cross products에 모두 사용될 수 있다.
bool arePerpendicular (const gp_Dir& theDir1, const gp_Dir& theDir2)
{
return Abs(theDir1 * theDir2) < Precision::Angular();
}
Precision::Confusion
3D 거리 테스트에 사용. 현재 값은 1.e-7, 즉 1/10 micron. (mm 단위일 때,)
bool areEqualPoints (const gp_Pnt& thePnt1, const gp_Pnt& thePnt2)
{
return thePnt1.IsEqual (thePnt2, Precision::Confusion());
}
bool isNullVector (const gp_Vec& theVec)
{
return theVec.Magnitude() < Precision::Confusion();
}
Precision::Intersection
reasonable precision to pass to an Intersection process as a limit of refinement of Intersection Points.
현재 값은 Confusion() / 100.
Precision::Approximation
reasonable precision to pass to an approximation process as a limit of refinement of fitting.
시간이 중요 요소일 때 사용.
현재 값은 Confusion() * 10.