gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
drawAxes.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 
6 #include <string>
7 #include <iostream>
8 #include "GmshGlobal.h"
9 #include <string.h>
10 #include "drawContext.h"
11 #include "Trackball.h"
12 #include "GModel.h"
13 #include "Context.h"
14 #include "Numeric.h"
15 #include "gl2ps.h"
16 
17 #define SQU(a) ((a) * (a))
18 
19 static int drawTics(drawContext *ctx, int comp, double n, std::string &format,
20  std::string &label, double p1[3], double p2[3],
21  double perp[3], int mikado, double pixelfact,
22  double value_p1[3], double value_p2[3])
23 {
24  // draws n tic marks (in direction perp) and labels along the line p1->p2
25 
26  double t[3] = {p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]};
27  double l = norme(t);
28  double value_t[3] = {value_p2[0] - value_p1[0], value_p2[1] - value_p1[1],
29  value_p2[2] - value_p1[2]};
30  double value_l = norme(value_t);
31  double w = 10 * pixelfact; // tic marks are 10 pixels long
32  double w2 = w * 1.25; // distance to labels
33 
34  // draw label at the end of the axis
35  ctx->drawString(label, p2[0] + t[0] * w2, p2[1] + t[1] * w2,
36  p2[2] + t[2] * w2);
37 
38  // return number of tics in special cases
39  if(n < 2.) return 0;
40  if(format.empty()) return n;
41 
42  // select perp direction automatically if it is not provided
43  double lp = norme(perp);
44  if(!lp) {
45  switch(comp) {
46  case 0: perp[1] = -1.; break;
47  case 1: perp[0] = -1.; break;
48  case 2: perp[0] = 1.; break;
49  default: break;
50  }
51  }
52 
53  // reduce number of tics depending on font size and length of axis on screen
54  drawContext::global()->setFont(CTX::instance()->glFontEnum,
55  CTX::instance()->glFontSize);
56  char tmp[256];
57  sprintf(tmp, format.c_str(), -M_PI * 1.e4);
58  double win1[3], win2[3];
59  ctx->world2Viewport(p1, win1);
60  ctx->world2Viewport(p2, win2);
61  double winl = sqrt(SQU(win2[0] - win1[0]) + SQU(win2[1] - win1[1]));
62  double strl = drawContext::global()->getStringWidth(tmp);
63  if((n - 1) * strl > winl) n = (int)(winl / strl) + 1;
64  if(n <= 1) {
65  if(comp < 0) // ruler
66  n = 2;
67  else
68  return 0;
69  }
70 
71  // draw n tics
72  double step = l / (double)(n - 1);
73  double value_step = value_l / (double)(n - 1);
74 
75  for(int i = 0; i < n; i++) {
76  double d = i * step;
77  double p[3] = {p1[0] + t[0] * d, p1[1] + t[1] * d, p1[2] + t[2] * d};
78  double q[3] = {p[0] + perp[0] * w, p[1] + perp[1] * w, p[2] + perp[2] * w};
79  double r[3] = {p[0] + perp[0] * w2, p[1] + perp[1] * w2,
80  p[2] + perp[2] * w2};
81 
82  double value_d = i * value_step;
83  double value_p[3] = {value_p1[0] + value_t[0] * value_d,
84  value_p1[1] + value_t[1] * value_d,
85  value_p1[2] + value_t[2] * value_d};
86 
87  glBegin(GL_LINES);
88  glVertex3d(p[0], p[1], p[2]);
89  glVertex3d(q[0], q[1], q[2]);
90  glEnd();
91 
92  // draw tic labels
93  if(comp < 0) // display the length value (ruler-mode, starting at 0)
94  sprintf(tmp, format.c_str(), value_d);
95  else // display the coordinate value
96  sprintf(tmp, format.c_str(), value_p[comp]);
97  double winp[3], winr[3];
98  ctx->world2Viewport(p, winp);
99  ctx->world2Viewport(r, winr);
100  if(fabs(winr[0] - winp[0]) < 2.) // center align
101  winr[0] -= drawContext::global()->getStringWidth(tmp) / 2.;
102  else if(winr[0] < winp[0]) // right align
103  winr[0] -= drawContext::global()->getStringWidth(tmp);
104  if(fabs(winr[1] - winp[1]) < 2.) // center align
105  winr[1] -= drawContext::global()->getStringHeight() / 3.;
106  else if(winr[1] < winp[1]) // top align
107  winr[1] -= drawContext::global()->getStringHeight();
108  ctx->viewport2World(winr, r);
109  ctx->drawString(tmp, r[0], r[1], r[2]);
110  }
111 
112  return n;
113 }
114 
115 static void drawGridStipple(int n1, int n2, double p1[3], double p2[3],
116  double p3[3])
117 {
118  double t1[3] = {p2[0] - p1[0], p2[1] - p1[1], p2[2] - p1[2]};
119  double t2[3] = {p3[0] - p1[0], p3[1] - p1[1], p3[2] - p1[2]};
120  double l1 = norme(t1);
121  double l2 = norme(t2);
122 
123  glEnable(GL_LINE_STIPPLE);
124  glLineStipple(1, 0x1111);
126  glBegin(GL_LINES);
127 
128  for(int i = 1; i < n1 - 1; i++) {
129  double d = (double)i / (double)(n1 - 1) * l1;
130  glVertex3d(p1[0] + t1[0] * d, p1[1] + t1[1] * d, p1[2] + t1[2] * d);
131  glVertex3d(p1[0] + t1[0] * d + t2[0] * l2, p1[1] + t1[1] * d + t2[1] * l2,
132  p1[2] + t1[2] * d + t2[2] * l2);
133  }
134  for(int i = 1; i < n2 - 1; i++) {
135  double d = (double)i / (double)(n2 - 1) * l2;
136  glVertex3d(p1[0] + t2[0] * d, p1[1] + t2[1] * d, p1[2] + t2[2] * d);
137  glVertex3d(p1[0] + t2[0] * d + t1[0] * l1, p1[1] + t2[1] * d + t1[1] * l1,
138  p1[2] + t2[2] * d + t1[2] * l1);
139  }
140 
141  glEnd();
142  glDisable(GL_LINE_STIPPLE);
144 }
145 
146 void drawContext::drawAxis(double xmin, double ymin, double zmin, double xmax,
147  double ymax, double zmax, int ntics, int mikado)
148 {
149  if(mikado) {
150  ntics = (ntics - 1) * mikado;
151  if(ntics < 1) ntics = 1;
152  double dd[3] = {(xmax - xmin) / ntics, (ymax - ymin) / ntics,
153  (zmax - zmin) / ntics};
154  double axe_color[4];
155  glGetDoublev(GL_CURRENT_COLOR, axe_color);
156  for(int i = 1; i <= ntics; i++) {
157  if(i % 2)
158  glColor4dv(axe_color);
159  else
160  glColor3f(1, 1, 1);
161  double cx[2] = {xmin + (i - 1) * dd[0], xmin + i * dd[0]};
162  double cy[2] = {ymin + (i - 1) * dd[1], ymin + i * dd[1]};
163  double cz[2] = {zmin + (i - 1) * dd[2], zmin + i * dd[2]};
164  drawCylinder(3.5, cx, cy, cz, 1);
165  }
166  glColor4dv(axe_color);
167  }
168  else {
169  glBegin(GL_LINES);
170  glVertex3d(xmin, ymin, zmin);
171  glVertex3d(xmax, ymax, zmax);
172  glEnd();
173  }
174 }
175 
176 void drawContext::drawAxes(int mode, double tics[3], std::string format[3],
177  std::string label[3], double bb[6], int mikado,
178  double value_bb[6])
179 {
180  // mode 0: nothing
181  // 1: axes
182  // 2: box
183  // 3: full grid
184  // 4: open grid
185  // 5: ruler
186 
187  if((mode < 1) || (bb[0] == bb[1] && bb[2] == bb[3] && bb[4] == bb[5])) return;
188 
189  double xmin = bb[0], xmax = bb[1];
190  double ymin = bb[2], ymax = bb[3];
191  double zmin = bb[4], zmax = bb[5];
192  double orig[3] = {xmin, ymin, zmin};
193 
194  double value_xmin = value_bb[0], value_xmax = value_bb[1];
195  double value_ymin = value_bb[2], value_ymax = value_bb[3];
196  double value_zmin = value_bb[4], value_zmax = value_bb[5];
197  double value_orig[3] = {value_xmin, value_ymin, value_zmin};
198 
199  double pixelfact = pixel_equiv_x / s[0];
200 
201  if(mode == 5) { // draw ruler from xyz_min to xyz_max
202  double end[3] = {xmax, ymax, zmax};
203  double dir[3] = {xmax - xmin, ymax - ymin, zmax - zmin};
204  double perp[3];
205  if((fabs(dir[0]) >= fabs(dir[1]) && fabs(dir[0]) >= fabs(dir[2])) ||
206  (fabs(dir[1]) >= fabs(dir[0]) && fabs(dir[1]) >= fabs(dir[2]))) {
207  perp[0] = dir[1];
208  perp[1] = -dir[0];
209  perp[2] = 0.;
210  }
211  else {
212  perp[0] = 0.;
213  perp[1] = dir[2];
214  perp[2] = -dir[1];
215  }
216  double value_end[3] = {value_xmax, value_ymax, value_zmax};
217  drawTics(this, -1, tics[0], format[0], label[0], orig, end, perp, mikado,
218  pixelfact, value_orig, value_end);
219  drawAxis(xmin, ymin, zmin, xmax, ymax, zmax, tics[0], mikado);
220  return;
221  }
222  double xx[3] = {xmax, ymin, zmin};
223  double yy[3] = {xmin, ymax, zmin};
224  double zz[3] = {xmin, ymin, zmax};
225  double value_xx[3] = {value_xmax, value_ymin, value_zmin};
226  double value_yy[3] = {value_xmin, value_ymax, value_zmin};
227  double value_zz[3] = {value_xmin, value_ymin, value_zmax};
228  double dxm[3] = {0., (ymin != ymax) ? -1. : 0., (zmin != zmax) ? -1. : 0.};
229  double dym[3] = {(xmin != xmax) ? -1. : 0., 0., (zmin != zmax) ? -1. : 0.};
230  double dzm[3] = {(xmin != xmax) ? -1. : 0., (ymin != ymax) ? -1. : 0., 0.};
231 
232  int nx = (xmin != xmax) ?
233  drawTics(this, 0, tics[0], format[0], label[0], orig, xx, dxm,
234  mikado, pixelfact, value_orig, value_xx) :
235  0;
236  int ny = (ymin != ymax) ?
237  drawTics(this, 1, tics[1], format[1], label[1], orig, yy, dym,
238  mikado, pixelfact, value_orig, value_yy) :
239  0;
240  int nz = (zmin != zmax) ?
241  drawTics(this, 2, tics[2], format[2], label[2], orig, zz, dzm,
242  mikado, pixelfact, value_orig, value_zz) :
243  0;
244 
245  drawAxis(xmin, ymin, zmin, xmax, ymin, zmin, nx, mikado);
246  drawAxis(xmin, ymin, zmin, xmin, ymax, zmin, ny, mikado);
247  drawAxis(xmin, ymin, zmin, xmin, ymin, zmax, nz, mikado);
248 
249  // open box
250  if(mode > 1) {
251  drawAxis(xmin, ymax, zmin, xmax, ymax, zmin, nx, mikado);
252  drawAxis(xmax, ymin, zmin, xmax, ymax, zmin, ny, mikado);
253  drawAxis(xmax, ymin, zmin, xmax, ymin, zmax, nz, mikado);
254  drawAxis(xmin, ymin, zmax, xmax, ymin, zmax, nx, mikado);
255  drawAxis(xmin, ymin, zmax, xmin, ymax, zmax, ny, mikado);
256  drawAxis(xmin, ymax, zmin, xmin, ymax, zmax, nz, mikado);
257  }
258 
259  // closed box
260  if(mode == 2 || mode == 3) {
261  drawAxis(xmin, ymax, zmax, xmax, ymax, zmax, nx, mikado);
262  drawAxis(xmax, ymin, zmax, xmax, ymax, zmax, ny, mikado);
263  drawAxis(xmax, ymax, zmin, xmax, ymax, zmax, nz, mikado);
264  }
265 
266  if(mode > 2) {
267  drawGridStipple(nx, ny, orig, xx, yy);
268  drawGridStipple(ny, nz, orig, yy, zz);
269  drawGridStipple(nx, nz, orig, xx, zz);
270  }
271 
272  if(mode == 3) {
273  double orig2[3] = {xmax, ymax, zmax};
274  double xy[3] = {xmax, ymax, zmin};
275  double yz[3] = {xmin, ymax, zmax};
276  double xz[3] = {xmax, ymin, zmax};
277  if(zmin != zmax) drawGridStipple(nx, ny, orig2, yz, xz);
278  if(xmin != xmax) drawGridStipple(ny, nz, orig2, xz, xy);
279  if(ymin != ymax) drawGridStipple(nx, nz, orig2, yz, xy);
280  }
281 }
282 
283 void drawContext::drawAxes(int mode, double tics[3], std::string format[3],
284  std::string label[3], SBoundingBox3d &bb, int mikado,
285  SBoundingBox3d &value_bb)
286 {
287  double bbox[6] = {bb.min().x(), bb.max().x(), bb.min().y(),
288  bb.max().y(), bb.min().z(), bb.max().z()};
289  double value_bbox[6] = {value_bb.min().x(), value_bb.max().x(),
290  value_bb.min().y(), value_bb.max().y(),
291  value_bb.min().z(), value_bb.max().z()};
292  drawAxes(mode, tics, format, label, bbox, mikado, value_bbox);
293 }
294 
296 {
297  bool geometryExists = false;
298  for(std::size_t i = 0; i < GModel::list.size(); i++) {
299  if(!GModel::list[i]->empty()) {
300  geometryExists = true;
301  break;
302  }
303  }
304 
305  if(geometryExists &&
306  (CTX::instance()->drawBBox || !CTX::instance()->mesh.draw)) {
307  glColor4ubv((GLubyte *)&CTX::instance()->color.fg);
308  glLineWidth((float)CTX::instance()->lineWidth);
309  gl2psLineWidth((float)(CTX::instance()->lineWidth *
311  drawBox(CTX::instance()->min[0], CTX::instance()->min[1],
312  CTX::instance()->min[2], CTX::instance()->max[0],
313  CTX::instance()->max[1], CTX::instance()->max[2]);
314  glColor3d(1., 0., 0.);
315  for(int j = 0; j < 6; j++)
316  if(CTX::instance()->geom.clip & (1 << j) ||
317  CTX::instance()->mesh.clip & (1 << j))
319  CTX::instance()->min[0], CTX::instance()->min[1],
320  CTX::instance()->min[2], CTX::instance()->max[0],
321  CTX::instance()->max[1], CTX::instance()->max[2],
322  CTX::instance()->clipPlane[j][0], CTX::instance()->clipPlane[j][1],
323  CTX::instance()->clipPlane[j][2], CTX::instance()->clipPlane[j][3]);
324  }
325 
326  if(CTX::instance()->axes) {
327  glColor4ubv((GLubyte *)&CTX::instance()->color.axes);
328  glLineWidth((float)CTX::instance()->lineWidth);
329  gl2psLineWidth((float)(CTX::instance()->lineWidth *
331  if(!CTX::instance()->axesAutoPosition) {
332  drawAxes(CTX::instance()->axes, CTX::instance()->axesTics,
333  CTX::instance()->axesFormat, CTX::instance()->axesLabel,
334  CTX::instance()->axesPosition, CTX::instance()->axesMikado,
335  CTX::instance()->axesForceValue ? CTX::instance()->axesValue :
336  CTX::instance()->axesPosition);
337  }
338  else {
339  double bb[6] = {CTX::instance()->min[0], CTX::instance()->max[0],
340  CTX::instance()->min[1], CTX::instance()->max[1],
341  CTX::instance()->min[2], CTX::instance()->max[2]};
342  drawAxes(CTX::instance()->axes, CTX::instance()->axesTics,
343  CTX::instance()->axesFormat, CTX::instance()->axesLabel, bb,
344  CTX::instance()->axesMikado,
345  CTX::instance()->axesForceValue ? CTX::instance()->axesValue :
346  bb);
347  }
348  }
349 
350  if(CTX::instance()->drawRotationCenter) {
351  glColor4ubv((GLubyte *)&CTX::instance()->color.fg);
352  if(CTX::instance()->rotationCenterCg)
353  drawSphere(CTX::instance()->pointSize, CTX::instance()->cg[0],
354  CTX::instance()->cg[1], CTX::instance()->cg[2],
355  CTX::instance()->geom.light);
356  else
357  drawSphere(CTX::instance()->pointSize, CTX::instance()->rotationCenter[0],
358  CTX::instance()->rotationCenter[1],
359  CTX::instance()->rotationCenter[2],
360  CTX::instance()->geom.light);
361  }
362 }
363 
365 {
366  double l = CTX::instance()->smallAxesSize;
367  double o = CTX::instance()->glFontSize / 5;
368 
369  double cx = CTX::instance()->smallAxesPos[0];
370  double cy = CTX::instance()->smallAxesPos[1];
371  fix2dCoordinates(&cx, &cy);
372 
373  double xx, xy, yx, yy, zx, zy;
374 
375  if(CTX::instance()->camera) {
376  glMatrixMode(GL_MODELVIEW);
377  glLoadIdentity();
380  camera.up.y, camera.up.z);
381  glPushMatrix();
382  glPopMatrix();
383  float fvViewMatrix[16];
384  glGetFloatv(GL_MODELVIEW_MATRIX, fvViewMatrix);
385  glLoadIdentity();
386  xx = l * fvViewMatrix[0];
387  xy = l * fvViewMatrix[1];
388  yx = l * fvViewMatrix[4];
389  yy = l * fvViewMatrix[5];
390  zx = l * fvViewMatrix[8];
391  zy = l * fvViewMatrix[9];
393 
395  }
396  else {
397  xx = l * rot[0];
398  xy = l * rot[1];
399  yx = l * rot[4];
400  yy = l * rot[5];
401  zx = l * rot[8];
402  zy = l * rot[9];
403  }
404  glLineWidth((float)CTX::instance()->lineWidth);
405  gl2psLineWidth((float)(CTX::instance()->lineWidth *
407  glColor4ubv((GLubyte *)&CTX::instance()->color.smallAxes);
408 
409  glBegin(GL_LINES);
410  glVertex2d(cx, cy);
411  glVertex2d(cx + xx, cy + xy);
412  glVertex2d(cx, cy);
413  glVertex2d(cx + yx, cy + yy);
414  glVertex2d(cx, cy);
415  glVertex2d(cx + zx, cy + zy);
416  glEnd();
417  drawString("X", cx + xx + o, cy + xy + o, 0.);
418  drawString("Y", cx + yx + o, cy + yy + o, 0.);
419  drawString("Z", cx + zx + o, cy + zy + o, 0.);
420 }
drawContext::fix2dCoordinates
int fix2dCoordinates(double *x, double *y)
Definition: drawGraph2d.cpp:15
drawGridStipple
static void drawGridStipple(int n1, int n2, double p1[3], double p2[3], double p3[3])
Definition: drawAxes.cpp:115
gl2ps.h
drawContext::rot
double rot[16]
Definition: drawContext.h:138
drawContext::viewport2World
void viewport2World(double vp[3], double xyz[3])
Definition: drawContext.cpp:861
Camera::up
XYZ up
Definition: Camera.h:53
gl2psEnable
GL2PSDLL_API GLint gl2psEnable(GLint mode)
Definition: gl2ps.cpp:6476
Camera::target
XYZ target
Definition: Camera.h:55
drawContext::drawString
void drawString(const std::string &s, double x, double y, double z, const std::string &font_name, int font_enum, int font_size, int align, int line_num=0)
Definition: drawGlyph.cpp:16
SBoundingBox3d::min
SPoint3 min() const
Definition: SBoundingBox3d.h:90
drawContextGlobal::setFont
virtual void setFont(int fontid, int fontsize)
Definition: drawContext.h:104
CTX::epsLineWidthFactor
double epsLineWidthFactor
Definition: Context.h:341
Camera::position
XYZ position
Definition: Camera.h:51
drawContext::global
static drawContextGlobal * global()
Definition: drawContext.cpp:85
drawContext::s
double s[3]
Definition: drawContext.h:135
drawContext::drawPlaneInBoundingBox
void drawPlaneInBoundingBox(double xmin, double ymin, double zmin, double xmax, double ymax, double zmax, double a, double b, double c, double d, int shade=0)
Definition: drawGlyph.cpp:824
CTX::fg
unsigned int fg
Definition: Context.h:358
CTX::instance
static CTX * instance()
Definition: Context.cpp:122
SQU
#define SQU(a)
Definition: drawAxes.cpp:17
SPoint3::x
double x(void) const
Definition: SPoint3.h:125
CTX::draw
int draw
Definition: Context.h:316
CTX::axes
int axes
Definition: Context.h:248
CTX::smallAxesPos
int smallAxesPos[2]
Definition: Context.h:246
contextMeshOptions::clip
int clip
Definition: Context.h:89
CTX::min
double min[3]
Definition: Context.h:229
Numeric.h
drawContext::drawAxes
void drawAxes()
Definition: drawAxes.cpp:295
CTX::max
double max[3]
Definition: Context.h:229
drawContext::drawAxis
void drawAxis(double xmin, double ymin, double zmin, double xmax, double ymax, double zmax, int nticks, int mikado)
Definition: drawAxes.cpp:146
CTX::smallAxes
int smallAxes
Definition: Context.h:246
drawTics
static int drawTics(drawContext *ctx, int comp, double n, std::string &format, std::string &label, double p1[3], double p2[3], double perp[3], int mikado, double pixelfact, double value_p1[3], double value_p2[3])
Definition: drawAxes.cpp:19
SPoint3::y
double y(void) const
Definition: SPoint3.h:127
drawContext::world2Viewport
void world2Viewport(double xyz[3], double vp[3])
Definition: drawContext.cpp:872
drawContext
Definition: drawContext.h:120
CTX::mesh
contextMeshOptions mesh
Definition: Context.h:313
Trackball.h
GL2PS_LINE_STIPPLE
#define GL2PS_LINE_STIPPLE
Definition: gl2ps.h:149
GModel::list
static std::vector< GModel * > list
Definition: GModel.h:202
drawContext::camera
Camera camera
Definition: drawContext.h:133
drawContext::drawBox
void drawBox(double xmin, double ymin, double zmin, double xmax, double ymax, double zmax, bool labels=true)
Definition: drawGlyph.cpp:787
GmshGlobal.h
drawContextGlobal::getStringHeight
virtual int getStringHeight()
Definition: drawContext.h:106
drawContextGlobal::getStringWidth
virtual double getStringWidth(const char *str)
Definition: drawContext.h:105
XYZ::x
double x
Definition: Camera.h:18
gl2psDisable
GL2PSDLL_API GLint gl2psDisable(GLint mode)
Definition: gl2ps.cpp:6512
Context.h
CTX::glFontSize
int glFontSize
Definition: Context.h:267
XYZ::y
double y
Definition: Camera.h:18
XYZ::z
double z
Definition: Camera.h:18
gl2psLineWidth
GL2PSDLL_API GLint gl2psLineWidth(GLfloat value)
Definition: gl2ps.cpp:6567
CTX::light
int light[6]
Definition: Context.h:274
SPoint3::z
double z(void) const
Definition: SPoint3.h:129
drawContext::drawCylinder
void drawCylinder(double width, double *x, double *y, double *z, int light)
Definition: drawGlyph.cpp:490
drawContext::pixel_equiv_x
double pixel_equiv_x
Definition: drawContext.h:141
GModel.h
norme
double norme(double a[3])
Definition: Numeric.h:123
SBoundingBox3d::max
SPoint3 max() const
Definition: SBoundingBox3d.h:91
SBoundingBox3d
Definition: SBoundingBox3d.h:21
drawContext::drawSphere
void drawSphere(double R, double x, double y, double z, int n1, int n2, int light)
Definition: drawGlyph.cpp:390
drawContext::drawSmallAxes
void drawSmallAxes()
Definition: drawAxes.cpp:364
CTX::smallAxesSize
int smallAxesSize
Definition: Context.h:246
drawContext.h