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 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

  1 // Copyright (C) 2010, Guy Barrand. All rights reserved.
  2 // See the file tools.license for terms.
  3 
  4 #ifndef tools_sg_vertices
  5 #define tools_sg_vertices
  6 
  7 #include "node"
  8 #include "gstos"
  9 
 10 #include "sf"
 11 #include "mf"
 12 #include "render_action"
 13 #include "pick_action"
 14 #include "bbox_action"
 15 #include "visible_action"
 16 
 17 #include "../vmanip"
 18 
 19 namespace tools {
 20 namespace sg {
 21 
 22 class vertices : public node, public gstos {
 23   TOOLS_NODE(vertices,tools::sg::vertices,node)
 24   typedef gstos parent_gstos;
 25 public:
 26   sf<gl::mode_t> mode;
 27   mf<float> xyzs;
 28 public:
 29   virtual const desc_fields& node_desc_fields() const {
 30     TOOLS_FIELD_DESC_NODE_CLASS(tools::sg::vertices)
 31     static const desc_fields s_v(parent::node_desc_fields(),2, //WARNING : take care of count.
 32       TOOLS_ARG_FIELD_DESC(mode),
 33       TOOLS_ARG_FIELD_DESC(xyzs)
 34     );
 35     return s_v;
 36   }
 37 private:
 38   void add_fields(){
 39     add_field(&mode);
 40     add_field(&xyzs);
 41   }
 42 protected: //gstos
 43   virtual unsigned int create_gsto(std::ostream&,sg::render_manager& a_mgr) {
 44     //unsigned int npt = xyzs.values().size()/3;
 45     //::printf("debug : vertices : %lu : create_gsto : %u\n",this,npt);
 46     return a_mgr.create_gsto_from_data(xyzs.values());
 47   }
 48 
 49 public:
 50   virtual void render(render_action& a_action) {
 51     if(touched()) {clean_gstos();reset_touched();}
 52     if(xyzs.empty()) return;
 53 
 54     const state& state = a_action.state();
 55 
 56     if(state.m_use_gsto) {
 57       unsigned int _id = get_gsto_id(a_action.out(),a_action.render_manager());
 58       if(_id) {
 59 #ifdef __APPLE__
 60         bool restore_blend = check_set_blend(a_action);
 61 #endif
 62         a_action.begin_gsto(_id);
 63         size_t npt = xyzs.values().size()/3;
 64         bufpos pos = 0;
 65         if(gl::is_line(mode.value())) {
 66           //Same logic as Inventor SoLightModel.model = BASE_COLOR.
 67           a_action.set_lighting(false);
 68           a_action.draw_gsto_v(mode.value(),npt,pos);
 69           a_action.set_lighting(state.m_GL_LIGHTING);
 70         } else {
 71           a_action.draw_gsto_v(mode.value(),npt,pos);
 72         }
 73         a_action.end_gsto();
 74 #ifdef __APPLE__
 75         if(restore_blend) a_action.set_blend(true);
 76 #endif
 77         return;
 78       } else { //!_id
 79         // use immediate rendering.
 80       }
 81 
 82     } else {
 83       clean_gstos(&a_action.render_manager());
 84     }
 85 
 86 #ifdef __APPLE__
 87     bool restore_blend = check_set_blend(a_action);
 88 #endif
 89 
 90     // immediate rendering :
 91     if(gl::is_line(mode.value())) {
 92       //Same logic as Inventor SoLightModel.model = BASE_COLOR.
 93       a_action.set_lighting(false);
 94       a_action.draw_vertex_array(mode.value(),xyzs.values());
 95       a_action.set_lighting(state.m_GL_LIGHTING);
 96     } else {
 97       a_action.draw_vertex_array(mode.value(),xyzs.values());
 98     }
 99 
100 #ifdef __APPLE__
101     if(restore_blend) a_action.set_blend(true);
102 #endif
103   }
104   virtual void pick(pick_action& a_action) {
105     if(touched()) {clean_gstos();reset_touched();}
106     a_action.add__primitive(*this,mode.value(),xyzs.values(),true);
107   }
108 
109   virtual void bbox(bbox_action& a_action) {
110     if(touched()) {clean_gstos();reset_touched();}
111     a_action.add_points(xyzs.values());
112   }
113   virtual void is_visible(visible_action& a_action) {
114     if(touched()) {clean_gstos();reset_touched();}
115     if(_is_visible(a_action)) a_action.increment();
116   }
117 
118 public:
119   vertices()
120   :parent()
121   ,mode(gl::line_strip()){
122 #ifdef TOOLS_MEM
123     mem::increment(s_class().c_str());
124 #endif
125     add_fields();
126   }
127   virtual ~vertices(){
128 #ifdef TOOLS_MEM
129     mem::decrement(s_class().c_str());
130 #endif
131   }
132 public:
133   vertices(const vertices& a_from)
134   :parent(a_from)
135   ,parent_gstos(a_from)
136   ,mode(a_from.mode)
137   ,xyzs(a_from.xyzs)
138   {
139 #ifdef TOOLS_MEM
140     mem::increment(s_class().c_str());
141 #endif
142     add_fields();
143   }
144   vertices& operator=(const vertices& a_from){
145     parent::operator=(a_from);
146     parent_gstos::operator=(a_from);
147 
148     mode = a_from.mode;
149     xyzs = a_from.xyzs;
150 
151     return *this;
152   }
153 public:
154   template <class VEC>
155   void add(const VEC& a_v) {
156     xyzs.add(a_v.x());
157     xyzs.add(a_v.y());
158     xyzs.add(a_v.z());
159   }
160   void add(float a_x,float a_y,float a_z) {
161     xyzs.add(a_x);
162     xyzs.add(a_y);
163     xyzs.add(a_z);
164   }
165   void add_allocated(size_t& a_pos,float a_x,float a_y,float a_z) {
166     std::vector<float>& v = xyzs.values();
167     v[a_pos] = a_x;a_pos++;
168     v[a_pos] = a_y;a_pos++;
169     v[a_pos] = a_z;a_pos++;
170     xyzs.touch();
171   }
172   bool add(const std::vector<float>& a_v) {
173     std::vector<float>::size_type _number = a_v.size()/3;
174     if(3*_number!=a_v.size()) return false;
175     std::vector<float>::const_iterator it;
176     for(it=a_v.begin();it!=a_v.end();it+=3) {
177       xyzs.add(*(it+0));
178       xyzs.add(*(it+1));
179       xyzs.add(*(it+2));
180     }
181     return true;
182   }
183 
184   size_t number() const {return xyzs.size()/3;}
185   void clear() {xyzs.clear();}
186 
187   bool add_dashed_line(float a_bx,float a_by,float a_bz,
188                        float a_ex,float a_ey,float a_ez,
189                        unsigned int a_num_dash) {
190     //optimized version.
191     if(!a_num_dash) return false;
192     // there is a dash at beg and end of line.
193 
194     float fac = 1.0f/float(2*a_num_dash-1);
195     float sx = (a_ex-a_bx)*fac;
196     float sy = (a_ey-a_by)*fac;
197     float sz = (a_ez-a_bz)*fac;
198 
199     float two_sx = sx*2.0f;
200     float two_sy = sy*2.0f;
201     float two_sz = sz*2.0f;
202 
203     float px = a_bx;
204     float py = a_by;
205     float pz = a_bz;
206 
207     for(unsigned int idash=0;idash<a_num_dash;idash++) {
208       add(px,py,pz);
209       add(px+sx,py+sy,pz+sz);
210       px += two_sx;
211       py += two_sy;
212       pz += two_sz;
213     }
214     return true;
215   }
216   bool center() {
217     std::vector<float>& v = xyzs.values();
218     std::vector<float>::size_type _number = v.size()/3;
219     if(!_number) return true;
220     if(3*_number!=v.size()) return false;
221     float x_mean = 0;
222     float y_mean = 0;
223     float z_mean = 0;
224    {for(std::vector<float>::const_iterator it=v.begin();it!=v.end();it+=3) {
225       x_mean += *(it+0);
226       y_mean += *(it+1);
227       z_mean += *(it+2);
228     }}
229     x_mean /= float(_number);
230     y_mean /= float(_number);
231     z_mean /= float(_number);
232    {for(std::vector<float>::iterator it=v.begin();it!=v.end();it+=3) {
233       *(it+0) -= x_mean;
234       *(it+1) -= y_mean;
235       *(it+2) -= z_mean;
236     }}
237     return true;
238   }
239 protected:
240   bool _is_visible(const matrix_action& a_action) {
241     if(xyzs.empty()) return false;
242     const state& _state = a_action.state();
243     pick_action action(a_action.out(),_state.m_ww,_state.m_wh,0,float(_state.m_ww),0,float(_state.m_wh));
244     action.set_win_size(_state.m_ww,_state.m_wh);
245     action.set_area(0,float(_state.m_ww),0,float(_state.m_wh));
246     action.set_stop_at_first(true);
247     action.matrix_action::operator=(a_action); //IMPORTANT.
248     int old_cur = action.cur(); //not 0.
249     action.add__primitive(*this,mode.value(),xyzs.values(),true);
250     if(action.cur()!=old_cur) return false;
251     if(!action.node()) return false;
252     return true;
253   }
254 #ifdef __APPLE__
255 protected:
256   // macOS/Mojave : on this version, points are blended even if alpha is one !
257   bool check_set_blend(render_action& a_action) {
258     bool restore_blend = false;
259     const state& state = a_action.state();
260     if(state.m_GL_BLEND) {
261       if(state.m_color.a()==1) {
262         a_action.set_blend(false);
263         restore_blend = true;
264       }
265     }
266     return restore_blend;
267   }
268 #endif //__APPLE__
269 };
270 
271 }}
272 
273 #endif