Developer documentation | Axl-2.5.1

axlActorSurfaceTrimmed.cpp
Go to the documentation of this file.
1 /* axlActorSurfaceTrimmed.cpp ---
2  *
3  * Author: Meriadeg Perrinel
4  * Copyright (C) 2008 - Meriadeg Perrinel, Inria.
5  * Created: Fri Dec 17 11:06:08 2010 (+0100)
6  * Version: $Id$
7  * Last-Updated: Mon Dec 17 15:04:32 2012 (+0100)
8  * By: Julien Wintz
9  * Update #: 76
10  */
11 
12 /* Commentary:
13  *
14  */
15 
16 /* Change log:
17  *
18  */
19 
20 #include "axlActorSurfaceTrimmed.h"
21 #include "axlControlPointsWidget.h"
22 
24 
25 #include <dtkMathSupport/dtkVector3D.h>
26 
27 #include <vtkActor.h>
28 #include <vtkActorCollection.h>
29 #include <vtkCellArray.h>
30 #include <vtkCellData.h>
31 #include <vtkCommand.h>
32 #include <vtkDoubleArray.h>
33 #include <vtkLookupTable.h>
34 #include <vtkObjectFactory.h>
35 //#include <vtkPainterPolyDataMapper.h>
36 #include <vtkPoints.h>
37 #include <vtkPointData.h>
38 #include <vtkPolyData.h>
39 #include <vtkPolyVertex.h>
40 #include <vtkPolygon.h>
41 #include <vtkPolyDataMapper.h>
42 #include <vtkPolyDataNormals.h>
43 #include <vtkProperty.h>
44 #include <vtkProp.h>
45 #include <vtkQuad.h>
46 #include <vtkTexture.h>
47 #include <vtkTriangle.h>
48 #include <vtkTimerLog.h>
49 #include <vtkSmartPointer.h>
50 
51 // /////////////////////////////////////////////////////////////////
52 // axlActorSurfaceTrimmedPrivate
53 // /////////////////////////////////////////////////////////////////
54 
55 class axlActorSurfaceTrimmedPrivate
56 {
57 public:
58  axlAbstractSurfaceTrimmed *splineSurface;
59 };
60 
61 #if (VTK_MAJOR_VERSION <= 5)
62 vtkCxxRevisionMacro(axlActorSurfaceTrimmed, "$Revision: 0.0.1 $");
63 #endif
64 
66 
67 dtkAbstractData *axlActorSurfaceTrimmed::data(void)
68 {
69  return d->splineSurface;
70 }
71 
73 {
74  // we compute here points, triangles, and normals
75  d->splineSurface = spline_Surface;
76 
77  this->setPoints(vtkSmartPointer<vtkPoints>::New());
78  this->setActor(vtkSmartPointer<vtkActor>::New());
79  this->setCellArray(vtkSmartPointer<vtkCellArray>::New());
80  this->setPolyData(vtkSmartPointer<vtkPolyData>::New());
81  this->setMapper(vtkSmartPointer<vtkPolyDataMapper>::New());
82 
83 
84  this->mehsProcess();
85 
86  this->setMapperCollorArray();
87 
88 
89  /*// add sphere param
90  this->initCurrentPoint();*/
91  vtkProp3D *prop3d = this->getActor();
92  this->AddPart(prop3d);
93 
94  // add the observer
95  if(!this->getObserver())
96  {
97  this->NewObserver();
98  this->setObserverData(d->splineSurface);
99  }
100 
101  QString shader = d->splineSurface->shader();
102  if(!shader.isEmpty())
103  this->setShader(shader);
104 
105 
106 }
107 
108 void axlActorSurfaceTrimmed::mehsProcess(void)
109 {
110 
111  double start_u = d->splineSurface->startParam_u();
112  double start_v = d->splineSurface->startParam_v();
113  double end_u = d->splineSurface->endParam_u();
114  double end_v = d->splineSurface->endParam_v();
115  double paramCourant_u = start_u;
116  double paramCourant_v = start_v;
117 
118  axlPoint pointCourant;
119 
120  int n_u = d->splineSurface->numSamples_u();// need to be superior than 1
121  int n_v = d->splineSurface->numSamples_v();
122 
123 
124  double interval_u = (double)(end_u - start_u) / (n_u - 1);
125  double interval_v = (double)(end_v - start_v) / (n_v - 1);
126 
127  vtkSmartPointer<vtkDoubleArray> normals = vtkSmartPointer<vtkDoubleArray>::New();
128  normals->SetNumberOfComponents(3);
129  normals->SetNumberOfTuples(n_u * n_v);
130  normals->SetName("normalArray");
131  double *currentNormal = new double[3];
132 
133  dtkDeprecated::dtkVector<bool> inDomain;
134  inDomain.allocate(n_u * n_v);
135 
136  dtkDeprecated::dtkVector<double> normal;
137  for(int j = 0; j < n_v - 1; j++)
138  {
139  for(int i = 0; i < n_u - 1; i++)
140  {
141  if(d->splineSurface->inDomain(paramCourant_u, paramCourant_v))
142  inDomain[j * n_u + i] = true;
143  else
144  inDomain[j * n_u + i] = false;
145 
146  pointCourant = d->splineSurface->eval(paramCourant_u, paramCourant_v);
147  normal = d->splineSurface->normal(paramCourant_u, paramCourant_v).unit();
148  currentNormal[0] = normal[0];
149  currentNormal[1] = normal[1];
150  currentNormal[2] = normal[2];
151  normals->SetTuple(j * n_u + i, currentNormal);
152 
153  this->getPoints()->InsertNextPoint(pointCourant.x(), pointCourant.y(), pointCourant.z());
154  paramCourant_u += interval_u;
155  }
156 
157  if(d->splineSurface->inDomain(end_u, paramCourant_v))
158  inDomain[j * n_u + n_u - 1] = true;
159  else
160  inDomain[j * n_u + n_u - 1] = false;
161 
162  normal = d->splineSurface->normal(end_u, paramCourant_v).unit();
163  currentNormal[0] = normal[0];
164  currentNormal[1] = normal[1];
165  currentNormal[2] = normal[2];
166  normals->SetTuple(j * n_u + n_u - 1, currentNormal);
167 
168  pointCourant = d->splineSurface->eval(end_u, paramCourant_v);
169  this->getPoints()->InsertNextPoint(pointCourant.x(), pointCourant.y(), pointCourant.z());
170  paramCourant_u = start_u;
171  paramCourant_v += interval_v;
172  }
173 
174  for(int i = 0; i < n_u - 1; i++)
175  {
176  if(d->splineSurface->inDomain(paramCourant_u, end_v))
177  inDomain[(n_v - 1) * n_u + i] = true;
178  else
179  inDomain[(n_v - 1) * n_u + i] = false;
180 
181 
182  normal = d->splineSurface->normal(paramCourant_u, end_v).unit();
183  currentNormal[0] = normal[0];
184  currentNormal[1] = normal[1];
185  currentNormal[2] = normal[2];
186  normals->SetTuple((n_v - 1) * n_u + i, currentNormal);
187 
188  pointCourant=d->splineSurface->eval(paramCourant_u, end_v);
189  this->getPoints()->InsertNextPoint(pointCourant.x(), pointCourant.y(), pointCourant.z());
190  paramCourant_u += interval_u;
191  }
192 
193  if(d->splineSurface->inDomain(end_u, end_v))
194  inDomain[n_v * n_u - 1] = true;
195  else
196  inDomain[n_v * n_u - 1] = false;
197 
198 
199  normal = d->splineSurface->normal(end_u, end_v).unit();
200  currentNormal[0] = normal[0];
201  currentNormal[1] = normal[1];
202  currentNormal[2] = normal[2];
203  normals->SetTuple(n_v * n_u - 1, currentNormal);
204 
205  pointCourant = d->splineSurface->eval(end_u, end_v);
206 
207  this->getPoints()->InsertNextPoint(pointCourant.x() , pointCourant.y(), pointCourant.z());
208 
209  int ind1 = 0;
210  int ind2 = 0;
211 
212  vtkSmartPointer<vtkTriangle> currentTriangle = vtkSmartPointer<vtkTriangle>::New();
213  currentTriangle->GetPointIds()->SetNumberOfIds(3);
214 
215  for(int j = 0; j < n_v - 1; j++)
216  {
217  for(int i= 0; i <n_u - 1; i++)
218  {
219  ind1 = j * n_u + i;
220  ind2 = ind1 + n_u;
221 
222  currentTriangle->GetPointIds()->SetId(0, ind1);
223  currentTriangle->GetPointIds()->SetId(1, ind1 + 1);
224  currentTriangle->GetPointIds()->SetId(2, ind2);
225 
226  if(inDomain[ind1] + inDomain[ind1 + 1] + inDomain[ind2] > 1)
227  this->getCellArray()->InsertNextCell(currentTriangle);
228 
229  currentTriangle->GetPointIds()->SetId(1, ind1 + 1);
230  currentTriangle->GetPointIds()->SetId(0, ind2);
231  currentTriangle->GetPointIds()->SetId(2, ind2 + 1);
232 
233  if(inDomain[ind1] + inDomain[ind1 + 1] + inDomain[ind2] > 1)
234  this->getCellArray()->InsertNextCell(currentTriangle);
235  }
236  }
237 
238  this->getPolyData()->SetPoints(this->getPoints());
239  this->getPolyData()->SetPolys(this->getCellArray());
240  //this->getPolyData()->GetPointData()->SetNormals(normals);
241 
242  vtkSmartPointer<vtkPolyDataNormals> polyDataNormals = vtkSmartPointer<vtkPolyDataNormals>::New();
243  polyDataNormals->AutoOrientNormalsOn();
244  polyDataNormals->FlipNormalsOn();
245 #if (VTK_MAJOR_VERSION <= 5)
246  polyDataNormals->SetInput(this->getPolyData());
247  this->getMapper()->SetInput(polyDataNormals->GetOutput());
248 #else
249  polyDataNormals->SetInputData(this->getPolyData());
250  this->getMapper()->SetInputData(polyDataNormals->GetOutput());
251 #endif
252  this->getActor()->SetMapper(this->getMapper());
253 
254  if(d->splineSurface->fields().count() != 0)
255  d->splineSurface->touchField();
256 
257 }
258 
259 
260 
262 {
263  vtkSmartPointer<vtkDoubleArray> scalarArray = vtkSmartPointer<vtkDoubleArray>::New();
264  scalarArray->SetName("mapperCollorArrayDefaultField");
265  scalarArray->SetNumberOfComponents(1);
266 
267  double start_u = d->splineSurface->startParam_u();
268  double start_v = d->splineSurface->startParam_v();
269  double end_u = d->splineSurface->endParam_u();
270  double end_v = d->splineSurface->endParam_v();
271  double paramCourant_u = start_u;
272  double paramCourant_v = start_v;
273 
274  int n_u = d->splineSurface->numSamples_u();// need to be superior than 1
275  int n_v = d->splineSurface->numSamples_v();
276 
277  scalarArray->SetNumberOfTuples(n_u * n_v);
278 
279  double interval_u = (double)(end_u - start_u) / (n_u - 1);
280  double interval_v = (double)(end_v - start_v) / (n_v - 1);
281 
282  for(int i = 0; i < n_v - 1 ; i++)
283  {
284  for(int j = 0; j < n_u - 1 ; j++)
285  {
286  scalarArray->SetTuple1(i * n_u + j, d->splineSurface->inDomain(paramCourant_u, paramCourant_v) ? 1:0);
287  paramCourant_u += interval_u;
288  }
289 
290  scalarArray->SetTuple1(i * n_u + (n_u - 1), d->splineSurface->inDomain(end_u, paramCourant_v) ? 1:0);
291  paramCourant_u = start_u;
292  paramCourant_v += interval_v;
293  }
294  for(int i = 0; i < n_u - 1; i++)
295  {
296  scalarArray->SetTuple1(n_u * (n_v - 1) + i, d->splineSurface->inDomain(paramCourant_u, end_v) ? 1:0);
297  paramCourant_u += interval_u;
298  }
299  scalarArray->SetTuple1(n_u * n_v - 1, d->splineSurface->inDomain(end_u, end_v) ? 1:0);
300 
301 
302  vtkSmartPointer<vtkPolyData> data = this->getPolyData();
303  data->GetPointData()->AddArray(scalarArray);
304  data->GetPointData()->SetActiveScalars("mapperCollorArrayDefaultField");
305 
306  vtkSmartPointer<vtkLookupTable> lookupTable = vtkSmartPointer<vtkLookupTable>::New();
307  lookupTable->SetRange(0.0, 1.0);
308  lookupTable->SetNumberOfTableValues(2);
309  lookupTable->Build();
310 
311  lookupTable->SetTableValue(0 , 0.0, 0.0, 0.0, 0.0);
312  lookupTable->SetTableValue(1 , 1.0, 1.0, 1.0, 1.0);
313 
314 
315  //add vertex attrib array
316  vtkSmartPointer<vtkPolyDataMapper> mapper = this->getMapper();
317  vtkSmartPointer<vtkPolyData> polyData = this->getPolyData();
318  mapper->SetLookupTable(lookupTable);
319  mapper->SetInterpolateScalarsBeforeMapping(true);
320  mapper->UseLookupTableScalarRangeOn();
321 
322  this->Modified();
323 }
324 
326 {
327  if(control) {
328  if(!this->getControlPoints())
329  {
330  //widget drawing
331 
333  this->getControlPoints()->SetInteractor(this->getInteractor());
334  this->getControlPoints()->SetProp3D(this->getActor());
335  this->getControlPoints()->setSpline(d->splineSurface);
337  this->getControlPoints()->PlaceWidget();
338 
339  this->AddPart(this->getControlPoints()->netActor());
340 
341  this->getControlPoints()->ptsActors()->InitTraversal();
342 
343  for(vtkIdType i = 0; i < this->getControlPoints()->ptsActors()->GetNumberOfItems(); i++)
344  {
345  this->AddPart(this->getControlPoints()->ptsActors()->GetNextActor());
346  }
347  }
348 
349  if(this->getObserver())
350  {
351  this->addToObserver(vtkCommand::InteractionEvent, (vtkCommand *)this->getObserver());
352  }
353 
354 
355  // there is always the controlPoints there
356  this->getControlPoints()->SetEnabled(true);
357  }
358 
359  if (!control)
360  {
361  if (this->getActor()) {
362 #if (VTK_MAJOR_VERSION <= 5)
363  this->getMapper()->SetInput(this->getPolyData());
364 #else
365  this->getMapper()->SetInputData(this->getPolyData());
366 #endif
367 
368  if(this->getControlPoints())
369  {
370  this->RemovePart(this->getControlPoints()->netActor());
371  this->getControlPoints()->ptsActors()->InitTraversal();
372  for(vtkIdType i = 0; i < this->getControlPoints()->ptsActors()->GetNumberOfItems(); i++)
373  {
374  this->RemovePart(this->getControlPoints()->ptsActors()->GetNextActor());
375  }
376 
377  this->getControlPoints()->SetEnabled(false);
378  this->setControlPoints(NULL);
379  }
380  }
381  }
382 }
383 
385 {
386 
387  //qDebug()<<"axlActorSurfaceTrimmed::onSamplingChanged";
388  if(d->splineSurface)
389  {
390  this->getMapper()->RemoveAllInputs();
391  this->getPolyData()->Initialize();
392 
393  // delete current vtkPoint and vtkCellArray
394  this->getPoints()->Reset();
395  this->getCellArray()->Reset();
396 
397  this->getPoints()->Squeeze();
398  this->getCellArray()->Squeeze();
399 
400  this->mehsProcess();
401 
402  }
403 }
404 
405 axlActorSurfaceTrimmed::axlActorSurfaceTrimmed(void) : axlActorSurfaceBSpline(), d(new axlActorSurfaceTrimmedPrivate)
406 {
407 
408 }
409 
411 {
412  delete d;
413 
414  d = NULL;
415 }
Class axlPoint defines 3D points.
Definition: axlPoint.h:34
vtkSmartPointer< vtkCellArray > getCellArray(void)
Definition: axlActor.cpp:307
virtual axlControlPointsWidget * getControlPoints(void)
vtkActorCollection * ptsActors(void)
dtkAbstractData * data(void)
vtkStandardNewMacro(axlActorSurfaceTrimmed)
vtkSmartPointer< vtkPolyData > getPolyData(void)
Definition: axlActor.cpp:295
void setCellArray(vtkSmartPointer< vtkCellArray > cellArray)
Definition: axlActor.cpp:312
static axlControlPointsWidget * New()
vtkCxxRevisionMacro(axlActorSurfaceTrimmed,"$Revision: 0.0.1 $")
vtkSmartPointer< vtkPoints > getPoints(void)
Definition: axlActor.cpp:275
virtual void setShader(QString vsfile)
Definition: axlActor.cpp:517
void setPolyData(vtkSmartPointer< vtkPolyData > polyData)
Definition: axlActor.cpp:300
void setMapper(vtkSmartPointer< vtkPolyDataMapper > mapper)
Definition: axlActor.cpp:332
void setActor(vtkSmartPointer< vtkActor > actor)
Definition: axlActor.cpp:322
vtkSmartPointer< vtkActor > getActor(void)
Definition: axlActor.cpp:317
double y
Definition: axlPoint.h:37
vtkSmartPointer< vtkPolyDataMapper > getMapper(void)
Definition: axlActor.cpp:327
double z
Definition: axlPoint.h:38
virtual void addToObserver(int idEvent, vtkCommand *observer)
void setObserverData(dtkAbstractData *data)
Definition: axlActor.cpp:424
void setSurface(axlAbstractSurfaceTrimmed *Surface)
axlActorControlPolygonObserver * getObserver(void)
Definition: axlActor.cpp:366
void setControlPolygon(bool control)
double x
Definition: axlPoint.h:37
virtual vtkRenderWindowInteractor * getInteractor(void)
Definition: axlActor.cpp:264
void PlaceWidget(double bounds[6]) override
virtual void onSamplingChanged(void)
void NewObserver(void)
Definition: axlActor.cpp:386
virtual void setControlPoints(axlControlPointsWidget *controlPoints)
void setPoints(vtkSmartPointer< vtkPoints > points)
Definition: axlActor.cpp:280
void setSpline(dtkAbstractData *spline)