summaryrefslogtreecommitdiff
path: root/src/AIS/AIS_FixRelation.cxx
blob: 04d1fc326258ff8b9181f72ab931080b9a930c7e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
// File:	AIS_FixRelation.cdl
// Created:	Tue Dec  5 15:09:04 1996
// Author:	Flore Lantheaume/Odile Olivier
//              <ODL>

#define BUC60915        //GG 05/06/01 Enable to compute the requested arrow size
//                      if any in all dimensions.

#include <Standard_NotImplemented.hxx>

#include <AIS_FixRelation.ixx>

#include <AIS_Shape.hxx>

#include <TopAbs_ShapeEnum.hxx>

#include <SelectMgr_EntityOwner.hxx>
#include <Select3D_SensitiveSegment.hxx>

#include <BRep_Tool.hxx>
#include <BRepAdaptor_Curve.hxx>

#include <TopoDS.hxx>
#include <TopoDS_Edge.hxx>
#include <TopoDS_Vertex.hxx>
#include <TopoDS_Wire.hxx>
#include <TopExp.hxx>
#include <TopLoc_Location.hxx>
#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
#include <TopTools_IndexedMapOfShape.hxx>
#include <TopTools_ListIteratorOfListOfShape.hxx>

#include <TColStd_ListIteratorOfListOfTransient.hxx>

#include <Geom_Curve.hxx>
#include <Geom_Line.hxx>
#include <Geom_Circle.hxx>
#include <Geom_Plane.hxx>

#include <gp_Pnt.hxx>
#include <gp_Vec.hxx>
#include <gp_Dir.hxx>
#include <gp_Ax1.hxx>
#include <gp_XYZ.hxx>

#include <ElCLib.hxx>
#include <ElSLib.hxx>

#include <Precision.hxx>

#include <Standard_DomainError.hxx>

#include <DsgPrs_FixPresentation.hxx>

#include <AIS.hxx>

static Standard_Boolean InDomain(const Standard_Real fpar,
					const Standard_Real lpar,
					const Standard_Real para) 
{
  if (fpar >= 0.) {
    return ((para >= fpar) && (para <= lpar));
  }
  if (para >= (fpar+2*PI)) return Standard_True;
  if (para <= lpar) return Standard_True;
  return Standard_False;
}

//=======================================================================
//function : Constructor
//purpose  : vertex Fix Relation
//=======================================================================

AIS_FixRelation::AIS_FixRelation(const TopoDS_Shape& aShape, 
				 const Handle(Geom_Plane)& aPlane, 
				 const TopoDS_Wire& aWire)
:AIS_Relation(),
 myWire(aWire),
 haspos(Standard_False)
{
  myFShape = aShape;
  myPlane = aPlane;
  myAutomaticPosition = Standard_True;
  myArrowSize = 5.;
}

//=======================================================================
//function : Constructor
//purpose  : vertex Fix Relation
//=======================================================================

AIS_FixRelation::AIS_FixRelation(const TopoDS_Shape& aShape, 
				 const Handle(Geom_Plane)& aPlane, 
				 const TopoDS_Wire& aWire, 
				 const gp_Pnt& aPosition, 
				 const Standard_Real anArrowSize)
:AIS_Relation(),
 myWire(aWire),
 haspos(Standard_False)
{
  myFShape = aShape;
  myPlane = aPlane;
  myPosition = aPosition;
#ifdef BUC60915
  SetArrowSize( anArrowSize );
#else
  myArrowSize = anArrowSize;
#endif
  myAutomaticPosition = Standard_False;
}


//=======================================================================
//function : Constructor
//purpose  : edge (line or circle) Fix Relation
//=======================================================================

AIS_FixRelation::AIS_FixRelation(const TopoDS_Shape& aShape, 
				 const Handle(Geom_Plane)& aPlane):
haspos(Standard_False)
{
  myFShape = aShape;
  myPlane = aPlane;
  myAutomaticPosition = Standard_True;
  myArrowSize = 5.;
}

//=======================================================================
//function : Constructor
//purpose  : edge (line or circle) Fix Relation
//=======================================================================

AIS_FixRelation::AIS_FixRelation(
	const TopoDS_Shape& aShape, 
	const Handle(Geom_Plane)& aPlane, 
	const gp_Pnt& aPosition, 
	const Standard_Real anArrowSize):
haspos(Standard_False)
{
  myFShape = aShape;
  myPlane = aPlane;
  myPosition = aPosition;
#ifdef BUC60915
  SetArrowSize( anArrowSize );
#else
  myArrowSize = anArrowSize;
#endif
  myAutomaticPosition = Standard_False;
}

//=======================================================================
//function : Wire
//purpose  : 
//=======================================================================

TopoDS_Wire AIS_FixRelation::Wire()
{
  return myWire;
}

//=======================================================================
//function : SetWire
//purpose  : 
//=======================================================================

void AIS_FixRelation::SetWire(const TopoDS_Wire& aWire)
{
  myWire = aWire;
}


//=======================================================================
//function : Compute
//purpose  : 
//=======================================================================

void AIS_FixRelation::Compute(const Handle(PrsMgr_PresentationManager3d)&, 
			      const Handle(Prs3d_Presentation)& aPresentation, 
			      const Standard_Integer)
{
  aPresentation->Clear();

  // Calculate position of the symbol and
  // point of attach of the segment on the shape
  gp_Pnt curpos;
  if (myFShape.ShapeType() == TopAbs_VERTEX)
    ComputeVertex(TopoDS::Vertex(myFShape), curpos);
  else if (myFShape.ShapeType() == TopAbs_EDGE)
    ComputeEdge(TopoDS::Edge(myFShape), curpos);

  const gp_Dir& nor = myPlane->Axis().Direction();

  
  // calculate presentation
  // definition of the symbol size
#ifdef BUC60915
  if( !myArrowSizeIsDefined )
#endif
    myArrowSize = 5.;

    //creation of the presentation
  DsgPrs_FixPresentation::Add(aPresentation,
			      myDrawer,
			      myPntAttach,
			      curpos,
			      nor,
			      myArrowSize);
}

//=======================================================================
//function : Compute
//purpose  : to avoid warning
//=======================================================================

void AIS_FixRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
			      const Handle(Prs3d_Presentation)& aPresentation)
{
// Standard_NotImplemented::Raise("AIS_FixRelation::Compute(const Handle(Prs3d_Projector)&,const Handle(Prs3d_Presentation)&)");
 PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ; 
}

//=======================================================================
//function : Compute
//purpose  : to avoid warning
//=======================================================================

void AIS_FixRelation::Compute(const Handle(PrsMgr_PresentationManager2d)& aPresentationManager2d,
			      const Handle(Graphic2d_GraphicObject)& aGraphicObject,
			      const Standard_Integer anInteger)
{
// Standard_NotImplemented::Raise("AIS_FixRelation::Compute(const Handle(PrsMgr_PresentationManager2d)&,const Handle(Graphic2d_GraphicObject)&,const Standard_Integer)");
 PrsMgr_PresentableObject::Compute( aPresentationManager2d ,aGraphicObject,anInteger) ; 
}

//=======================================================================
//function : Compute
//purpose  : 
//=======================================================================

void AIS_FixRelation::Compute(const Handle_Prs3d_Projector& aProjector,
                              const Handle_Geom_Transformation& aTransformation,
                              const Handle_Prs3d_Presentation& aPresentation)
{
// Standard_NotImplemented::Raise("AIS_FixRelation::Compute(const Handle_Prs3d_Projector&, const Handle_Geom_Transformation&, const Handle_Prs3d_Presentation&)");
 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
}

//=======================================================================
//function : ComputeSelection
//purpose  : 
//=======================================================================

void AIS_FixRelation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection, 
				       const Standard_Integer)
{
  Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);

  // creation of segment sensible for the linked segment  
  // of the shape fixed to symbol 'Fix'
  Handle(Select3D_SensitiveSegment) seg;
  seg = new Select3D_SensitiveSegment(own,
				      myPntAttach,
				      myPosition);
  aSelection->Add(seg);
  
  // Creation of the sensible zone of symbol 'Fix'
  gp_Dir norm = myPlane->Axis().Direction();
    
  gp_Vec dirac(myPntAttach,myPosition);
  dirac.Normalize();
  gp_Vec norac = dirac.Crossed(gp_Vec(norm));
  gp_Ax1 ax(myPosition, norm);
  norac.Rotate(ax, PI/8);

  norac*=(myArrowSize/2);
  gp_Pnt P1 = myPosition.Translated(norac);
  gp_Pnt P2 = myPosition.Translated(-norac);
  seg = new Select3D_SensitiveSegment(own,
				      P1,
				      P2);
  aSelection->Add(seg);

  norac*=0.8;
  P1 = myPosition.Translated(norac);
  P2 = myPosition.Translated(-norac);
  dirac*=(myArrowSize/2);
  gp_Pnt PF(P1.XYZ());
  gp_Pnt PL = PF.Translated(dirac);
  PL.Translate(norac);
  seg = new Select3D_SensitiveSegment(own,
				      PF,
				      PL);
  aSelection->Add(seg);


  PF.SetXYZ(P2.XYZ());
  PL = PF.Translated(dirac);
  PL.Translate(norac);
  seg = new Select3D_SensitiveSegment(own,
				      PF,
				      PL);
  aSelection->Add(seg);

  PF.SetXYZ( (P1.XYZ() + P2.XYZ()) /2 );
  PL = PF.Translated(dirac);
  PL.Translate(norac);
  seg = new Select3D_SensitiveSegment(own,
				      PF,
				      PL);
}

//=======================================================================
//function : ComputeVertex
//purpose  : computes myPntAttach and the position of the presentation 
//           when you fix a vertex
//=======================================================================

void AIS_FixRelation::ComputeVertex(const TopoDS_Vertex& /*FixVertex*/,
				    gp_Pnt& curpos)
{
  myPntAttach = BRep_Tool::Pnt(TopoDS::Vertex(myFShape));
  curpos = myPosition;
  if (myAutomaticPosition) {
      gp_Pln pln(myPlane->Pln());
      gp_Dir dir(pln.XAxis().Direction());
      gp_Vec transvec = gp_Vec(dir)*myArrowSize;
      curpos = myPntAttach.Translated(transvec);;
      myPosition = curpos;
      myAutomaticPosition = Standard_True;
  }
}

//=======================================================================
//function : ComputePosition
//purpose  : 
//=======================================================================

gp_Pnt AIS_FixRelation::ComputePosition(const Handle(Geom_Curve)& curv1, 
					const Handle(Geom_Curve)& curv2, 
					const gp_Pnt& firstp1, 
					const gp_Pnt& lastp1, 
					const gp_Pnt& firstp2, 
					const gp_Pnt& lastp2) const 
{
  //---------------------------------------------------------
  // calculate the point of attach
  //---------------------------------------------------------
  gp_Pnt curpos;

  if (curv1->IsInstance(STANDARD_TYPE(Geom_Circle)) || curv2->IsInstance(STANDARD_TYPE(Geom_Circle))) {    
    Handle(Geom_Circle) gcirc = Handle(Geom_Circle)::DownCast(curv1);
    if (gcirc.IsNull()) gcirc = Handle(Geom_Circle)::DownCast(curv2);
    gp_Dir dir( gcirc->Location().XYZ() + myPntAttach.XYZ() );
    gp_Vec transvec = gp_Vec(dir)*myArrowSize;
    curpos = myPntAttach.Translated(transvec);    
  }

  else {
    gp_Vec vec1(firstp1,lastp1);
    gp_Vec vec2(firstp2,lastp2);
    
    if (!vec1.IsParallel(vec2, Precision::Angular()) ) {
      gp_Dir dir;
      Standard_Real conf =Precision::Confusion();
      if (lastp1.IsEqual(firstp2,conf) || firstp1.IsEqual(lastp2,conf)) dir.SetXYZ(vec1.XYZ() - vec2.XYZ());
      else dir.SetXYZ(vec1.XYZ() + vec2.XYZ());
      gp_Vec transvec = gp_Vec(dir)*myArrowSize;
      curpos = myPntAttach.Translated(transvec);
    }
    else {
      gp_Vec crossvec = vec1.Crossed(vec2);
      vec1.Cross(crossvec);
      gp_Dir dir(vec1);
      curpos = myPntAttach.Translated(gp_Vec(dir)*myArrowSize);
    }
  }

  return curpos;
}

//=======================================================================
//function : ComputePosition
//purpose  : Computes the position of the "fix dimension" when the 
//           fixed object is a vertex which is set at the intersection
//           of two curves.
//           The "dimension" is in the "middle" of the two edges.
//=======================================================================

gp_Pnt AIS_FixRelation::ComputePosition(const Handle(Geom_Curve)& curv, 
					const gp_Pnt& firstp, 
					const gp_Pnt& lastp) const 
{
  //---------------------------------------------------------
  // calculate the point of attach
  //---------------------------------------------------------
  gp_Pnt curpos;

  if (curv->IsKind(STANDARD_TYPE(Geom_Circle))) {
    
    Handle(Geom_Circle) gcirc = Handle(Geom_Circle)::DownCast(curv);
    gp_Dir dir( gcirc->Location().XYZ() + myPntAttach.XYZ() );
    gp_Vec transvec = gp_Vec(dir)*myArrowSize;
    curpos = myPntAttach.Translated(transvec);
    
  } //if (curv->IsKind(STANDARD_TYPE(Geom_Circle))

  else {
//    gp_Pln pln(Component()->WorkingPlane()->Plane()->GetValue()->Pln());
    gp_Pln pln(myPlane->Pln());
    gp_Dir NormPln = pln.Axis().Direction();
    gp_Vec vec(firstp,lastp);
    vec.Cross( gp_Vec(NormPln));
    vec.Normalize();
    gp_Vec transvec = vec*myArrowSize;
    curpos = myPntAttach.Translated(transvec);
    gp_Ax1 RotAx( myPntAttach, NormPln);
    curpos.Rotate(RotAx, PI/10);
  }

  return curpos;
 }

//=======================================================================
//function : ComputeEdge 
//purpose  : computes myPntAttach and the position of the presentation 
//           when you fix an edge
//=======================================================================

void AIS_FixRelation::ComputeEdge(const TopoDS_Edge& FixEdge, gp_Pnt& curpos)
{
  Handle(Geom_Curve) curEdge;
  gp_Pnt ptbeg,ptend;
  if (!AIS::ComputeGeometry(FixEdge,curEdge,ptbeg,ptend)) return;

  //---------------------------------------------------------
  // calcul du point de positionnement du symbole 'fix'
  //---------------------------------------------------------
        //--> In case of a straight line
  if (curEdge->IsKind(STANDARD_TYPE(Geom_Line))){
    gp_Lin glin = Handle(Geom_Line)::DownCast(curEdge)->Lin();
    Standard_Real pfirst(ElCLib::Parameter(glin,ptbeg));
    Standard_Real plast(ElCLib::Parameter(glin,ptend));
    ComputeLinePosition(glin, curpos, pfirst, plast);
  }
  
        //--> In case of a circle
  else if (curEdge->IsKind(STANDARD_TYPE(Geom_Circle))) {
    gp_Circ  gcirc = Handle(Geom_Circle)::DownCast(curEdge)->Circ();
    Standard_Real pfirst, plast;
    BRepAdaptor_Curve cu(FixEdge);
    pfirst = cu.FirstParameter();
    plast = cu.LastParameter();
    ComputeCirclePosition(gcirc, curpos, pfirst, plast);
  }
  
  else
    return;
    
}

//=======================================================================
//function : ComputeLinePosition
//purpose  : compute the values of myPntAttach and the position <pos> of
//           the symbol when the fixed edge has a geometric support equal
//           to a line.
//=======================================================================

void AIS_FixRelation::ComputeLinePosition(const gp_Lin& glin, 
					  gp_Pnt& pos, 
					  Standard_Real& pfirst, 
					  Standard_Real& plast)
{
  if (myAutomaticPosition) {
    // point of attach is chosen as middle of the segment
    myPntAttach = ElCLib::Value((pfirst+ plast)/2, glin);
    
    gp_Dir norm = myPlane ->Axis().Direction();
     
    norm.Cross(glin.Position().Direction());
    pos = myPntAttach.Translated(gp_Vec(norm)*myArrowSize);
    myAutomaticPosition = Standard_True;
  } // if (myAutomaticPosition)

  else {
    pos = myPosition;
    Standard_Real linparam = ElCLib::Parameter(glin, pos);

    // case if the projection of position is located between 2 vertices
    // de l'edge
    if ( (linparam >= pfirst) && (linparam <= plast) )
      myPntAttach = ElCLib::Value(linparam,glin);
    
    // case if the projection of Position is outside of the limits
    // of the edge : the point closest to the projection is chosen 
    // as the attach point
    else {
      Standard_Real pOnLin;
      if (linparam > plast)
	pOnLin = plast;
      else
	pOnLin = pfirst;
      myPntAttach = ElCLib::Value(pOnLin,glin);
      gp_Dir norm = myPlane->Axis().Direction();
	
      norm.Cross(glin.Position().Direction());
      gp_Lin lsup(myPntAttach, norm);
      Standard_Real parpos = ElCLib::Parameter(lsup,myPosition);
      pos =  ElCLib::Value(parpos,lsup);
    }

  }
  myPosition = pos;
}

//=======================================================================
//function : ComputeCirclePosition 
//purpose  : compute the values of myPntAttach and the position <pos> of
//           the symbol when the fixed edge has a geometric support equal
//           to a circle. 
//=======================================================================

void AIS_FixRelation::ComputeCirclePosition(
	const gp_Circ& gcirc, 
	gp_Pnt& pos, 
	Standard_Real& pfirst, 
	Standard_Real& plast)
{
  // readjust parametres on the circle
  if (plast > 2*PI ) {
    Standard_Real nbtours = Floor(plast / (2*PI));
    plast -= nbtours*2*PI;
    pfirst -= nbtours*2*PI;
  }

  if (myAutomaticPosition) {
    // the point attach is the "middle" of the segment (relatively
    // to the parametres of start and end vertices of the edge
    
    Standard_Real circparam = (pfirst + plast)/2.;

    if ( !InDomain(pfirst,plast,circparam)) {
      Standard_Real otherpar = circparam + PI;
      if (otherpar > 2*PI) otherpar -= 2*PI;
      circparam = otherpar;
    }

    myPntAttach = ElCLib::Value(circparam, gcirc );

    gp_Vec dir( gcirc.Location().XYZ(), myPntAttach.XYZ() );
    dir.Normalize();
    gp_Vec transvec = dir*myArrowSize;
    pos = myPntAttach.Translated(transvec);
    myPosition = pos;
    myAutomaticPosition = Standard_True;
  } // if (myAutomaticPosition)

  else {
    // case if the projection of myPosition is outside of 2
    // vertices of the edge. In this case the parameter is readjusted
    // in the valid part of the circle
    pos = myPosition;

    Standard_Real circparam = ElCLib::Parameter(gcirc, pos);

    if ( !InDomain(pfirst,plast,circparam)) {
      Standard_Real otherpar = circparam + PI;
      if (otherpar > 2*PI) otherpar -= 2*PI;
      circparam = otherpar;
    }
    
    myPntAttach = ElCLib::Value(circparam,gcirc);
  }
}

//=======================================================================
//function : ConnectedEdges
//purpose  : 
//=======================================================================
Standard_Boolean AIS_FixRelation::ConnectedEdges(const TopoDS_Wire& WIRE,
						 const TopoDS_Vertex& V, 
						 TopoDS_Edge& E1, 
						 TopoDS_Edge& E2)
{
  TopTools_IndexedDataMapOfShapeListOfShape  vertexMap;
  TopExp::MapShapesAndAncestors (WIRE,TopAbs_VERTEX,TopAbs_EDGE,vertexMap);
  
  Standard_Boolean found(Standard_False);
  TopoDS_Vertex theVertex;
  for (Standard_Integer i=1; i<=vertexMap.Extent() && !found; i++) {
    if (vertexMap.FindKey(i).IsSame(V)) {
     theVertex = TopoDS::Vertex(vertexMap.FindKey(i));
     found = Standard_True;
   }
  }
  if (!found) {
    E1.Nullify();
    E2.Nullify();
    return Standard_False;
  }

  TopTools_ListIteratorOfListOfShape iterator(vertexMap.FindFromKey(theVertex));
  if (iterator.More()) {
    E1 = TopoDS::Edge(iterator.Value());
    BRepAdaptor_Curve curv(E1);
    iterator.Next();
  }
  else {
    E1.Nullify();
    return Standard_False;
  }

  if (iterator.More()) {
    E2 = TopoDS::Edge(iterator.Value());
    BRepAdaptor_Curve curv(E2);
    iterator.Next();
  }
  else {
    E2.Nullify();
    return Standard_False;
  }
    
  if (iterator.More()) {
    E1.Nullify();
    E2.Nullify();
    return Standard_False;
  }
  return Standard_True;
}