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

Last change on this file since 1135 was 1058, checked in by garnier, 15 years ago

file release beta

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