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