source: trunk/source/graphics_reps/include/G4NURBS.hh@ 1320

Last change on this file since 1320 was 1228, checked in by garnier, 16 years ago

update geant4.9.3 tag

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