gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
Annotate.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 <vector>
7 #include "GmshConfig.h"
8 #include "Annotate.h"
9 #include "Context.h"
10 
11 #if defined(HAVE_OPENGL)
12 #include "drawContext.h"
13 #endif
14 
21  {GMSH_FULLRC, "View", nullptr, -1.}};
22 
24  {GMSH_FULLRC, "Text", GMSH_AnnotatePlugin::callbackText, "My Text"},
25  {GMSH_FULLRC, "Font", GMSH_AnnotatePlugin::callbackFont, "Helvetica"},
27 
28 extern "C" {
30 }
31 
32 static double getStyle()
33 {
34  int fontsize = (int)AnnotateOptions_Number[4].def, font = 0, align = 0;
35 #if defined(HAVE_OPENGL)
36  font =
38  align =
40 #endif
41  return (double)((align << 16) | (font << 8) | (fontsize));
42 }
43 
44 void GMSH_AnnotatePlugin::draw(void *context)
45 {
46 #if defined(HAVE_OPENGL)
47  double X = AnnotateOptions_Number[0].def;
48  double Y = AnnotateOptions_Number[1].def;
49  double Z = AnnotateOptions_Number[2].def;
50  double style = getStyle();
51  drawContext *ctx = (drawContext *)context;
52 
53  glColor4ubv((GLubyte *)&CTX::instance()->color.fg);
54  if(AnnotateOptions_Number[3].def) { // 3D
55  ctx->drawString(AnnotateOptions_String[0].def, X, Y, Z, style);
56  // draw 10-pixel marker
57  double d = 10 * ctx->pixel_equiv_x / ctx->s[0];
58  glBegin(GL_LINES);
59  glVertex3d(X - d, Y, Z);
60  glVertex3d(X + d, Y, Z);
61  glVertex3d(X, Y - d, Z);
62  glVertex3d(X, Y + d, Z);
63  glVertex3d(X, Y, Z - d);
64  glVertex3d(X, Y, Z + d);
65  glEnd();
66  }
67  else {
68  double modelview[16], projection[16];
69  glGetDoublev(GL_PROJECTION_MATRIX, projection);
70  glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
71 
72  glMatrixMode(GL_PROJECTION);
73  glLoadIdentity();
74  glOrtho((double)ctx->viewport[0], (double)ctx->viewport[2],
75  (double)ctx->viewport[1], (double)ctx->viewport[3], -1., 1.);
76  glMatrixMode(GL_MODELVIEW);
77  glLoadIdentity();
78  ctx->fix2dCoordinates(&X, &Y);
79  ctx->drawString(AnnotateOptions_String[0].def, X, Y, 0., style);
80  // draw 10-pixel marker
81  glBegin(GL_LINES);
82  glVertex2d(X - 10, Y);
83  glVertex2d(X + 10, Y);
84  glVertex2d(X, Y - 10);
85  glVertex2d(X, Y + 10);
86  glEnd();
87 
88  glMatrixMode(GL_PROJECTION);
89  glLoadMatrixd(projection);
90  glMatrixMode(GL_MODELVIEW);
91  glLoadMatrixd(modelview);
92  }
93 #endif
94 }
95 
96 double GMSH_AnnotatePlugin::callback(int num, int action, double value,
97  double *opt, double step, double min,
98  double max)
99 {
100  switch(action) { // configure the input field
101  case 1: return step;
102  case 2: return min;
103  case 3: return max;
104  default: break;
105  }
106  *opt = value;
108  return 0.;
109 }
110 
111 std::string GMSH_AnnotatePlugin::callbackStr(int num, int action,
112  const std::string &value,
113  std::string &opt)
114 {
115  opt = value;
117  return opt;
118 }
119 
120 double GMSH_AnnotatePlugin::callbackX(int num, int action, double value)
121 {
122  // not perfect: the change will only take place if we reopen the dialog...
123  int dim3 = (int)AnnotateOptions_Number[3].def;
124  return callback(num, action, value, &AnnotateOptions_Number[0].def,
125  dim3 ? CTX::instance()->lc / 200. : 0.5,
126  dim3 ? -CTX::instance()->lc : -100.,
127  dim3 ? CTX::instance()->lc : 100000.);
128 }
129 
130 double GMSH_AnnotatePlugin::callbackY(int num, int action, double value)
131 {
132  // not perfect: the change will only take place if we reopen the dialog...
133  int dim3 = (int)AnnotateOptions_Number[3].def;
134  return callback(num, action, value, &AnnotateOptions_Number[1].def,
135  dim3 ? CTX::instance()->lc / 200. : 0.5,
136  dim3 ? -CTX::instance()->lc : -100.,
137  dim3 ? CTX::instance()->lc : 100000.);
138 }
139 
140 double GMSH_AnnotatePlugin::callbackZ(int num, int action, double value)
141 {
142  // not perfect: the change will only take place if we reopen the dialog...
143  int dim3 = (int)AnnotateOptions_Number[3].def;
144  return callback(num, action, value, &AnnotateOptions_Number[2].def,
145  dim3 ? CTX::instance()->lc / 200. : 0.5,
146  dim3 ? -CTX::instance()->lc : -100.,
147  dim3 ? CTX::instance()->lc : 100000.);
148 }
149 
150 double GMSH_AnnotatePlugin::callback3D(int num, int action, double value)
151 {
152  return callback(num, action, value, &AnnotateOptions_Number[3].def, 1, 0, 1);
153 }
154 
155 double GMSH_AnnotatePlugin::callbackFontSize(int num, int action, double value)
156 {
157  return callback(num, action, value, &AnnotateOptions_Number[4].def, 1, 5,
158  100);
159 }
160 
161 std::string GMSH_AnnotatePlugin::callbackText(int num, int action,
162  const std::string &value)
163 {
164  return callbackStr(num, action, value, AnnotateOptions_String[0].def);
165 }
166 
167 std::string GMSH_AnnotatePlugin::callbackFont(int num, int action,
168  const std::string &value)
169 {
170  return callbackStr(num, action, value, AnnotateOptions_String[1].def);
171 }
172 
173 std::string GMSH_AnnotatePlugin::callbackAlign(int num, int action,
174  const std::string &value)
175 {
176  return callbackStr(num, action, value, AnnotateOptions_String[2].def);
177 }
178 
179 std::string GMSH_AnnotatePlugin::getHelp() const
180 {
181  return "Plugin(Annotate) adds the text string `Text', "
182  "in font `Font' and size `FontSize', in the view "
183  "`View'. The string is aligned according to `Align'.\n\n"
184  "If `ThreeD' is equal to 1, the plugin inserts "
185  "the string in model coordinates at the position "
186  "(`X',`Y',`Z'). If `ThreeD' is equal to 0, the plugin "
187  "inserts the string in screen coordinates at "
188  "the position (`X',`Y').\n\n"
189  "If `View' < 0, the plugin is run on the current view.\n\n"
190  "Plugin(Annotate) is executed in-place for list-based "
191  "datasets or creates a new list-based view for other datasets.";
192 }
193 
195 {
196  return sizeof(AnnotateOptions_Number) / sizeof(StringXNumber);
197 }
198 
200 {
201  return &AnnotateOptions_Number[iopt];
202 }
203 
205 {
206  return sizeof(AnnotateOptions_String) / sizeof(StringXString);
207 }
208 
210 {
211  return &AnnotateOptions_String[iopt];
212 }
213 
215 {
216  double X = AnnotateOptions_Number[0].def;
217  double Y = AnnotateOptions_Number[1].def;
218  double Z = AnnotateOptions_Number[2].def;
219  int dim3 = (int)AnnotateOptions_Number[3].def;
220  int iView = (int)AnnotateOptions_Number[5].def;
221  std::string text = AnnotateOptions_String[0].def;
222  double style = getStyle();
223 
224  PView *v1 = getView(iView, v);
225  if(!v1) return v;
226  PViewData *data1 = v1->getData();
227 
228  PView *v2 = v1;
229  PViewDataList *data2 = getDataList(v2, false);
230  if(!data2) {
231  v2 = new PView();
232  data2 = getDataList(v2);
233  }
234 
235  if(dim3) {
236  data2->T3D.push_back(X);
237  data2->T3D.push_back(Y);
238  data2->T3D.push_back(Z);
239  data2->T3D.push_back(style);
240  data2->T3D.push_back(data2->T3C.size());
241  for(std::size_t i = 0; i < text.size(); i++) data2->T3C.push_back(text[i]);
242  data2->T3C.push_back('\0');
243  data2->NbT3++;
244  }
245  else {
246  data2->T2D.push_back(X);
247  data2->T2D.push_back(Y);
248  data2->T2D.push_back(style);
249  data2->T2D.push_back(data2->T2C.size());
250  for(std::size_t i = 0; i < text.size(); i++) data2->T2C.push_back(text[i]);
251  data2->T2C.push_back('\0');
252  data2->NbT2++;
253  }
254 
255  if(v2 != v1) {
256  for(int i = 0; i < data1->getNumTimeSteps(); i++)
257  data2->Time.push_back(data1->getTime(i));
258  data2->setName(data1->getName() + "_Annotate");
259  data2->setFileName(data1->getName() + "_Annotate.pos");
260  }
261 
262  data2->finalize();
263  return v2;
264 }
drawContext::fix2dCoordinates
int fix2dCoordinates(double *x, double *y)
Definition: drawGraph2d.cpp:15
StringXString
Definition: Options.h:910
Annotate.h
PView
Definition: PView.h:27
drawContextGlobal::getFontAlign
virtual int getFontAlign(const char *alignstr)
Definition: drawContext.h:102
PViewDataList::NbT3
int NbT3
Definition: PViewDataList.h:48
PViewDataList::T2C
std::vector< char > T2C
Definition: PViewDataList.h:50
PViewDataList::T3D
std::vector< double > T3D
Definition: PViewDataList.h:49
GMSH_Plugin
Definition: Plugin.h:26
StringXNumber::def
double def
Definition: Options.h:922
PViewData::getNumTimeSteps
virtual int getNumTimeSteps()=0
GMSH_AnnotatePlugin::callbackX
static double callbackX(int, int, double)
Definition: Annotate.cpp:120
PViewDataList
Definition: PViewDataList.h:17
GMSH_AnnotatePlugin::getOptionStr
StringXString * getOptionStr(int iopt)
Definition: Annotate.cpp:209
GMSH_Plugin::draw
static void(* draw)(void *)
Definition: Plugin.h:77
GMSH_AnnotatePlugin::getOption
StringXNumber * getOption(int iopt)
Definition: Annotate.cpp:199
GMSH_AnnotatePlugin::getNbOptions
int getNbOptions() const
Definition: Annotate.cpp:194
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
AnnotateOptions_String
StringXString AnnotateOptions_String[]
Definition: Annotate.cpp:23
StringXNumber
Definition: Options.h:918
projection
static double projection(SPoint3 &p1, SPoint3 &p2, SPoint3 &q, SPoint3 &result)
Definition: hausdorffDistance.cpp:29
GMSH_AnnotatePlugin::callback
static double callback(int num, int action, double value, double *opt, double step, double min, double max)
Definition: Annotate.cpp:96
PViewData::setFileName
virtual void setFileName(const std::string &val)
Definition: PViewData.h:75
drawContext::global
static drawContextGlobal * global()
Definition: drawContext.cpp:85
drawContext::s
double s[3]
Definition: drawContext.h:135
GMSH_AnnotatePlugin::callbackFont
static std::string callbackFont(int, int, const std::string &)
Definition: Annotate.cpp:167
CTX::fg
unsigned int fg
Definition: Context.h:358
CTX::instance
static CTX * instance()
Definition: Context.cpp:122
getStyle
static double getStyle()
Definition: Annotate.cpp:32
PViewData::getTime
virtual double getTime(int step)
Definition: PViewData.h:94
drawContextGlobal::getFontIndex
virtual int getFontIndex(const char *fontname)
Definition: drawContext.h:99
GMSH_FULLRC
#define GMSH_FULLRC
Definition: Options.h:20
PViewDataList::T3C
std::vector< char > T3C
Definition: PViewDataList.h:50
PView::getData
PViewData * getData(bool useAdaptiveIfAvailable=false)
Definition: PView.cpp:233
drawContext
Definition: drawContext.h:120
PViewDataList::finalize
bool finalize(bool computeMinMax=true, const std::string &interpolationScheme="")
Definition: PViewDataList.cpp:81
PViewData::setName
virtual void setName(const std::string &val)
Definition: PViewData.h:71
StringXString::def
std::string def
Definition: Options.h:914
PViewData
Definition: PViewData.h:29
AnnotateOptions_Number
StringXNumber AnnotateOptions_Number[]
Definition: Annotate.cpp:15
PViewDataList::NbT2
int NbT2
Definition: PViewDataList.h:48
Context.h
PViewDataList::T2D
std::vector< double > T2D
Definition: PViewDataList.h:49
GMSH_AnnotatePlugin::callback3D
static double callback3D(int, int, double)
Definition: Annotate.cpp:150
GMSH_Plugin::setDrawFunction
static void setDrawFunction(void(*fct)(void *))
Definition: Plugin.cpp:21
GMSH_AnnotatePlugin
Definition: Annotate.h:16
drawContext::viewport
int viewport[4]
Definition: drawContext.h:137
GMSH_PostPlugin::getView
virtual PView * getView(int index, PView *view)
Definition: Plugin.cpp:81
PViewDataList::Time
std::vector< double > Time
Definition: PViewDataList.h:25
GMSH_AnnotatePlugin::getNbOptionsStr
int getNbOptionsStr() const
Definition: Annotate.cpp:204
GMSH_AnnotatePlugin::callbackText
static std::string callbackText(int, int, const std::string &)
Definition: Annotate.cpp:161
GMSH_AnnotatePlugin::execute
PView * execute(PView *)
Definition: Annotate.cpp:214
GMSH_AnnotatePlugin::callbackY
static double callbackY(int, int, double)
Definition: Annotate.cpp:130
GMSH_AnnotatePlugin::callbackFontSize
static double callbackFontSize(int, int, double)
Definition: Annotate.cpp:155
drawContext::pixel_equiv_x
double pixel_equiv_x
Definition: drawContext.h:141
GMSH_AnnotatePlugin::callbackStr
static std::string callbackStr(int num, int action, const std::string &value, std::string &opt)
Definition: Annotate.cpp:111
PViewData::getName
virtual std::string getName()
Definition: PViewData.h:70
GMSH_AnnotatePlugin::getHelp
std::string getHelp() const
Definition: Annotate.cpp:179
GMSH_AnnotatePlugin::callbackAlign
static std::string callbackAlign(int, int, const std::string &)
Definition: Annotate.cpp:173
GMSH_AnnotatePlugin::callbackZ
static double callbackZ(int, int, double)
Definition: Annotate.cpp:140
GMSH_PostPlugin::getDataList
virtual PViewDataList * getDataList(PView *view, bool showError=true)
Definition: Plugin.cpp:107
GMSH_RegisterAnnotatePlugin
GMSH_Plugin * GMSH_RegisterAnnotatePlugin()
Definition: Annotate.cpp:29
drawContext.h