Developer documentation | Axl-2.5.1

axlProcessProjection.cpp
Go to the documentation of this file.
1 /* axlProcessProjection.cpp ---
2  *
3  * Author: Anais Ducoffe
4  * Copyright (C) 2013, Anais Ducoffe, INRIA.
5  */
6 
7 /* Commentary:
8  */
9 
10 /* Change log:
11  *
12  */
13 #include "axlProcessProjection.h"
14 #include <axlCore/axlPoint.h>
15 #include <axlCore/axlLine.h>
17 
18 #include <dtkCoreSupport/dtkAbstractProcessFactory.h>
19 
23 
24 class axlProcessProjectionPrivate{
25 
26 public :
27  QPair <axlPoint *, axlLine *> inputs;
28 
29 
30 };
31 
35 axlProcessProjection::axlProcessProjection(QObject *parent) : axlAbstractProcess(), d(new axlProcessProjectionPrivate){
36 
37 }
38 
40  delete d;
41  d = NULL;
42 }
43 
44 
46  if(channel == 0){
47  return d->inputs.first;
48  }else{
49  return d->inputs.second;
50  }
51 }
52 
53 
55  d->inputs.first = dynamic_cast<axlPoint *>(process->output());
56 }
57 
59  return false;
60 }
61 
62 
64 {
65 
66  if (d->inputs.first && d->inputs.second) {
67  double xa = d->inputs.second->firstPoint()->x();
68  double ya = d->inputs.second->firstPoint()->y();
69  double za = d->inputs.second->firstPoint()->z();
70 
71  double a = d->inputs.second->secondPoint()->x() - xa;
72  double b = d->inputs.second->secondPoint()->y() - ya;
73  double c = d->inputs.second->secondPoint()->z() - za;
74 
75  double x = d->inputs.first->x();
76  double y = d->inputs.first->y();
77  double z = d->inputs.first->z();
78 
79 
80  if ( d->inputs.first->isEqualTo(d->inputs.second->firstPoint()) ||
81  d->inputs.first->isEqualTo(d->inputs.second->secondPoint()) ) {
82  return true;
83  }
84 
85  double signe = 0;
86  if (d->inputs.second->planeDirection() < 0) {
87  signe = -1;
88  } else if(d->inputs.second->planeDirection() > 0) {
89  signe = 1;
90  }
91 
92  if (a==0 && b ==0 && c ==0) {
93  return (x==xa && y ==ya && z==za);
94  } else {
95  double ta = 0;
96  double tb = 0;
97  double tc = 0;
98 
99  QList<double> hasBeenComputed;
100 
101  if (a !=0) {
102  ta = (x-xa)/a;
103  hasBeenComputed << ta;
104  }
105  if (b !=0) {
106  tb = (y-ya)/b;
107  hasBeenComputed << tb;
108  }
109  if (c !=0) {
110  tc = (z-za)/c;
111  hasBeenComputed << tc;
112  }
113  if (hasBeenComputed.size() ==1) {
114  return (signe *hasBeenComputed.at(0)> 0 && signe*hasBeenComputed.at(0) < 1);
115  } else {
116  bool areEqual = true;
117  for (int i = 0; i < hasBeenComputed.size()-1; i++) {
118  if (!(hasBeenComputed.at(i)== hasBeenComputed.at(i+1))) {
119  areEqual = false;
120  }
121  }
122  return areEqual;
123  }
124  }
125  } else {
126  dtkWarn() << "No inputs available";
127  return false;
128  }
129 
130 }
131 
132 
133 dtkAbstractData *axlProcessProjection::output(void){
134  return d->inputs.first;
135 }
136 
137 
138 void axlProcessProjection::setInput(dtkAbstractData *newData, int channel){
139  if(channel == 0){
140  if(dynamic_cast<axlPoint *>(newData)){
141  axlPoint *point = dynamic_cast<axlPoint *>(newData);
142  d->inputs.first = point;
143  }else{
144  qDebug() << "is not a point.";
145  }
146  }else if(channel == 1){
147  if(dynamic_cast<axlLine *>(newData)){
148  axlLine *line = dynamic_cast<axlLine *>(newData);
149  d->inputs.second = line;
150  }else{
151  qDebug() << "is not a line.";
152  }
153  }else{
154  qDebug() << "channel has only two possible values : 0 and 1.";
155  }
156 }
157 
158 
160 
161 
162  if(isOnLine()){
163  return 1;
164  }
165 
166 
167  double AB = axlPoint::distance(d->inputs.second->secondPoint(), d->inputs.second->firstPoint());
168  double ABcarre = AB*AB;
169 
170 
171  if(ABcarre == 0){
172  d->inputs.first->setCoordinates(d->inputs.second->firstPoint()->x(),d->inputs.second->firstPoint()->y(),d->inputs.second->firstPoint()->z());
173  }else{
174 
175  axlPoint point1;
176  axlPoint point2;
177  point1.setCoordinates(d->inputs.second->firstPoint()->x()-d->inputs.first->x(),d->inputs.second->firstPoint()->y()-d->inputs.first->y() ,d->inputs.second->firstPoint()->z()-d->inputs.first->z() );
178  point2.setCoordinates(d->inputs.second->secondPoint()->x()-d->inputs.second->firstPoint()->x(),d->inputs.second->secondPoint()->y()-d->inputs.second->firstPoint()->y() ,d->inputs.second->secondPoint()->z()-d->inputs.second->firstPoint()->z() );
179  double scalarProduct =axlPoint::dotProduct(point1,point2);
180  scalarProduct = -1*scalarProduct;
181 
182  double t = scalarProduct/ABcarre;
183 
184  double x = d->inputs.second->firstPoint()->x() + t*(d->inputs.second->secondPoint()->x() - d->inputs.second->firstPoint()->x());
185  double y = d->inputs.second->firstPoint()->y() + t*(d->inputs.second->secondPoint()->y() - d->inputs.second->firstPoint()->y());
186  double z = d->inputs.second->firstPoint()->z() + t*(d->inputs.second->secondPoint()->z() - d->inputs.second->firstPoint()->z());
187  d->inputs.first->setCoordinates(x,y,z);
188  }
189 
190  return 1;
191 
192 }
193 
195  return "if the point is not on the line, axlProcessProjection projects it on the line";
196 }
197 
198 
200  return "axlProcessProjection";
201 }
202 
203 
205  return dtkAbstractProcessFactory::instance()->registerProcessType("axlProcessProjection", createaxlProcessProjection, "axlAbstractProcess");
206 }
207 
208 dtkAbstractProcess *createaxlProcessProjection(void){
209 
210  return new axlProcessProjection;
211 }
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
virtual axlAbstractData * getInput(int channel) const
virtual ~axlProcessProjection(void)
Class axlLine defines 3D lines.
Definition: axlLine.h:35
virtual dtkAbstractData * output(void)
virtual QString identifier(void) const
void copyProcess(axlAbstractProcess *process)
static bool registered(void)
virtual void setInput(dtkAbstractData *newData, int channel)
static double dotProduct(const axlPoint &lhs, const axlPoint &rhs)
Definition: axlPoint.cpp:508
virtual QString description(void) const
axlProcessProjection(QObject *parent=0)
static double distance(const axlPoint &lhs, const axlPoint &rhs)
Returns the distance between lhs point and rhs point.
Definition: axlPoint.cpp:459
void setCoordinates(double x, double y, double z)
Change coordinates of this point.
Definition: axlPoint.cpp:370
dtkAbstractProcess * createaxlProcessProjection(void)
Class axlAbstractData defines an API for all type of axel data.