Developer documentation | Axl-2.5.1

axlSamplingCurve.cpp
Go to the documentation of this file.
1 #include "axlSamplingCurve.h"
3 
4 axlPoint *rotatePoint(double a, double b, double c, double x, double y,double z, double u, double v, double w, double alpha)
5 {
6  axlPoint *res = new axlPoint();
7  qreal a1 = (a*(qPow(v,2)+qPow(w,2))-u*(b*v+c*w-u*x-v*y-w*z))*(1-qCos(alpha))+x*qCos(alpha)+(-c*v+b*w-w*y+v*z)*qSin(alpha);
8  qreal a2 = (b*(qPow(u,2)+qPow(w,2))-v*(a*u+c*w-u*x-v*y-w*z))*(1-qCos(alpha))+y*qCos(alpha)+(c*u-a*w+w*x-u*z)*qSin(alpha);
9  qreal a3 = (c*(qPow(u,2)+qPow(v,2))-w*(a*u+b*v-u*x-v*y-w*z))*(1-qCos(alpha))+z*qCos(alpha)+(-b*u+a*v-v*x+u*y)*qSin(alpha);
10  res->setValues(a1,a2,a3);
11  return res;
12 };
13 
15 {
16  return rotatePoint(a.x(),a.y(),a.z(),x.x(),x.y(),x.z(),u.x(),u.y(),u.z(),alpha);
17 };
18 
19 axlPoint* RotatePoint(axlLine *line,axlPoint *x, double alpha)
20 {
21  axlPoint dir = line->secondPoint()->operator-(line->firstPoint());
22  dir.normalize();//use better function
23  return RotatePoint(*(line->firstPoint()),*x,dir,alpha);
24 }
25 
26 void normalize(axlPoint& point)
27 {
28  double n = point.norm();
29  if(n>1e-5)
30  {
31  point.setValues(point.x()/n,point.y()/n,point.z()/n);
32  }
33 }
34 //computing the angle of vector vCompute if you choose the vRef is origin (where the angle = 0) and the result will be between [-PI, PI].
35 //Vector normal here is very important, because, the angle is computing by the counter-clockwise with the direction of vector go out of plane.
36 //For more details, see the atan2() of c++ references or google "c++ atan2".
37 double angle(axlPoint vCompute, axlPoint vRef, axlPoint normal)
38 {
39  normalize(vCompute);
40  normalize(vRef);
41  normalize(normal);
42  axlPoint y = axlPoint::crossProduct(normal,vRef);
43  return std::atan2(axlPoint::dotProduct(vCompute,y),axlPoint::dotProduct(vCompute,vRef));
44 }
45 
46 //conputing the angle of arc with base on the function angle().
47 double angleOfArc(const axlCircleArc *arc)
48 {
49  axlPoint p1 = arc->point1(), p2 = arc->point2();
50 
51 
52  if(std::abs(axlPoint::crossProduct(arc->point1()-arc->center(),arc->point2()-arc->center()).norm())<1e-5)
53  {
54  if(arc->isDirect())
55  return M_PI;
56  else
57  return -M_PI;
58  }
59  if(!arc->isDirect())
60  qSwap(p1,p2);
61 
62  double ang = angle(p2-arc->center(),p1-arc->center(),arc->normal());
63  if(ang<0)
64  return ang+ 2*M_PI;
65  return ang;
66 }
68 {
69 
70  if(std::abs(axlPoint::crossProduct(arc.point1()-arc.center(),arc.point2()-arc.center()).norm())<1e-5)
71  {
72  if(arc.isDirect())
73  return arc.normal();
74  else
75  return arc.normal()*(-1);
76  }
77  else
78  return arc.calculateNormal();
79 
80 }
81 QList<axlPoint *> Sampling(axlAbstractCurve *curve)
82 {
83  QList<axlPoint *> result;
84 
85  if(axlLine *line = dynamic_cast<axlLine *>(curve))
86  {
87  result.append(line->firstPoint());
88  result.append(line->secondPoint());
89  }
90  else if(axlCircleArc *ca = dynamic_cast<axlCircleArc *>(curve))
91  {
92  double step = angleOfArc(ca)/40;
93  axlPoint nor = ca->normal()/*normalCCWArc(*ca)*/;
94  nor.normalize();
95  /*qDebug()<<"angle = "<<angleOfArc(ca);
96  qDebug()<<"nor = "<< nor.description();*/
97  axlPoint *inter;
98  if(ca->isDirect())
99  inter = new axlPoint(ca->point1());
100  else
101  inter = new axlPoint(ca->point2());
102  for(int i = 0; i<=40;i++)
103  {
104  result.append(RotatePoint(new axlLine(ca->center(),ca->center()+nor),inter,i*step));
105  }
106  }
107  else if(axlAbstractCurveBSpline *curvepara = dynamic_cast<axlAbstractCurveBSpline *>(curve))
108  {
109  double step = (curvepara->endParam()-curvepara->startParam())/40;
110  for(int i = 0;i<41;i++)
111  {
112  result.append(new axlPoint(curvepara->eval(curvepara->startParam()+i*step)));
113  }
114 
115  }
116  return result;
117 }
bool isDirect(void) const
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
Class axlLine defines 3D lines.
Definition: axlLine.h:35
axlPoint point1(void) const
axlPoint * secondPoint(void) const
Returns second point of the line.
Definition: axlLine.cpp:137
#define M_PI
axlPoint center(void) const
axlPoint * RotatePoint(axlPoint a, axlPoint x, axlPoint u, double alpha)
axlPoint * firstPoint(void) const
Returns first point of the line.
Definition: axlLine.cpp:128
void normalize(void)
Definition: axlPoint.cpp:410
double norm(void) const
Definition: axlPoint.cpp:450
axlPoint point2(void) const
double angle(axlPoint vCompute, axlPoint vRef, axlPoint normal)
QList< axlPoint * > Sampling(axlAbstractCurve *curve)
static double dotProduct(const axlPoint &lhs, const axlPoint &rhs)
Definition: axlPoint.cpp:508
axlPoint normalCCWArc(const axlCircleArc &arc)
void normalize(axlPoint &point)
double y
Definition: axlPoint.h:37
axlPoint calculateNormal(void) const
axlPoint * rotatePoint(double a, double b, double c, double x, double y, double z, double u, double v, double w, double alpha)
axlPoint normal(void) const
double z
Definition: axlPoint.h:38
static axlPoint crossProduct(const axlPoint &lhs, const axlPoint &rhs)
Returns the cross product between lhs (coords) and rhs (coords).
Definition: axlPoint.cpp:485
double x
Definition: axlPoint.h:37
double angleOfArc(const axlCircleArc *arc)