0030853: Visualization, AIS_ViewController - fix 1 pixel Y shift while zooming
[occt.git] / src / AIS / AIS_FixRelation.cxx
CommitLineData
b311480e 1// Created on: 1996-12-05
2// Created by: Flore Lantheaume/Odile Olivier
3// Copyright (c) 1996-1999 Matra Datavision
973c2be1 4// Copyright (c) 1999-2014 OPEN CASCADE SAS
b311480e 5//
973c2be1 6// This file is part of Open CASCADE Technology software library.
b311480e 7//
d5f74e42 8// This library is free software; you can redistribute it and/or modify it under
9// the terms of the GNU Lesser General Public License version 2.1 as published
973c2be1 10// by the Free Software Foundation, with special exception defined in the file
11// OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
12// distribution for complete text of the license and disclaimer of any warranty.
b311480e 13//
973c2be1 14// Alternatively, this file may be used under the terms of Open CASCADE
15// commercial license or contractual agreement.
7fd59977 16
7fd59977 17
42cf5bc1 18#include <AIS.hxx>
19#include <AIS_FixRelation.hxx>
7fd59977 20#include <AIS_Shape.hxx>
7fd59977 21#include <BRep_Tool.hxx>
22#include <BRepAdaptor_Curve.hxx>
42cf5bc1 23#include <DsgPrs_FixPresentation.hxx>
24#include <ElCLib.hxx>
25#include <ElSLib.hxx>
26#include <Geom_Circle.hxx>
7fd59977 27#include <Geom_Curve.hxx>
28#include <Geom_Line.hxx>
7fd59977 29#include <Geom_Plane.hxx>
42cf5bc1 30#include <Geom_Transformation.hxx>
31#include <gp_Ax1.hxx>
32#include <gp_Circ.hxx>
33#include <gp_Dir.hxx>
34#include <gp_Lin.hxx>
7fd59977 35#include <gp_Pnt.hxx>
36#include <gp_Vec.hxx>
7fd59977 37#include <gp_XYZ.hxx>
7fd59977 38#include <Precision.hxx>
42cf5bc1 39#include <Prs3d_Presentation.hxx>
40#include <Prs3d_Projector.hxx>
41#include <Select3D_SensitiveSegment.hxx>
42#include <SelectMgr_EntityOwner.hxx>
43#include <SelectMgr_Selection.hxx>
7fd59977 44#include <Standard_DomainError.hxx>
42cf5bc1 45#include <Standard_NotImplemented.hxx>
46#include <Standard_Type.hxx>
47#include <TColStd_ListIteratorOfListOfTransient.hxx>
48#include <TopAbs_ShapeEnum.hxx>
49#include <TopExp.hxx>
50#include <TopLoc_Location.hxx>
51#include <TopoDS.hxx>
52#include <TopoDS_Edge.hxx>
53#include <TopoDS_Shape.hxx>
54#include <TopoDS_Vertex.hxx>
55#include <TopoDS_Wire.hxx>
56#include <TopTools_IndexedDataMapOfShapeListOfShape.hxx>
57#include <TopTools_IndexedMapOfShape.hxx>
58#include <TopTools_ListIteratorOfListOfShape.hxx>
7fd59977 59
92efcf78 60IMPLEMENT_STANDARD_RTTIEXT(AIS_FixRelation,AIS_Relation)
61
7fd59977 62static Standard_Boolean InDomain(const Standard_Real fpar,
63 const Standard_Real lpar,
64 const Standard_Real para)
65{
66 if (fpar >= 0.) {
67 return ((para >= fpar) && (para <= lpar));
68 }
c6541a0c 69 if (para >= (fpar+2*M_PI)) return Standard_True;
7fd59977 70 if (para <= lpar) return Standard_True;
71 return Standard_False;
72}
73
74//=======================================================================
75//function : Constructor
76//purpose : vertex Fix Relation
77//=======================================================================
78
79AIS_FixRelation::AIS_FixRelation(const TopoDS_Shape& aShape,
80 const Handle(Geom_Plane)& aPlane,
81 const TopoDS_Wire& aWire)
82:AIS_Relation(),
258ff83b 83 myWire(aWire)
7fd59977 84{
85 myFShape = aShape;
86 myPlane = aPlane;
87 myAutomaticPosition = Standard_True;
88 myArrowSize = 5.;
89}
90
91//=======================================================================
92//function : Constructor
93//purpose : vertex Fix Relation
94//=======================================================================
95
96AIS_FixRelation::AIS_FixRelation(const TopoDS_Shape& aShape,
97 const Handle(Geom_Plane)& aPlane,
98 const TopoDS_Wire& aWire,
99 const gp_Pnt& aPosition,
100 const Standard_Real anArrowSize)
101:AIS_Relation(),
258ff83b 102 myWire(aWire)
7fd59977 103{
104 myFShape = aShape;
105 myPlane = aPlane;
106 myPosition = aPosition;
7fd59977 107 SetArrowSize( anArrowSize );
7fd59977 108 myAutomaticPosition = Standard_False;
109}
110
111
112//=======================================================================
113//function : Constructor
114//purpose : edge (line or circle) Fix Relation
115//=======================================================================
116
117AIS_FixRelation::AIS_FixRelation(const TopoDS_Shape& aShape,
258ff83b 118 const Handle(Geom_Plane)& aPlane)
7fd59977 119{
120 myFShape = aShape;
121 myPlane = aPlane;
122 myAutomaticPosition = Standard_True;
123 myArrowSize = 5.;
124}
125
126//=======================================================================
127//function : Constructor
128//purpose : edge (line or circle) Fix Relation
129//=======================================================================
130
131AIS_FixRelation::AIS_FixRelation(
132 const TopoDS_Shape& aShape,
133 const Handle(Geom_Plane)& aPlane,
134 const gp_Pnt& aPosition,
258ff83b 135 const Standard_Real anArrowSize)
7fd59977 136{
137 myFShape = aShape;
138 myPlane = aPlane;
139 myPosition = aPosition;
7fd59977 140 SetArrowSize( anArrowSize );
7fd59977 141 myAutomaticPosition = Standard_False;
142}
143
144//=======================================================================
145//function : Wire
146//purpose :
147//=======================================================================
148
149TopoDS_Wire AIS_FixRelation::Wire()
150{
151 return myWire;
152}
153
154//=======================================================================
155//function : SetWire
156//purpose :
157//=======================================================================
158
159void AIS_FixRelation::SetWire(const TopoDS_Wire& aWire)
160{
161 myWire = aWire;
162}
163
164
165//=======================================================================
166//function : Compute
167//purpose :
168//=======================================================================
169
170void AIS_FixRelation::Compute(const Handle(PrsMgr_PresentationManager3d)&,
171 const Handle(Prs3d_Presentation)& aPresentation,
172 const Standard_Integer)
173{
81bba717 174 // Calculate position of the symbol and
175 // point of attach of the segment on the shape
7fd59977 176 gp_Pnt curpos;
177 if (myFShape.ShapeType() == TopAbs_VERTEX)
178 ComputeVertex(TopoDS::Vertex(myFShape), curpos);
179 else if (myFShape.ShapeType() == TopAbs_EDGE)
180 ComputeEdge(TopoDS::Edge(myFShape), curpos);
181
182 const gp_Dir& nor = myPlane->Axis().Direction();
183
184
81bba717 185 // calculate presentation
186 // definition of the symbol size
7fd59977 187 if( !myArrowSizeIsDefined )
7fd59977 188 myArrowSize = 5.;
189
81bba717 190 //creation of the presentation
7fd59977 191 DsgPrs_FixPresentation::Add(aPresentation,
192 myDrawer,
193 myPntAttach,
194 curpos,
195 nor,
196 myArrowSize);
197}
198
199//=======================================================================
200//function : Compute
201//purpose : to avoid warning
202//=======================================================================
203
204void AIS_FixRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
205 const Handle(Prs3d_Presentation)& aPresentation)
206{
9775fa61 207// throw Standard_NotImplemented("AIS_FixRelation::Compute(const Handle(Prs3d_Projector)&,const Handle(Prs3d_Presentation)&)");
7fd59977 208 PrsMgr_PresentableObject::Compute( aProjector , aPresentation ) ;
209}
210
7fd59977 211//=======================================================================
212//function : Compute
213//purpose :
214//=======================================================================
215
857ffd5e 216void AIS_FixRelation::Compute(const Handle(Prs3d_Projector)& aProjector,
217 const Handle(Geom_Transformation)& aTransformation,
218 const Handle(Prs3d_Presentation)& aPresentation)
7fd59977 219{
9775fa61 220// throw Standard_NotImplemented("AIS_FixRelation::Compute(const Handle(Prs3d_Projector)&, const Handle(Geom_Transformation)&, const Handle(Prs3d_Presentation)&)");
7fd59977 221 PrsMgr_PresentableObject::Compute( aProjector , aTransformation , aPresentation ) ;
222}
223
224//=======================================================================
225//function : ComputeSelection
226//purpose :
227//=======================================================================
228
229void AIS_FixRelation::ComputeSelection(const Handle(SelectMgr_Selection)& aSelection,
230 const Standard_Integer)
231{
232 Handle(SelectMgr_EntityOwner) own = new SelectMgr_EntityOwner(this,7);
233
81bba717 234 // creation of segment sensible for the linked segment
235 // of the shape fixed to symbol 'Fix'
7fd59977 236 Handle(Select3D_SensitiveSegment) seg;
237 seg = new Select3D_SensitiveSegment(own,
238 myPntAttach,
239 myPosition);
240 aSelection->Add(seg);
241
81bba717 242 // Creation of the sensible zone of symbol 'Fix'
7fd59977 243 gp_Dir norm = myPlane->Axis().Direction();
244
245 gp_Vec dirac(myPntAttach,myPosition);
246 dirac.Normalize();
247 gp_Vec norac = dirac.Crossed(gp_Vec(norm));
248 gp_Ax1 ax(myPosition, norm);
c6541a0c 249 norac.Rotate(ax, M_PI/8);
7fd59977 250
251 norac*=(myArrowSize/2);
252 gp_Pnt P1 = myPosition.Translated(norac);
253 gp_Pnt P2 = myPosition.Translated(-norac);
254 seg = new Select3D_SensitiveSegment(own,
255 P1,
256 P2);
257 aSelection->Add(seg);
258
259 norac*=0.8;
260 P1 = myPosition.Translated(norac);
261 P2 = myPosition.Translated(-norac);
262 dirac*=(myArrowSize/2);
263 gp_Pnt PF(P1.XYZ());
264 gp_Pnt PL = PF.Translated(dirac);
265 PL.Translate(norac);
266 seg = new Select3D_SensitiveSegment(own,
267 PF,
268 PL);
269 aSelection->Add(seg);
270
271
272 PF.SetXYZ(P2.XYZ());
273 PL = PF.Translated(dirac);
274 PL.Translate(norac);
275 seg = new Select3D_SensitiveSegment(own,
276 PF,
277 PL);
278 aSelection->Add(seg);
279
280 PF.SetXYZ( (P1.XYZ() + P2.XYZ()) /2 );
281 PL = PF.Translated(dirac);
282 PL.Translate(norac);
283 seg = new Select3D_SensitiveSegment(own,
284 PF,
285 PL);
286}
287
288//=======================================================================
289//function : ComputeVertex
290//purpose : computes myPntAttach and the position of the presentation
291// when you fix a vertex
292//=======================================================================
293
294void AIS_FixRelation::ComputeVertex(const TopoDS_Vertex& /*FixVertex*/,
295 gp_Pnt& curpos)
296{
297 myPntAttach = BRep_Tool::Pnt(TopoDS::Vertex(myFShape));
298 curpos = myPosition;
299 if (myAutomaticPosition) {
300 gp_Pln pln(myPlane->Pln());
301 gp_Dir dir(pln.XAxis().Direction());
302 gp_Vec transvec = gp_Vec(dir)*myArrowSize;
303 curpos = myPntAttach.Translated(transvec);;
304 myPosition = curpos;
305 myAutomaticPosition = Standard_True;
306 }
307}
308
309//=======================================================================
310//function : ComputePosition
311//purpose :
312//=======================================================================
313
314gp_Pnt AIS_FixRelation::ComputePosition(const Handle(Geom_Curve)& curv1,
315 const Handle(Geom_Curve)& curv2,
316 const gp_Pnt& firstp1,
317 const gp_Pnt& lastp1,
318 const gp_Pnt& firstp2,
319 const gp_Pnt& lastp2) const
320{
321 //---------------------------------------------------------
81bba717 322 // calculate the point of attach
7fd59977 323 //---------------------------------------------------------
324 gp_Pnt curpos;
325
326 if (curv1->IsInstance(STANDARD_TYPE(Geom_Circle)) || curv2->IsInstance(STANDARD_TYPE(Geom_Circle))) {
327 Handle(Geom_Circle) gcirc = Handle(Geom_Circle)::DownCast(curv1);
328 if (gcirc.IsNull()) gcirc = Handle(Geom_Circle)::DownCast(curv2);
329 gp_Dir dir( gcirc->Location().XYZ() + myPntAttach.XYZ() );
330 gp_Vec transvec = gp_Vec(dir)*myArrowSize;
331 curpos = myPntAttach.Translated(transvec);
332 }
333
334 else {
335 gp_Vec vec1(firstp1,lastp1);
336 gp_Vec vec2(firstp2,lastp2);
337
338 if (!vec1.IsParallel(vec2, Precision::Angular()) ) {
339 gp_Dir dir;
340 Standard_Real conf =Precision::Confusion();
341 if (lastp1.IsEqual(firstp2,conf) || firstp1.IsEqual(lastp2,conf)) dir.SetXYZ(vec1.XYZ() - vec2.XYZ());
342 else dir.SetXYZ(vec1.XYZ() + vec2.XYZ());
343 gp_Vec transvec = gp_Vec(dir)*myArrowSize;
344 curpos = myPntAttach.Translated(transvec);
345 }
346 else {
347 gp_Vec crossvec = vec1.Crossed(vec2);
348 vec1.Cross(crossvec);
349 gp_Dir dir(vec1);
350 curpos = myPntAttach.Translated(gp_Vec(dir)*myArrowSize);
351 }
352 }
353
354 return curpos;
355}
356
357//=======================================================================
358//function : ComputePosition
359//purpose : Computes the position of the "fix dimension" when the
360// fixed object is a vertex which is set at the intersection
361// of two curves.
362// The "dimension" is in the "middle" of the two edges.
363//=======================================================================
364
365gp_Pnt AIS_FixRelation::ComputePosition(const Handle(Geom_Curve)& curv,
366 const gp_Pnt& firstp,
367 const gp_Pnt& lastp) const
368{
369 //---------------------------------------------------------
81bba717 370 // calculate the point of attach
7fd59977 371 //---------------------------------------------------------
372 gp_Pnt curpos;
373
374 if (curv->IsKind(STANDARD_TYPE(Geom_Circle))) {
375
376 Handle(Geom_Circle) gcirc = Handle(Geom_Circle)::DownCast(curv);
377 gp_Dir dir( gcirc->Location().XYZ() + myPntAttach.XYZ() );
378 gp_Vec transvec = gp_Vec(dir)*myArrowSize;
379 curpos = myPntAttach.Translated(transvec);
380
381 } //if (curv->IsKind(STANDARD_TYPE(Geom_Circle))
382
383 else {
384// gp_Pln pln(Component()->WorkingPlane()->Plane()->GetValue()->Pln());
385 gp_Pln pln(myPlane->Pln());
386 gp_Dir NormPln = pln.Axis().Direction();
387 gp_Vec vec(firstp,lastp);
388 vec.Cross( gp_Vec(NormPln));
389 vec.Normalize();
390 gp_Vec transvec = vec*myArrowSize;
391 curpos = myPntAttach.Translated(transvec);
392 gp_Ax1 RotAx( myPntAttach, NormPln);
c6541a0c 393 curpos.Rotate(RotAx, M_PI/10);
7fd59977 394 }
395
396 return curpos;
397 }
398
399//=======================================================================
400//function : ComputeEdge
401//purpose : computes myPntAttach and the position of the presentation
402// when you fix an edge
403//=======================================================================
404
405void AIS_FixRelation::ComputeEdge(const TopoDS_Edge& FixEdge, gp_Pnt& curpos)
406{
407 Handle(Geom_Curve) curEdge;
408 gp_Pnt ptbeg,ptend;
409 if (!AIS::ComputeGeometry(FixEdge,curEdge,ptbeg,ptend)) return;
410
411 //---------------------------------------------------------
412 // calcul du point de positionnement du symbole 'fix'
413 //---------------------------------------------------------
81bba717 414 //--> In case of a straight line
7fd59977 415 if (curEdge->IsKind(STANDARD_TYPE(Geom_Line))){
416 gp_Lin glin = Handle(Geom_Line)::DownCast(curEdge)->Lin();
417 Standard_Real pfirst(ElCLib::Parameter(glin,ptbeg));
418 Standard_Real plast(ElCLib::Parameter(glin,ptend));
419 ComputeLinePosition(glin, curpos, pfirst, plast);
420 }
421
81bba717 422 //--> In case of a circle
7fd59977 423 else if (curEdge->IsKind(STANDARD_TYPE(Geom_Circle))) {
424 gp_Circ gcirc = Handle(Geom_Circle)::DownCast(curEdge)->Circ();
425 Standard_Real pfirst, plast;
426 BRepAdaptor_Curve cu(FixEdge);
427 pfirst = cu.FirstParameter();
428 plast = cu.LastParameter();
429 ComputeCirclePosition(gcirc, curpos, pfirst, plast);
430 }
431
432 else
433 return;
434
435}
436
437//=======================================================================
438//function : ComputeLinePosition
439//purpose : compute the values of myPntAttach and the position <pos> of
440// the symbol when the fixed edge has a geometric support equal
441// to a line.
442//=======================================================================
443
444void AIS_FixRelation::ComputeLinePosition(const gp_Lin& glin,
445 gp_Pnt& pos,
446 Standard_Real& pfirst,
447 Standard_Real& plast)
448{
449 if (myAutomaticPosition) {
81bba717 450 // point of attach is chosen as middle of the segment
7fd59977 451 myPntAttach = ElCLib::Value((pfirst+ plast)/2, glin);
452
453 gp_Dir norm = myPlane ->Axis().Direction();
454
455 norm.Cross(glin.Position().Direction());
456 pos = myPntAttach.Translated(gp_Vec(norm)*myArrowSize);
457 myAutomaticPosition = Standard_True;
458 } // if (myAutomaticPosition)
459
460 else {
461 pos = myPosition;
462 Standard_Real linparam = ElCLib::Parameter(glin, pos);
463
81bba717 464 // case if the projection of position is located between 2 vertices
7fd59977 465 // de l'edge
466 if ( (linparam >= pfirst) && (linparam <= plast) )
467 myPntAttach = ElCLib::Value(linparam,glin);
468
81bba717 469 // case if the projection of Position is outside of the limits
470 // of the edge : the point closest to the projection is chosen
471 // as the attach point
7fd59977 472 else {
473 Standard_Real pOnLin;
474 if (linparam > plast)
475 pOnLin = plast;
476 else
477 pOnLin = pfirst;
478 myPntAttach = ElCLib::Value(pOnLin,glin);
479 gp_Dir norm = myPlane->Axis().Direction();
480
481 norm.Cross(glin.Position().Direction());
482 gp_Lin lsup(myPntAttach, norm);
483 Standard_Real parpos = ElCLib::Parameter(lsup,myPosition);
484 pos = ElCLib::Value(parpos,lsup);
485 }
486
487 }
488 myPosition = pos;
489}
490
491//=======================================================================
492//function : ComputeCirclePosition
493//purpose : compute the values of myPntAttach and the position <pos> of
494// the symbol when the fixed edge has a geometric support equal
495// to a circle.
496//=======================================================================
497
498void AIS_FixRelation::ComputeCirclePosition(
499 const gp_Circ& gcirc,
500 gp_Pnt& pos,
501 Standard_Real& pfirst,
502 Standard_Real& plast)
503{
81bba717 504 // readjust parametres on the circle
c6541a0c
D
505 if (plast > 2*M_PI ) {
506 Standard_Real nbtours = Floor(plast / (2*M_PI));
507 plast -= nbtours*2*M_PI;
508 pfirst -= nbtours*2*M_PI;
7fd59977 509 }
510
511 if (myAutomaticPosition) {
81bba717 512 // the point attach is the "middle" of the segment (relatively
513 // to the parametres of start and end vertices of the edge
7fd59977 514
515 Standard_Real circparam = (pfirst + plast)/2.;
516
517 if ( !InDomain(pfirst,plast,circparam)) {
c6541a0c
D
518 Standard_Real otherpar = circparam + M_PI;
519 if (otherpar > 2*M_PI) otherpar -= 2*M_PI;
7fd59977 520 circparam = otherpar;
521 }
522
523 myPntAttach = ElCLib::Value(circparam, gcirc );
524
525 gp_Vec dir( gcirc.Location().XYZ(), myPntAttach.XYZ() );
526 dir.Normalize();
527 gp_Vec transvec = dir*myArrowSize;
528 pos = myPntAttach.Translated(transvec);
529 myPosition = pos;
530 myAutomaticPosition = Standard_True;
531 } // if (myAutomaticPosition)
532
533 else {
81bba717 534 // case if the projection of myPosition is outside of 2
535 // vertices of the edge. In this case the parameter is readjusted
536 // in the valid part of the circle
7fd59977 537 pos = myPosition;
538
539 Standard_Real circparam = ElCLib::Parameter(gcirc, pos);
540
541 if ( !InDomain(pfirst,plast,circparam)) {
c6541a0c
D
542 Standard_Real otherpar = circparam + M_PI;
543 if (otherpar > 2*M_PI) otherpar -= 2*M_PI;
7fd59977 544 circparam = otherpar;
545 }
546
547 myPntAttach = ElCLib::Value(circparam,gcirc);
548 }
549}
550
551//=======================================================================
552//function : ConnectedEdges
553//purpose :
554//=======================================================================
555Standard_Boolean AIS_FixRelation::ConnectedEdges(const TopoDS_Wire& WIRE,
556 const TopoDS_Vertex& V,
557 TopoDS_Edge& E1,
558 TopoDS_Edge& E2)
559{
560 TopTools_IndexedDataMapOfShapeListOfShape vertexMap;
561 TopExp::MapShapesAndAncestors (WIRE,TopAbs_VERTEX,TopAbs_EDGE,vertexMap);
562
563 Standard_Boolean found(Standard_False);
564 TopoDS_Vertex theVertex;
565 for (Standard_Integer i=1; i<=vertexMap.Extent() && !found; i++) {
566 if (vertexMap.FindKey(i).IsSame(V)) {
567 theVertex = TopoDS::Vertex(vertexMap.FindKey(i));
568 found = Standard_True;
569 }
570 }
571 if (!found) {
572 E1.Nullify();
573 E2.Nullify();
574 return Standard_False;
575 }
576
577 TopTools_ListIteratorOfListOfShape iterator(vertexMap.FindFromKey(theVertex));
578 if (iterator.More()) {
579 E1 = TopoDS::Edge(iterator.Value());
580 BRepAdaptor_Curve curv(E1);
581 iterator.Next();
582 }
583 else {
584 E1.Nullify();
585 return Standard_False;
586 }
587
588 if (iterator.More()) {
589 E2 = TopoDS::Edge(iterator.Value());
590 BRepAdaptor_Curve curv(E2);
591 iterator.Next();
592 }
593 else {
594 E2.Nullify();
595 return Standard_False;
596 }
597
598 if (iterator.More()) {
599 E1.Nullify();
600 E2.Nullify();
601 return Standard_False;
602 }
603 return Standard_True;
604}