Developer documentation | Axl-2.5.1

axlCompositeCurveConverter.cpp
Go to the documentation of this file.
2 #include "axlCompositeCurve.h"
3 
4 
5 #include <axlCore/axlMesh.h>
6 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
7 
8 
9 class axlCompositeCurveConverterPrivate
10 {
11 public:
12  axlCompositeCurve *data;
13 };
14 
16 {
17  d->data = NULL;
18 };
19 
21 {
22  delete d;
23  d = NULL;
24 }
25 
27 {
28  return "Converter from axlCompositeCurve to axlMesh";
29 }
30 
32 {
33  return "axlCompositeCurveConverter";
34 }
35 
37 {
38  return QStringList() << "axlCompositeCurveConverter" << "axlCompositeCurve";
39 }
41 {
42  return "axlMesh";
43 }
44 
46 {
47  return dtkAbstractDataFactory::instance()->registerDataConverterType("axlCompositeCurveConverter", QStringList(), "axlMesh", createaxlCompositeCurveConverter);
48 }
49 
50 //this function just use whenthe meshes have no face.
51 void axlCompositeCurveConverter::addMesh2Mesh(axlMesh& mother_mesh, axlMesh *child_mesh)
52 {
53  QVector<axlMesh::Edge> edge_of_child_mesh = child_mesh->edgeSet();
54  int count_vertex = mother_mesh.vertex_count();
55  qDebug()<<"count_vertex = "<<count_vertex;
56  qDebug()<<"child_mesh = "<<child_mesh->vertex_count();
57  //add vertex from child_mesh to mother_mesh
58  for(int i = 0; i<child_mesh->vertex_count(); ++i)
59  {
60  mother_mesh.push_back_vertex(new axlPoint(child_mesh->vertexX(i),child_mesh->vertexY(i),child_mesh->vertexZ(i)));
61  }
62 
63  //add edge to mother_mesh
64  for(int i = 0; i<edge_of_child_mesh.size(); i++)
65  {
66  QVector<int> temp(edge_of_child_mesh.at(i).size());
67 
68  for(int j = 0; j < edge_of_child_mesh.at(i).size(); ++j)
69  {
70  temp.insert(j, edge_of_child_mesh.at(i).at(j) + count_vertex );
71  if(j==1)
72  {
73  qDebug()<< edge_of_child_mesh.at(i).at(j)<<" ---> "<<edge_of_child_mesh.at(i).at(j) + count_vertex;
74  }
75  }
76 
77  mother_mesh.push_back_edge(temp);
78  }
79 }
80 
81 axlPoint *rotatePoint(double a, double b, double c, double x, double y,double z, double u, double v, double w, double alpha)
82 {
83  axlPoint *res = new axlPoint();
84  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);
85  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);
86  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);
87  res->setCoordinates(a1,a2,a3);
88  return res;
89 };
90 
92 {
93  return rotatePoint(a.x(),a.y(),a.z(),x.x(),x.y(),x.z(),u.x(),u.y(),u.z(),alpha);
94 };
95 
96 axlPoint* RotatePoint(axlLine *line,axlPoint *x, double alpha)
97 {
98  axlPoint dir = line->secondPoint()->operator-(line->firstPoint());
99  dir.normalize();//use better function
100  return RotatePoint(*(line->firstPoint()),*x,dir,alpha);
101 }
102 
103 void normalize(axlPoint& point)
104 {
105  double n = point.norm();
106  if(n>1e-5)
107  {
108  point.setCoordinates(point.x()/n,point.y()/n,point.z()/n);
109  }
110 }
111 //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].
112 //Vector normal here is very important, because, the angle is computing by the counter-clockwise with the direction of vector go out of plane.
113 //For more details, see the atan2() of c++ references or google "c++ atan2".
114 double angle(axlPoint vCompute, axlPoint vRef, axlPoint normal)
115 {
116  normalize(vCompute);
117  normalize(vRef);
118  normalize(normal);
119  axlPoint y = axlPoint::crossProduct(normal,vRef);
120  return std::atan2(axlPoint::dotProduct(vCompute,y),axlPoint::dotProduct(vCompute,vRef));
121 }
122 
123 //conputing the angle of arc with base on the function angle().
124 double angleOfArc(const axlCircleArc *arc)
125 {
126  axlPoint p1 = arc->point1(), p2 = arc->point2();
127 
128 
129  if(std::abs(axlPoint::crossProduct(arc->point1()-arc->center(),arc->point2()-arc->center()).norm())<1e-5)
130  {
131  if(arc->isDirect())
132  return M_PI;
133  else
134  return -M_PI;
135  }
136  if(!arc->isDirect())
137  qSwap(p1,p2);
138 
139  double ang = angle(p2-arc->center(),p1-arc->center(),arc->normal());
140  if(ang<0)
141  return ang+ 2*M_PI;
142  return ang;
143 }
145 {
146 
147  if(std::abs(axlPoint::crossProduct(arc.point1()-arc.center(),arc.point2()-arc.center()).norm())<1e-5)
148  {
149  if(arc.isDirect())
150  return arc.normal();
151  else
152  return arc.normal()*(-1);
153  }
154  else
155  return arc.calculateNormal();
156 
157 }
158 QList<axlPoint *> Sampling(axlAbstractCurve *curve)
159 {
160  QList<axlPoint *> result;
161 
162  if(axlLine *line = dynamic_cast<axlLine *>(curve))
163  {
164  result.append(line->firstPoint());
165  result.append(line->secondPoint());
166  }
167  else if(axlCircleArc *ca = dynamic_cast<axlCircleArc *>(curve))
168  {
169  double step = angleOfArc(ca)/40;
170  axlPoint nor = ca->normal()/*normalCCWArc(*ca)*/;
171  nor.normalize();
172  /*qDebug()<<"angle = "<<angleOfArc(ca);
173  qDebug()<<"nor = "<< nor.description();*/
174  axlPoint *inter;
175  if(ca->isDirect())
176  inter = new axlPoint(ca->point1());
177  else
178  inter = new axlPoint(ca->point2());
179  for(int i = 0; i<=40;i++)
180  {
181  result.append(RotatePoint(new axlLine(ca->center(),ca->center()+nor),inter,i*step));
182  }
183  }
184  else if(axlAbstractCurveBSpline *curvepara = dynamic_cast<axlAbstractCurveBSpline *>(curve))
185  {
186  double step = (curvepara->endParam()-curvepara->startParam())/40;
187  for(int i = 0;i<41;i++)
188  {
189  result.append(new axlPoint(curvepara->eval(curvepara->startParam()+i*step)));
190  }
191 
192  }
193  return result;
194 }
195 
197 {
198  if(!d->data)
199  return NULL;
200 
201 
202  axlMesh *mesh = new axlMesh();
203  if(d->data->getCase())
204  {
205  QList<QList<axlPoint *> > $list;
206  for(int i = 0; i<d->data->count_curve(); ++i)
207  {
208  $list.append(Sampling(d->data->get_pt_curve_at_id(i)));
209  }
210 
211  for(int i = 0; i<$list.size(); ++i)
212  {
213  int count = mesh->vertex_count();
214  for(int j = 0; j<$list.at(i).size(); ++j)
215  {
216  mesh->push_back_vertex($list.at(i).value(j));
217  if(j!=0)
218  mesh->push_back_edge( count + j - 1, count + j );
219  }
220  }
221 
222  mesh->vertex_show() = false;
223  mesh->normal_used() = false;
224  mesh->color_used() = false;
225  mesh->edge_show() = true;
226  mesh->face_show() = false;
227  }
228  else
229  {
230  mesh->push_back_vertex(d->data->get_start_point());
231  mesh->push_back_vertex(d->data->get_end_point());
232 
233  mesh->vertex_show() = true;
234  mesh->normal_used() = false;
235  mesh->color_used() = false;
236  mesh->edge_show() = false;
237  mesh->face_show() = false;
238  }
239 
240  return mesh;
241 }
242 
243 void axlCompositeCurveConverter::setData(dtkAbstractData *data)
244 {
245  if(axlCompositeCurve *Data = dynamic_cast<axlCompositeCurve *>(data))
246  d->data = Data;
247 }
248 
249 dtkAbstractDataConverter *createaxlCompositeCurveConverter(void)
250 {
251  return new axlCompositeCurveConverter;
252 }
253 
254 
255 
256 //===================================================================================
257 
258 //@from toMesh()
259 
260 /*
261 
262  axlAbstractCurve *curve = d->data->get_pt_curve_at_id(i);
263  if(axlLine *line = dynamic_cast<axlLine *>(curve))
264  {
265  mesh->push_back_vertex(line->firstPoint());
266  mesh->push_back_vertex(line->secondPoint());
267  mesh->push_back_edge(mesh->vertex_count() -2, mesh->vertex_count() -1);
268  }
269  else if(axlCircleArc *arc = dynamic_cast<axlCircleArc *>(curve))
270  {
271  int index = mesh->vertex_count();
272  mesh->push_back_vertex(arc->point1());
273  mesh->push_back_vertex(arc->point2());
274 
275  qDebug()<<"curve:"<< curve->description();
276  QString converterName = curve->identifier()+"Converter";
277 
278  axlAbstractDataConverter* converter =
279  dynamic_cast<axlAbstractDataConverter*>(dtkAbstractDataFactory
280  ::instance()->converter(converterName));
281 
282  if (converter) {
283  converter->setData(curve);
284  converter->setParams(0, mesh->vertex_count());
285  converter->setParams(1, converter->ge);
286  converter->setOutput(mesh);
287  addMesh2Mesh(*mesh,converter->toMesh());
288  }
289  }
290 
291 
292 
293 
294 
295 */
bool isDirect(void) const
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
double vertexZ(const int &ind) const
return Z coordinates of vertex with index ind.
Definition: axlMesh.cpp:313
QVector< Edge > edgeSet(void) const
Definition: axlMesh.cpp:645
Class axlLine defines 3D lines.
Definition: axlLine.h:35
axlPoint * RotatePoint(axlPoint a, axlPoint x, axlPoint u, double alpha)
bool vertex_show(void) const
Definition: axlMesh.cpp:159
axlPoint point1(void) const
void push_back_edge(int, int)
Definition: axlMesh.cpp:669
void normalize(axlPoint &point)
double angleOfArc(const axlCircleArc *arc)
axlPoint * secondPoint(void) const
Returns second point of the line.
Definition: axlLine.cpp:137
#define M_PI
bool normal_used(void) const
Definition: axlMesh.cpp:170
axlPoint center(void) const
axlPoint normalCCWArc(const axlCircleArc &arc)
bool edge_show(void) const
Definition: axlMesh.cpp:190
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
dtkAbstractDataConverter * createaxlCompositeCurveConverter(void)
axlPoint point2(void) const
double vertexY(const int &ind) const
return Y coordinates of vertex with index ind.
Definition: axlMesh.cpp:304
void setData(dtkAbstractData *data)
static double dotProduct(const axlPoint &lhs, const axlPoint &rhs)
Definition: axlPoint.cpp:508
axlPoint * rotatePoint(double a, double b, double c, double x, double y, double z, double u, double v, double w, double alpha)
QList< axlPoint * > Sampling(axlAbstractCurve *curve)
bool face_show(void) const
Definition: axlMesh.cpp:200
double y
Definition: axlPoint.h:37
axlPoint calculateNormal(void) const
bool color_used(void) const
Definition: axlMesh.cpp:180
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 vertexX(const int &ind) const
Return X coordinates of vertex with index ind.
Definition: axlMesh.cpp:295
double x
Definition: axlPoint.h:37
void setCoordinates(double x, double y, double z)
Change coordinates of this point.
Definition: axlPoint.cpp:370
int vertex_count(void) const
Definition: axlMesh.cpp:122
Class axlMesh defines a piecewise-linear 3D object.
Definition: axlMesh.h:41
double angle(axlPoint vCompute, axlPoint vRef, axlPoint normal)
void push_back_vertex(const double &x, const double &y, const double &z)
Add a new vertex to the mesh.
Definition: axlMesh.cpp:333