summaryrefslogtreecommitdiff
path: root/src/Graphic2d/Graphic2d_Line.cxx
blob: 54ae7737bf20c38d4512f67a64aa910d852d776f (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
// Modified     23/02/98 : FMN ; Remplacement PI par Standard_PI

//		10/11/98 : GG ; Protection sur methode IsOn() lorsque
//				les points sont confondus.

#define G002     //GG_100500
//              Change IsOn method with a best computation on short segments.
//

#include <Graphic2d_Line.ixx>

Graphic2d_Line::Graphic2d_Line (const Handle(Graphic2d_GraphicObject)& aGraphicObject)
         :Graphic2d_Primitive (aGraphicObject),
	  myTypeOfPolygonFilling (Graphic2d_TOPF_EMPTY),
          myDrawEdge (Standard_True),
          myWidthIndex	(0),
	  myTypeIndex	(0),
          myPatternIndex (0),
          myInteriorColorIndex (1)  {

	SetFamily(Graphic2d_TOP_LINE);
}

void Graphic2d_Line::SetWidthIndex (const Standard_Integer anIndex) {

	myWidthIndex	= anIndex;
	ResetIndex ();

}

void Graphic2d_Line::SetTypeIndex (const Standard_Integer anIndex) {

	myTypeIndex	= anIndex;
	ResetIndex ();

}

void Graphic2d_Line::SetInteriorColorIndex (const Standard_Integer anIndex) {

	myInteriorColorIndex	= anIndex;
	ResetIndex ();

}

void Graphic2d_Line::SetDrawEdge (const Standard_Boolean aDraw) {

	myDrawEdge = aDraw;
	ResetIndex ();

}

void Graphic2d_Line::SetInteriorPattern (const Standard_Integer anIndex) {
  
	myPatternIndex = anIndex;
	ResetIndex ();

}

void Graphic2d_Line::SetTypeOfPolygonFilling (const Graphic2d_TypeOfPolygonFilling aType) {

	myTypeOfPolygonFilling = aType;
	ResetIndex ();

}

Standard_Integer Graphic2d_Line::WidthIndex () const { 

	return myWidthIndex;

}

Standard_Integer Graphic2d_Line::InteriorColorIndex () const { 

	return myInteriorColorIndex;

}

Standard_Integer Graphic2d_Line::InteriorPattern () const { 

	return myPatternIndex;

}

Graphic2d_TypeOfPolygonFilling Graphic2d_Line::TypeOfPolygonFilling () const { 

	return myTypeOfPolygonFilling;

}

Standard_Integer Graphic2d_Line::TypeIndex () const { 

	return myTypeIndex;

}

void Graphic2d_Line::DrawLineAttrib (const Handle(Graphic2d_Drawer)& aDrawer) 
 const {

	aDrawer->SetLineAttrib (myColorIndex,myTypeIndex,myWidthIndex);

	switch (myTypeOfPolygonFilling) {
		case Graphic2d_TOPF_FILLED:
		aDrawer->SetPolyAttrib (myInteriorColorIndex,0,myDrawEdge);
		break;
		case Graphic2d_TOPF_PATTERNED:
		aDrawer->SetPolyAttrib (myInteriorColorIndex,
						myPatternIndex,myDrawEdge);
		break;
		default: break;
	}

}

void Graphic2d_Line::DrawMarkerAttrib (const Handle(Graphic2d_Drawer)& aDrawer) 
 const {

Standard_Boolean filled = (myTypeOfPolygonFilling != Graphic2d_TOPF_EMPTY);

	switch (myTypeOfPolygonFilling) {
		case Graphic2d_TOPF_FILLED:
		aDrawer->SetPolyAttrib (myInteriorColorIndex,0,Standard_False);
		break;
		case Graphic2d_TOPF_PATTERNED:
		aDrawer->SetPolyAttrib (myInteriorColorIndex,myPatternIndex,Standard_False);
		break;
		default: break;
	}

	aDrawer->SetMarkerAttrib (myColorIndex,myWidthIndex,filled);

}

Standard_Boolean Graphic2d_Line::IsIn ( const Standard_ShortReal aX,
					const Standard_ShortReal aY,
					const TShort_Array1OfShortReal&  X,
					const TShort_Array1OfShortReal&  Y,
					const Standard_ShortReal aPrecision)
{ 
  Standard_Integer i1=0,i2=0,n;
  Standard_Real dx1,dy1,dx2,dy2,anglesum=0.,angle;
  Standard_Real prosca,provec,norme1,norme2,cosin;
  n = X.Length ();
  for (Standard_Integer m = 1; m <= n; m++) {
    i1++; 
    i2 = (i1 == n) ? 1 : i1 + 1;
    dx1 = X(i1)  - aX; dy1 = Y(i1) - aY;
    dx2 = X(i2)  - aX; dy2 = Y(i2) - aY;
    prosca = dx1 * dx2 + dy1 * dy2;
    provec = dx1 * dy2 - dx2 * dy1;    
    norme1  = Sqrt ( dx1 * dx1 + dy1 * dy1 );
    norme2  = Sqrt ( dx2 * dx2 + dy2 * dy2 );
    if ( norme1 <= aPrecision || norme2 <= aPrecision ) return Standard_True;

    cosin = prosca / norme1 / norme2;
    if ( cosin >= 1 ) angle = 0.;
    else {
      if ( cosin <= -1) angle = - Standard_PI;
      else
	angle = Sign ( ACos ( cosin ) , provec );
	}
    anglesum = anglesum + angle;}
    return (Abs (anglesum) > 1.) ;
}

Standard_Boolean Graphic2d_Line::IsOn ( const Standard_ShortReal aX,
					const Standard_ShortReal aY,
					const Standard_ShortReal aX1,
					const Standard_ShortReal aY1,
					const Standard_ShortReal aX2,
					const Standard_ShortReal aY2,
					const Standard_ShortReal aPrecision)
{

Standard_ShortReal DX = aX2 - aX1, DY = aY2 - aY1, dd = DX*DX + DY*DY;


#ifdef G002
        if( Sqrt(dd) < aPrecision )
#else
	if( dd < aPrecision )
#endif
	  return (Abs (aX - aX1) + Abs (aY - aY1)) < aPrecision;

Standard_ShortReal lambda = (DX*(aX-aX1) + DY*(aY-aY1)) / dd;

	if ( lambda >= 0. && lambda <= 1. ) {

		//  On prend comme norme la somme des valeurs absolues:
		Standard_ShortReal Xproj = aX1 + lambda * DX;
		Standard_ShortReal Yproj = aY1 + lambda * DY;
		return (Abs (aX - Xproj) + Abs (aY - Yproj)) < aPrecision;

	}
	else
		return Standard_False;
}

void Graphic2d_Line::Save(Aspect_FStream& aFStream) const
{
	*aFStream << myColorIndex << ' ' << myWidthIndex << endl;
	*aFStream << myTypeIndex << ' ' << myPatternIndex << endl;
	*aFStream << myInteriorColorIndex << ' ' << myTypeOfPolygonFilling << ' ' << myDrawEdge << endl;
}

void Graphic2d_Line::Retrieve(Aspect_IFStream& anIFStream)
{
	int topf;
	*anIFStream >> myColorIndex >> myWidthIndex;
	*anIFStream >> myTypeIndex >> myPatternIndex;
	*anIFStream >> myInteriorColorIndex;
	*anIFStream >> topf;
	myTypeOfPolygonFilling=Graphic2d_TypeOfPolygonFilling(topf);
	*anIFStream >> myDrawEdge;
}