NEURON
rotate3d.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 #if HAVE_IV // to end of file
3 
4 #include <math.h>
5 #include <InterViews/canvas.h>
6 #include <IV-look/kit.h>
7 #include <InterViews/font.h>
8 #include <InterViews/polyglyph.h>
9 #include "rot3band.h"
10 #include <stdio.h>
11 #include "nrnoc2iv.h"
12 #include "shape.h"
13 #include "ivoc.h"
14 
15 #define Rotate_ "Rotate3D PlotShape"
16 
18  identity();
19  int i, j;
20  for (i = 0; i < 2; ++i)
21  for (j = 0; j < 3; ++j)
22  o_[i][j] = 0.;
23 }
24 
26 
27 void Rotation3d::identity() {
28  int i, j;
29  for (i = 0; i < 3; ++i) {
30  for (j = 0; j < 3; ++j) {
31  a_[i][j] = 0.;
32  }
33  a_[i][i] = 1.;
34  }
35 }
36 
37 void Rotation3d::rotate_x(float radian) {
38  Rotation3d m;
39  m.a_[1][1] = m.a_[2][2] = cos(radian);
40  m.a_[2][1] = -(m.a_[1][2] = sin(radian));
41  post_multiply(m);
42 }
43 void Rotation3d::rotate_y(float radian) {
44  Rotation3d m;
45  m.a_[2][2] = m.a_[0][0] = cos(radian);
46  m.a_[2][0] = -(m.a_[0][2] = sin(radian));
47  post_multiply(m);
48 }
49 void Rotation3d::rotate_z(float radian) {
50  Rotation3d m;
51  m.a_[0][0] = m.a_[1][1] = cos(radian);
52  m.a_[1][0] = -(m.a_[0][1] = sin(radian));
53  post_multiply(m);
54 }
55 
56 void Rotation3d::post_multiply(Rotation3d& m) { // r = m*r
57  float x[3][3];
58  int i, j, k;
59  for (i = 0; i < 3; ++i) {
60  for (j = 0; j < 3; ++j) {
61  x[i][j] = 0;
62  for (k = 0; k < 3; ++k) {
63  x[i][j] += m.a_[i][k] * a_[k][j];
64  }
65  }
66  }
67  for (i = 0; i < 3; ++i) {
68  for (j = 0; j < 3; ++j) {
69  a_[i][j] = x[i][j];
70  }
71  }
72 }
73 
74 void Rotation3d::rotate(float x, float y, float z, float* tr) const {
75  float r[3];
76  r[0] = x;
77  r[1] = y;
78  r[2] = z;
79  rotate(r, tr);
80 }
81 
82 void Rotation3d::rotate(float* r, float* tr) const {
83  int i;
84  float x[3];
85  for (i = 0; i < 3; ++i) {
86  x[i] = r[i] - o_[0][i];
87  }
88  for (i = 0; i < 3; ++i) {
89  tr[i] = a_[i][0] * x[0] + a_[i][1] * x[1] + a_[i][2] * x[2] + o_[1][i];
90  }
91 }
92 
93 void Rotation3d::origin(float x, float y, float z) {
94  o_[0][0] = x;
95  o_[0][1] = y;
96  o_[0][2] = z;
97 }
98 void Rotation3d::offset(float x, float y) {
99  o_[1][0] = x;
100  o_[1][1] = y;
101  o_[1][2] = 0.;
102 }
103 
104 void Rotation3d::x_axis(float& x, float& y) const {
105  x = a_[0][0];
106  y = a_[1][0];
107 }
108 void Rotation3d::y_axis(float& x, float& y) const {
109  x = a_[0][1];
110  y = a_[1][1];
111 }
112 void Rotation3d::z_axis(float& x, float& y) const {
113  x = a_[0][2];
114  y = a_[1][2];
115 }
116 void Rotation3d::inverse_rotate(float* tr, float* r) const {
117  int i;
118  float x[3];
119  for (i = 0; i < 3; ++i) {
120  x[i] = tr[i];
121  }
122  for (i = 0; i < 3; ++i) {
123  r[i] = a_[0][i] * x[0] + a_[1][i] * x[1] + a_[2][i] * x[2];
124  }
125 }
126 
128  : Rubberband(ra, c) {
129  if (r3) {
130  rot_ = r3;
131  } else {
132  rot_ = new Rotation3d();
133  }
134  Resource::ref(rot_);
135 }
136 
139 }
140 
142  return rot_;
143 }
144 
145 bool Rotate3Band::event(Event& e) {
146  const float deg = 3.14159265358979323846 / 18.;
147  if (e.type() == Event::key) {
148  undraw(x(), y());
149  char buf[2];
150  if (e.mapkey(buf, 1) > 0)
151  switch (buf[0]) {
152  case 'x':
153  rot_->identity();
154  rot_->rotate_y(3.14159265358979323846 / 2.);
155  break;
156  case 'y':
157  case 'a':
158  rot_->identity();
159  rot_->rotate_x(3.14159265358979323846 / 2.);
160  break;
161  case 'z':
162  case ' ':
163  rot_->identity();
164  break;
165  case 'X':
166  rot_->rotate_x(deg);
167  break;
168  case 'Y':
169  case 'A':
170  rot_->rotate_y(deg);
171  break;
172  case 'Z':
173  rot_->rotate_z(deg);
174  break;
175  case 037 & 'x':
176  rot_->rotate_x(-deg);
177  break;
178  case 037 & 'y':
179  case 037 & 'a':
180  rot_->rotate_y(-deg);
181  break;
182  case 037 & 'z':
183  rot_->rotate_z(-deg);
184  break;
185  default:
186  break;
187  }
188  draw(x(), y());
189  return true;
190  } else {
191  return Rubberband::event(e);
192  }
193 }
194 
195 void Rotate3Band::help() {
196  Oc::help(Rotate_);
197 }
198 
199 void Rotate3Band::press(Event& e) {
200  Canvas* c = canvas();
201  c->push_transform();
202  Transformer t;
203  c->transformer(transformer());
205  c->fill_rect(v->left(), v->bottom(), v->right(), v->top(), Scene::default_background());
206  c->pop_transform();
207 
208  x_old_ = x();
209  y_old_ = y();
210  ShapeScene* ss = (ShapeScene*) v->scene();
211  Coord x1, y1;
212  transformer().inverse_transform(x(), y(), x1, y1);
213  ss->nearest(x1, y1);
214  ShapeSection* ssec = ss->selected();
215  Section* s = ssec->section();
216  // this is the point we want to stay fixed.
217  int i = ssec->get_coord(ss->arc_selected(), x1, y1);
218  // what is it now
219  float r[3];
220  rot_->rotate(s->pt3d[i].x, s->pt3d[i].y, s->pt3d[i].z, r);
221  rot_->origin(s->pt3d[i].x, s->pt3d[i].y, s->pt3d[i].z);
222  rot_->offset(r[0], r[1]);
223 }
224 
225 void Rotate3Band::drag(Event&) {
226  float dx = x() - x_old_;
227  float dy = y() - y_old_;
228  // printf ("dx=%g dy=%g\n", dx,dy);
229  rot_->rotate_x(dy / 50);
230  rot_->rotate_y(dx / 50);
231  x_old_ = x();
232  y_old_ = y();
233 }
234 
236  Canvas* c = canvas();
237  const Font* f = WidgetKit::instance()->font();
238  float x, y;
239  float x0, y0;
240 
241  c->push_transform();
242  c->transformer(transformer());
243  PolyGlyph* sg = ((ShapeScene*) XYView::current_pick_view()->scene())->shape_section_list();
244  GlyphIndex cnt = sg->count();
245  for (GlyphIndex i = 0; i < cnt; ++i) {
246  Section* sec = ((ShapeSection*) sg->component(i))->section();
247  if (sec->npt3d) {
248  float r[3];
249  int i = 0;
250  r[0] = sec->pt3d[i].x;
251  r[1] = sec->pt3d[i].y;
252  r[2] = sec->pt3d[i].z;
253  rot_->rotate(r, r);
254  c->move_to(r[0], r[1]);
255  i = sec->npt3d - 1;
256  r[0] = sec->pt3d[i].x;
257  r[1] = sec->pt3d[i].y;
258  r[2] = sec->pt3d[i].z;
259  rot_->rotate(r, r);
260  c->line_to(r[0], r[1]);
261  c->stroke(color(), brush());
262  }
263  }
264  c->pop_transform();
265 
266  x0 = x_begin();
267  y0 = y_begin();
268  c->push_transform();
269  Transformer t;
270  c->transformer(t);
271  c->new_path();
272  Coord w = canvas()->width() / 4;
273  rot_->x_axis(x, y);
274  // printf("x_axis %g %g\n", x, y);
275  c->line(x0, y0, x0 + x * w, y0 + y * w, color(), brush());
276 #ifndef WIN32
277  c->character(f, 'x', f->width('x'), color(), x0 + x * w * 1.1, y0 + y * w * 1.1);
278 #endif
279  rot_->y_axis(x, y);
280  // printf("y_axis %g %g\n", x, y);
281  c->line(x0, y0, x0 + x * w, y0 + y * w, color(), brush());
282 #ifndef WIN32
283  c->character(f, 'y', f->width('y'), color(), x0 + x * w * 1.1, y0 + y * w * 1.1);
284 #endif
285  rot_->z_axis(x, y);
286  // printf("z_axis %g %g\n", x, y);
287  c->line(x0, y0, x0 + x * w, y0 + y * w, color(), brush());
288 #ifndef WIN32
289  c->character(f, 'z', f->width('z'), color(), x0 + x * w * 1.1, y0 + y * w * 1.1);
290 #endif
291  c->pop_transform();
292 }
293 #endif
#define Transformer
Definition: _defines.h:313
#define Canvas
Definition: _defines.h:63
#define Coord
Definition: _defines.h:17
#define GlyphIndex
Definition: _defines.h:21
#define Event
Definition: _defines.h:105
#define PolyGlyph
Definition: _defines.h:205
#define Font
Definition: _defines.h:118
static void help(const char *)
virtual void ref() const
Definition: resource.cpp:42
virtual void unref() const
Definition: resource.cpp:47
virtual void press(Event &)
float y_old_
Definition: rot3band.h:35
float x_old_
Definition: rot3band.h:35
virtual ~Rotate3Band()
Rotation3d * rot_
Definition: rot3band.h:34
Rotate3Band(Rotation3d *=NULL, RubberAction *=NULL, Canvas *=NULL)
virtual void draw(Coord, Coord)
virtual void drag(Event &)
Rotation3d * rotation()
virtual void help()
float o_[2][3]
Definition: rotate3d.h:29
float a_[3][3]
Definition: rotate3d.h:28
void offset(float x, float y)
void inverse_rotate(float *tr, float *r) const
void z_axis(float &x, float &y) const
void origin(float x, float y, float z)
void y_axis(float &x, float &y) const
void identity()
void rotate(float x, float y, float z, float *tr) const
virtual ~Rotation3d()
void rotate_x(float radians)
void rotate_z(float radians)
void post_multiply(Rotation3d &)
void x_axis(float &x, float &y) const
void rotate_y(float radians)
Coord x() const
Definition: rubband.h:107
const Event & event() const
Definition: rubband.h:51
virtual void undraw(Coord x, Coord y)
Coord y_begin() const
Definition: rubband.h:116
static const Color * color()
Coord y() const
Definition: rubband.h:110
const Transformer & transformer() const
Definition: rubband.h:48
Coord x_begin() const
Definition: rubband.h:113
Canvas * canvas() const
Definition: rubband.h:45
static const Brush * brush()
static const Color * default_background()
virtual float arc_selected()
virtual float nearest(Coord, Coord)
virtual ShapeSection * selected()
int get_coord(double arc, Coord &, Coord &) const
Section * section() const
virtual Scene * scene() const
static XYView * current_pick_view()
#define cnt
Definition: tqueue.hpp:44
#define key
Definition: tqueue.hpp:45
#define v
Definition: md1redef.h:11
#define sec
Definition: md1redef.h:20
#define i
Definition: md1redef.h:19
static RNG::key_type k
Definition: nrnran123.cpp:9
char buf[512]
Definition: init.cpp:13
static int c
Definition: hoc.cpp:169
sin
Definition: extdef.h:3
cos
Definition: extdef.h:3
size_t j
s
Definition: multisend.cpp:521