xref: /petsc/src/sys/classes/draw/interface/drect.c (revision 9895aa37ac365bac650f6bd8bf977519f7222510)
1 
2 /*
3        Provides the calling sequences for all the basic PetscDraw routines.
4 */
5 #include <petsc-private/drawimpl.h>  /*I "petscdraw.h" I*/
6 
7 
8 #undef __FUNCT__
9 #define __FUNCT__ "PetscDrawIndicatorFunction"
10 /*@C
11    PetscDrawIndicatorFunction - Draws an indicator function (where a relationship is true) on a PetscDraw
12 
13    Not collective
14 
15    Input Parameter:
16 +  draw - a PetscDraw
17 .  xmin,xmax,ymin,ymax - region to draw indicator function
18 -  f - the indicator function
19 
20    Level: developer
21 
22 @*/
23 PetscErrorCode PetscDrawIndicatorFunction(PetscDraw draw, PetscReal xmin, PetscReal xmax, PetscReal ymin, PetscReal ymax,int c,PetscErrorCode (*f)(void*,PetscReal,PetscReal,PetscBool*),void *ctx)
24 {
25   PetscInt       xstart,ystart,xend,yend,i,j,tmp;
26   PetscErrorCode ierr;
27   PetscReal      x,y;
28   PetscBool      isnull,flg;
29 
30   PetscFunctionBegin;
31   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
32   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
33   if (isnull) PetscFunctionReturn(0);
34 
35   ierr = PetscDrawCoordinateToPixel(draw,xmin,ymin,&xstart,&ystart);CHKERRQ(ierr);
36   ierr = PetscDrawCoordinateToPixel(draw,xmax,ymax,&xend,&yend);CHKERRQ(ierr);
37   if (yend < ystart) {
38     tmp    = ystart;
39     ystart = yend;
40     yend   = tmp;
41   }
42   for (i=xstart; i<xend+1; i++) {
43     for (j=ystart; j<yend+1; j++) {
44       ierr = PetscDrawPixelToCoordinate(draw,i,j,&x,&y);CHKERRQ(ierr);
45       ierr = f(ctx,x,y,&flg);CHKERRQ(ierr);
46       if (flg) {
47         ierr = PetscDrawPointPixel(draw,i,j,c);CHKERRQ(ierr);
48       }
49     }
50   }
51   PetscFunctionReturn(0);
52 }
53 
54 
55 #undef __FUNCT__
56 #define __FUNCT__ "PetscDrawCoordinateToPixel"
57 /*@C
58    PetscDrawCoordinateToPixel - given a coordinate in a PetscDraw returns the pixel location
59 
60    Not collective
61 
62    Input Parameters:
63 +  draw - the draw where the coordinates are defined
64 -  x,y - the coordinate location
65 
66    Output Parameters:
67 -  i,j - the pixel location
68 
69    Level: developer
70 
71 @*/
72 PetscErrorCode PetscDrawCoordinateToPixel(PetscDraw draw,PetscReal x,PetscReal y,PetscInt *i,PetscInt *j)
73 {
74   PetscErrorCode ierr;
75   PetscBool      isnull;
76 
77   PetscFunctionBegin;
78   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
79   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
80   if (isnull) PetscFunctionReturn(0);
81   if (!draw->ops->coordinatetopixel) SETERRQ(PetscObjectComm((PetscObject)draw),PETSC_ERR_SUP,"No support for locating pixel");
82   ierr = (*draw->ops->coordinatetopixel)(draw,x,y,i,j);CHKERRQ(ierr);
83   PetscFunctionReturn(0);
84 }
85 
86 #undef __FUNCT__
87 #define __FUNCT__ "PetscDrawPixelToCoordinate"
88 /*@C
89    PetscDrawPixelToCoordinate - given a pixel in a PetscDraw returns the coordinate
90 
91    Not collective
92 
93    Input Parameters:
94 +  draw - the draw where the coordinates are defined
95 -  i,j - the pixel location
96 
97    Output Parameters:
98 .  x,y - the coordinate location
99 
100    Level: developer
101 
102 @*/
103 PetscErrorCode PetscDrawPixelToCoordinate(PetscDraw draw,PetscInt i,PetscInt j,PetscReal *x,PetscReal *y)
104 {
105   PetscErrorCode ierr;
106   PetscBool      isnull;
107 
108   PetscFunctionBegin;
109   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
110   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
111   if (isnull) PetscFunctionReturn(0);
112   if (!draw->ops->pixeltocoordinate) SETERRQ(PetscObjectComm((PetscObject)draw),PETSC_ERR_SUP,"No support for locating coordiante from ");
113   ierr = (*draw->ops->pixeltocoordinate)(draw,i,j,x,y);CHKERRQ(ierr);
114   PetscFunctionReturn(0);
115 }
116 
117 #undef __FUNCT__
118 #define __FUNCT__ "PetscDrawRectangle"
119 /*@
120    PetscDrawRectangle - PetscDraws a rectangle  onto a drawable.
121 
122    Not Collective
123 
124    Input Parameters:
125 +  draw - the drawing context
126 .  xl,yl,xr,yr - the coordinates of the lower left, upper right corners
127 -  c1,c2,c3,c4 - the colors of the four corners in counter clockwise order
128 
129    Level: beginner
130 
131    Concepts: drawing^rectangle
132    Concepts: graphics^rectangle
133    Concepts: rectangle
134 
135 @*/
136 PetscErrorCode  PetscDrawRectangle(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr,int c1,int c2,int c3,int c4)
137 {
138   PetscErrorCode ierr;
139   PetscBool      isnull;
140 
141   PetscFunctionBegin;
142   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
143   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
144   if (isnull) PetscFunctionReturn(0);
145   if (!draw->ops->rectangle) SETERRQ(PetscObjectComm((PetscObject)draw),PETSC_ERR_SUP,"No support for drawing rectangle");
146   ierr = (*draw->ops->rectangle)(draw,xl,yl,xr,yr,c1,c2,c3,c4);CHKERRQ(ierr);
147   PetscFunctionReturn(0);
148 }
149 
150 #undef __FUNCT__
151 #define __FUNCT__ "PetscDrawSave"
152 /*@
153    PetscDrawSave - Saves a drawn image
154 
155    Not Collective
156 
157    Input Parameters:
158 .  draw - the drawing context
159 
160    Level: advanced
161 
162    Notes: this is not normally called by the user, it is called by PetscDrawClear_X() to save a sequence of images.
163 
164 .seealso: PetscDrawSetSave()
165 
166 @*/
167 PetscErrorCode  PetscDrawSave(PetscDraw draw)
168 {
169   PetscErrorCode ierr;
170   PetscBool      isnull;
171 
172   PetscFunctionBegin;
173   PetscValidHeaderSpecific(draw,PETSC_DRAW_CLASSID,1);
174   ierr = PetscObjectTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);CHKERRQ(ierr);
175   if (isnull) PetscFunctionReturn(0);
176   if (!draw->ops->save) PetscFunctionReturn(0);
177   ierr = (*draw->ops->save)(draw);CHKERRQ(ierr);
178   PetscFunctionReturn(0);
179 }
180