source: trunk/source/graphics_reps/include/HepPolyhedron.h @ 1202

Last change on this file since 1202 was 1140, checked in by garnier, 15 years ago

update to CVS

File size: 18.6 KB
Line 
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: HepPolyhedron.h,v 1.25 2009/10/28 13:38:54 allison Exp $
28// GEANT4 tag $Name:  $
29//
30//
31// Class Description:
32// HepPolyhedron is an intermediate class between description of a shape
33// and visualization systems. It is intended to provide some service like:
34//   - polygonization of shapes with triangulization (quadrilaterization)
35//     of complex polygons;
36//   - calculation of normals for faces and vertices;
37//   - finding result of boolean operation on polyhedra;
38//
39// Public constructors:
40//
41//   HepPolyhedronBox (dx,dy,dz)
42//                                        - create polyhedron for Box;
43//   HepPolyhedronTrd1 (dx1,dx2,dy,dz)
44//                                        - create polyhedron for Trd1;
45//   HepPolyhedronTrd2 (dx1,dx2,dy1,dy2,dz)
46//                                        - create polyhedron for Trd2;
47//   HepPolyhedronTrap (dz,theta,phi, h1,bl1,tl1,alp1, h2,bl2,tl2,alp2)
48//                                        - create polyhedron for Trap;
49//   HepPolyhedronPara (dx,dy,dz,alpha,theta,phi)
50//                                        - create polyhedron for Para;
51//   HepPolyhedronTube (rmin,rmax,dz)
52//                                        - create polyhedron for Tube;
53//   HepPolyhedronTubs (rmin,rmax,dz,phi1,dphi)
54//                                        - create polyhedron for Tubs;
55//   HepPolyhedronCone (rmin1,rmax1,rmin2,rmax2,dz)
56//                                        - create polyhedron for Cone;
57//   HepPolyhedronCons (rmin1,rmax1,rmin2,rmax2,dz,phi1,dphi)
58//                                        - create polyhedron for Cons;
59//   HepPolyhedronPgon (phi,dphi,npdv,nz, z(*),rmin(*),rmax(*))
60//                                        - create polyhedron for Pgon;
61//   HepPolyhedronPcon (phi,dphi,nz, z(*),rmin(*),rmax(*))
62//                                        - create polyhedron for Pcon;
63//   HepPolyhedronSphere (rmin,rmax,phi,dphi,the,dthe)
64//                                        - create polyhedron for Sphere;
65//   HepPolyhedronTorus (rmin,rmax,rtor,phi,dphi)
66//                                        - create polyhedron for Torus;
67//   HepPolyhedronEllipsoid (dx,dy,dz,zcut1,zcut2)
68//                                        - create polyhedron for Ellipsoid;
69// Public functions:
70//
71//   GetNoVertices ()       - returns number of vertices;
72//   GetNoFacets ()         - returns number of faces;
73//   GetNextVertexIndex (index,edgeFlag) - get vertex indeces of the
74//                            quadrilaterals in order;
75//                            returns false when finished each face;
76//   GetVertex (index)      - returns vertex by index;
77//   GetNextVertex (vertex,edgeFlag) - get vertices with edge visibility
78//                            of the quadrilaterals in order;
79//                            returns false when finished each face;
80//   GetNextVertex (vertex,edgeFlag,normal) - get vertices with edge
81//                            visibility and normal of the quadrilaterals
82//                            in order; returns false when finished each face;
83//   GetNextEdgeIndeces (i1,i2,edgeFlag) - get indeces of the next edge;
84//                            returns false for the last edge;
85//   GetNextEdgeIndeces (i1,i2,edgeFlag,iface1,iface2) - get indeces of
86//                            the next edge with indeces of the faces
87//                            to which the edge belongs;
88//                            returns false for the last edge;
89//   GetNextEdge (p1,p2,edgeFlag) - get next edge;
90//                            returns false for the last edge;
91//   GetNextEdge (p1,p2,edgeFlag,iface1,iface2) - get next edge with indeces
92//                            of the faces to which the edge belongs;
93//                            returns false for the last edge;
94//   GetFacet (index,n,nodes,edgeFlags=0,normals=0) - get face by index;
95//   GetNextFacet (n,nodes,edgeFlags=0,normals=0) - get next face with normals
96//                            at the nodes; returns false for the last face;
97//   GetNormal (index)      - get normal of face given by index;
98//   GetUnitNormal (index)  - get unit normal of face given by index;
99//   GetNextNormal (normal) - get normals of each face in order;
100//                            returns false when finished all faces;
101//   GetNextUnitNormal (normal) - get normals of unit length of each face
102//                            in order; returns false when finished all faces;
103//   GetSurfaceArea()       - get surface area of the polyhedron;
104//   GetVolume()            - get volume of the polyhedron;
105//   GetNumberOfRotationSteps()   - get number of steps for whole circle;
106//   SetNumberOfRotationSteps (n) - set number of steps for whole circle;
107//   ResetNumberOfRotationSteps() - reset number of steps for whole circle
108//                            to default value;
109// History:
110//
111// 20.06.96 Evgeni Chernyaev <Evgueni.Tcherniaev@cern.ch> - initial version
112//
113// 23.07.96 John Allison
114// - added GetNoVertices, GetNoFacets, GetNextVertex, GetNextNormal
115//
116// 30.09.96 E.Chernyaev
117// - added GetNextVertexIndex, GetVertex by Yasuhide Sawada
118// - added GetNextUnitNormal, GetNextEdgeIndeces, GetNextEdge
119// - improvements: angles now expected in radians
120//                 int -> G4int, double -> G4double 
121// - G4ThreeVector replaced by either G4Point3D or G4Normal3D
122//
123// 15.12.96 E.Chernyaev
124// - private functions G4PolyhedronAlloc, G4PolyhedronPrism renamed
125//   to AllocateMemory and CreatePrism
126// - added private functions GetNumberOfRotationSteps, RotateEdge,
127//   RotateAroundZ, SetReferences
128// - rewritten G4PolyhedronCons;
129// - added G4PolyhedronPara, ...Trap, ...Pgon, ...Pcon, ...Sphere, ...Torus,
130//   so full List of implemented shapes now looks like:
131//   BOX, TRD1, TRD2, TRAP, TUBE, TUBS, CONE, CONS, PARA, PGON, PCON,
132//   SPHERE, TORUS
133//
134// 01.06.97 E.Chernyaev
135// - RotateAroundZ modified and SetSideFacets added to allow Rmin=Rmax
136//   in bodies of revolution
137//
138// 24.06.97 J.Allison
139// - added static private member fNumberOfRotationSteps and static public
140//   functions void SetNumberOfRotationSteps (G4int n) and
141//   void ResetNumberOfRotationSteps ().  Modified
142//   GetNumberOfRotationSteps() appropriately.  Made all three functions
143//   inline (at end of this .hh file).
144//   Usage:
145//    G4Polyhedron::SetNumberOfRotationSteps
146//     (fpView -> GetViewParameters ().GetNoOfSides ());
147//    pPolyhedron = solid.CreatePolyhedron ();
148//    G4Polyhedron::ResetNumberOfRotationSteps ();
149//
150// 19.03.00 E.Chernyaev
151// - added boolean operations (add, subtract, intersect) on polyhedra;
152//
153// 25.05.01 E.Chernyaev
154// - added GetSurfaceArea() and GetVolume();
155//
156// 05.11.02 E.Chernyaev
157// - added createTwistedTrap() and createPolyhedron();
158//
159// 06.03.05 J.Allison
160// - added IsErrorBooleanProcess
161//
162// 20.06.05 G.Cosmo
163// - added HepPolyhedronEllipsoid
164//
165// 21.10.09 J.Allison
166// - removed IsErrorBooleanProcess (now error is returned through argument)
167//
168
169#ifndef HEP_POLYHEDRON_HH
170#define HEP_POLYHEDRON_HH
171
172#include <CLHEP/Geometry/Point3D.h>
173#include <CLHEP/Geometry/Normal3D.h>
174
175#ifndef DEFAULT_NUMBER_OF_STEPS
176#define DEFAULT_NUMBER_OF_STEPS 24
177#endif
178
179class G4Facet {
180  friend class HepPolyhedron;
181  friend std::ostream& operator<<(std::ostream&, const G4Facet &facet);
182
183 private:
184  struct G4Edge { int v,f; };
185  G4Edge edge[4];
186
187 public:
188  G4Facet(int v1=0, int f1=0, int v2=0, int f2=0, 
189          int v3=0, int f3=0, int v4=0, int f4=0)
190  { edge[0].v=v1; edge[0].f=f1; edge[1].v=v2; edge[1].f=f2;
191    edge[2].v=v3; edge[2].f=f3; edge[3].v=v4; edge[3].f=f4; }
192};
193
194class HepPolyhedron {
195  friend std::ostream& operator<<(std::ostream&, const HepPolyhedron &ph);
196
197 protected:
198  static int fNumberOfRotationSteps;
199  int nvert, nface;
200  HepGeom::Point3D<double> *pV;
201  G4Facet    *pF;
202
203  // Re-allocate memory for HepPolyhedron
204  void AllocateMemory(int Nvert, int Nface);
205
206  // Find neighbouring facet
207  int FindNeighbour(int iFace, int iNode, int iOrder) const;
208
209  // Find normal at node
210  HepGeom::Normal3D<double> FindNodeNormal(int iFace, int iNode) const;
211
212  // Create HepPolyhedron for prism with quadrilateral base
213  void CreatePrism();
214
215  // Generate facets by revolving an edge around Z-axis
216  void RotateEdge(int k1, int k2, double r1, double r2,
217                  int v1, int v2, int vEdge,
218                  bool ifWholeCircle, int ns, int &kface);
219
220  // Set side facets for the case of incomplete rotation
221  void SetSideFacets(int ii[4], int vv[4],
222                     int *kk, double *r,
223                     double dphi, int ns, int &kface);
224
225  // Create HepPolyhedron for body of revolution around Z-axis
226  void RotateAroundZ(int nstep, double phi, double dphi,
227                     int np1, int np2,
228                     const double *z, double *r,
229                     int nodeVis, int edgeVis);
230
231  // For each edge set reference to neighbouring facet
232  void SetReferences();
233
234  // Invert the order on nodes in facets
235  void InvertFacets();
236
237 public:
238  // Constructor
239  HepPolyhedron() : nvert(0), nface(0), pV(0), pF(0) {}
240
241  // Copy constructor
242  HepPolyhedron(const HepPolyhedron & from);
243
244  // Destructor
245  virtual ~HepPolyhedron() { delete [] pV; delete [] pF; }
246
247  // Assignment
248  HepPolyhedron & operator=(const HepPolyhedron & from);
249
250  // Get number of vertices
251  int GetNoVertices() const { return nvert; }
252
253  // Get number of facets
254  int GetNoFacets() const { return nface; }
255
256  // Transform the polyhedron
257  HepPolyhedron & Transform(const HepGeom::Transform3D & t);
258
259  // Get next vertex index of the quadrilateral
260  bool GetNextVertexIndex(int & index, int & edgeFlag) const;
261
262  // Get vertex by index
263  HepGeom::Point3D<double> GetVertex(int index) const;
264
265  // Get next vertex + edge visibility of the quadrilateral
266  bool GetNextVertex(HepGeom::Point3D<double> & vertex, int & edgeFlag) const;
267
268  // Get next vertex + edge visibility + normal of the quadrilateral
269  bool GetNextVertex(HepGeom::Point3D<double> & vertex, int & edgeFlag,
270                     HepGeom::Normal3D<double> & normal) const;
271
272  // Get indeces of the next edge with indeces of the faces
273  bool GetNextEdgeIndeces(int & i1, int & i2, int & edgeFlag,
274                          int & iface1, int & iface2) const;
275
276  // Get indeces of the next edge
277  bool GetNextEdgeIndeces(int & i1, int & i2, int & edgeFlag) const;
278
279  // Get next edge
280  bool GetNextEdge(HepGeom::Point3D<double> &p1,
281                   HepGeom::Point3D<double> &p2, int &edgeFlag) const;
282
283  // Get next edge
284  bool GetNextEdge(HepGeom::Point3D<double> &p1,
285                   HepGeom::Point3D<double> &p2, int &edgeFlag,
286                   int &iface1, int &iface2) const;
287
288  // Get face by index
289  void GetFacet(int iFace, int &n, int *iNodes,
290                int *edgeFlags = 0, int *iFaces = 0) const;
291
292  // Get face by index
293  void GetFacet(int iFace, int &n, HepGeom::Point3D<double> *nodes,
294                int *edgeFlags=0, HepGeom::Normal3D<double> *normals=0) const;
295
296  // Get next face with normals at the nodes
297  bool GetNextFacet(int &n, HepGeom::Point3D<double> *nodes, int *edgeFlags=0,
298                    HepGeom::Normal3D<double> *normals=0) const;
299
300  // Get normal of the face given by index
301  HepGeom::Normal3D<double> GetNormal(int iFace) const;
302
303  // Get unit normal of the face given by index
304  HepGeom::Normal3D<double> GetUnitNormal(int iFace) const;
305
306  // Get normal of the next face
307  bool GetNextNormal(HepGeom::Normal3D<double> &normal) const;
308
309  // Get normal of unit length of the next face
310  bool GetNextUnitNormal(HepGeom::Normal3D<double> &normal) const;
311
312  // Boolean operations
313  HepPolyhedron add(const HepPolyhedron &p) const;
314  HepPolyhedron subtract(const HepPolyhedron &p) const;
315  HepPolyhedron intersect(const HepPolyhedron &p) const;
316
317  // Get area of the surface of the polyhedron
318  double GetSurfaceArea() const;
319
320  // Get volume of the polyhedron
321  double GetVolume() const;
322
323  // Get number of steps for whole circle
324  static int GetNumberOfRotationSteps();
325
326  // Set number of steps for whole circle
327  static void SetNumberOfRotationSteps(int n);
328
329  // Reset number of steps for whole circle to default value
330  static void ResetNumberOfRotationSteps();
331
332  /**
333   * Creates polyhedron for twisted trapezoid.
334   * The trapezoid is given by two bases perpendicular to the z-axis.
335   *
336   * @param  Dz  half length in z
337   * @param  xy1 1st base (at z = -Dz)
338   * @param  xy2 2nd base (at z = +Dz)
339   * @return status of the operation - is non-zero in case of problem
340   */
341  int createTwistedTrap(double Dz,
342                        const double xy1[][2], const double xy2[][2]);
343
344  /**
345   * Creates user defined polyhedron.
346   * This function allows to the user to define arbitrary polyhedron.
347   * The faces of the polyhedron should be either triangles or planar
348   * quadrilateral. Nodes of a face are defined by indexes pointing to
349   * the elements in the xyz array. Numeration of the elements in the
350   * array starts from 1 (like in fortran). The indexes can be positive
351   * or negative. Negative sign means that the corresponding edge is
352   * invisible. The normal of the face should be directed to exterior
353   * of the polyhedron.
354   *
355   * @param  Nnodes number of nodes
356   * @param  Nfaces number of faces
357   * @param  xyz    nodes
358   * @param  faces  faces (quadrilaterals or triangles)
359   * @return status of the operation - is non-zero in case of problem
360   */
361  int createPolyhedron(int Nnodes, int Nfaces,
362                       const double xyz[][3], const int faces[][4]);
363};
364
365class HepPolyhedronTrd2 : public HepPolyhedron
366{
367 public:
368  HepPolyhedronTrd2(double Dx1, double Dx2,
369                    double Dy1, double Dy2, double Dz);
370  virtual ~HepPolyhedronTrd2();
371};
372
373class HepPolyhedronTrd1 : public HepPolyhedronTrd2
374{
375 public:
376  HepPolyhedronTrd1(double Dx1, double Dx2,
377                    double Dy, double Dz);
378  virtual ~HepPolyhedronTrd1();
379};
380
381class HepPolyhedronBox : public HepPolyhedronTrd2
382{
383 public:
384  HepPolyhedronBox(double Dx, double Dy, double Dz);
385  virtual ~HepPolyhedronBox();
386};
387
388class HepPolyhedronTrap : public HepPolyhedron
389{
390 public:
391  HepPolyhedronTrap(double Dz, double Theta, double Phi,
392                    double Dy1,
393                    double Dx1, double Dx2, double Alp1,
394                    double Dy2,
395                    double Dx3, double Dx4, double Alp2);
396  virtual ~HepPolyhedronTrap();
397};
398
399class HepPolyhedronPara : public HepPolyhedronTrap
400{
401 public:
402  HepPolyhedronPara(double Dx, double Dy, double Dz,
403                    double Alpha, double Theta, double Phi);
404  virtual ~HepPolyhedronPara();
405};
406
407class HepPolyhedronParaboloid : public HepPolyhedron
408{
409 public:
410  HepPolyhedronParaboloid(double r1,
411                          double r2,
412                          double dz,
413                          double Phi1,
414                          double Dphi);
415  virtual ~HepPolyhedronParaboloid();
416};
417
418class HepPolyhedronHype : public HepPolyhedron
419{
420 public:
421  HepPolyhedronHype(double r1,
422                    double r2,
423                    double tan1,
424                    double tan2,
425                    double halfZ);
426  virtual ~HepPolyhedronHype();
427};
428
429class HepPolyhedronCons : public HepPolyhedron
430{
431 public:
432  HepPolyhedronCons(double Rmn1, double Rmx1, 
433                    double Rmn2, double Rmx2, double Dz,
434                    double Phi1, double Dphi); 
435  virtual ~HepPolyhedronCons();
436};
437
438class HepPolyhedronCone : public HepPolyhedronCons
439{
440 public:
441  HepPolyhedronCone(double Rmn1, double Rmx1, 
442                    double Rmn2, double Rmx2, double Dz);
443  virtual ~HepPolyhedronCone();
444};
445
446class HepPolyhedronTubs : public HepPolyhedronCons
447{
448 public:
449  HepPolyhedronTubs(double Rmin, double Rmax, double Dz, 
450                    double Phi1, double Dphi);
451  virtual ~HepPolyhedronTubs();
452};
453
454class HepPolyhedronTube : public HepPolyhedronCons
455{
456 public:
457  HepPolyhedronTube (double Rmin, double Rmax, double Dz);
458  virtual ~HepPolyhedronTube();
459};
460
461class HepPolyhedronPgon : public HepPolyhedron
462{
463 public:
464  HepPolyhedronPgon(double phi, double dphi, int npdv, int nz,
465                    const double *z,
466                    const double *rmin,
467                    const double *rmax);
468  virtual ~HepPolyhedronPgon();
469};
470
471class HepPolyhedronPcon : public HepPolyhedronPgon
472{
473 public:
474  HepPolyhedronPcon(double phi, double dphi, int nz,
475                    const double *z,
476                    const double *rmin,
477                    const double *rmax);
478  virtual ~HepPolyhedronPcon();
479};
480
481class HepPolyhedronSphere : public HepPolyhedron
482{
483 public:
484  HepPolyhedronSphere(double rmin, double rmax,
485                      double phi, double dphi,
486                      double the, double dthe);
487  virtual ~HepPolyhedronSphere();
488};
489
490class HepPolyhedronTorus : public HepPolyhedron
491{
492 public:
493  HepPolyhedronTorus(double rmin, double rmax, double rtor,
494                     double phi, double dphi);
495  virtual ~HepPolyhedronTorus();
496};
497
498class HepPolyhedronEllipsoid : public HepPolyhedron
499{
500 public:
501  HepPolyhedronEllipsoid(double dx, double dy, double dz, 
502                         double zcut1, double zcut2);
503  virtual ~HepPolyhedronEllipsoid();
504};
505
506class HepPolyhedronEllipticalCone : public HepPolyhedron
507{
508 public:
509  HepPolyhedronEllipticalCone(double dx, double dy, double z,
510                              double zcut1);
511  virtual ~HepPolyhedronEllipticalCone();
512};
513
514#endif /* HEP_POLYHEDRON_HH */
Note: See TracBrowser for help on using the repository browser.