Actual source code: dviewp.c

  1: /*$Id: dviewp.c,v 1.45 2001/03/23 23:20:08 balay Exp $*/
  2: /*
  3:        Provides the calling sequences for all the basic PetscDraw routines.
  4: */
 5:  #include src/sys/src/draw/drawimpl.h

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

 11:    Collective on PetscDraw

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

 19:    Level: advanced

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

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

 41: /*@
 42:    PetscDrawSplitViewPort - Splits a window shared by several processes into smaller
 43:    view ports. One for each process. 

 45:    Collective on PetscDraw

 47:    Input Parameter:
 48: .  draw - the drawing context

 50:    Level: advanced

 52:    Concepts: drawing^in subset of window

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

 56: @*/
 57: int PetscDrawSplitViewPort(PetscDraw draw)
 58: {
 59:   int        rank,size,n,ierr;
 60:   PetscTruth isnull;
 61:   PetscReal  xl,xr,yl,yr,h;

 65:   PetscTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
 66:   if (isnull) return(0);

 68:   MPI_Comm_rank(draw->comm,&rank);
 69:   MPI_Comm_size(draw->comm,&size);

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

 74:   h  = 1.0/n;
 75:   xl = (rank % n)*h;
 76:   xr = xl + h;
 77:   yl = (rank/n)*h;
 78:   yr = yl + h;

 80:   PetscDrawLine(draw,xl,yl,xl,yr,PETSC_DRAW_BLACK);
 81:   PetscDrawLine(draw,xl,yr,xr,yr,PETSC_DRAW_BLACK);
 82:   PetscDrawLine(draw,xr,yr,xr,yl,PETSC_DRAW_BLACK);
 83:   PetscDrawLine(draw,xr,yl,xl,yl,PETSC_DRAW_BLACK);
 84:   PetscDrawSynchronizedFlush(draw);

 86:   draw->port_xl = xl + .1*h;
 87:   draw->port_xr = xr - .1*h;
 88:   draw->port_yl = yl + .1*h;
 89:   draw->port_yr = yr - .1*h;

 91:   if (draw->ops->setviewport) {
 92:      (*draw->ops->setviewport)(draw,xl,yl,xr,yr);
 93:   }
 94:   return(0);
 95: }

 97: /*@C
 98:    PetscDrawViewPortsCreate - Splits a window into smaller
 99:        view ports. Each processor shares all the viewports.

101:    Collective on PetscDraw

103:    Input Parameter:
104: .  draw - the drawing context

106:    Output Parameter:
107: .  divide - a PetscDrawViewPorts context (C structure)

109:    Level: advanced

111:    Concepts: drawing^in subset of window

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

115: @*/
116: int PetscDrawViewPortsCreate(PetscDraw draw,int nports,PetscDrawViewPorts **ports)
117: {
118:   int        i,ierr,n;
119:   PetscTruth isnull;
120:   PetscReal  *xl,*xr,*yl,*yr,h;

124:   PetscTypeCompare((PetscObject)draw,PETSC_DRAW_NULL,&isnull);
125:   if (isnull) {
126:     *ports = PETSC_NULL;
127:     return(0);
128:   }

130:   ierr             = PetscNew(PetscDrawViewPorts,ports);
131:   (*ports)->draw   = draw;
132:   (*ports)->nports = nports;

134:   PetscObjectReference((PetscObject)draw);

136:   n = (int)(.1 + sqrt((double)nports));
137:   while (n*n < nports) {n++;}
138: 
139:   PetscMalloc(n*n*sizeof(PetscReal),&xl);(*ports)->xl = xl;
140:   PetscMalloc(n*n*sizeof(PetscReal),&xr);(*ports)->xr = xr;
141:   PetscMalloc(n*n*sizeof(PetscReal),&yl);(*ports)->yl = yl;
142:   PetscMalloc(n*n*sizeof(PetscReal),&yr);(*ports)->yr = yr;

144:   h  = 1.0/n;

146:   for (i=0; i<n*n; i++) {
147:     xl[i] = (i % n)*h;
148:     xr[i] = xl[i] + h;
149:     yl[i] = (i/n)*h;
150:     yr[i] = yl[i] + h;

152:     PetscDrawLine(draw,xl[i],yl[i],xl[i],yr[i],PETSC_DRAW_BLACK);
153:     PetscDrawLine(draw,xl[i],yr[i],xr[i],yr[i],PETSC_DRAW_BLACK);
154:     PetscDrawLine(draw,xr[i],yr[i],xr[i],yl[i],PETSC_DRAW_BLACK);
155:     PetscDrawLine(draw,xr[i],yl[i],xl[i],yl[i],PETSC_DRAW_BLACK);

157:     xl[i] += .1*h;
158:     xr[i] -= .1*h;
159:     yl[i] += .1*h;
160:     yr[i] -= .1*h;
161:   }
162:   PetscDrawSynchronizedFlush(draw);

164:   return(0);
165: }

167: /*@C
168:    PetscDrawViewPortsDestroy - frees a PetscDrawViewPorts object

170:    Collective on PetscDraw inside PetscDrawViewPorts

172:    Input Parameter:
173: .  ports - the PetscDrawViewPorts object

175:    Level: advanced

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

179: @*/
180: int PetscDrawViewPortsDestroy(PetscDrawViewPorts *ports)
181: {
182:   int        ierr;


186:   if (!ports) return(0);
187:   if (ports->draw) {PetscDrawDestroy(ports->draw);}
188:   PetscFree(ports->xl);
189:   PetscFree(ports->xr);
190:   PetscFree(ports->yl);
191:   PetscFree(ports->yr);
192:   PetscFree(ports);

194:   return(0);
195: }

197: /*@C
198:    PetscDrawViewPortsSet - sets a draw object to use a particular subport

200:    Collective on PetscDraw inside PetscDrawViewPorts

202:    Input Parameter:
203: +  ports - the PetscDrawViewPorts object
204: -  port - the port number, from 0 to nports-1

206:    Level: advanced

208:    Concepts: drawing^in subset of window

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

212: @*/
213: int PetscDrawViewPortsSet(PetscDrawViewPorts *ports,int port)
214: {
215:   int        ierr;

218:   if (ports) {
219:     if (port < 0 || port > ports->nports-1) {
220:       SETERRQ2(1,"Port is out of range requested %d from 0 to %dn",port,ports->nports);
221:     }
222:     PetscDrawSetViewPort(ports->draw,ports->xl[port],ports->yl[port],ports->xr[port],ports->yr[port]);
223:   }
224:   return(0);
225: }