1 // Modified 23/02/98 : FMN ; Remplacement PI par Standard_PI
3 // 10/11/98 : GG ; Protection sur methode IsOn() lorsque
4 // les points sont confondus.
6 #define G002 //GG_100500
7 // Change IsOn method with a best computation on short segments.
10 #include <Graphic2d_Line.ixx>
12 Graphic2d_Line::Graphic2d_Line (const Handle(Graphic2d_GraphicObject)& aGraphicObject)
13 :Graphic2d_Primitive (aGraphicObject),
14 myTypeOfPolygonFilling (Graphic2d_TOPF_EMPTY),
15 myDrawEdge (Standard_True),
19 myInteriorColorIndex (1) {
21 SetFamily(Graphic2d_TOP_LINE);
24 void Graphic2d_Line::SetWidthIndex (const Standard_Integer anIndex) {
26 myWidthIndex = anIndex;
31 void Graphic2d_Line::SetTypeIndex (const Standard_Integer anIndex) {
33 myTypeIndex = anIndex;
38 void Graphic2d_Line::SetInteriorColorIndex (const Standard_Integer anIndex) {
40 myInteriorColorIndex = anIndex;
45 void Graphic2d_Line::SetDrawEdge (const Standard_Boolean aDraw) {
52 void Graphic2d_Line::SetInteriorPattern (const Standard_Integer anIndex) {
54 myPatternIndex = anIndex;
59 void Graphic2d_Line::SetTypeOfPolygonFilling (const Graphic2d_TypeOfPolygonFilling aType) {
61 myTypeOfPolygonFilling = aType;
66 Standard_Integer Graphic2d_Line::WidthIndex () const {
72 Standard_Integer Graphic2d_Line::InteriorColorIndex () const {
74 return myInteriorColorIndex;
78 Standard_Integer Graphic2d_Line::InteriorPattern () const {
80 return myPatternIndex;
84 Graphic2d_TypeOfPolygonFilling Graphic2d_Line::TypeOfPolygonFilling () const {
86 return myTypeOfPolygonFilling;
90 Standard_Integer Graphic2d_Line::TypeIndex () const {
96 void Graphic2d_Line::DrawLineAttrib (const Handle(Graphic2d_Drawer)& aDrawer)
99 aDrawer->SetLineAttrib (myColorIndex,myTypeIndex,myWidthIndex);
101 switch (myTypeOfPolygonFilling) {
102 case Graphic2d_TOPF_FILLED:
103 aDrawer->SetPolyAttrib (myInteriorColorIndex,0,myDrawEdge);
105 case Graphic2d_TOPF_PATTERNED:
106 aDrawer->SetPolyAttrib (myInteriorColorIndex,
107 myPatternIndex,myDrawEdge);
114 void Graphic2d_Line::DrawMarkerAttrib (const Handle(Graphic2d_Drawer)& aDrawer)
117 Standard_Boolean filled = (myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY);
119 switch (myTypeOfPolygonFilling) {
120 case Graphic2d_TOPF_FILLED:
121 aDrawer->SetPolyAttrib (myInteriorColorIndex,0,Standard_False);
123 case Graphic2d_TOPF_PATTERNED:
124 aDrawer->SetPolyAttrib (myInteriorColorIndex,myPatternIndex,Standard_False);
129 aDrawer->SetMarkerAttrib (myColorIndex,myWidthIndex,filled);
133 Standard_Boolean Graphic2d_Line::IsIn ( const Standard_ShortReal aX,
134 const Standard_ShortReal aY,
135 const TShort_Array1OfShortReal& X,
136 const TShort_Array1OfShortReal& Y,
137 const Standard_ShortReal aPrecision)
139 Standard_Integer i1=0,i2=0,n;
140 Standard_Real dx1,dy1,dx2,dy2,anglesum=0.,angle;
141 Standard_Real prosca,provec,norme1,norme2,cosin;
143 for (Standard_Integer m = 1; m <= n; m++) {
145 i2 = (i1 == n) ? 1 : i1 + 1;
146 dx1 = X(i1) - aX; dy1 = Y(i1) - aY;
147 dx2 = X(i2) - aX; dy2 = Y(i2) - aY;
148 prosca = dx1 * dx2 + dy1 * dy2;
149 provec = dx1 * dy2 - dx2 * dy1;
150 norme1 = Sqrt ( dx1 * dx1 + dy1 * dy1 );
151 norme2 = Sqrt ( dx2 * dx2 + dy2 * dy2 );
152 if ( norme1 <= aPrecision || norme2 <= aPrecision ) return Standard_True;
154 cosin = prosca / norme1 / norme2;
155 if ( cosin >= 1 ) angle = 0.;
157 if ( cosin <= -1) angle = - M_PI;
159 angle = Sign ( ACos ( cosin ) , provec );
161 anglesum = anglesum + angle;}
162 return (Abs (anglesum) > 1.) ;
165 Standard_Boolean Graphic2d_Line::IsOn ( const Standard_ShortReal aX,
166 const Standard_ShortReal aY,
167 const Standard_ShortReal aX1,
168 const Standard_ShortReal aY1,
169 const Standard_ShortReal aX2,
170 const Standard_ShortReal aY2,
171 const Standard_ShortReal aPrecision)
174 Standard_ShortReal DX = aX2 - aX1, DY = aY2 - aY1, dd = DX*DX + DY*DY;
178 if( Sqrt(dd) < aPrecision )
180 if( dd < aPrecision )
182 return (Abs (aX - aX1) + Abs (aY - aY1)) < aPrecision;
184 Standard_ShortReal lambda = (DX*(aX-aX1) + DY*(aY-aY1)) / dd;
186 if ( lambda >= 0. && lambda <= 1. ) {
188 // On prend comme norme la somme des valeurs absolues:
189 Standard_ShortReal Xproj = aX1 + lambda * DX;
190 Standard_ShortReal Yproj = aY1 + lambda * DY;
191 return (Abs (aX - Xproj) + Abs (aY - Yproj)) < aPrecision;
195 return Standard_False;
198 void Graphic2d_Line::Save(Aspect_FStream& aFStream) const
200 *aFStream << myColorIndex << ' ' << myWidthIndex << endl;
201 *aFStream << myTypeIndex << ' ' << myPatternIndex << endl;
202 *aFStream << myInteriorColorIndex << ' ' << myTypeOfPolygonFilling << ' ' << myDrawEdge << endl;
205 void Graphic2d_Line::Retrieve(Aspect_IFStream& anIFStream)
208 *anIFStream >> myColorIndex >> myWidthIndex;
209 *anIFStream >> myTypeIndex >> myPatternIndex;
210 *anIFStream >> myInteriorColorIndex;
212 myTypeOfPolygonFilling=Graphic2d_TypeOfPolygonFilling(topf);
213 *anIFStream >> myDrawEdge;