g4tools  5.4.0
ellipse
Go to the documentation of this file.
1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
2 // See the file tools.license for terms.
3 
4 #ifndef tools_sg_ellipse
5 #define tools_sg_ellipse
6 
7 // same logic as ROOT/TEllipse.
8 
9 #include "node"
10 #include "sf"
11 #include "render_action"
12 #include "pick_action"
13 #include "bbox_action"
14 
15 #include "../mathf"
16 #include "../curve"
17 
18 namespace tools {
19 namespace sg {
20 
21 class ellipse : public node,public curve {
23 public:
24  virtual void* cast(const std::string& a_class) const {
25  if(void* p = cmp_cast<ellipse>(this,a_class)) return p;
26  if(void* p = cmp_cast<curve>(this,a_class)) return p;
27  return node::cast(a_class);
28  }
29 public:
32  sf<float> phi_min; //radians
33  sf<float> phi_max; //radians
35 public:
36  virtual const desc_fields& node_desc_fields() const {
38  static const desc_fields s_v(parent::node_desc_fields(),5, //WARNING : take care of count.
44  );
45  return s_v;
46  }
47 private:
48  void add_fields(){
49  add_field(&rx);
50  add_field(&ry);
53  add_field(&steps);
54  }
55 public: //curve
56  virtual bool pos_tan_nor(float /*a_s*/,
57  vec3f& a_pos,
58  vec3f& a_tan,
59  vec3f& a_nor) const {
60 
61  //float r = radius.value();
62  //float cs = fcos(a_s);
63  //float sn = fsin(a_s);
64 
65  float x,y,z;
66 
67  {//x = r*cs;y = r*sn;z = 0;
68  x = 0;y = 0;z = 0;
69  m_model.mul_3f(x,y,z);
70  a_pos.set_value(x,y,z);}
71 
72  {//x = -sn;y = cs;z = 0;
73  x = 0;y = 1;z = 0;
74  m_model.mul_dir_3(x,y,z);
75  a_tan.set_value(x,y,z);}
76 
77  {x = 0;y = 0;z = 1;
78  m_model.mul_dir_3(x,y,z);
79  a_nor.set_value(x,y,z);}
80 
81  return true;
82  }
83 public:
84  virtual void copy(curve*& a_new) const {a_new = new ellipse(*this);}
85 public:
86  virtual void render(render_action& a_action) {
87  if(touched()) {
88  update_sg();
89  reset_touched();
90  }
91  //Same logic as Inventor SoLightModel.model = BASE_COLOR.
92  const state& state = a_action.state();
93  a_action.set_lighting(false);
94  a_action.add_line_strip(m_xyzs);
96  }
97 
98  virtual void pick(pick_action& a_action) {
99  if(touched()) {
100  update_sg();
101  reset_touched();
102  }
103  if(a_action.stop_at_first()){
104  a_action.add_line_strip(m_xyzs);
105  if(a_action.done()) a_action.set_node(this);
106  } else {
107  a_action.set_done(false);
108  a_action.zs().clear();
109  a_action.ws().clear();
110  a_action.add_line_strip(m_xyzs);
111  if(a_action.done()) {
112  a_action.add_pick(*this,a_action.zs(),a_action.ws(),a_action.state());
113  a_action.set_done(false);
114  }
115  }
116  }
117  virtual void bbox(bbox_action& a_action) {
118  if(touched()) {
119  update_sg();
120  reset_touched();
121  }
122  a_action.add_line_strip(m_xyzs);
123  }
124 public:
126  :parent()
127  ,curve()
128  ,rx(1)
129  ,ry(1)
130  ,phi_min(0)
131  ,phi_max(tools::ftwo_pi())
132  ,steps(40)
133  {
134  add_fields();
135  }
136  virtual ~ellipse(){}
137 public:
138  ellipse(const ellipse& a_from)
139  :parent(a_from)
140  ,curve(a_from)
141  ,rx(a_from.rx)
142  ,ry(a_from.ry)
143  ,phi_min(a_from.phi_min)
144  ,phi_max(a_from.phi_max)
145  ,steps(a_from.steps)
146  {
147  add_fields();
148  }
149  ellipse& operator=(const ellipse& a_from){
150  parent::operator=(a_from);
151  curve::operator=(a_from);
152  rx = a_from.rx;
153  ry = a_from.ry;
154  phi_min = a_from.phi_min;
155  phi_max = a_from.phi_max;
156  steps = a_from.steps;
157  return *this;
158  }
159 protected:
160  void update_sg() {
161  m_xyzs.clear();
162  if(!steps.value()) return;
163 
164  unsigned int num = steps.value();
165 
166  //set number of points approximatively proportional to the ellipse circumference
167  //float circ = kPI*(r1+r2)*(phi2-phi1)/360;
168  //Int_t n = (Int_t)(np*circ/((gPad->GetX2()-gPad->GetX1())+(gPad->GetY2()-gPad->GetY1())));
169  //if (n < 8) n= 8;
170  //if (n > np) n = np;
171 
172  m_xyzs.resize((num+1)*3);
173 
174  float phimin = phi_min.value();
175  float phimax = phi_max.value();
176  float r1 = rx.value();
177  float r2 = ry.value();
178 
179  float phi1 = mn<float>(phimin,phimax);
180  float phi2 = mx<float>(phimin,phimax);
181 
182  float angle,dx,dy;
183  float dphi = (phi2-phi1)/float(num);
184  size_t pos = 0;
185  for(unsigned int i=0;i<=num;i++) {
186  angle = phi1 + float(i)*dphi;
187  dx = r1*fcos(angle);
188  dy = r2*fsin(angle);
189  m_xyzs[pos] = dx;pos++;
190  m_xyzs[pos] = dy;pos++;
191  m_xyzs[pos] = 0;pos++;
192  }
193  /*
194  TString opt = option;
195  opt.ToLower();
196  if (phi2-phi1 >= 360 ) {
197  if (GetFillStyle()) gPad->PaintFillArea(n,x,y);
198  if (GetLineStyle()) gPad->PaintPolyLine(n+1,x,y);
199  } else {
200  x[n+1] = gPad->XtoPad(x1);
201  y[n+1] = gPad->YtoPad(y1);
202  x[n+2] = x[0];
203  y[n+2] = y[0];
204  if (GetFillStyle()) gPad->PaintFillArea(n+2,x,y);
205  if (GetLineStyle()) {
206  if (TestBit(kNoEdges) || opt.Contains("only")) gPad->PaintPolyLine(n+1,x,y);
207  else gPad->PaintPolyLine(n+3,x,y);
208  }
209  }
210  */
211 
212  }
213 
214 protected:
215  std::vector<float> m_xyzs;
216 };
217 
218 }}
219 
220 #endif
tools::sg::pick_action::zs
const std::vector< float > & zs() const
Definition: pick_action:320
node
tools::sg::ellipse::node_desc_fields
virtual const desc_fields & node_desc_fields() const
Definition: ellipse:36
tools::sg::ellipse::render
virtual void render(render_action &a_action)
Definition: ellipse:86
tools::sg::state
Definition: state:25
tools::sg::render_action::set_lighting
virtual void set_lighting(bool)=0
tools::sg::state::m_GL_LIGHTING
bool m_GL_LIGHTING
Definition: state:248
tools::mat4f::mul_3f
void mul_3f(float &a_x, float &a_y, float &a_z) const
Definition: mat4f:58
tools::sg::ellipse::cast
virtual void * cast(const std::string &a_class) const
Definition: ellipse:24
pick_action
tools::sg::ellipse::pos_tan_nor
virtual bool pos_tan_nor(float, vec3f &a_pos, vec3f &a_tan, vec3f &a_nor) const
Definition: ellipse:56
tools::vec3::set_value
void set_value(const T &a0, const T &a1, const T &a2)
Definition: vec3:92
TOOLS_NODE_NO_CAST
#define TOOLS_NODE_NO_CAST(a__class, a__sclass, a__parent)
Definition: node:328
render_action
tools::sg::pick_action::set_node
void set_node(sg::node *a_node)
Definition: pick_action:257
tools::sg::node
Definition: node:28
tools::sg::bbox_action
Definition: bbox_action:15
tools::sg::pick_action::add_pick
void add_pick(sg::node &a_node, const std::vector< float > &a_zs, const std::vector< float > &a_ws, const sg::state &a_state)
Definition: pick_action:265
tools::sg::ellipse
Definition: ellipse:21
tools::sg::ellipse::phi_min
sf< float > phi_min
Definition: ellipse:32
tools::sg::ellipse::ry
sf< float > ry
Definition: ellipse:31
tools::sg::ellipse::copy
virtual void copy(curve *&a_new) const
Definition: ellipse:84
tools::fcos
float fcos(const float &x)
Definition: mathf:42
tools::sg::ellipse::ellipse
ellipse(const ellipse &a_from)
Definition: ellipse:138
tools::sg::render_action::add_line_strip
bool add_line_strip(size_t a_floatn, const float *a_xyzs)
Definition: render_action:184
tools::sg::pick_action::stop_at_first
bool stop_at_first() const
Definition: pick_action:252
tools::sg::desc_fields
Definition: field_desc:148
tools::sg::pick_action::set_done
void set_done(bool a_value)
Definition: pick_action:254
tools::sg::ellipse::phi_max
sf< float > phi_max
Definition: ellipse:33
tools::sg::pick_action
Definition: pick_action:59
tools::sg::ellipse::ellipse
ellipse()
Definition: ellipse:125
tools::sg::node::cast
virtual void * cast(const std::string &a_class) const
Definition: node:38
tools::sg::ellipse::pick
virtual void pick(pick_action &a_action)
Definition: ellipse:98
tools::sg::ellipse::rx
sf< float > rx
Definition: ellipse:30
tools::mat4::mul_dir_3
void mul_dir_3(T &a_x, T &a_y, T &a_z) const
Definition: mat4:211
sf
tools::vec3f
Definition: vec3f:13
tools::sg::render_action
Definition: render_action:24
tools::curve::m_model
mat4f m_model
Definition: curve:58
tools::sg::ellipse::bbox
virtual void bbox(bbox_action &a_action)
Definition: ellipse:117
tools::curve::operator=
curve & operator=(const curve &a_from)
Definition: curve:51
tools
inlined C code : ///////////////////////////////////
Definition: aida_ntuple:26
tools::sg::node::add_field
void add_field(field *a_field)
Definition: node:128
tools::sg::primitive_visitor::add_line_strip
bool add_line_strip(size_t a_floatn, const float *a_xyzs, bool a_stop=false)
Definition: primitive_visitor:787
tools::ftwo_pi
float ftwo_pi()
Definition: mathf:12
tools::curve
Definition: curve:19
tools::sg::pick_action::done
bool done() const
Definition: pick_action:255
TOOLS_FIELD_DESC_NODE_CLASS
#define TOOLS_FIELD_DESC_NODE_CLASS(a__class)
Definition: field:68
tools::sg::ellipse::update_sg
void update_sg()
Definition: ellipse:160
tools::sg::pick_action::ws
const std::vector< float > & ws() const
Definition: pick_action:323
tools::sg::bsf::value
T & value()
Definition: bsf:98
tools::fsin
float fsin(const float &x)
Definition: mathf:43
tools::sg::ellipse::operator=
ellipse & operator=(const ellipse &a_from)
Definition: ellipse:149
tools::sg::states::state
const sg::state & state() const
Definition: states:76
tools::sg::node::touched
virtual bool touched()
Definition: node:96
tools::sg::sf< float >
tools::sg::ellipse::~ellipse
virtual ~ellipse()
Definition: ellipse:136
TOOLS_ARG_FIELD_DESC
#define TOOLS_ARG_FIELD_DESC(a__field)
Definition: field:71
tools::sg::ellipse::steps
sf< unsigned int > steps
Definition: ellipse:34
tools::sg::ellipse::m_xyzs
std::vector< float > m_xyzs
Definition: ellipse:215
tools::sg::node::reset_touched
virtual void reset_touched()
Definition: node:102
bbox_action