Geant4  9.6.p02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
G4NURBS.hh
Go to the documentation of this file.
1 //
2 // ********************************************************************
3 // * License and Disclaimer *
4 // * *
5 // * The Geant4 software is copyright of the Copyright Holders of *
6 // * the Geant4 Collaboration. It is provided under the terms and *
7 // * conditions of the Geant4 Software License, included in the file *
8 // * LICENSE and available at http://cern.ch/geant4/license . These *
9 // * include a list of copyright holders. *
10 // * *
11 // * Neither the authors of this software system, nor their employing *
12 // * institutes,nor the agencies providing financial support for this *
13 // * work make any representation or warranty, express or implied, *
14 // * regarding this software system or assume any liability for its *
15 // * use. Please see the license in the file LICENSE and URL above *
16 // * for the full disclaimer and the limitation of liability. *
17 // * *
18 // * This code implementation is the result of the scientific and *
19 // * technical work of the GEANT4 collaboration. *
20 // * By using, copying, modifying or distributing the software (or *
21 // * any work based on the software) you agree to acknowledge its *
22 // * use in resulting scientific publications, and indicate your *
23 // * acceptance of all terms of the Geant4 Software license. *
24 // ********************************************************************
25 //
26 //
27 // $Id$
28 //
29 // Olivier Crumeyrolle 12 September 1996
30 
31 // G4NURBS.hh
32 // prototype for class G4NURBS - see documentation in graphics_reps/doc.
33 // OC 280896
34 
35 // Class Description:
36 // Base class for shapes with NURBS drawing style.
37 // See documentation in graphics_reps/doc for details.
38 
39 #ifndef __C_G4NURBS__
40 #define __C_G4NURBS__ 1
41 
42 #include "globals.hh"
43 #include "G4Visible.hh"
44 
45 // The internal floating point type is G4Float, defined line 162
46 
47 #include "G4ios.hh"
48 #include "G4Point3D.hh"
49 #include "G4Vector3D.hh"
50 
51 class G4NURBS : public G4Visible
52 {
53  public:
54  // NO public constructor. A G4NURBS must be builded with a child class.
55  // Pure virtual function Whoami so one can't instanciate G4NURBS at all.
56 
57  // Whoami return a string describing the NURBS (e.g "Box")
58  // * this string must not contain any \n *
59  // this string is *not* yours (const char)
60  virtual const char* Whoami() const = 0;
61 
62  // the copy constructor and assignment opertor are private.
63 
64  // destructor.
65  virtual ~G4NURBS();
66 
67  // direction selector defined as a type because the user will use it
68  // and we want the user to be well-manered.
69  // However internally this typed enum is not as easy to use as it
70  // could be (we can't ++). "t_" means it's a kind of local type.
72  {
73  U = 0,
74  V = 1,
75  DMask = 1, // NofD : Number of Directions
76  NofD = 2 // DMask : direction mask for fast range control,
77  }; // e.g. : m[a_dir & DMask]
78 
79  // external representation for t_direction (just U -> 'U' V -> 'V')
80  static char Tochar(t_direction in_dir);
81 
82  // mother index type (I'd like to be able to use unsigned G4int
83  // but it's impossible)
84  typedef unsigned int t_index;
85 
86  // type for knot index, derivate from t_index
87  typedef t_index t_indKnot;
88 
89  // type for ctrlpt coord and ctrlpt index
90  typedef unsigned int t_indCoord;
91  typedef unsigned int t_indCtrlPt; // mono index
92  typedef t_index t_inddCtrlPt; // bi dim index, derivate from t_index
93 
94  // why only t_inddCtrlPt and t_indKnot (and t_order further)
95  // "derive" of t_index ? Because only these ones need
96  // to be compatible (order + nbrctrlpts = nbrknots in a given direction)
97  // Ok, typedefs are not true type derivation,
98  // but this is the "spirit" of declarations with t_index.
99  // To do true derivation we need true classes
100  // but classes for int are wastefull with today's compilers.
101 
102  // Note that these index types are defined
103  // without knowledge of the indexed items types and that's perfect.
104 
105  // interface data type for the rationnal control points
106  enum { X, Y, Z, W, NofC }; // NofC : number of coordinates
107 
108  // not typed as t_indCoord so loops are easy
109  // to write, but the user is less restricted
110  typedef G4double t_doubleCtrlPt [NofC]; // with doubles
111  typedef G4float t_floatCtrlPt [NofC]; // with floats
112 
113  // access functions for others (e.g. GraphicsModel)
114  G4int GetUorder() const;
115  G4int GetVorder() const;
116  G4int GetUnbrKnots() const;
117  G4int GetVnbrKnots() const;
118  G4int GetUnbrCtrlPts() const;
119  G4int GetVnbrCtrlPts() const;
120  G4int GettotalnbrCtrlPts() const;
121 
122  G4double GetUmin() const;
123  G4double GetUmax() const;
124  G4double GetVmin() const;
125  G4double GetVmax() const;
126  void CalcPoint(G4double u, G4double v,
127  G4Point3D &p, G4Vector3D &utan, G4Vector3D &vtan) const;
128 
129  // alternate access functions with G4NURBS::t_direction
130  // e.g. mynurb.Getorder(G4NURBS::U)
131  // these functions never fail because in_dir is masked
132  G4int Getorder(t_direction in_dir) const;
133  G4int GetnbrKnots(t_direction in_dir) const;
134  G4int GetnbrCtrlPts(t_direction in_dir) const;
135 
136  // crude access to knots vector and control points.
137  // float and double versions.
138  // * one should rather use the iterators below *
139 
140  // get a *copy* of the value; this copy is the user's
141  // one, so the user is intended to manage it (including delete).
142  // in_dir is masked, in_index checked and rounded.
143  // errors on G4cerr
144  G4float GetfloatKnot(t_direction in_dir, t_indKnot in_index) const;
145  G4double GetdoubleKnot(t_direction in_dir, t_indKnot in_index) const;
146  t_floatCtrlPt* GetfloatCtrlPt(t_indCtrlPt in_onedimindex) const;
148  t_inddCtrlPt in_Vindex) const;
149  t_doubleCtrlPt* GetdoubleCtrlPt(t_indCtrlPt in_onedimindex) const;
151  t_inddCtrlPt in_Vindex) const;
152 
153  // complete copy functions
154  // the user don't control the allocation and the copy process
155  // but he/she own the result and will have to delete it
156  // when he/she does not need it any more.
157  G4float* GetfloatAllKnots(t_direction in_dir) const;
158  G4double* GetdoubleAllKnots(t_direction in_dir) const;
159  G4float* GetfloatAllCtrlPts() const;
160  G4double* GetdoubleAllCtrlPts() const;
161 
162  // the iterators need that, the user does not
163 
164  protected:
165  // internal type for reel numbers
166  // ( Float is defined in templates.hh and is
167  // under the control of HIGH_PRECISION )
168  typedef Float G4Float;
169 
170  public:
171 
172  // internal type for order, derivate from t_index
173  typedef t_index t_order;
174 
175  // internal type for knot
176  typedef G4Float t_Knot;
177 
178  protected:
179 
180  // internal types for the control points
181  typedef G4Float t_Coord;
182  typedef t_Coord t_CtrlPt [NofC];
183 
184  // (nb: templates.hh included in globals.hh)
185  // type for ref counting
186  //typedef unsigned int t_refcount;
187 
188  public:
189  // iterators for an .... iterative access to knots and control points
190 
191  // errors are reported on G4cerr
192  // they are friends, they use the protected members.
193  // one can have as many iterators as he/she wants working in the same time.
194 
195  // declarations of iterators
196  class KnotsIterator;
197  class CtrlPtsCoordsIterator;
199 
200  // friendness declarations for iterators
201  friend class KnotsIterator;
202  friend class CtrlPtsCoordsIterator;
203  friend class CtrlPtsIterator;
204 
205  // Example for the KnotsIterator
206  // G4float * my_array, * my_float_p;
207  // my_float_p = my_array = new float [my_nurb.GetnbrKnots(G4NURBS::U)];
208  // G4NURBS::KnotsIterator my_iterator(my_nurb, G4NURBS::U);
209  // while (my_iterator.pick(my_float_p++));
210  // that's all! my_array contain all the U knots.
211 
213  {
214  public:
215  KnotsIterator(const G4NURBS & in_rNurb, t_direction in_dir,
216  t_indKnot in_startIndex = 0);
217  G4bool pick(G4double * inout_pDbl);
218  G4bool pick(G4float * inout_pFlt);
219  //~KnotsIterator();
220 
221  protected:
223  const t_Knot * const kmpMax;
224  const t_Knot * mp;
225  };
226 
227  // the CtrlPtsCoordsIterator. Works like the knots' one :
228  // G4float * my_array, * my_float_p;
229  // my_float_p = my_array =
230  // new float [my_nurb.GettotalnbrCtrlPts()*G4NURBS::NofC*sizeof(float)];
231  // G4NURBS::CtrlPtsCoordsIterator my_iterator(my_nurb);
232  // while (my_iterator.pick(my_float_p++));
233  // after the while statement; my_float_p point just after the array
234  // Remember ctrlpts are given U index increasing first
235 
237  {
238  public:
239  CtrlPtsCoordsIterator(const G4NURBS & in_rNurb,
240  t_indCtrlPt in_startCtrlPtIndex = 0);
241  G4bool pick(G4double * inout_pDbl);
242  G4bool pick(G4float * inout_pFlt);
243  //~CtrlPtsCoordsIterator();
244 
245  protected:
246  const t_Coord * const kmpMax;
247  const t_Coord * mp;
248  };
249 
250  // this iterator work CtrlPt by CtrlPt
251  // see the << overload for an example
253  {
254  public:
255  CtrlPtsIterator(const G4NURBS & in_rNurb, t_indCtrlPt in_startIndex = 0);
256  G4bool pick(t_doubleCtrlPt * inout_pDblCtrlPt);
257  G4bool pick(t_floatCtrlPt * inout_pFltCtrlPt);
258  //~CtrlPtsIterator();
259 
260  protected:
261  const t_CtrlPt * const kmpMax;
262  const t_CtrlPt * mp;
263  };
264 
265  // Q: a directional Iterator to extract one col/row of CtrlPts ?
266 
267  protected:
268 
269  // little structure containing data for each direction
270  struct t_Dir
271  {
276  //t_refcount nbralias;
277  };
278 
279  // check flag for the constructor
280  typedef enum { NOcheck, check } t_CheckFlag;
281 
282  // first constructor (see G4NURBScylinder.cc for an example)
283  // compulsory arguments :
284  // order of the surface in U and V direction
285  // number of control points in U and V direction
286  // control points array (usualy empty here, *but* allocated)
287  // optional arguments :
288  // U and V knots vector (can be automaticaly generated)
289  // check flag (default is to check!)
290  //
291  G4NURBS (t_order in_Uorder, t_order in_Vorder,
292  t_inddCtrlPt in_UnbrCtrlPts, t_inddCtrlPt in_VnbrCtrlPts,
293  t_CtrlPt * in_pCtrlPts,
294  t_Knot * in_pUKnots = 0, t_Knot * in_pVKnots = 0,
295  t_CheckFlag in_CheckFlag = check );
296 
297  // NB: the minimal NURBS is order 1, 2 knots, => 1 control points
298  // one can actually define some curves with G4NURBS, set U as you want
299  // set the V dir as order 1, 1 ctrlpt, 2 knots { 0 1 }
300  // OpenGL work with this kind of data
301 
302  // second constructor (easier to use) (see G4NURBStube.cc for an example)
303  // compulsory arguments :
304  // order of the surface in U and V direction
305  // number of control points in U and V direction
306  // optional arguments :
307  // U and V knots vector generation flag (automaticaly or not)
308  // check flag (default is to check!)
309  // Allocations are Done for the user
310  // but he/she still have to fill some arrays
311  // For the moment I don't see yet how to ensure
312  // that the user correctly fill the arrays
313  // (in particular how avoid out of range access)
314  // without class types for arrays.
315 
316  public:
317 
318  // knots vector generation flag
320  {
321  UserDefined, // The user will fill the array (in the child constructor
322  // for instance).
323 
324  Regular, // First and last knot repeated order time
325  // other knots regularly spaced, unrepeated.
326  // Typically used for "linear" knots vector
327 
328  RegularRep // First and last knot repeated order time
329  // other knots regularly spaced but repeated one time.
330  // Typically used for "circular" knots vector and alikes.
331  }; //t_KnotVectorGenFlag
332 
333  protected:
334 
335  // external representation for t_KnotVectorGenFlag
336  // as a << overload.
337  // (used in errors report)
338  friend std::ostream & operator << (std::ostream & inout_OutStream,
339  t_KnotVectorGenFlag in_KVGFlag);
340 
341  G4NURBS (t_order in_Uorder, t_order in_Vorder,
342  t_inddCtrlPt in_UnbrCtrlPts, t_inddCtrlPt in_VnbrCtrlPts,
343  t_KnotVectorGenFlag in_UKVGFlag = Regular,
344  t_KnotVectorGenFlag in_VKVGFlag = Regular,
345  t_CheckFlag in_CheckFlag = check );
346 
347  // nurbs data
348  t_Dir m[NofD]; // t_Dir : order nbrCtrlPts nbrKnots pKnots
349  t_indCtrlPt mtotnbrCtrlPts; // Total number of control points
350  t_CtrlPt * mpCtrlPts; // U increasing first, V after
351  //t_refcount mnbralias; // ref count for mpCtrlPts
352 
353  // 2dim index to 1 dim conversion
354  t_indCtrlPt To1d(t_inddCtrlPt in_Uindex, t_inddCtrlPt in_Vindex) const;
355 
356  // internal functions for converting the internal
357  // data points to the interface type required
358  // one can do some better things with class conversion
359  // but for the moment control point data types are not class.
360  // static functions.
361  // if changed to member functions, one must add the const
362  // status and rewrite calls with an instance in
363  // some of the get functions.
364 
365  // return a float copy
366  static t_floatCtrlPt* TofloatCtrlPt(const t_CtrlPt &);
367 
368  // return a double copy
369  static t_doubleCtrlPt* TodoubleCtrlPt(const t_CtrlPt &);
370 
371 
372  // Building functions
373 
374  // KnotsVector builder
375  // static function that work on a t_Dir and its
376  // knot vector. So we can define
377  // some knots vector outside a nurbs
378  // object. (This avoid the existence
379  // of some incompletly defined nurbs object,
380  // used just as knots vector container)
381  // Return true if succesfull.
382  // ALWAYS allocate the knots array.
383  // (return false and do nothing if it already exists (ie != 0))
384  // Always fail if order + nbrCtrlPt != nbrKnots
385  static G4bool MakeKnotVector(t_Dir & inout_dirdat,
386  t_KnotVectorGenFlag in_KVGFlag);
387  static G4bool MakeKnotVector(t_Dir * p_inoutdirdat,
388  t_KnotVectorGenFlag in_KVGFlag);
389 
390  static void CP(G4NURBS::t_CtrlPt & rcp, t_Coord x, t_Coord y,
391  t_Coord z, t_Coord w);
392  static void CP(G4NURBS::t_CtrlPt & rcp, t_Coord x, t_Coord y,
393  t_Coord z, t_Coord w, G4Float factor);
394 
395  private:
396  // check function used internally by constructors.
397  // no returned value because all errors reported are fatals.
398  // (assume order + nbrCtrlPts == nbrKnots
399  // cf constructors to understand why)
400  void Conscheck() const;
401 
402  // copy constructor.
403  // Not really necessary for geant. A warning is issued when used.
404  G4NURBS(const G4NURBS &);
405 
406  // Private assignment operator - don't use, doesn't exist.
407  // (Added to satisfy Coverity, JA 11/11/11.)
408  G4NURBS& operator= (const G4NURBS&);
409 
410 };
411 
412 // external representation for t_KnotVectorGenFlag
413 std::ostream & operator << (std::ostream & inout_OutStream,
414  G4NURBS::t_KnotVectorGenFlag in_KVGFlag);
415 
416 
417 // << overload to dump a nurbs
418 // writted with public access functions
419 // do not depends on protected part
420 
421 std::ostream & operator << (std::ostream & inout_outStream,
422  const G4NURBS & in_kNurb);
423 
424 /***********************************************************************
425  * *
426  * Inline code for public access functions. *
427  * depends on the protected part *
428  * *
429  ***********************************************************************/
430 
431 inline G4int G4NURBS::GetUorder() const { return m[U].order; }
432 inline G4int G4NURBS::GetVorder() const { return m[V].order; }
433 inline G4int G4NURBS::GetUnbrKnots() const { return m[U].nbrKnots; }
434 inline G4int G4NURBS::GetVnbrKnots() const { return m[V].nbrKnots; }
435 inline G4int G4NURBS::GetUnbrCtrlPts() const { return m[U].nbrCtrlPts; }
436 inline G4int G4NURBS::GetVnbrCtrlPts() const { return m[V].nbrCtrlPts; }
438 
439 inline G4double G4NURBS::GetUmin() const {
440  return (G4double) m[U].pKnots[GetUorder()-1];
441 }
442 
443 inline G4double G4NURBS::GetUmax() const {
444  return (G4double) m[U].pKnots[GetUnbrCtrlPts()];
445 }
446 
447 inline G4double G4NURBS::GetVmin() const {
448  return (G4double) m[V].pKnots[GetVorder()-1];
449 }
450 
451 inline G4double G4NURBS::GetVmax() const {
452  return (G4double) m[V].pKnots[GetVnbrCtrlPts()];
453 }
454 
456  return m[in_dir & DMask].order;
457 }
458 
460  return m[in_dir & DMask].nbrKnots;
461 }
462 
464  return m[in_dir & DMask].nbrCtrlPts;
465 }
466 
468  return (in_dir != U? 'V': 'U');
469 }
470 
471 /***********************************************************************
472  * *
473  * inline code for protected functions *
474  * *
475  ***********************************************************************/
476 
477 // convert two dim. index to one dim.
478 //( Ctrl Pts are stored U increasing first )
479 // no check.
481 G4NURBS::To1d(t_inddCtrlPt in_Uindex, t_inddCtrlPt in_Vindex) const
482 {
483  return in_Uindex + in_Vindex*m[U].nbrCtrlPts;
484 }
485 
486 // return a float copy
488 G4NURBS::TofloatCtrlPt(const t_CtrlPt & in_krcp)
489 {
491  for (G4int indCoord = X; indCoord < NofC; indCoord++)
492  (*pcopy)[indCoord] = (G4float)in_krcp[indCoord];
493  return pcopy;
494 }
495 
496 // return a double copy
498 G4NURBS::TodoubleCtrlPt(const t_CtrlPt & in_krcp)
499 {
501  for (G4int indCoord = X; indCoord < NofC; indCoord++)
502  (*pcopy)[indCoord] = (G4double)in_krcp[indCoord];
503  return pcopy;
504 }
505 
506 // MakeKnotVector alias
508  G4NURBS::t_KnotVectorGenFlag in_KVGFlag)
509 {
510  return MakeKnotVector(*p_inoutdirdat, in_KVGFlag);
511 }
512 
513 /***********************************************************************
514  * *
515  * inlines functions to simplify control points definition *
516  * see GG4NURBSbox.cc for instance *
517  * *
518  ***********************************************************************/
519 
520 inline void G4NURBS::CP(G4NURBS::t_CtrlPt & rcp,
522 {
523  rcp[G4NURBS::X]=x;
524  rcp[G4NURBS::Y]=y;
525  rcp[G4NURBS::Z]=z;
526  rcp[G4NURBS::W]=w;
527 }
528 
529 // with a common factor
531  t_Coord y, t_Coord z, t_Coord w, G4Float factor)
532 {
533  rcp[G4NURBS::X]=factor*x;
534  rcp[G4NURBS::Y]=factor*y;
535  rcp[G4NURBS::Z]=factor*z;
536  rcp[G4NURBS::W]=factor*w;
537 }
538 
539 #endif /* end of __C_G4NURBS__ */