SeExpr
ExprGrapher2d.cpp
Go to the documentation of this file.
1 /*
2 * Copyright Disney Enterprises, Inc. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License
6 * and the following modification to it: Section 6 Trademarks.
7 * deleted and replaced with:
8 *
9 * 6. Trademarks. This License does not grant permission to use the
10 * trade names, trademarks, service marks, or product names of the
11 * Licensor and its affiliates, except as required for reproducing
12 * the content of the NOTICE file.
13 *
14 * You may obtain a copy of the License at
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * @file ExprGrapher2d.cpp
18 * @brief A 2d image graph view for expression editing previewing
19 * @author jlacewel
20 */
21 
22 #include "ExprGrapher2d.h"
23 #include <QtGui/QGridLayout>
24 #include <QtGui/QLineEdit>
25 #include <QtGui/QDoubleValidator>
26 #include <QtGui/QHBoxLayout>
27 #include <QtGui/QLabel>
28 
29 
30 ExprGrapherWidget::ExprGrapherWidget(QWidget* parent,int width,int height)
31  :view(new ExprGrapherView(*this,this,width,height)),
32  expr("",SeExpr2::ExprType().FP(1))
33 {
34  Q_UNUSED(parent);
35  setFixedSize(width, height+30);
36  QVBoxLayout* vbox=new QVBoxLayout;
37  vbox->setMargin(0);
38  setLayout(vbox);
39  vbox->addWidget(view,0,Qt::AlignLeft | Qt::AlignTop);
40  QHBoxLayout* hbox=new QHBoxLayout;
41  vbox->addLayout(hbox);
42  hbox->setMargin(0);
43 
44  float xmin,xmax,ymin,ymax,z;
45  view->getWindow(xmin,xmax,ymin,ymax,z);
46  scale=new QLineEdit();
47  QDoubleValidator *valValidator = new QDoubleValidator(0.0,10000000.0,6,scale);
48  scale->setValidator(valValidator);
49  scale->setValidator(valValidator);
51 
52 
53  connect(scale, SIGNAL(returnPressed()), this, SLOT(scaleValueEdited()));
54  connect(view, SIGNAL(scaleValueManipulated()), this, SLOT(scaleValueManipulated()));
55  connect(view, SIGNAL(clicked()), this, SLOT(forwardPreview()));
56 
57  hbox->addWidget(new QLabel("Width"),0);
58  hbox->addWidget(scale,0);
59 }
60 
62 {
63  float xmin,xmax,ymin,ymax,z;
64  view->getWindow(xmin,xmax,ymin,ymax,z);
65  float xdiff=xmax-xmin,ydiff=ymax-ymin;
66  float xcenter=.5*(xmax+xmin),ycenter=.5*(ymin+ymax);
67  float newScale=atof(scale->text().toStdString().c_str());
68 
69  float aspect=ydiff/xdiff;
70 
71  xmin=xcenter-newScale;
72  xmax=xcenter+newScale;
73  ymin=ycenter-aspect*newScale;
74  ymax=ycenter+aspect*newScale;
75  view->setWindow(xmin,xmax,ymin,ymax,z);
76 }
77 
79 {
80  float xmin,xmax,ymin,ymax,z;
81  view->getWindow(xmin,xmax,ymin,ymax,z);
82  scale->setText(QString("%1").arg(.5*(xmax-xmin)));
83 }
84 
86 {
88 
89  view->update();
90 }
91 
93 {
94  emit preview();
95 }
96 
97 
98 ExprGrapherView::ExprGrapherView(ExprGrapherWidget& widget,QWidget* parent, int width, int height)
99  : QGLWidget(parent), widget(widget), _image(NULL), _width(width), _height(height),
100  scaling(false),translating(false)
101 {
102  this->setFixedSize(width, height);
103 
104  _image = new float[3*_width*_height];
105  setWindow(-1,1,-1,1,0);
106  clear();
107 
108  setCursor(Qt::OpenHandCursor);
109 
110 }
111 
113 {
114  delete [] _image;
115 }
116 
117 void ExprGrapherView::setWindow(float xmin,float xmax,float ymin,float ymax,float z)
118 {
119  this->z=z;
120  this->xmin=xmin;
121  this->xmax=xmax;
122  this->ymin=ymin;
123  this->ymax=ymax;
124 
125  dx=(xmax-xmin)/_width;
126  dy=(ymax-ymin)/_height;
127 }
128 
129 void ExprGrapherView::getWindow(float& xmin,float& xmax,float& ymin,float& ymax,float& z)
130 {
131  z=this->z;
132  xmin=this->xmin;
133  xmax=this->xmax;
134  ymin=this->ymin;
135  ymax=this->ymax;
136 }
137 
139 {
140  for (int row = 0; row < _height; ++row) {
141  for (int col = 0; col < _width; ++col) {
142  int index = 3*row*_width + 3*col;
143  _image[index] = 1.0f;
144  _image[index+1] = 0.0f;
145  _image[index+2] = 0.0f;
146  }
147  }
148 }
149 
150 void ExprGrapherView::mousePressEvent(QMouseEvent* event)
151 {
152  if(event->button()==Qt::MidButton){
153  setCursor(Qt::ClosedHandCursor);
154  translating=true;
155  }
156  if(event->button()==Qt::RightButton){
157  setCursor(Qt::SizeAllCursor);
158  scaling=true;
159  }
160  event_oldx=event->x();
161  event_oldy=event->y();
162 
163 }
164 void ExprGrapherView::mouseReleaseEvent(QMouseEvent* event)
165 {
166  if(event->button()==Qt::LeftButton)
167  emit clicked();
168  scaling=translating=false;
169  setCursor(Qt::OpenHandCursor);
170 }
171 void ExprGrapherView::mouseMoveEvent(QMouseEvent* event)
172 {
173  int x=event->x(),y=event->y();
174  float offsetx=dx*(x-event_oldx);
175  float offsety=-dy*(y-event_oldy);
176 
177  if(translating){
178  xmin-=offsetx;
179  xmax-=offsetx;
180  ymin-=offsety;
181  ymax-=offsety;
182  update();
183  repaint();
184  }else if(scaling){
185  float offset=(fabs(offsetx)>fabs(offsety))?offsetx:offsety;
186 
187  float width=.5*(xmax-xmin),height=.5*(ymax-ymin);
188  float xcenter=.5*(xmin+xmax),ycenter=.5*(ymin+ymax);
189  // Use float args for pow() to fix Windows compile error
190  float scale_factor=pow(10.f,-offset/(xmax-xmin));
191  width*=scale_factor;
192  height*=scale_factor;
193  setWindow(xcenter-width,xcenter+width,ycenter-height,ycenter+height,z);
194  emit scaleValueManipulated();
195  update();
196  repaint();
197  }
198  event_oldx=x;
199  event_oldy=y;
200 }
201 
202 
204 {
205 
206  if (!widget.expr.isValid()) {
207  clear();
208  updateGL();
209  return;
210  }
211 
212 
213  float dv = 1.0f / _height;
214  float du = 1.0f / _width;
215 
216  float y=.5*dy+ymin;
217  float v=.5*dv;
218  int index=0;
219  for(int row=0;row<_height;row++,y+=dy,v+=dv){
220  float x=.5*dx+xmin;
221  float u=.5*du;
222  widget.expr.v.value=v;
223  for(int col=0;col<_width;col++,x+=dy,u+=du){
224  widget.expr.u.value=u;
226  const double* value=widget.expr.evalFP();
227  _image[index] = value[0];
228  _image[index+1] = value[1];
229  _image[index+2] = value[2];
230  index+=3;
231  }
232  }
233 
234  updateGL();
235 }
236 
238 {
239  glMatrixMode(GL_PROJECTION);
240  glLoadIdentity();
241  glOrtho(0.0f, (GLfloat)_width, 0.0, (GLfloat)_height, -1.0, 1.0);
242  glMatrixMode(GL_MODELVIEW);
243  glLoadIdentity();
244 
245  glDisable(GL_DEPTH_TEST);
246  glDepthFunc(0);
247  glClearColor(1,0,0,1);
248  glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
249 
250  glRasterPos2i(0,0);
251  glDrawPixels(_width, _height, GL_RGB, GL_FLOAT, _image);
252 
253 }
254 
BasicExpression expr
Definition: ExprGrapher2d.h:78
void mouseMoveEvent(QMouseEvent *event)
</pre >< h2 > Evaluating expressions</h2 > Evaluating an expression is pretty easy But before we can do that we need to make an instance< pre > GrapherExpr expr("x+x^2")
void mouseReleaseEvent(QMouseEvent *event)
void setDesiredReturnType(const ExprType &type)
Definition: Expression.cpp:154
ExprGrapherWidget & widget
Definition: ExprGrapher2d.h:36
void setWindow(float xmin, float xmax, float ymin, float ymax, float z)
ExprGrapherWidget(QWidget *parent, int width, int height)
ExprGrapherView(ExprGrapherWidget &widget, QWidget *parent, int width, int height)
with numParticles numAttributes A variable block contains variable names and types but doesn t care what the values are< pre > void f(const std::string &s, MyParticleData *p, int outputDim=3)
Definition: varblocks.txt:35
QLineEdit * scale
Definition: ExprGrapher2d.h:74
For any rgb or hsl value(except for negative s values)
void getWindow(float &xmin, float &xmax, float &ymin, float &ymax, float &z)
The result is computed int int< br >< divstyle="margin-left:40px;"> Picks values randomly between loRange and hiRange based on supplied index(which is automatically hashed).&nbsp
Vec< double, 3, false > Vec3d
Definition: Vec.h:368
< br > pow($a, 0.5)+$b< br >< br ></div > External variables can also be overridden by local assignment.&nbsp
ExprGrapherView * view
Definition: ExprGrapher2d.h:77
</pre >< h3 > A simple variable reference</h3 > This is not a very interesting subclass of expression until we add some additional variables Variables on some applications may be very dynamic In this we only need x
Definition: tutorial.txt:108
void mousePressEvent(QMouseEvent *event)
void scaleValueManipulated()
const double * evalFP(VarBlock *varBlock=nullptr) const
Definition: Expression.cpp:299
bool isValid() const
Definition: Expression.h:133
This is the same as the prman cellnoise function< br ></div >< br > float< b > float y< br > float< b > float float z
Definition: userdoc.txt:218
virtual ~ExprGrapherView()
void scaleValueManipulated()
This is the same as the prman cellnoise function< br ></div >< br > float< b > float y< br > float< b > float y
Definition: userdoc.txt:218