Actual source code: dviewp.c

  1: /*
  2:        Provides the calling sequences for all the basic PetscDraw routines.
  3: */
 4:  #include src/sys/src/draw/drawimpl.h

  8: /*@
  9:    PetscDrawSetViewPort - Sets the portion of the window (page) to which draw
 10:    routines will write.

 12:    Collective on PetscDraw

 14:    Input Parameters:
 15: +  xl,yl,xr,yr - upper right and lower left corners of subwindow
 16:                  These numbers must always be between 0.0 and 1.0.
 17:                  Lower left corner is (0,0).
 18: -  draw - the drawing context

 20:    Level: advanced

 22:    Concepts: drawing^in subset of window
 23:    Concepts: graphics^in subset of window

 25: @*/
 26: PetscErrorCode PetscDrawSetViewPort(PetscDraw draw,PetscReal xl,PetscReal yl,PetscReal xr,PetscReal yr)
 27: {
 31:   if (xl < 0.0 || xr > 1.0 || yl < 0.0 || yr > 1.0 || xr <= xl || yr <= yl) {
 32:     SETERRQ4(PETSC_ERR_ARG_OUTOFRANGE,"ViewPort values must be >= 0 and <= 1: Instead %g %g %g %g",xl,yl,xr,yr);
 33:   }
 34:   draw->port_xl = xl; draw->port_yl = yl;
 35:   draw->port_xr = xr; draw->port_yr = yr;
 36:   if (draw->ops->setviewport) {
 37:     (*draw->ops->setviewport)(draw,xl,yl,xr,yr);
 38:   }
 39:   return(0);
 40: }

 44: /*@
 45:    PetscDrawSplitViewPort - Splits a window shared by several processes into smaller
 46:    view ports. One for each process. 

 48:    Collective on PetscDraw

 50:    Input Parameter:
 51: .  draw - the drawing context

 53:    Level: advanced

 55:    Concepts: drawing^in subset of window

 57: .seealso: PetscDrawDivideViewPort(), PetscDrawSetViewPort()

 59: @*/
 60: PetscErrorCode PetscDrawSplitViewPort(PetscDraw draw)
 61: {
 63:   PetscMPIInt    rank,size;
 64:   int            n;
 65:   PetscTruth     isnull;
 66:   PetscReal      xl,xr,yl,yr,h;

 70:   PetscTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
 71:   if (isnull) return(0);

 73:   MPI_Comm_rank(draw->comm,&rank);
 74:   MPI_Comm_size(draw->comm,&size);

 76:   n = (int)(.1 + sqrt((double)size));
 77:   while (n*n < size) {n++;}

 79:   h  = 1.0/n;
 80:   xl = (rank % n)*h;
 81:   xr = xl + h;
 82:   yl = (rank/n)*h;
 83:   yr = yl + h;

 85:   PetscDrawLine(draw,xl,yl,xl,yr,PETSC_DRAW_BLACK);
 86:   PetscDrawLine(draw,xl,yr,xr,yr,PETSC_DRAW_BLACK);
 87:   PetscDrawLine(draw,xr,yr,xr,yl,PETSC_DRAW_BLACK);
 88:   PetscDrawLine(draw,xr,yl,xl,yl,PETSC_DRAW_BLACK);
 89:   PetscDrawSynchronizedFlush(draw);

 91:   draw->port_xl = xl + .1*h;
 92:   draw->port_xr = xr - .1*h;
 93:   draw->port_yl = yl + .1*h;
 94:   draw->port_yr = yr - .1*h;

 96:   if (draw->ops->setviewport) {
 97:      (*draw->ops->setviewport)(draw,xl,yl,xr,yr);
 98:   }
 99:   return(0);
100: }

104: /*@C
105:    PetscDrawViewPortsCreate - Splits a window into smaller
106:        view ports. Each processor shares all the viewports.

108:    Collective on PetscDraw

110:    Input Parameter:
111: .  draw - the drawing context

113:    Output Parameter:
114: .  divide - a PetscDrawViewPorts context (C structure)

116:    Level: advanced

118:    Concepts: drawing^in subset of window

120: .seealso: PetscDrawSplitViewPort(), PetscDrawSetViewPort(), PetscDrawViewPortsSet(), PetscDrawViewPortsDestroy()

122: @*/
123: PetscErrorCode PetscDrawViewPortsCreate(PetscDraw draw,int nports,PetscDrawViewPorts **ports)
124: {
125:   int        i,n;
127:   PetscTruth isnull;
128:   PetscReal  *xl,*xr,*yl,*yr,h;

133:   PetscTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
134:   if (isnull) {
135:     *ports = PETSC_NULL;
136:     return(0);
137:   }

139:   PetscNew(PetscDrawViewPorts,ports);
140:   (*ports)->draw   = draw;
141:   (*ports)->nports = nports;

143:   PetscObjectReference((PetscObject)draw);

145:   n = (int)(.1 + sqrt((double)nports));
146:   while (n*n < nports) {n++;}
147: 
148:   PetscMalloc(n*n*sizeof(PetscReal),&xl);(*ports)->xl = xl;
149:   PetscMalloc(n*n*sizeof(PetscReal),&xr);(*ports)->xr = xr;
150:   PetscMalloc(n*n*sizeof(PetscReal),&yl);(*ports)->yl = yl;
151:   PetscMalloc(n*n*sizeof(PetscReal),&yr);(*ports)->yr = yr;

153:   h  = 1.0/n;

155:   for (i=0; i<n*n; i++) {
156:     xl[i] = (i % n)*h;
157:     xr[i] = xl[i] + h;
158:     yl[i] = (i/n)*h;
159:     yr[i] = yl[i] + h;

161:     PetscDrawLine(draw,xl[i],yl[i],xl[i],yr[i],PETSC_DRAW_BLACK);
162:     PetscDrawLine(draw,xl[i],yr[i],xr[i],yr[i],PETSC_DRAW_BLACK);
163:     PetscDrawLine(draw,xr[i],yr[i],xr[i],yl[i],PETSC_DRAW_BLACK);
164:     PetscDrawLine(draw,xr[i],yl[i],xl[i],yl[i],PETSC_DRAW_BLACK);

166:     xl[i] += .1*h;
167:     xr[i] -= .1*h;
168:     yl[i] += .1*h;
169:     yr[i] -= .1*h;
170:   }
171:   PetscDrawSynchronizedFlush(draw);

173:   return(0);
174: }

178: /*@C
179:    PetscDrawViewPortsDestroy - frees a PetscDrawViewPorts object

181:    Collective on PetscDraw inside PetscDrawViewPorts

183:    Input Parameter:
184: .  ports - the PetscDrawViewPorts object

186:    Level: advanced

188: .seealso: PetscDrawSplitViewPort(), PetscDrawSetViewPort(), PetscDrawViewPortsSet(), PetscDrawViewPortsCreate()

190: @*/
191: PetscErrorCode PetscDrawViewPortsDestroy(PetscDrawViewPorts *ports)
192: {


197:   if (!ports) return(0);
198:   if (ports->draw) {PetscDrawDestroy(ports->draw);}
199:   PetscFree(ports->xl);
200:   PetscFree(ports->xr);
201:   PetscFree(ports->yl);
202:   PetscFree(ports->yr);
203:   PetscFree(ports);

205:   return(0);
206: }

210: /*@C
211:    PetscDrawViewPortsSet - sets a draw object to use a particular subport

213:    Collective on PetscDraw inside PetscDrawViewPorts

215:    Input Parameter:
216: +  ports - the PetscDrawViewPorts object
217: -  port - the port number, from 0 to nports-1

219:    Level: advanced

221:    Concepts: drawing^in subset of window

223: .seealso: PetscDrawSplitViewPort(), PetscDrawSetViewPort(), PetscDrawViewPortsDestroy(), PetscDrawViewPortsCreate()

225: @*/
226: PetscErrorCode PetscDrawViewPortsSet(PetscDrawViewPorts *ports,int port)
227: {

231:   if (ports) {
232:     if (port < 0 || port > ports->nports-1) {
233:       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Port is out of range requested %d from 0 to %d\n",port,ports->nports);
234:     }
235:     PetscDrawSetViewPort(ports->draw,ports->xl[port],ports->yl[port],ports->xr[port],ports->yr[port]);
236:   }
237:   return(0);
238: }