Developer documentation | Axl-2.5.1

axlLineConverter.cpp
Go to the documentation of this file.
1 /* axlLineConverter.cpp ---
2  *
3  * Author: Valentin Michelet
4  * Copyright (C) 2008-2011 - Valentin Michelet, Inria.
5  * Created: Tue Apr 30 11:03:18 2012 (+0100)
6  * Version: $Id$
7  * Update #: 50
8  */
9 
10 /* Commentary:
11  *
12  */
13 
14 /* Change log:
15  *
16  */
17 
18 #include "axlMesh.h"
19 #include "axlLine.h"
20 #include "axlLineConverter.h"
21 #include <dtkCoreSupport/dtkAbstractDataFactory.h>
22 #include "axlPoint.h"
23 
24 #include <cmath>
25 
26 class axlLineConverterPrivate {
27 public:
28  axlLine* data;
29  axlMesh* output;
30  int startIndex;
31  int endIndex;
32  int nbsegments;
33  double precision;
34 };
35 
36 axlLineConverter::axlLineConverter(void) : axlAbstractDataConverter(), d(new axlLineConverterPrivate) {
37  d->data = NULL;
38  d->output = NULL;
39  d->startIndex = -1;
40  d->endIndex = -1;
41  d->nbsegments = 1;
42  d->precision = 0.1;
43 }
44 
46  delete d;
47  d = NULL;
48 }
49 
50 QString axlLineConverter::description(void) const {
51  return "Converter from axlLineConverter to axlMesh";
52 }
53 
55  return "axlLineConverter";
56 }
57 
58 QStringList axlLineConverter::fromTypes(void) const {
59  return QStringList() << "axlLineConverter" << "axlLine";
60 }
61 
62 QString axlLineConverter::toType (void) const {
63  return "axlMesh";
64 }
65 
67  return dtkAbstractDataFactory::instance()->registerDataConverterType("axlLineConverter", QStringList(), "axlMesh", createaxlLineConverter);
68 }
69 
71  if(!d->data)
72  return NULL;
73 
74  if (!d->output) {
75  d->output = new axlMesh;
76  }
77 
78  // Compute segment length
79  double length = axlPoint::distance(d->data->firstPoint(), d->data->secondPoint());
80  // Number of edges within mesh, according to precision and length
81  int nbEdges = d->nbsegments;//std::ceil(length/d->precision);
82 
83  // Determine segment direction vector
84  axlPoint direction(d->data->secondPoint()->operator -(d->data->firstPoint()));
85  // Normalize it
86  direction.normalize();
87 
88  // Create new edge to record vertices list
89  int arcIndex = d->output->push_back_new_edge();
90 
91  // If start and end points don't already exist
92  if (d->startIndex == -1 && d->endIndex == -1) {
93  // Add vertices to mesh
94  for (int k = 0; k <= nbEdges; k++) {
95  // Compute current shift value from first point
96  double step = (length*k)/nbEdges;
97  // Current point
98  axlPoint currPoint(d->data->firstPoint()->operator + (direction*step));
99  // Add current point to mesh
100  d->output->push_back_vertex(currPoint);
101  }
102 
103  // Get number of points within mesh
104  int nbPointsInMesh = d->output->vertex_count();
105 
106  // Add edge between first and second points of circle arc
107  d->output->edgePushBack(arcIndex, nbPointsInMesh-nbEdges-1);
108 
109  // Add other edges to mesh
110  for (int k = 0; k < nbPointsInMesh-1; k++) {
111  d->output->edgePushBack(arcIndex, k+1);
112  }
113 
114  // Add edge between last last and last points of circle arc
115  d->output->edgePushBack(arcIndex, nbPointsInMesh-2);
116  // If start and end points already exist
117  } else if (d->startIndex != -1 && d->endIndex != -1) {
118  // Add edge between first and second points of circle arc
119  d->output->edgePushBack(arcIndex, d->startIndex);
120 
121  // Add vertices to mesh
122  for (int k = 1; k <= nbEdges-1; k++) {
123  // Compute current shift value from first point
124  double step = (length*k)/nbEdges;
125  // Current point
126  axlPoint currPoint(d->data->firstPoint()->operator +(direction*step));
127  // Add current edge to mesh
128  d->output->edgePushBack(arcIndex, d->output->vertex_count());
129  // Add current point to mesh
130  d->output->push_back_vertex(currPoint);
131  }
132 
133  // Add edge between last last and last points of circle arc
134  d->output->edgePushBack(arcIndex, d->endIndex);
135  } else {
136  dtkError() << "Error within axlLineConverter::toMesh: one point already exists and not the other one.";
137  }
138 
139  // Set mesh parameters
140  d->output->vertex_show() = false;
141  d->output->normal_used() = false;
142  d->output->color_used() = true;
143  d->output->edge_show() = true;
144  d->output->face_show() = false;
145  d->output->isPlanar() = true;
146 
147  d->output->setColor(d->data->color());
148  d->output->setOpacity(d->data->opacity());
149  d->output->setShader(d->data->shader());
150 
151  return d->output;
152 }
153 
154 void axlLineConverter::setData(dtkAbstractData* data) {
155  if(axlLine* line = dynamic_cast<axlLine*>(data))
156  d->data = line;
157 }
158 
159 void axlLineConverter::setParams(int channel, int index) {
160  if (channel == 0)
161  d->startIndex = index;
162  else if (channel == 1)
163  d->endIndex = index;
164  else
165  dtkWarn() << "axlLineConverter::setParams usage: channel must be 0 or 1.";
166 }
167 
169  d->output = static_cast<axlMesh*>(output);
170 }
171 
173  d->precision = eps;
174 }
175 
176 dtkAbstractDataConverter* createaxlLineConverter(void) {
177  return new axlLineConverter;
178 }
void setData(dtkAbstractData *data)
QString toType(void) const
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
Class axlLine defines 3D lines.
Definition: axlLine.h:35
void setPrecision(double eps)
virtual ~axlLineConverter(void)
void setOutput(axlAbstractData *output)
QString identifier(void) const
void normalize(void)
Definition: axlPoint.cpp:410
QString description(void) const
dtkAbstractDataConverter * createaxlLineConverter(void)
QStringList fromTypes(void) const
static bool registered(void)
void setParams(int channel, int index)
static double distance(const axlPoint &lhs, const axlPoint &rhs)
Returns the distance between lhs point and rhs point.
Definition: axlPoint.cpp:459
Class axlAbstractData defines an API for all type of axel data.
axlMesh * toMesh(void)
Class axlMesh defines a piecewise-linear 3D object.
Definition: axlMesh.h:41