G4NURBS.hh

Go to the documentation of this file.
00001 //
00002 // ********************************************************************
00003 // * License and Disclaimer                                           *
00004 // *                                                                  *
00005 // * The  Geant4 software  is  copyright of the Copyright Holders  of *
00006 // * the Geant4 Collaboration.  It is provided  under  the terms  and *
00007 // * conditions of the Geant4 Software License,  included in the file *
00008 // * LICENSE and available at  http://cern.ch/geant4/license .  These *
00009 // * include a list of copyright holders.                             *
00010 // *                                                                  *
00011 // * Neither the authors of this software system, nor their employing *
00012 // * institutes,nor the agencies providing financial support for this *
00013 // * work  make  any representation or  warranty, express or implied, *
00014 // * regarding  this  software system or assume any liability for its *
00015 // * use.  Please see the license in the file  LICENSE  and URL above *
00016 // * for the full disclaimer and the limitation of liability.         *
00017 // *                                                                  *
00018 // * This  code  implementation is the result of  the  scientific and *
00019 // * technical work of the GEANT4 collaboration.                      *
00020 // * By using,  copying,  modifying or  distributing the software (or *
00021 // * any work based  on the software)  you  agree  to acknowledge its *
00022 // * use  in  resulting  scientific  publications,  and indicate your *
00023 // * acceptance of all terms of the Geant4 Software license.          *
00024 // ********************************************************************
00025 //
00026 //
00027 // $Id$
00028 // 
00029 // Olivier Crumeyrolle  12 September 1996
00030 
00031 // G4NURBS.hh
00032 // prototype for class G4NURBS - see documentation in graphics_reps/doc.
00033 // OC 280896
00034 
00035 // Class Description:
00036 // Base class for shapes with NURBS drawing style.
00037 // See documentation in graphics_reps/doc for details.
00038 
00039 #ifndef __C_G4NURBS__ 
00040 #define __C_G4NURBS__ 1
00041 
00042 #include "globals.hh"
00043 #include "G4Visible.hh"
00044 
00045 // The internal floating point type is G4Float, defined line 162
00046 
00047 #include "G4ios.hh"
00048 #include "G4Point3D.hh"
00049 #include "G4Vector3D.hh"
00050 
00051 class G4NURBS : public G4Visible
00052 { 
00053  public:
00054   // NO public constructor. A G4NURBS must be builded with a child class.
00055   // Pure virtual function Whoami so one can't instanciate G4NURBS at all.
00056 
00057   // Whoami return a string describing the NURBS (e.g "Box")
00058   // * this string must not contain any \n *
00059   // this string is *not* yours (const char)
00060   virtual const char* Whoami() const = 0;
00061 
00062   // the copy constructor and assignment opertor are private.
00063 
00064   // destructor. 
00065   virtual ~G4NURBS();
00066 
00067   // direction selector defined as a type because the user will use it
00068   // and we want the user to be well-manered.
00069   // However internally this typed enum is not as easy to use as it
00070   // could be (we can't ++). "t_" means it's a kind of local type.
00071   enum t_direction
00072   {
00073     U     = 0,
00074     V     = 1,
00075     DMask = 1,  // NofD : Number of Directions 
00076     NofD  = 2   // DMask : direction mask for fast range control,
00077   };            // e.g. : m[a_dir & DMask]
00078 
00079   // external representation for t_direction (just U -> 'U' V -> 'V') 
00080   static char Tochar(t_direction in_dir);
00081 
00082   // mother index type (I'd like to be able to use unsigned G4int
00083   // but it's impossible)
00084   typedef unsigned int t_index;
00085 
00086   // type for knot index, derivate from t_index
00087   typedef t_index t_indKnot;
00088 
00089   // type for ctrlpt coord and ctrlpt index
00090   typedef unsigned int t_indCoord;
00091   typedef unsigned int t_indCtrlPt;     // mono index
00092   typedef t_index      t_inddCtrlPt;    // bi dim index, derivate from t_index
00093                 
00094   // why only t_inddCtrlPt and t_indKnot (and t_order further)
00095   // "derive" of t_index ? Because only these ones need
00096   // to be compatible (order + nbrctrlpts = nbrknots in a given direction)
00097   // Ok, typedefs are not true type derivation,
00098   // but this is the "spirit" of declarations with t_index.
00099   // To do true derivation we need true classes 
00100   // but classes for int are wastefull with today's compilers.
00101 
00102   // Note that these index types are defined
00103   // without knowledge of the indexed items types and that's perfect.
00104 
00105   // interface data type for the rationnal control points
00106   enum { X, Y, Z, W, NofC };    // NofC : number of coordinates
00107 
00108   // not typed as t_indCoord so loops are easy
00109   // to write, but the user is less restricted
00110   typedef G4double t_doubleCtrlPt [NofC]; // with doubles 
00111   typedef G4float  t_floatCtrlPt [NofC];  // with floats
00112 
00113   // access functions for others (e.g. GraphicsModel)
00114   G4int GetUorder() const;
00115   G4int GetVorder() const;
00116   G4int GetUnbrKnots() const;
00117   G4int GetVnbrKnots() const;
00118   G4int GetUnbrCtrlPts() const; 
00119   G4int GetVnbrCtrlPts() const;
00120   G4int GettotalnbrCtrlPts() const;     
00121 
00122   G4double GetUmin() const;
00123   G4double GetUmax() const;
00124   G4double GetVmin() const;
00125   G4double GetVmax() const;
00126   void CalcPoint(G4double u, G4double v,
00127                  G4Point3D &p, G4Vector3D &utan, G4Vector3D &vtan) const;
00128 
00129   // alternate access functions with G4NURBS::t_direction
00130   // e.g. mynurb.Getorder(G4NURBS::U)
00131   // these functions never fail because in_dir is masked
00132   G4int Getorder(t_direction in_dir) const;
00133   G4int GetnbrKnots(t_direction in_dir) const;
00134   G4int GetnbrCtrlPts(t_direction in_dir) const;        
00135 
00136   // crude access to knots vector and control points.
00137   // float and double versions.
00138   // * one should rather use the iterators below *
00139                 
00140   // get a *copy* of the value; this copy is the user's
00141   // one, so the user is intended to manage it (including delete).
00142   // in_dir is masked, in_index checked and rounded.
00143   // errors on G4cerr
00144   G4float         GetfloatKnot(t_direction in_dir, t_indKnot in_index) const;
00145   G4double        GetdoubleKnot(t_direction in_dir, t_indKnot in_index) const;
00146   t_floatCtrlPt*  GetfloatCtrlPt(t_indCtrlPt in_onedimindex) const;
00147   t_floatCtrlPt*  GetfloatCtrlPt(t_inddCtrlPt in_Uindex,
00148                                  t_inddCtrlPt in_Vindex) const;
00149   t_doubleCtrlPt* GetdoubleCtrlPt(t_indCtrlPt in_onedimindex) const;
00150   t_doubleCtrlPt* GetdoubleCtrlPt(t_inddCtrlPt in_Uindex,
00151                                   t_inddCtrlPt in_Vindex) const;
00152 
00153   // complete copy functions
00154   // the user don't control the allocation and the copy process
00155   // but he/she own the result and will have to delete it
00156   // when he/she does not need it any more.
00157   G4float*  GetfloatAllKnots(t_direction in_dir) const;
00158   G4double* GetdoubleAllKnots(t_direction in_dir) const;
00159   G4float*  GetfloatAllCtrlPts() const;
00160   G4double* GetdoubleAllCtrlPts() const;
00161 
00162   // the iterators need that, the user does not
00163   
00164  protected:
00165   // internal type for reel numbers
00166   // ( Float is defined in templates.hh and is
00167   // under the control of HIGH_PRECISION )
00168   typedef Float G4Float;
00169 
00170  public:
00171 
00172   // internal type for order, derivate from t_index
00173   typedef t_index t_order;
00174 
00175   // internal type for knot
00176   typedef G4Float t_Knot;
00177 
00178  protected:
00179 
00180   // internal types for the control points
00181   typedef G4Float t_Coord;
00182   typedef t_Coord t_CtrlPt [NofC];
00183 
00184   // (nb: templates.hh included in globals.hh)
00185   // type for ref counting
00186   //typedef unsigned int t_refcount; 
00187 
00188  public:
00189   // iterators for an .... iterative access to knots and control points
00190 
00191   // errors are reported on G4cerr
00192   // they are friends, they use the protected members.
00193   // one can have as many iterators as he/she wants working in the same time.
00194 
00195   // declarations of iterators
00196   class KnotsIterator;
00197   class CtrlPtsCoordsIterator;
00198   class CtrlPtsIterator;
00199 
00200   // friendness declarations for iterators
00201   friend class KnotsIterator;
00202   friend class CtrlPtsCoordsIterator;
00203   friend class CtrlPtsIterator;
00204 
00205   // Example for the KnotsIterator
00206   //   G4float * my_array, * my_float_p;
00207   //   my_float_p = my_array = new float [my_nurb.GetnbrKnots(G4NURBS::U)]; 
00208   //   G4NURBS::KnotsIterator  my_iterator(my_nurb, G4NURBS::U);
00209   //   while (my_iterator.pick(my_float_p++));
00210   // that's all! my_array contain all the U knots.
00211 
00212   class KnotsIterator
00213   {
00214   public:
00215     KnotsIterator(const G4NURBS & in_rNurb, t_direction in_dir,
00216                                             t_indKnot in_startIndex = 0);
00217     G4bool pick(G4double * inout_pDbl);
00218     G4bool pick(G4float * inout_pFlt);
00219     //~KnotsIterator();
00220  
00221   protected:
00222     const t_direction    kmdir;
00223     const t_Knot * const kmpMax;
00224     const t_Knot *       mp;
00225   };
00226 
00227   // the CtrlPtsCoordsIterator. Works like the knots' one :
00228   //   G4float * my_array, * my_float_p;
00229   //   my_float_p = my_array =
00230   //      new float [my_nurb.GettotalnbrCtrlPts()*G4NURBS::NofC*sizeof(float)]; 
00231   //   G4NURBS::CtrlPtsCoordsIterator my_iterator(my_nurb);
00232   //   while (my_iterator.pick(my_float_p++));
00233   // after the while statement; my_float_p point just after the array
00234   // Remember ctrlpts are given U index increasing first
00235 
00236   class CtrlPtsCoordsIterator
00237   {
00238   public:
00239     CtrlPtsCoordsIterator(const G4NURBS & in_rNurb,
00240                           t_indCtrlPt in_startCtrlPtIndex = 0); 
00241     G4bool pick(G4double * inout_pDbl);
00242     G4bool pick(G4float * inout_pFlt);
00243     //~CtrlPtsCoordsIterator();
00244 
00245   protected:
00246     const t_Coord * const kmpMax;
00247     const t_Coord * mp;
00248   };
00249 
00250   // this iterator work CtrlPt by CtrlPt
00251   // see the << overload for an example
00252   class CtrlPtsIterator
00253   {
00254   public:
00255     CtrlPtsIterator(const G4NURBS & in_rNurb, t_indCtrlPt in_startIndex = 0);
00256     G4bool pick(t_doubleCtrlPt * inout_pDblCtrlPt);
00257     G4bool pick(t_floatCtrlPt * inout_pFltCtrlPt);
00258     //~CtrlPtsIterator();
00259 
00260   protected:
00261     const t_CtrlPt * const  kmpMax;
00262     const t_CtrlPt *        mp;
00263   };
00264 
00265   // Q: a directional Iterator to extract one col/row of CtrlPts ?
00266         
00267  protected:
00268                 
00269   // little structure containing data for each direction
00270   struct t_Dir
00271   {
00272     t_order      order;
00273     t_inddCtrlPt nbrCtrlPts;
00274     t_indKnot    nbrKnots;
00275     t_Knot *     pKnots;
00276     //t_refcount        nbralias; 
00277   };
00278 
00279   // check flag for the constructor
00280   typedef enum { NOcheck, check } t_CheckFlag;
00281                 
00282   // first constructor (see G4NURBScylinder.cc for an example)
00283   // compulsory arguments :
00284   //   order of the surface in U and V direction
00285   //   number of control points in U and V direction
00286   //   control points array (usualy empty here, *but* allocated)
00287   // optional arguments :
00288   //   U and V knots vector (can be automaticaly generated)
00289   //   check flag       (default is to check!)
00290   //
00291   G4NURBS (t_order in_Uorder, t_order in_Vorder,
00292            t_inddCtrlPt in_UnbrCtrlPts, t_inddCtrlPt in_VnbrCtrlPts,
00293            t_CtrlPt * in_pCtrlPts,
00294            t_Knot * in_pUKnots = 0, t_Knot * in_pVKnots = 0,
00295            t_CheckFlag in_CheckFlag = check );
00296 
00297   // NB: the minimal NURBS is order 1, 2 knots, => 1 control points
00298   // one can actually define some curves with G4NURBS, set U as you want
00299   // set the V dir as order 1, 1 ctrlpt, 2 knots { 0 1 }
00300   // OpenGL work with this kind of data
00301 
00302   // second constructor (easier to use) (see G4NURBStube.cc for an example)
00303   // compulsory arguments :
00304   //   order of the surface in U and V direction
00305   //   number of control points in U and V direction
00306   // optional arguments :
00307   //   U and V knots vector generation flag (automaticaly or not)
00308   //   check flag       (default is to check!)
00309   // Allocations are Done for the user
00310   // but he/she still have to fill some arrays
00311   // For the moment I don't see yet how to ensure
00312   // that the user correctly fill the arrays
00313   // (in particular how avoid out of range access)
00314   // without class types for arrays.
00315 
00316  public:
00317 
00318   // knots vector generation flag
00319   enum t_KnotVectorGenFlag
00320   { 
00321     UserDefined, // The user will fill the array (in the child constructor
00322                  // for instance).
00323 
00324     Regular,     // First and last knot repeated order time
00325                  // other knots regularly spaced, unrepeated.
00326                  // Typically used for "linear" knots vector
00327 
00328     RegularRep   // First and last knot repeated order time
00329                  // other knots regularly spaced but repeated one time.
00330                  // Typically used for "circular" knots vector and alikes.
00331   }; //t_KnotVectorGenFlag
00332 
00333  protected:
00334 
00335   // external representation for t_KnotVectorGenFlag
00336   // as a << overload.
00337   // (used in errors report)
00338   friend std::ostream & operator << (std::ostream & inout_OutStream,
00339                                 t_KnotVectorGenFlag in_KVGFlag);
00340 
00341   G4NURBS (t_order in_Uorder, t_order in_Vorder,
00342            t_inddCtrlPt in_UnbrCtrlPts, t_inddCtrlPt in_VnbrCtrlPts,
00343            t_KnotVectorGenFlag in_UKVGFlag = Regular,
00344            t_KnotVectorGenFlag in_VKVGFlag = Regular,
00345            t_CheckFlag in_CheckFlag = check );
00346 
00347   // nurbs data
00348   t_Dir         m[NofD];        // t_Dir : order nbrCtrlPts nbrKnots pKnots
00349   t_indCtrlPt   mtotnbrCtrlPts; // Total number of control points
00350   t_CtrlPt *    mpCtrlPts;      // U increasing first, V after
00351   //t_refcount  mnbralias;      // ref count for mpCtrlPts
00352                 
00353   // 2dim index to 1 dim conversion
00354   t_indCtrlPt  To1d(t_inddCtrlPt in_Uindex, t_inddCtrlPt in_Vindex) const;
00355 
00356   // internal functions for converting the internal
00357   // data points to the interface type required
00358   // one can do some better things with class conversion
00359   // but for the moment control point data types are not class. 
00360   // static functions.
00361   // if changed to member functions, one must add the const
00362   // status and rewrite calls with an instance in 
00363   // some of the get functions.
00364                 
00365   // return a float copy  
00366   static t_floatCtrlPt* TofloatCtrlPt(const t_CtrlPt &);
00367 
00368   // return a double copy
00369   static t_doubleCtrlPt* TodoubleCtrlPt(const t_CtrlPt &);
00370 
00371 
00372   // Building functions
00373 
00374   // KnotsVector builder
00375   // static function that work on a t_Dir and its
00376   // knot vector. So we can define
00377   // some knots vector outside a nurbs
00378   // object. (This avoid the existence
00379   // of some incompletly defined nurbs object,
00380   // used just as knots vector container)
00381   // Return true if succesfull.
00382   // ALWAYS allocate the knots array.
00383   // (return false and do nothing if it already exists (ie != 0))
00384   // Always fail if order + nbrCtrlPt != nbrKnots
00385   static G4bool MakeKnotVector(t_Dir & inout_dirdat,
00386                                t_KnotVectorGenFlag in_KVGFlag);
00387   static G4bool MakeKnotVector(t_Dir * p_inoutdirdat,
00388                                t_KnotVectorGenFlag in_KVGFlag);
00389 
00390   static void CP(G4NURBS::t_CtrlPt & rcp, t_Coord x, t_Coord y,
00391                                           t_Coord z, t_Coord w);
00392   static void CP(G4NURBS::t_CtrlPt & rcp, t_Coord x, t_Coord y,
00393                                           t_Coord z, t_Coord w, G4Float factor);
00394 
00395  private:       
00396   // check function used internally by constructors.
00397   // no returned value because all errors reported are fatals.
00398   // (assume order + nbrCtrlPts == nbrKnots
00399   //  cf constructors to understand why)
00400   void Conscheck() const;
00401 
00402   // copy constructor.
00403   // Not really necessary for geant. A warning is issued when used.
00404   G4NURBS(const G4NURBS &);
00405 
00406   // Private assignment operator - don't use, doesn't exist.
00407   // (Added to satisfy Coverity, JA 11/11/11.)
00408   G4NURBS& operator= (const G4NURBS&);
00409 
00410 };
00411 
00412 // external representation for t_KnotVectorGenFlag
00413 std::ostream & operator << (std::ostream & inout_OutStream,
00414                               G4NURBS::t_KnotVectorGenFlag in_KVGFlag);
00415 
00416 
00417 // << overload to dump a nurbs
00418 // writted with public access functions
00419 // do not depends on protected part
00420 
00421 std::ostream & operator << (std::ostream & inout_outStream,
00422                               const G4NURBS & in_kNurb);
00423 
00424 /***********************************************************************
00425  *                                                                     *
00426  * Inline code for public access functions.                            *
00427  * depends on the protected part                                       *
00428  *                                                                     *
00429  ***********************************************************************/
00430 
00431 inline G4int G4NURBS::GetUorder() const          { return m[U].order; }
00432 inline G4int G4NURBS::GetVorder() const          { return m[V].order; }
00433 inline G4int G4NURBS::GetUnbrKnots() const       { return m[U].nbrKnots; }
00434 inline G4int G4NURBS::GetVnbrKnots() const       { return m[V].nbrKnots; }
00435 inline G4int G4NURBS::GetUnbrCtrlPts() const     { return m[U].nbrCtrlPts; }
00436 inline G4int G4NURBS::GetVnbrCtrlPts() const     { return m[V].nbrCtrlPts; }
00437 inline G4int G4NURBS::GettotalnbrCtrlPts() const { return mtotnbrCtrlPts; }
00438 
00439 inline G4double G4NURBS::GetUmin() const {
00440   return (G4double) m[U].pKnots[GetUorder()-1];
00441 }
00442 
00443 inline G4double G4NURBS::GetUmax() const {
00444   return (G4double) m[U].pKnots[GetUnbrCtrlPts()];
00445 }
00446 
00447 inline G4double G4NURBS::GetVmin() const { 
00448   return (G4double) m[V].pKnots[GetVorder()-1];
00449 }
00450 
00451 inline G4double G4NURBS::GetVmax() const {
00452   return (G4double) m[V].pKnots[GetVnbrCtrlPts()];
00453 }
00454 
00455 inline G4int G4NURBS::Getorder(G4NURBS::t_direction in_dir) const {
00456   return m[in_dir & DMask].order;
00457 }
00458 
00459 inline G4int G4NURBS::GetnbrKnots(G4NURBS::t_direction in_dir) const {
00460   return m[in_dir & DMask].nbrKnots;
00461 }
00462 
00463 inline G4int G4NURBS::GetnbrCtrlPts(G4NURBS::t_direction in_dir) const {
00464   return m[in_dir & DMask].nbrCtrlPts;
00465 } 
00466 
00467 inline char G4NURBS::Tochar(G4NURBS::t_direction in_dir) {
00468   return (in_dir != U? 'V': 'U');
00469 }
00470 
00471 /***********************************************************************
00472  *                                                                     *
00473  * inline code for protected functions                                 *
00474  *                                                                     *
00475  ***********************************************************************/
00476 
00477 // convert two dim. index to one dim.
00478 //( Ctrl Pts are stored U increasing first )
00479 // no check.
00480 inline G4NURBS::t_indCtrlPt
00481 G4NURBS::To1d(t_inddCtrlPt in_Uindex, t_inddCtrlPt in_Vindex) const
00482 {
00483   return in_Uindex + in_Vindex*m[U].nbrCtrlPts;
00484 }
00485 
00486 // return a float copy
00487 inline G4NURBS::t_floatCtrlPt*
00488 G4NURBS::TofloatCtrlPt(const t_CtrlPt & in_krcp)
00489 {
00490   G4NURBS::t_floatCtrlPt * pcopy = new G4NURBS::t_floatCtrlPt [1];
00491   for (G4int indCoord = X; indCoord < NofC; indCoord++)
00492     (*pcopy)[indCoord] = (G4float)in_krcp[indCoord];
00493   return pcopy;
00494 }
00495                 
00496 // return a double copy
00497 inline G4NURBS::t_doubleCtrlPt* 
00498 G4NURBS::TodoubleCtrlPt(const t_CtrlPt & in_krcp)
00499 {
00500   G4NURBS::t_doubleCtrlPt *  pcopy = new G4NURBS::t_doubleCtrlPt [4];
00501   for (G4int indCoord = X; indCoord < NofC; indCoord++)
00502     (*pcopy)[indCoord] = (G4double)in_krcp[indCoord];
00503   return pcopy;
00504 }
00505 
00506 // MakeKnotVector alias
00507 inline G4bool G4NURBS::MakeKnotVector(G4NURBS::t_Dir * p_inoutdirdat,
00508                              G4NURBS::t_KnotVectorGenFlag in_KVGFlag)
00509 {
00510   return MakeKnotVector(*p_inoutdirdat, in_KVGFlag);
00511 }
00512 
00513 /***********************************************************************
00514  *                                                                     *
00515  * inlines functions to simplify control points definition             *
00516  * see GG4NURBSbox.cc for instance                                     *
00517  *                                                                     *
00518  ***********************************************************************/
00519 
00520 inline void G4NURBS::CP(G4NURBS::t_CtrlPt & rcp,
00521                         t_Coord x, t_Coord y, t_Coord z, t_Coord w)
00522 {
00523   rcp[G4NURBS::X]=x;
00524   rcp[G4NURBS::Y]=y;
00525   rcp[G4NURBS::Z]=z;
00526   rcp[G4NURBS::W]=w;    
00527 }
00528 
00529 // with a common factor
00530 inline void G4NURBS::CP(G4NURBS::t_CtrlPt & rcp, t_Coord x,
00531                         t_Coord y, t_Coord z, t_Coord w, G4Float factor)
00532 {
00533   rcp[G4NURBS::X]=factor*x;
00534   rcp[G4NURBS::Y]=factor*y;
00535   rcp[G4NURBS::Z]=factor*z;
00536   rcp[G4NURBS::W]=factor*w;     
00537 }
00538 
00539 #endif /* end of __C_G4NURBS__ */

Generated on Mon May 27 17:49:07 2013 for Geant4 by  doxygen 1.4.7