1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License, or (at your option) any later version.
8 // This library is distributed in the hope that it will be useful,
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 // File : VTKViewer_ArcBuilder.cxx
21 // Author : Roman NIKOLAEV
24 #include "VTKViewer_ArcBuilder.h"
31 #include <vtkUnstructuredGrid.h>
32 #include <vtkTransformFilter.h>
33 #include <vtkTransform.h>
34 #include <vtkPoints.h>
35 #include <vtkVertex.h>
36 #include <vtkCellArray.h>
37 #include <vtkTriangle.h>
38 #include <vtkPolyData.h>
39 #include <vtkPointData.h>
41 #define PRECISION 10e-4
42 #define ANGLE_PRECISION 0.5
50 bool CheckAngle(const double compare, const double angle){
51 if((angle <= compare - ANGLE_PRECISION) || (angle >= compare + ANGLE_PRECISION))
57 //------------------------------------------------------------------------
62 XYZ::XYZ(double X, double Y, double Z):
79 double XYZ::Modulus () const {
80 return sqrt (x * x + y * y + z * z);
83 //------------------------------------------------------------------------
93 scalarValue(ScalarValue)
108 //------------------------------------------------------------------------
113 Vec::Vec (const double Xv,
117 double D = sqrt (Xv * Xv + Yv * Yv + Zv * Zv);
118 if(D > std::numeric_limits<double>::min() ) {
137 * Calculate angle between vectors in radians
139 double Vec::AngleBetween(const Vec & Other)
142 double numerator = GetXYZ().X()*Other.GetXYZ().X()+GetXYZ().Y()*Other.GetXYZ().Y()+GetXYZ().Z()*Other.GetXYZ().Z();
143 double denumerator = GetXYZ().Modulus()*Other.GetXYZ().Modulus();
144 double d = numerator/denumerator;
145 if( d < -1 && d > -1 - PRECISION )
147 else if( d > 1 && d < 1 + PRECISION )
154 * Calculate angle between vectors in degrees
156 double Vec::AngleBetweenInGrad(const Vec & Other){
157 return AngleBetween(Other)*vtkMath::DegreesFromRadians(1.);
161 * Calculate vector multiplication
163 Vec Vec::VectMultiplication(const Vec & Other) const{
164 double x = GetXYZ().Y()*Other.GetXYZ().Z() - GetXYZ().Z()*Other.GetXYZ().Y();
165 double y = GetXYZ().Z()*Other.GetXYZ().X() - GetXYZ().X()*Other.GetXYZ().Z();
166 double z = GetXYZ().X()*Other.GetXYZ().Y() - GetXYZ().Y()*Other.GetXYZ().X();
170 /*---------------------Class Plane --------------------------------*/
174 Plane::Plane(const Pnt& thePnt1, const Pnt& thePnt2, const Pnt& thePnt3)
176 CalculatePlane(thePnt1,thePnt2,thePnt3);
186 * Return plane normale
188 Vec Plane::GetNormale() const{
189 return Vec(myA,myB,myC);
193 * Calculate A,B,C coeefs of plane
195 void Plane::CalculatePlane(const Pnt& thePnt1, const Pnt& thePnt2, const Pnt& thePnt3){
197 double x1 = thePnt1.GetXYZ().X();
198 double x2 = thePnt2.GetXYZ().X();
199 double x3 = thePnt3.GetXYZ().X();
201 double y1 = thePnt1.GetXYZ().Y();
202 double y2 = thePnt2.GetXYZ().Y();
203 double y3 = thePnt3.GetXYZ().Y();
205 double z1 = thePnt1.GetXYZ().Z();
206 double z2 = thePnt2.GetXYZ().Z();
207 double z3 = thePnt3.GetXYZ().Z();
209 myA = y1*(z2 - z3) + y2*(z3 - z1) + y3*(z1 - z2);
210 myB = z1*(x2 - x3) + z2*(x3 - x1) + z3*(x1 - x2);
211 myC = x1*(y2 - y3) + x2*(y3 - y1) + x3*(y1 - y2);
214 cout<<"Plane A: "<<myA<<endl;
215 cout<<"Plane B: "<<myB<<endl;
216 cout<<"Plane C: "<<myC<<endl;
222 /*---------------------Class VTKViewer_ArcBuilder --------------------------------*/
226 VTKViewer_ArcBuilder::VTKViewer_ArcBuilder(const Pnt& thePnt1,
233 Vec V1(thePnt2.GetXYZ().X()-thePnt1.GetXYZ().X(),
234 thePnt2.GetXYZ().Y()-thePnt1.GetXYZ().Y(),
235 thePnt2.GetXYZ().Z()-thePnt1.GetXYZ().Z());
237 Vec V2(thePnt2.GetXYZ().X()-thePnt3.GetXYZ().X(),
238 thePnt2.GetXYZ().Y()-thePnt3.GetXYZ().Y(),
239 thePnt2.GetXYZ().Z()-thePnt3.GetXYZ().Z());
241 double angle = V1.AngleBetweenInGrad(V2);
243 //Check that points are not belong one line
245 cout<<"Angle for check: "<<angle<<endl;
247 if(CheckAngle(180,angle)) {
249 // Build plane by three points
250 Plane aPlane(thePnt1,thePnt2,thePnt3);
253 Vec aPlaneNormale = aPlane.GetNormale();
255 std::cout<<"X Normale: "<<aPlaneNormale.GetXYZ().X()<<std::endl;
256 std::cout<<"Y Normale: "<<aPlaneNormale.GetXYZ().Y()<<std::endl;
257 std::cout<<"Z Normale: "<<aPlaneNormale.GetXYZ().Z()<<std::endl;
262 //Projection plane normale on XOY
263 Vec aNormaleProjection (aPlaneNormale.GetXYZ().X(),
264 aPlaneNormale.GetXYZ().Y(),
267 std::cout<<"X Normale Projection: "<<aNormaleProjection.GetXYZ().X()<<std::endl;
268 std::cout<<"Y Normale Projection: "<<aNormaleProjection.GetXYZ().Y()<<std::endl;
269 std::cout<<"Z Normale Projection: "<<aNormaleProjection.GetXYZ().Z()<<std::endl;
273 Vec aAxis = aNormaleProjection.VectMultiplication(OZ);
275 std::cout<<"X Axis: "<<aAxis.GetXYZ().X()<<std::endl;
276 std::cout<<"Y Axis: "<<aAxis.GetXYZ().Y()<<std::endl;
277 std::cout<<"Z Axis: "<<aAxis.GetXYZ().Z()<<std::endl;
280 double anAngle = OZ.AngleBetweenInGrad(aPlaneNormale);
282 std::cout<<"Rotation Angle :"<<anAngle<<endl;
285 aInputPnts.push_back(thePnt1);
286 aInputPnts.push_back(thePnt2);
287 aInputPnts.push_back(thePnt3);
289 vtkSmartPointer<vtkUnstructuredGrid> aGrid = BuildGrid(aInputPnts);
292 bool needRotation = true;
293 if(anAngle == 0 || anAngle == 180)
294 needRotation = false;
298 aGrid = TransformGrid(aGrid,aAxis,anAngle);
301 cout<<"Need Rotation!!!"<<endl;
306 cout<<"Rotation does not need!!!"<<endl;
311 aGrid->GetPoint(0,coords);
312 myPnt1 = Pnt(coords[0],coords[1],coords[2], thePnt1.GetScalarValue());
313 aGrid->GetPoint(1,coords);
314 myPnt2 = Pnt(coords[0],coords[1],coords[2], thePnt2.GetScalarValue());
315 aGrid->GetPoint(2,coords);
316 myPnt3 = Pnt(coords[0],coords[1],coords[2], thePnt3.GetScalarValue());
318 vtkSmartPointer<vtkUnstructuredGrid> anArc = BuildArc(myScalarValues);
321 anArc = TransformGrid(anArc,aAxis,-anAngle);
324 myPoints = anArc->GetPoints();
330 std::cout<<"Points lay on the one line !"<<endl;
333 aList.push_back(thePnt1);
334 aList.push_back(thePnt2);
335 aList.push_back(thePnt3);
336 vtkUnstructuredGrid* aGrid = BuildGrid(aList);
337 myPoints = aGrid->GetPoints();
340 myScalarValues.clear();
341 myScalarValues.push_back(thePnt1.GetScalarValue());
342 myScalarValues.push_back(thePnt2.GetScalarValue());
343 myScalarValues.push_back(thePnt3.GetScalarValue());
351 VTKViewer_ArcBuilder::~VTKViewer_ArcBuilder()
356 * Add to the vtkUnstructuredGrid points from input list
358 vtkUnstructuredGrid* VTKViewer_ArcBuilder::BuildGrid(const PntList& theList) const
360 int aListsize = theList.size();
361 vtkUnstructuredGrid* aGrid = NULL;
364 aGrid = vtkUnstructuredGrid::New();
365 vtkPoints* aPoints = vtkPoints::New();
366 aPoints->SetNumberOfPoints(aListsize);
368 aGrid->Allocate(aListsize);
370 PntList::const_iterator it = theList.begin();
372 for(;it != theList.end();it++) {
374 aPoints->InsertPoint(aCounter,
378 vtkVertex* aVertex = vtkVertex::New();
379 aVertex->GetPointIds()->SetId(0, aCounter);
380 aGrid->InsertNextCell(aVertex->GetCellType(), aVertex->GetPointIds());
384 aGrid->SetPoints(aPoints);
392 VTKViewer_ArcBuilder::TransformGrid(vtkUnstructuredGrid* theGrid,
393 const Vec& theAxis, const double angle) const
395 vtkTransform *aTransform = vtkTransform::New();
396 aTransform->RotateWXYZ(angle, theAxis.GetXYZ().X(), theAxis.GetXYZ().Y(), theAxis.GetXYZ().Z());
397 vtkTransformFilter* aTransformFilter = vtkTransformFilter::New();
398 aTransformFilter->SetTransform(aTransform);
399 aTransform->Delete();
400 aTransformFilter->SetInputData(theGrid);
401 aTransformFilter->Update();
402 vtkUnstructuredGrid * aGrid = aTransformFilter->GetUnstructuredGridOutput();
404 aTransformFilter->Delete();
409 void VTKViewer_ArcBuilder::GetAngle(const double theAngle){
413 double InterpolateScalarValue(int index, int count, double firstValue, double middleValue, double lastValue)
415 bool isFirstHalf = index <= count / 2;
416 double first = isFirstHalf ? firstValue : lastValue;
417 double last = isFirstHalf ? middleValue : middleValue;
418 double ratio = (double)index / (double)count;
419 double position = isFirstHalf ? ratio * 2 : ( 1 - ratio ) * 2;
420 double value = first + (last - first) * position;
424 vtkUnstructuredGrid* VTKViewer_ArcBuilder::BuildArc(std::vector<double>& theScalarValues)
426 double x1 = myPnt1.GetXYZ().X(); double x2 = myPnt2.GetXYZ().X(); double x3 = myPnt3.GetXYZ().X();
427 double y1 = myPnt1.GetXYZ().Y(); double y2 = myPnt2.GetXYZ().Y(); double y3 = myPnt3.GetXYZ().Y();
428 double z = myPnt1.GetXYZ().Z(); //Points on plane || XOY
431 theScalarValues.clear();
435 bool okK1 = false, okK2 = false;
436 if ( fabs(x2 - x1) > DBL_MIN )
437 K1 = (y2 - y1)/(x2 - x1), okK1 = true;
438 if ( fabs(x3 - x2) > DBL_MIN )
439 K2 = (y3 - y2)/(x3 - x2), okK2 = true;
442 std::cout<<"K1 : "<< K1 <<endl;
443 std::cout<<"K2 : "<< K2 <<endl;
446 if( !okK2 ) //K2 --> infinity
447 xCenter = (K1*(y1-y3) + (x1+x2))/2.0;
449 else if( !okK1 ) //K1 --> infinity
450 xCenter = (K2*(y1-y3) - (x2+x3))/(-2.0);
453 xCenter = (K1*K2*(y1-y3) + K2*(x1+x2) - K1*(x2+x3))/ (2.0*(K2-K1));
458 yCenter = (-1/K2)*(xCenter - (x2+x3)/2.0) + (y2 + y3)/2.0;
460 yCenter = (-1/K1)*(xCenter - (x1+x2)/2.0) + (y1 + y2)/2.0;
464 std::cout<<"xCenter : "<<xCenter<<endl;
465 std::cout<<"yCenter : "<<yCenter<<endl;
466 std::cout<<"zCenter : "<<zCenter<<endl;
468 double aRadius = sqrt((x1 - xCenter)*(x1 - xCenter) + (y1 - yCenter)*(y1 - yCenter));
470 double angle1 = GetPointAngleOnCircle(xCenter,yCenter,x1,y1);
471 double angle2 = GetPointAngleOnCircle(xCenter,yCenter,x2,y2);
472 double angle3 = GetPointAngleOnCircle(xCenter,yCenter,x3,y3);
475 double aMaxAngle = vtkMath::RadiansFromDegrees(1.)*myAngle*2;
477 /* double aTotalAngle = fabs(angle3 - angle1);
479 if (aTotalAngle > vtkMath::Pi())
480 aTotalAngle = 2*vtkMath::Pi()-aTotalAngle;
483 double aTotalAngle = 0;
484 IncOrder aOrder = GetArcAngle(angle1,angle2,angle3,&aTotalAngle);
486 vtkUnstructuredGrid *aC = NULL;
488 if(aTotalAngle > aMaxAngle) {
489 int nbSteps = int(aTotalAngle/aMaxAngle)+1;
490 double anIncrementAngle = aTotalAngle/nbSteps;
491 double aCurrentAngle = angle1;
492 if(aOrder == VTKViewer_ArcBuilder::MINUS)
493 aCurrentAngle-=anIncrementAngle;
495 aCurrentAngle+=anIncrementAngle;
497 cout<<"Total angle :"<<aTotalAngle<<endl;
498 cout<<"Max Increment Angle :"<<aMaxAngle<<endl;
499 cout<<"Real Increment angle :"<<anIncrementAngle<<endl;
500 cout<<"Nb Steps : "<<nbSteps<<endl;
504 aList.push_back(myPnt1);
505 theScalarValues.push_back(myPnt1.GetScalarValue());
506 for(int i=1;i<=nbSteps-1;i++){
507 double x = xCenter + aRadius*cos(aCurrentAngle);
508 double y = yCenter + aRadius*sin(aCurrentAngle);
509 double value = InterpolateScalarValue(i, nbSteps, myPnt1.GetScalarValue(), myPnt2.GetScalarValue(), myPnt3.GetScalarValue());
510 Pnt aPnt(x,y,z,value);
512 aList.push_back(aPnt);
513 theScalarValues.push_back(aPnt.GetScalarValue());
514 if(aOrder == VTKViewer_ArcBuilder::MINUS)
515 aCurrentAngle-=anIncrementAngle;
517 aCurrentAngle+=anIncrementAngle;
519 aList.push_back(myPnt3);
520 theScalarValues.push_back(myPnt3.GetScalarValue());
522 aC = BuildGrid(aList);
524 cout<<"angle1 : "<<angle1*vtkMath::DoubleRadiansToDegrees()<<endl;
525 cout<<"angle2 : "<<angle2*vtkMath::DoubleRadiansToDegrees()<<endl;
526 cout<<"angle3 : "<<angle3*vtkMath::DoubleRadiansToDegrees()<<endl;
531 aList.push_back(myPnt1);
532 aList.push_back(myPnt2);
533 aList.push_back(myPnt3);
534 aC = BuildGrid(aList);
536 theScalarValues.push_back(myPnt1.GetScalarValue());
537 theScalarValues.push_back(myPnt2.GetScalarValue());
538 theScalarValues.push_back(myPnt3.GetScalarValue());
543 double VTKViewer_ArcBuilder::
544 GetPointAngleOnCircle(const double theXCenter, const double theYCenter,
545 const double theXPoint, const double theYPoint){
546 double result = atan2(theYCenter - theYPoint, theXPoint - theXCenter);
548 result = result+vtkMath::Pi()*2;
549 return vtkMath::Pi()*2-result;
553 vtkPoints* VTKViewer_ArcBuilder::GetPoints(){
557 const std::vector<double>& VTKViewer_ArcBuilder::GetScalarValues()
559 return myScalarValues;
562 VTKViewer_ArcBuilder::IncOrder VTKViewer_ArcBuilder::GetArcAngle( const double& P1, const double& P2, const double& P3,double* Ang){
564 if(P1 < P2 && P2 < P3){
566 aResult = VTKViewer_ArcBuilder::PLUS;
568 else if((P1 < P3 && P3 < P2) || (P2 < P1 && P1 < P3)){
569 *Ang = 2*vtkMath::Pi() - P3 + P1;
570 aResult = VTKViewer_ArcBuilder::MINUS;
572 else if((P2 < P3 && P3 < P1) || (P3 < P1 && P1 < P2)){
573 *Ang = 2*vtkMath::Pi() - P1 + P3;
574 aResult = VTKViewer_ArcBuilder::PLUS;
576 else if(P3 < P2 && P2 < P1){
578 aResult = VTKViewer_ArcBuilder::MINUS;
583 //------------------------------------------------------------------------
584 Pnt CreatePnt(vtkCell* cell, vtkDataArray* scalars, vtkIdType index)
587 cell->GetPoints()->GetPoint(index, coord);
588 vtkIdType pointId = cell->GetPointId(index);
589 double scalarValue = scalars ? scalars->GetTuple1(pointId) : 0;
590 Pnt point(coord[0], coord[1], coord[2], scalarValue);
594 //------------------------------------------------------------------------
595 vtkIdType Build1DArc(vtkIdType cellId, vtkUnstructuredGrid* input,
596 vtkPolyData *output,vtkIdType *pts,
597 double myMaxArcAngle){
599 vtkIdType aResult = -1;
600 vtkIdType *aNewPoints;
602 vtkDataArray* inputScalars = input->GetPointData()->GetScalars();
603 vtkDataArray* outputScalars = output->GetPointData()->GetScalars();
605 vtkCell* aCell = input->GetCell(cellId);
606 //Get All points from input cell
607 Pnt P0 = CreatePnt( aCell, inputScalars, 0 );
608 Pnt P1 = CreatePnt( aCell, inputScalars, 1 );
609 Pnt P2 = CreatePnt( aCell, inputScalars, 2 );
611 VTKViewer_ArcBuilder aBuilder(P0,P2,P1,myMaxArcAngle);
612 if (aBuilder.GetStatus() != VTKViewer_ArcBuilder::Arc_Done) {
616 vtkPoints* aPoints = aBuilder.GetPoints();
617 std::vector<double> aScalarValues = aBuilder.GetScalarValues();
618 vtkIdType aNbPts = aPoints->GetNumberOfPoints();
619 std::vector< vtkIdType > aNewPoints( aNbPts );
621 vtkIdType aCellType = VTK_POLY_LINE;
623 aNewPoints[0] = pts[0];
624 for(vtkIdType idx = 1; idx < aNbPts-1;idx++) {
625 curID = output->GetPoints()->InsertNextPoint(aPoints->GetPoint(idx));
627 outputScalars->InsertNextTuple1(aScalarValues[idx]);
628 aNewPoints[idx] = curID;
630 aNewPoints[aNbPts-1] = pts[1];
632 aResult = output->InsertNextCell(aCellType,aNbPts,&aNewPoints[0]);
638 * Add all points from the input vector theCollection into thePoints.
639 * Array theIds - it is array with ids of added points.
641 vtkIdType MergevtkPoints(const std::vector< vtkSmartPointer< vtkPoints > >& theCollection,
642 const std::vector< std::vector<double> >& theScalarCollection,
643 vtkPoints* thePoints,
644 std::map<int, double>& thePntId2ScalarValue,
646 vtkIdType aNbPoints = 0;
647 vtkIdType anIdCounter = 0;
648 vtkIdType aNewPntId = 0;
650 //Compute number of points
651 std::vector< vtkSmartPointer< vtkPoints > >::const_iterator it = theCollection.begin();
652 for(;it != theCollection.end();it++){
653 vtkPoints* aPoints = *it;
655 aNbPoints += aPoints->GetNumberOfPoints()-1; //Because we add all points except last
658 it = theCollection.begin();
659 std::vector< std::vector<double> >::const_iterator itScalar = theScalarCollection.begin();
660 theIds = new vtkIdType[aNbPoints];
661 // ..and add all points
662 for(;it != theCollection.end() && itScalar != theScalarCollection.end(); it++, itScalar++){
663 vtkPoints* aPoints = *it;
664 std::vector<double> aScalarValues = *itScalar;
667 for(vtkIdType idx = 0;idx < aPoints->GetNumberOfPoints()-1;idx++){
668 aNewPntId = thePoints->InsertNextPoint(aPoints->GetPoint(idx));
669 theIds[anIdCounter] = aNewPntId;
670 thePntId2ScalarValue[ aNewPntId ] = aScalarValues[idx];